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 *.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

View File

@ -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

19
main.go
View File

@ -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 {}
}