init.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // +build linux
  2. package nsinit
  3. import (
  4. "fmt"
  5. "github.com/dotcloud/docker/pkg/libcontainer"
  6. "github.com/dotcloud/docker/pkg/libcontainer/apparmor"
  7. "github.com/dotcloud/docker/pkg/libcontainer/capabilities"
  8. "github.com/dotcloud/docker/pkg/libcontainer/network"
  9. "github.com/dotcloud/docker/pkg/libcontainer/utils"
  10. "github.com/dotcloud/docker/pkg/system"
  11. "github.com/dotcloud/docker/pkg/user"
  12. "os"
  13. "syscall"
  14. )
  15. // Init is the init process that first runs inside a new namespace to setup mounts, users, networking,
  16. // and other options required for the new container.
  17. func (ns *linuxNs) Init(container *libcontainer.Container, uncleanRootfs, console string, syncPipe *SyncPipe, args []string) error {
  18. rootfs, err := utils.ResolveRootfs(uncleanRootfs)
  19. if err != nil {
  20. return err
  21. }
  22. // We always read this as it is a way to sync with the parent as well
  23. context, err := syncPipe.ReadFromParent()
  24. if err != nil {
  25. syncPipe.Close()
  26. return err
  27. }
  28. syncPipe.Close()
  29. if console != "" {
  30. slave, err := system.OpenTerminal(console, syscall.O_RDWR)
  31. if err != nil {
  32. return fmt.Errorf("open terminal %s", err)
  33. }
  34. if err := dupSlave(slave); err != nil {
  35. return fmt.Errorf("dup2 slave %s", err)
  36. }
  37. }
  38. if _, err := system.Setsid(); err != nil {
  39. return fmt.Errorf("setsid %s", err)
  40. }
  41. if console != "" {
  42. if err := system.Setctty(); err != nil {
  43. return fmt.Errorf("setctty %s", err)
  44. }
  45. }
  46. /*
  47. if err := system.ParentDeathSignal(); err != nil {
  48. return fmt.Errorf("parent death signal %s", err)
  49. }
  50. */
  51. if err := setupNewMountNamespace(rootfs, console, container.ReadonlyFs); err != nil {
  52. return fmt.Errorf("setup mount namespace %s", err)
  53. }
  54. if err := setupNetwork(container, context); err != nil {
  55. return fmt.Errorf("setup networking %s", err)
  56. }
  57. if err := system.Sethostname(container.Hostname); err != nil {
  58. return fmt.Errorf("sethostname %s", err)
  59. }
  60. if err := finalizeNamespace(container); err != nil {
  61. return fmt.Errorf("finalize namespace %s", err)
  62. }
  63. if err := apparmor.ApplyProfile(os.Getpid(), container.Context["apparmor_profile"]); err != nil {
  64. return err
  65. }
  66. return system.Execv(args[0], args[0:], container.Env)
  67. }
  68. func setupUser(container *libcontainer.Container) error {
  69. switch container.User {
  70. case "root", "":
  71. if err := system.Setgroups(nil); err != nil {
  72. return err
  73. }
  74. if err := system.Setresgid(0, 0, 0); err != nil {
  75. return err
  76. }
  77. if err := system.Setresuid(0, 0, 0); err != nil {
  78. return err
  79. }
  80. default:
  81. uid, gid, suppGids, err := user.GetUserGroupSupplementary(container.User, syscall.Getuid(), syscall.Getgid())
  82. if err != nil {
  83. return err
  84. }
  85. if err := system.Setgroups(suppGids); err != nil {
  86. return err
  87. }
  88. if err := system.Setgid(gid); err != nil {
  89. return err
  90. }
  91. if err := system.Setuid(uid); err != nil {
  92. return err
  93. }
  94. }
  95. return nil
  96. }
  97. // dupSlave dup2 the pty slave's fd into stdout and stdin and ensures that
  98. // the slave's fd is 0, or stdin
  99. func dupSlave(slave *os.File) error {
  100. if err := system.Dup2(slave.Fd(), 0); err != nil {
  101. return err
  102. }
  103. if err := system.Dup2(slave.Fd(), 1); err != nil {
  104. return err
  105. }
  106. if err := system.Dup2(slave.Fd(), 2); err != nil {
  107. return err
  108. }
  109. return nil
  110. }
  111. // setupVethNetwork uses the Network config if it is not nil to initialize
  112. // the new veth interface inside the container for use by changing the name to eth0
  113. // setting the MTU and IP address along with the default gateway
  114. func setupNetwork(container *libcontainer.Container, context libcontainer.Context) error {
  115. for _, config := range container.Networks {
  116. strategy, err := network.GetStrategy(config.Type)
  117. if err != nil {
  118. return err
  119. }
  120. return strategy.Initialize(config, context)
  121. }
  122. return nil
  123. }
  124. // finalizeNamespace drops the caps and sets the correct user
  125. // and working dir before execing the command inside the namespace
  126. func finalizeNamespace(container *libcontainer.Container) error {
  127. if err := capabilities.DropCapabilities(container); err != nil {
  128. return fmt.Errorf("drop capabilities %s", err)
  129. }
  130. if err := setupUser(container); err != nil {
  131. return fmt.Errorf("setup user %s", err)
  132. }
  133. if container.WorkingDir != "" {
  134. if err := system.Chdir(container.WorkingDir); err != nil {
  135. return fmt.Errorf("chdir to %s %s", container.WorkingDir, err)
  136. }
  137. }
  138. return nil
  139. }