factory_linux.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // +build linux
  2. package libcontainer
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "regexp"
  10. "runtime/debug"
  11. "strconv"
  12. "syscall"
  13. "github.com/docker/docker/pkg/mount"
  14. "github.com/opencontainers/runc/libcontainer/cgroups"
  15. "github.com/opencontainers/runc/libcontainer/cgroups/fs"
  16. "github.com/opencontainers/runc/libcontainer/cgroups/systemd"
  17. "github.com/opencontainers/runc/libcontainer/configs"
  18. "github.com/opencontainers/runc/libcontainer/configs/validate"
  19. "github.com/opencontainers/runc/libcontainer/utils"
  20. )
  21. const (
  22. stateFilename = "state.json"
  23. )
  24. var (
  25. idRegex = regexp.MustCompile(`^[\w-\.]+$`)
  26. maxIdLen = 1024
  27. )
  28. // InitArgs returns an options func to configure a LinuxFactory with the
  29. // provided init arguments.
  30. func InitArgs(args ...string) func(*LinuxFactory) error {
  31. return func(l *LinuxFactory) error {
  32. name := args[0]
  33. if filepath.Base(name) == name {
  34. if lp, err := exec.LookPath(name); err == nil {
  35. name = lp
  36. }
  37. } else {
  38. abs, err := filepath.Abs(name)
  39. if err != nil {
  40. return err
  41. }
  42. name = abs
  43. }
  44. l.InitPath = "/proc/self/exe"
  45. l.InitArgs = append([]string{name}, args[1:]...)
  46. return nil
  47. }
  48. }
  49. // InitPath returns an options func to configure a LinuxFactory with the
  50. // provided absolute path to the init binary and arguements.
  51. func InitPath(path string, args ...string) func(*LinuxFactory) error {
  52. return func(l *LinuxFactory) error {
  53. l.InitPath = path
  54. l.InitArgs = args
  55. return nil
  56. }
  57. }
  58. // SystemdCgroups is an options func to configure a LinuxFactory to return
  59. // containers that use systemd to create and manage cgroups.
  60. func SystemdCgroups(l *LinuxFactory) error {
  61. l.NewCgroupsManager = func(config *configs.Cgroup, paths map[string]string) cgroups.Manager {
  62. return &systemd.Manager{
  63. Cgroups: config,
  64. Paths: paths,
  65. }
  66. }
  67. return nil
  68. }
  69. // Cgroupfs is an options func to configure a LinuxFactory to return
  70. // containers that use the native cgroups filesystem implementation to
  71. // create and manage cgroups.
  72. func Cgroupfs(l *LinuxFactory) error {
  73. l.NewCgroupsManager = func(config *configs.Cgroup, paths map[string]string) cgroups.Manager {
  74. return &fs.Manager{
  75. Cgroups: config,
  76. Paths: paths,
  77. }
  78. }
  79. return nil
  80. }
  81. // TmpfsRoot is an option func to mount LinuxFactory.Root to tmpfs.
  82. func TmpfsRoot(l *LinuxFactory) error {
  83. mounted, err := mount.Mounted(l.Root)
  84. if err != nil {
  85. return err
  86. }
  87. if !mounted {
  88. if err := syscall.Mount("tmpfs", l.Root, "tmpfs", 0, ""); err != nil {
  89. return err
  90. }
  91. }
  92. return nil
  93. }
  94. // New returns a linux based container factory based in the root directory and
  95. // configures the factory with the provided option funcs.
  96. func New(root string, options ...func(*LinuxFactory) error) (Factory, error) {
  97. if root != "" {
  98. if err := os.MkdirAll(root, 0700); err != nil {
  99. return nil, newGenericError(err, SystemError)
  100. }
  101. }
  102. l := &LinuxFactory{
  103. Root: root,
  104. Validator: validate.New(),
  105. CriuPath: "criu",
  106. }
  107. InitArgs(os.Args[0], "init")(l)
  108. Cgroupfs(l)
  109. for _, opt := range options {
  110. if err := opt(l); err != nil {
  111. return nil, err
  112. }
  113. }
  114. return l, nil
  115. }
  116. // LinuxFactory implements the default factory interface for linux based systems.
  117. type LinuxFactory struct {
  118. // Root directory for the factory to store state.
  119. Root string
  120. // InitPath is the absolute path to the init binary.
  121. InitPath string
  122. // InitArgs are arguments for calling the init responsibilities for spawning
  123. // a container.
  124. InitArgs []string
  125. // CriuPath is the path to the criu binary used for checkpoint and restore of
  126. // containers.
  127. CriuPath string
  128. // Validator provides validation to container configurations.
  129. Validator validate.Validator
  130. // NewCgroupsManager returns an initialized cgroups manager for a single container.
  131. NewCgroupsManager func(config *configs.Cgroup, paths map[string]string) cgroups.Manager
  132. }
  133. func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, error) {
  134. if l.Root == "" {
  135. return nil, newGenericError(fmt.Errorf("invalid root"), ConfigInvalid)
  136. }
  137. if err := l.validateID(id); err != nil {
  138. return nil, err
  139. }
  140. if err := l.Validator.Validate(config); err != nil {
  141. return nil, newGenericError(err, ConfigInvalid)
  142. }
  143. containerRoot := filepath.Join(l.Root, id)
  144. if _, err := os.Stat(containerRoot); err == nil {
  145. return nil, newGenericError(fmt.Errorf("container with id exists: %v", id), IdInUse)
  146. } else if !os.IsNotExist(err) {
  147. return nil, newGenericError(err, SystemError)
  148. }
  149. if err := os.MkdirAll(containerRoot, 0700); err != nil {
  150. return nil, newGenericError(err, SystemError)
  151. }
  152. c := &linuxContainer{
  153. id: id,
  154. root: containerRoot,
  155. config: config,
  156. initPath: l.InitPath,
  157. initArgs: l.InitArgs,
  158. criuPath: l.CriuPath,
  159. cgroupManager: l.NewCgroupsManager(config.Cgroups, nil),
  160. }
  161. c.state = &stoppedState{c: c}
  162. return c, nil
  163. }
  164. func (l *LinuxFactory) Load(id string) (Container, error) {
  165. if l.Root == "" {
  166. return nil, newGenericError(fmt.Errorf("invalid root"), ConfigInvalid)
  167. }
  168. containerRoot := filepath.Join(l.Root, id)
  169. state, err := l.loadState(containerRoot)
  170. if err != nil {
  171. return nil, err
  172. }
  173. r := &nonChildProcess{
  174. processPid: state.InitProcessPid,
  175. processStartTime: state.InitProcessStartTime,
  176. fds: state.ExternalDescriptors,
  177. }
  178. c := &linuxContainer{
  179. initProcess: r,
  180. id: id,
  181. config: &state.Config,
  182. initPath: l.InitPath,
  183. initArgs: l.InitArgs,
  184. criuPath: l.CriuPath,
  185. cgroupManager: l.NewCgroupsManager(state.Config.Cgroups, state.CgroupPaths),
  186. root: containerRoot,
  187. created: state.Created,
  188. }
  189. c.state = &createdState{c: c, s: Created}
  190. if err := c.refreshState(); err != nil {
  191. return nil, err
  192. }
  193. return c, nil
  194. }
  195. func (l *LinuxFactory) Type() string {
  196. return "libcontainer"
  197. }
  198. // StartInitialization loads a container by opening the pipe fd from the parent to read the configuration and state
  199. // This is a low level implementation detail of the reexec and should not be consumed externally
  200. func (l *LinuxFactory) StartInitialization() (err error) {
  201. fdStr := os.Getenv("_LIBCONTAINER_INITPIPE")
  202. pipefd, err := strconv.Atoi(fdStr)
  203. if err != nil {
  204. return fmt.Errorf("error converting env var _LIBCONTAINER_INITPIPE(%q) to an int: %s", fdStr, err)
  205. }
  206. var (
  207. pipe = os.NewFile(uintptr(pipefd), "pipe")
  208. it = initType(os.Getenv("_LIBCONTAINER_INITTYPE"))
  209. )
  210. // clear the current process's environment to clean any libcontainer
  211. // specific env vars.
  212. os.Clearenv()
  213. var i initer
  214. defer func() {
  215. // We have an error during the initialization of the container's init,
  216. // send it back to the parent process in the form of an initError.
  217. // If container's init successed, syscall.Exec will not return, hence
  218. // this defer function will never be called.
  219. if _, ok := i.(*linuxStandardInit); ok {
  220. // Synchronisation only necessary for standard init.
  221. if err := utils.WriteJSON(pipe, syncT{procError}); err != nil {
  222. panic(err)
  223. }
  224. }
  225. if err := utils.WriteJSON(pipe, newSystemError(err)); err != nil {
  226. panic(err)
  227. }
  228. // ensure that this pipe is always closed
  229. pipe.Close()
  230. }()
  231. defer func() {
  232. if e := recover(); e != nil {
  233. err = fmt.Errorf("panic from initialization: %v, %v", e, string(debug.Stack()))
  234. }
  235. }()
  236. i, err = newContainerInit(it, pipe)
  237. if err != nil {
  238. return err
  239. }
  240. return i.Init()
  241. }
  242. func (l *LinuxFactory) loadState(root string) (*State, error) {
  243. f, err := os.Open(filepath.Join(root, stateFilename))
  244. if err != nil {
  245. if os.IsNotExist(err) {
  246. return nil, newGenericError(err, ContainerNotExists)
  247. }
  248. return nil, newGenericError(err, SystemError)
  249. }
  250. defer f.Close()
  251. var state *State
  252. if err := json.NewDecoder(f).Decode(&state); err != nil {
  253. return nil, newGenericError(err, SystemError)
  254. }
  255. return state, nil
  256. }
  257. func (l *LinuxFactory) validateID(id string) error {
  258. if !idRegex.MatchString(id) {
  259. return newGenericError(fmt.Errorf("invalid id format: %v", id), InvalidIdFormat)
  260. }
  261. if len(id) > maxIdLen {
  262. return newGenericError(fmt.Errorf("invalid id format: %v", id), InvalidIdFormat)
  263. }
  264. return nil
  265. }