commit:     5eaf116d571a17fdc0bd14fdb6761557bc4fa763
Author:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
AuthorDate: Fri Jun 23 21:51:16 2017 +0000
Commit:     Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
CommitDate: Fri Jun 23 21:52:20 2017 +0000
URL:        https://gitweb.gentoo.org/proj/genkernel.git/commit/?id=5eaf116d

linuxrc: show why switch_root might fail, and make related code more readable.

Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org>

 defaults/initrd.scripts |  5 +++++
 defaults/linuxrc        | 19 ++++++++++++++++---
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/defaults/initrd.scripts b/defaults/initrd.scripts
index d00e1c7..efc6421 100644
--- a/defaults/initrd.scripts
+++ b/defaults/initrd.scripts
@@ -567,6 +567,7 @@ findnfsmount() {
                        else
                                bad_msg "The DHCP Server did not send a valid 
root-path."
                                bad_msg "Please check your DHCP setup, or 
provide a nfsroot=<...> parameter."
+                               return 1
                        fi
                fi
 
@@ -590,6 +591,7 @@ findnfsmount() {
                                        REAL_ROOT="/dev/nfs"
                                else
                                        bad_msg "NFS Mounting failed. Is the 
path corrent ?"
+                                       return 1
                                fi
                        else
                                good_msg "Attempting to mount NFS root on 
${NFSROOT} with options ${NFSOPTIONS}"
@@ -599,12 +601,15 @@ findnfsmount() {
                                        REAL_ROOT="/dev/nfs"
                                else
                                        bad_msg "NFS Mounting failed. Is the 
path correct ?"
+                                       return 1
                                fi
                                # FIXME: Need to start portmap and the other 
rpc daemons in
                                # order to remount rw.
                        fi
 
                fi
+       else # IP / DHCP
+               return 1
        fi
 }
 

diff --git a/defaults/linuxrc b/defaults/linuxrc
index 66f7bd9..b227ed2 100644
--- a/defaults/linuxrc
+++ b/defaults/linuxrc
@@ -640,6 +640,7 @@ do
                # Try to mount the device as ${NEW_ROOT}
                if [ "${REAL_ROOT}" = '/dev/nfs' ]; then
                        findnfsmount
+                       mountret=$?
                else
                        # If $REAL_ROOT is a symlink
                        # Resolve it like util-linux mount does
@@ -648,15 +649,17 @@ do
                        if [ "${REAL_ROOTFLAGS}" = '' ]; then
                                good_msg "Using mount -t ${ROOTFSTYPE} -o 
${MOUNT_STATE} ${REAL_ROOT} ${NEW_ROOT}"
                                mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE} 
${REAL_ROOT} ${NEW_ROOT}
+                               mountret=$?
                        else
                                good_msg "Using mount -t ${ROOTFSTYPE} -o 
${MOUNT_STATE},${REAL_ROOTFLAGS} ${REAL_ROOT} ${NEW_ROOT}"
                                mount -t ${ROOTFSTYPE} -o 
${MOUNT_STATE},${REAL_ROOTFLAGS} ${REAL_ROOT} ${NEW_ROOT}
+                               mountret=$?
                        fi
                fi
 
                # If mount is successful break out of the loop
                # else not a good root and start over.
-               if [ "$?" = '0' ]
+               if [ "$mountret" = '0' ]
                then
                        if [ -d ${NEW_ROOT}/dev -a -x 
"${NEW_ROOT}${REAL_INIT:-/sbin/init}" ] || [ "${REAL_ROOT}" = "/dev/nfs" ]
                        then
@@ -1037,12 +1040,22 @@ fi
 rundebugshell "before entering switch_root"
 
 # init_opts is set in the environment by the kernel when it parses the command 
line
-exec /sbin/switch_root -c "/dev/console" "${CHROOT}" 
"${REAL_INIT:-/sbin/init}" ${init_opts}
+init=${REAL_INIT:-/sbin/init}
+if ! mountpoint "${CHROOT}"; then
+       bad_msg "$CHROOT was not a mountpoint"
+elif [ ! -x ${CHROOT}/${init} ]; then
+       bad_msg "init=${init} does not exist in the rootfs!"
+elif [ $$ != 1 ]; then
+       bad_msg "PID was not 1! switch_root would fail"
+else
+       good_msg "Switching to real root: /sbin/switch_root -c /dev/console 
${CHROOT} ${init} ${init_opts}"
+       exec /sbin/switch_root -c "/dev/console" "${CHROOT}" "${init}" 
${init_opts}
+fi
 
 # If we get here, something bad has happened
 splash 'verbose'
 
-bad_msg "A fatal error has occured since ${REAL_INIT:-/sbin/init} did not"
+bad_msg "A fatal error has occured since ${init} did not"
 bad_msg "boot correctly. Trying to open a shell..."
 
 exec /bin/bash

Reply via email to