docker_init.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package control
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "strings"
  8. "syscall"
  9. "time"
  10. "github.com/rancher/os/config"
  11. "github.com/rancher/os/pkg/log"
  12. "github.com/rancher/os/pkg/util"
  13. "github.com/codegangsta/cli"
  14. )
  15. const (
  16. dockerConf = "/var/lib/rancher/conf/docker"
  17. dockerDone = "/run/docker-done"
  18. dockerLog = "/var/log/docker.log"
  19. dockerCompletionLinkFile = "/usr/share/bash-completion/completions/docker"
  20. dockerCompletionFile = "/var/lib/rancher/engine/completion"
  21. )
  22. func dockerInitAction(c *cli.Context) error {
  23. // TODO: this should be replaced by a "Console ready event watcher"
  24. for {
  25. if _, err := os.Stat(consoleDone); err == nil {
  26. break
  27. }
  28. time.Sleep(200 * time.Millisecond)
  29. }
  30. if _, err := os.Stat(dockerCompletionFile); err != nil {
  31. if _, err := os.Readlink(dockerCompletionLinkFile); err == nil {
  32. syscall.Unlink(dockerCompletionLinkFile)
  33. }
  34. }
  35. dockerBin := ""
  36. dockerPaths := []string{
  37. "/usr/bin",
  38. "/opt/bin",
  39. "/usr/local/bin",
  40. "/var/lib/rancher/docker",
  41. }
  42. for _, binPath := range dockerPaths {
  43. if util.ExistsAndExecutable(path.Join(binPath, "dockerd")) {
  44. dockerBin = path.Join(binPath, "dockerd")
  45. break
  46. }
  47. }
  48. if dockerBin == "" {
  49. for _, binPath := range dockerPaths {
  50. if util.ExistsAndExecutable(path.Join(binPath, "docker")) {
  51. dockerBin = path.Join(binPath, "docker")
  52. break
  53. }
  54. }
  55. }
  56. if dockerBin == "" {
  57. err := fmt.Errorf("Failed to find either dockerd or docker binaries")
  58. log.Error(err)
  59. return err
  60. }
  61. log.Infof("Found %s", dockerBin)
  62. if err := syscall.Mount("", "/", "", syscall.MS_SHARED|syscall.MS_REC, ""); err != nil {
  63. log.Error(err)
  64. }
  65. if err := syscall.Mount("", "/run", "", syscall.MS_SHARED|syscall.MS_REC, ""); err != nil {
  66. log.Error(err)
  67. }
  68. mountInfo, err := ioutil.ReadFile("/proc/self/mountinfo")
  69. if err != nil {
  70. return err
  71. }
  72. for _, mount := range strings.Split(string(mountInfo), "\n") {
  73. if strings.Contains(mount, "/var/lib/user-docker /var/lib/docker") && strings.Contains(mount, "rootfs") {
  74. os.Setenv("DOCKER_RAMDISK", "true")
  75. }
  76. }
  77. cfg := config.LoadConfig()
  78. baseSymlink := symLinkEngineBinary(cfg.Rancher.Docker.Engine)
  79. for _, link := range baseSymlink {
  80. syscall.Unlink(link.newname)
  81. if err := os.Symlink(link.oldname, link.newname); err != nil {
  82. log.Error(err)
  83. }
  84. }
  85. err = checkZfsBackingFS(cfg.Rancher.Docker.StorageDriver, cfg.Rancher.Docker.Graph)
  86. if err != nil {
  87. log.Fatal(err)
  88. }
  89. args := []string{
  90. "bash",
  91. "-c",
  92. 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),
  93. }
  94. // TODO: this should be replaced by a "Docker ready event watcher"
  95. if err := ioutil.WriteFile(dockerDone, []byte(CurrentEngine()), 0644); err != nil {
  96. log.Error(err)
  97. }
  98. return syscall.Exec("/bin/bash", args, os.Environ())
  99. }