Tag Archives: pvmove command

How to safely remove disk from LVM

Learn how to safely remove the disk from LVM. It’s useful when you need to free up disks from the volume group and re-use somewhere else or replace a faulty disk.

How to safely remove disk from LVM

This article will serve solution for below questions :

  • How to safely remove the disk from LVM
  • How to remove the disk from VG online
  • How to copy data from one disk to other at the physical level
  • How to replace a faulty disk in LVM online
  • How to move physical extents from one disk to another
  • How to free up disk from VG to shrink VG size
  • How to safely reduce VG

We have volume group named vg01 which has 20M logical volume created in it and mounted it on /mydata mount point. Check lsblk output below –

root@kerneltalks # lsblk
NAME         MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda         202:0    0  10G  0 disk
├─xvda1      202:1    0   1M  0 part
└─xvda2      202:2    0  10G  0 part /
xvdf         202:80   0   1G  0 disk
└─vg01-lvol1 253:0    0  20M  0 lvm  /mydata

Now, attach new disk of the same or bigger size of the disk /dev/xvdf. Identify the new disk on the system by using lsblk command again and comparing the output to the previous one.

root@kerneltalks # lsblk
NAME         MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda         202:0    0  10G  0 disk
├─xvda1      202:1    0   1M  0 part
└─xvda2      202:2    0  10G  0 part /
xvdf         202:80   0   1G  0 disk
└─vg01-lvol1 253:0    0  20M  0 lvm  /mydata
xvdg         202:96   0   1G  0 disk

You can see the new disk has been identified as /dev/xvdg. Now, we will add this disk to current VG vg01. This can be done using vgextend command. Obviously, before using it in LVM you need to run pvcreate on it.

root@kerneltalks # pvcreate /dev/xvdg
  Physical volume "/dev/xvdg" successfully created.
root@kerneltalks # vgextend vg01 /dev/xvdg
  Volume group "vg01" successfully extended

Now we have disk to be removed /dev/xvdf and new disk to be added /dev/xvdg in the same volume group vg01. You can verify it using pvs command

root@kerneltalks # pvs
  PV         VG   Fmt  Attr PSize    PFree
  /dev/xvdf  vg01 lvm2 a--  1020.00m 1000.00m
  /dev/xvdg  vg01 lvm2 a--  1020.00m 1020.00m

Observe the above output. Since we created a 20M mount point from disk /dev/xvdf it has 20M less free size. The new disk /dev/xvdg is completely free.

Now, we need to move physical extents from disk xvdf to xvdg. pvmove is the command used to achieve this. You just need to supply a disk name from where you need to move out PE. Command will move PE out of that disk and write them to all available disks in the same volume group. In our case, only one other disk is available to move PE.

root@kerneltalks # pvmove /dev/xvdf
  /dev/xvdf: Moved: 0.00%
  /dev/xvdf: Moved: 100.00%

Move progress is shown periodically. If due to any reason operation interrupted in between then moved PE will remain at destination disks and un-moved PEs will remain on the source disk. The operation can be resumed by issuing the same command again. It will then move the remaining PE out of the source disk.

You can even run it in background with nohup.

root@kerneltalks # pvmove /dev/xvdf 2>error.log >normal.log &
[1] 1639

In the above command, it will run pvmove in the background. It will redirect normal console outputs in normal.log file under the current working directory whereas errors will be redirected and saved in error.log file in the current working directory.

Now if you check pvs output again, you will find all space on disk xvdf is free which means its not been used to store any data in that VG. This ensures you can remove the disk without any issues.

root@kerneltalks # pvs
  PV         VG   Fmt  Attr PSize    PFree
  /dev/xvdf  vg01 lvm2 a--  1020.00m 1020.00m
  /dev/xvdg  vg01 lvm2 a--  1020.00m 1000.00m

Before removing/detaching disk from the server, you need to remove it from LVM. You can do this by reducing VG and opting for that disk out.

root@kerneltalks # vgreduce vg01 /dev/xvdf
  Removed "/dev/xvdf" from volume group "vg01"

Now disk xvdf can be removed/detached from server safely.

Few useful switches of pvmove :

Verbose mode prints more detailed information on the operation. It can be invoked by using -v switch.

root@kerneltalks # pvmove -v /dev/xvdf
    Cluster mirror log daemon is not running.
    Wiping internal VG cache
    Wiping cache of LVM-capable devices
    Archiving volume group "vg01" metadata (seqno 17).
    Creating logical volume pvmove0
    activation/volume_list configuration setting not defined: Checking only host tags for vg01/lvol1.
    Moving 5 extents of logical volume vg01/lvol1.
    activation/volume_list configuration setting not defined: Checking only host tags for vg01/lvol1.
    Creating vg01-pvmove0
    Loading table for vg01-pvmove0 (253:1).
    Loading table for vg01-lvol1 (253:0).
    Suspending vg01-lvol1 (253:0) with device flush
    Resuming vg01-pvmove0 (253:1).
    Resuming vg01-lvol1 (253:0).
    Creating volume group backup "/etc/lvm/backup/vg01" (seqno 18).
    activation/volume_list configuration setting not defined: Checking only host tags for vg01/pvmove0.
    Checking progress before waiting every 15 seconds.
  /dev/xvdf: Moved: 0.00%
  /dev/xvdf: Moved: 100.00%
    Polling finished successfully.

The interval at which command updates the progress can be changed. -i switch followed by a number of seconds can be used to get updates from command on user-defined intervals on progress.

root@kerneltalks # pvmove -i 1 /dev/xvdf

LVM commands tutorial : Part 1 : Physical Volume (pvchange, pvmove)

Series of the tutorial to learn LVM commands. In this part of the tutorial, learn different tasks on physical volume  (pvchange, pvmove).

This is the second post on the LVM tutorial. Rest posts from this series can be found on the below links.

This is the last post for part 1 which is dedicated to physical volume. In the last post, we saw pvcreate and pvdisplay. This post we will be checking out pvchange and pvmove commands.

Command: pvchange

pvchange used for changing attributes of physical volume. There are 6 options switch which can be used with this command. Let’s see them one by one.

-a

This switch used for the availability attribute of PV. This should be accompanied with y or n argument. y making PV available and n making it unavailable. You can see the example below with n argument which turns PV unavailable which can be verified in pvdisplay output.

# pvchange -a n /dev/disk/disk3
Physical volume "/dev/disk/disk3" has been successfully changed.

# pvdisplay /dev/disk/disk3
--- Physical volumes ---
PV Name                     /dev/disk/disk3
VG Name                     /dev/vg01
PV Status                   unavailable
Allocatable                 yes
VGDA                        2
Cur LV                      2
PE Size (Mbytes)            8
----- output truncated -----
-S

This switch enables or disables auto-switching. It also has a y or n argument. If activated it makes the current access path to switch on the better available path. If deactivated, path switching happens only if the current access path goes to an unavailable state (due to hardware, cable failure). This applies to disks with multi-paths only.

-t

It’s a timeout value. This switch should be supplied with a number of seconds. This value determines if IO timeout happens and the problem can be declared for IO on particular PV. Useful in clustering.

# pvchange -t 90 /dev/disk/disk3
Physical volume "/dev/disk/disk3" has been successfully changed.
-x

It enables or disables the extensibility of PV. This switch has y and n arguments that enable admin to add/restrict physical extents to PV. Refer to LVM legends to have a better understanding.

-s

Immediately begin accessing the associated physical volume.

-z

Defines PV is spare or regular. This option has significance in mirroring only.

Command: pvmove

As name suggests, it used to move data (in LVM term PE) from one PV to another PV. Command essential moves PEs and hence data within from source to destination PV. If destination PV is not specified then all available PV’s in the current volume group are considered for move operation. Command decides himself best-suited PV for move operation so that allocation policies can be met correctly.

Also if the command is supplied with a list of PV names then the first PV is always considered as source and rest all PVs are considered as a destination.

# pvmove /dev/disk/disk1 /dev/disk/disk2 /dev/disk/disk3

In above example PEs will be moved from disk 1 to disk2 & disk3.

You can also move data for particular lvol to new PV.

# pvmove -n /dev/vg01/lvol2 /dev/disk/disk1 /dev/disk/disk2
Transferring logical extents of logical volume "/dev/vg01/lvol2"...
Physical volume "/dev/disk/disk1" has been successfully moved.
Volume Group configuration for /dev/vg01 has been saved in /etc/lvmconf/vg01.conf

# /usr/sbin/lvdisplay -v /dev/vg01/lvol2

----- output clipped -----

   --- Distribution of logical volume ---
   PV Name                 LE on PV  PE on PV
   /dev/disk/disk2         1557      1557

   --- Logical extents ---
   LE    PV1                     PE1   Status 1
   00000 /dev/disk/disk2         00000 current
   00001 /dev/disk/disk2         00001 current
   00002 /dev/disk/disk2         00002 current
   00003 /dev/disk/disk2         00003 current

In above example PE belonging to lvol2 will be moved from disk1 to disk2. PE distribution can be confirmed via lvdisplay output.

This marks end of part 1 : Physical volume. If you have any questions, please drop in the comments below.