1
0
mirror of https://github.com/rancher/os.git synced 2025-09-07 09:42:21 +00:00

Mount the boot partition when booting

Also update StateDir and OemDir for better clarity
This commit is contained in:
niusmallnan
2018-09-30 14:30:25 +08:00
committed by niusmallnan
parent 0b5002fa47
commit eeebf0ae0f
12 changed files with 244 additions and 226 deletions

View File

@@ -560,20 +560,20 @@ func layDownOS(image, installType, cloudConfig, device, partition, statedir, kap
kernelArgs = kernelArgs + " console=" + CONSOLE
if kappend == "" {
preservedAppend, _ := ioutil.ReadFile(filepath.Join(baseName, install.BootDir+"append"))
preservedAppend, _ := ioutil.ReadFile(filepath.Join(baseName, config.BootDir, "append"))
kappend = string(preservedAppend)
} else {
ioutil.WriteFile(filepath.Join(baseName, install.BootDir+"append"), []byte(kappend), 0644)
ioutil.WriteFile(filepath.Join(baseName, config.BootDir, "append"), []byte(kappend), 0644)
}
if installType == "amazon-ebs-pv" {
menu := install.BootVars{
BaseName: baseName,
BootDir: install.BootDir,
BootDir: config.BootDir,
Timeout: 0,
Fallback: 0, // need to be conditional on there being a 'rollback'?
Entries: []install.MenuEntry{
install.MenuEntry{"RancherOS-current", install.BootDir, VERSION, kernelArgs, kappend},
install.MenuEntry{"RancherOS-current", config.BootDir, VERSION, kernelArgs, kappend},
},
}
install.PvGrubConfig(menu)
@@ -587,7 +587,7 @@ func layDownOS(image, installType, cloudConfig, device, partition, statedir, kap
log.Debugf("installRancher done")
if kexec {
power.Kexec(false, filepath.Join(baseName, install.BootDir), kernelArgs+" "+kappend)
power.Kexec(false, filepath.Join(baseName, config.BootDir), kernelArgs+" "+kappend)
}
return nil
@@ -801,7 +801,7 @@ func setBootable(device, diskType string) error {
func upgradeBootloader(device, baseName, diskType string) error {
log.Debugf("start upgradeBootloader")
grubDir := filepath.Join(baseName, install.BootDir+"grub")
grubDir := filepath.Join(baseName, config.BootDir, "grub")
if _, err := os.Stat(grubDir); os.IsNotExist(err) {
log.Debugf("%s does not exist - no need to upgrade bootloader", grubDir)
// we've already upgraded
@@ -809,12 +809,12 @@ func upgradeBootloader(device, baseName, diskType string) error {
return nil
}
// deal with systems which were previously upgraded, then rolled back, and are now being re-upgraded
grubBackup := filepath.Join(baseName, install.BootDir+"grub_backup")
grubBackup := filepath.Join(baseName, config.BootDir, "grub_backup")
if err := os.RemoveAll(grubBackup); err != nil {
log.Errorf("RemoveAll (%s): %s", grubBackup, err)
return err
}
backupSyslinuxDir := filepath.Join(baseName, install.BootDir+"syslinux_backup")
backupSyslinuxDir := filepath.Join(baseName, config.BootDir, "syslinux_backup")
if _, err := os.Stat(backupSyslinuxDir); !os.IsNotExist(err) {
backupSyslinuxLdlinuxSys := filepath.Join(backupSyslinuxDir, "ldlinux.sys")
if _, err := os.Stat(backupSyslinuxLdlinuxSys); !os.IsNotExist(err) {
@@ -837,7 +837,7 @@ func upgradeBootloader(device, baseName, diskType string) error {
return err
}
syslinuxDir := filepath.Join(baseName, install.BootDir+"syslinux")
syslinuxDir := filepath.Join(baseName, config.BootDir, "syslinux")
// it seems that v0.5.0 didn't have a syslinux dir, while 0.7 does
if _, err := os.Stat(syslinuxDir); !os.IsNotExist(err) {
if err := os.Rename(syslinuxDir, backupSyslinuxDir); err != nil {
@@ -858,15 +858,15 @@ func upgradeBootloader(device, baseName, diskType string) error {
cfg = strings.Replace(cfg, "current", "previous", -1)
// TODO consider removing the APPEND line - as the global.cfg should have the same result
ioutil.WriteFile(filepath.Join(baseName, install.BootDir, "linux-current.cfg"), []byte(cfg), 0644)
ioutil.WriteFile(filepath.Join(baseName, config.BootDir, "linux-current.cfg"), []byte(cfg), 0644)
lines := strings.Split(cfg, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "APPEND") {
log.Errorf("write new (%s) %s", filepath.Join(baseName, install.BootDir, "global.cfg"), err)
log.Errorf("write new (%s) %s", filepath.Join(baseName, config.BootDir, "global.cfg"), err)
// TODO: need to append any extra's the user specified
ioutil.WriteFile(filepath.Join(baseName, install.BootDir, "global.cfg"), []byte(cfg), 0644)
ioutil.WriteFile(filepath.Join(baseName, config.BootDir, "global.cfg"), []byte(cfg), 0644)
break
}
}
@@ -927,7 +927,7 @@ func installSyslinux(device, baseName, diskType string) error {
}
}
sysLinuxDir := filepath.Join(baseName, install.BootDir, "syslinux")
sysLinuxDir := filepath.Join(baseName, config.BootDir, "syslinux")
if err := os.MkdirAll(sysLinuxDir, 0755); err != nil {
log.Errorf("MkdirAll(%s)): %s", sysLinuxDir, err)
//return err
@@ -985,12 +985,12 @@ func installRancher(baseName, VERSION, DIST, kappend string) (string, error) {
log.Debugf("installRancher")
// detect if there already is a linux-current.cfg, if so, move it to linux-previous.cfg,
currentCfg := filepath.Join(baseName, install.BootDir, "linux-current.cfg")
currentCfg := filepath.Join(baseName, config.BootDir, "linux-current.cfg")
if _, err := os.Stat(currentCfg); !os.IsNotExist(err) {
existingCfg := filepath.Join(DIST, "linux-current.cfg")
// only remove previous if there is a change to the current
if different(currentCfg, existingCfg) {
previousCfg := filepath.Join(baseName, install.BootDir, "linux-previous.cfg")
previousCfg := filepath.Join(baseName, config.BootDir, "linux-previous.cfg")
if _, err := os.Stat(previousCfg); !os.IsNotExist(err) {
if err := os.Remove(previousCfg); err != nil {
return currentCfg, err
@@ -1012,7 +1012,7 @@ func installRancher(baseName, VERSION, DIST, kappend string) (string, error) {
if file.Name() == "global.cfg" {
overwrite = false
}
if err := dfs.CopyFileOverwrite(filepath.Join(DIST, file.Name()), filepath.Join(baseName, install.BootDir), file.Name(), overwrite); err != nil {
if err := dfs.CopyFileOverwrite(filepath.Join(DIST, file.Name()), filepath.Join(baseName, config.BootDir), file.Name(), overwrite); err != nil {
log.Errorf("copy %s: %s", file.Name(), err)
//return err
}
@@ -1020,7 +1020,7 @@ func installRancher(baseName, VERSION, DIST, kappend string) (string, error) {
// the general INCLUDE syslinuxcfg
isolinuxFile := filepath.Join(DIST, "isolinux", "isolinux.cfg")
syslinuxDir := filepath.Join(baseName, install.BootDir, "syslinux")
syslinuxDir := filepath.Join(baseName, config.BootDir, "syslinux")
if err := dfs.CopyFileOverwrite(isolinuxFile, syslinuxDir, "syslinux.cfg", true); err != nil {
log.Errorf("copy global syslinux.cfgS%s: %s", "syslinux.cfg", err)
//return err
@@ -1030,7 +1030,7 @@ func installRancher(baseName, VERSION, DIST, kappend string) (string, error) {
}
// The global.cfg INCLUDE - useful for over-riding the APPEND line
globalFile := filepath.Join(filepath.Join(baseName, install.BootDir), "global.cfg")
globalFile := filepath.Join(baseName, config.BootDir, "global.cfg")
if _, err := os.Stat(globalFile); !os.IsNotExist(err) {
err := ioutil.WriteFile(globalFile, []byte("APPEND "+kappend), 0644)
if err != nil {

View File

@@ -11,8 +11,6 @@ import (
"github.com/rancher/os/pkg/util"
)
const BootDir = "boot/"
type MenuEntry struct {
Name, BootDir, Version, KernelArgs, Append string
}
@@ -53,7 +51,7 @@ func MountDevice(baseName, device, partition string, raw bool) (string, string,
}
if d != "" {
partition = d
baseName = filepath.Join(baseName, BootDir)
baseName = filepath.Join(baseName, config.BootDir)
} else {
if dev := util.ResolveDevice(cfg.Rancher.State.Dev); dev != "" {
// try the rancher.state.dev setting

View File

@@ -48,16 +48,17 @@ func RunInit() error {
{"set env", env.Init},
{"preparefs", prepare.FS},
{"save init cmdline", prepare.SaveCmdline},
{"mount OEM", fsmount.OEM},
{"mount OEM", fsmount.MountOem},
{"debug save cfg", debug.PrintAndLoadConfig},
{"load modules", modules.LoadModules},
{"recovery console", recovery.LoadRecoveryConsole},
{"cloud-init", cloudinit.CloudInit},
{"b2d env", b2d.B2D},
{"mount and bootstrap", fsmount.MountAndBootstrap},
{"mount STATE and bootstrap", fsmount.MountStateAndBootstrap},
{"read cfg and log files", configfiles.ReadConfigFiles},
{"switchroot", switchroot.SwitchRoot},
{"mount OEM2", fsmount.OEM},
{"mount OEM2", fsmount.MountOem},
{"mount BOOT", fsmount.MountBoot},
{"write cfg and log files", configfiles.WriteConfigFiles},
{"hypervisor Env", hypervisor.Env},
{"b2d Env", b2d.Env},

View File

@@ -178,7 +178,7 @@ func reboot(name string, force bool, code uint) {
return
}
defer util.Unmount(baseName)
Kexec(previouskexecFlag, filepath.Join(baseName, install.BootDir), kexecAppendFlag)
Kexec(previouskexecFlag, filepath.Join(baseName, config.BootDir), kexecAppendFlag)
return
}

View File

@@ -172,7 +172,9 @@ var schema = `{
"rngd": {"type": "boolean"},
"script": {"type": "string"},
"oem_fstype": {"type": "string"},
"oem_dev": {"type": "string"}
"oem_dev": {"type": "string"},
"boot_fstype": {"type": "string"},
"boot_dev": {"type": "string"}
}
},

View File

@@ -12,7 +12,9 @@ import (
)
const (
OEM = "/usr/share/ros/oem"
OemDir = "/usr/share/ros/oem"
BootDir = "/boot"
StateDir = "/state"
RosBin = "/usr/bin/ros"
SysInitBin = "/usr/bin/ros-sysinit"
SystemDockerHost = "unix:///var/run/system-docker.sock"
@@ -47,11 +49,10 @@ const (
EtcResolvConfFile = "/etc/resolv.conf"
MultiDockerConfFile = "/var/lib/rancher/conf.d/m-user-docker.yml"
MultiDockerDataDir = "/var/lib/m-user-docker"
State = "/state"
)
var (
OemConfigFile = OEM + "/oem-config.yml"
OemConfigFile = OemDir + "/oem-config.yml"
Version string
BuildDate string
Arch string
@@ -205,6 +206,8 @@ type StateConfig struct {
Script string `yaml:"script,omitempty"`
OemFsType string `yaml:"oem_fstype,omitempty"`
OemDev string `yaml:"oem_dev,omitempty"`
BootFsType string `yaml:"boot_fstype,omitempty"`
BootDev string `yaml:"boot_dev,omitempty"`
}
type CloudInit struct {

View File

@@ -82,6 +82,8 @@ rancher:
fstype: auto
oem_fstype: auto
oem_dev: LABEL=RANCHER_OEM
boot_fstype: auto
boot_dev: LABEL=RANCHER_BOOT
rngd: true
sysctl:
fs.file-max: 1000000000

View File

@@ -10,7 +10,7 @@ import (
)
func CloudInit(cfg *config.CloudConfig) (*config.CloudConfig, error) {
cfg.Rancher.CloudInit.Datasources = config.LoadConfigWithPrefix(config.State).Rancher.CloudInit.Datasources
cfg.Rancher.CloudInit.Datasources = config.LoadConfigWithPrefix(config.StateDir).Rancher.CloudInit.Datasources
hypervisor := util.GetHypervisor()
if hypervisor == "" {
log.Infof("ros init: No Detected Hypervisor")

View File

@@ -86,7 +86,7 @@ func WriteConfigFiles(cfg *config.CloudConfig) (*config.CloudConfig, error) {
log.Error(err)
}
log.FsReady()
log.Debugf("WARNING: switchroot and mount OEM2 phases not written to log file")
log.Debugf("WARNING: switchroot and mount OEM2 and BOOT phases not written to log file")
return cfg, nil
}

View File

@@ -19,15 +19,27 @@ var (
ShouldSwitchRoot bool
)
func OEM(cfg *config.CloudConfig) (*config.CloudConfig, error) {
if cfg == nil {
cfg = config.LoadConfig()
}
if err := mountConfigured("oem", cfg.Rancher.State.OemDev, cfg.Rancher.State.OemFsType, config.OEM); err != nil {
func MountOem(cfg *config.CloudConfig) (*config.CloudConfig, error) {
if err := mountConfigured("oem", cfg.Rancher.State.OemDev, cfg.Rancher.State.OemFsType, config.OemDir); err != nil {
log.Debugf("Not mounting OEM: %v", err)
} else {
log.Infof("Mounted OEM: %s", cfg.Rancher.State.OemDev)
}
return cfg, nil
}
func MountBoot(cfg *config.CloudConfig) (*config.CloudConfig, error) {
if IsInitrd() {
return cfg, nil
}
if err := mountConfigured("boot", cfg.Rancher.State.BootDev, cfg.Rancher.State.BootFsType, config.BootDir); err != nil {
log.Debugf("Not mounting BOOT: %v", err)
} else {
log.Infof("Mounted BOOT: %s", cfg.Rancher.State.BootDev)
}
return cfg, nil
}
@@ -56,7 +68,7 @@ func mountConfigured(display, dev, fsType, target string) error {
}
func mountState(cfg *config.CloudConfig) error {
return mountConfigured("state", cfg.Rancher.State.Dev, cfg.Rancher.State.FsType, config.State)
return mountConfigured("state", cfg.Rancher.State.Dev, cfg.Rancher.State.FsType, config.StateDir)
}
func tryMountState(cfg *config.CloudConfig) error {
@@ -74,7 +86,7 @@ func tryMountState(cfg *config.CloudConfig) error {
return mountState(cfg)
}
func tryMountAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, bool, error) {
func tryMountStateAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, bool, error) {
if !IsInitrd() || cfg.Rancher.State.Dev == "" {
return cfg, false, nil
}
@@ -94,9 +106,9 @@ func IsInitrd() bool {
return int64(stat.Type) == tmpfsMagic || int64(stat.Type) == ramfsMagic
}
func MountAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, error) {
func MountStateAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, error) {
var err error
cfg, ShouldSwitchRoot, err = tryMountAndBootstrap(cfg)
cfg, ShouldSwitchRoot, err = tryMountStateAndBootstrap(cfg)
if err != nil {
return nil, err

View File

@@ -19,8 +19,8 @@ func SwitchRoot(cfg *config.CloudConfig) (*config.CloudConfig, error) {
if !fsmount.ShouldSwitchRoot {
return cfg, nil
}
log.Debugf("Switching to new root at %s %s", config.State, cfg.Rancher.State.Directory)
if err := switchRoot(config.State, cfg.Rancher.State.Directory, cfg.Rancher.RmUsr); err != nil {
log.Debugf("Switching to new root at %s %s", config.StateDir, cfg.Rancher.State.Directory)
if err := switchRoot(config.StateDir, cfg.Rancher.State.Directory, cfg.Rancher.RmUsr); err != nil {
return cfg, err
}
return cfg, nil
@@ -112,7 +112,7 @@ func copyMoveRoot(rootfs string, rmUsr bool) error {
}
func switchRoot(rootfs, subdir string, rmUsr bool) error {
if err := syscall.Unmount(config.OEM, 0); err != nil {
if err := syscall.Unmount(config.OemDir, 0); err != nil {
log.Debugf("Not umounting OEM: %v", err)
}

View File

@@ -22,7 +22,7 @@ const (
)
func hasImage(name string) bool {
stamp := path.Join(config.State, name)
stamp := path.Join(config.StateDir, name)
if _, err := os.Stat(stamp); os.IsNotExist(err) {
return false
}