mirror of
https://github.com/rancher/os.git
synced 2025-09-16 23:21:19 +00:00
Use a subdirectory of the state partition
This commit is contained in:
22
init/init.go
22
init/init.go
@@ -83,18 +83,17 @@ func MainInit() {
|
||||
}
|
||||
}
|
||||
|
||||
func mountState(cfg *config.CloudConfig) error {
|
||||
func mountConfigured(display, dev, fsType, target string) error {
|
||||
var err error
|
||||
|
||||
if cfg.Rancher.State.Dev == "" {
|
||||
if dev == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
dev := util.ResolveDevice(cfg.Rancher.State.Dev)
|
||||
dev = util.ResolveDevice(dev)
|
||||
if dev == "" {
|
||||
return fmt.Errorf("Could not resolve device %q", cfg.Rancher.State.Dev)
|
||||
return fmt.Errorf("Could not resolve device %q", dev)
|
||||
}
|
||||
fsType := cfg.Rancher.State.FsType
|
||||
if fsType == "auto" {
|
||||
fsType, err = util.GetFsType(dev)
|
||||
}
|
||||
@@ -104,8 +103,12 @@ func mountState(cfg *config.CloudConfig) error {
|
||||
}
|
||||
|
||||
log.Debugf("FsType has been set to %s", fsType)
|
||||
log.Infof("Mounting state device %s to %s", dev, STATE)
|
||||
return util.Mount(dev, STATE, fsType, "")
|
||||
log.Infof("Mounting %s device %s to %s", display, dev, target)
|
||||
return util.Mount(dev, target, fsType, "")
|
||||
}
|
||||
|
||||
func mountState(cfg *config.CloudConfig) error {
|
||||
return mountConfigured("state", cfg.Rancher.State.Dev, cfg.Rancher.State.FsType, STATE)
|
||||
}
|
||||
|
||||
func tryMountState(cfg *config.CloudConfig) error {
|
||||
@@ -128,8 +131,9 @@ func tryMountAndBootstrap(cfg *config.CloudConfig) (*config.CloudConfig, error)
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
log.Debugf("Switching to new root at %s", STATE)
|
||||
return cfg, switchRoot(STATE, cfg.Rancher.RmUsr)
|
||||
log.Debugf("Switching to new root at %s %s", STATE, cfg.Rancher.State.Directory)
|
||||
err := switchRoot(STATE, cfg.Rancher.State.Directory, cfg.Rancher.RmUsr)
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
func getLaunchConfig(cfg *config.CloudConfig, dockerCfg *config.DockerConfig) (*dockerlaunch.Config, []string) {
|
||||
|
27
init/root.go
27
init/root.go
@@ -5,6 +5,7 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
@@ -84,7 +85,7 @@ func copyMoveRoot(rootfs string, rmUsr bool) error {
|
||||
for _, file := range files {
|
||||
filename := path.Join("/", file.Name())
|
||||
|
||||
if filename == rootfs {
|
||||
if filename == rootfs || strings.HasPrefix(rootfs, filename+"/") {
|
||||
log.Debugf("Skipping Deleting %s", filename)
|
||||
continue
|
||||
}
|
||||
@@ -98,7 +99,23 @@ func copyMoveRoot(rootfs string, rmUsr bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func switchRoot(rootfs string, rmUsr bool) error {
|
||||
func switchRoot(rootfs, subdir string, rmUsr bool) error {
|
||||
if subdir != "" {
|
||||
fullRootfs := path.Join(rootfs, subdir)
|
||||
if _, err := os.Stat(fullRootfs); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(fullRootfs, 0755); err != nil {
|
||||
log.Errorf("Failed to create directory %s: %v", fullRootfs, err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("Bind mounting mount %s to %s", fullRootfs, rootfs)
|
||||
if err := syscall.Mount(fullRootfs, rootfs, "", syscall.MS_BIND, ""); err != nil {
|
||||
log.Errorf("Failed to bind mount subdir for %s: %v", fullRootfs, err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, i := range []string{"/dev", "/sys", "/proc", "/run"} {
|
||||
log.Debugf("Moving mount %s to %s", i, path.Join(rootfs, i))
|
||||
if err := os.MkdirAll(path.Join(rootfs, i), 0755); err != nil {
|
||||
@@ -113,23 +130,27 @@ func switchRoot(rootfs string, rmUsr bool) error {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debugf("chdir %s", rootfs)
|
||||
if err := syscall.Chdir(rootfs); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debugf("mount MS_MOVE %s", rootfs)
|
||||
if err := syscall.Mount(rootfs, "/", "", syscall.MS_MOVE, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debug("chroot .")
|
||||
if err := syscall.Chroot("."); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debug("chdir /")
|
||||
if err := syscall.Chdir("/"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debugf("Successfully moved to new root at %s", rootfs)
|
||||
log.Debugf("Successfully moved to new root at %s", path.Join(rootfs, subdir))
|
||||
os.Unsetenv("DOCKER_RAMDISK")
|
||||
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user