How can we add new virtual disk from Linux Host machine to Guest machine through Libvirt. Sometime our requirement is to add new hard disk on Guest machines, which can occur easily with libvirt tools and its commands. Let’s see some examples with proof of some screenshots.

setup

Host machine   srv3 192.168.1.11
Guest machine Guest_image_1 192.168.1.110

With this command you can add Disk on Guest machine.

[root@srv3 ~]# virsh attach-disk 9 /vm_images/Guest_image_1.1 xvdb --driver file

Disk attached successfully

Explanation of following commands options

virsh       -- command that used while adding new Disk
attach-disk -- This is options which used to attach disk
9           -- Machine ID number
/vm_imaes/Guest_image_1.1 -- This is file path that will act as new hard disk in Guest machine
Xvdb        -- Disk path name inside Guest machine
Driver      -- This is driver type need to bind the file with Guest machine, here we use file as we are using raw file.

Here we can see this Linux guest machine only have one hard disk attached. Now we will prepare disk file on host machine and attached it to Guest machine.

 

 

 

 

 

 

 

 

 

 

In this screenshot, we make 2 GB of file with dd command but actually its size is not 2 GB, as it start using in Guest machine will consume space from Guest file system.

We also add this file in Guest machine and able to see that in Guest terminal with fdisk  -l  command.
We can also detach this hard disk from guest OS with single command from Host machine.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Update-Sep 2017

In recent times, there are some changes in virtual disk attach in KVM through virsh commands. Below are details with examples.

Setup

We are working on Ubuntu machine 16.04 LTS with Virsh version 1.3.1

root@jarvis:~# lsb_release -d
Description:	Ubuntu 16.04.3 LTS

root@jarvis:~# virsh -v
1.3.1

qemu-system-x86_64: -V: invalid option
root@jarvis:~# kvm --version
QEMU emulator version 2.5.0 (Debian 1:2.5+dfsg-5ubuntu10.14), Copyright (c) 2003-2008 Fabrice Bellard

Example

In below examples, we have two disk,name Disk1 and Disk2, that attach to virtual machine name srv7

root@jarvis:~# virsh attach-disk srv7 --source /data/vm_images/CentOS7/Disk1 --target vdb --driver qemu
Disk attached successfully

root@jarvis:~# virsh attach-disk srv7 --source /data/vm_images/CentOS7/Disk2 --target vdc --driver qemu
Disk attached successfully

In case we need to detach Disk, we need to use below commands from Host machine

root@jarvis:~# virsh detach-disk srv7 vdb
Disk detached successfully

root@jarvis:~# virsh detach-disk srv7 vdc
Disk detached successfully