b2d.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package b2d
  2. import (
  3. "os"
  4. "github.com/rancher/os/config"
  5. "github.com/rancher/os/pkg/init/configfiles"
  6. "github.com/rancher/os/pkg/log"
  7. "github.com/rancher/os/pkg/util"
  8. )
  9. const (
  10. boot2DockerMagic string = "boot2docker, please format-me"
  11. )
  12. var (
  13. boot2DockerEnvironment bool
  14. )
  15. func B2D(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  16. if _, err := os.Stat("/var/lib/boot2docker"); os.IsNotExist(err) {
  17. err := os.Mkdir("/var/lib/boot2docker", 0755)
  18. if err != nil {
  19. log.Errorf("Failed to create boot2docker directory: %v", err)
  20. }
  21. }
  22. if dev := util.ResolveDevice("LABEL=B2D_STATE"); dev != "" {
  23. boot2DockerEnvironment = true
  24. cfg.Rancher.State.Dev = "LABEL=B2D_STATE"
  25. log.Infof("boot2DockerEnvironment %s: %s", cfg.Rancher.State.Dev, dev)
  26. return cfg, nil
  27. }
  28. devices := []string{"/dev/sda", "/dev/vda"}
  29. data := make([]byte, len(boot2DockerMagic))
  30. for _, device := range devices {
  31. f, err := os.Open(device)
  32. if err == nil {
  33. defer f.Close()
  34. _, err = f.Read(data)
  35. if err == nil && string(data) == boot2DockerMagic {
  36. boot2DockerEnvironment = true
  37. cfg.Rancher.State.Dev = "LABEL=B2D_STATE"
  38. cfg.Rancher.State.Autoformat = []string{device}
  39. log.Infof("boot2DockerEnvironment %s: Autoformat %s", cfg.Rancher.State.Dev, cfg.Rancher.State.Autoformat[0])
  40. break
  41. }
  42. }
  43. }
  44. // save here so the bootstrap service can see it (when booting from iso, its very early)
  45. if boot2DockerEnvironment {
  46. if err := config.Set("rancher.state.dev", cfg.Rancher.State.Dev); err != nil {
  47. log.Errorf("Failed to update rancher.state.dev: %v", err)
  48. }
  49. if err := config.Set("rancher.state.autoformat", cfg.Rancher.State.Autoformat); err != nil {
  50. log.Errorf("Failed to update rancher.state.autoformat: %v", err)
  51. }
  52. }
  53. return config.LoadConfig(), nil
  54. }
  55. func Env(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  56. log.Debugf("memory Resolve.conf == [%s]", configfiles.ConfigFiles["/etc/resolv.conf"])
  57. if boot2DockerEnvironment {
  58. if err := config.Set("rancher.state.dev", cfg.Rancher.State.Dev); err != nil {
  59. log.Errorf("Failed to update rancher.state.dev: %v", err)
  60. }
  61. if err := config.Set("rancher.state.autoformat", cfg.Rancher.State.Autoformat); err != nil {
  62. log.Errorf("Failed to update rancher.state.autoformat: %v", err)
  63. }
  64. }
  65. return config.LoadConfig(), nil
  66. }