This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 657
/
b2d.go
81 lines (66 loc) · 2.28 KB
/
b2d.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package b2d
import (
"os"
"github.com/rancher/os/config"
"github.com/rancher/os/pkg/init/configfiles"
"github.com/rancher/os/pkg/log"
"github.com/rancher/os/pkg/util"
)
const (
boot2DockerMagic string = "boot2docker, please format-me"
)
var (
boot2DockerEnvironment bool
)
func B2D(cfg *config.CloudConfig) (*config.CloudConfig, error) {
if _, err := os.Stat("/var/lib/boot2docker"); os.IsNotExist(err) {
err := os.Mkdir("/var/lib/boot2docker", 0755)
if err != nil {
log.Errorf("Failed to create boot2docker directory: %v", err)
}
}
if dev := util.ResolveDevice("LABEL=B2D_STATE"); dev != "" {
boot2DockerEnvironment = true
cfg.Rancher.State.Dev = "LABEL=B2D_STATE"
log.Infof("boot2DockerEnvironment %s: %s", cfg.Rancher.State.Dev, dev)
return cfg, nil
}
devices := []string{"/dev/sda", "/dev/vda"}
data := make([]byte, len(boot2DockerMagic))
for _, device := range devices {
f, err := os.Open(device)
if err == nil {
defer f.Close()
_, err = f.Read(data)
if err == nil && string(data) == boot2DockerMagic {
boot2DockerEnvironment = true
cfg.Rancher.State.Dev = "LABEL=B2D_STATE"
cfg.Rancher.State.Autoformat = []string{device}
log.Infof("boot2DockerEnvironment %s: Autoformat %s", cfg.Rancher.State.Dev, cfg.Rancher.State.Autoformat[0])
break
}
}
}
// save here so the bootstrap service can see it (when booting from iso, its very early)
if boot2DockerEnvironment {
if err := config.Set("rancher.state.dev", cfg.Rancher.State.Dev); err != nil {
log.Errorf("Failed to update rancher.state.dev: %v", err)
}
if err := config.Set("rancher.state.autoformat", cfg.Rancher.State.Autoformat); err != nil {
log.Errorf("Failed to update rancher.state.autoformat: %v", err)
}
}
return config.LoadConfig(), nil
}
func Env(cfg *config.CloudConfig) (*config.CloudConfig, error) {
log.Debugf("memory Resolve.conf == [%s]", configfiles.ConfigFiles["/etc/resolv.conf"])
if boot2DockerEnvironment {
if err := config.Set("rancher.state.dev", cfg.Rancher.State.Dev); err != nil {
log.Errorf("Failed to update rancher.state.dev: %v", err)
}
if err := config.Set("rancher.state.autoformat", cfg.Rancher.State.Autoformat); err != nil {
log.Errorf("Failed to update rancher.state.autoformat: %v", err)
}
}
return config.LoadConfig(), nil
}