How can virtual machine image formats be converted?
I only need one option for paraphrasing the following natively:
Opening statement
When you are using a remote cloud server, you typically work with a virtual machine. This virtual machine is like a separate operating system that runs within a larger server cluster. It is isolated and sandboxed to ensure security. You can also work with virtual machines locally on your desktop by using software such as QEMU or VirtualBox. Virtual machines can either have a complete desktop interface or just an SSH server for terminal connections. Despite any optimizations for cloud servers, the only major difference between different virtualization environments is the virtual machine hard disks and configuration options. These differences are known as virtual machine images and they have different formats depending on the provider.
In this guide, you will be taught the process of converting virtual machine image formats (such as qcow2, vdi, vhd, and vmdk) to seamlessly transfer your virtualized environments between your desktop and the cloud, whenever necessary.
Requirements
You can execute the commands explained in this tutorial on Windows, macOS, or Linux by installing QEMU and VirtualBox, along with their respective command line tools. The tutorial includes installation instructions for Homebrew, a package manager that can be installed on macOS, Linux, or on Windows using WSL2.
If you are using a Silicon Cloud droplet, make sure it has a minimum of 2 CPUs and 2GB of memory.
To execute the commands in this tutorial, it’s essential to have a minimum of 10GB of available storage space.
Note
Could you provide alternative phrasing for “QEMU”?
QEMU is an open source virtualization software that is highly powerful and widely supported. It can be used on various platforms such as Windows, macOS, Linux, and Android. It offers both virtualization and emulation capabilities depending on the requirements. For example, if you have a Linux server image running on a Windows desktop, QEMU can achieve optimal performance through virtualization as both platforms have a similar underlying architecture (Intel/AMD x86). However, if you need to mix and match architectures, like running x86 Linux or Windows with ARM architectures of macOS or Android, QEMU will automatically use emulation to translate between the different architectures.
Emulation is typically not employed in production settings due to the considerable performance drawback, in contrast to virtualization. Nonetheless, QEMU’s approach of considering virtualization and emulation as nearly equal presents notable compatibility benefits, enabling the utilization of identical image formats and tools for all scenarios.
QEMU is a command line tool that requires significant effort to learn, especially when compared to user-friendly desktop virtualization software such as VMware or VirtualBox. The reason is that QEMU offers a vast range of options to configure virtual machines. However, it is possible to use graphical user interfaces like QtEmu on Windows or Linux, and UTM on macOS alongside QEMU. This guide will provide instructions for working with QEMU through the command line.
Setting up QEMU
To download and set up QEMU, you can make use of Homebrew’s brew install command.
- brew install qemu
Ensure that you have access to the qemu-img command by executing which qemu-img – this will give you the complete range of QEMU tools and commands, including qemu-img that allows conversion of virtual machine images via the command line.
- which qemu-img
/usr/local/bin/qemu-img
Note
In this tutorial, QEMU offers the majority of the required features. Alternatively, you have the option to install VirtualBox, which is another commonly used virtualization software.
VirtualBox is a software designed to create and manage virtual machines.
VirtualBox is a virtualization software created by Oracle that is open source. It is compatible with all x86 operating systems such as Windows, Linux, and macOS computers made before 2020. It is not suitable for emulation or non-x86 architectures, but it is particularly useful for running Linux on Windows or vice versa. Additionally, VirtualBox is easier to use than QEMU, offers default settings that are widely compatible, and comes with a desktop interface already included.
Due to its limited command line capabilities compared to QEMU, it is commonly used alongside specialized orchestration software such as Vagrant to automate and replicate virtual machine setups. Nevertheless, it does offer the VBoxManage command-line tool that allows manipulation of certain virtual machine images.
Setting up VirtualBox
To add VirtualBox to macOS, employ the brew install command with the –cask parameter, as VirtualBox is provided as a complete application bundle, known as a cask, in Homebrew.
- brew install –cask virtualbox
To set up VirtualBox on Ubuntu or Debian-based Linux distributions (including under WSL2), start by refreshing your package sources using apt update. After that, use apt install to install VirtualBox.
- sudo apt update
- sudo apt install virtualbox
If you don’t have WSL2 and want to install VirtualBox on Windows, you can get it by visiting the download page of VirtualBox.
Once VirtualBox is installed, confirm your access to the vboxmanage command by executing the command which vboxmanage.
- which vboxmanage
/usr/local/bin/vboxmanage
This tutorial will not directly cover alternative virtualization software like Hyper-V on Windows or VMware, which are platform-specific or intended for large-scale enterprise use. However, the following steps will guide you on how to convert their image formats.
Using QEMU to generate a virtual machine image.
If you already possess an image to convert, you may skip this step. However, to illustrate the process of converting virtual machine images, you can initiate by creating an image using QEMU.
To create an image in QEMU’s default qcow2 format, utilize the qemu-img create command along with the -f qcow2 flag.
- qemu-img create -f qcow2 ubuntu-desktop-22.04.qcow2 10G
By now, you would have generated a virtual machine image that is still empty. Despite naming it ubuntu-desktop-22.04.qcow2 and specifying a capacity of 10G, the image itself does not yet have Ubuntu 22.04 installed nor does it occupy 10GB of space.
Formatting ‘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 are designed to occupy minimal disk space but still appear to have their maximum declared capacity to any software running in the virtual machine. In this scenario, the virtual machine believes it has 10GB of available capacity, which is also the maximum size the virtual machine image will expand to automatically.
You could proceed by installing Ubuntu 22.04 on the virtual machine by utilizing a Ubuntu 22.04 ISO. Nevertheless, for this tutorial, you can choose to omit this step as you won’t be directly working with this virtual machine. An ISO refers to a read-only disk image that was typically recorded onto CD and DVD installation media in the past, rather than a writable hard disk image. Other software like mkisofs and dd are available for manipulating ISO images. For further information on installing Linux using QEMU, please consult the QEMU documentation.
Converting an image in the QEMU format to a different format.
After creating a VM image, you have the option to utilize qemu-img convert to transform it into various formats. The default image format for VirtualBox is vdi. This process does not remove the original VM image by default; it solely generates a new one.
- qemu-img convert -f qcow2 ubuntu-desktop-22.04.qcow2 -O vdi ubuntu-desktop-22.04.vdi
To specify the input format and filename, you can use the -f flag. Similarly, the -O flag is used for specifying the output format and filename. If you want to view a progress bar while converting large images, you can add the -p flag. Since the image created in the previous step is empty, it will convert instantly. To verify that you successfully created ubuntu-desktop-22.04.vdi, you can use the ls command.
- ls ubuntu-desktop-22.04.vdi
ubuntu-desktop-22.04.vdi
Another popular format is vmdk, which is mainly used by VMware, and vhd, which is primarily used by Microsoft’s Hyper-V. If necessary, you can also generate a “raw” image by following the subsequent instructions.
Transforming different virtual machine formats into QEMU.
You can convert an image in vdi format by utilizing the VirtualBox-provided vboxmanage command.
- vboxmanage clonemedium ubuntu-desktop-22.04.vdi ubuntu-desktop-22.04.img –format raw
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100% Clone medium created in format ‘raw’. UUID: 0501e707-c94e-4103-9069-d7b64a933d78
Keep in mind that this process will automatically link the newly created image to your current VirtualBox settings, which can be inconvenient if you want to make temporary copies. If any duplicates are inadvertently registered, you will need to manually delete them from the VirtualBox interface.
The virtual machine images in their raw form are devoid of any compression. Consequently, running this command will generate a complete 10GB file, in contrast to the previous situation. You can revert the process in the last step by altering the syntax, thereby transforming it into qcow2 format suitable for utilization with QEMU.
- qemu-img convert -f raw ubuntu-desktop-22.04.img -O qcow2 ubuntu-desktop-22.04-new.qcow2
To free up disk space, you have the option to delete the original image.
- rm ubuntu-desktop-22.04.img
Ultimately, you have the option to utilize the qemu-img check and qemu-img info commands to generate metadata from your virtual machine images.
- qemu-img check ubuntu-desktop-22.04.qcow2
No errors were found on the image. Image end offset: 262144
- qemu-img info ubuntu-desktop-22.04.qcow2
image: 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
To gather more details about converting images using qemu-img, you can consult either the qemu-img manual page or its official documentation.
Note
In conclusion,
You have utilized the command line tools of QEMU and VirtualBox in this guide to produce, transform, and transfer virtual machine images. You have explored various frequently used formats for virtual machine images and learned about handy defaults for working with virtual machines. It is beneficial to have a grasp on the underlying technologies of popular concepts like virtualization, particularly when transitioning systems or switching cloud providers.
Afterward, it might be helpful for you to examine our manual on resizing virtual machines according to your requirements.
More Tutorials
Python 3 installing on Rocky Linux 9(Opens in a new browser tab)
QR code generator in Java using zxing.(Opens in a new browser tab)
One example of Spring REST XML and JSON(Opens in a new browser tab)
How to Set Up Git on Ubuntu 22.04(Opens in a new browser tab)
Python Compiler Error during package installation?(Opens in a new browser tab)