Working with logical Volumes (part 3)

By: Luke Rawlins Dec 12, 2017 | 4 minutes read
Share this:

Tags: filesystem, Linux, lvm

Following this tutorial assumes that you have followed along with the other two parts in this series. However, if you already have some familiarity with Linux you should be able to follow along.

Working with logical volumes (part 1)

Working with logical volumes (part 2)

One of the great things about lvm is that you can add and remove physical volumes on the fly without data loss and without interrupting services.

If you haven’t already done so, add a new hard disk to your virtual machine. I created an additional 10 GB disk but you can make the disk any size you want. It doesn’t have to match the previous disk that we created.

When I run sudo fdsik -l among my output is the following:

......
Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
.......

I have two disks that are 10 GB in size, however, if you remember from the last post we already identified /dev/sdb as the disk that has been used as a physical volume in our vgtest volume group. Knowing this I can safely create a new physical volume with /dev/sdc. (Check part 1 if you need a refresher in how to become familiar with your disk setup)

sudo pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created.

Now we want to add /dev/sdc into the vgtest volume group, using the vgextend command.

sudo vgextend vgtest /dev/sdc
Volume group "vgtest" successfully extended

That’s all it takes to add a new disk to the volume group. You can see that the disk has been added by once again running pvscan and looking at the output.

sudo pvscan
PV /dev/sdb    VG vgtest          lvm2 [10.00 GiB / 6.00 GiB free]
PV /dev/sdc    VG vgtest          lvm2 [10.00 GiB / 10.00 GiB free]

As opposed to the first time we ran this command we can now see that “vgtest” has two disks associated with it. At the terminal run sudo vgdisplay vgtest -v and take a close look at the output. If you were successful in adding the disk you should see an abundance of information about the entire volume group.

What if I need to get rid of one of the disks in my volume group? The first step in this process is to move all existing data off of the physical volume that you no longer need.

You can move data from a physical volume with the pvmove command. I’ll demonstrate how to do this by moving all of the data from /dev/sdb to /dev/sdc.

sudo pvmove /dev/sdb /dev/sdc
/dev/sdb: Moved: 0.05%
.....
/dev/sdb: Moved: 50.00%
.....
/dev/sdb: Moved: 100.00%

What you see above is the abbrevieated output of the pvmove command when performed on a live system. The syntax for pvmove is just like the cp command. You first list the device that you want to move data from, next is the device that you want to move that data to.

In my case I want to remove the first device we used which was /dev/sdb. This is the device that contained the data we wanted to move to /dev/sdc. To remove a device from a volume group use the vgreduce command.

sudo vgreduce vgtest /dev/sdb
Removed "/dev/sdb" from volume group "vgtest"

The syntax on this command can be a bit tricky to remember (at least for me). You need to specify the device that you want to remove, not the one you are keeping. You are reducing the volume group by the obsolete device. Not reducing it down to the one you are keeping….

Now when you do pvscan you should be able to see that your vgtest volume group only contains one physical volume (/dev/sdc). But you can also still see that there is a physical volume on /dev/sdb.

sudo pvscan
  PV /dev/sdc    VG vgtest          lvm2 [10.00 GiB / 2.00 GiB free]
  PV /dev/sdb                       lvm2 [10.00 GiB]

Notice that /dev/sdb doesn’t have an associated volume group (notated by “VG” in the above output)

Once you are sure that you no longer need the old device go ahead and remove it.

sudo pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped

Working with logical volumes is actually much easier than many people make it out to be. The best part about using LVM is that you do not need to stop any services, or reboot the machine in order to make the changes you want. LVM allows you to make all of these changes without any kind of interruption in the normal operation of a Linux Server (or Desktop).

Related Posts


Working with logical volumes (part 2)

In this post I want to cover one of the most commonly used features of lvm, extending a logical volume. If you were following along with the last post, “Working with logical volumes part 1”, then you should already have a volume group with a couple of live volumes attached. With lvm you can quickly and easily extend a Linux file system on the fly without interrupting any services. Becoming familiar with our lvm environment. Read more

Working with files in Linux - File Attributes

In the previous two posts, we’ve looked at file permissions and access control lists. Today let’s take look at file attributes. Whereas, permissions and ACL’s deal with user and group access to a file, attributes are properties of a file that regulate how the operating system interacts with a given file. There are 15 file attributes: append only (a), no atime updates (A), compressed (c), no copy on write (C), no dump (d), synchronous directory updates (D), extent format (e), immutable (i), data journalling (j), project hierarchy (P), secure deletion (s), synchronous updates (S), no tail-merging (t), top of directory hierarchy (T), and undeletable (u). Read more

Working with logical volumes (part 1)

Part one of working with logical volumes will cover the basic’s involved in creating logical volumes. TL;DR For those of you who just want the order of the commands. sudo pvcreate </path/to/device> sudo vgcreate <vgname> </path/to/device> sudo lvcreate -n <lvname> -L <size> <vgname> sudo mkfs.<filesystem> </path/to/lv> What you need to follow this guide * A free disk (I used an empty virtual machine disk) * Any Linux distribution (In this example I'll be using Fedora 26, but the commands are the same across the entire Linux spectrum) * LVM packages (lvm2 - usually pre-installed) What is LVM? Read more


Contact

If you’d like to get in touch, contact with me via email - or follow on Twitter.

[email protected]