Skip to content

Commit dcb99a8

Browse files
committed
mount /dev/console
1 parent 958a83b commit dcb99a8

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

vminitd/Sources/vmexec/Mount.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import ContainerizationOCI
1818
import ContainerizationOS
1919
import Foundation
2020
import Musl
21+
#if canImport(Glibc)
22+
import Glibc
23+
#endif
2124

2225
struct ContainerMount {
2326
private let mounts: [ContainerizationOCI.Mount]
@@ -35,7 +38,7 @@ struct ContainerMount {
3538
}
3639
}
3740

38-
func configureConsole() throws {
41+
func configureConsole(process: ContainerizationOCI.Process) throws {
3942
let ptmx = self.rootfs.standardizingPath.appendingPathComponent("/dev/ptmx")
4043

4144
guard remove(ptmx) == 0 else {
@@ -44,6 +47,29 @@ struct ContainerMount {
4447
guard symlink("pts/ptmx", ptmx) == 0 else {
4548
throw App.Errno(stage: "symlink(pts/ptmx)")
4649
}
50+
51+
if process.terminal {
52+
var buf = [CChar](repeating: 0, count: 4096)
53+
let len = readlink("/proc/self/fd/0", &buf, buf.count - 1)
54+
if len != -1 {
55+
buf[Int(len)] = 0
56+
let ptyPath = String(cString: buf)
57+
58+
let console = self.rootfs.standardizingPath.appendingPathComponent("/dev/console")
59+
60+
if access(console, F_OK) != 0 {
61+
let fd = open(console, O_RDWR | O_CREAT, mode_t(UInt16(0o600)))
62+
if fd == -1 {
63+
throw App.erno(stage: "open(/dev/console)")
64+
}
65+
close(fd)
66+
}
67+
68+
if mount(ptyPath, console, "", UInt(MS_BIND), nil) != 0 {
69+
throw App.Errno(stage: "mount(/dev/console)")
70+
}
71+
}
72+
}
4773
}
4874

4975
private func mkdirAll(_ name: String, _ perm: Int16) throws {

vminitd/Sources/vmexec/RunCommand.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ struct RunCommand: ParsableCommand {
4242
try execInNamespace(spec: ociSpec, log: log)
4343
}
4444

45-
private func childRootSetup(rootfs: ContainerizationOCI.Root, mounts: [ContainerizationOCI.Mount], log: Logger) throws {
45+
private func childRootSetup(rootfs: ContainerizationOCI.Root, mounts: [ContainerizationOCI.Mount], process: ContainerizationOCI.Process, log: Logger) throws {
4646
// setup rootfs
4747
try prepareRoot(rootfs: rootfs.path)
48-
try mountRootfs(rootfs: rootfs.path, mounts: mounts)
48+
try mountRootfs(rootfs: rootfs.path, mounts: mounts, process: process)
4949
try setDevSymlinks(rootfs: rootfs.path)
5050

5151
try pivotRoot(rootfs: rootfs.path)
@@ -93,7 +93,7 @@ struct RunCommand: ParsableCommand {
9393
throw App.Errno(stage: "setsid()")
9494
}
9595

96-
try childRootSetup(rootfs: root, mounts: spec.mounts, log: log)
96+
try childRootSetup(rootfs: root, mounts: spec.mounts, log: log, process: process)
9797

9898
if !spec.hostname.isEmpty {
9999
let errCode = spec.hostname.withCString { ptr in
@@ -138,10 +138,10 @@ struct RunCommand: ParsableCommand {
138138
}
139139
}
140140

141-
private func mountRootfs(rootfs: String, mounts: [ContainerizationOCI.Mount]) throws {
141+
private func mountRootfs(rootfs: String, mounts: [ContainerizationOCI.Mount], process: ContainerizationOCI.Process) throws {
142142
let containerMount = ContainerMount(rootfs: rootfs, mounts: mounts)
143143
try containerMount.mountToRootfs()
144-
try containerMount.configureConsole()
144+
try containerMount.configureConsole(process)
145145
}
146146

147147
private func prepareRoot(rootfs: String) throws {

0 commit comments

Comments
 (0)