How to move a physical volume on LVM

3 minute read

LVM move


If you need to move data from one disk to another one you could think in copying the data with cp, rsync, or any other tool, and then umount the partition, edit the /etc/fstab file to point to the new path, and all that involve to remove a disk or partition for another one. But if you forcasted and partitioned using LVM, there is a cleaner way of changing partitions on the fly, I mean, without umounting partitions and even leaving the same references in the /etc/file, because the same LVM partition will be used. Let’s see an example: if you have a 50 GB disk as a physical volume in /dev/sdb, which is set in a volume grup called data, and you need to move all data to another disk /dev/sdc, just do the following. First the initial distribution si shown:

LVM concepts


You can check the current state of the physical volumes using the pvs command:

# pvs
PV VG Fmt Attr PSize PFree
 /dev/sda2 system lvm2 a- 19.77g 0 
 /dev/sdb data lvm2 a- 50.00g 0

Then create the physical volume where the data will be moved:

# pvcreate /dev/sdc
 Physical volume "/dev/sdc" successfully created

Check the physical volumes again:

# pvs
PV VG Fmt Attr PSize PFree 
 /dev/sda2 system lvm2 a- 19.77g 0 
 /dev/sdb data lvm2 a- 50.00g 0 
 /dev/sdc lvm2 a- 50.00g 50.00g

Now check the volume groups:

# vgs
VG #PV #LV #SN Attr VSize VFree
 data 1 1 0 wz--n- 50.00g 0 
 system 1 6 0 wz--n- 19.77g 0

As you can see the volume group called data sizes 50 GB. Because this is the volume group which have the physical volume you want to move (/dev/sdb), you must add the physical volume to this group:

# vgextend data /dev/sdc
 Volume group "data" successfully extended

If you check it one more time you will see that the volume group has 100 GB  which correspond to  /dev/sdb (0 GB free) y /dev/sdc ( 50 GB free):

# pvs
PV VG Fmt Attr PSize PFree 
 /dev/sda2 system lvm2 a- 19.77g 0 
 /dev/sdb data lvm2 a- 50.00g 0 
 <strong>/dev/sdc data lvm2 a- 50.00g 50.00g</strong>
# vgs
 VG #PV #LV #SN Attr VSize VFree 
 <strong>data 2 1 0 wz--n- 99.99g 50.00g</strong>
 system 1 6 0 wz--n- 19.77g 0

Now let’s proceed to move the data from the physical volume  /dev/sdb to /dev/sdc:

LVM move


# pvmove /dev/sdb
/dev/sdb: Moved: 0.0%
/dev/sdb: Moved: 11.8%
/dev/sdb: Moved: 30.5%
/dev/sdb: Moved: 75.7%
/dev/sdb: Moved: 95.0%
/dev/sdb: Moved: 99.3%
/dev/sdb: Moved: 100.0%

There is not need to specify the other volume because the data will be distributed among the remaining physical volumes of the volume group, and in this case it is just /dev/sdc. Now, let’s check the distribution of the physical volumes:

# pvs
PV VG Fmt Attr PSize PFree 
 /dev/sda2 system lvm2 a- 19.77g 0 
 <strong>/dev/sdb data lvm2 a- 50.00g 50.00g</strong>
<strong> /dev/sdc data lvm2 a- 50.00g 0</strong>

As you can see the /dev/sdb has no data (50 GB free) while /dev/sdc has the data (0 GB free). Just

Como puede verse el disco /dev/sdb no tiene datos (50 GB libres) mientras que /dev/sdc tiene los datos (0 GB libres). Now remove the physical volume to disincorporate from the volume group:

# vgreduce data /dev/sdb
 Removed "/dev/sdb" from volume group "data"

Now check the volume groups and the physical volumes:

# pvs
PV VG Fmt Attr PSize PFree 
 /dev/sda2 system lvm2 a- 19.77g 0 
 <strong>/dev/sdb lvm2 a- 50.00g 50.00g</strong>
 /dev/sdc data lvm2 a- 50.00g 0
# vgs
VG #PV #LV #SN Attr VSize VFree
 <strong>data 1 1 0 wz--n- 50.00g 0</strong> 
 system 1 6 0 wz--n- 19.77g 0

Finally proceed to wipe the physical volume::

# pvremove /dev/sdb
 Labels on physical volume "/dev/sdb" successfully wiped
# pvs
PV VG Fmt Attr PSize PFree
 /dev/sda2 system lvm2 a- 19.77g 0
 /dev/sdc data lvm2 a- 50.00g 0

This allows data to be moved from one disk to another without copying the data on operating system level, without interrupting services or restarting the computer. At the end we will have the disk /dev/sdc as the physical volume of the volume data as shown below:

LVM final


Reference: http://www.tldp.org/HOWTO/LVM-HOWTO/removeadisk.html

Leave a Comment