Converting Virtual Machines To Run On Different Virtual Environments

Converting virtual machines are a necessary evil in today’s ever-evolving Information Technology (IT) modernization age. As IT infrastructures continue to advance and the costs for building and maintain them reduces, the need to ensure your virtual environments are functioning according to industry standard becomes not only feasible, but necessary. 

Table of Contents

Introduction

In this article, we will review how to migrate a virtual machine (VM) running in one virtual environment to another virtual environment. Specifically, we will migrate a VM created using a kernel-based virtual machine (KVM) hypervisor to another virtual environment running VMware.

Converting virtual machines - Servers in a Data Center

Photo by Sergei Starostin from Pexels

Pre-Installation Checks

This tutorial assumes you have root privileges or can sudo to become root to perform the tasks and procedures necessary when migrating a VM from one virtual environment to another. We will use the quick emulator (QEMU) disk imaging utility or qemu-img as its commonly-known, to complete this task.

QEMU: Image Formats

The qemu-img utility will allow us to create, convert, and modify images offline. The following image formats (below) are supported by QEMU.

FormatDescriptionTypical Use
VHDTypically used as the hard disk of a virtual machine in modern versions of Windows. Native format for Microsoft’s Hyper-V hypervisor.Microsoft Hyper-V
VMDKOriginally developed by VMware, now an open format. Commonly used in VMware Workstation or Oracle VirtualBox virtual machines.VMware Workstation, Oracle VirtualBox
QCOWUtilizes a storage optimization strategy delaying allocation until needed. Commonly used in Red Hat Enterprise Virtualization (RHEV), KVM, and projects like oVirt. Supports qcow, qcow2, and qcow3.Red Hat Enterprise Virtualization, KVM, oVirt
RAWA simple and unstructured disk image format natively supported by both KVM and Xen hypervisors.KVM, Xen
VDITypically used as the disk image for virtual machines in Oracle Virtualbox environment.Oracle VirtualBox

QEMU: Installation

The installation of the qemu-img package will differ depending on your Linux distribution.  Below we will review how to install qemu-img on rpm-based and Debian-based distributions.

Installation RHEL7/CentOS7

				
					sudo yum -y install qemu-img
				
			
Converting virtual machines - qemu-img install

Photo by admingeek from Infotechys

Installation RHEL8/CentOS8

				
					sudo dnf -y install qemu-img
				
			

Installation Ubuntu 20.04

				
					sudo apt install qemu-utils -y
				
			

You can verify qemu-img is installed on your machine by issuing the following command (below):

				
					qemu-img --version
				
			

The command output displays the current running qemu-img version along with other useful information like command syntax and parameters, as well as, supported image file formats.

qemu-img version output

Photo by admingeek from Infotechys

QEMU: Check Image Formats

Before we proceed with the format conversion process, we must first determine the format of the image we want to convert. Passing the info option to the qemu-img command (below), checks for the disk image format and outputs the results to our terminal.

				
					sudo qemu-img info dev-infotechys-1.img
				
			
image: dev-infotechys-1.img
file format: raw
virtual size: 100 GiB (107374182400 bytes)
disk size: 6.12 GiB

Moreover, we can see the qcow2 optimization process in action (also referred to as thin-provisioning). Although, the size allocated to the disk image is 100GB, it is actually using 6.12GB of storage space.

Breakdown

Image Format Conversion

In order to convert an image from one format to another, pass the convert option to the qemu-img command (see an example of the proper syntax below).

NOTE: Make sure to powered down any virtual machine(s) associated with the image you’d like to convert. Converting a disk image while it is running in your virtual environment can cause serious damage to the disk image.

				
					sudo qemu-img convert -f <current image format> -O <converted image format> <path to image file> <converted image name>.converted_image_extension
				
			

Raw Image to VMDK

We can convert a raw image to vmdk format by running the following command (below):

				
					sudo qemu-img convert -f raw -O vmdk /tmp/images/vm5-dev.infotechys.img vm5.vmdk
				
			

In this example (above), we are converting the raw image file (vm5-dev.infotechys.img) to an image format compatible with the VMware environment (vm5.vmdk). Depending on your machine’s resources and the size of the image you’re converting, this process could take several minutes to several hours to complete.

Pass the -p flag to qemu-img convert command to show progress (below):

				
					sudo qemu-img convert -f raw -O vmdk /tmp/images/vm5-dev.infotechys.img vm5.vmdk -p
				
			

VMware Conversion Tools

Upon completing the disk image conversion, you will notice the following:

  • The vm5.vmdk file is much smaller in size that the original 100GB disk image (vm5-dev.infotechys.img) due to thin-provisioning.
  • This vm5.vmdk will not run in its current form in the VMware environment.
				
					$ ls -l /tmp/images
				
			
				
					total 175550224
-rw-r--r--. 1 root root 107374182400 Feb 24 21:24 vm5-dev.infotechys.img
-rw-r--r--. 1 root root 2169503744 Feb 24 21:33 vm5.vmdk
				
			

Importing VMDK Disk Image

Thankfully, VMware has a tool called vmkfstools. Using this tool, we can import the vm5.vmdk image to the VMware environment and account for thin-provisioning. However, before importing the vmdk image, we will create a new directory on our ESXI host to store the new image.

				
					sudo mkdir /vmfs/volumes/datastore0/VM5
				
			
				
					sudo vmkfstools -i /vmfs/volumes/datastore0/vm5.vmdk -d thin /vmfs/volumes/datastore0/VM5/new-vm5.vmdk
				
			
Destination disk format: VMFS thin-provisioned
Cloning disk '/vmfs/volumes/datastore0/VM.tmp.vmdk'...
Clone: 100% done.

As you can see in line #2 (above), the vm5.vmdk image is cloned  (-i stands for --clonevirtualdisk oldName newName) to the name (new-vm5.vmdk) located under /vmfs/volumes/datastore0/VM5 (-d stands for --diskformat).

The new-vm5.vmdk should run in your VMware environment. If you’re still encountering problems running the new vmdk image, consider changing the type of hard drive (from IDE to SCSI or vice versa in VMware).

Conclusion

We’ve reviewed how to migrated a raw disk image format to vmdk and deployed it to a VMware environment. Was this article helpful to you? If so, leave us some feedback in the comment section below. We’d love to hear from you!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *