How To Install A New Drive to Ubuntu LVM and Mount It

How To Install A New Drive to Ubuntu LVM and Mount It

This article will guide you through the process of installing a new drive to your Ubuntu system using Logical Volume Manager (LVM) and mounting it for persistent use. LVM is a flexible and powerful tool for managing disk storage, allowing you to easily resize and manage logical volumes.

Step 1: Check the Drives

To begin, let’s check the available drives on your system using the lshw command:

sudo lshw -C disk

Output:

  *-disk:0
      description: ATA Disk
      product: Samsung SSD 870
      physical id: 0
      bus info: scsi@0:0.0.0
      logical name: /dev/sda
      version: 3B6Q
      serial: S758NS0W807436T
      size: 3726GiB (4TB)
      configuration: ansiversion=5 logicalsectorsize=512 sectorsize=512
 *-disk:1
      description: ATA Disk
      product: RS512GSSD310
      physical id: 1
      bus info: scsi@1:0.0.0
      logical name: /dev/sdb
      version: 2A0
      serial: EB091502A000561
      size: 476GiB (512GB)
      capabilities: gpt-1.00 partitioned partitioned:gpt
      configuration: ansiversion=5 guid=0c679d6a-d4b6-40a1-962c-75145d093352 logicalsectorsize=512 sectorsize=512

This command displays detailed information about the disks connected to your system. In this example, we have two disks: /dev/sda (4TB) and /dev/sdb (512GB).

Step 2: Display Physical Volumes (PV)

To display the existing physical volumes (PV) on your system, use the pvs command:

sudo pvs

Output:

 PV         VG        Fmt  Attr PSize    PFree
 /dev/sdb3  ubuntu-vg lvm2 a--  <473.89g    0

This output shows that there is currently one physical volume (/dev/sdb3) associated with the volume group ubuntu-vg.

Step 3: Create Physical Volumes (PV) on New Disk

To create a new physical volume on the new disk (/dev/sda), use the pvcreate command:

sudo pvcreate /dev/sda

Output:

Physical volume "/dev/sda" successfully created.

Verify the creation of the new physical volume using lvmdiskscan:

sudo lvmdiskscan -l

Output:

 WARNING: only considering LVM devices
 /dev/sda   [      <3.64 TiB] LVM physical volume
 /dev/sdb3  [    <473.89 GiB] LVM physical volume
 1 LVM physical volume whole disk
 1 LVM physical volume

Step 4: Create a Volume Group (VG)

Create a new volume group named mediavg using the new physical volume /dev/sda:

sudo vgcreate mediavg /dev/sda

Output:

volume group "mediavg" successfully created

Step 5: Create a Logical Volume (LV)

Create a new logical volume named medialv within the mediavg volume group, using all available space:

sudo lvcreate -l +100%FREE -n medialv mediavg

Output:

Logical volume "medialv" created.

Step 6: Create a Filesystem

Create an ext4 filesystem on the new logical volume:

sudo mkfs.ext4 /dev/mediavg/medialv

Output:

mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done
Creating filesystem with 976753664 4k blocks and 244195328 inodes
Filesystem UUID: 2d665675-4b2e-4a1f-9af6-4652e387d76e
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
	4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
	102400000, 214990848, 512000000, 550731776, 644972544

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

Step 7: Mount the Filesystem

Create a mount point directory and mount the new filesystem:

sudo mkdir -p /media/storage
sudo mount /dev/mediavg/medialv /media/storage

Verify the mount using the df command:

df -h

Output:

Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  2.4M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  466G   23G  424G   6% /
tmpfs                              7.8G     0  7.8G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sdb2                          2.0G  252M  1.6G  14% /boot
/dev/sdb1                          1.1G  6.1M  1.1G   1% /boot/efi
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/1000
/dev/mapper/mediavg-medialv        3.6T   28K  3.4T   1% /media/storage

Step 8: Make the Mount Persistent

To ensure the filesystem is mounted automatically on system boot, add an entry to the /etc/fstab file.

Obtain the UUID of the new filesystem using blkid:

sudo blkid /dev/mediavg/medialv

Output:

/dev/mediavg/medialv: UUID="2d665675-4b2e-4a1f-9af6-4652e387d76e" BLOCK_SIZE="4096" TYPE="ext4"

Edit /etc/fstab and add a line like the following, replacing UUID with the actual UUID:

UUID=2d665675-4b2e-4a1f-9af6-4652e387d76e /media/storage ext4 defaults 0 2

Step 9: Reboot and Verify

Reboot the server to ensure the new filesystem is mounted automatically:

sudo reboot

After the reboot, check the mounted filesystems using df:

df -h

Output:

Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  2.4M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  466G   23G  424G   6% /
tmpfs                              7.8G     0  7.8G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sdb2                          2.0G  252M  1.6G  14% /boot
/dev/sdb1                          1.1G  6.1M  1.1G   1% /boot/efi
/dev/mapper/mediavg-medialv        3.6T   28K  3.4T   1% /media/storage
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/1000

The output should show that /dev/mapper/mediavg-medialv is mounted on /media/storage.

Conclusion

Congratulations! You have successfully installed a new drive to your Ubuntu system using LVM, created a logical volume, formatted it with the ext4 filesystem, and mounted it persistently. You can now use the /media/storage directory to store your data on the new drive.