Skip to content

Commit fee2511

Browse files
committed
Run mknod in the right namespace
1 parent 6445a95 commit fee2511

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ concept there.
88
Here are the manual commands to test this out. You should see a Linux boot process.
99

1010
$ sudo docker run -ti fedora /bin/bash
11-
# yum install wget qemu-kvm
11+
# yum install qemu-kvm
1212
...
13-
# wget https://download.fedoraproject.org/pub/alt/atomic/stable/Fedora-Atomic-25-20170314.0/CloudImages/x86_64/images/Fedora-Atomic-25-20170314.0.x86_64.qcow2
14-
# wget https://rawgit.com/cockpit-project/cockpit/master/test/common/cloud-init.iso
15-
# mknod /dev/kvm c 10 232
16-
# chmod 666 /dev/kvm
17-
# qemu-kvm -boot c -net nic -net user -m 1024 -nographic -cdrom cloud-init.iso Fedora-Atomic-25-20170314.0.x86_64.qcow2
13+
# curl -Lo atomic.qcow2 https://ftp-stud.hs-esslingen.de/pub/Mirrors/alt.fedoraproject.org/atomic/stable/Fedora-Atomic-26-20170707.1/CloudImages/x86_64/images/Fedora-Atomic-26-20170707.1.x86_64.qcow2
14+
# curl -Lo cloud-init.iso https://rawgit.com/stefwalter/oci-kvm-hook/master/test/cloud-init.iso
15+
# qemu-kvm -boot c -net nic -net user -m 1024 -nographic -cdrom cloud-init.iso atomic.qcow2
1816

1917
Or with a prebuilt constainer:
2018

oci-kvm-hook.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,31 @@ type Process struct {
2626

2727
func allowKvm(state State) {
2828
// TODO: Use state.Pid and /proc/$pid/cgroup to determine the right devices.allow file
29-
path := fmt.Sprintf("/sys/fs/cgroup/devices/system.slice/docker-%s.scope/devices.allow", state.ID)
30-
allow, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0600)
29+
allow_path := fmt.Sprintf("/sys/fs/cgroup/devices/system.slice/docker-%s.scope/devices.allow", state.ID)
30+
allow, err := os.OpenFile(allow_path, os.O_APPEND|os.O_WRONLY, 0600)
3131
if err != nil {
32-
log.Printf("Failed to open file: %s: %v", path, err.Error())
32+
log.Printf("Failed to open file: %s: %v", allow_path, err.Error())
3333
return
3434
}
3535

3636
_, err = allow.WriteString("c 10:232 rwm")
3737
if err != nil {
38-
log.Printf("Failed to write to group file: %s: %v", path, err.Error())
38+
log.Printf("Failed to write to group file: %s: %v", allow_path, err.Error())
3939
return
4040
}
4141

42-
path = fmt.Sprintf("%s/dev/kvm", state.Root)
43-
cmd := exec.Command("/usr/bin/mknod", path, "c", "10", "232")
44-
_, err = cmd.Output()
42+
allow.Close()
43+
44+
kvm_path := fmt.Sprintf("%s/dev/kvm", state.Root)
45+
cmd := exec.Command("/usr/bin/nsenter", "--target", fmt.Sprintf("%d", state.Pid), "--mount", "--cgroup", "--",
46+
"/usr/bin/mknod", "-m", "0666", kvm_path, "c", "10", "232")
47+
output, err := cmd.CombinedOutput()
4548
if err != nil {
46-
log.Printf("Failed to run mknod: %s: %v", path, err.Error())
49+
log.Printf("Failed to run mknod: %s: %v: %s", kvm_path, err.Error(), output)
4750
return
4851
}
4952

50-
log.Printf("Allowed /dev/kvm in new container: %s %s", path, state.ID)
53+
log.Printf("Allowed /dev/kvm in new container: %s %s %s", kvm_path, allow_path, state.ID)
5154
}
5255

5356
func main() {

test/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM fedora:25
22

3-
RUN curl -o /image.qcow2 https://dl.fedoraproject.org/pub/fedora/linux/releases/25/CloudImages/x86_64/images/Fedora-Cloud-Base-25-1.3.x86_64.qcow2
3+
RUN curl -o /atomic.qcow2 https://ftp-stud.hs-esslingen.de/pub/Mirrors/alt.fedoraproject.org/atomic/stable/Fedora-Atomic-26-20170707.1/CloudImages/x86_64/images/Fedora-Atomic-26-20170707.1.x86_64.qcow2
44
RUN yum -y install qemu-kvm
55
ADD cloud-init.iso /
6-
CMD test -f /dev/kvm || mknod --mode=0666 /dev/kvm c 10 232; qemu-kvm -boot c -net nic -net user -m 1024 -nographic -cdrom /cloud-init.iso /image.qcow2
6+
CMD test -e /dev/kvm || mknod --mode=0666 /dev/kvm c 10 232; qemu-kvm -boot c -net nic -net user -m 1024 -nographic -cdrom /cloud-init.iso /atomic.qcow2

0 commit comments

Comments
 (0)