docker_init.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/log"
  12. "github.com/rancher/os/util"
  13. )
  14. const (
  15. dockerConf = "/var/lib/rancher/conf/docker"
  16. dockerDone = "/run/docker-done"
  17. dockerLog = "/var/log/docker.log"
  18. )
  19. func dockerInitAction(c *cli.Context) error {
  20. // TODO: this should be replaced by a "Console ready event watcher"
  21. for {
  22. if _, err := os.Stat(consoleDone); err == nil {
  23. break
  24. }
  25. time.Sleep(200 * time.Millisecond)
  26. }
  27. dockerBin := ""
  28. dockerPaths := []string{
  29. "/usr/bin",
  30. "/opt/bin",
  31. "/usr/local/bin",
  32. "/var/lib/rancher/docker",
  33. }
  34. for _, binPath := range dockerPaths {
  35. if util.ExistsAndExecutable(path.Join(binPath, "dockerd")) {
  36. dockerBin = path.Join(binPath, "dockerd")
  37. break
  38. }
  39. }
  40. if dockerBin == "" {
  41. for _, binPath := range dockerPaths {
  42. if util.ExistsAndExecutable(path.Join(binPath, "docker")) {
  43. dockerBin = path.Join(binPath, "docker")
  44. break
  45. }
  46. }
  47. }
  48. if dockerBin == "" {
  49. err := fmt.Errorf("Failed to find either dockerd or docker binaries")
  50. log.Error(err)
  51. return err
  52. }
  53. log.Infof("Found %s", dockerBin)
  54. if err := syscall.Mount("", "/", "", syscall.MS_SHARED|syscall.MS_REC, ""); err != nil {
  55. log.Error(err)
  56. }
  57. if err := syscall.Mount("", "/run", "", syscall.MS_SHARED|syscall.MS_REC, ""); err != nil {
  58. log.Error(err)
  59. }
  60. mountInfo, err := ioutil.ReadFile("/proc/self/mountinfo")
  61. if err != nil {
  62. return err
  63. }
  64. for _, mount := range strings.Split(string(mountInfo), "\n") {
  65. if strings.Contains(mount, "/var/lib/user-docker /var/lib/docker") && strings.Contains(mount, "rootfs") {
  66. os.Setenv("DOCKER_RAMDISK", "true")
  67. }
  68. }
  69. args := []string{
  70. "bash",
  71. "-c",
  72. 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),
  73. }
  74. // TODO: this should be replaced by a "Docker ready event watcher"
  75. if err := ioutil.WriteFile(dockerDone, []byte(CurrentEngine()), 0644); err != nil {
  76. log.Error(err)
  77. }
  78. return syscall.Exec("/bin/bash", args, os.Environ())
  79. }