Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the 99squash module #1088

Merged
merged 11 commits into from Mar 9, 2021
Merged

Refactor the 99squash module #1088

merged 11 commits into from Mar 9, 2021

Conversation

ryncsn
Copy link
Contributor

@ryncsn ryncsn commented Feb 11, 2021

Note: this PR depends on systemd/systemd#18124, so I'm not sure when this could be merged, if acceptable.

Current problems

The aim of 99squash module is to reduce the memory usage of initramfs, but there are several problems with the 99squash module:

  1. It try to reuse what is already installed in the initramfs, this make many files not squash-able, left outside of squash image. These files could be large.
  2. The initramfs is doubled compressed, once by the mksquashfs, once upon generate the cpio (Many files are kept outside, so double compress is needed)
  3. The code is complex, and the squash image setup layout is complex.
  4. Performance and memory usage is not optimal due to above reasons.

Changes

With this PR, the 99squash module is changed in following way:

  1. Using busybox as the sole binary for setup the image, (almost) everything is packed in squash image.
  2. Refactor image setup process, in a much simpler way. Now it setup a new root and chroot into instead of overlaying on multiple mount points on existing root. (depend on initrd: add an env variable to accept non-ramfs rootfs systemd/systemd#18124)
  3. Disable initramfs compress when 99squash is enabled. (everything is squashed, no need to compress twice)

Now the 99squash can be considered an alternative way of compressing the initramfs.
Instead of compress the initramfs image, and decompress everything to ram upon boot, 99squash make the intiamfs content compressed, and decompressed upon file access. This have some advantage on performance and memory usage.

Example initamfs content with the new 99squash (and also a lot of other module enabled for testing):

[root@localhost image]# lsinitrd /boot/initramfs-5.10.11-200.fc33.x86_64.img 
Image: /boot/initramfs-5.10.11-200.fc33.x86_64.img: 92M
========================================================================
Early CPIO image
========================================================================
drwxr-xr-x   1 root     root            0 Feb 11 15:51 .
-rw-r--r--   1 root     root            2 Feb 11 15:51 early_cpio
drwxr-xr-x   1 root     root            0 Feb 11 15:51 kernel
drwxr-xr-x   1 root     root            0 Feb 11 15:51 kernel/x86
drwxr-xr-x   1 root     root            0 Feb 11 15:51 kernel/x86/microcode
-rw-r--r--   1 root     root        30546 Feb 11 15:51 kernel/x86/microcode/AuthenticAMD.bin
-rw-r--r--   1 root     root      3620864 Feb 11 15:51 kernel/x86/microcode/GenuineIntel.bin
========================================================================
Version: dracut-051-76.git20210211.fc33

Arguments: --add 'squash' --force

dracut modules:
bash
systemd
systemd-initrd
modsign
nss-softokn
rdma
rngd
i18n
network-manager
network
ifcfg
drm
plymouth
bcache
btrfs
crypt
dm
dmraid
kernel-modules
kernel-modules-extra
kernel-network-modules
lvm
mdraid
nvdimm
qemu
qemu-net
cifs
iscsi
lunmask
nfs
resume
rootfs-block
terminfo
udev-rules
virtfs
dracut-systemd
usrmount
base
fs-lib
memstrack
shutdown
squash
========================================================================
drwxr-xr-x   1 root     root            0 Feb 11 15:51 .
-rwxr-xr-x   1 root     root         1281 Feb 11 15:51 init
lrwxrwxrwx   1 root     root            7 Feb 11 15:51 lib -> usr/lib
drwxr-xr-x   1 root     root            0 Feb 11 15:51 squash
-rw-r--r--   1 root     root     90984448 Feb 11 15:51 squash-root.img
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr/bin
-rwxr-xr-x   1 root     root      1293688 Jul 27  2020 usr/bin/busybox
lrwxrwxrwx   1 root     root            7 Feb 11 15:51 usr/bin/cat -> busybox
lrwxrwxrwx   1 root     root            7 Feb 11 15:51 usr/bin/echo -> busybox
lrwxrwxrwx   1 root     root            7 Feb 11 15:51 usr/bin/mkdir -> busybox
lrwxrwxrwx   1 root     root            7 Feb 11 15:51 usr/bin/modprobe -> busybox
lrwxrwxrwx   1 root     root            7 Feb 11 15:51 usr/bin/mount -> busybox
lrwxrwxrwx   1 root     root            7 Feb 11 15:51 usr/bin/rm -> busybox
lrwxrwxrwx   1 root     root            7 Feb 11 15:51 usr/bin/sh -> busybox
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr/lib
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr/lib/dracut
-rw-r--r--   1 root     root           23 Feb 11 15:51 usr/lib/dracut/build-parameter.txt
-rw-r--r--   1 root     root           31 Feb 11 15:51 usr/lib/dracut/dracut-051-76.git20210211.fc33
-rw-r--r--   1 root     root          358 Feb 11 15:51 usr/lib/dracut/modules.txt
-rw-r--r--   1 root     root            0 Feb 11 15:51 usr/lib/dracut/need-initqueue
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr/lib/modules
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel/drivers
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel/drivers/block
-rw-r--r--   1 root     root        15760 Jan 28 04:41 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel/drivers/block/loop.ko.xz
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel/fs
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel/fs/overlayfs
-rw-r--r--   1 root     root        53288 Jan 28 04:41 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel/fs/overlayfs/overlay.ko.xz
drwxr-xr-x   1 root     root            0 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel/fs/squashfs
-rw-r--r--   1 root     root        25476 Jan 28 04:41 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel/fs/squashfs/squashfs.ko.xz
-rw-r--r--   1 root     root          185 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.alias
-rw-r--r--   1 root     root          198 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.alias.bin
-rw-r--r--   1 root     root         9865 Jan 28 04:35 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.builtin
-rw-r--r--   1 root     root            0 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.builtin.alias.bin
-rw-r--r--   1 root     root        12385 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.builtin.bin
-rw-r--r--   1 root     root          103 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.dep
-rw-r--r--   1 root     root          192 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.dep.bin
-rw-r--r--   1 root     root           78 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.devname
-rw-r--r--   1 root     root       151428 Jan 28 04:35 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.order
-rw-r--r--   1 root     root           55 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.softdep
-rw-r--r--   1 root     root          133 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.symbols
-rw-r--r--   1 root     root          110 Feb 11 15:51 usr/lib/modules/5.10.11-200.fc33.x86_64/modules.symbols.bin
========================================================================

Note all initramfs content is packed in squash-root.img, and consume 92M of RAM during boot (previously 180M).

This is mostly used for kdump, and the kdump test result is also improved compared to previous implementation.

Checklist

  • [ x ] I have tested it locally
  • I have reviewed and updated any documentation if relevant
  • I am providing new code and test(s) for it

Fixes #

@ryncsn ryncsn changed the title Redo the 99squash module Refactor the 99squash module Feb 11, 2021
@github-actions github-actions bot added modules Issue tracker for all modules needs review squash Issues related to the squash module labels Feb 11, 2021
Copy link
Collaborator

@johannbg johannbg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the scope of this change where you seem to be making the assumption that no one is using the squash module unless she/he is using buxybox as well. which I do believe is not the case here, it's probably best if these changes are introduced in a separated module ( squash-busybox ) and or separated in existing module depending on the busybox module being included or not.

@ryncsn
Copy link
Contributor Author

ryncsn commented Feb 11, 2021

Given the scope of this change where you seem to be making the assumption that no one is using the squash module unless she/he is using buxybox as well. which I do believe is not the case here, it's probably best if these changes are introduced in a separated module ( squash-busybox ) and or separated in existing module depending on the busybox module being included or not.

That's a good suggestion, I'm just not sure if it worth keep two version of 99squash, the new approach is superior in every way except it depends on busybox.

Anyway, I'll do a rebase as there are some conflicts, and try if I can introduce a squash-busybox, and still keep things clean.

@haraldh
Copy link
Collaborator

haraldh commented Feb 15, 2021

Do we need a systemd version check for SYSTEMD_IN_INITRD=lenient, so it does not cause any data loss?

@ryncsn
Copy link
Contributor Author

ryncsn commented Feb 15, 2021

Do we need a systemd version check for SYSTEMD_IN_INITRD=lenient, so it does not cause any data loss?

I think that's better be done in the specfile, bump the required systemd version for dracut-squash package, but have to wait until a new systemd release.

Without SYSTEMD_IN_INITRD=lenient support, this PR will not cause data loss, just fail to boot as systemd refuse to recognize the initrd and refuse to reach initrd.target.

@ryncsn
Copy link
Contributor Author

ryncsn commented Feb 15, 2021

I've updated the PR, now busybox is a weak dependency, 99squash can work without it, and still keep things simple.

@haraldh
Copy link
Collaborator

haraldh commented Feb 15, 2021

sorry, the latest shellcheck PR rendered this PR unmergeable.. Apologies, that you will have to rebase.

@ryncsn
Copy link
Contributor Author

ryncsn commented Feb 15, 2021

Rebased and check the new change with shellcheck, now it looks good.

@johannbg
Copy link
Collaborator

@ryncsn you need to fix your commit messages so they pass the commisery check.
They should all start with fix(squash): ( we dont use number is modules since the numbers are subjected to change )
And don't use title case in the description.

Good
fix(squash): move the post install scripts into the module-setup.sh
Bad
fix(squash): Move the post install scripts into the module-setup.sh

dracut.sh Outdated Show resolved Hide resolved
dracut.sh Outdated Show resolved Hide resolved
dracut.sh Outdated Show resolved Hide resolved
dracut.spec Outdated Show resolved Hide resolved
modules.d/99squash/module-setup.sh Outdated Show resolved Hide resolved
modules.d/99squash/init-squash.sh Outdated Show resolved Hide resolved
@ryncsn
Copy link
Contributor Author

ryncsn commented Feb 21, 2021

@ryncsn you need to fix your commit messages so they pass the commisery check.
They should all start with fix(squash): ( we dont use number is modules since the numbers are subjected to change )
And don't use title case in the description.

Good
fix(squash): move the post install scripts into the module-setup.sh
Bad
fix(squash): Move the post install scripts into the module-setup.sh

Thanks, I'll fix these issues.

Any other shell should just work.
@johannbg
Copy link
Collaborator

@ryncsn you still need to fix your commit message to pass the commisery check.
Change this commit msg ( contains a title case in the description )
refactor(squash): Move the post install scripts into the module-setup.sh
to this
refactor(squash): move the post install scripts into the module-setup.sh
and this ( is repeating the type tag )
refactor(squash): refactor in a cleaner way
to this
refactor(squash): structure in a cleaner way
and it should pass the check.

@ryncsn
Copy link
Contributor Author

ryncsn commented Feb 25, 2021

Yes, I just notices that. Is there anyway I can do the commisery check locally before pushing it?

@johannbg
Copy link
Collaborator

johannbg commented Feb 25, 2021

Yes, I just notices that. Is there anyway I can do the commisery check locally before pushing it?

There are instructions here [1] but in most cases people ( including myself ) have done the same mistakes you just did and once you get passed that + avoid including the module number you should be fine on your PR to dracut.

The failure messages of the commisery check is pretty self explanatory, if you just read and fix based on the output of the error msg of test itself, you dont need to install/setup anything locally.

  1. https://github.com/tomtom-international/commisery

No function change, just move the post install code to 99squash to clean
up dracut.sh.
Ensure dracut squash module doesn't effect other steps, and stripping
can cover the new binaries installed by it.
Seperate of init.sh and setup-squash.sh is pointless, merge them into one.
Separately install the modules required for squash image setup.
These modules can be deleted after squash image setup to save
memory.

Signed-off-by: Kairui Song <kasong@redhat.com>
The squasn mount points are recorded in text file so later clean up
script can umount them, this is not needed, the mount points are fixed
so just detect the umount by hardcoded pattern.
modules.d/99squash/module-setup.sh Outdated Show resolved Hide resolved
Simplify the squash mount layout. Instead of overlay on each top
directory (/etc, /usr), just mount and switch_root into the squash
image, with a overlay on top of it.

Also install the binaries and setup scripts separately, so the squash
setup code and the squash image content is independent of each other,
all squash setup script and binaries can be deleted safely upon
switch_root.

With this change, previous squash clean up service and other tricky
implementations are all gone.

This commit depends on systemd commits from:
systemd/systemd#18124

Previouly systemd doesn't recognize non-ramfs initramfs, now this is
doable with SYSTEMD_IN_INITRD=lenient

Signed-off-by: Kairui Song <kasong@redhat.com>
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 ... >
========================================================================
No function change, decide which compressor to use right before
compressing the initramfs.

This may delay the print of this message:
"dracut: no compression tool available. Initramfs image is going to be big."
but should be OK, this message is not an error.
Try to make mksquashfs follow --compress option if squash module is
included, if not specified or invalid, fall back to use mksquashfs's
default compressor.
With squash module, the initramfs is double compressed, which slow down
the build progress and doesn't shrink the size much.
Copy link
Collaborator

@johannbg johannbg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

dracut.sh Show resolved Hide resolved
dracut.sh Show resolved Hide resolved
@johannbg johannbg enabled auto-merge (rebase) March 9, 2021 13:39
@johannbg johannbg merged commit 7c0bc0b into dracutdevs:master Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
modules Issue tracker for all modules squash Issues related to the squash module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants