MaiDeveloper

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.

  1. Start fdisk for the new drive.

    fdisk /dev/vdb
    
  2. Type n for a new partition.

  3. Type p for a primary partition.

  4. Type 1 for first partition.

  5. Press Enter and Enter.

  6. Press p to verify the partition table.

    /dev/vdb1      ...     ...    ...    ..
    
  7. Press w to write changes and quit.

Formatting the new partition with ext4 file system

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


Mike Mai
Mike Mai   Brooklyn, New York
I am full-stack web developer, passionate about building world class web applications. Knowledge in designing, coding, testing, and debugging. I love to solve problems.