How to Replace One of the Physical Volumes in an LVM Volume Group
In this article, I’ll show you how to replace the physical volumes safely from a LVM volume group on Linux systems (RPM Family “Redhat / CentOS / Scientific Linux” and Debian Family “Debian / Ubuntu” and other families ). As a system administrator, you will face this scenario and you must know how to replace one of the physical volumes within an existing LVM volume group without loosing your data.
Scenario
Suppose that /dev/vg0 is a volume group composed of a single physical volume located on a hard drive with a capacity of 100GB. You wish to replace this with a new hard drive having a capacity of 200GB. You have been able to temporarily connect both drives to the system, such that the old drive is presented as /dev/sdb and the new drive as /dev/sdc.
The method described here has five steps:
- Initialize the new storage device as a physical volume.
- Add the new physical volume to the volume group.
- Migrate all data located on the old physical volume to the new physical volume.
- Remove the old physical volume from the volume group.
- Optionally, wipe the label from the old storage device to prevent it from being detected as a physical volume.
The corresponding sequence of commands is as follows:
pvcreate /dev/sdc vgextend vg0 /dev/sdc pvmove /dev/sdb /dev/sdc vgreduce vg0 /dev/sdb pvremove /dev/sdb
Be aware that any operation of this nature carries some risk of data loss. This is unlikely if the hardware is in good working order, but it would be prudent to make a backup of the relevant volume group before starting.
Step 1: Initialize the new storage device as a physical volume
With older versions of LVM it was necessary for physical volumes to be explicitly initialized using pvcreate before being added to a volume group:
# pvcreate /dev/sdc
As of version 2.02.54 this is no longer necessary because initialization will occur automatically if required. Prior initialization may still be desirable in order to deviate from the default settings used by pvcreate, obtain better diagnostics by proceeding one step at a time, or retain compatibility with older versions of LVM.
Step 2: Add the new physical volume to the volume group
A physical volume can be added to a volume group using the vgextend command:
# vgextend vg0 /dev/sdc
The first argument is the name of the volume group to be extended. This can be written as a path-name if you prefer (/dev/vg0). Subsequent arguments are the physical volumes to be added.
If successful you should see a response of the form:
Volume group "vg0" successfully extended
If the physical volumes have not previously been initialized using pvcreate then there will be some additional diagnostic messages, for example:
No physical volume label read from /dev/sdc
Physical volume "/dev/sdc" successfully created
Volume group "vg0" successfully extended
You can check that the physical volume has been added using the pvs command:
# pvs
If successful then you should see a response of the form:
PV VG Fmt Attr PSize PFree /dev/sdb vg0 lvm2 a- 100.00g 0 /dev/sdc vg0 lvm2 a- 200.00g 200.00g
The VG column indicates which volume group each physical volume is a member of (if any). In this instance it shows (as expected) that both /dev/sdb and /dev/sdc are members of vg0.
Step 3: Migrate all data located on the old physical volume to the new physical volume.
Data can be transferred from one physical volume to another using the pvmove command:
# pvmove /dev/sdb /dev/sdc
The first argument is the physical volume to be emptied. The second argument is the physical volume to which the content should be moved. For large storage devices the transfer can take a considerable amount of time, however the machine should remain usable during this period so pvmove can be left to run in the background. It is safe to continue using filing systems that are wholly or partly located within the physical volume that is being moved.
pvmove periodically reports the progress it has made as a percentage, returning control when the transfer is complete. If it is interrupted for any reason then the transfer can be resumed by executing pvmove again with no arguments.
You can check that the migration was successful using the pvs command again. The response should show that the source physical volume contains no data (and therefore that PFree
is equal to PSize
):
PV VG Fmt Attr PSize PFree /dev/sdb vg0 lvm2 a- 100.00g 100.00g /dev/sdc vg0 lvm2 a- 200.00g 100.00g
Step 4: Remove the old physical volume from the volume group.
A Physical volume can be removed from a volume group using the vgreduce command:
# vgreduce vg0 /dev/sdb
The first argument is the name of the volume group. Subsequent arguments are the names of physical volumes to be removed. If successful you should see a response of the form:
Removed "/dev/sdb" from volume group "vg0"
You can check that the physical volume has been removed using the pvs command again. The response should show that the source physical volume is no longer a member of any volume group:
PV VG Fmt Attr PSize PFree /dev/sdb lvm2 a- 100.00g 100.00g /dev/sdc vg0 lvm2 a- 200.00g 100.00g
Step 5: Optionally, wipe the label from the old storage device to prevent it from being detected as a physical volume
LVM will continue to recognize the old storage device as a physical volume (albeit an empty one) unless you take explicit action to wipe the label that was written by pvcreate. This can be done using the pvremove command:
# pvremove /dev/sdb
If successful you should see a response of the form:
Labels on physical volume "/dev/sdb" successfully wiped
Leaving the label in place is not necessarily harmful, but it can cause confusion in some circumstances. For example, re-partitioning a hard drive can result in LVM discovering physical volumes that are the wrong size for the drive layout. Use of pvremove is therefore recommended unless there is a reason not to.
Summary
In this article, we have explained in five steps how to replace a physical volume “hard disk” in a volume group that contains only one physical volume “one hard disk. The steps are straight forward, but caution must be taken. You should backup your existing data on the old physical volume first.
I hope this article is good enough for you.
See you in other articles.
If You Appreciate What We Do Here On Mimastech, You Should Consider:
- Stay Connected to: Facebook | Twitter | Google+
- Support us via PayPal Donation
- Subscribe to our email newsletters.
- Tell other sysadmins / friends about Us - Share and Like our posts and services
We are thankful for your never ending support.