2019-01-12 04:58:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-09-27 21:51:53 +00:00
|
|
|
"fmt"
|
2019-01-12 04:58:27 +00:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2021-04-14 18:11:13 +00:00
|
|
|
"strconv"
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2019-09-27 21:51:53 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/logs"
|
2019-01-12 04:58:27 +00:00
|
|
|
_ "github.com/opencontainers/runc/libcontainer/nsenter"
|
2019-09-27 21:51:53 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-01-12 04:58:27 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if len(os.Args) > 1 && os.Args[1] == "init" {
|
|
|
|
runtime.GOMAXPROCS(1)
|
|
|
|
runtime.LockOSThread()
|
2019-09-27 21:51:53 +00:00
|
|
|
|
|
|
|
level := os.Getenv("_LIBCONTAINER_LOGLEVEL")
|
|
|
|
logLevel, err := logrus.ParseLevel(level)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("libcontainer: failed to parse log level: %q: %v", level, err))
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:11:13 +00:00
|
|
|
logPipeFdStr := os.Getenv("_LIBCONTAINER_LOGPIPE")
|
|
|
|
logPipeFd, err := strconv.Atoi(logPipeFdStr)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("libcontainer: failed to convert environment variable _LIBCONTAINER_LOGPIPE=%s to int: %s", logPipeFdStr, err))
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
err = logs.ConfigureLogging(logs.Config{
|
2021-04-14 18:11:13 +00:00
|
|
|
LogPipeFd: logPipeFd,
|
2019-09-27 21:51:53 +00:00
|
|
|
LogFormat: "json",
|
|
|
|
LogLevel: logLevel,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("libcontainer: failed to configure logging: %v", err))
|
|
|
|
}
|
|
|
|
logrus.Debug("child process in init()")
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var initCommand = cli.Command{
|
|
|
|
Name: "init",
|
|
|
|
Usage: `initialize the namespaces and launch the process (do not call it outside of runc)`,
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
factory, _ := libcontainer.New("")
|
|
|
|
if err := factory.StartInitialization(); err != nil {
|
|
|
|
// as the error is sent back to the parent there is no need to log
|
|
|
|
// or write it to stderr because the parent process will handle this
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
panic("libcontainer: container init failed to exec")
|
|
|
|
},
|
|
|
|
}
|