Skip to content

Commit

Permalink
initoverlayfs-install: generic improvements
Browse files Browse the repository at this point in the history
- split extract_initrd_into_initoverlayfs()
- remove unneeded |--* in case statement
- replace initoverlayfs_conf with constant
- remove unused no_kern
- fix some shellcheck warnings

Signed-off-by: Douglas Schilling Landgraf <[email protected]>
  • Loading branch information
dougsland committed Oct 31, 2023
1 parent 7e7f855 commit 047538a
Showing 1 changed file with 94 additions and 52 deletions.
146 changes: 94 additions & 52 deletions bin/initoverlayfs-install
Original file line number Diff line number Diff line change
@@ -1,58 +1,105 @@
#!/bin/bash

set -ex
set -e

extract_initrd_into_initoverlayfs() {
DIR_TO_DUMP_INITRAMFS="/var/tmp/initoverlayfs"
mkdir -p "$DIR_TO_DUMP_INITRAMFS"

file_type=$(file /boot/initramfs-$kver.img)
decompressor="lz4cat"
decompressor_dracut="--lz4"
if [[ "$file_type" == *"ASCII cpio archive (SVR4 with no CRC)"* ]]; then
decompressor_dracut=""
decompressor="zcat"
elif [[ "$file_type" == *"regular file, no read permission"* ]] || \
[[ "$file_type" == *"gzip"* ]]; then
decompressor_dracut=""
decompressor="zcat"
fi

if command -v mkfs.erofs; then
fstype="erofs"
cd "$DIR_TO_DUMP_INITRAMFS"
/usr/lib/dracut/skipcpio /boot/initramfs-$kver.img | $decompressor | cpio -ivd
cd -
rm -f /boot/initoverlayfs-$kver.img
mkfs.erofs /boot/initoverlayfs-$kver.img "$DIR_TO_DUMP_INITRAMFS"
else
echo "Please install mkfs.erofs"
exit 0

fstype="ext4"
dd if=/dev/zero of=/boot/initoverlayfs-$kver.img bs=64M count=1
dev=$(losetup -fP --show /boot/initoverlayfs-$kver.img)
mkfs.$fstype $dev
mount $dev "$DIR_TO_DUMP_INITRAMFS"
cd "$DIR_TO_DUMP_INITRAMFS"
/usr/lib/dracut/skipcpio /boot/initramfs-$kver.img | zstd -d --stdout | cpio -ivd
sync
cd -
while ! umount "$DIR_TO_DUMP_INITRAMFS"; do
# Constants with default value
DECOMPRESSOR="lz4cat"
DECOMPRESSOR_DRACUT="--lz4"

INITOVERLAYFS_CONF="/etc/initoverlayfs.conf"
INITRAMFS_DUMP_DIR="/var/tmp/initoverlayfs"

SUPPORTED_FILESYSTEM=("erofs" "ext4")
SKIPCPIO_BIN="/usr/lib/dracut/skipcpio"

exec_erofs() {
pushd "${INITRAMFS_DUMP_DIR}" || exit
"${SKIPCPIO_BIN}" /boot/initramfs-"${kver}".img | "${DECOMPRESSOR}" | cpio -ivd
popd || exit
rm -f /boot/initoverlayfs-"${kver}".img
mkfs.erofs /boot/initoverlayfs-"${kver}".img ${INITRAMFS_DUMP_DIR}
}

exec_ext4() {
dd if=/dev/zero of=/boot/initoverlayfs-"${kver}".img bs=64M count=1
dev=$(losetup -fP --show /boot/initoverlayfs-"${kver}".img)
mkfs.ext4 "${dev}"
mount "${dev}" "${INITRAMFS_DUMP_DIR}"

pushd "${INITRAMFS_DUMP_DIR}" || exit
"${SKIPCPIO_BIN}" /boot/initramfs-"${kver}".img | zstd -d --stdout | cpio -ivd
sync
popd || exit

while ! umount "${INITRAMFS_DUMP_DIR}"; do
sleep 1
done

losetup -d $dev
fi
losetup -d "${dev}"
}

detect_initramfs() {
if [ ! -d "${INITRAMFS_DUMP_DIR}" ]; then
mkdir -p "${INITRAMFS_DUMP_DIR}"
fi

echo "Extracting initrd into initoverlayfs..."

file_type=$(file /boot/initramfs-"${kver}".img)
case "${file_type}" in
*"ASCII cpio archive (SVR4 with no CRC)"*)
DECOMPRESSOR_DRACUT=""
DECOMPRESSOR="zcat"
;;
*"regular file, no read permission"*|"gzip"*)
DECOMPRESSOR_DRACUT=""
DECOMPRESSOR="zcat"
;;
esac

# Set delimiter for splitting the string
IFS=":"
read -r file_path file_desc <<< "${file_type}"
echo " - File path: ${file_path}"
echo " - File Description: ${file_desc}"
echo " - Decompressor: ${DECOMPRESSOR}"
echo " - Decompressor Dracut: ${DECOMPRESSOR_DRACUT}"
}

extract_initrd_into_initoverlayfs() {
if command -v mkfs.erofs; then
fstype="erofs"
elif command -v mkfs.ext4; then
fstype="ext4"
else
fstype="unsupported"
fi

case "${fstype}" in
*ext4*)
exec_ext4
;;
*erofs*)
exec_erofs
;;
*)
echo -e "The detected filesytem: is ${fstype}." \
"Unfortunately it's not supported at moment."
echo -e "Supported filesystems: ${SUPPORTED_FILESYSTEM[*]}"
exit 1
;;
esac
}

# main()

while [[ $# -gt 0 ]]; do
case $1 in
--kver)
kver="$2"
shift 2
;;
-*|--*)
-*)
echo "Unknown option $1"
exit 1
;;
Expand All @@ -66,22 +113,17 @@ if [ -z "$kver" ]; then
kver="$(uname -r)"
fi

initoverlayfs_conf="/etc/initoverlayfs.conf"
if ! [ -e "$initoverlayfs_conf" ]; then
boot_partition=$(cat /etc/fstab | grep "/boot.*ext4" | awk '{print $1}')
echo -e "bootfs $boot_partition\nbootfstype ext4\ninitoverlayfs_builder dracut -H -f -v -M --reproducible -o \"initoverlayfs\"\ninitrd_builder dracut -H -f -v -M --reproducible -m \"kernel-modules udev-rules initoverlayfs\" -o \"bash systemd systemd-initrd i18n kernel-modules-extra rootfs-block dracut-systemd usrmount base fs-lib microcode_ctl-fw_dir_override shutdown nss-softokn\"\nudev_trigger udevadm trigger --type=devices --action=add --subsystem-match=module --subsystem-match=block --subsystem-match=virtio --subsystem-match=pci --subsystem-match=nvme\n" > $initoverlayfs_conf
fi

no_kern=""
if ! compgen -G /boot/vmlinu* > /dev/null; then
no_kern="--no-kernel"
if ! [ -e "$INITOVERLAYFS_CONF" ]; then
boot_partition=$(< /etc/fstab grep "/boot.*ext4" | awk '{print $1}')
echo -e "bootfs $boot_partition\nbootfstype ext4\ninitoverlayfs_builder dracut -H -f -v -M --reproducible -o \"initoverlayfs\"\ninitrd_builder dracut -H -f -v -M --reproducible -m \"kernel-modules udev-rules initoverlayfs\" -o \"bash systemd systemd-initrd i18n kernel-modules-extra rootfs-block dracut-systemd usrmount base fs-lib microcode_ctl-fw_dir_override shutdown nss-softokn\"\nudev_trigger udevadm trigger --type=devices --action=add --subsystem-match=module --subsystem-match=block --subsystem-match=virtio --subsystem-match=pci --subsystem-match=nvme\n" > $INITOVERLAYFS_CONF
fi

initoverlayfs_builder=$(sed -ne "s/^initoverlayfs_builder\s//pg" "$initoverlayfs_conf")
initoverlayfs_builder=$(sed -ne "s/^initoverlayfs_builder\s//pg" "$INITOVERLAYFS_CONF")
/bin/bash -c "$initoverlayfs_builder"

detect_initramfs
extract_initrd_into_initoverlayfs

initrd_builder=$(sed -ne "s/^initrd_builder\s//pg" "$initoverlayfs_conf")
initrd_builder=$(sed -ne "s/^initrd_builder\s//pg" "$INITOVERLAYFS_CONF")
/bin/bash -c "$initrd_builder"

0 comments on commit 047538a

Please sign in to comment.