1
0
mirror of https://github.com/rancher/os.git synced 2025-09-01 14:48:55 +00:00

Refactor state

This commit is contained in:
Josh Curl
2016-07-27 23:25:08 -07:00
parent 1877cfa16b
commit 6b4222888c
8 changed files with 91 additions and 32 deletions

View File

@@ -19,7 +19,8 @@ import (
)
const (
STATE string = "/state"
STATE string = "/state"
BOOT2DOCKER_MAGIC string = "boot2docker, please format-me"
TMPFS_MAGIC int64 = 0x01021994
RAMFS_MAGIC int64 = 0x858458f6
@@ -143,18 +144,21 @@ func tryMountState(cfg *config.CloudConfig) error {
}
func tryMountAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, error) {
if isInitrd() {
if err := tryMountState(cfg); !cfg.Rancher.State.Required && err != nil {
return cfg, nil
} else if err != nil {
return cfg, err
}
log.Debugf("Switching to new root at %s %s", STATE, cfg.Rancher.State.Directory)
if err := switchRoot(STATE, cfg.Rancher.State.Directory, cfg.Rancher.RmUsr); err != nil {
return cfg, err
}
if !isInitrd() || cfg.Rancher.State.Dev == "" {
return cfg, nil
}
if err := tryMountState(cfg); !cfg.Rancher.State.Required && err != nil {
return cfg, nil
} else if err != nil {
return cfg, err
}
log.Debugf("Switching to new root at %s %s", STATE, cfg.Rancher.State.Directory)
if err := switchRoot(STATE, cfg.Rancher.State.Directory, cfg.Rancher.RmUsr); err != nil {
return cfg, err
}
return mountOem(cfg)
}
@@ -213,6 +217,7 @@ func RunInit() error {
log.Debug("Booting off a persistent filesystem")
}
boot2DockerEnvironment := false
initFuncs := []config.CfgFunc{
func(c *config.CloudConfig) (*config.CloudConfig, error) {
return c, dockerlaunch.PrepareFs(&mountConfig)
@@ -233,8 +238,44 @@ func RunInit() error {
return cfg, nil
},
loadModules,
func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
if util.ResolveDevice("LABEL=B2D_STATE") != "" {
boot2DockerEnvironment = true
cfg.Rancher.State.Dev = "LABEL=B2D_STATE"
return cfg, nil
}
devices := []string{"/dev/sda", "/dev/vda"}
data := make([]byte, len(BOOT2DOCKER_MAGIC))
for _, device := range devices {
f, err := os.Open(device)
if err == nil {
defer f.Close()
_, err = f.Read(data)
if err == nil && string(data) == BOOT2DOCKER_MAGIC {
boot2DockerEnvironment = true
cfg.Rancher.State.Dev = "LABEL=B2D_STATE"
cfg.Rancher.State.Autoformat = []string{device}
break
}
}
}
return cfg, nil
},
tryMountAndBootstrap,
func(_ *config.CloudConfig) (*config.CloudConfig, error) {
func(cfg *config.CloudConfig) (*config.CloudConfig, error) {
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
},
loadModules,