mirror of
https://github.com/rancher/os.git
synced 2025-09-02 07:15:41 +00:00
Refactor state
This commit is contained in:
@@ -149,6 +149,7 @@ type StateConfig struct {
|
||||
Directory string `yaml:"directory,omitempty"`
|
||||
FsType string `yaml:"fstype,omitempty"`
|
||||
Dev string `yaml:"dev,omitempty"`
|
||||
Wait bool `yaml:"wait,omitempty"`
|
||||
Required bool `yaml:"required,omitempty"`
|
||||
Autoformat []string `yaml:"autoformat,omitempty"`
|
||||
FormatZero bool `yaml:"formatzero,omitempty"`
|
||||
|
@@ -24,9 +24,9 @@ for dev in ${DEVS[@]}; do
|
||||
continue
|
||||
fi
|
||||
|
||||
mkfs.ext4 -L RANCHER_STATE ${dev}
|
||||
|
||||
if [ -e "/userdata.tar" ]; then
|
||||
mkfs.ext4 -L B2D_STATE ${dev}
|
||||
mkdir -p /mnt/new-root
|
||||
mount -t ext4 ${dev} /mnt/new-root
|
||||
pushd /mnt/new-root
|
||||
@@ -59,6 +59,8 @@ users:
|
||||
EOF
|
||||
popd
|
||||
umount /mnt/new-root
|
||||
else
|
||||
mkfs.ext4 -L RANCHER_STATE ${dev}
|
||||
fi
|
||||
|
||||
# do not check another device
|
||||
|
@@ -8,11 +8,26 @@ udevd --daemon
|
||||
udevadm trigger --action=add
|
||||
udevadm settle
|
||||
|
||||
if [ "$BOOTSTRAP" = true ]; then
|
||||
# This was needed to get USB devices to fully register
|
||||
# There is probably a better way to do this
|
||||
killall udevd
|
||||
udevd --daemon
|
||||
udevadm trigger --action=add
|
||||
udevadm settle
|
||||
dev=$(ros config get rancher.state.dev)
|
||||
wait=$(ros config get rancher.state.wait)
|
||||
if [ "$BOOTSTRAP" != true ] || [ "$dev" == "" ] || [ "$wait" != "true" ]; then
|
||||
exit
|
||||
fi
|
||||
|
||||
for i in `seq 1 30`; do
|
||||
drive=$(ros dev $dev)
|
||||
if [ "$drive" != "" ]; then
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
drive=$(ros dev $dev)
|
||||
if [ "$drive" = "" ]; then
|
||||
exit
|
||||
fi
|
||||
for i in `seq 1 30`; do
|
||||
if [ -e $drive ]; then
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
47
init/init.go
47
init/init.go
@@ -20,6 +20,7 @@ import (
|
||||
|
||||
const (
|
||||
STATE string = "/state"
|
||||
BOOT2DOCKER_MAGIC string = "boot2docker, please format-me"
|
||||
|
||||
TMPFS_MAGIC int64 = 0x01021994
|
||||
RAMFS_MAGIC int64 = 0x858458f6
|
||||
@@ -143,7 +144,10 @@ func tryMountState(cfg *config.CloudConfig) error {
|
||||
}
|
||||
|
||||
func tryMountAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, error) {
|
||||
if isInitrd() {
|
||||
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 {
|
||||
@@ -154,7 +158,7 @@ func tryMountAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, error)
|
||||
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,
|
||||
|
@@ -39,6 +39,7 @@ rancher:
|
||||
- /dev:/host/dev
|
||||
- /lib/modules:/lib/modules
|
||||
- /lib/firmware:/lib/firmware
|
||||
- /usr/bin/ros:/usr/bin/ros:ro
|
||||
autoformat:
|
||||
autoformat:
|
||||
image: {{.OS_REPO}}/os-autoformat:{{.VERSION}}{{.SUFFIX}}
|
||||
@@ -74,7 +75,6 @@ rancher:
|
||||
url: {{.OS_SERVICES_REPO}}/{{.REPO_VERSION}}
|
||||
state:
|
||||
fstype: auto
|
||||
dev: LABEL=RANCHER_STATE
|
||||
oem_fstype: auto
|
||||
oem_dev: LABEL=RANCHER_OEM
|
||||
services:
|
||||
|
@@ -85,7 +85,7 @@ set timeout="1"
|
||||
|
||||
menuentry "RancherOS-current" {
|
||||
set root=(hd0,msdos1)
|
||||
linux /boot/vmlinuz-${VERSION}-rancheros ${append_line} console=${CONSOLE}
|
||||
linux /boot/vmlinuz-${VERSION}-rancheros ${append_line} rancher.state.dev=LABEL=RANCHER_STATE rancher.state.wait console=${CONSOLE}
|
||||
initrd /boot/initrd-${VERSION}-rancheros
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ if [ ! -z ${ROLLBACK_VERSION} ]; then
|
||||
cat >>${grub_cfg} <<EOF
|
||||
menuentry "RancherOS-rollback" {
|
||||
set root=(hd0,msdos1)
|
||||
linux /boot/vmlinuz-${ROLLBACK_VERSION}-rancheros ${append_line} console=${CONSOLE}
|
||||
linux /boot/vmlinuz-${ROLLBACK_VERSION}-rancheros ${append_line} rancher.state.dev=LABEL=RANCHER_STATE rancher.state.wait console=${CONSOLE}
|
||||
initrd /boot/initrd-${ROLLBACK_VERSION}-rancheros
|
||||
}
|
||||
EOF
|
||||
@@ -123,7 +123,7 @@ hiddenmenu
|
||||
|
||||
title RancherOS ${VERSION}-(current)
|
||||
root (hd0)
|
||||
kernel /boot/vmlinuz-${VERSION}-rancheros ${append_line} console=${CONSOLE}
|
||||
kernel /boot/vmlinuz-${VERSION}-rancheros ${append_line} rancher.state.dev=LABEL=RANCHER_STATE rancher.state.wait console=${CONSOLE}
|
||||
initrd /boot/initrd-${VERSION}-rancheros
|
||||
|
||||
EOF
|
||||
@@ -133,7 +133,7 @@ if [ ! -z ${ROLLBACK_VERSION} ]; then
|
||||
cat >> ${grub_file}<<EOF
|
||||
title RancherOS ${ROLLBACK_VERSION}-(rollback)
|
||||
root (hd0)
|
||||
kernel /boot/vmlinuz-${ROLLBACK_VERSION}-rancheros ${append_line} console=${CONSOLE}
|
||||
kernel /boot/vmlinuz-${ROLLBACK_VERSION}-rancheros ${append_line} rancher.state.dev=LABEL=RANCHER_STATE rancher.state.wait console=${CONSOLE}
|
||||
initrd /boot/initrd-${ROLLBACK_VERSION}-rancheros
|
||||
EOF
|
||||
fi
|
||||
|
@@ -2,4 +2,4 @@ default rancheros
|
||||
label rancheros
|
||||
kernel /boot/vmlinuz
|
||||
initrd /boot/initrd
|
||||
append quiet rancher.password=rancher rancher.state.autoformat=[/dev/sda,/dev/vda]
|
||||
append quiet rancher.password=rancher
|
||||
|
@@ -122,7 +122,7 @@ fi
|
||||
|
||||
KERNEL_ARGS="quiet rancher.password=rancher console=${TTYCONS} ${QEMU_APPEND}"
|
||||
if [ "$FORMAT" == "1" ]; then
|
||||
KERNEL_ARGS="${KERNEL_ARGS} rancher.state.formatzero=true rancher.state.autoformat=[/dev/sda,/dev/vda]"
|
||||
KERNEL_ARGS="${KERNEL_ARGS} rancher.state.dev=LABEL=RANCHER_STATE rancher.state.formatzero=true rancher.state.autoformat=[/dev/sda,/dev/vda]"
|
||||
fi
|
||||
if [ "$RM_USR" == "1" ]; then
|
||||
KERNEL_ARGS="${KERNEL_ARGS} rancher.rm_usr"
|
||||
|
Reference in New Issue
Block a user