Browse code

Setup standard /dev symlinks

After copying allowed device nodes, set up "/dev/fd", "/dev/stdin",
"/dev/stdout", and "/dev/stderr" symlinks.

Docker-DCO-1.1-Signed-off-by: Bernerd Schaefer <bj.schaefer@gmail.com> (github: bernerdschaefer)
[rebased by @crosbymichael]
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Bernerd Schaefer authored on 2014/05/12 21:41:07
Showing 1 changed files
... ...
@@ -54,6 +54,9 @@ func InitializeMountNamespace(rootfs, console string, container *libcontainer.Co
54 54
 	if err := SetupPtmx(rootfs, console, container.Context["mount_label"]); err != nil {
55 55
 		return err
56 56
 	}
57
+	if err := setupDevSymlinks(rootfs); err != nil {
58
+		return fmt.Errorf("dev symlinks %s", err)
59
+	}
57 60
 	if err := system.Chdir(rootfs); err != nil {
58 61
 		return fmt.Errorf("chdir into %s %s", rootfs, err)
59 62
 	}
... ...
@@ -114,6 +117,34 @@ func createIfNotExists(path string, isDir bool) error {
114 114
 	return nil
115 115
 }
116 116
 
117
+func setupDevSymlinks(rootfs string) error {
118
+	var links = [][2]string{
119
+		{"/proc/self/fd", "/dev/fd"},
120
+		{"/proc/self/fd/0", "/dev/stdin"},
121
+		{"/proc/self/fd/1", "/dev/stdout"},
122
+		{"/proc/self/fd/2", "/dev/stderr"},
123
+	}
124
+
125
+	// kcore support can be toggled with CONFIG_PROC_KCORE; only create a symlink
126
+	// in /dev if it exists in /proc.
127
+	if _, err := os.Stat("/proc/kcore"); err == nil {
128
+		links = append(links, [2]string{"/proc/kcore", "/dev/kcore"})
129
+	}
130
+
131
+	for _, link := range links {
132
+		var (
133
+			src = link[0]
134
+			dst = filepath.Join(rootfs, link[1])
135
+		)
136
+
137
+		if err := os.Symlink(src, dst); err != nil && !os.IsExist(err) {
138
+			return fmt.Errorf("symlink %s %s %s", src, dst, err)
139
+		}
140
+	}
141
+
142
+	return nil
143
+}
144
+
117 145
 func setupBindmounts(rootfs string, bindMounts libcontainer.Mounts) error {
118 146
 	for _, m := range bindMounts.OfType("bind") {
119 147
 		var (