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

Is it a good idea to compress rootfs in live-iso using zstd instead of gzip? #820

Open
hksdpc255 opened this issue Dec 25, 2024 · 4 comments

Comments

@hksdpc255
Copy link

Is your feature request related to a problem? Please describe.
There is a hard-coded gzip compression at line 95:

args := []string{
"mksquashfs",
tmpPaths[clrRootfs],
tmpPaths[clrCdroot] + "/images/rootfs.img",
"-b",
"131072",
"-comp",
"gzip",
"-e",
"boot/",
"-e",
"proc/",
"-e",
"sys/",
"-e",
"dev/",
"-e",
"run/",
}

Lots of benchmarks shows that zstd decompresion speed is much faster than gzip even using compression level 22:
https://gist.github.com/baryluk/70a99b5f26df4671378dd05afef97fce

Describe the solution you'd like
Zstd with compression level 22 will make the rootfs.img smaller but it will consume more memory when the kernel decompress it.

	args := []string{
		"mksquashfs",
		tmpPaths[clrRootfs],
		tmpPaths[clrCdroot] + "/images/rootfs.img",
		"-b",
		"131072",
		"-comp",
		"zstd",
		"-Xcompression-level",
		"22",
		"-e",
		"boot/",
		"-e",
		"proc/",
		"-e",
		"sys/",
		"-e",
		"dev/",
		"-e",
		"run/",
	}

Describe alternatives you've considered
Zstd with compression level 19 will make the rootfs.img small and fast, consuming resonable memory.

	args := []string{
		"mksquashfs",
		tmpPaths[clrRootfs],
		tmpPaths[clrCdroot] + "/images/rootfs.img",
		"-b",
		"131072",
		"-comp",
		"zstd",
		"-Xcompression-level",
		"19",
		"-e",
		"boot/",
		"-e",
		"proc/",
		"-e",
		"sys/",
		"-e",
		"dev/",
		"-e",
		"run/",
	}

Additional context
Currently, zstd support for mksquashfs in ClearLinux seems broken. clearlinux/distribution#3235
But I still believe compress rootfs using zstd is good for both performance and disk usage.

@fenrus75
Copy link

fenrus75 commented Dec 25, 2024 via email

@hksdpc255
Copy link
Author

I successfully interposed a custom shell script wrapper for /bin/mksquashfs to modify the compression parameters to zstd. The generated Live-ISO boot sequence completed successfully without any observable issues.

@hksdpc255
Copy link
Author

I noticed that the initrd located in Live-ISO is gzip-compressed.

@hksdpc255
Copy link
Author

hksdpc255 commented Feb 21, 2025

Zstd-compressed initrd and squashfs tested with this post-install script:

#!/bin/sh -ex

[ -f /bin/xorriso.bak ] || mv /bin/xorriso{,.bak}
cat << 'EOF' > /bin/xorriso
#!/bin/sh -ex

for arg in "$@" ; do
    [ -f "$arg/EFI/BOOT/initrd.gz" ] && [ -f "$arg/EFI/efiboot.img" ] && [ -f "$arg/loader/entries/iso-checksum.conf" ] && [ -f "$arg/loader/entries/Clear-"*".conf" ] && [ -f "$arg/isolinux/isolinux.cfg" ] || continue
    gzip -d < "$arg/EFI/BOOT/initrd.gz" | zstd -19 > "$arg/EFI/BOOT/initrd.zst"
    rm "$arg/EFI/BOOT/initrd.gz"
    mcopy -spmi "$arg/EFI/efiboot.img" :: "$arg/EFI/efiboot.dir"
    rm "$arg/EFI/efiboot.img" "$arg/EFI/efiboot.dir/EFI/BOOT/initrd.gz"
    cp "$arg/EFI/BOOT/initrd.zst" "$arg/EFI/efiboot.dir/EFI/BOOT/initrd.zst"
    sed -i 's@ /EFI/BOOT/initrd.gz@ /EFI/BOOT/initrd.zst@g' "$arg/EFI/efiboot.dir/loader/entries/"*.conf "$arg/loader/entries/"*.conf "$arg/isolinux/isolinux.cfg"
    fallocate -l "$(du -d 0 -B 4K "$arg/EFI/efiboot.dir" | awk '{print int(($1+255)/256)+1;exit}')M" "$arg/EFI/efiboot.img"
    mkfs.fat -n CLEAR_EFI "$arg/EFI/efiboot.img"
    mcopy -spmi "$arg/EFI/efiboot.img" "$arg/EFI/efiboot.dir/"* ::
    rm -r "$arg/EFI/efiboot.dir"
done
xorriso.bak -md5 all "$@" -J -R -iso-level 3
EOF
chmod +x /bin/xorriso

[ -f /bin/mksquashfs.bak ] || mv /bin/mksquashfs{,.bak}
cat << 'EOF' > /bin/mksquashfs
#!/bin/sh -ex

for i in $(seq $#) ; do
    case "$1" in
        -Xhc|-noI|-noId|-noD|-noF|-noX|-no-compression) shift ;;
        -X*)   shift 2 && set -- -Xhc "$@" ;;
        -comp) shift 2 && set -- -Xhc "$@" -comp zstd -Xcompression-level 19 -b 1048576 ;;
        -b)    shift 2 && set -- -Xhc "$@" ;;
        *)     set -- "$@" "$1" && shift ;;
    esac
done
mksquashfs.bak "$@"
EOF
chmod +x /bin/mksquashfs

Boot sequence completed successfully.

Remember to run mv /bin/mksquashfs{.bak,} && mv /bin/xorriso{.bak,} after image creation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants