How to Attach Additional Disk on your KVM based VPS

Attaching an additional disk to your KVM-based Virtual Private Server (VPS) can provide you with extra storage space for data, applications, or any other specific needs. This process allows you to expand your server's storage capacity, making it a useful task for managing your VPS efficiently.

In this guide, we'll walk you through the steps to attach an additional disk to your KVM-based VPS. The process generally involves creating a new disk, formatting it, partitioning it with a filesystem, and mounting it to your VPS.

Once the additional disk has been assigned to your Server, we need to verify the disk by using the fdisk -l command:

fdisk -l

Output

root@vps:~# fdisk -l
Disk /dev/vda: 150 GiB, 161061273600 bytes, 314572800 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: CA147C86-F18D-46FC-A68E-BC0F0EFB040E

Device       Start       End   Sectors   Size Type
/dev/vda1     2048      4095      2048     1M BIOS boot
/dev/vda2     4096   1052671   1048576   512M Linux swap
/dev/vda3  1052672 314570239 313517568 149.5G Linux filesystem

Disk /dev/vdb: 50 GiB, 53687091200 bytes, 104857600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

As you can notice from the above output, there are two disks present on the server.

The first disk /dev/vda contains the main system partitions, while the second disk /dev/vdb is the newly added disk with no partitions present.

Note: It is important to note down the correct disk name for the below steps. Formatting the wrong disk would result in permanent data loss. In our case, it is vdb, but the disk name may vary for you

This is for EXT4 Filesystems only.

Format the new disk into EXT4 Filesystem with the below command.

mkfs.ext4 /dev/vdb

Create a mount directory on the server on which the new disk will be mounted.

This can be named as per your requirements.

mkdir /disk2

Now, mount the disk onto the /disk2 directory.

mount /dev/vdb /disk2/

Next, we will add an entry into /etc/fstab so the disk is mounted permanently.

vi /etc/fstab

Add the following line to the end of the file:

/dev/vdb    /disk2    ext4     defaults    0 0

Save changes and exit the file.

Verify the changes made and that the disk is mounted onto /disk2 with the following commands:

fdisk -l

df -Th

Output:

root@vps:~# fdisk -l
Disk /dev/vda: 150 GiB, 161061273600 bytes, 314572800 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: CA147C86-F18D-46FC-A68E-BC0F0EFB040E

Device       Start       End   Sectors   Size Type
/dev/vda1     2048      4095      2048     1M BIOS boot
/dev/vda2     4096   1052671   1048576   512M Linux swap
/dev/vda3  1052672 314570239 313517568 149.5G Linux filesystem

Disk /dev/vdb: 50 GiB, 53687091200 bytes, 104857600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
root@vps:~# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
tmpfs          tmpfs   96M 1008K   95M   2% /run
/dev/vda3      ext4   148G  3.1G  137G   3% /
tmpfs          tmpfs  479M     0  479M   0% /dev/shm
tmpfs          tmpfs  5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs   96M  4.0K   96M   1% /run/user/0
/dev/vdb       ext4    49G   24K   47G   1% /disk2