You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.7 KiB

Add support for building a squashed initramfs With all files stored in ramfs, and most of them are not compressed, the initramfs will take up a lot of memory. Besides, if the file number is large, each file will waste some memory due to page fragmetation. This is due to ramfs' design, at least one page will be allocated for one file however small the file is. On machine with large page size, this will become worse and waste too many memory. One approach to reducing the memory usage is to reduce the number of files that got directly loaded into the root ramfs, and compress files by put most files will into a read-only squash image and keep a minimum set of executable and libraries outside as the loader for the squash image. After the squash image is mounted, the real 'init' will be executed and then everything behaves as usual. This patch will introduce a '99squash' module which will never be included by default. User can force add it, and if it is included, dracut will perform some extra steps before creating the final image: For now, "/etc" and "/usr" will be moved into the squashfs image. "/init" will be renamed to "/init.stock" and replaced by "/init.squash". Files and folders need to be accessible before mounting the image will be still avaliable at their original place. And due to squashfs is readonly, an overlayfs layer will be created on top of squashfs mount point, as many dracut module require readwrite access to "/etc" and "/usr", "init.squash" will ultimately call "/init.stock". An extra systemd service will be installed. This service will umount all squashfs related mount points right before switch-root to release resources properly. This service will not actually do anything if switch-root is not used. This is very helpful when mem resource is very limited, like Kdump. According to my tests, this squash module can help save about 35MB of memory with 64K page size, or about 15MB with 4K page size on an ordinary kdump capture routine. This module could also help reduce memory usage for normal boot up process. Won't change any behavior if squash module is not enabled. Signed-off-by: Kairui Song <kasong@redhat.com>
7 years ago
#!/bin/bash
check() {
require_binaries mksquashfs unsquashfs || return 1
for i in CONFIG_SQUASHFS CONFIG_BLK_DEV_LOOP CONFIG_OVERLAY_FS; do
if ! check_kernel_config $i; then
dinfo "dracut-squash module requires kernel configuration $i (y or m)"
return 1
fi
done
Add support for building a squashed initramfs With all files stored in ramfs, and most of them are not compressed, the initramfs will take up a lot of memory. Besides, if the file number is large, each file will waste some memory due to page fragmetation. This is due to ramfs' design, at least one page will be allocated for one file however small the file is. On machine with large page size, this will become worse and waste too many memory. One approach to reducing the memory usage is to reduce the number of files that got directly loaded into the root ramfs, and compress files by put most files will into a read-only squash image and keep a minimum set of executable and libraries outside as the loader for the squash image. After the squash image is mounted, the real 'init' will be executed and then everything behaves as usual. This patch will introduce a '99squash' module which will never be included by default. User can force add it, and if it is included, dracut will perform some extra steps before creating the final image: For now, "/etc" and "/usr" will be moved into the squashfs image. "/init" will be renamed to "/init.stock" and replaced by "/init.squash". Files and folders need to be accessible before mounting the image will be still avaliable at their original place. And due to squashfs is readonly, an overlayfs layer will be created on top of squashfs mount point, as many dracut module require readwrite access to "/etc" and "/usr", "init.squash" will ultimately call "/init.stock". An extra systemd service will be installed. This service will umount all squashfs related mount points right before switch-root to release resources properly. This service will not actually do anything if switch-root is not used. This is very helpful when mem resource is very limited, like Kdump. According to my tests, this squash module can help save about 35MB of memory with 64K page size, or about 15MB with 4K page size on an ordinary kdump capture routine. This module could also help reduce memory usage for normal boot up process. Won't change any behavior if squash module is not enabled. Signed-off-by: Kairui Song <kasong@redhat.com>
7 years ago
return 255
}
depends() {
echo "systemd-initrd"
Add support for building a squashed initramfs With all files stored in ramfs, and most of them are not compressed, the initramfs will take up a lot of memory. Besides, if the file number is large, each file will waste some memory due to page fragmetation. This is due to ramfs' design, at least one page will be allocated for one file however small the file is. On machine with large page size, this will become worse and waste too many memory. One approach to reducing the memory usage is to reduce the number of files that got directly loaded into the root ramfs, and compress files by put most files will into a read-only squash image and keep a minimum set of executable and libraries outside as the loader for the squash image. After the squash image is mounted, the real 'init' will be executed and then everything behaves as usual. This patch will introduce a '99squash' module which will never be included by default. User can force add it, and if it is included, dracut will perform some extra steps before creating the final image: For now, "/etc" and "/usr" will be moved into the squashfs image. "/init" will be renamed to "/init.stock" and replaced by "/init.squash". Files and folders need to be accessible before mounting the image will be still avaliable at their original place. And due to squashfs is readonly, an overlayfs layer will be created on top of squashfs mount point, as many dracut module require readwrite access to "/etc" and "/usr", "init.squash" will ultimately call "/init.stock". An extra systemd service will be installed. This service will umount all squashfs related mount points right before switch-root to release resources properly. This service will not actually do anything if switch-root is not used. This is very helpful when mem resource is very limited, like Kdump. According to my tests, this squash module can help save about 35MB of memory with 64K page size, or about 15MB with 4K page size on an ordinary kdump capture routine. This module could also help reduce memory usage for normal boot up process. Won't change any behavior if squash module is not enabled. Signed-off-by: Kairui Song <kasong@redhat.com>
7 years ago
return 0
}
installpost() {
feat(squash): use busybox for early setup if available Use busybox can help reduce the size of early setup environment. With this change, everything is packed in the squash image, and the setup files will be dropped once squash image setup is done, so initramfs stage memory usage is reduced to the minimun, and initramfs decompress is also faster. File layout of a squash initramfs looks like this: ======================================================================== drwxr-xr-x 1 root root 0 Feb 15 14:07 . -rwxr-xr-x 1 root root 946 Feb 15 14:07 init lrwxrwxrwx 1 root root 7 Feb 15 14:07 lib -> usr/lib drwxr-xr-x 1 root root 0 Feb 15 14:07 squash -rw-r--r-- 1 root root 91000832 Feb 15 14:07 squash-root.img drwxr-xr-x 1 root root 0 Feb 15 14:07 usr drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/bin -rwxr-xr-x 1 root root 1293688 Jul 27 2020 usr/bin/busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/echo -> busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/mkdir -> busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/modprobe -> busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/mount -> busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/sh -> busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/switch_root -> busybox drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/lib drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/lib/dracut -rw-r--r-- 1 root root 23 Feb 15 14:07 usr/lib/dracut/build-parameter.txt -rw-r--r-- 1 root root 31 Feb 15 14:07 usr/lib/dracut/dracut-051-93.git20210215.fc33 -rw-r--r-- 1 root root 358 Feb 15 14:07 usr/lib/dracut/modules.txt -rw-r--r-- 1 root root 0 Feb 15 14:07 usr/lib/dracut/need-initqueue drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/lib/modules drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/lib/modules/5.10.11-200.fc33.x86_64 drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel <... kernel module misc files skipped ... > ========================================================================
4 years ago
local _busybox
_busybox=$(find_binary busybox)
# Move everything under $initdir except $squash_dir
# itself into squash image
for i in "$initdir"/*; do
[[ $squash_dir == "$i"/* ]] || mv "$i" "$squash_dir"/
done
# Create mount points for squash loader
mkdir -p "$initdir"/squash/
mkdir -p "$squash_dir"/squash/
# Copy dracut spec files out side of the squash image
# so dracut rebuild and lsinitrd can work
for file in "$squash_dir"/usr/lib/dracut/*; do
[[ -f $file ]] || continue
DRACUT_RESOLVE_DEPS=1 dracutsysrootdir="$squash_dir" inst "${file#$squash_dir}"
done
# Install required modules and binaries for the squash image init script.
feat(squash): use busybox for early setup if available Use busybox can help reduce the size of early setup environment. With this change, everything is packed in the squash image, and the setup files will be dropped once squash image setup is done, so initramfs stage memory usage is reduced to the minimun, and initramfs decompress is also faster. File layout of a squash initramfs looks like this: ======================================================================== drwxr-xr-x 1 root root 0 Feb 15 14:07 . -rwxr-xr-x 1 root root 946 Feb 15 14:07 init lrwxrwxrwx 1 root root 7 Feb 15 14:07 lib -> usr/lib drwxr-xr-x 1 root root 0 Feb 15 14:07 squash -rw-r--r-- 1 root root 91000832 Feb 15 14:07 squash-root.img drwxr-xr-x 1 root root 0 Feb 15 14:07 usr drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/bin -rwxr-xr-x 1 root root 1293688 Jul 27 2020 usr/bin/busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/echo -> busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/mkdir -> busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/modprobe -> busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/mount -> busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/sh -> busybox lrwxrwxrwx 1 root root 7 Feb 15 14:07 usr/bin/switch_root -> busybox drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/lib drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/lib/dracut -rw-r--r-- 1 root root 23 Feb 15 14:07 usr/lib/dracut/build-parameter.txt -rw-r--r-- 1 root root 31 Feb 15 14:07 usr/lib/dracut/dracut-051-93.git20210215.fc33 -rw-r--r-- 1 root root 358 Feb 15 14:07 usr/lib/dracut/modules.txt -rw-r--r-- 1 root root 0 Feb 15 14:07 usr/lib/dracut/need-initqueue drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/lib/modules drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/lib/modules/5.10.11-200.fc33.x86_64 drwxr-xr-x 1 root root 0 Feb 15 14:07 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel <... kernel module misc files skipped ... > ========================================================================
4 years ago
if [[ $_busybox ]]; then
inst "$_busybox" /usr/bin/busybox
for _i in sh echo mount modprobe mkdir switch_root; do
ln_r /usr/bin/busybox /usr/bin/$_i
done
else
DRACUT_RESOLVE_DEPS=1 inst_multiple sh mount modprobe mkdir switch_root
fi
hostonly="" instmods "loop" "squashfs" "overlay"
dracut_kernel_post
# Install squash image init script.
ln -sfn /usr/bin "$initdir/bin"
ln -sfn /usr/sbin "$initdir/sbin"
inst_simple "$moddir"/init-squash.sh /init
}
install() {
if [[ $DRACUT_SQUASH_POST_INST ]]; then
installpost
fi
Add support for building a squashed initramfs With all files stored in ramfs, and most of them are not compressed, the initramfs will take up a lot of memory. Besides, if the file number is large, each file will waste some memory due to page fragmetation. This is due to ramfs' design, at least one page will be allocated for one file however small the file is. On machine with large page size, this will become worse and waste too many memory. One approach to reducing the memory usage is to reduce the number of files that got directly loaded into the root ramfs, and compress files by put most files will into a read-only squash image and keep a minimum set of executable and libraries outside as the loader for the squash image. After the squash image is mounted, the real 'init' will be executed and then everything behaves as usual. This patch will introduce a '99squash' module which will never be included by default. User can force add it, and if it is included, dracut will perform some extra steps before creating the final image: For now, "/etc" and "/usr" will be moved into the squashfs image. "/init" will be renamed to "/init.stock" and replaced by "/init.squash". Files and folders need to be accessible before mounting the image will be still avaliable at their original place. And due to squashfs is readonly, an overlayfs layer will be created on top of squashfs mount point, as many dracut module require readwrite access to "/etc" and "/usr", "init.squash" will ultimately call "/init.stock". An extra systemd service will be installed. This service will umount all squashfs related mount points right before switch-root to release resources properly. This service will not actually do anything if switch-root is not used. This is very helpful when mem resource is very limited, like Kdump. According to my tests, this squash module can help save about 35MB of memory with 64K page size, or about 15MB with 4K page size on an ordinary kdump capture routine. This module could also help reduce memory usage for normal boot up process. Won't change any behavior if squash module is not enabled. Signed-off-by: Kairui Song <kasong@redhat.com>
7 years ago
}