improve switch_root

This commit is contained in:
Mikaël Cluseau 2019-03-07 11:35:11 +11:00
parent 9b1cf89a05
commit 24c1c4ddca
3 changed files with 12 additions and 15 deletions

View File

@ -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 vendor /go/src/init/vendor
add *.go /go/src/init/ add *.go /go/src/init/
@ -8,7 +8,7 @@ workdir /go/src/init
run CGO_ENABLED=0 go build -o /layer/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 \ env busybox_v=1.28.1-defconfig-multiarch \
arch=x86_64 arch=x86_64

View File

@ -1,11 +1,13 @@
#! /bin/sh #! /bin/sh
set -ex 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/null -m 0666 c 1 3
mknod dev/tty -m 0666 c 5 0 mknod dev/tty -m 0666 c 5 0
mknod dev/console -m 0600 c 5 1 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 curl -L -o bin/busybox https://busybox.net/downloads/binaries/$busybox_v/busybox-$arch
chmod +x bin/busybox chmod +x bin/busybox

19
main.go
View File

@ -7,6 +7,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"syscall" "syscall"
"time" "time"
@ -33,6 +34,8 @@ type config struct {
} }
func main() { func main() {
runtime.LockOSThread()
log.Print("Welcome to ", VERSION) log.Print("Welcome to ", VERSION)
// essential mounts // essential mounts
@ -133,11 +136,9 @@ func main() {
// switch root // switch root
log.Print("switching root") log.Print("switching root")
err = syscall.Exec("/sbin/switch_root", []string{"switch_root", "/system", "/sbin/init"}, os.Environ()) err = syscall.Exec("/sbin/switch_root", []string{"switch_root",
if err != nil { "-c", "/dev/console", "/system", "/sbin/init"}, os.Environ())
log.Print("switch_root failed: ", err) fatal("switch_root failed: ", err)
select {}
}
} }
func layerPath(name string) string { func layerPath(name string) string {
@ -147,18 +148,12 @@ func layerPath(name string) string {
func fatal(v ...interface{}) { func fatal(v ...interface{}) {
log.Print("*** FATAL ***") log.Print("*** FATAL ***")
log.Print(v...) log.Print(v...)
dropToShell() select {}
} }
func fatalf(pattern string, v ...interface{}) { func fatalf(pattern string, v ...interface{}) {
log.Print("*** FATAL ***") log.Print("*** FATAL ***")
log.Printf(pattern, v...) log.Printf(pattern, v...)
dropToShell()
}
func dropToShell() {
err := syscall.Exec("/bin/sh", []string{"/bin/sh"}, os.Environ())
log.Print("shell drop failed: ", err)
select {} select {}
} }