bootstrap.go 2.5 KB

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