docker_init.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package control
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "strings"
  8. "syscall"
  9. "time"
  10. "github.com/codegangsta/cli"
  11. "github.com/rancher/os/config"
  12. "github.com/rancher/os/log"
  13. "github.com/rancher/os/util"
  14. )
  15. const (
  16. dockerConf = "/var/lib/rancher/conf/docker"
  17. dockerDone = "/run/docker-done"
  18. dockerLog = "/var/log/docker.log"
  19. )
  20. func dockerInitAction(c *cli.Context) error {
  21. for {
  22. if _, err := os.Stat(consoleDone); err == nil {
  23. break
  24. }
  25. time.Sleep(200 * time.Millisecond)
  26. }
  27. dockerBin := "/usr/bin/docker"
  28. for _, binPath := range []string{
  29. "/opt/bin",
  30. "/usr/local/bin",
  31. "/var/lib/rancher/docker",
  32. } {
  33. if util.ExistsAndExecutable(path.Join(binPath, "dockerd")) {
  34. dockerBin = path.Join(binPath, "dockerd")
  35. break
  36. }
  37. if util.ExistsAndExecutable(path.Join(binPath, "docker")) {
  38. dockerBin = path.Join(binPath, "docker")
  39. break
  40. }
  41. }
  42. if err := syscall.Mount("", "/", "", syscall.MS_SHARED|syscall.MS_REC, ""); err != nil {
  43. log.Error(err)
  44. }
  45. if err := syscall.Mount("", "/run", "", syscall.MS_SHARED|syscall.MS_REC, ""); err != nil {
  46. log.Error(err)
  47. }
  48. mountInfo, err := ioutil.ReadFile("/proc/self/mountinfo")
  49. if err != nil {
  50. return err
  51. }
  52. for _, mount := range strings.Split(string(mountInfo), "\n") {
  53. if strings.Contains(mount, "/var/lib/docker /var/lib/docker") && strings.Contains(mount, "rootfs") {
  54. os.Setenv("DOCKER_RAMDISK", "1")
  55. }
  56. }
  57. args := []string{
  58. "bash",
  59. "-c",
  60. fmt.Sprintf(`[ -e %s ] && source %s; exec /usr/bin/dockerlaunch %s %s $DOCKER_OPTS >> %s 2>&1`, dockerConf, dockerConf, dockerBin, strings.Join(c.Args(), " "), dockerLog),
  61. }
  62. cfg := config.LoadConfig()
  63. if err := ioutil.WriteFile(dockerDone, []byte(cfg.Rancher.Docker.Engine), 0644); err != nil {
  64. log.Error(err)
  65. }
  66. return syscall.Exec("/bin/bash", args, os.Environ())
  67. }