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.
Add a disk to the volume group
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
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.
Removing a physical volume
Step 1 move your data
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 <source> <target> 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.
Step 2 – remove the device from the volume group
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
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)
Step 3 – remove the physical volume
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).