From 24c1c4ddca930405edfa69a4a4b77f15bc6d8bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Thu, 7 Mar 2019 11:35:11 +1100 Subject: [PATCH] improve switch_root --- Dockerfile | 4 ++-- build-layer | 4 +++- main.go | 19 +++++++------------ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0736d71..66d1228 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # ------------------------------------------------------------------------ -from golang:1.11.5-alpine3.8 as build +from golang:1.12.0-alpine3.9 as build add vendor /go/src/init/vendor add *.go /go/src/init/ @@ -8,7 +8,7 @@ workdir /go/src/init run CGO_ENABLED=0 go build -o /layer/init . # ------------------------------------------------------------------------ -from alpine:3.8 +from alpine:3.9 env busybox_v=1.28.1-defconfig-multiarch \ arch=x86_64 diff --git a/build-layer b/build-layer index 709337e..296c18e 100755 --- a/build-layer +++ b/build-layer @@ -1,11 +1,13 @@ #! /bin/sh set -ex -mkdir dev sys proc bin sbin usr usr/bin usr/sbin +mkdir dev mknod dev/null -m 0666 c 1 3 mknod dev/tty -m 0666 c 5 0 mknod dev/console -m 0600 c 5 1 +mkdir sys proc bin sbin usr usr/bin usr/sbin + curl -L -o bin/busybox https://busybox.net/downloads/binaries/$busybox_v/busybox-$arch chmod +x bin/busybox diff --git a/main.go b/main.go index 28380ae..372817d 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "syscall" "time" @@ -33,6 +34,8 @@ type config struct { } func main() { + runtime.LockOSThread() + log.Print("Welcome to ", VERSION) // essential mounts @@ -133,11 +136,9 @@ func main() { // switch root log.Print("switching root") - err = syscall.Exec("/sbin/switch_root", []string{"switch_root", "/system", "/sbin/init"}, os.Environ()) - if err != nil { - log.Print("switch_root failed: ", err) - select {} - } + err = syscall.Exec("/sbin/switch_root", []string{"switch_root", + "-c", "/dev/console", "/system", "/sbin/init"}, os.Environ()) + fatal("switch_root failed: ", err) } func layerPath(name string) string { @@ -147,18 +148,12 @@ func layerPath(name string) string { func fatal(v ...interface{}) { log.Print("*** FATAL ***") log.Print(v...) - dropToShell() + select {} } func fatalf(pattern string, v ...interface{}) { log.Print("*** FATAL ***") log.Printf(pattern, v...) - dropToShell() -} - -func dropToShell() { - err := syscall.Exec("/bin/sh", []string{"/bin/sh"}, os.Environ()) - log.Print("shell drop failed: ", err) select {} }