How To Customize Qcow2/Raw Linux OS disk image with virt-customize

Posted on 98 views

This article will discuss how you can customize Qcow2 Image with virt-customize. QCOW, which stands for (QEMU Copy On Write), is one of the disk image formats supported by the QEMU processor emulator. It uses a disk storage optimization strategy that delays the allocation of storage until it is actually needed.

Qcow2 is intended to supersede the qcow image format. These image formats are often used in OpenStack Platform, KVM hypervisors, and oVirt/RHEV virtualization platforms. In this post, we will consider a few examples of customizing Qcow image formats, while retaining data integrity and small overhead on image size.

What is virt-customize?

The virt-customize command-line tool is provided by libguestfs-tools package available for installation on various Linux distributions. Virt-customize can customize a virtual machine (disk image) by installing packages, editing configuration files, and so on. It does this by modifying the guest or disk image in place. It works for both raw and qcow2 image formats.

How to Install virt-customize on Linux

Install virt-customize command line tool through libguestfs-tools.

---- CentOS / RHEL -----
$ sudo yum -y install libguestfs-tools

---- Ubuntu / Debian ----
$ sudo apt-get -y install libguestfs-tools

---- Arch / Manjaro ----
$ yaourt -S --noconfirm --needed libguestfs

Confirm installation by viewing help page.

$ virt-customize --help

If working on a running Virtual Machine image, stop it before doing in place editing. On KVM, do this via:

sudo virsh shutdown 

virt-customize usage examples

Let’s cover a few examples on how to customize Qcow2 and Raw OS image formats with virt-customize.

First, access set LIBGUESTFS_BACKEND to direct.


1. Set root password

To set root password, use the command:

# virt-customize -a rhel-server-7.6.qcow2 --root-password password:StrongRootPassword
[   0.0] Examining the guest ...
[   1.9] Setting a random seed
[   1.9] Setting passwords
[   6.8] Finishing off


  • rhel-server-7.6.qcow2 is the name of the image to be modified
  • StrongRootPassword is the password to be set for root user.

2. Register RHEL System

To register a RHEL image and subscribe to available pools, use the command:

$ virt-customize -a overcloud-full.qcow2 --run-command 'subscription-manager register --username=[username] --password=[password]'
[   0.0] Examining the guest ...
[   2.0] Setting a random seed
[   2.0] Running: subscription-manager register --username=user1 --password=mypassword
[  38.5] Finishing off

$ virt-customize -a rhel-server-7.6.qcow2 --run-command 'subscription-manager attach --pool [subscription-pool]'


  • [username] – is replaced with valid username – e.g –username=admin
  • [password] – is replaced with a valid password for the provided username

The –run-command option is used to execute any command inside a virtual image file.

3. Install Software packages inside an image

Software packages can be installed inside qcow2 or raw disk image using the command:

$ virt-customize -a rhel-server-7.6.qcow2 --install [vim,bash-completion,wget,curl,telnet,unzip]
[   0.0] Examining the guest ...
[   2.1] Setting a random seed
[   2.1] Installing packages: [vim bash-completion wget curl telnet unzip]
[ 563.2] Finishing off

$ virt-customize -a rhel-server-7.6.qcow2 --install net-tools

4. Uploading files

See examples below:

$ virt-customize -a rhel-server-7.6.qcow2 --upload rhsm.conf:/etc/rhsm/rhsm.conf
[   0.0] Examining the guest ...
[   2.9] Setting a random seed
[   3.0] Setting the machine ID in /etc/machine-id
[   3.0] Uploading: rhsm.conf to /etc/rhsm/rhsm.conf
[   3.4] Finishing off

# virt-customize -a rhel-server-7.6.qcow2 --upload yum.conf:/etc/yum.conf
[   0.0] Examining the guest ...
[   1.9] Setting a random seed
[   1.9] Uploading: yum.conf to /etc/yum.conf
[   2.2] Finishing off

# virt-customize -a rhel-server-7.6.qcow2 --upload
[   0.0] Examining the guest ...
[   1.9] Setting a random seed
[   1.9] Uploading: to /etc/profile.d/
[   2.3] Finishing off

The format is:

  • local_file_path:image_file_path

5. Set Timezone

You can also set timezone on OS image file:

# virt-customize -a rhel-server-7.6.qcow2 --timezone "Africa/Nairobi"

6. Upload SSH public key

Upload an SSH public key for a user:

$ virt-customize -a rhel-server-7.6.qcow2  --ssh-inject jmutai:file:./
[   0.0] Examining the guest ...
[   1.9] Setting a random seed
[   2.0] SSH key inject: jmutai
[   3.2] Finishing off

7. Relabel SELinux

TO relabel SELinux file contexts, use:

$ virt-customize -a rhel-server-7.6.qcow2 --selinux-relabel
[   0.0] Examining the guest ...
[   2.0] Setting a random seed
[   2.0] SELinux relabelling
[   8.6] Finishing off

For more command usage options, check:

$ man virt-customize
$ virt-customize --help

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.