mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-12-26 10:09:11 +00:00
Switch runc and containerd startup to be entirely Go
At present they use a small shared function called "prepare" that does the read-write remounts, that I will switch to doing overlay mounts soon. Signed-off-by: Justin Cormack <justin.cormack@docker.com>
This commit is contained in:
63
pkg/runc/cmd/onboot/main.go
Normal file
63
pkg/runc/cmd/onboot/main.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
runcBinary = "/usr/bin/runc"
|
||||
onbootPath = "/containers/onboot"
|
||||
shutdownPath = "/containers/onshutdown"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// try to work out how we are being called
|
||||
command := os.Args[0]
|
||||
if len(os.Args) > 1 {
|
||||
command = os.Args[1]
|
||||
}
|
||||
var path = onbootPath
|
||||
switch {
|
||||
case strings.Contains(command, "boot"):
|
||||
path = onbootPath
|
||||
case strings.Contains(command, "shutdown"):
|
||||
path = shutdownPath
|
||||
}
|
||||
|
||||
// do nothing if the path does not exist
|
||||
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// get files; note ReadDir already sorts them
|
||||
files, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
log.Fatalf("Cannot read files in %s: %v", path, err)
|
||||
}
|
||||
|
||||
status := 0
|
||||
|
||||
for _, file := range files {
|
||||
name := file.Name()
|
||||
fullPath := filepath.Join(path, name)
|
||||
if err := prepare(fullPath); err != nil {
|
||||
log.Printf("Error preparing %s: %v", name, err)
|
||||
status = 1
|
||||
continue
|
||||
}
|
||||
cmd := exec.Command(runcBinary, "run", "--bundle", fullPath, name)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.Printf("Error running %s: %v", name, err)
|
||||
status = 1
|
||||
}
|
||||
}
|
||||
|
||||
os.Exit(status)
|
||||
}
|
||||
21
pkg/runc/cmd/onboot/prepare.go
Normal file
21
pkg/runc/cmd/onboot/prepare.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
// Please note this file is shared between pkg/runc and pkg/containerd
|
||||
// Update it in both places if you make changes
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func prepare(path string) error {
|
||||
rootfs := filepath.Join(path, "rootfs")
|
||||
if err := syscall.Mount(rootfs, rootfs, "", syscall.MS_BIND, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
// remount rw
|
||||
if err := syscall.Mount("", rootfs, "", syscall.MS_REMOUNT, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user