2014-04-11 15:06:56 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package nodes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
2014-05-19 20:46:59 +00:00
|
|
|
|
2014-02-17 23:14:30 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/devices"
|
2014-05-19 20:46:59 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-04-11 15:06:56 +00:00
|
|
|
)
|
|
|
|
|
2014-02-17 23:14:30 +00:00
|
|
|
// Create the device nodes in the container.
|
2014-05-31 01:30:27 +00:00
|
|
|
func CreateDeviceNodes(rootfs string, nodesToCreate []*devices.Device) error {
|
2014-04-11 15:06:56 +00:00
|
|
|
oldMask := system.Umask(0000)
|
|
|
|
defer system.Umask(oldMask)
|
|
|
|
|
2014-02-17 23:14:30 +00:00
|
|
|
for _, node := range nodesToCreate {
|
|
|
|
if err := CreateDeviceNode(rootfs, node); err != nil {
|
2014-04-11 15:06:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-17 23:14:30 +00:00
|
|
|
// Creates the device node in the rootfs of the container.
|
2014-05-31 01:30:27 +00:00
|
|
|
func CreateDeviceNode(rootfs string, node *devices.Device) error {
|
2014-04-11 15:06:56 +00:00
|
|
|
var (
|
2014-02-17 23:14:30 +00:00
|
|
|
dest = filepath.Join(rootfs, node.Path)
|
2014-05-22 19:14:10 +00:00
|
|
|
parent = filepath.Dir(dest)
|
2014-04-11 15:06:56 +00:00
|
|
|
)
|
2014-05-19 20:46:59 +00:00
|
|
|
|
2014-05-22 19:14:10 +00:00
|
|
|
if err := os.MkdirAll(parent, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-02-17 23:14:30 +00:00
|
|
|
fileMode := node.FileMode
|
|
|
|
switch node.Type {
|
|
|
|
case 'c':
|
|
|
|
fileMode |= syscall.S_IFCHR
|
|
|
|
case 'b':
|
|
|
|
fileMode |= syscall.S_IFBLK
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%c is not a valid device type for device %s", node.Type, node.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := system.Mknod(dest, uint32(fileMode), devices.Mkdev(node.MajorNumber, node.MinorNumber)); err != nil && !os.IsExist(err) {
|
|
|
|
return fmt.Errorf("mknod %s %s", node.Path, err)
|
2014-04-11 15:06:56 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|