How to attach disk image to KVM virtual machine with virsh

This article explains the detailed steps to attach disk image to KVM virtual machine with virsh command.

Step1: Create the new disk image in the current folder

For raw format,

#qemu-img create -f raw disk1-5G 5G

For qcow2 format,

#qemu-img create -f qcow2 disk1-5G 5G

Step2: Attach new disk to the virtual server

For attaching the disk temporarily,

#virsh attach-disk {vm-name} {img-name-here} vdb --cache none

For attaching the disk permanently,

#virsh attach-disk {vm-name} \
--source {img-name-here} \
--target vdb \
--persistent

Step3: Partition the new disk

#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.

Step4: Format the partition

#mkfs.ext4 /dev/vdb1

Step5: Create mount point

#mkdir /disk1

Step6: Mount the new disk

#mount /dev/vdb1 /disk1

Step7: Open the file '/etc/fstab' and update the below entry for mounting the disk permanently

/dev/vdb1 /disk2 ext4 defaults 0 0

 

That's all.........

 

Leave a Reply