init.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // +build linux
  2. package nsinit
  3. import (
  4. "fmt"
  5. "os"
  6. "runtime"
  7. "syscall"
  8. "github.com/dotcloud/docker/pkg/apparmor"
  9. "github.com/dotcloud/docker/pkg/label"
  10. "github.com/dotcloud/docker/pkg/libcontainer"
  11. "github.com/dotcloud/docker/pkg/libcontainer/console"
  12. "github.com/dotcloud/docker/pkg/libcontainer/mount"
  13. "github.com/dotcloud/docker/pkg/libcontainer/network"
  14. "github.com/dotcloud/docker/pkg/libcontainer/security/capabilities"
  15. "github.com/dotcloud/docker/pkg/libcontainer/utils"
  16. "github.com/dotcloud/docker/pkg/system"
  17. "github.com/dotcloud/docker/pkg/user"
  18. )
  19. // Init is the init process that first runs inside a new namespace to setup mounts, users, networking,
  20. // and other options required for the new container.
  21. func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, consolePath string, syncPipe *SyncPipe, args []string) error {
  22. rootfs, err := utils.ResolveRootfs(uncleanRootfs)
  23. if err != nil {
  24. return err
  25. }
  26. // We always read this as it is a way to sync with the parent as well
  27. context, err := syncPipe.ReadFromParent()
  28. if err != nil {
  29. syncPipe.Close()
  30. return err
  31. }
  32. syncPipe.Close()
  33. if consolePath != "" {
  34. if err := console.OpenAndDup(consolePath); err != nil {
  35. return err
  36. }
  37. }
  38. if _, err := system.Setsid(); err != nil {
  39. return fmt.Errorf("setsid %s", err)
  40. }
  41. if consolePath != "" {
  42. if err := system.Setctty(); err != nil {
  43. return fmt.Errorf("setctty %s", err)
  44. }
  45. }
  46. if err := setupNetwork(container, context); err != nil {
  47. return fmt.Errorf("setup networking %s", err)
  48. }
  49. label.Init()
  50. if err := mount.InitializeMountNamespace(rootfs, consolePath, container); err != nil {
  51. return fmt.Errorf("setup mount namespace %s", err)
  52. }
  53. if err := system.Sethostname(container.Hostname); err != nil {
  54. return fmt.Errorf("sethostname %s", err)
  55. }
  56. if err := finalizeNamespace(container); err != nil {
  57. return fmt.Errorf("finalize namespace %s", err)
  58. }
  59. if profile := container.Context["apparmor_profile"]; profile != "" {
  60. if err := apparmor.ApplyProfile(os.Getpid(), profile); err != nil {
  61. return err
  62. }
  63. }
  64. runtime.LockOSThread()
  65. if err := label.SetProcessLabel(container.Context["process_label"]); err != nil {
  66. return fmt.Errorf("set process label %s", err)
  67. }
  68. return system.Execv(args[0], args[0:], container.Env)
  69. }
  70. // SetupUser changes the groups, gid, and uid for the user inside the container
  71. func SetupUser(u string) error {
  72. uid, gid, suppGids, err := user.GetUserGroupSupplementary(u, syscall.Getuid(), syscall.Getgid())
  73. if err != nil {
  74. return fmt.Errorf("get supplementary groups %s", err)
  75. }
  76. if err := system.Setgroups(suppGids); err != nil {
  77. return fmt.Errorf("setgroups %s", err)
  78. }
  79. if err := system.Setgid(gid); err != nil {
  80. return fmt.Errorf("setgid %s", err)
  81. }
  82. if err := system.Setuid(uid); err != nil {
  83. return fmt.Errorf("setuid %s", err)
  84. }
  85. return nil
  86. }
  87. // setupVethNetwork uses the Network config if it is not nil to initialize
  88. // the new veth interface inside the container for use by changing the name to eth0
  89. // setting the MTU and IP address along with the default gateway
  90. func setupNetwork(container *libcontainer.Container, context libcontainer.Context) error {
  91. for _, config := range container.Networks {
  92. strategy, err := network.GetStrategy(config.Type)
  93. if err != nil {
  94. return err
  95. }
  96. err1 := strategy.Initialize(config, context)
  97. if err1 != nil {
  98. return err1
  99. }
  100. }
  101. return nil
  102. }
  103. // finalizeNamespace drops the caps, sets the correct user
  104. // and working dir, and closes any leaky file descriptors
  105. // before execing the command inside the namespace
  106. func finalizeNamespace(container *libcontainer.Container) error {
  107. if err := capabilities.DropCapabilities(container); err != nil {
  108. return fmt.Errorf("drop capabilities %s", err)
  109. }
  110. if err := system.CloseFdsFrom(3); err != nil {
  111. return fmt.Errorf("close open file descriptors %s", err)
  112. }
  113. if err := SetupUser(container.User); err != nil {
  114. return fmt.Errorf("setup user %s", err)
  115. }
  116. if container.WorkingDir != "" {
  117. if err := system.Chdir(container.WorkingDir); err != nil {
  118. return fmt.Errorf("chdir to %s %s", container.WorkingDir, err)
  119. }
  120. }
  121. return nil
  122. }