add support for booting from initramfs 79/306979/3
authorMarek Szyprowski <m.szyprowski@samsung.com>
Fri, 1 Mar 2024 10:02:42 +0000 (11:02 +0100)
committerMarek Szyprowski <m.szyprowski@samsung.com>
Fri, 15 Mar 2024 12:20:51 +0000 (13:20 +0100)
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 <m.szyprowski@samsung.com>
Change-Id: I8d89d93ddd1b41e68cb965132abc4a72e2f8950e

data/initrd-file.list
scripts/init.sh

index b392c86..5f059fd 100644 (file)
@@ -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
 "
index d200645..0170748 100755 (executable)
@@ -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