1 |
commit: 5eaf116d571a17fdc0bd14fdb6761557bc4fa763 |
2 |
Author: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org> |
3 |
AuthorDate: Fri Jun 23 21:51:16 2017 +0000 |
4 |
Commit: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri Jun 23 21:52:20 2017 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/genkernel.git/commit/?id=5eaf116d |
7 |
|
8 |
linuxrc: show why switch_root might fail, and make related code more readable. |
9 |
|
10 |
Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org> |
11 |
|
12 |
defaults/initrd.scripts | 5 +++++ |
13 |
defaults/linuxrc | 19 ++++++++++++++++--- |
14 |
2 files changed, 21 insertions(+), 3 deletions(-) |
15 |
|
16 |
diff --git a/defaults/initrd.scripts b/defaults/initrd.scripts |
17 |
index d00e1c7..efc6421 100644 |
18 |
--- a/defaults/initrd.scripts |
19 |
+++ b/defaults/initrd.scripts |
20 |
@@ -567,6 +567,7 @@ findnfsmount() { |
21 |
else |
22 |
bad_msg "The DHCP Server did not send a valid root-path." |
23 |
bad_msg "Please check your DHCP setup, or provide a nfsroot=<...> parameter." |
24 |
+ return 1 |
25 |
fi |
26 |
fi |
27 |
|
28 |
@@ -590,6 +591,7 @@ findnfsmount() { |
29 |
REAL_ROOT="/dev/nfs" |
30 |
else |
31 |
bad_msg "NFS Mounting failed. Is the path corrent ?" |
32 |
+ return 1 |
33 |
fi |
34 |
else |
35 |
good_msg "Attempting to mount NFS root on ${NFSROOT} with options ${NFSOPTIONS}" |
36 |
@@ -599,12 +601,15 @@ findnfsmount() { |
37 |
REAL_ROOT="/dev/nfs" |
38 |
else |
39 |
bad_msg "NFS Mounting failed. Is the path correct ?" |
40 |
+ return 1 |
41 |
fi |
42 |
# FIXME: Need to start portmap and the other rpc daemons in |
43 |
# order to remount rw. |
44 |
fi |
45 |
|
46 |
fi |
47 |
+ else # IP / DHCP |
48 |
+ return 1 |
49 |
fi |
50 |
} |
51 |
|
52 |
|
53 |
diff --git a/defaults/linuxrc b/defaults/linuxrc |
54 |
index 66f7bd9..b227ed2 100644 |
55 |
--- a/defaults/linuxrc |
56 |
+++ b/defaults/linuxrc |
57 |
@@ -640,6 +640,7 @@ do |
58 |
# Try to mount the device as ${NEW_ROOT} |
59 |
if [ "${REAL_ROOT}" = '/dev/nfs' ]; then |
60 |
findnfsmount |
61 |
+ mountret=$? |
62 |
else |
63 |
# If $REAL_ROOT is a symlink |
64 |
# Resolve it like util-linux mount does |
65 |
@@ -648,15 +649,17 @@ do |
66 |
if [ "${REAL_ROOTFLAGS}" = '' ]; then |
67 |
good_msg "Using mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE} ${REAL_ROOT} ${NEW_ROOT}" |
68 |
mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE} ${REAL_ROOT} ${NEW_ROOT} |
69 |
+ mountret=$? |
70 |
else |
71 |
good_msg "Using mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE},${REAL_ROOTFLAGS} ${REAL_ROOT} ${NEW_ROOT}" |
72 |
mount -t ${ROOTFSTYPE} -o ${MOUNT_STATE},${REAL_ROOTFLAGS} ${REAL_ROOT} ${NEW_ROOT} |
73 |
+ mountret=$? |
74 |
fi |
75 |
fi |
76 |
|
77 |
# If mount is successful break out of the loop |
78 |
# else not a good root and start over. |
79 |
- if [ "$?" = '0' ] |
80 |
+ if [ "$mountret" = '0' ] |
81 |
then |
82 |
if [ -d ${NEW_ROOT}/dev -a -x "${NEW_ROOT}${REAL_INIT:-/sbin/init}" ] || [ "${REAL_ROOT}" = "/dev/nfs" ] |
83 |
then |
84 |
@@ -1037,12 +1040,22 @@ fi |
85 |
rundebugshell "before entering switch_root" |
86 |
|
87 |
# init_opts is set in the environment by the kernel when it parses the command line |
88 |
-exec /sbin/switch_root -c "/dev/console" "${CHROOT}" "${REAL_INIT:-/sbin/init}" ${init_opts} |
89 |
+init=${REAL_INIT:-/sbin/init} |
90 |
+if ! mountpoint "${CHROOT}"; then |
91 |
+ bad_msg "$CHROOT was not a mountpoint" |
92 |
+elif [ ! -x ${CHROOT}/${init} ]; then |
93 |
+ bad_msg "init=${init} does not exist in the rootfs!" |
94 |
+elif [ $$ != 1 ]; then |
95 |
+ bad_msg "PID was not 1! switch_root would fail" |
96 |
+else |
97 |
+ good_msg "Switching to real root: /sbin/switch_root -c /dev/console ${CHROOT} ${init} ${init_opts}" |
98 |
+ exec /sbin/switch_root -c "/dev/console" "${CHROOT}" "${init}" ${init_opts} |
99 |
+fi |
100 |
|
101 |
# If we get here, something bad has happened |
102 |
splash 'verbose' |
103 |
|
104 |
-bad_msg "A fatal error has occured since ${REAL_INIT:-/sbin/init} did not" |
105 |
+bad_msg "A fatal error has occured since ${init} did not" |
106 |
bad_msg "boot correctly. Trying to open a shell..." |
107 |
|
108 |
exec /bin/bash |