1
0
mirror of https://github.com/rancher/os.git synced 2025-05-23 23:33:14 +00:00
os/init/root.go

162 lines
3.8 KiB
Go
Raw Normal View History

2015-07-29 07:51:49 +00:00
package init
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
2015-07-29 07:51:49 +00:00
"syscall"
"github.com/docker/docker/pkg/archive"
"github.com/rancher/os/config"
"github.com/rancher/os/dfs"
"github.com/rancher/os/log"
2015-07-29 07:51:49 +00:00
)
2015-08-29 04:13:01 +00:00
func cleanupTarget(rootfs, targetUsr, usr, usrVer, tmpDir string) (bool, error) {
log.Debugf("Deleting %s", targetUsr)
if err := os.Remove(targetUsr); err != nil && !os.IsNotExist(err) {
log.Errorf("Failed to delete %s, possibly invalid RancherOS state partition: %v", targetUsr, err)
return false, err
}
if err := dfs.CreateSymlink(usrVer, path.Join(rootfs, "usr")); err != nil {
2015-08-29 04:13:01 +00:00
return false, err
}
log.Debugf("Deleting %s", tmpDir)
if err := os.RemoveAll(tmpDir); err != nil {
// Don't care if this fails
log.Errorf("Failed to cleanup temp directory %s: %v", tmpDir, err)
2015-07-29 07:51:49 +00:00
}
if _, err := os.Stat(usr); err == nil {
2015-08-29 04:13:01 +00:00
return false, nil
}
return true, nil
2015-07-29 07:51:49 +00:00
}
func copyMoveRoot(rootfs string, rmUsr bool) error {
2016-11-28 08:06:00 +00:00
usrVer := fmt.Sprintf("usr-%s", config.Version)
2015-07-29 07:51:49 +00:00
usr := path.Join(rootfs, usrVer)
2015-08-29 04:13:01 +00:00
targetUsr := path.Join(rootfs, "usr")
tmpDir := path.Join(rootfs, "tmp")
2015-07-29 07:51:49 +00:00
if rmUsr {
log.Warnf("Development setup. Removing old usr: %s", usr)
if err := os.RemoveAll(usr); err != nil {
// Don't care if this fails
log.Errorf("Failed to remove %s: %v", usr, err)
}
}
2015-08-29 04:13:01 +00:00
if cont, err := cleanupTarget(rootfs, targetUsr, usr, usrVer, tmpDir); !cont {
2015-07-29 07:51:49 +00:00
return err
}
2015-08-29 04:13:01 +00:00
log.Debugf("Creating temp dir directory %s", tmpDir)
if err := os.MkdirAll(tmpDir, 0755); err != nil {
return err
}
usrVerTmp, err := ioutil.TempDir(tmpDir, usrVer)
if err != nil {
return err
}
log.Debugf("Copying to temp dir %s", usrVerTmp)
if err := archive.CopyWithTar("/usr", usrVerTmp); err != nil {
return err
}
log.Debugf("Renaming %s => %s", usrVerTmp, usr)
if err := os.Rename(usrVerTmp, usr); err != nil {
2015-07-29 07:51:49 +00:00
return err
}
files, err := ioutil.ReadDir("/")
if err != nil {
return err
}
for _, file := range files {
filename := path.Join("/", file.Name())
if filename == rootfs || strings.HasPrefix(rootfs, filename+"/") {
2015-08-04 21:45:38 +00:00
log.Debugf("Skipping Deleting %s", filename)
2015-07-29 07:51:49 +00:00
continue
}
log.Debugf("Deleting %s", filename)
if err := os.RemoveAll(filename); err != nil {
return err
}
}
return nil
}
func switchRoot(rootfs, subdir string, rmUsr bool) error {
if err := syscall.Unmount(config.OEM, 0); err != nil {
log.Debugf("Not umounting OEM: %v", err)
}
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
}
}
2015-07-29 07:51:49 +00:00
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 {
return err
}
if err := syscall.Mount(i, path.Join(rootfs, i), "", syscall.MS_MOVE, ""); err != nil {
return err
}
}
if err := copyMoveRoot(rootfs, rmUsr); err != nil {
2015-07-29 07:51:49 +00:00
return err
}
log.Debugf("chdir %s", rootfs)
2015-07-29 07:51:49 +00:00
if err := syscall.Chdir(rootfs); err != nil {
return err
}
log.Debugf("mount MS_MOVE %s", rootfs)
2015-07-29 07:51:49 +00:00
if err := syscall.Mount(rootfs, "/", "", syscall.MS_MOVE, ""); err != nil {
return err
}
log.Debug("chroot .")
2015-07-29 07:51:49 +00:00
if err := syscall.Chroot("."); err != nil {
return err
}
log.Debug("chdir /")
2015-07-29 07:51:49 +00:00
if err := syscall.Chdir("/"); err != nil {
return err
}
log.Debugf("Successfully moved to new root at %s", path.Join(rootfs, subdir))
2015-08-04 21:45:38 +00:00
os.Unsetenv("DOCKER_RAMDISK")
2015-07-29 07:51:49 +00:00
return nil
}