Working with Logical Volume Management

30 minutes
  • 5 Learning Objectives

About this Hands-on Lab

Effective storage management is a constant challenge in any enterprise environment. The ability to react quickly to increasing storage needs without disrupting the current configuration is key. In this activity, we will set up Logical Volume Manager (LVM) storage utilities to address this challenge.

We will partition a disk for use with LVM, and then create Physical Volumes, Volume Groups, and Logical Volumes. We will also delete a Logical Volume, and extend a Volume Group to be able to provide additional space in a Logical Volume. Learning how to use the flexibility of LVM makes us more effective at storage management.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create Linux LVM Type Partitions on /dev/xvdb and /dev/xvdc to Use All Space

Start an interactive root shell with sudo. Use fdisk to create a new default partition of type 8e on /dev/xvdb and /dev/xvdc. Create the first:

sudo -i
fdisk /dev/xvdb

Use the n command and choose all the defaults to create a new partition:

Command (m for help): n
Partition Type: p
Partition number (1-4, default 1): Press Enter to accept the default
First sector: Press Enter to accept the default
Last sector: Press Enter to accept the default

Use the t command to change to 8e (Linux LVM):

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Use the w command to write the changes to the partition table and exit:

Command (m for help): w

Now create the second:

fdisk /dev/xvdc

Use the n command and choose all the defaults to create a new partition:

Command (m for help): n
Partition Type: p
Partition number (1-4, default 1): Press Enter to accept the default
First sector: Press Enter to accept the default
Last sector: Press Enter to accept the default

Use the t command to change to 8e (Linux LVM):

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Use the w command to write the changes to the partition table and exit.

The result is that the /dev/xvdb1 and /dev/xvdc1 partitions of type 8e have been created.

Create Physical Volumes with the LVM Partitions /dev/xvdb1 and /dev/xvdc1, and Create the Volume Group volgroup Using /dev/xvdb1

Initialize the /dev/xvdb1 and /dev/xvdc1 LVM partitions as Physical Volumes with pvcreate. Inspect the Physical Volumes with pvs and pvdisplay:

pvcreate /dev/xvdb1 /dev/xvdc1
pvs
pvdisplay

Use vgcreate to create the volgroup Volume Group using the /dev/xvdb1 Physical Volume. Inspect volgroup with vgs and vgdisplay:

vgcreate volgroup /dev/xvdb1
vgs
vgdisplay
Create the Logical Volume datavol Using 3GB of Space and tempvol Using 1GB of Space

Use the lvcreate command to create two Logical Volumes, a 3 GB named datavol and a 1 GB named tempvol. Inspect them using lvs and lvdisplay commands:

lvcreate -n datavol -L3G volgroup
lvcreate -n tempvol -L1G volgroup
lvs
lvdisplay
Remove /dev/volgroup/tempvol, Extend volgroup with /dev/xvdc1, and Then Resize /dev/volgroup/datavol to Use All Space in the Volume Group

Use lvremove to get rid of the tempvol, then run vgextend to expand the volume group. Create an ext4 filesystem on /dev/volgroup/datavol and mount it on /mnt/data. Use lvresize to extend the datavol Logical Volume and filesystem to the maximum size possible. Check everything afterward using the df and vgs commands:

lvremove /dev/volgroup/tempvol
Type 'y' and Enter to confirm 
vgs
vgextend volgroup /dev/xvdc1
vgs
vgdisplay
mkfs -t ext4 /dev/volgroup/datavol
mkdir /mnt/data
mount  /dev/volgroup/datavol /mnt/data
df -h /mnt/data
lvresize -r -L 9.99G /dev/volgroup/datavol
df -h
Configure the /dev/volgroup/datavol Logical Volume to Mount on /mnt/data Persistently

Unmount the /mnt/data directory with the umount command:

umount /mnt/data

Add the following line to /etc/fstab:

/dev/volgroup/datavol /mnt/data ext4 defaults 0 1

Use an editor like vim to add the line to the file:

vim /etc/fstab

Mount the device with the mount command. Use df to verify it mounted:

mount -a
df -h /mnt/data

Additional Resources

The development team in our organization wants one of their workstations to store about 10 GB of data for a new web-based application. Although it currently has 10 GB of unallocated storage space, that is available on two separate 5 GB disks, /dev/xvdb and /dev/xvdc.

We have decided to take advantage of the features of Logical Volume Management (LVM) by combining those devices into a Volume Group, and using a Logical Volume named /dev/volgroup/datavol. They have asked us to make approximately 10 GB of storage space persistently mounted on the directory called /mnt/data.

Logging In

Use the credentials provided on the hands-on lab overview page, and log in as cloud_user.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?