How to add additional hard drive (disk image) to KVM virtual machine?
This section covers adding storage devices to a guest. Additional storage can only be added as needed.
Creating an new disk image
cd /var/lib/libvirt/images/
qemu-img create -f qcow2 new_image.qcow2 1000G
Attaching the disk image
virsh attach-disk Guest1 /var/lib/libvirt/images/new_image.qcow2 vdb --cache none
Partitioning the disk drive
The guest now has a hard drive called /dev/vdb
.
Start fdisk
for the new drive.
fdisk /dev/vdb
Type n
for a new partition.
Type p
for a primary partition.
Type 1
for first partition.
Press Enter
and Enter
.
Press p
to verify the partition table.
/dev/vdb1 ... ... ... ..
Press w
to write changes and quit.
mkfs.ext4 /dev/vdb1
Mounting the disk drive
First create a directory for mounting.
mkdir /disk2/
Mount the disk drive
mount /dev/vdb1 /disk2/
Edit fstab
to mount the disk drive on system startup
vi /etc/fstab
Add the following line to fstab
/dev/vdb1 /disk2 ext4 defaults 0 0
Mount the disk drive on startup using label (optional)
Label the disk partition
e2label /dev/vdb1 mylabel
Verify the label
e2label /dev/vdb1
Output
mylabel
Edit fstab
LABEL=mylabel /disk2 ext4 defaults 0 0