How to setup KVM on Ubuntu 12.04 (IntelVT 64bit)


1. This is not strictly required, but I always do this on a fresh server to avoid problems with LC_ALL:

sudo vim /etc/environment
We then need to set the LC_ALL to something like en_US.UTF-8,mine is:

LANG="en_US.UTF-8"
LC_MESSAGES="C"
LC_ALL="en_US.UTF-8"

2. Reboot the machine
# reboot

3. In the following I will assume that you are running 64bit with Intel VT, but you can check if you really are this way:

$ grep -E 'vmx|svm' /proc/cpuinfo
Verify that it says vmx (Intel) in the output.

Check for 64 bit
$ egrep -c ' lm ' /proc/cpuinfo
$ uname -m

4. Make sure the system is up-to-date
# apt-get update
# apt-get upgrade

5. Install some stuff you want like KVM/qemu, bridging tools, virt-install helper

# apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils qemu libcap2-bin bridge-utils virtinst

HOW TO SETUP A NEW GUEST:

1. Create a directory for your guest disks
# mkdir -p /var/kvm/images/

A GUEST WITH VNC:
virt-install -n www3 -r 2048 --disk path=/var/kvm/images/www3.img,bus=virtio,cache=none,sparse=true,size=20 --vcpus=2 --os-type linux --os-variant=ubuntuprecise --noautoconsole --hvm --location='http://de.archive.ubuntu.com/ubuntu/dists/precise/main/installer-amd64/' --vnc

You can connect to VNC this way:
ssh -L 5900:localhost:5900 root@213.239.192.49

Then connect to localhost with TigerVNC

You can find the port by executing "virsh vncdisplay www3". If it returns :1 it means that the port offset is 1. The final port will be base 5900 + 1 = port 5901.

You can also define the port on the command prompt, or change it later by executing: virsh edit www3

A GUEST WITHOUT VNC:
virt-install -n www -r 2048 --disk path=/var/kvm/images/www.img,bus=virtio,cache=none,sparse=false,size=20 --vcpus=2 --os-type linux --os-variant=ubuntuprecise --nographics --noautoconsole --hvm --location='http://de.archive.ubuntu.com/ubuntu/dists/precise/main/installer-amd64/' --extra-args='console=tty0 console=ttyS0,115200n8 serial'

A GUEST WITHOUT VNC (WITH EXISTING ISO):

virt-install -n www4 -r 2048 --disk path=/var/kvm/images/www4.img,bus=virtio,cache=none,sparse=true,size=20 --vcpus=2 --os-type linux --os-variant=ubuntuprecise --nographics --noautoconsole --hvm --location /var/kvm/ubuntu-12.04.3-server-amd64.iso --extra-args='console=tty0 console=ttyS0,115200n8 serial'


Configuring DHCP

Usually your guest VMs will have the same IPs when you destroy/start them, but if you want to make sure, this is how you do it.

1. Find the mac of your guest VM
$ virsh edit www
Look for “mac address”

2. virsh net-edit default
Add as a sub-element to DHCP (on same level as range)
<host mac="00:54:21:bf:33:22" name="www" ip="192.168.122.14" />




3. Restart the virtual network
virsh net-destroy default

virsh net-start default



Setting up port forwarding

When you want to contact your guest VM, but your host VM only has a single IP, you need to set up port forwarding. You want to do this if you are hosting a webserver on a guest VM or similar.

1. Setup a QEMU hook

# wget --output-document /etc/libvirt/hooks/qemu https://gist.github.com/jjoergensen/6924800/raw/87a879d1620934baa761abc985ee90bbcc45d269/qemu > /etc/libvirt/hooks/qemu

# chmod +x /etc/libvirt/hooks/qemu 

2. Change the configuration so that it fits the machines

# vi /etc/libvirt/hooks/qemu

3. Reboot and test that forwarding works.





Comments

Popular posts from this blog

Ruby weirdness

Running LXD/LXC on WSL2 with Ubuntu 20.04

Installing pikvm on raspberry pi zero 2 w