- 10File & Disk
- Adding software RAID to LVM configurations
- Basics of making a LVM
- Convert Ext2 till Ext3
- Create and Resize a LUKS Encryped LVM Partition
- Create partition with Parted
- Enabling Encrypted Swap
- How to completely remove a hdd in linux
- How to encrypt hdd/partition/file with cryptsetup and LUKS support
- How to Tweak Your SSD in Ubuntu for Better Perform
- Replace one of the physical volumes in an LVM volume
To replace one of the physical volumes within an existing LVM volume group
/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
The method described here has five steps:
- Optionally, initialise 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.
With older versions of LVM it was necessary for physical volumes to be explicitly initialised using
pvcreate before being added to a volume group:
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.
A physical volume can be added to a volume group using the
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
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
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/sdc are members of
Data can be transferred from one physical volume to another using the
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
PV VG Fmt Attr PSize PFree /dev/sdb vg0 lvm2 a- 100.00g 100.00g /dev/sdc vg0 lvm2 a- 200.00g 100.00g
A Physical volume can be removed from a volume group using the
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
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.