Installation

Unmask to get the latest versions.

root@host # emerge -pv kvm

Creating KVM Disk Images

To create Virtual Machine Images (VMI) using standard file formats use the kvm-img command. The raw format is the default and qcow2 is very versitile supporting compression (-c) and encryption (-). Here are examples of each.

qemu-img create -f raw /mnt/image-dir/sample-image.raw 8G
qemu-img create -c -e -f qcow2 /mnt/image-dir/sample-image.qcow2 8G

Then mkfs on the image to create a usable file system.

mke2fs -j -L'Sample Image' /mnt/image-dir/sample-image.raw

LVM Based Images

If the LVM was partitioned by the Installer (Ubuntu)

# losetup /dev/loop0 foo.img
# kpartx -av /dev/loop0
# mount /dev/mapper/loop0p1 /mnt
...
# unmount /mnt
# kpartx -dv /dev/loop0
# losetup -d /dev/loop0

Mounting qcow2 Images

The qcow2 image format is not directly mountable, to access the file system inside the Network Block Device (nbd). Make sure modules are loaded, connect with qemu-nbd, access as regular disk.

root@host # modprobe nbd max_part=16
root@host # qmeu-nbd --connect=/dev/nbd0 /path/to/disk.qcow2
root@host # sfdisk -l /dev/nbd0
root@host # mount /dev/nbd0p1 /mnt/vm

Installing Windows 7

This example shows how to install & then boot Windows 7. The example used here was from the Windows 7 Professional 32bit media; increase ram and possibily storage if using 64bit version.

Here we create the image file, bridge the tunnel adapter and boot the system from the installer ISO image file, adjust nic settings as necessary.

root@host # qemu-img create -f qcow2 /opt/kvm/Win_7_Pro.qcow2 64G

root@host # ifconfig tap12 >/dev/null 2>&1 ] || tunctl -t tap12 >/dev/null
root@host # brctl show|grep -q "tap12$" || brctl addif br0 tap12

root@host # /usr/bin/kvm \
  -m 2048 \
  -name Win_7_Pro \
  -boot d \
  -drive file=/opt/kvm/Win_7_Pro.qcow2,if=ide,index=0 \
  -cdrom /opt/kvm/en_windows_7_professional_x86_dvd_x15-65804.iso \
  -net nic,vlan=0,macaddr=00:ed:0c:e0:00:12,model=e1000 \
  -net tap,vlan=0,ifname=tap12,script=no,downscript=no \
  -vga std \
  -usb \
  -usbdevice tablet \
  -vnc 0.0.0.0:12 \
  -monitor telnet:localhost:2312,server,nowait,nodelay \
  -daemonize

Once the installation is complete power-off the image and boot with the following:

root@host # /usr/bin/kvm \
  -m 2048 \
  -name Win_7_Pro \
  -boot c \
  -drive file=/opt/kvm/Win_7_Pro.qcow2,if=ide,index=0 \
  -net nic,vlan=0,macaddr=00:ed:0c:e0:00:12,model=e1000 \
  -net tap,vlan=0,ifname=tap12,script=no,downscript=no \
  -vga std \
  -usb \
  -usbdevice tablet \
  -vnc 0.0.0.0:12 \
  -monitor telnet:localhost:2312,server,nowait,nodelay \
  -daemonize

See Also