container.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. package docker
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "github.com/kr/pty"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "path/filepath"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "syscall"
  18. "time"
  19. )
  20. type Container struct {
  21. root string
  22. Id string
  23. Created time.Time
  24. Path string
  25. Args []string
  26. Config *Config
  27. State State
  28. Image string
  29. network *NetworkInterface
  30. NetworkSettings *NetworkSettings
  31. SysInitPath string
  32. ResolvConfPath string
  33. cmd *exec.Cmd
  34. stdout *writeBroadcaster
  35. stderr *writeBroadcaster
  36. stdin io.ReadCloser
  37. stdinPipe io.WriteCloser
  38. ptyMaster io.Closer
  39. runtime *Runtime
  40. waitLock chan struct{}
  41. Volumes map[string]string
  42. }
  43. type Config struct {
  44. Hostname string
  45. User string
  46. Memory int64 // Memory limit (in bytes)
  47. MemorySwap int64 // Total memory usage (memory + swap); set `-1' to disable swap
  48. AttachStdin bool
  49. AttachStdout bool
  50. AttachStderr bool
  51. PortSpecs []string
  52. Tty bool // Attach standard streams to a tty, including stdin if it is not closed.
  53. OpenStdin bool // Open stdin
  54. StdinOnce bool // If true, close stdin after the 1 attached client disconnects.
  55. Env []string
  56. Cmd []string
  57. Dns []string
  58. Image string // Name of the image as it was passed by the operator (eg. could be symbolic)
  59. Volumes map[string]struct{}
  60. VolumesFrom string
  61. }
  62. func ParseRun(args []string, capabilities *Capabilities) (*Config, *flag.FlagSet, error) {
  63. cmd := Subcmd("run", "[OPTIONS] IMAGE COMMAND [ARG...]", "Run a command in a new container")
  64. if len(args) > 0 && args[0] != "--help" {
  65. cmd.SetOutput(ioutil.Discard)
  66. }
  67. flHostname := cmd.String("h", "", "Container host name")
  68. flUser := cmd.String("u", "", "Username or UID")
  69. flDetach := cmd.Bool("d", false, "Detached mode: leave the container running in the background")
  70. flAttach := NewAttachOpts()
  71. cmd.Var(flAttach, "a", "Attach to stdin, stdout or stderr.")
  72. flStdin := cmd.Bool("i", false, "Keep stdin open even if not attached")
  73. flTty := cmd.Bool("t", false, "Allocate a pseudo-tty")
  74. flMemory := cmd.Int64("m", 0, "Memory limit (in bytes)")
  75. if capabilities != nil && *flMemory > 0 && !capabilities.MemoryLimit {
  76. //fmt.Fprintf(stdout, "WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n")
  77. *flMemory = 0
  78. }
  79. var flPorts ListOpts
  80. cmd.Var(&flPorts, "p", "Expose a container's port to the host (use 'docker port' to see the actual mapping)")
  81. var flEnv ListOpts
  82. cmd.Var(&flEnv, "e", "Set environment variables")
  83. var flDns ListOpts
  84. cmd.Var(&flDns, "dns", "Set custom dns servers")
  85. flVolumes := NewPathOpts()
  86. cmd.Var(flVolumes, "v", "Attach a data volume")
  87. flVolumesFrom := cmd.String("volumes-from", "", "Mount volumes from the specified container")
  88. if err := cmd.Parse(args); err != nil {
  89. return nil, cmd, err
  90. }
  91. if *flDetach && len(flAttach) > 0 {
  92. return nil, cmd, fmt.Errorf("Conflicting options: -a and -d")
  93. }
  94. // If neither -d or -a are set, attach to everything by default
  95. if len(flAttach) == 0 && !*flDetach {
  96. if !*flDetach {
  97. flAttach.Set("stdout")
  98. flAttach.Set("stderr")
  99. if *flStdin {
  100. flAttach.Set("stdin")
  101. }
  102. }
  103. }
  104. parsedArgs := cmd.Args()
  105. runCmd := []string{}
  106. image := ""
  107. if len(parsedArgs) >= 1 {
  108. image = cmd.Arg(0)
  109. }
  110. if len(parsedArgs) > 1 {
  111. runCmd = parsedArgs[1:]
  112. }
  113. config := &Config{
  114. Hostname: *flHostname,
  115. PortSpecs: flPorts,
  116. User: *flUser,
  117. Tty: *flTty,
  118. OpenStdin: *flStdin,
  119. Memory: *flMemory,
  120. AttachStdin: flAttach.Get("stdin"),
  121. AttachStdout: flAttach.Get("stdout"),
  122. AttachStderr: flAttach.Get("stderr"),
  123. Env: flEnv,
  124. Cmd: runCmd,
  125. Dns: flDns,
  126. Image: image,
  127. Volumes: flVolumes,
  128. VolumesFrom: *flVolumesFrom,
  129. }
  130. if capabilities != nil && *flMemory > 0 && !capabilities.SwapLimit {
  131. //fmt.Fprintf(stdout, "WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.\n")
  132. config.MemorySwap = -1
  133. }
  134. // When allocating stdin in attached mode, close stdin at client disconnect
  135. if config.OpenStdin && config.AttachStdin {
  136. config.StdinOnce = true
  137. }
  138. return config, cmd, nil
  139. }
  140. type NetworkSettings struct {
  141. IpAddress string
  142. IpPrefixLen int
  143. Gateway string
  144. Bridge string
  145. PortMapping map[string]string
  146. }
  147. // String returns a human-readable description of the port mapping defined in the settings
  148. func (settings *NetworkSettings) PortMappingHuman() string {
  149. var mapping []string
  150. for private, public := range settings.PortMapping {
  151. mapping = append(mapping, fmt.Sprintf("%s->%s", public, private))
  152. }
  153. sort.Strings(mapping)
  154. return strings.Join(mapping, ", ")
  155. }
  156. // Inject the io.Reader at the given path. Note: do not close the reader
  157. func (container *Container) Inject(file io.Reader, pth string) error {
  158. // Make sure the directory exists
  159. if err := os.MkdirAll(path.Join(container.rwPath(), path.Dir(pth)), 0755); err != nil {
  160. return err
  161. }
  162. // FIXME: Handle permissions/already existing dest
  163. dest, err := os.Create(path.Join(container.rwPath(), pth))
  164. if err != nil {
  165. return err
  166. }
  167. if _, err := io.Copy(dest, file); err != nil {
  168. return err
  169. }
  170. return nil
  171. }
  172. func (container *Container) Cmd() *exec.Cmd {
  173. return container.cmd
  174. }
  175. func (container *Container) When() time.Time {
  176. return container.Created
  177. }
  178. func (container *Container) FromDisk() error {
  179. data, err := ioutil.ReadFile(container.jsonPath())
  180. if err != nil {
  181. return err
  182. }
  183. // Load container settings
  184. if err := json.Unmarshal(data, container); err != nil {
  185. return err
  186. }
  187. return nil
  188. }
  189. func (container *Container) ToDisk() (err error) {
  190. data, err := json.Marshal(container)
  191. if err != nil {
  192. return
  193. }
  194. return ioutil.WriteFile(container.jsonPath(), data, 0666)
  195. }
  196. func (container *Container) generateLXCConfig() error {
  197. fo, err := os.Create(container.lxcConfigPath())
  198. if err != nil {
  199. return err
  200. }
  201. defer fo.Close()
  202. if err := LxcTemplateCompiled.Execute(fo, container); err != nil {
  203. return err
  204. }
  205. return nil
  206. }
  207. func (container *Container) startPty() error {
  208. ptyMaster, ptySlave, err := pty.Open()
  209. if err != nil {
  210. return err
  211. }
  212. container.ptyMaster = ptyMaster
  213. container.cmd.Stdout = ptySlave
  214. container.cmd.Stderr = ptySlave
  215. // Copy the PTYs to our broadcasters
  216. go func() {
  217. defer container.stdout.CloseWriters()
  218. Debugf("[startPty] Begin of stdout pipe")
  219. io.Copy(container.stdout, ptyMaster)
  220. Debugf("[startPty] End of stdout pipe")
  221. }()
  222. // stdin
  223. if container.Config.OpenStdin {
  224. container.cmd.Stdin = ptySlave
  225. container.cmd.SysProcAttr = &syscall.SysProcAttr{Setctty: true, Setsid: true}
  226. go func() {
  227. defer container.stdin.Close()
  228. Debugf("[startPty] Begin of stdin pipe")
  229. io.Copy(ptyMaster, container.stdin)
  230. Debugf("[startPty] End of stdin pipe")
  231. }()
  232. }
  233. if err := container.cmd.Start(); err != nil {
  234. return err
  235. }
  236. ptySlave.Close()
  237. return nil
  238. }
  239. func (container *Container) start() error {
  240. container.cmd.Stdout = container.stdout
  241. container.cmd.Stderr = container.stderr
  242. if container.Config.OpenStdin {
  243. stdin, err := container.cmd.StdinPipe()
  244. if err != nil {
  245. return err
  246. }
  247. go func() {
  248. defer stdin.Close()
  249. Debugf("Begin of stdin pipe [start]")
  250. io.Copy(stdin, container.stdin)
  251. Debugf("End of stdin pipe [start]")
  252. }()
  253. }
  254. return container.cmd.Start()
  255. }
  256. func (container *Container) Attach(stdin io.ReadCloser, stdinCloser io.Closer, stdout io.Writer, stderr io.Writer) chan error {
  257. var cStdout, cStderr io.ReadCloser
  258. var nJobs int
  259. errors := make(chan error, 3)
  260. if stdin != nil && container.Config.OpenStdin {
  261. nJobs += 1
  262. if cStdin, err := container.StdinPipe(); err != nil {
  263. errors <- err
  264. } else {
  265. go func() {
  266. Debugf("[start] attach stdin\n")
  267. defer Debugf("[end] attach stdin\n")
  268. // No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr
  269. if cStdout != nil {
  270. defer cStdout.Close()
  271. }
  272. if cStderr != nil {
  273. defer cStderr.Close()
  274. }
  275. if container.Config.StdinOnce && !container.Config.Tty {
  276. defer cStdin.Close()
  277. }
  278. if container.Config.Tty {
  279. _, err = CopyEscapable(cStdin, stdin)
  280. } else {
  281. _, err = io.Copy(cStdin, stdin)
  282. }
  283. if err != nil {
  284. Debugf("[error] attach stdin: %s\n", err)
  285. }
  286. // Discard error, expecting pipe error
  287. errors <- nil
  288. }()
  289. }
  290. }
  291. if stdout != nil {
  292. nJobs += 1
  293. if p, err := container.StdoutPipe(); err != nil {
  294. errors <- err
  295. } else {
  296. cStdout = p
  297. go func() {
  298. Debugf("[start] attach stdout\n")
  299. defer Debugf("[end] attach stdout\n")
  300. // If we are in StdinOnce mode, then close stdin
  301. if container.Config.StdinOnce {
  302. if stdin != nil {
  303. defer stdin.Close()
  304. }
  305. if stdinCloser != nil {
  306. defer stdinCloser.Close()
  307. }
  308. }
  309. _, err := io.Copy(stdout, cStdout)
  310. if err != nil {
  311. Debugf("[error] attach stdout: %s\n", err)
  312. }
  313. errors <- err
  314. }()
  315. }
  316. }
  317. if stderr != nil {
  318. nJobs += 1
  319. if p, err := container.StderrPipe(); err != nil {
  320. errors <- err
  321. } else {
  322. cStderr = p
  323. go func() {
  324. Debugf("[start] attach stderr\n")
  325. defer Debugf("[end] attach stderr\n")
  326. // If we are in StdinOnce mode, then close stdin
  327. if container.Config.StdinOnce {
  328. if stdin != nil {
  329. defer stdin.Close()
  330. }
  331. if stdinCloser != nil {
  332. defer stdinCloser.Close()
  333. }
  334. }
  335. _, err := io.Copy(stderr, cStderr)
  336. if err != nil {
  337. Debugf("[error] attach stderr: %s\n", err)
  338. }
  339. errors <- err
  340. }()
  341. }
  342. }
  343. return Go(func() error {
  344. if cStdout != nil {
  345. defer cStdout.Close()
  346. }
  347. if cStderr != nil {
  348. defer cStderr.Close()
  349. }
  350. // FIXME: how do clean up the stdin goroutine without the unwanted side effect
  351. // of closing the passed stdin? Add an intermediary io.Pipe?
  352. for i := 0; i < nJobs; i += 1 {
  353. Debugf("Waiting for job %d/%d\n", i+1, nJobs)
  354. if err := <-errors; err != nil {
  355. Debugf("Job %d returned error %s. Aborting all jobs\n", i+1, err)
  356. return err
  357. }
  358. Debugf("Job %d completed successfully\n", i+1)
  359. }
  360. Debugf("All jobs completed successfully\n")
  361. return nil
  362. })
  363. }
  364. func (container *Container) Start() error {
  365. container.State.lock()
  366. defer container.State.unlock()
  367. if container.State.Running {
  368. return fmt.Errorf("The container %s is already running.", container.Id)
  369. }
  370. if err := container.EnsureMounted(); err != nil {
  371. return err
  372. }
  373. if err := container.allocateNetwork(); err != nil {
  374. return err
  375. }
  376. // Make sure the config is compatible with the current kernel
  377. if container.Config.Memory > 0 && !container.runtime.capabilities.MemoryLimit {
  378. log.Printf("WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n")
  379. container.Config.Memory = 0
  380. }
  381. if container.Config.Memory > 0 && !container.runtime.capabilities.SwapLimit {
  382. log.Printf("WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.\n")
  383. container.Config.MemorySwap = -1
  384. }
  385. container.Volumes = make(map[string]string)
  386. // Create the requested volumes volumes
  387. for volPath := range container.Config.Volumes {
  388. if c, err := container.runtime.volumes.Create(nil, container, "", "", nil); err != nil {
  389. return err
  390. } else {
  391. if err := os.MkdirAll(path.Join(container.RootfsPath(), volPath), 0755); err != nil {
  392. return nil
  393. }
  394. container.Volumes[volPath] = c.Id
  395. }
  396. }
  397. if container.Config.VolumesFrom != "" {
  398. c := container.runtime.Get(container.Config.VolumesFrom)
  399. if c == nil {
  400. return fmt.Errorf("Container %s not found. Impossible to mount its volumes", container.Id)
  401. }
  402. for volPath, id := range c.Volumes {
  403. if _, exists := container.Volumes[volPath]; exists {
  404. return fmt.Errorf("The requested volume %s overlap one of the volume of the container %s", volPath, c.Id)
  405. }
  406. if err := os.MkdirAll(path.Join(container.RootfsPath(), volPath), 0755); err != nil {
  407. return nil
  408. }
  409. container.Volumes[volPath] = id
  410. }
  411. }
  412. if err := container.generateLXCConfig(); err != nil {
  413. return err
  414. }
  415. params := []string{
  416. "-n", container.Id,
  417. "-f", container.lxcConfigPath(),
  418. "--",
  419. "/sbin/init",
  420. }
  421. // Networking
  422. params = append(params, "-g", container.network.Gateway.String())
  423. // User
  424. if container.Config.User != "" {
  425. params = append(params, "-u", container.Config.User)
  426. }
  427. if container.Config.Tty {
  428. params = append(params, "-e", "TERM=xterm")
  429. }
  430. // Setup environment
  431. params = append(params,
  432. "-e", "HOME=/",
  433. "-e", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  434. )
  435. for _, elem := range container.Config.Env {
  436. params = append(params, "-e", elem)
  437. }
  438. // Program
  439. params = append(params, "--", container.Path)
  440. params = append(params, container.Args...)
  441. container.cmd = exec.Command("lxc-start", params...)
  442. // Setup logging of stdout and stderr to disk
  443. if err := container.runtime.LogToDisk(container.stdout, container.logPath("stdout")); err != nil {
  444. return err
  445. }
  446. if err := container.runtime.LogToDisk(container.stderr, container.logPath("stderr")); err != nil {
  447. return err
  448. }
  449. var err error
  450. if container.Config.Tty {
  451. err = container.startPty()
  452. } else {
  453. err = container.start()
  454. }
  455. if err != nil {
  456. return err
  457. }
  458. // FIXME: save state on disk *first*, then converge
  459. // this way disk state is used as a journal, eg. we can restore after crash etc.
  460. container.State.setRunning(container.cmd.Process.Pid)
  461. // Init the lock
  462. container.waitLock = make(chan struct{})
  463. container.ToDisk()
  464. go container.monitor()
  465. return nil
  466. }
  467. func (container *Container) Run() error {
  468. if err := container.Start(); err != nil {
  469. return err
  470. }
  471. container.Wait()
  472. return nil
  473. }
  474. func (container *Container) Output() (output []byte, err error) {
  475. pipe, err := container.StdoutPipe()
  476. if err != nil {
  477. return nil, err
  478. }
  479. defer pipe.Close()
  480. if err := container.Start(); err != nil {
  481. return nil, err
  482. }
  483. output, err = ioutil.ReadAll(pipe)
  484. container.Wait()
  485. return output, err
  486. }
  487. // StdinPipe() returns a pipe connected to the standard input of the container's
  488. // active process.
  489. //
  490. func (container *Container) StdinPipe() (io.WriteCloser, error) {
  491. return container.stdinPipe, nil
  492. }
  493. func (container *Container) StdoutPipe() (io.ReadCloser, error) {
  494. reader, writer := io.Pipe()
  495. container.stdout.AddWriter(writer)
  496. return newBufReader(reader), nil
  497. }
  498. func (container *Container) StderrPipe() (io.ReadCloser, error) {
  499. reader, writer := io.Pipe()
  500. container.stderr.AddWriter(writer)
  501. return newBufReader(reader), nil
  502. }
  503. func (container *Container) allocateNetwork() error {
  504. iface, err := container.runtime.networkManager.Allocate()
  505. if err != nil {
  506. return err
  507. }
  508. container.NetworkSettings.PortMapping = make(map[string]string)
  509. for _, spec := range container.Config.PortSpecs {
  510. if nat, err := iface.AllocatePort(spec); err != nil {
  511. iface.Release()
  512. return err
  513. } else {
  514. container.NetworkSettings.PortMapping[strconv.Itoa(nat.Backend)] = strconv.Itoa(nat.Frontend)
  515. }
  516. }
  517. container.network = iface
  518. container.NetworkSettings.Bridge = container.runtime.networkManager.bridgeIface
  519. container.NetworkSettings.IpAddress = iface.IPNet.IP.String()
  520. container.NetworkSettings.IpPrefixLen, _ = iface.IPNet.Mask.Size()
  521. container.NetworkSettings.Gateway = iface.Gateway.String()
  522. return nil
  523. }
  524. func (container *Container) releaseNetwork() {
  525. container.network.Release()
  526. container.network = nil
  527. container.NetworkSettings = &NetworkSettings{}
  528. }
  529. // FIXME: replace this with a control socket within docker-init
  530. func (container *Container) waitLxc() error {
  531. for {
  532. if output, err := exec.Command("lxc-info", "-n", container.Id).CombinedOutput(); err != nil {
  533. return err
  534. } else {
  535. if !strings.Contains(string(output), "RUNNING") {
  536. return nil
  537. }
  538. }
  539. time.Sleep(500 * time.Millisecond)
  540. }
  541. return nil
  542. }
  543. func (container *Container) monitor() {
  544. // Wait for the program to exit
  545. Debugf("Waiting for process")
  546. // If the command does not exists, try to wait via lxc
  547. if container.cmd == nil {
  548. if err := container.waitLxc(); err != nil {
  549. Debugf("%s: Process: %s", container.Id, err)
  550. }
  551. } else {
  552. if err := container.cmd.Wait(); err != nil {
  553. // Discard the error as any signals or non 0 returns will generate an error
  554. Debugf("%s: Process: %s", container.Id, err)
  555. }
  556. }
  557. Debugf("Process finished")
  558. var exitCode int = -1
  559. if container.cmd != nil {
  560. exitCode = container.cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
  561. }
  562. // Cleanup
  563. container.releaseNetwork()
  564. if container.Config.OpenStdin {
  565. if err := container.stdin.Close(); err != nil {
  566. Debugf("%s: Error close stdin: %s", container.Id, err)
  567. }
  568. }
  569. if err := container.stdout.CloseWriters(); err != nil {
  570. Debugf("%s: Error close stdout: %s", container.Id, err)
  571. }
  572. if err := container.stderr.CloseWriters(); err != nil {
  573. Debugf("%s: Error close stderr: %s", container.Id, err)
  574. }
  575. if container.ptyMaster != nil {
  576. if err := container.ptyMaster.Close(); err != nil {
  577. Debugf("%s: Error closing Pty master: %s", container.Id, err)
  578. }
  579. }
  580. if err := container.Unmount(); err != nil {
  581. log.Printf("%v: Failed to umount filesystem: %v", container.Id, err)
  582. }
  583. // Re-create a brand new stdin pipe once the container exited
  584. if container.Config.OpenStdin {
  585. container.stdin, container.stdinPipe = io.Pipe()
  586. }
  587. // Report status back
  588. container.State.setStopped(exitCode)
  589. // Release the lock
  590. close(container.waitLock)
  591. if err := container.ToDisk(); err != nil {
  592. // FIXME: there is a race condition here which causes this to fail during the unit tests.
  593. // If another goroutine was waiting for Wait() to return before removing the container's root
  594. // from the filesystem... At this point it may already have done so.
  595. // This is because State.setStopped() has already been called, and has caused Wait()
  596. // to return.
  597. // FIXME: why are we serializing running state to disk in the first place?
  598. //log.Printf("%s: Failed to dump configuration to the disk: %s", container.Id, err)
  599. }
  600. }
  601. func (container *Container) kill() error {
  602. if !container.State.Running {
  603. return nil
  604. }
  605. // Sending SIGKILL to the process via lxc
  606. output, err := exec.Command("lxc-kill", "-n", container.Id, "9").CombinedOutput()
  607. if err != nil {
  608. log.Printf("error killing container %s (%s, %s)", container.Id, output, err)
  609. }
  610. // 2. Wait for the process to die, in last resort, try to kill the process directly
  611. if err := container.WaitTimeout(10 * time.Second); err != nil {
  612. if container.cmd == nil {
  613. return fmt.Errorf("lxc-kill failed, impossible to kill the container %s", container.Id)
  614. }
  615. log.Printf("Container %s failed to exit within 10 seconds of lxc SIGKILL - trying direct SIGKILL", container.Id)
  616. if err := container.cmd.Process.Kill(); err != nil {
  617. return err
  618. }
  619. }
  620. // Wait for the container to be actually stopped
  621. container.Wait()
  622. return nil
  623. }
  624. func (container *Container) Kill() error {
  625. container.State.lock()
  626. defer container.State.unlock()
  627. if !container.State.Running {
  628. return nil
  629. }
  630. return container.kill()
  631. }
  632. func (container *Container) Stop(seconds int) error {
  633. container.State.lock()
  634. defer container.State.unlock()
  635. if !container.State.Running {
  636. return nil
  637. }
  638. // 1. Send a SIGTERM
  639. if output, err := exec.Command("lxc-kill", "-n", container.Id, "15").CombinedOutput(); err != nil {
  640. log.Print(string(output))
  641. log.Print("Failed to send SIGTERM to the process, force killing")
  642. if err := container.kill(); err != nil {
  643. return err
  644. }
  645. }
  646. // 2. Wait for the process to exit on its own
  647. if err := container.WaitTimeout(time.Duration(seconds) * time.Second); err != nil {
  648. log.Printf("Container %v failed to exit within %d seconds of SIGTERM - using the force", container.Id, seconds)
  649. if err := container.kill(); err != nil {
  650. return err
  651. }
  652. }
  653. return nil
  654. }
  655. func (container *Container) Restart(seconds int) error {
  656. if err := container.Stop(seconds); err != nil {
  657. return err
  658. }
  659. if err := container.Start(); err != nil {
  660. return err
  661. }
  662. return nil
  663. }
  664. // Wait blocks until the container stops running, then returns its exit code.
  665. func (container *Container) Wait() int {
  666. <-container.waitLock
  667. return container.State.ExitCode
  668. }
  669. func (container *Container) ExportRw() (Archive, error) {
  670. return Tar(container.rwPath(), Uncompressed)
  671. }
  672. func (container *Container) RwChecksum() (string, error) {
  673. rwData, err := Tar(container.rwPath(), Xz)
  674. if err != nil {
  675. return "", err
  676. }
  677. return HashData(rwData)
  678. }
  679. func (container *Container) Export() (Archive, error) {
  680. if err := container.EnsureMounted(); err != nil {
  681. return nil, err
  682. }
  683. return Tar(container.RootfsPath(), Uncompressed)
  684. }
  685. func (container *Container) WaitTimeout(timeout time.Duration) error {
  686. done := make(chan bool)
  687. go func() {
  688. container.Wait()
  689. done <- true
  690. }()
  691. select {
  692. case <-time.After(timeout):
  693. return fmt.Errorf("Timed Out")
  694. case <-done:
  695. return nil
  696. }
  697. panic("unreachable")
  698. }
  699. func (container *Container) EnsureMounted() error {
  700. if mounted, err := container.Mounted(); err != nil {
  701. return err
  702. } else if mounted {
  703. return nil
  704. }
  705. return container.Mount()
  706. }
  707. func (container *Container) Mount() error {
  708. image, err := container.GetImage()
  709. if err != nil {
  710. return err
  711. }
  712. return image.Mount(container.RootfsPath(), container.rwPath())
  713. }
  714. func (container *Container) Changes() ([]Change, error) {
  715. image, err := container.GetImage()
  716. if err != nil {
  717. return nil, err
  718. }
  719. return image.Changes(container.rwPath())
  720. }
  721. func (container *Container) GetImage() (*Image, error) {
  722. if container.runtime == nil {
  723. return nil, fmt.Errorf("Can't get image of unregistered container")
  724. }
  725. return container.runtime.graph.Get(container.Image)
  726. }
  727. func (container *Container) Mounted() (bool, error) {
  728. return Mounted(container.RootfsPath())
  729. }
  730. func (container *Container) Unmount() error {
  731. return Unmount(container.RootfsPath())
  732. }
  733. // ShortId returns a shorthand version of the container's id for convenience.
  734. // A collision with other container shorthands is very unlikely, but possible.
  735. // In case of a collision a lookup with Runtime.Get() will fail, and the caller
  736. // will need to use a langer prefix, or the full-length container Id.
  737. func (container *Container) ShortId() string {
  738. return TruncateId(container.Id)
  739. }
  740. func (container *Container) logPath(name string) string {
  741. return path.Join(container.root, fmt.Sprintf("%s-%s.log", container.Id, name))
  742. }
  743. func (container *Container) ReadLog(name string) (io.Reader, error) {
  744. return os.Open(container.logPath(name))
  745. }
  746. func (container *Container) jsonPath() string {
  747. return path.Join(container.root, "config.json")
  748. }
  749. func (container *Container) lxcConfigPath() string {
  750. return path.Join(container.root, "config.lxc")
  751. }
  752. // This method must be exported to be used from the lxc template
  753. func (container *Container) RootfsPath() string {
  754. return path.Join(container.root, "rootfs")
  755. }
  756. func (container *Container) GetVolumes() (map[string]string, error) {
  757. ret := make(map[string]string)
  758. for volPath, id := range container.Volumes {
  759. volume, err := container.runtime.volumes.Get(id)
  760. if err != nil {
  761. return nil, err
  762. }
  763. root, err := volume.root()
  764. if err != nil {
  765. return nil, err
  766. }
  767. ret[volPath] = path.Join(root, "layer")
  768. }
  769. return ret, nil
  770. }
  771. func (container *Container) rwPath() string {
  772. return path.Join(container.root, "rw")
  773. }
  774. func validateId(id string) error {
  775. if id == "" {
  776. return fmt.Errorf("Invalid empty id")
  777. }
  778. return nil
  779. }
  780. // GetSize, return real size, virtual size
  781. func (container *Container) GetSize() (int64, int64) {
  782. var sizeRw, sizeRootfs int64
  783. filepath.Walk(container.rwPath(), func(path string, fileInfo os.FileInfo, err error) error {
  784. if fileInfo != nil {
  785. sizeRw += fileInfo.Size()
  786. }
  787. return nil
  788. })
  789. _, err := os.Stat(container.RootfsPath())
  790. if err == nil {
  791. filepath.Walk(container.RootfsPath(), func(path string, fileInfo os.FileInfo, err error) error {
  792. if fileInfo != nil {
  793. sizeRootfs += fileInfo.Size()
  794. }
  795. return nil
  796. })
  797. }
  798. return sizeRw, sizeRootfs
  799. }