2015-06-27 09:15:48 +08:00
|
|
|
package main
|
|
|
|
|
2016-05-14 06:49:45 +08:00
|
|
|
import (
|
2019-04-04 19:57:28 +08:00
|
|
|
"fmt"
|
2019-04-19 22:36:52 +08:00
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
2016-05-14 06:49:45 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2019-04-04 19:57:28 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/logs"
|
2016-05-14 06:49:45 +08:00
|
|
|
_ "github.com/opencontainers/runc/libcontainer/nsenter"
|
2018-08-04 01:11:20 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-06-07 02:45:46 +08:00
|
|
|
"github.com/urfave/cli"
|
2016-05-14 06:49:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if len(os.Args) > 1 && os.Args[1] == "init" {
|
|
|
|
runtime.GOMAXPROCS(1)
|
|
|
|
runtime.LockOSThread()
|
2018-08-04 01:11:20 +08:00
|
|
|
|
2019-04-19 22:36:52 +08: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))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = logs.ConfigureLogging(logs.Config{
|
2019-04-04 19:57:28 +08:00
|
|
|
LogPipeFd: os.Getenv("_LIBCONTAINER_LOGPIPE"),
|
|
|
|
LogFormat: "json",
|
2019-04-19 22:36:52 +08:00
|
|
|
LogLevel: logLevel,
|
2019-04-04 19:57:28 +08:00
|
|
|
})
|
2018-08-04 01:11:20 +08:00
|
|
|
if err != nil {
|
2019-04-04 19:57:28 +08:00
|
|
|
panic(fmt.Sprintf("libcontainer: failed to configure logging: %v", err))
|
2018-08-04 01:11:20 +08:00
|
|
|
}
|
|
|
|
logrus.Debug("child process in init()")
|
2016-05-14 06:49:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var initCommand = cli.Command{
|
|
|
|
Name: "init",
|
|
|
|
Usage: `initialize the namespaces and launch the process (do not call it outside of runc)`,
|
2016-06-08 05:42:54 +08:00
|
|
|
Action: func(context *cli.Context) error {
|
2016-05-14 06:49:45 +08:00
|
|
|
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")
|
|
|
|
},
|
|
|
|
}
|