[git commit] switch_root: don't bail out when console doesn't exist

Denys Vlasenko vda.linux at googlemail.com
Fri Mar 24 15:39:08 UTC 2017

commit: https://git.busybox.net/busybox/commit/?id=e3b65ab43d2e2d097a4cd2ee5aa1e1606a8a0663
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

Busybox is very often used in initramfs at the end of which usually
there is a switch_root to the actual rootfs. There are many cases where
the console kernel argument is either just a placeholder (for example
RaspberryPi uses serial0 and serial1) or configured as null to avoid any
console messages - usually you would see such of a setup in production

Currently busybox bails out if can't open the console argument. If this
happenes in initramfs and if the console=null for example, you get in a
blind kernel panic. Avoid this by only warning instead of dying.

function                                             old     new   delta
switch_root_main                                     371     368      -3

Signed-off-by: Andrei Gherzan <andrei at gherzan.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
 util-linux/switch_root.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/util-linux/switch_root.c b/util-linux/switch_root.c
index 6034485..aaee35a 100644
--- a/util-linux/switch_root.c
+++ b/util-linux/switch_root.c
@@ -141,10 +141,12 @@ int switch_root_main(int argc UNUSED_PARAM, char **argv)
 	// If a new console specified, redirect stdin/stdout/stderr to it
 	if (console) {
-		close(0);
-		xopen(console, O_RDWR);
-		xdup2(0, 1);
-		xdup2(0, 2);
+		int fd = open_or_warn(console, O_RDWR);
+		if (fd >= 0) {
+			xmove_fd(fd, 0);
+			xdup2(0, 1);
+			xdup2(0, 2);
+		}
 	// Exec real init

More information about the busybox-cvs mailing list