How to use pvmove command in LVM in RHEL ?

In this article I am going to show you the usage of pvmove command. Its very useful commands. I have give couple of interviews and I always faced the question regarding it. But didn’t get the chance to use it in production.

Today, in my test lab I gave it a try.

My Lab setup:
RHEL 6.2 64-bit
Volume group with name : demo_vg
Logical Volume with name : demo_lv1
Physical disks used : /dev/sd[c-d]1

Step 1 : I created Volume group on top of physical volume “/dev/sdc1”. After creating volume group I created logical volume of 200MB size on it.

[root@Node2 ~]# vgcreate demo_vg /dev/sdc1
Volume group “demo_vg” successfully created

[root@Node2 ~]# lvcreate -n demo_lv1 –size 200M  demo_vg
Logical volume “demo_lv1” created

Step 2 : I am adding one physical volume “/dev/sdd1” to volume group demo_vg

[root@Node2 ~]# vgextend demo_vg /dev/sdd1
Volume group “demo_vg” successfully extended

[root@Node2 ~]# pvs
PV         VG       Fmt  Attr PSize    PFree
/dev/sda2  vg_node2 lvm2 a–    11.51g       0
/dev/sdc1  demo_vg  lvm2 a–  1016.00m  816.00m
/dev/sdd1  demo_vg  lvm2 a–  1016.00m 1016.00m
/dev/sde1           lvm2 a–  1019.72m 1019.72m

Step 3 : I have extended the logical volume by 100MB specifying the newly added logical volume. Now you can see that “/dev/sdd1″ is also in use now”

[root@Node2 ~]# lvextend -L+100M /dev/demo_vg/demo_lv1 /dev/sdd1
Extending logical volume demo_lv1 to 300.00 MiB
Logical volume demo_lv1 successfully resized

[root@Node2 ~]# pvs
PV         VG       Fmt  Attr PSize    PFree
/dev/sda2  vg_node2 lvm2 a–    11.51g       0
/dev/sdc1  demo_vg  lvm2 a–  1016.00m  816.00m
/dev/sdd1  demo_vg  lvm2 a–  1016.00m  916.00m
/dev/sde1           lvm2 a–  1019.72m 1019.72m

Step 4 : Created one file system on top of it. Mounted that file system and created one file of size 250MB just to show you the usage of pvmove command. Chosed 250 MB because it will include both physical volume for usage.

Note : output truncated to keep this article precise.

[root@Node2 ~]# mkfs.ext4 /dev/demo_vg/demo_lv1

[root@Node2 ~]# mount /dev/demo_vg/demo_lv1 /mnt

[root@Node2 ~]# df -h /mnt
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/demo_vg-demo_lv1
291M   11M  266M   4% /mnt

[root@Node2 ~]# cd /mnt

[root@Node2 mnt]# dd if=/dev/zero of=/mnt/vikrant bs=1M count=250
250+0 records in
250+0 records out
262144000 bytes (262 MB) copied, 8.28814 s, 31.6 MB/s

[root@Node2 mnt]# ll
total 256014
drwx——. 2 root root     12288 Sep 29 21:54 lost+found
-rw-r–r–. 1 root root 262144000 Sep 29 21:54 vikrant

Step 5 : Now I want to remove the added physical volume in Step 2. I can move the physical volume from “/dev/sdd1” to “/dev/sdc1” to avoid any data loss. But before doing that we need to make sure that our target physical volume has suffficent space to accomodate the data. This operation is online.

[root@Node2 mnt]# pvmove -v /dev/sdd1 /dev/sdc1
Finding volume group “demo_vg”
Archiving volume group “demo_vg” metadata (seqno 5).
Creating logical volume pvmove0
Moving 25 extents of logical volume demo_vg/demo_lv1
Found volume group “demo_vg”
activation/volume_list configuration setting not defined, checking only host tags for demo_vg/demo_lv1
Updating volume group metadata
Found volume group “demo_vg”
Found volume group “demo_vg”
Creating demo_vg-pvmove0
Loading demo_vg-pvmove0 table (253:4)
Loading demo_vg-demo_lv1 table (253:3)
Suspending demo_vg-demo_lv1 (253:3) with device flush
Suspending demo_vg-pvmove0 (253:4) with device flush
Found volume group “demo_vg”
activation/volume_list configuration setting not defined, checking only host tags for demo_vg/pvmove0
Resuming demo_vg-pvmove0 (253:4)
Found volume group “demo_vg”
Loading demo_vg-pvmove0 table (253:4)
Suppressed demo_vg-pvmove0 identical table reload.
Resuming demo_vg-demo_lv1 (253:3)
Creating volume group backup “/etc/lvm/backup/demo_vg” (seqno 6).
Checking progress before waiting every 15 seconds
/dev/sdd1: Moved: 4.0%
/dev/sdd1: Moved: 100.0%
Found volume group “demo_vg”
Found volume group “demo_vg”
Loading demo_vg-demo_lv1 table (253:3)
Loading demo_vg-pvmove0 table (253:4)
Suspending demo_vg-demo_lv1 (253:3) with device flush
Suspending demo_vg-pvmove0 (253:4) with device flush
Found volume group “demo_vg”
Resuming demo_vg-pvmove0 (253:4)
Found volume group “demo_vg”
Resuming demo_vg-demo_lv1 (253:3)
Found volume group “demo_vg”
Removing demo_vg-pvmove0 (253:4)
Removing temporary pvmove LV
Writing out final volume group after pvmove
Creating volume group backup “/etc/lvm/backup/demo_vg” (seqno 8).

Step 6 : Now compare this output with Step 3. /dev/sdd1 physical volume is totally free.

[root@Node2 mnt]# pvs
PV         VG       Fmt  Attr PSize    PFree
/dev/sda2  vg_node2 lvm2 a–    11.51g       0
/dev/sdc1  demo_vg  lvm2 a–  1016.00m  716.00m
/dev/sdd1  demo_vg  lvm2 a–  1016.00m 1016.00m
/dev/sde1           lvm2 a–  1019.72m 1019.72m

Step 7 : I have issued vgreduce command to remove the physical volume from volume group.

[root@Node2 mnt]# vgreduce demo_vg /dev/sdd1
Removed “/dev/sdd1” from volume group “demo_vg”

[root@Node2 mnt]# pvs
PV         VG       Fmt  Attr PSize    PFree
/dev/sda2  vg_node2 lvm2 a–    11.51g       0
/dev/sdc1  demo_vg  lvm2 a–  1016.00m  716.00m
/dev/sdd1           lvm2 a–  1019.72m 1019.72m
/dev/sde1           lvm2 a–  1019.72m 1019.72m

My data is intact 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s