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/lxc"
|
2014-02-24 23:41:09 -05:00
|
|
|
_ "github.com/dotcloud/docker/execdriver/native"
|
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-02-14 20:28:50 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-01-20 19:05:07 -05:00
|
|
|
var (
|
|
|
|
// Get cmdline arguments
|
|
|
|
user = flag.String("u", "", "username or uid")
|
|
|
|
gateway = flag.String("g", "", "gateway address")
|
|
|
|
ip = flag.String("i", "", "ip address")
|
|
|
|
workDir = flag.String("w", "", "workdir")
|
|
|
|
privileged = flag.Bool("privileged", false, "privileged mode")
|
|
|
|
mtu = flag.Int("mtu", 1500, "interface mtu")
|
|
|
|
driver = flag.String("driver", "", "exec driver")
|
2014-02-24 15:21:13 -05:00
|
|
|
pipe = flag.Int("pipe", 0, "sync pipe fd")
|
|
|
|
console = flag.String("console", "", "console (pty slave) path")
|
2014-02-25 15:41:31 -05:00
|
|
|
root = flag.String("root", ".", "root path for configuration files")
|
2014-01-20 19:05:07 -05:00
|
|
|
)
|
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
|
2014-02-24 20:02:46 -05:00
|
|
|
content, err := ioutil.ReadFile(".dockerenv")
|
2013-12-02 13:58:45 -05:00
|
|
|
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,
|
2014-02-24 15:21:13 -05:00
|
|
|
Console: *console,
|
|
|
|
Pipe: *pipe,
|
2014-02-25 15:41:31 -05:00
|
|
|
Root: *root,
|
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
|
|
|
}
|