How to add disk image to KVM virtual machine with virsh command

How do I add additional disk storage to a guest operating system to KVM virtual machine with virsh command on Linux operating system?

You can easily add additional hard drives to KVM domain/VM to get increased storage space for a guest. In this tutorial shows you how to add additional disk storage to a KVM VM using virsh command line option on Linux.

Here are steps to add file-based storage (disk image) to virtual machine with virsh command on Linux:

Step 1 – Create the new disk image

Type the following command on KVM host to create a new disk image called ubuntu-box1-vm-disk1-5G with 5G size:
$ cd /var/lib/libvirt/images/
$ sudo qemu-img create -f raw ubuntu-box1-vm-disk1-5G 5G
[sudo] password for vivek:
Formatting 'ubuntu-box1-vm-disk1-5G', fmt=raw size=5368709120

Or you can use the dd command to create a pre-allocated file using the following command:
$ sudo dd if=/dev/zero of=ubuntu-box1-vm-disk1-5G bs=1M count=5120 status=progress
5192548352 bytes (5.2 GB, 4.8 GiB) copied, 10.5212 s, 494 MB/s
5120+0 records in
5120+0 records out
5368709120 bytes (5.4 GB, 5.0 GiB) copied, 10.9421 s, 491 MB/s

You just created qemu-img or dd command to create a new raw disk image with a size of 5 GB. The disk image is named ubuntu-box1-vm-disk1-5G:
$ sudo ls -lh
Sample outputs:

total 17G
-rw------- 1 libvirt-qemu kvm   41G Feb 17 15:41 freebsd11.qcow2
-rw------- 1 libvirt-qemu kvm  3.1G Feb 17 15:33 ubuntu-box-1-clone.qcow2
-rw------- 1 libvirt-qemu kvm   41G Feb 17 15:34 ubuntu-box-1.qcow2
-rw-r--r-- 1 root         root 5.0G Feb 17 15:42 ubuntu-box1-vm-disk1-5G

A note about qcow2 format

Raw disk image format is default. This format has the advantage of being simple and easily exportable to all other emulators. However, QEMU image format (qcow2) the most versatile format. If you need to take VM snapshots or AES encryption. Try qcow2 format. The syntax is as follows:
$ sudo qemu-img create -f qcow2 ubuntu-box2-vm-disk1-5G 5G

Step 2 – Attach the disk to the virtual machine

Before you attache the disk to your VM, find out current disk name. Login to your VM and type the following command:
$ df
$ sudo fdisk -l | grep '^Disk /dev/vd[a-z]'
Sample outputs:

Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 sectors

So my VM has /dev/vda with 40GiB size. To attach newly created ubuntu-box1-vm-disk1-5G image, you must use a /dev/vdb. If you already have a /dev/vdb disk you need to change vdb to a free device like /dev/vdc and so on. The syntax is as follows to attach the disk to a vm called ubuntu-box1:
# virsh attach-disk {vm-name} /var/lib/libvirt/images/{img-name-here} vdb --cache none
# virsh attach-disk {vm-name} \
--source /var/lib/libvirt/images/{img-name-here} \
--target vdb \

For example, attach the disk image ‘/var/lib/libvirt/images/ubuntu-box1-vm-disk1-5G‘ as a virtio disk to the VM/domain named ‘ubuntu-box1‘ and update the domain xml file for new disk (type command on the host):
$ sudo virsh attach-disk ubuntu-box1 /var/lib/libvirt/images/ubuntu-box1-vm-disk1-5G vdb --cache none
Sample outputs:

[sudo] password for vivek: 
Disk attached successfully

Be Careful: With just a few keystrokes, fdisk can wipe out part or all of your hard disk or working partition. Make sure you use correct device names with the fdisk command.

Step 3 – Partitioning the disk drive in VM

Now the guest named ‘ubuntu-box1’ now has a hard disk device called /dev/vdb. Login to your VM and type the following command to verify the same:
$ sudo fdisk -l | grep '^Disk /dev/vd[a-z]'
Sample outputs:

Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk /dev/vdb: 5 GiB, 5368709120 bytes, 10485760 sectors

Next, start fdisk for the new device:
$ sudo fdisk /dev/vdb
Type n for a new partition. Type p for a primary partition. Choose an available partition number 1. Enter the default first cylinder by pressing Enter. Choose the entire disk is allocated by pressing Enter. Finally type p to verify new partition. Enter w to write changes and quit. Sample session from the fdisk command:

Fig.01: Partition the drive with fdisk command in VM

To format the new partition with the ext4 file system, enter:
$ sudo mkfs.ext4 /dev/vdb1
Sample outputs:

Fig.02: Format /dev/vdb1 it as ext4

Finally, you need to create a mount directory:
$ sudo mkdir /disk2/
And mount the disk on the guest:
$ sudo mount /dev/vdb1 /disk2/
Edit the file /etc/fstab
$ sudo vi /etc/fstab
And update it as follows so that /dev/vdb1 get mounted across reboot persistently:
/dev/vdb1    /disk2    ext4     defaults    0 0

Save and close the file. And there you have it, the guest VM now has an additional virtualized file-based storage device under KVM Linux-based system.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Nehal J Wani Feb 18, 2017 @ 15:59

    One can also create a sparse image using the command:
    truncate -s 5G ubuntu-box1-vm-disk1-5G

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum