Mount Linux 2nd Drive

Whenever you build a Linux VM with two or more drives, the first drive contains the operating system (OS) and is automatically attached to the VM. You need to manually “attach” the second and subsequent drives after the VM is built in order to use it/them. There are multiple ways of doing this, the tutorial below demonstrates how to create and mount a logical volume using a logical volume manager (LVM). In this example, there is a 15GB drive on which the OS is installed (primary drive) and a second 25GB drive for extra storage.

Step 1

Log into VM using the credentials that were emailed when the VM was built and run sudo apt update and sudo apt upgrade.

Step 2

Run command below to view the basic details about all available drives in the VM.

sudo lsblk

../_images/mount_secondary1.png

The secondary drive in the screenshot above is /dev/vdb.

Note that your secondary drive may be named something different.

Step 3

Install Logical Volume Manager library

sudo apt-get install lvm2

Step 4

Create Physical Volumes out of additional drives:

sudo pvcreate /dev/vdb

../_images/mount_secondary2.png

If there is warning reported that the physical volume cannot be initialised, this means /dev/vdb has some existing metadata or data that prevents it from being initialized as a physical volume. This occurs if the drive was previously mounted on a VM.

../_images/cant_initialize.png

To get the name of the Volume:

sudo vgscan

../_images/vgscan.png

To activate the volume group so that its logical volumes can be accessed:

sudo vgchange -ay MyVolGroup

../_images/vgchange.png

This makes the logical volumes available for mounting. Please continue to Step 8

Step 5

Create Volume Group named “MyVolGroup” out of all additional drives:

sudo vgcreate MyVolGroup /dev/vdb

../_images/mount_secondary3.png

Step 6

Create Logical Volume “MyLv” with all the available pace of the Volume Group “MyVolGroup”

sudo lvcreate -l 100%FREE -n MyLv MyVolGroup

../_images/mount_secondary4.png

Step 7

Format LV “MyLv” with the ext4 file system

sudo mkfs.ext4 /dev/MyVolGroup/MyLv

../_images/mount_secondary5.png

Step 8

Create a mount point.

sudo mkdir /mnt/data

Step 9

Mount the logical volume to the mount point

sudo mount /dev/mapper/MyVolGroup-MyLv /mnt/data/

Step 10

Modify /etc/fstab so that the LV is mounted during the boot

sudo nano /etc/fstab

Append the following to the end of the fstab file

/dev/mapper/MyVolGroup-MyLv /mnt/data ext4 defaults 0 0

../_images/mount_secondary6.png

Step 11

Logical Volume is mounted on /mnt/data

sudo df -h

../_images/mount_secondary7.png

Step 12

Reboot VM

sudo reboot