bootstrap.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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: Rngd(%v)", cfg.Rancher.State.Rngd)
  21. if cfg.Rancher.State.Rngd {
  22. if err := runRngd(); err != nil {
  23. log.Errorf("Failed to run rngd: %v", err)
  24. }
  25. }
  26. log.Debugf("bootstrapAction: MdadmScan(%v)", cfg.Rancher.State.MdadmScan)
  27. if cfg.Rancher.State.MdadmScan {
  28. if err := mdadmScan(); err != nil {
  29. log.Errorf("Failed to run mdadm scan: %v", err)
  30. }
  31. }
  32. stateScript := cfg.Rancher.State.Script
  33. log.Debugf("bootstrapAction: stateScript(%v)", stateScript)
  34. if stateScript != "" {
  35. if err := runStateScript(stateScript); err != nil {
  36. log.Errorf("Failed to run state script: %v", err)
  37. }
  38. }
  39. log.Debugf("bootstrapAction: RunCommandSequence(%v)", cfg.Bootcmd)
  40. err := util.RunCommandSequence(cfg.Bootcmd)
  41. if err != nil {
  42. log.Error(err)
  43. }
  44. if cfg.Rancher.State.Dev != "" && cfg.Rancher.State.Wait {
  45. waitForRoot(cfg)
  46. }
  47. if len(cfg.Rancher.State.Autoformat) > 0 {
  48. log.Infof("bootstrap container: Autoformat(%v) as %s", cfg.Rancher.State.Autoformat, "ext4")
  49. if err := autoformat(cfg.Rancher.State.Autoformat); err != nil {
  50. log.Errorf("Failed to run autoformat: %v", err)
  51. }
  52. }
  53. log.Debugf("bootstrapAction: udev settle2")
  54. if err := UdevSettle(); err != nil {
  55. log.Errorf("Failed to run udev settle: %v", err)
  56. }
  57. }
  58. func mdadmScan() error {
  59. cmd := exec.Command("mdadm", "--assemble", "--scan")
  60. cmd.Stdout = os.Stdout
  61. cmd.Stderr = os.Stderr
  62. return cmd.Run()
  63. }
  64. func runRngd() error {
  65. // use /dev/urandom as random number input for rngd
  66. // this is a really bad idea
  67. // since I am simple filling the kernel entropy pool with entropy coming from the kernel itself!
  68. // but this does not need to consider the user's hw rngd drivers.
  69. cmd := exec.Command("rngd", "-r", "/dev/urandom", "-q")
  70. cmd.Stdout = os.Stdout
  71. cmd.Stderr = os.Stderr
  72. return cmd.Run()
  73. }
  74. func runStateScript(script string) error {
  75. f, err := ioutil.TempFile("", "")
  76. if err != nil {
  77. return err
  78. }
  79. if _, err := f.WriteString(script); err != nil {
  80. return err
  81. }
  82. if err := f.Chmod(os.ModePerm); err != nil {
  83. return err
  84. }
  85. if err := f.Close(); err != nil {
  86. return err
  87. }
  88. return util.RunScript(f.Name())
  89. }
  90. func waitForRoot(cfg *config.CloudConfig) {
  91. var dev string
  92. for i := 0; i < 30; i++ {
  93. dev = util.ResolveDevice(cfg.Rancher.State.Dev)
  94. if dev != "" {
  95. break
  96. }
  97. time.Sleep(time.Millisecond * 1000)
  98. }
  99. if dev == "" {
  100. return
  101. }
  102. for i := 0; i < 30; i++ {
  103. if _, err := os.Stat(dev); err == nil {
  104. break
  105. }
  106. time.Sleep(time.Millisecond * 1000)
  107. }
  108. }
  109. func autoformat(autoformatDevices []string) error {
  110. cmd := exec.Command("/usr/sbin/auto-format.sh")
  111. cmd.Stdout = os.Stdout
  112. cmd.Stderr = os.Stderr
  113. cmd.Env = []string{
  114. "AUTOFORMAT=" + strings.Join(autoformatDevices, " "),
  115. }
  116. return cmd.Run()
  117. }