sysinit.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package sysinit
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "github.com/dotcloud/docker/execdriver"
  7. _ "github.com/dotcloud/docker/execdriver/lxc"
  8. _ "github.com/dotcloud/docker/execdriver/native"
  9. "io/ioutil"
  10. "log"
  11. "os"
  12. "strings"
  13. )
  14. // Clear environment pollution introduced by lxc-start
  15. func setupEnv(args *execdriver.InitArgs) {
  16. os.Clearenv()
  17. for _, kv := range args.Env {
  18. parts := strings.SplitN(kv, "=", 2)
  19. if len(parts) == 1 {
  20. parts = append(parts, "")
  21. }
  22. os.Setenv(parts[0], parts[1])
  23. }
  24. }
  25. func executeProgram(args *execdriver.InitArgs) error {
  26. setupEnv(args)
  27. dockerInitFct, err := execdriver.GetInitFunc(args.Driver)
  28. if err != nil {
  29. panic(err)
  30. }
  31. return dockerInitFct(args)
  32. }
  33. // Sys Init code
  34. // This code is run INSIDE the container and is responsible for setting
  35. // up the environment before running the actual process
  36. func SysInit() {
  37. if len(os.Args) <= 1 {
  38. fmt.Println("You should not invoke dockerinit manually")
  39. os.Exit(1)
  40. }
  41. var (
  42. // Get cmdline arguments
  43. user = flag.String("u", "", "username or uid")
  44. gateway = flag.String("g", "", "gateway address")
  45. ip = flag.String("i", "", "ip address")
  46. workDir = flag.String("w", "", "workdir")
  47. privileged = flag.Bool("privileged", false, "privileged mode")
  48. mtu = flag.Int("mtu", 1500, "interface mtu")
  49. driver = flag.String("driver", "", "exec driver")
  50. pipe = flag.Int("pipe", 0, "sync pipe fd")
  51. console = flag.String("console", "", "console (pty slave) path")
  52. root = flag.String("root", ".", "root path for configuration files")
  53. )
  54. flag.Parse()
  55. // Get env
  56. var env []string
  57. content, err := ioutil.ReadFile(".dockerenv")
  58. if err != nil {
  59. log.Fatalf("Unable to load environment variables: %v", err)
  60. }
  61. if err := json.Unmarshal(content, &env); err != nil {
  62. log.Fatalf("Unable to unmarshal environment variables: %v", err)
  63. }
  64. // Propagate the plugin-specific container env variable
  65. env = append(env, "container="+os.Getenv("container"))
  66. args := &execdriver.InitArgs{
  67. User: *user,
  68. Gateway: *gateway,
  69. Ip: *ip,
  70. WorkDir: *workDir,
  71. Privileged: *privileged,
  72. Env: env,
  73. Args: flag.Args(),
  74. Mtu: *mtu,
  75. Driver: *driver,
  76. Console: *console,
  77. Pipe: *pipe,
  78. Root: *root,
  79. }
  80. if err := executeProgram(args); err != nil {
  81. log.Fatal(err)
  82. }
  83. }