I am gonna show you today a practical and useful tool for managing Linux virtual machines and disks. With virtualization, people start to use a lot of VMS in their personal or corporate PC’s. But managing them is difficult. I faced these difficulties too. So I search for a tool helps me especially in updates. I came with virt-customize which is a tool in libguestfs-tools package. Here we start.
Install virt-customize
In order to use the virt-customize command, we will install libguestfs-tools package.
Install For Ubuntu, Debian, Mint, Kali
$ sudo apt install libguestfs-tools

Install For Fedora, CentOS, Red Hat
$ sudo yum install libguestfs-tools-c
OR
$ sudo dnf install libguestfs-tools-c
Load Image
Update existing disk image Virt-customize opens the disk and then look for last update seed and then try to run update commands which are used this distro. Windows image are not supported yet but you can use the run command to update windows images. Here you can you URI for remote disk images. The disk format is automatically detected. While loading image we may get some errors which prevent to load the image. This is generally permission related error which can be solved with the sudo
command.
$ sudo virt-customize -a opensuse12.qcow2

Mount ISO to Image
We can also run the interactive shell by attaching an ISO image into the VM image. We will use --attach
command by providing the ISO file.
$ sudo virt-customize --attach fedora30.iso -a opensuse12.qcow2
Connect Libvirt or Hypervisor
To connect and hypervisor and run virt-customize there use -c option with hypervisor URI which is here local libvirtd and give domain name or UUID with -d option. In this example, we will connect the local hypervisor and open the VM named Debian disk image.
$ virt-customize -c qemu:///system -d debian [ 0.0] Examining the guest ... [ 35.0] Setting a random seed [ 35.0] Finishing off
Run Operations in Test Mode
All actions done inside image will be persistent. But if we want to work on test or dry run mode we can use -n
option where the action will not affect the image.
$ virt-customize -n -c qemu:///system -d debian [ 0.0] Examining the guest ... [ 6.0] Setting a random seed [ 7.0] Finishing off
Set RAM/Memory
You can specify memory usage with -m or –memory option. Default memory size can be seen with guesfish get-memsize command which is 500MB. In this example, we will set the memory size 512 MB with the -m
option like below.
$ guestfish get-memsize 500 $ virt-customize -m 512 -c qemu:///system -d debian [ 0.0] Examining the guest ... [ 6.0] Setting a random seed [ 6.0] Finishing off
Start Without Network
By default, the started image will be connected to the default network which is generally the internet connection. We can prevent to connect to the network with the --no-network
option like below.
$ virt-customize --no-network -c qemu:///system -d debian [ 0.0] Examining the guest ... [ 6.0] Setting a random seed [ 6.0] Finishing off
Verbose or Debug Mode
If there is a problem to troubleshoot or we need more information about the start we can print detailed information with the -v
verbose option.
$ sudo virt-customize -v -a fedora.qcow2

We can see from the screenshot that all actions and configuration are printed to the terminal in a detailed way.
Delete File
We can delete file and folders inside the VM image with the --delete
option by providing the file or folder name. In this example, we will delete the folder /root/.ssh
.
$ sudo virt-customize -c qemu:///system -d fedora --delete /root/.ssh [ 0.0] Examining the guest ... [ 4.0] Setting a random seed [ 4.0] Deleting: /root/.ssh [ 5.0] Finishing off
Set Hostname
We can set the hostname for the given system. We will use the --hostname
option and provide the hostname which is poftut.fed1
in this example. poftut
is the domain part where fed1
is the hostname.
$ sudo virt-customize -c qemu:///system -d fedora --hostname poftut.fed1 [ 0.0] Examining the guest ... [ 4.0] Setting a random seed [ 4.0] Setting the hostname: poftut.fed1 [ 5.0] Finishing off
Install Packages
We can also install packages into a domain image by using the --install
option. With the –install option we will also provide the package names we want to install. In this example, we will install packages named httpd and vim.
$ sudo virt-customize -c qemu:///system -d fedora --install httpd,vim [ 0.0] Examining the guest ... [ 3.0] Setting a random seed [ 3.0] Installing packages: httpd [ 49.0] Finishing off
Create Directory
To create directories on file system use --mkdir
option. There is no border it acts like root you can create whatever you want
$ sudo virt-customize -c qemu:///system -d fedora --mkdir /root/sil [ 0.0] Examining the guest ... [ 3.0] Setting a random seed [ 3.0] Making directory: /root/sil [ 4.0] Finishing off
Run Command
Up to now, we call options related to standard Linux commands also we can run commands inside disk image with –run-command option.
$ sudo virt-customize -c qemu:///system -d fedora --run-command "rmdir /root/sil" [ 0.0] Examining the guest ... [ 3.0] Setting a random seed [ 3.0] Running: rmdir /root/sil [ 4.0] Finishing off
Run Script
If we want to run script we can use –run command with the script path. In this example, we will run the script named test.sh
which is stored under the directory named downloads
$ sudo virt-customize -c qemu:///system -d fedora --run downloads/test.sh [ 0.0] Examining the guest ... [ 3.0] Setting a random seed [ 3.0] Running: downloads/test.sh [ 3.0] Finishing off
Upload File To The Image
We can upload a file from the host system to the image with the --upload
option. We will provide the local file name and the destination path in the image we wanto upload to. In this example, we will upload the file named test.sh
into the image /root/test.sh
.
$ sudo virt-customize -c qemu:///system -d fedora --upload downloads/test.sh:/root/test.sh [ 0.0] Examining the guest ... [ 2.0] Setting a random seed [ 2.0] Uploading: downloads/test.sh to /root/test.sh [ 3.0] Finishing off