From 3b4e28288eea4893105e617f46a7ef9f3de512f4 Mon Sep 17 00:00:00 2001 From: Marek Szyprowski Date: Fri, 1 Mar 2024 11:02:42 +0100 Subject: [PATCH] add support for booting from initramfs When system starts from initramfs, the pivot_root syscall fails for the initial tmpfs filesystem and this is a known limitation. Switching to the new rootfs has to be done by the 'switch_root' command in such case. Signed-off-by: Marek Szyprowski Change-Id: I8d89d93ddd1b41e68cb965132abc4a72e2f8950e --- data/initrd-file.list | 4 ++++ scripts/init.sh | 22 ++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/data/initrd-file.list b/data/initrd-file.list index b392c86..5f059fd 100644 --- a/data/initrd-file.list +++ b/data/initrd-file.list @@ -10,6 +10,7 @@ SYS_SKELDIR=" /usr/lib /sysroot /opt +/run " DIR_LINKS=" @@ -25,6 +26,7 @@ VERBATIMS=" PROGS=" /usr/bin/wc /usr/bin/find +/usr/bin/findmnt /usr/bin/bash /usr/bin/echo /usr/bin/xargs @@ -33,6 +35,7 @@ PROGS=" /usr/bin/umount /usr/sbin/blkid /usr/sbin/pivot_root +/usr/sbin/switch_root /usr/bin/chroot /usr/bin/ls /usr/bin/cat @@ -51,4 +54,5 @@ PROGS=" SYM_LINKS=" /bin/sh:bash /etc/mtab:../proc/self/mounts +/init:sbin/init " diff --git a/scripts/init.sh b/scripts/init.sh index d200645..0170748 100755 --- a/scripts/init.sh +++ b/scripts/init.sh @@ -14,6 +14,7 @@ DATAFS_MNT= MODULES_MNT= IN_INITRD=0 +IN_INITRAMFS=0 NEED_RESIZEFS=1 USERFS_ENCRYPTED=0 @@ -209,10 +210,16 @@ function check_flags() { if [ -e /etc/initrd-release ] then - IN_INITRD=1 + if [ "$(findmnt -n --output=fstype /)" != "rootfs" ] + then + IN_INITRD=1 + else + IN_INITRAMFS=1 + fi ROOTFS_MNT="/sysroot" else IN_INITRD=0 + IN_INITRAMFS=0 ROOTFS_MNT="/" fi HALFS_MNT=${ROOTFS_MNT}/hal @@ -246,7 +253,7 @@ function process_rootfs() if [ x$ROOTFS = "x" ] then echo "WARNING : THERE IS NO ROOTFS." - if [ "$IN_INITRD" = "1" ] + if [ "$IN_INITRD" = "1" -o "$IN_INITRAMFS" = "1" ] then exec /bin/sh else @@ -261,7 +268,7 @@ function process_rootfs() if [ "$IN_INITRD" = "1" ] then /sbin/btrfs check $ROOTFS - if [ "$NEED_RESIZEFS" = "1" ] + if [ "$NEED_RESIZEFS" = "1" -o "$IN_INITRAMFS" = "1" ] then /bin/mount $ROOTFS $ROOTFS_MNT -o rw,compress-force=lzo /sbin/btrfs filesystem resize max $ROOTFS_MNT @@ -281,8 +288,8 @@ function process_rootfs() return fi - # check verify boot only in initrd - if [ "$IN_INITRD" = "1" ] + # check verify boot only in initrd/initramfs + if [ "$IN_INITRD" = "1" -o "$IN_INITRAMFS" = "1" ] then /usr/bin/verityctl create rootfs $ROOTFS $ROOTFS_MNT case $? in @@ -312,7 +319,7 @@ function process_rootfs() fi # ext4 partion - if [ "$IN_INITRD" = "1" ] + if [ "$IN_INITRD" = "1" -o "$IN_INITRAMFS" = "1" ] then if [ "$NEED_RESIZEFS" = "1" ] then @@ -454,6 +461,9 @@ function change_root() if [ "$IN_INITRD" = "1" ] then exec chroot . /sbin/init $@ + elif [ "$IN_INITRAMFS" = "1" ] + then + exec switch_root $ROOTFS_MNT /sbin/init $@ else exec /usr/lib/systemd/systemd $@ fi -- 2.7.4