1
0
mirror of https://github.com/rancher/os.git synced 2025-09-02 15:24:32 +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

@@ -149,6 +149,7 @@ type StateConfig struct {
Directory string `yaml:"directory,omitempty"` Directory string `yaml:"directory,omitempty"`
FsType string `yaml:"fstype,omitempty"` FsType string `yaml:"fstype,omitempty"`
Dev string `yaml:"dev,omitempty"` Dev string `yaml:"dev,omitempty"`
Wait bool `yaml:"wait,omitempty"`
Required bool `yaml:"required,omitempty"` Required bool `yaml:"required,omitempty"`
Autoformat []string `yaml:"autoformat,omitempty"` Autoformat []string `yaml:"autoformat,omitempty"`
FormatZero bool `yaml:"formatzero,omitempty"` FormatZero bool `yaml:"formatzero,omitempty"`

View File

@@ -12,7 +12,7 @@ for dev in ${DEVS[@]}; do
# Test for our magic string (it means that the disk was made by ./boot2docker init) # Test for our magic string (it means that the disk was made by ./boot2docker init)
HEADER=`dd if=${dev} bs=1 count=${#MAGIC} 2>/dev/null` HEADER=`dd if=${dev} bs=1 count=${#MAGIC} 2>/dev/null`
if [ "$HEADER" = "$MAGIC" ]; then if [ "$HEADER" = "$MAGIC" ]; then
# save the preload userdata.tar file # save the preload userdata.tar file
dd if=${dev} of=/userdata.tar bs=1 count=8192 dd if=${dev} of=/userdata.tar bs=1 count=8192
@@ -23,10 +23,10 @@ for dev in ${DEVS[@]}; do
# do not auto-format if the disk does not begin with 1MB filled with 00 # do not auto-format if the disk does not begin with 1MB filled with 00
continue continue
fi fi
mkfs.ext4 -L RANCHER_STATE ${dev}
if [ -e "/userdata.tar" ]; then if [ -e "/userdata.tar" ]; then
mkfs.ext4 -L B2D_STATE ${dev}
mkdir -p /mnt/new-root mkdir -p /mnt/new-root
mount -t ext4 ${dev} /mnt/new-root mount -t ext4 ${dev} /mnt/new-root
pushd /mnt/new-root pushd /mnt/new-root
@@ -49,7 +49,7 @@ rancher:
ssh_authorized_keys: ssh_authorized_keys:
- ${AUTHORIZED_KEY1} - ${AUTHORIZED_KEY1}
- ${AUTHORIZED_KEY2} - ${AUTHORIZED_KEY2}
users: users:
- name: docker - name: docker
@@ -59,6 +59,8 @@ users:
EOF EOF
popd popd
umount /mnt/new-root umount /mnt/new-root
else
mkfs.ext4 -L RANCHER_STATE ${dev}
fi fi
# do not check another device # do not check another device

View File

@@ -8,11 +8,26 @@ udevd --daemon
udevadm trigger --action=add udevadm trigger --action=add
udevadm settle udevadm settle
if [ "$BOOTSTRAP" = true ]; then dev=$(ros config get rancher.state.dev)
# This was needed to get USB devices to fully register wait=$(ros config get rancher.state.wait)
# There is probably a better way to do this if [ "$BOOTSTRAP" != true ] || [ "$dev" == "" ] || [ "$wait" != "true" ]; then
killall udevd exit
udevd --daemon
udevadm trigger --action=add
udevadm settle
fi 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

View File

@@ -19,7 +19,8 @@ import (
) )
const ( const (
STATE string = "/state" STATE string = "/state"
BOOT2DOCKER_MAGIC string = "boot2docker, please format-me"
TMPFS_MAGIC int64 = 0x01021994 TMPFS_MAGIC int64 = 0x01021994
RAMFS_MAGIC int64 = 0x858458f6 RAMFS_MAGIC int64 = 0x858458f6
@@ -143,18 +144,21 @@ func tryMountState(cfg *config.CloudConfig) error {
} }
func tryMountAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, error) { func tryMountAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, error) {
if isInitrd() { if !isInitrd() || cfg.Rancher.State.Dev == "" {
if err := tryMountState(cfg); !cfg.Rancher.State.Required && err != nil { return cfg, 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 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) return mountOem(cfg)
} }
@@ -213,6 +217,7 @@ func RunInit() error {
log.Debug("Booting off a persistent filesystem") log.Debug("Booting off a persistent filesystem")
} }
boot2DockerEnvironment := false
initFuncs := []config.CfgFunc{ initFuncs := []config.CfgFunc{
func(c *config.CloudConfig) (*config.CloudConfig, error) { func(c *config.CloudConfig) (*config.CloudConfig, error) {
return c, dockerlaunch.PrepareFs(&mountConfig) return c, dockerlaunch.PrepareFs(&mountConfig)
@@ -233,8 +238,44 @@ func RunInit() error {
return cfg, nil return cfg, nil
}, },
loadModules, 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, 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 return config.LoadConfig(), nil
}, },
loadModules, loadModules,

View File

@@ -39,6 +39,7 @@ rancher:
- /dev:/host/dev - /dev:/host/dev
- /lib/modules:/lib/modules - /lib/modules:/lib/modules
- /lib/firmware:/lib/firmware - /lib/firmware:/lib/firmware
- /usr/bin/ros:/usr/bin/ros:ro
autoformat: autoformat:
autoformat: autoformat:
image: {{.OS_REPO}}/os-autoformat:{{.VERSION}}{{.SUFFIX}} image: {{.OS_REPO}}/os-autoformat:{{.VERSION}}{{.SUFFIX}}
@@ -74,7 +75,6 @@ rancher:
url: {{.OS_SERVICES_REPO}}/{{.REPO_VERSION}} url: {{.OS_SERVICES_REPO}}/{{.REPO_VERSION}}
state: state:
fstype: auto fstype: auto
dev: LABEL=RANCHER_STATE
oem_fstype: auto oem_fstype: auto
oem_dev: LABEL=RANCHER_OEM oem_dev: LABEL=RANCHER_OEM
services: services:

View File

@@ -85,7 +85,7 @@ set timeout="1"
menuentry "RancherOS-current" { menuentry "RancherOS-current" {
set root=(hd0,msdos1) 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 initrd /boot/initrd-${VERSION}-rancheros
} }
@@ -96,7 +96,7 @@ if [ ! -z ${ROLLBACK_VERSION} ]; then
cat >>${grub_cfg} <<EOF cat >>${grub_cfg} <<EOF
menuentry "RancherOS-rollback" { menuentry "RancherOS-rollback" {
set root=(hd0,msdos1) 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 initrd /boot/initrd-${ROLLBACK_VERSION}-rancheros
} }
EOF EOF
@@ -123,7 +123,7 @@ hiddenmenu
title RancherOS ${VERSION}-(current) title RancherOS ${VERSION}-(current)
root (hd0) 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 initrd /boot/initrd-${VERSION}-rancheros
EOF EOF
@@ -133,7 +133,7 @@ if [ ! -z ${ROLLBACK_VERSION} ]; then
cat >> ${grub_file}<<EOF cat >> ${grub_file}<<EOF
title RancherOS ${ROLLBACK_VERSION}-(rollback) title RancherOS ${ROLLBACK_VERSION}-(rollback)
root (hd0) 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 initrd /boot/initrd-${ROLLBACK_VERSION}-rancheros
EOF EOF
fi fi

View File

@@ -2,4 +2,4 @@ default rancheros
label rancheros label rancheros
kernel /boot/vmlinuz kernel /boot/vmlinuz
initrd /boot/initrd initrd /boot/initrd
append quiet rancher.password=rancher rancher.state.autoformat=[/dev/sda,/dev/vda] append quiet rancher.password=rancher

View File

@@ -122,7 +122,7 @@ fi
KERNEL_ARGS="quiet rancher.password=rancher console=${TTYCONS} ${QEMU_APPEND}" KERNEL_ARGS="quiet rancher.password=rancher console=${TTYCONS} ${QEMU_APPEND}"
if [ "$FORMAT" == "1" ]; then 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 fi
if [ "$RM_USR" == "1" ]; then if [ "$RM_USR" == "1" ]; then
KERNEL_ARGS="${KERNEL_ARGS} rancher.rm_usr" KERNEL_ARGS="${KERNEL_ARGS} rancher.rm_usr"