2014-04-11 15:06:56 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package nodes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-05-20 00:13:00 +00:00
|
|
|
"io/ioutil"
|
2014-04-11 15:06:56 +00:00
|
|
|
"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.
|
|
|
|
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.
|
|
|
|
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
|
|
|
|
}
|
2014-05-20 00:13:00 +00:00
|
|
|
|
2014-02-17 23:14:30 +00:00
|
|
|
func getDeviceNodes(path string) ([]string, error) {
|
2014-05-22 19:14:10 +00:00
|
|
|
out := []string{}
|
|
|
|
files, err := ioutil.ReadDir(path)
|
2014-05-20 00:13:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
2014-05-22 19:14:10 +00:00
|
|
|
if f.IsDir() && f.Name() != "pts" && f.Name() != "shm" {
|
2014-02-17 23:14:30 +00:00
|
|
|
sub, err := getDeviceNodes(filepath.Join(path, f.Name()))
|
2014-05-22 19:14:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = append(out, sub...)
|
|
|
|
} else if f.Mode()&os.ModeDevice == os.ModeDevice {
|
|
|
|
out = append(out, filepath.Join(path, f.Name()))
|
2014-05-20 00:13:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2014-05-22 19:14:10 +00:00
|
|
|
|
|
|
|
func GetHostDeviceNodes() ([]string, error) {
|
2014-02-17 23:14:30 +00:00
|
|
|
return getDeviceNodes("/dev")
|
2014-05-22 19:14:10 +00:00
|
|
|
}
|