All HowTo's Linux Redhat, Fedora and CentOS Linux Ubuntu, Mint & Debian Linux Virtualization

How to Add a Disk to a KVM Virtual Machine

This article demonstrates how to add and remove a disk from a virtual machine running in KVM. We’re using CentOS 6 but the same works with CentOS 7.

First list the disks attached to the virtual machine before we add the new disk. Run the following command while logged into the virtual machine.

lsblk

Note the list of disks in the above output. In our example, we have “sda” but no “sdb”. We’ll use the name “sdb” as the name of the new disk later on.

Create the new disk. In this case we’re creating it in the “raw” format as a 2GB disk in the “/opt/iso/” location:

qemu-img create -f raw /opt/iso/disk2.img 2G

Now list the virtual machines. We need the name of the virtual machine that we’re going to add the disk to:

virsh list

Now attach it to the virtual machine. Run this command on the host server. Notice the name of the new disk as it will appear on the virtual machine, “sdb”:

virsh attach-disk TestCentos6 /opt/iso/disk2.img sdb

Run the following command to list the disk on the virtual machine. You should see your new disk:

lsblk

You can now partition and format this disk.

Now we’ll remove it. Run the following command to remove the new disk:

virsh detach-disk TestCentos6 sdb

At this point we’re back where we started. Detaching the disk doesn’t delete the disk from the host’s filesystem. It simply removes it from the virtual machine. You can re-attach it later if need be.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *