2013-10-10 04:53:38 -04:00
|
|
|
package sysinit
|
2013-02-13 17:01:44 -05:00
|
|
|
|
|
|
|
import (
|
2013-08-14 20:14:30 -04:00
|
|
|
"encoding/json"
|
2013-02-13 20:24:35 -05:00
|
|
|
"flag"
|
2013-02-13 17:01:44 -05:00
|
|
|
"fmt"
|
2014-01-13 21:36:59 -05:00
|
|
|
"github.com/dotcloud/docker/execdriver"
|
2014-01-16 18:09:00 -05:00
|
|
|
_ "github.com/dotcloud/docker/execdriver/chroot"
|
|
|
|
_ "github.com/dotcloud/docker/execdriver/lxc"
|
2013-08-13 18:40:23 -04:00
|
|
|
"io/ioutil"
|
2013-02-13 17:01:44 -05:00
|
|
|
"log"
|
|
|
|
"os"
|
2013-03-23 22:11:00 -04:00
|
|
|
"strings"
|
2013-02-13 17:01:44 -05:00
|
|
|
)
|
|
|
|
|
2013-03-23 22:11:00 -04:00
|
|
|
// Clear environment pollution introduced by lxc-start
|
2014-01-15 16:57:07 -05:00
|
|
|
func setupEnv(args *execdriver.InitArgs) {
|
2013-03-23 22:11:00 -04:00
|
|
|
os.Clearenv()
|
2014-01-13 21:36:59 -05:00
|
|
|
for _, kv := range args.Env {
|
2013-03-23 22:11:00 -04:00
|
|
|
parts := strings.SplitN(kv, "=", 2)
|
|
|
|
if len(parts) == 1 {
|
|
|
|
parts = append(parts, "")
|
|
|
|
}
|
|
|
|
os.Setenv(parts[0], parts[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 16:57:07 -05:00
|
|
|
func executeProgram(args *execdriver.InitArgs) error {
|
2013-12-02 17:21:29 -05:00
|
|
|
setupEnv(args)
|
2014-01-15 16:57:07 -05:00
|
|
|
dockerInitFct, err := execdriver.GetInitFunc(args.Driver)
|
2013-02-13 20:24:35 -05:00
|
|
|
if err != nil {
|
2014-01-13 21:36:59 -05:00
|
|
|
panic(err)
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
2014-01-13 21:36:59 -05:00
|
|
|
return dockerInitFct(args)
|
2013-02-13 20:24:35 -05:00
|
|
|
|
2014-01-13 21:36:59 -05:00
|
|
|
if args.Driver == "lxc" {
|
2014-01-13 20:55:16 -05:00
|
|
|
// Will never reach
|
2014-01-13 21:36:59 -05:00
|
|
|
} else if args.Driver == "chroot" {
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
2013-12-02 17:21:29 -05:00
|
|
|
|
|
|
|
return nil
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
|
|
|
|
2013-02-13 17:01:44 -05:00
|
|
|
// Sys Init code
|
|
|
|
// This code is run INSIDE the container and is responsible for setting
|
|
|
|
// up the environment before running the actual process
|
|
|
|
func SysInit() {
|
|
|
|
if len(os.Args) <= 1 {
|
2013-10-18 01:39:57 -04:00
|
|
|
fmt.Println("You should not invoke dockerinit manually")
|
2013-02-13 17:01:44 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2013-12-02 13:58:45 -05:00
|
|
|
// Get cmdline arguments
|
|
|
|
user := flag.String("u", "", "username or uid")
|
|
|
|
gateway := flag.String("g", "", "gateway address")
|
2013-10-29 14:37:00 -04:00
|
|
|
ip := flag.String("i", "", "ip address")
|
2013-12-02 13:58:45 -05:00
|
|
|
workDir := flag.String("w", "", "workdir")
|
2013-10-30 23:24:47 -04:00
|
|
|
privileged := flag.Bool("privileged", false, "privileged mode")
|
2013-12-18 23:41:34 -05:00
|
|
|
mtu := flag.Int("mtu", 1500, "interface mtu")
|
2014-01-13 14:13:49 -05:00
|
|
|
driver := flag.String("driver", "", "exec driver")
|
2013-02-13 20:24:35 -05:00
|
|
|
flag.Parse()
|
2013-02-20 20:45:46 -05:00
|
|
|
|
2013-12-02 13:58:45 -05:00
|
|
|
// Get env
|
|
|
|
var env []string
|
|
|
|
content, err := ioutil.ReadFile("/.dockerenv")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to load environment variables: %v", err)
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(content, &env); err != nil {
|
|
|
|
log.Fatalf("Unable to unmarshal environment variables: %v", err)
|
|
|
|
}
|
|
|
|
|
2013-11-08 17:15:11 -05:00
|
|
|
// Propagate the plugin-specific container env variable
|
|
|
|
env = append(env, "container="+os.Getenv("container"))
|
|
|
|
|
2014-01-15 16:57:07 -05:00
|
|
|
args := &execdriver.InitArgs{
|
2014-01-13 21:36:59 -05:00
|
|
|
User: *user,
|
|
|
|
Gateway: *gateway,
|
|
|
|
Ip: *ip,
|
|
|
|
WorkDir: *workDir,
|
|
|
|
Privileged: *privileged,
|
|
|
|
Env: env,
|
|
|
|
Args: flag.Args(),
|
|
|
|
Mtu: *mtu,
|
|
|
|
Driver: *driver,
|
2013-12-02 13:58:45 -05:00
|
|
|
}
|
|
|
|
|
2013-12-02 17:21:29 -05:00
|
|
|
if err := executeProgram(args); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2013-02-13 17:01:44 -05:00
|
|
|
}
|