Skip to content

Commit 6ade476

Browse files
committed
feature/runtime-squashfs: New feature to create a squashed initramfs
This feature introduces support for building a squashed initramfs to significantly reduce memory usage during system boot, especially in memory-constrained environments. By consolidating most files into a compressed, read-only squashfs image, the number of files loaded directly into ramfs is minimized, reducing both memory consumption and page fragmentation. Signed-off-by: Alexey Gladkov <[email protected]>
1 parent a4e469f commit 6ade476

File tree

6 files changed

+131
-7
lines changed

6 files changed

+131
-7
lines changed

Makefile.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ ifeq ($(BUILD_OS),linux-musl)
368368
BBCFG_SED_ARGS += -e "s,^CONFIG_FEATURE_VI_REGEX_SEARCH=.*,\# CONFIG_FEATURE_VI_REGEX_SEARCH is not set,"
369369
endif
370370

371+
# We want to have this functionality in busybox, but we don't want symlinks to
372+
# exist because links won't allow us to install other implementations of these
373+
# utilities.
374+
BB_SYMLINK_DISABLE = ash insmod mount
375+
371376
$(BBCFG): $(external_srcdir)/busybox/config
372377
$(Q)mkdir -p -- "$(OBJDIR)/external/busybox"
373378
$(Q)mkdir -p -- $(dest_runtimedir)
@@ -383,6 +388,7 @@ $(dest_data_bindir)/busybox: $(BBCFG)
383388
$(Q)mkdir -p -- "$(dest_data_bindir)" "$(dest_data_sbindir)" "$(dest_data_libdir)"
384389
$(Q)cp -a -- "$(OBJDIR)/external/busybox/busybox" "$@"
385390
$(Q)while read -r busybox_link; do \
391+
for ign in $(BB_SYMLINK_DISABLE); do [ -n "$${busybox_link##*/$$ign}" ] || continue 2; done; \
386392
ln -srf -- "$@" "$(dest_runtimedir)$$busybox_link"; \
387393
done < "$(OBJDIR)/external/busybox/busybox.links"
388394

external/busybox/config

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ CONFIG_SULOGIN=y
570570
#
571571
# CONFIG_MODPROBE_SMALL is not set
572572
# CONFIG_DEPMOD is not set
573-
# CONFIG_INSMOD is not set
573+
CONFIG_INSMOD=y
574574
# CONFIG_LSMOD is not set
575575
# CONFIG_FEATURE_LSMOD_PRETTY_2_6_OUTPUT is not set
576576
# CONFIG_MODINFO is not set
@@ -645,7 +645,7 @@ CONFIG_XXD=y
645645
# CONFIG_IPCS is not set
646646
# CONFIG_LAST is not set
647647
# CONFIG_FEATURE_LAST_FANCY is not set
648-
# CONFIG_LOSETUP is not set
648+
CONFIG_LOSETUP=y
649649
# CONFIG_LSPCI is not set
650650
# CONFIG_LSUSB is not set
651651
CONFIG_MDEV=y
@@ -667,14 +667,14 @@ CONFIG_FEATURE_MDEV_DAEMON=y
667667
# CONFIG_MKSWAP is not set
668668
# CONFIG_FEATURE_MKSWAP_UUID is not set
669669
# CONFIG_MORE is not set
670-
# CONFIG_MOUNT is not set
671-
# CONFIG_FEATURE_MOUNT_FAKE is not set
672-
# CONFIG_FEATURE_MOUNT_VERBOSE is not set
670+
CONFIG_MOUNT=y
671+
CONFIG_FEATURE_MOUNT_FAKE=y
672+
CONFIG_FEATURE_MOUNT_VERBOSE=y
673673
# CONFIG_FEATURE_MOUNT_HELPERS is not set
674674
# CONFIG_FEATURE_MOUNT_LABEL is not set
675675
# CONFIG_FEATURE_MOUNT_NFS is not set
676676
# CONFIG_FEATURE_MOUNT_CIFS is not set
677-
# CONFIG_FEATURE_MOUNT_FLAGS is not set
677+
CONFIG_FEATURE_MOUNT_FLAGS=y
678678
# CONFIG_FEATURE_MOUNT_FSTAB is not set
679679
# CONFIG_FEATURE_MOUNT_OTHERTAB is not set
680680
CONFIG_MOUNTPOINT=y
@@ -1133,7 +1133,7 @@ CONFIG_SH_IS_NONE=y
11331133
# CONFIG_BASH_IS_HUSH is not set
11341134
CONFIG_BASH_IS_NONE=y
11351135
# CONFIG_SHELL_ASH is not set
1136-
# CONFIG_ASH is not set
1136+
CONFIG_ASH=y
11371137
# CONFIG_ASH_OPTIMIZE_FOR_SIZE is not set
11381138
# CONFIG_ASH_INTERNAL_GLOB is not set
11391139
# CONFIG_ASH_BASH_COMPAT is not set
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Feature: Squashed Runtime
2+
3+
## Overview
4+
5+
This feature introduces support for building a **squashed initramfs** to
6+
significantly reduce memory usage during system boot, especially in
7+
memory-constrained environments. By consolidating most files into a compressed,
8+
read-only squashfs image, the number of files loaded directly into ramfs is
9+
minimized, reducing both memory consumption and page fragmentation.
10+
11+
## Background
12+
13+
- **Problem:** Traditional initramfs stores all files in ramfs, which is
14+
uncompressed and allocates at least one page per file, regardless of file
15+
size. This leads to:
16+
- High memory usage, especially with many small files.
17+
- Increased waste on systems with large page sizes due to page fragmentation.
18+
19+
- **Solution:** Move most files into a compressed squashfs image, keeping only a
20+
minimal set of executables and libraries outside as a loader. This reduces the
21+
number of files in ramfs and compresses their contents.
22+
23+
## Parameters
24+
25+
- **RUNTIME_SQUASHFS_COMPRESS** -- The parameter specifies a compression
26+
algorithm (default: `xz`).
27+
- **RUNTIME_SQUASHFS_ARGS** -- Extra arguments for mksquashfs.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash -eu
2+
# SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
. sh-functions
5+
6+
if [ -f "$KERNEL_CONFIG" ]; then
7+
rc=0
8+
for cfgname in CONFIG_SQUASHFS CONFIG_BLK_DEV_LOOP CONFIG_OVERLAY_FS; do
9+
if ! grep -qs "^$cfgname=[my]" "$KERNEL_CONFIG"; then
10+
message "The kernel lacks support for needed parameters: $cfgname"
11+
rc=1
12+
fi
13+
done
14+
[ "$rc" -eq 0 ]
15+
fi
16+
17+
mkdir "$rootdir/squash"
18+
19+
find -P "$rootdir" -type f -execdir touch -c -m --date="1970-01-01 00:00:00 +0000" '{}' '+'
20+
21+
mv -- "$rootdir" "$rootdir.raw"
22+
mkdir -- "$rootdir"
23+
24+
mksquashfs "$rootdir.raw" "$rootdir"/squash.img -quiet -no-progress -no-xattrs \
25+
${RUNTIME_SQUASHFS_ARGS} \
26+
${RUNTIME_SQUASHFS_COMPRESS:+-comp "$RUNTIME_SQUASHFS_COMPRESS"} \
27+
-all-root \
28+
-p '/dev/ram b 0644 0 0 1 1' \
29+
-p '/dev/null c 0666 0 0 1 3' \
30+
-p '/dev/zero c 0666 0 0 1 5' \
31+
-p '/dev/full c 0666 0 0 1 7' \
32+
-p '/dev/random c 0666 0 0 1 8' \
33+
-p '/dev/systty c 0666 0 0 4 0' \
34+
-p '/dev/tty0 c 0666 0 0 4 0' \
35+
-p '/dev/tty1 c 0666 0 0 4 1' \
36+
-p '/dev/tty c 0666 0 0 5 0' \
37+
-p '/dev/console c 0600 0 0 5 1' \
38+
-p '/dev/ptmx c 0666 0 0 5 2' \
39+
#
40+
41+
mkdir -p -- \
42+
"$rootdir"/bin \
43+
"$rootdir"/dev \
44+
"$rootdir"/root \
45+
"$rootdir"/squash \
46+
"$rootdir"/usr \
47+
#
48+
49+
ln -s -- ../bin "$rootdir"/usr/bin
50+
ln -s -- ../sbin "$rootdir"/usr/bin
51+
52+
put-file -r "$rootdir.raw" "$rootdir" "$rootdir.raw"/bin/busybox
53+
54+
cat > "$rootdir"/init <<EOF
55+
#!/bin/busybox ash
56+
EOF
57+
58+
depinfo --set-version="$KERNEL" --no-prefix --no-builtin \
59+
$RUNTIME_SQUASHFS_MODULES |
60+
while read -r modname; do
61+
mkdir -p -- "$rootdir/${modname%/*}"
62+
cp -- "$rootdir.raw/$modname" "$rootdir/$modname"
63+
printf '/bin/busybox insmod %s\n' "$modname"
64+
done >> "$rootdir/init"
65+
66+
cat >> "$rootdir"/init <<EOF
67+
/bin/busybox mknod /dev/loop0 b 7 0
68+
/bin/busybox losetup /dev/loop0 /squash.img
69+
/bin/busybox mount -t ramfs ramfs /squash
70+
/bin/busybox mkdir /squash/root /squash/upper /squash/work
71+
/bin/busybox mount -t squashfs /dev/loop0 /squash/root
72+
/bin/busybox mount -t overlay overlay -o lowerdir=/squash/root,upperdir=/squash/upper,workdir=/squash/work /root
73+
/bin/busybox mount --move /squash /root/squash
74+
exec /bin/busybox switch_root /root /init
75+
EOF
76+
chmod +x "$rootdir"/init
77+
78+
rm -rf -- "$rootdir.raw"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
RUNTIME_SQUASHFS_ARGS ?= -b 1M
4+
RUNTIME_SQUASHFS_COMPRESS ?= xz

features/runtime-squashfs/rules.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
RUNTIME_SQUASHFS_MODULES = loop squashfs overlay
4+
5+
MODULES_ADD += $(RUNTIME_SQUASHFS_MODULES)
6+
7+
pre-pack::
8+
@$(VMSG) "Creating squashfs image ..."
9+
@$(FEATURESDIR)/runtime-squashfs/bin/pack-squashfs

0 commit comments

Comments
 (0)