2014-04-11 23:06:56 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package nodes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
2014-05-20 04:46:59 +08:00
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/devices"
|
2014-04-11 23:06:56 +08:00
|
|
|
)
|
|
|
|
|
2014-02-18 07:14:30 +08:00
|
|
|
// Create the device nodes in the container.
|
2014-05-31 09:30:27 +08:00
|
|
|
func CreateDeviceNodes(rootfs string, nodesToCreate []*devices.Device) error {
|
2014-07-15 07:55:49 +08:00
|
|
|
oldMask := syscall.Umask(0000)
|
|
|
|
defer syscall.Umask(oldMask)
|
2014-04-11 23:06:56 +08:00
|
|
|
|
2014-02-18 07:14:30 +08:00
|
|
|
for _, node := range nodesToCreate {
|
|
|
|
if err := CreateDeviceNode(rootfs, node); err != nil {
|
2014-04-11 23:06:56 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-18 07:14:30 +08:00
|
|
|
// Creates the device node in the rootfs of the container.
|
2014-05-31 09:30:27 +08:00
|
|
|
func CreateDeviceNode(rootfs string, node *devices.Device) error {
|
2014-04-11 23:06:56 +08:00
|
|
|
var (
|
2014-02-18 07:14:30 +08:00
|
|
|
dest = filepath.Join(rootfs, node.Path)
|
2014-05-23 03:14:10 +08:00
|
|
|
parent = filepath.Dir(dest)
|
2014-04-11 23:06:56 +08:00
|
|
|
)
|
2014-05-20 04:46:59 +08:00
|
|
|
|
2014-05-23 03:14:10 +08:00
|
|
|
if err := os.MkdirAll(parent, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-02-18 07:14:30 +08: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)
|
|
|
|
}
|
|
|
|
|
2014-07-15 07:55:49 +08:00
|
|
|
if err := syscall.Mknod(dest, uint32(fileMode), devices.Mkdev(node.MajorNumber, node.MinorNumber)); err != nil && !os.IsExist(err) {
|
2014-02-18 07:14:30 +08:00
|
|
|
return fmt.Errorf("mknod %s %s", node.Path, err)
|
2014-04-11 23:06:56 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|