Skip to main content

VM image conversion from KVM to VMware

646 words·
KVM VMware

VM Image Migration
#

To migrate a KVM image to ESXi, the KVM image needs to be converted two times. The first conversion is done with “qemu-img” on a Linux server, the seconed conversion is done with “vmkfstools” directly on the ESXi shell.

The qemu-img command converts an qcow2 or raw image to vmdk. This vmdk image is supposed to be used on VMware worksation - I have not tried if it works - and needs to be converted again with vmkfstools, so that it can be used with VMware ESXi.

To access the files from the different servers I used an NFS share that was mounted on the Linux server and the ESXi node.

Part 1: gemu-img
#

Install qemu-img
#

  • SSH into a Linux server
# Update package index
sudo apt update

# Install packages for qemu-img
sudo apt install qemu qemu-kvm -y

Note: I used a seperate Ubuntu VM for the migration and not the KVM hypervisor itself.

Convert Image
#

# Convert image: qcow2 to VMDK (Workstation Version)
sudo qemu-img convert -p -f qcow2 /path/to/source/image.qcow2 -O vmdk /path/to/destination/image.vmdk

# Convert image: raw to VMDK (Workstation Version)
sudo qemu-img convert -p -f raw /mnt/tmp-vm-migration/myimage.raw -O vmdk /mnt/tmp-vm-migration/myimage.vmdk

# Convert image: LV (raw) to VMDK (Workstation Version)
sudo qemu-img convert -p -f raw /dev/pathto/logical-volume -O vmdk /mnt/tmp-vm-migration/myimage.vmdk

If the image will be used on VMware Workstation, use the following command instead to create a thin provisioned image:

# Convert image: qcow2 to VMDK: (Workstation Thin Provisioning Version)
sudo qemu-img convert -p -f qcow2 -O vmdk -o subformat=streamOptimized \
 /mnt/tmp-vm-migration/apt-dater.qcow2 \
  /mnt/tmp-vm-migration/apt-dater_thin.vmdk

Part 2: vmkfstools
#

  • SSH into an ESXi Shell

Storage
#

Local storage pools and NFS shares can be found in the following directory:

# Path to storage pools & NFS shares
ls /vmfs/volumes

Convert Image
#

# Convert VMDK from Workstation to ESXi
vmkfstools -i /path/to/source/image.vmdk -d thin /path/to/destination/image_final.vmdk

For long running convertions the following option can be used to run the command in the background, that makes it possible to close the SSH connection:

# Convert VMDK from Workstation to ESXi
nohup vmkfstools -i /path/to/source/image.vmdk -d thin /path/to/destination/image_final.vmdk &
# Check if process is still running
ps | grep vmkfstools

Optional: Calculate Checksum
#

To be absolutely sure the image conversion worked, a checksum can be calculated. For this I booted from a Kali Linux live system and calculated a sha256 checksum of both the old and the new image.

Download Kali Linux
#

# Kali Linux
https://www.kali.org/get-kali/#kali-live

# Download Kali Linux "Live Boot"
wget https://cdimage.kali.org/kali-2023.4/kali-linux-2023.4-live-amd64.iso

vSphere Boot Option
#

  • RightClick VM > Edit Settings...

  • Go to VM Options > Boot Options

  • Check During the next boot, force entry into the BIOS setup screen

  • Start VM

  • Boot into Live system (amd64 forensic mode)

  • Open Kali terminal

# Switch to root
sudo su

# List Blockdevices
lsblk

# Calculate Checksum
sudo sha256sum /dev/sda

Appendix
#

This are just the basic steps for the image conversion from KVM to VMware.

On some old Linux VMs it was afterwards necessary to boot into a live system and correct parts of the configuration, like disk names and create a new Initial Ramdisk.

Here are examples of changes that were necessary on OpenSUSE:

# Boot from Live System
# Login: 
root

# Mount disk
mount /dev/sda1 /mnt

# Change root
chroot /mnt

# Open fstab: Correct disk names
vi /etc/fstab

# Mount directories
mount /dev
mount /sys
mount /proc
mount /boot

# Open Bootloader config: Change disk names, uncomment DEFAULT_VGA and FAILSAVE_VGA
vi /etc/sysconfig/bootloader


# Open Yast: Go to "System" > "Bootloader" and edit the boot configurations: Correct disk names
yast2

# Exit Yast

# Create new Initial Ramdisk file
mkinitrd
# Unmount
umount /boot
umount /dev
umount /proc
umount /sys

# Switch back to original root
exit

# Unmount directories
umount /mnt

# Restart and boot into orginal system
# Install Open-VMware-Tools
zypper install open-vm-tools