LFCS – Working With Logical Volumes

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
372
Reaction score
432
Credits
13,798
Sometimes, an administrator will need to change disk drives, add drives, or remove drives from the server. Sometimes, you may be tempted to back up a drive and replace the drive, then restore the data.

With Logical drives, we can manage this a little easier.

NOTE: A physical backup is always a good idea under any circumstance when dealing with hardware inside a server.

Physical, Volume Groups and Logical Volumes

A physical volume is the basic hardware disk drive in a system. Let’s say we add three Solid State Drives (SSD) that are 1 TB (terabyte) in size. Granted, in our example, we aren’t using volumes of that size. In our actual examples, we’ll use smaller disks, but we can assume they are larger, especially if you are following along with the previous examples and setup of the server.

We can take the Physical Volumes, usually designated with ‘PV’ and make a Volume Group (VG). The Volume Group can be made into a Logical Volume (LV) and formatted as a single usable disk.

The Volume Group can be manipulated as needed to add or remove disks.

For you to see this use on VirtualBox, you need to create another disk (SDC) and add it to the server. We can say 5GB in size and it does not need to be partitioned or formatted.

Create a Logical Volume

To start, we need to see what is already available on the system. For Ubuntu, there should be no information reported yet on these commands. CentOS already has a ‘swap’ and ‘root’ volume that shows up.

So, to list the physical drives, use the command ‘pvscan’ to list Physical Volumes. For Volume Groups, use the command ‘vgscan’. And to see Logical Volumes, use ‘lvscan’.

If you have been using the previous server setup, you may remember creating LVM partitions. These are marked as Logical Volumes in the partition table, but not to the Operating System (OS).

Now you need to keep in mind that the three we created are all on one physical drive and that they are partitions and not the whole physical disk.

You can see the setup we made by using the command:

Code:
sudo fdisk -l /dev/sdb

If you run ‘sudo su’ first, you do not need the ‘sudo’ word in the command above, or any of the following commands.

The output example is shown in Figure 1.

Figure 1.JPG

FIGURE 1

We will use the command ‘pvcreate’ to initialize physical volumes, disks or partitions, to be used as logical volumes.

Since we already have set aside partitions (/dev/sdb10, /dev/sdb11 and /dev/sdb12), we will use the first two to initialize as Physical Volumes.

Code:
sudo pvcreate /dev/sdb10
sudo pvcreate /dev/sd11

The command can be run on both CentOS and Ubuntu. After you execute the commands, you can run ‘pvscan’ and see the two Physical Volumes that have been created, as shown in Figure 2. This basically adds the specified devices to the Logical Volume Manager (LVM). Any device added to the LVM can be used by these commands.

Figure 2.JPG

FIGURE 2

Now that we have Physical Volumes created, we can now use these to create a Volume Group. A Volume Group is a grouping of Physical Volumes into a single storage unit. In this manner, we can use disks or partitions of multiple physical devices to create one logical drive.

NOTE: Keep in mind that if one disk or partition fails, the whole Volume Group fails, causing the Logical Volume to also fail. This is where redundancy comes into play. Redundant Array of Inexpensive Disks (RAID) will be covered in a later article.

To create a Volume Group, we use the command ‘vgcreate’. The command is:

Code:
sudo vgcreate vgroup /dev/sdb10 /dev/sdb11

The command will create a Volume Group named ‘vgroup’ using the Physical Volumes, ‘/dev/sdb10’ and ‘/dev/sdb11’, we created before. You cannot use a drive or partition unless you have added it to the LVM.

After creating the Volume Group, you can list the Volume Group with:

Code:
sudo vgscan

The output is shown in Figure 3.

Figure 3.JPG

FIGURE 3

The next step is to use the Volume Group to create a Logical Volume. Similar to the previous commands, the command is:

Code:
sudo lvcreate -n logvol -L 1900m vgroup

Here, we create a Logical Volume named (-n) ‘logvol’ using the Volume Group names ‘vgroup’. We specify a size using the ‘-L’ parameter and set the size to 1900MB, or 1.9GB. The individual Physical Volumes are 953MB each, so the total size we could use is 1906MB, but I went down a bit.

We can see the group using the command ‘lvscan’, as shown in Figure 4.

Figure 4.JPG

FIGURE 4

So, we now have a Logical Volume made up of two partitions that reside on the same physical disk. We need to format the drive for usage, which is detailed in ‘Creating File Systems’. Let’s make it ‘EXT4’, so use the command:

Code:
sudo mkfs.ext4 /dev/vgroup/logvol

The designation of the Logical Volume is in the ‘devices’ folder under the folder of the Volume Group name, ‘vgroup’. Then the sub-folder is the name given to the Logical Volume. Once the command is completed, we now have a formatted Logical Volume. The output is shown in Figure 5.

Figure 5.JPG

FIGURE 5

All we need to do now is mount it, but we will need a folder in which to mount it. So, create the folder ‘/lv/’, or any valid name. Mounting the Logical Volume can be done in the ‘/etc/fstab’ file by adding the line at the end of the file:

Code:
/dev/vgroup/logvol /lv ext4 defaults 0 0

After the line is added, use the command ‘sudo mount -a’ to remount the entries in the ‘fstab’ file. You should now be able to change to the folder ‘/lv’ and run:

Code:
df /lv

The output should be like Figure 6.

Figure 6.JPG

FIGURE 6

We have a mounted Logical Volume that is empty. Try the following command to copy some files into the ‘/lv’ folder from ‘/etc’:

Code:
sudo cp /etc/* /lv

Now that we have some existing files on the volume, you can see that it works properly.

Resizing the Logical Volume

After a volume is created, you may need to extend it, or even make it smaller. Either we can add a Physical Volume, or remove one (especially if a disk needs replaced).

We had created a few partitions, so let’s add one to our Logical Volume. Perform the following to create a Physical Volume and add it to the Volume Group.

Code:
sudo pvcreate /dev/sdb12
sudo vgextend vgroup /dev/sdb12
sudo lvextend -L +100m /dev/vgroup/logvol

NOTE: Be sure to use the plus sign (+) to add an amount and not change the amount to a new value.

We have extended the volume, but the new part is not formatted. We need to extend the format to the new portion:

Code:
sudo resize2fs /dev/vgroup/logvol

NOTE: Use the command for the file system you are using.

If you use the command ‘df /lv’, it should show the new size. By running the ‘ls’ command, you can see that the files are still intact.

To reduce the size of the Logical Volume, you need to unmount the volume:

Code:
sudo umount /lv

Once it is unmounted, you can then reduce the volume and the file system:

Code:
lvreduce -r -L -100M /dev/vgroup/logvol

Remount the volumes with ‘mount -a’ and you should have a new sized volume. The ‘-r’ is used to resize the file system, and the ‘-L -size’ is used to reduce the volume by the specified amount.

If you reduce the size, then you can check to see that the files are still on the volume.

We’ve added another partition to be included in the Logical Volume, but what if we want to replace one?

So, now we will use the SDC partition to add more space, then move the data from one Physical Volume and remove the Physical Volume. You can format ‘/dev/sdc1’ as needed and set the Flag to ‘lvm’. Yes, this can be done in Gparted or use ‘fdisk’ from the terminal.

It will work best if the Logical Volume is unmounted:

Code:
sudo umount /lv

We need to add the new ‘sdc1’ to the LVM as a Physical Volume:

Code:
sudo pvcreate /dev/sdc1

Now that it is added to the LVM, we can then extend the volume group with the new volume:

Code:
sudo vgextend vgroup /dev/sdc1

Let’s say we are removing the Physical Volume ‘/dev/sdb11’. We’ll assume we are replacing the disk if we assume it is a whole disk and not just a partition. To do this, we need to get any data from it and move it to the new drive:

Code:
sudo pvmove /dev/sdb11 /dev/sdc1

The data should be copied over and depending how much data there is will determine the time it will take. Once done, we can reduce the Volume Group and then remove ‘/dev/sdb11’.

Code:
vgreduce vgroup /dev/sdb11
pvremove /dev/sdb11

You can remount the Logical Volume with the command ‘mount -a’. If you run the command ‘pvscan’, you can see that ‘/dev/sdb11’ is now gone from the LVM listing.

Conclusion

Using Volumes can make things easier to swap out drives and still keep data online.

Give this a try using VirtualBox to be familiar with the usage. Play around and get used to adding, removing and changing volumes. It can definitely be handy to know on a system that already is set up with volumes.
 

Members online


Top