bootstrap.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package control
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "os/exec"
  6. "strings"
  7. "time"
  8. "github.com/codegangsta/cli"
  9. "github.com/rancher/os/config"
  10. "github.com/rancher/os/log"
  11. "github.com/rancher/os/util"
  12. )
  13. func bootstrapAction(c *cli.Context) error {
  14. log.Debugf("bootstrapAction")
  15. if err := UdevSettle(); err != nil {
  16. log.Errorf("Failed to run udev settle: %v", err)
  17. }
  18. log.Debugf("bootstrapAction: loadingConfig")
  19. cfg := config.LoadConfig()
  20. log.Debugf("bootstrapAction: MdadmScan(%v)", cfg.Rancher.State.MdadmScan)
  21. if cfg.Rancher.State.MdadmScan {
  22. if err := mdadmScan(); err != nil {
  23. log.Errorf("Failed to run mdadm scan: %v", err)
  24. }
  25. }
  26. stateScript := cfg.Rancher.State.Script
  27. log.Debugf("bootstrapAction: stateScript(%v)", stateScript)
  28. if stateScript != "" {
  29. if err := runStateScript(stateScript); err != nil {
  30. log.Errorf("Failed to run state script: %v", err)
  31. }
  32. }
  33. log.Debugf("bootstrapAction: RunCommandSequence(%v)", cfg.Bootcmd)
  34. util.RunCommandSequence(cfg.Bootcmd)
  35. if cfg.Rancher.State.Dev != "" && cfg.Rancher.State.Wait {
  36. waitForRoot(cfg)
  37. }
  38. if len(cfg.Rancher.State.Autoformat) > 0 {
  39. log.Infof("bootstrap container: Autoformat(%v) as %s", cfg.Rancher.State.Autoformat, "ext4")
  40. if err := autoformat(cfg.Rancher.State.Autoformat); err != nil {
  41. log.Errorf("Failed to run autoformat: %v", err)
  42. }
  43. }
  44. log.Debugf("bootstrapAction: udev settle2")
  45. if err := UdevSettle(); err != nil {
  46. log.Errorf("Failed to run udev settle: %v", err)
  47. }
  48. return nil
  49. }
  50. func mdadmScan() error {
  51. cmd := exec.Command("mdadm", "--assemble", "--scan")
  52. cmd.Stdout = os.Stdout
  53. cmd.Stderr = os.Stderr
  54. return cmd.Run()
  55. }
  56. func runStateScript(script string) error {
  57. f, err := ioutil.TempFile("", "")
  58. if err != nil {
  59. return err
  60. }
  61. if _, err := f.WriteString(script); err != nil {
  62. return err
  63. }
  64. if err := f.Chmod(os.ModePerm); err != nil {
  65. return err
  66. }
  67. if err := f.Close(); err != nil {
  68. return err
  69. }
  70. return util.RunScript(f.Name())
  71. }
  72. func waitForRoot(cfg *config.CloudConfig) {
  73. var dev string
  74. for i := 0; i < 30; i++ {
  75. dev = util.ResolveDevice(cfg.Rancher.State.Dev)
  76. if dev != "" {
  77. break
  78. }
  79. time.Sleep(time.Millisecond * 1000)
  80. }
  81. if dev == "" {
  82. return
  83. }
  84. for i := 0; i < 30; i++ {
  85. if _, err := os.Stat(dev); err == nil {
  86. break
  87. }
  88. time.Sleep(time.Millisecond * 1000)
  89. }
  90. }
  91. func autoformat(autoformatDevices []string) error {
  92. cmd := exec.Command("/usr/sbin/auto-format.sh")
  93. cmd.Stdout = os.Stdout
  94. cmd.Stderr = os.Stderr
  95. cmd.Env = []string{
  96. "AUTOFORMAT=" + strings.Join(autoformatDevices, " "),
  97. }
  98. return cmd.Run()
  99. }