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).
Luke has an RHCSA for Red Hat Enterpirse Linux 7 and currently works as a Linux Systems Adminstrator in Ohio.