KVM - andreydiveev/wiki GitHub Wiki

Install KVM

# aptitude install qemu-kvm libvirt-bin

Qemu XML directory

# ls -la /etc/libvirt/qemu/

Fix MAC-address issue after storage migrating

# rm /etc/udev/rules.d/70-persistent-net.rules

KVM to VMware

# qemu-img convert /dev/vg0/myvm -O vmdk myvm.vmdk    

VMware to KVM

# tar -tf myvm.ova
# tar xvf myvm.ova
# file myvm*
# qemu-img -h |tail -n1
# qemu-img convert -O qcow2 myvm.vmdk myvm.qcow2

QEMU. Supported formats

# qemu-img -h | tail -n1

tcpdump

# apt-get install tcpdump

Setup bridge for VM

<interface type='bridge'>
  <mac address='52:54:00:43:01:68'/>
  <source bridge='vmbr0'/>
  <model type='virtio'/>
  <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>

Install (Debain 7)

# aptitude install qemu-kvm libvirt-bin

LVM info (Logical Volume Manager)

# lvs

Creating file system (e.g. for /home/)

# mkfs.ext4 /dev/vg0/home

Moving VM disk to another host:

On source host...
# qemu-img convert -p -O qcow2 /dev/vg0/my-vm-disk my-vm-disk.qcow2
# scp my-vm-disk.qcow2 target.example.org:/home/myuser/

On target host...
# qemu-img convert -p -O raw my-vm-disk.qcow2 /dev/vg0/my-vm-disk

VM migration to another host (MAY CORRUPT DISK DATA!!!):

1. Create the logical volume on the target. It should be the same size as the source volume.

2. Run nc -l 1234 | dd of=/dev/<vgname>/<lvname> on the target host. This tells netcat to listen on the network port and pipe everything to the logical volume.

3. Shutdown the source machine cleanly to write everything to disk.

4. Run dd if=/dev/<vgname>/<lvname> | nc <targetip> 1234 on the source host. This tells netcat to pipe everything over the network to the target host.

5. Go fetch a cup of coffee. This takes a few minutes depending on your volume size and network speed.

6. Copy the xml definitions from /etc/libvirt/qemu/ from the source to the target.

7. Edit the storage portion of the xml file and any other necessary bits such as network bridge name if they are different.

8. Reload libvirt on the target to read the xml. An /etc/init.d/libvirt-bin reload should do the trick (or systemctl restart libvirtd for distributions that use systemd)

9. Start-up the machine on the target host.

Move VM to another host (using snapshot):

server1# virsh dumpxml virtual > virtual.xml
server1# virsh save virtual virtual.state
server1# lvcreate --snapshot -L10G -n virtual-snap /dev/vg0/virtual
server1# virsh restore virtual.state
server1# dd if=/dev/vg0/virtual-snap bs=1M | gzip -c | pv -ptrb | ssh me@server2 "gunzip -c | dd of=/dev/vg0/virtual"
server1# scp virtual.xml me@server2:~/
server1# scp virtual.state me@server2:~/
server1# lvremove /dev/vg0/virtual-snap
server2# virsh define virtual.xml
server2# virsh restore virtual.state

List block devices (with filesystems):

# lsblk -f

Restart networking without reboot (DO NOW USE REMOTELY!!!):

# ifdown -a
# ifup -a

Show superblocks:

# dumpe2fs /dev/vda1 | grep superblock

Show superblock size:

# dumpe2fs /dev/vda1 | grep -i "block size"

Access to VM console from host (for Ubuntu guest!):

myguest# cp /etc/init/tty1.conf /etc/init/ttyS0.conf
myguest# sed -i "s/exec \/sbin\/getty -8 38400 tty1/exec \/sbin\/getty -8 115200 ttyS0 xterm/g" /etc/init/ttyS0.conf
host# virsh shutdown myguest
host# virsh start myguest
host# virsh console myguest
⚠️ **GitHub.com Fallback** ⚠️