Rate this page

Replace one of the physical volumes in an LVM volume group

Tested on

Debian (Lenny, Squeeze)
Ubuntu (Hardy, Interpid, Jaunty, Karmic, Lucid, Maverick, Natty, Oneiric, Precise)

Objective

To replace one of the physical volumes within an existing LVM volume group

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.

Method

Overview

The method described here has five steps:

  1. Optionally, initialise the new storage device as a physical volume.
  2. Add the new physical volume to the volume group.
  3. Migrate all data located on the old physical volume to the new physical volume.
  4. Remove the old physical volume from the volume group.
  5. 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.

Optionally, initialise the new storage device as a physical volume

With older versions of LVM it was necessary for physical volumes to be explicitly initialised using pvcreate before being added to a volume group:

pvcreate /dev/sdc

As of version 2.02.54 this is no longer necessary because initialisation will occur automatically if required. Prior initialisation 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.

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 pathname 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 initialised 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.

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

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

Optionally, wipe the label from the old storage device to prevent it from being detected as a physical volume

LVM will continue to recognise 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, repartitioning 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.

Troubleshooting

See Troubleshooting LVM.

Errors

Physical volume not found in volume group

An error of the form:

  Physical volume "/dev/sdc" not found in Volume Group "vg0"

in response to pvmove indicates that the source and destination physical volumes do not belong to the same volume group. The purpose of pvmove is to move data physically without affecting how it is presented logically. For this reason it can only move data within a volume group, not between volume groups.

If the new physical volume has merely not been added to the correct volume group then you can correct this using vgextend, as described above. If it has been added to the wrong volume group then you will first need to remove it using vgreduce.

See also

Tags: lvm