Assume you want replace a small disk with large one, or to replace a slow SATA drive with SAS one. Within LVM, you can do it on the fly.
Note: You should always backup your system before attempting a disk remove operation.
Move data from one disk to other disks
Suppose you have a drive named /dev/hdb. You want to migrate the files on it to other disk(s).
Distributing extents to other disks in Volume Group
If you have enough free extents on the other disks in the volume group, you have it easy. Simply run
# pvmove /dev/hdb pvmove -- moving physical extents in active volume group "dev" pvmove -- WARNING: moving of active logical volumes may cause data loss! pvmove -- do you want to continue? [y/n] y pvmove -- 249 extents of physical volume "/dev/hdb" successfully moved
This will move the allocated physical extents from /dev/hdb onto the rest of the disks in the volume group.
Note: pvmove is slow, this is because it has to copy the contents of a disk block by block to one or more disks within the LVM.
Want to see the progress ? use '-v' option
# pvmove /dev/hdb -v
Now you can remove the disk from the VG, or leave it for other purpose.
Move data from a Disk to the other disk
Usually it's the case you want to replace current disk with a new one(faster, or bigger). If not, just ignore the first two steps and the very last one too.
Prepare the disk
First, you need to pvcreate the new disk to make it available to LVM. In this recipe we show that you don't need to partition a disk to be able to use it.
# pvcreate /dev/sdf pvcreate -- physical volume "/dev/sdf" successfully created
Add it to the volume group
As developers use a lot of disk space this is a good volume group to add it into.
# vgextend dev /dev/sdf vgextend -- INFO: maximum logical volume size is 255.99 Gigabyte vgextend -- doing automatic backup of volume group "dev" vgextend -- volume group "dev" successfully extended
Move the data
Next we move the data from the old disk onto the new one. Note that it is not necessary to unmount the file system before doing this. Although it is *highly* recommended that you do a full backup before attempting this operation in case of a power outage or some other problem that may interrupt it. The pvmove command can take a considerable amount of time to complete and it also exacts a performance hit on the two volumes so, although it isn't necessary, it is advisable to do this when the volumes are not too busy.
# pvmove /dev/hdb /dev/sdf pvmove -- moving physical extents in active volume group "dev" pvmove -- WARNING: moving of active logical volumes may cause data loss! pvmove -- do you want to continue? [y/n] y pvmove -- 249 extents of physical volume "/dev/hdb" successfully moved
Remove the unused disk
We can now remove the old IDE disk from the volume group.
# vgreduce dev /dev/hdb vgreduce -- doing automatic backup of volume group "dev" vgreduce -- volume group "dev" successfully reduced by physical volume: vgreduce -- /dev/hdb
The drive can now be either physically removed when the machine is next powered down or reallocated to some other users.