Wednesday, 30 October 2019

LVM - Data Migration

Use LVM for live data migration

I have to migrate some data to new hard drive. Lets use LVM to do that.

Source: volume group vg_mydata
Destination: /dev/sdd
[root@rifle ~]# pvs
  PV         VG           Fmt  Attr PSize   PFree
  /dev/sda2  centos_rifle lvm2 a--   <7.00g     0
  /dev/sdb   vg_mydata    lvm2 a--    1.03g     0
  /dev/sdc   vg_mydata    lvm2 a--  516.00m 32.00m

[root@rifle ~]# lvmdiskscan
  /dev/centos_rifle/root   [      <6.20 GiB]
  /dev/sda1                [       1.00 GiB]
  /dev/centos_rifle/swap   [     820.00 MiB]
  /dev/sda2                [      <7.00 GiB] LVM physical volume
  /dev/vg_mydata/lv_mydata [       1.50 GiB]
  /dev/sdb                 [      <1.04 GiB] LVM physical volume
  /dev/sdc                 [     519.34 MiB] LVM physical volume
  /dev/sdd                 [       2.00 GiB]
  4 disks
  1 partition
  2 LVM physical volume whole disks
  1 LVM physical volume

1. Adding /dev/sdd to logical volume manager

[root@rifle ~]# pvcreate /dev/sdd
  Physical volume "/dev/sdd" successfully created.

2. Adding new disk to the volume group

[root@rifle ~]# vgextend vg_mydata /dev/sdd
  Volume group "vg_mydata" successfully extended

[root@rifle ~]# pvs
  PV         VG           Fmt  Attr PSize   PFree
  /dev/sda2  centos_rifle lvm2 a--   <7.00g     0
  /dev/sdb   vg_mydata    lvm2 a--    1.03g     0
  /dev/sdc   vg_mydata    lvm2 a--  516.00m 32.00m
  /dev/sdd   vg_mydata    lvm2 a--   <2.00g <2.00g

In vg_mydata we have two disks /dev/sdb and /dev/sdc, which have 0 and 32Mb free space respectively.

3.Moving the data from the /dev/sdb to /dev/sdd

[root@rifle ~]# pvmove /dev/sdb /dev/sdd
  /dev/sdb: Moved: 1.14%
  /dev/sdb: Moved: 28.03%
  /dev/sdb: Moved: 44.32%
  /dev/sdb: Moved: 62.88%
  /dev/sdb: Moved: 79.17%
  /dev/sdb: Moved: 95.83%
  /dev/sdb: Moved: 100.00%

Lets check what do we have now:
[root@rifle ~]# pvs
  PV         VG           Fmt  Attr PSize   PFree
  /dev/sda2  centos_rifle lvm2 a--   <7.00g      0
  /dev/sdb   vg_mydata    lvm2 a--    1.03g   1.03g
  /dev/sdc   vg_mydata    lvm2 a--  516.00m  32.00m
  /dev/sdd   vg_mydata    lvm2 a--   <2.00g 988.00m

[root@rifle ~]# pvdisplay /dev/sdb
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               vg_mydata
  PV Size               <1.04 GiB / not usable 4.70 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              264
  Free PE               264
  Allocated PE          0
  PV UUID               ycc9Uq-9yuu-fxUN-OFmK-ceKU-ZihU-bzUGWT

We have 1.03 Gb free on disk /dev/sdb and all 264 physical extents (PEs) on the disk are free.

4.Removing /dev/sdb from the volume group

[root@rifle ~]# vgreduce vg_mydata /dev/sdb
  Removed "/dev/sdb" from volume group "vg_mydata"

4.Moving data from /dev/sdc to /dev/sdd

[root@rifle ~]# pvmove /dev/sdc /dev/sdd
  /dev/sdc: Moved: 0.83%
  /dev/sdc: Moved: 36.36%
  /dev/sdc: Moved: 72.73%
  /dev/sdc: Moved: 100.00%

Just to check that all data are moved:
[root@rifle ~]# pvs
  PV         VG           Fmt  Attr PSize   PFree
  /dev/sda2  centos_rifle lvm2 a--   <7.00g      0
  /dev/sdb                lvm2 ---   <1.04g  <1.04g
  /dev/sdc   vg_mydata    lvm2 a--  516.00m 516.00m
  /dev/sdd   vg_mydata    lvm2 a--   <2.00g 504.00m

5.Removing /dev/sdc from the volume group

[root@rifle ~]# vgreduce vg_mydata /dev/sdc
  Removed "/dev/sdc" from volume group "vg_mydata"

6.Removing /dev/sdb and /dev/sdc from logical volume manager

[root@rifle ~]# pvremove /dev/sdb /dev/sdc
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc" successfully wiped.

And check what we have now:
[root@rifle ~]# pvs
  PV         VG           Fmt  Attr PSize  PFree
  /dev/sda2  centos_rifle lvm2 a--  <7.00g      0
  /dev/sdd   vg_mydata    lvm2 a--  <2.00g 504.00m

[root@rifle ~]# df -h /mydata/
Filesystem                       Size  Used Avail Use% Mounted on
/dev/mapper/vg_mydata-lv_mydata  1.5G  1.3G  153M  90% /mydata

All data migrated to the new disk without downtime.

No comments: