Saturday, February 27, 2016

Converting VMWare VM into KVM on Centos 6.7


1. On ESXi, power off guest.

2. Copy files from datastore to CentOS system :
root@kvm1#  scp -v root@esxi:/vmfs/volumes/< datastore name >/ < VM Name >/*  .

Note that there are additional steps if your VMWare VM has snapshots, multiple drives etc. I didn't, so YMMV. Please check the links at the bottom of this post for more info.

3. Convert vmdk to qcow2:
root@kvm1# qemu-img convert ./myvm.vmdk -O qcow2 mykvm.img

4. Download vmware2libvirt script from:

5. In my case, the vmx file had a value for memSize, while the script looked for memsize. Change accordingly and run it against the vmx file:
root@kvm1# vmware2libvirt -f myvm.vmx > mykvm.xml

6. Edit mykvm.xml if you want to change the domain name. I also made the following changes to mykvm.xml:
a. Edit the value for < emulator > from /usr/bin/kvm to /usr/libexec/qemu-kvm, otherwise the following step ("Import XML") will fail.
b. Edit the value for "< source file=" to the correct location of the img file you created earlier.
c. Add  < driver name='qemu' type='qcow2' cache='none' / > before the source file tag.
d. If you want VNC, edit the line with "vnc" with the appropriate IP address on your KVM hypervisor: eg: < graphics type='vnc' port='-1' listen='10.X.X.X' keymap='en-us'/ > where 10.X.X.X is the IP that your hypervisor listens to VNC on.
e. ESXi has networks, KVM has bridges. Please replace:
    < interface type='network' >
      < mac address='00:XX:YY:ZZ:AA:BB'/ >
      < source network='default'/ >
    < /interface >
With
    < interface type='bridge' >
      < mac address='00:XX:YY:ZZ:AA:BB'/ >
      < source bridge='br0'/ >
    < /interface >

That's if you want proper, pass-through, bridged networking. Its useful if you run services on your VM.

If you're OK with NAT where outside systems can't get to your KVM, but your KVM can get out, then you should be good with the default setup.

Blogger.com doesn't like text immediately after a "<" or ">" so when you update your XML file please adjust accordingly.

7. Import XML. This will create an XML file in /etc/libvirt/qemu/mykvm.xml
root@kvm1# virsh -c qemu:///system define mykvm.xml

8. Verify that the domain has been defined.
root@kvm1# virsh list --all
 Id    Name                           State
----------------------------------------------------
  -     mykvm                           shut off

9. Start it up.
root@kvm1# virsh start mykvm
root@kvm1# virsh list --all
 Id    Name                           State
----------------------------------------------------
  -     mykvm                           running

10. Networking can be tricky. This is a great resource.
https://jamielinux.com/docs/libvirt-networking-handbook/

Other great links:
http://www.held.org.il/blog/2012/04/how-i-converted-vmware-vm-to-kvm/
http://linsec.ca/2013/01/27/converting-a-vmware-fusion-virtual-machine-to-kvm/
https://raymii.org/s/tutorials/KVM_add_disk_image_or_swap_image_to_virtual_machine_with_virsh.html