Contents
When working with a remote cloud server, you are usually working with a virtual machine: a protected, isolated instance of an entire operating system running inside a larger cluster of servers. You can also work with virtual machines locally on your desktop using software such as QEMU or VirtualBox. Virtual machines can include an entire desktop interface, or they can run an SSH server to allow only terminal connections. In both cases they are technically similar and make similar assumptions. Aside from any large-scale cloud optimizations, the only significant difference between virtualization environments is the virtual machine hard disks and configuration options. These are called virtual machine images and they are formatted differently across providers.
In this tutorial, you will learn how to convert between virtual machine image formats ( qcow2, vdi, vhd, and vmdk) so that you can move your virtual environments from your desktop to the cloud and back again when needed.
Prerequisites
The commands in this tutorial can be run on Windows, macOS or Linux as long as you can install QEMU and VirtualBox along with their command-line tools. This tutorial provides installation instructions using the Homebrew package manager . You can install Homebrew on macOS , on Linux or on Windows under WSL2 .
You should have at least 2 CPUs and 2 GB of memory.
You will also need at least 10 GB of free space to run the commands in this tutorial.
Note: If you are using macOS with an Apple Silicon CPU (made in 2020 or later), you will only be able to use QEMU, not VirtualBox.
QEMU
QEMU is the most powerful and widely supported open-source virtualization software. It runs on all major platforms (including Windows, macOS, Linux and other operating systems such as Android) and, when needed, is capable of both virtualization and emulation . For example, if you are running a Linux server image on a Windows desktop (a common use case), both of these platforms share a common underlying architecture (Intel/AMD x86 ), so QEMU can achieve full performance through virtualization. If you use QEMU to mix and match architectures , for example by running ARM with x86 Linux or Windows (as the "host" or "guest" operating system) on macOS or Android architectures, QEMU automatically uses emulation to translate between the architectures.
Compared to virtualization, emulation is generally not used in production environments because it adds a significant performance penalty. However, since QEMU treats virtualization and emulation as nearly equivalent, it provides significant compatibility advantages: the same image formats and the same tools can be used in every scenario.
QEMU is command-line software, and because of the many options with which virtual machines can be configured, it has a steep learning curve compared to desktop virtualization software such as VMware or VirtualBox. It can optionally be used with GUI front ends such as QtEmu on Windows or Linux and on macOS UTM . This tutorial will provide command-line instructions for working with QEMU.
Installing QEMU
To install QEMU, use Homebrew's brew install:
brew install qemu
This will provide the full suite of QEMU tools and commands, including qemu-img, which is used to convert virtual machine images on the command line . qemu-img Verify that you have access to the command by running which qemu-img:
which qemu-img
Output/usr/local/bin/qemu-img
Note: If you prefer to work on Windows without WSL2, you can also install QEMU on Windows using its native installer .
QEMU provides most of the functionality you will need in this tutorial. You can also install VirtualBox, another popular virtualization package.
VirtualBox
VirtualBox is open-source virtualization software provided by Oracle. It supports all x86 operating systems (Windows, Linux and macOS computers made before 2020). It does not support emulation or non-x86 architectures, which makes it primarily useful for running Linux under Windows and vice versa. However, it is simpler to use than QEMU, provides widely compatible default settings, and includes a desktop interface by default.
It does not provide as much command-line functionality as QEMU, so it is often paired with dedicated orchestration software such as Vagrant to automate and recreate virtual machine configurations. However, it includes the VBoxManage command-line tool, which can be used to handle some virtual machine images.
Installing VirtualBox
To install VirtualBox on macOS, use brew install with the –cask flag, because VirtualBox is distributed as a full application package, which Homebrew calls a cask :
brew install --cask virtualbox
To install VirtualBox on Ubuntu or Debian-derived Linux distributions (including under WSL2), first use apt update to refresh your package sources, then use apt install:
sudo apt update
sudo apt install virtualbox
To install VirtualBox on Windows without WSL2, see the download page .
After installing VirtualBox, verify that you have access to the vboxmanage command by running which vboxmanage:
which vboxmanage
Output/usr/local/bin/vboxmanage
Alternatives to QEMU and VirtualBox exist for virtualization. However, most of them are platform-specific (such as Hyper-V on Windows) or designed for large-scale enterprise use (such as VMware ). They will not be covered directly in this tutorial, but in the steps below you will convert image formats between them.
Creating a Virtual Machine Image Using QEMU
To demonstrate converting virtual machine images, you can start by creating an image with QEMU. If you already have an image to convert, you can skip this section.
To create an image in QEMU's default format, use the qemu-img create command with the -f qcow2 flag, which sets the format to qcow2:
qemu-img create -f qcow2 ubuntu-desktop-22.04.qcow2 10G
At this point you will have created an empty virtual machine image. ubuntu-desktop-22.04.qcow2 Even though you named it and gave it a capacity of 10G, at this point it will neither have Ubuntu 22.04 installed nor be 10GB in size.
OutputFormatting 'ubuntu-desktop-22.04.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=10737418240 lazy_refcounts=off refcount_bits=16
QEMU images always take up as little space as possible on your disk, while reflecting their declared maximum capacity to any software running in the virtual machine. In this case, 10 GB will be the capacity your virtual machine thinks is available, and the maximum size to which the virtual machine image will automatically scale.
The typical next step would be to install Ubuntu 22.04 on the virtual machine using an Ubuntu 22.04 ISO . However, you can skip this step for this tutorial, since you will not actually be working with this virtual machine directly. An ISO is another type of disk image that is typically read-only and, instead of being an image of a writable hard disk, was historically written to CD and DVD installation media. Other software such as mkisofs and dd is also available for working with ISO images . For more information about installing Linux using QEMU, see the QEMU documentation .
Converting a QEMU Image to Another Format
After creating a VM image, you can use qemu-img convertto convert it to other formats. vdi is VirtualBox's default image format. By default, this does not delete the original VM image; it only creates a new one:
qemu-img convert -f qcow2 ubuntu-desktop-22.04.qcow2 -O vdi ubuntu-desktop-22.04.vdi
-f is used to specify the input format and the input file name. The -O flag is used to specify the output format and the output file name. You can also add the -pflag to show a progress bar, which is helpful when converting very large images. The image you created in the last step will convert instantly, since it has no content yet. ubuntu-desktop-22.04.vdi You can confirm that you created it using ls:
ls ubuntu-desktop-22.04.vdi
Outputubuntu-desktop-22.04.vdi
Other common formats include vmdk, used primarily by VMware, and vhd, used primarily by Microsoft's Hyper-V. If your software requires it, you can also create a "raw" image by following the next step.
Converting Other Virtual Machine Formats to QEMU
If you have an image in the vdi format, you can use the vboxmanage command provided by VirtualBox to convert it:
vboxmanage clonemedium ubuntu-desktop-22.04.vdi ubuntu-desktop-22.04.img --format raw
Output0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'raw'. UUID: 0501e707-c94e-4103-9069-d7b64a933d78
Note that this will automatically register the created image with your local VirtualBox configuration, which can be inconvenient if you are trying to create temporary copies. If you accidentally register copies, you may need to manually remove the new entries from the VirtualBox interface.
Raw virtual machine images are not compressed at all, so unlike qcow2, this command will actually create a full 10GB file. By reversing the syntax from the last step, you can convert it to the format for use with QEMU:
qemu-img convert -f raw ubuntu-desktop-22.04.img -O qcow2 ubuntu-desktop-22.04-new.qcow2
You can now delete the raw image to reclaim disk space:
rm ubuntu-desktop-22.04.img
Finally, you can use the qemu-img check and qemu-img info commands to output some metadata from your virtual machine images :
qemu-img check ubuntu-desktop-22.04.qcow2
OutputNo errors were found on the image.
Image end offset: 262144
qemu-img info ubuntu-desktop-22.04.qcow2
Outputimage: ubuntu-desktop-22.04.qcow2
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false
For more information about converting images with qemu-img, see the qemu-img man page or its official documentation .