1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Ensure that loopback devices are mounted inside the conatiner

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-02-26 17:21:09 -08:00
parent 70820b69ec
commit 7cd2245947

View file

@ -37,6 +37,9 @@ func setupNewMountNamespace(rootfs, console string, readonly bool) error {
if err := copyDevNodes(rootfs); err != nil { if err := copyDevNodes(rootfs); err != nil {
return fmt.Errorf("copy dev nodes %s", err) return fmt.Errorf("copy dev nodes %s", err)
} }
if err := setupLoopbackDevices(rootfs); err != nil {
return fmt.Errorf("setup loopback devices %s", err)
}
if err := setupDev(rootfs); err != nil { if err := setupDev(rootfs); err != nil {
return err return err
} }
@ -76,21 +79,57 @@ func copyDevNodes(rootfs string) error {
"urandom", "urandom",
"tty", "tty",
} { } {
stat, err := os.Stat(filepath.Join("/dev", node)) if err := copyDevNode(rootfs, node); err != nil {
return err
}
}
return nil
}
func setupLoopbackDevices(rootfs string) error {
for i := 0; ; i++ {
var (
device = fmt.Sprintf("loop%d", i)
source = filepath.Join("/dev", device)
dest = filepath.Join(rootfs, "dev", device)
)
if _, err := os.Stat(source); err != nil {
if !os.IsNotExist(err) {
return err
}
return nil
}
if _, err := os.Stat(dest); err == nil {
os.Remove(dest)
}
f, err := os.Create(dest)
if err != nil { if err != nil {
return err return err
} }
var ( f.Close()
dest = filepath.Join(rootfs, "dev", node) if err := system.Mount(source, dest, "none", syscall.MS_BIND, ""); err != nil {
st = stat.Sys().(*syscall.Stat_t) return err
)
if err := system.Mknod(dest, st.Mode, int(st.Rdev)); err != nil && !os.IsExist(err) {
return fmt.Errorf("copy %s %s", node, err)
} }
} }
return nil return nil
} }
func copyDevNode(rootfs, node string) error {
stat, err := os.Stat(filepath.Join("/dev", node))
if err != nil {
return err
}
var (
dest = filepath.Join(rootfs, "dev", node)
st = stat.Sys().(*syscall.Stat_t)
)
if err := system.Mknod(dest, st.Mode, int(st.Rdev)); err != nil && !os.IsExist(err) {
return fmt.Errorf("copy %s %s", node, err)
}
return nil
}
// setupDev symlinks the current processes pipes into the // setupDev symlinks the current processes pipes into the
// appropriate destination on the containers rootfs // appropriate destination on the containers rootfs
func setupDev(rootfs string) error { func setupDev(rootfs string) error {