lost and found ( for me ? )

Ubuntu 14.04 : use KVM within a container

Here are logs when using KVM within a container.

Host OS : Ubuntu 14.04
Container : Ubuntu 14.04

create a container.
# lxc-create -n ubuntu-cn1 -t ubuntu -- -r trusty

create /dev/kvm and /dev/net/tun character devices for the container so that the container can use KVM.

edit /var/lib/lxc/ubuntu-cn1/rootfs/etc/rc.local
Add three lines before ‘exit 0’
# grep -v ^# /var/lib/lxc/ubuntu-cn1/rootfs/etc/rc.local

mkdir -p /dev/net || true
mknod /dev/kvm c 10 232
mknod /dev/net/tun c 10 200
exit 0

start the container and install KVM within the container.
# lxc-start -n ubuntu-cn1


root@ubuntu-cn1:~# apt-get update
root@ubuntu-cn1:~# apt-get install qemu-kvm libvirt-bin virtinst

root@ubuntu-cn1:~# kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

build a VM.
nnn, error.
the container can not mount loop devices.
root@ubuntu-cn1:/home/ubuntu# virt-install --connect qemu:///system -n ubuntu1404-vm1 -s 10 -r 512
-f /var/lib/libvirt/images/ubuntu1404-vm1.img --vcpus=1 --os-variant ubuntutrusty --nographics --lo
cation ./ubuntu-14.04-server-amd64.iso  --extra-args='console=tty0 console=ttyS0,115200n8' --keymap
ja

Starting install...
mount: Could not find any loop device. Maybe this kernel does not know
      about the loop device? (If so, recompile or `modprobe loop'.)
umount: /var/lib/libvirt/boot/virtinstmnt.1ZhSOK: not mounted
ERROR    Error preparing install location
Traceback (most recent call last):
 File "/usr/lib/python2.7/dist-packages/virtinst/OSDistro.py", line 123, in _locationCheckWrapper
   fetcher.prepareLocation()
 File "/usr/lib/python2.7/dist-packages/virtinst/ImageFetcher.py", line 201, in prepareLocation
   (self.location))
ValueError: Mounting location '/home/ubuntu/ubuntu-14.04-server-amd64.iso' failed
ERROR    Invalid install location: Mounting location '/home/ubuntu/ubuntu-14.04-server-amd64.iso' failed
Domain installation does not appear to have been successful.
If it was, you can restart your domain by running:
 virsh --connect qemu:///system start ubuntu1404-vm1
otherwise, please restart your installation.

There are loop devices in that container.
root@ubuntu-cn1:/home/ubuntu# ls /dev/loop* -l
brw-rw---- 1 root disk 7, 0 Jan  7 16:07 /dev/loop0
brw-rw---- 1 root disk 7, 1 Jan  7 16:07 /dev/loop1
brw-rw---- 1 root disk 7, 2 Jan  7 16:07 /dev/loop2
brw-rw---- 1 root disk 7, 3 Jan  7 16:07 /dev/loop3
brw-rw---- 1 root disk 7, 4 Jan  7 16:07 /dev/loop4
brw-rw---- 1 root disk 7, 5 Jan  7 16:07 /dev/loop5
brw-rw---- 1 root disk 7, 6 Jan  7 16:07 /dev/loop6
brw-rw---- 1 root disk 7, 7 Jan  7 16:07 /dev/loop7

So the root cause on this error is .. LXC host does not allow containers to mount loop devices?

shutdown the container.
root@ubuntu-cn1:/home/ubuntu# init 0

edit /var/lib/lxc/ubuntu-cn1/config
allow the container to use /dev/loop, /dev/kvm and /dev/net/tun
root@ubuntu2-hat:~# grep -v ^# /var/lib/lxc/ubuntu-cn1/config

lxc.include = /usr/share/lxc/config/ubuntu.common.conf

lxc.mount.auto = cgroup
lxc.aa_profile = lxc-container-default-with-nesting
lxc.rootfs = /var/lib/lxc/ubuntu-cn1/rootfs
lxc.mount = /var/lib/lxc/ubuntu-cn1/fstab
lxc.utsname = ubuntu-cn1
lxc.arch = amd64

lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = lxcbr0
lxc.network.hwaddr =

lxc.cgroup.devices.allow = b 7:* rwm
lxc.cgroup.devices.allow = c 10:230 rw
lxc.cgroup.devices.allow = c 10:232 rw

start the container
# lxc-start -n ubuntu-cn1

build a VM.okay.
root@ubuntu-cn1:/home/ubuntu# virt-install --connect qemu:///system -n ubuntu1404-vm1 -s 10 -r 512 -f /var/lib/libvirt/images/ubuntu1404-vm1.img --vcpus=1 --os-variant ubuntutrusty --nographics --lo
cation ./ubuntu-14.04-server-amd64.iso  --extra-args='console=tty0 console=ttyS0,115200n8' --keymap
ja

Starting install...

The VM “ubuntu1404-vm1” is running within the container “ubuntu-cn1”.
root@ubuntu-cn1:~# virsh list --all
Id    Name                           State
----------------------------------------------------
3     ubuntu1404-vm1                 running

root@ubuntu-cn1:~#

root@ubuntu-cn1:~# virsh console 3
Connected to domain ubuntu1404-vm1
Escape character is ^]

hattori@ubuntu:~$

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.