Merge pull request #480 from mrunalp/device_mount

mount: Add a flag to bind devices when user namespaces are enabled.
This commit is contained in:
Michael Crosby 2015-03-24 17:43:55 -07:00
commit 8530167f7f
1 changed files with 12 additions and 11 deletions

View File

@ -186,7 +186,9 @@ func reOpenDevNull(rootfs string) error {
func createDevices(config *configs.Config) error { func createDevices(config *configs.Config) error {
oldMask := syscall.Umask(0000) oldMask := syscall.Umask(0000)
for _, node := range config.Devices { for _, node := range config.Devices {
if err := createDeviceNode(config.Rootfs, node); err != nil { // containers running in a user namespace are not allowed to mknod
// devices so we can just bind mount it from the host.
if err := createDeviceNode(config.Rootfs, node, config.Namespaces.Contains(configs.NEWUSER)); err != nil {
syscall.Umask(oldMask) syscall.Umask(oldMask)
return err return err
} }
@ -196,20 +198,13 @@ func createDevices(config *configs.Config) error {
} }
// Creates the device node in the rootfs of the container. // Creates the device node in the rootfs of the container.
func createDeviceNode(rootfs string, node *configs.Device) error { func createDeviceNode(rootfs string, node *configs.Device, bind bool) error {
dest := filepath.Join(rootfs, node.Path) dest := filepath.Join(rootfs, node.Path)
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil { if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
return err return err
} }
if err := mknodDevice(dest, node); err != nil {
if os.IsExist(err) { if bind {
return nil
}
if err != syscall.EPERM {
return err
}
// containers running in a user namespace are not allowed to mknod
// devices so we can just bind mount it from the host.
f, err := os.Create(dest) f, err := os.Create(dest)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
return err return err
@ -219,6 +214,12 @@ func createDeviceNode(rootfs string, node *configs.Device) error {
} }
return syscall.Mount(node.Path, dest, "bind", syscall.MS_BIND, "") return syscall.Mount(node.Path, dest, "bind", syscall.MS_BIND, "")
} }
if err := mknodDevice(dest, node); err != nil {
if os.IsExist(err) {
return nil
}
return err
}
return nil return nil
} }