How to migrate from LVM Partitions to New Logical Volume

LVM Migration

LVM migration allows you to migrate the logical volumes to a new disk without the any data loss and downtime. The need of this feature is it to move your data from old disk to a new disk. We usually do migrations from one disk to other disk storage only when error occurs in disks.

Features of Migration

  • Migrate disks without data loss and downtime.
  • We can use any type of disk like SATA, SSD, SAS, SAN storage iSCSI or FC.
  • Moving logical volumes from one disk to other disk.
  • In LVM Migration, we will swap every volumes, file-system and it’ s data in the existing storage.

In order to upgrade your server with SSD Hard-drive you need not reformat the server. The LVM provides you with the option to migrate the old SATA Drives with new SSD Drives. The migration supports any kind of disks such as local drive, SAN or Fiber channel. There are two possible ways to migrate LVM partitions, one is using Mirroring method and the other using pvmove command.

To Check the Present Drives

Assuming that we already have one virtual drive named vdb that mapped to one of the logical volume lv1. To migrate this vdb logical volume drive to some other storage, first you need to check the virtual drive and logical volume names by using fdisk and lvs commands

[root@linuxhelp ~]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b67c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     4810751     2097152   82  Linux swap / Solaris
/dev/sda3         4810752    35530751    15360000   83  Linux
/dev/sda4        35530752    62914559    13691904    5  Extended
/dev/sda5        35532800    56504319    10485760   8e  Linux LVM
Disk /dev/mapper/vg1-lv1: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@linuxhelp logical]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  lv1  vg1  -wi-ao---- 8.00g

To Check Newly added Drive

Now attach the new SSD drive to system and check the newly added drive using the fdisk command.

[root@linuxhelp ~]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b67c7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     4810751     2097152   82  Linux swap / Solaris
/dev/sda3         4810752    35530751    15360000   83  Linux
/dev/sda4        35530752    62914559    13691904    5  Extended
/dev/sda5        35532800    56504319    10485760   8e  Linux LVM

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/vg1-lv1: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Important: The new drive “ /dev/sdb“ has been added successfully .

To Check Present Logical and Physical Volume

Now we are going to create physical volume, volume group and logical volume for migration. Before moving to the creation of volumes, check the present logical volume data under /mnt/lvm mount point. Now use the following commands to list the mounts

[root@linuxhelp ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda3             15G  3.8G   11G  26% /
devtmpfs             486M     0  486M   0% /dev
tmpfs                494M  140K  494M   1% /dev/shm
tmpfs                494M  7.1M  487M   2% /run
tmpfs                494M     0  494M   0% /sys/fs/cgroup
/dev/sda1            297M  106M  192M  36% /boot
/dev/sr0             3.9G  3.9G     0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg1-lv1  7.8G   36M  7.3G   1% /mnt/logical
[root@linuxhelp ~]# cd /mnt/logical/
[root@linuxhelp logical]# cat file1.txt 
hello

Welcome To Linux Help !!!!!


Confirm the names of logical volume and volume group and also confirm which physical volume is used to hold them.

[root@linuxhelp logical]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  lv1  vg1  -wi-ao---- 8.00g   


To Create New Physical Volume

First define the partition using fdisk and do not forget to change the Type to LVM(8e), while you are creating partitions.

[root@linuxhelp ~]# pvcreate /dev/sdb1 -v
    Set up physical volume for " /dev/sdb1"  with 20971520 available sectors
    Zeroing start of device /dev/sdb1
    Writing physical volume data to disk " /dev/sdb1" 
  Physical volume " /dev/sdb1"  successfully created


Now add the newly created physical volume to existing volume group vg1 using vgextend command

[root@linuxhelp ~]# vgextend vg1 /dev/sdb1
  Volume group " vg1"  successfully extended


To display the full list of information concerning volume group use vgdisplay command.

[root@linuxhelp ~]# vgdisplay vg1 -v
    Using volume group(s) on command line
    Finding volume group " vg1" 
  --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       2048 / 8.00 GiB
  Free  PE / Size       3070 / 11.99 GiB
  VG UUID               aSuYgU-T4dv-tH5C-dKe6-lgNJ-WWa1-NPgF1k
   
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                IPBCS6-Iokv-9eJR-5z9X-4umT-fGJ7-r5kXXF
  LV Write Access        read/write
  LV Creation host, time linuxhelp, 2016-04-25 14:46:51 +0530
  LV Status              available
  # open                 1
  LV Size                8.00 GiB
  Current LE             2048
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Physical volumes ---
  PV Name               /dev/sda5     
  PV UUID               2UUFfq-5bOB-Mf7q-oLTB-X5Hf-STQ3-b4dv7Z
  PV Status             allocatable
  Total PE / Free PE    2559 / 511
   
  PV Name               /dev/sdb1     
  PV UUID               qpBHZz-lfVA-LgrE-E4gZ-DWmg-8RJe-NHc0x7
  PV Status             allocatable
  Total PE / Free PE    2559 / 2559


Important: In the above screen, we can see PV has been added to the volume group.

To know more information about which devices are mapped, use the dmsetup dependency command



[root@linuxhelp ~]# dmsetup deps /dev/vg1/lv1
1 dependencies : (8, 5)


LVM Mirroring Procedure

In order to migrate from old logical volume to new drive using Mirroring method, use lvconvert command to migrate data

[root@linuxhelp ~]# lvconvert -m 1 /dev/vg1/lv1 /dev/sdb1
-m = mirror
1 = adding a single mirror


The migration process will take time according to your volume size.

After migration process completed, check the converted mirror.

[root@linuxhelp ~]# lvs -o+devices
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert Devices
lv1 vg1 rwi-aor--- 8.00g 100.00 lv1_rimage_0(0),lv1_rimage_1(0)


After you review the converted mirror, remove the old virtual disk vdb1. The option -m is used to remove the mirror.

[root@linuxhelp ~]# lvconvert -m 0 /dev/vg1/lv1 /dev/sdb1
Logical volume lv1 converted


After the old virtual disk is removed, you can check the devices for logical volumes using following command.

[root@linuxhelp ~]# lvs -o+devices
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert Devices
lv1 vg1 -wi-ao---- 8.00g /dev/sdb1(1)
[root@linuxhelp ~]# dmsetup deps /dev/vg1/lv1
1 dependencies : (8, 17) 


Now check the files that you have migrated from old drive to new drive. If the same information is present in the new drive then it means that we have done all steps perfectly.

[root@linuxhelp ~]# cd /mnt/logical/
[root@linuxhelp logical]# ls
file1.txt lost+found
[root@linuxhelp logical]# cat file1.txt
hello

Welcome To Linux Help !!!!!


Next delete the vdb1 from volume group and confirm the devices that depends on volume group.


[root@linuxhelp logical]# vgreduce /dev/vg1 /dev/sda5
Removed " /dev/sda5"  from volume group " vg1" 
[root@linuxhelp logical]# vgs -o+devices
VG #PV #LV #SN Attr VSize VFree Devices
vg1 1 1 0 wz--n- 10.00g 2.00g /dev/sdb1(1)


After removing vdb1 from volume group the logical volume is present because we have migrated it to sda1 from vdb1.

[root@linuxhelp logical]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv1 vg1 -wi-ao---- 8.00g

LVM pvmove Mirroring Procedure

pvmove command with option -n is used to mirror data between two devices.

[root@linuxhelp logical]# pvmove -n /dev/vg1/lv1 /dev/sdb1 /dev/sda5


Mirroring is actually used more often than pvmove.

FAQ
Q
How to reduce or shrink the size of the LVM partition?
A
Umount the filesystem by using "umount" command,
use resize2fs command , e.g resiz2fs /dev/mapper/myvg-mylv 10G
Q
What is the maximum size of a single LV?
A
LV size is 2TB. For some older kernels, however, the limit was 1TB due to signedness problems in the block layer.
For 32-bit CPUs on 2.6 kernels, the maximum LV size is 16TB.
For 64-bit CPUs on 2.6 kernels, the maximum LV size is 8EB.
Q
How to mirror between two devices?
A
You can use this "pvmove" command to mirror bet
Say for example "pvmove -n /dev/vg1/lv1 /dev/sdb1 /dev/sda5" So pv move followed by two partitions
Q
How does LVM mirror differ from normal LVM?
A
LVM migration allows you to migrate the logical volumes to a new disk without any data loss and downtime.
Q
How to mirror LVM partition?
A
By using this "lvconvert" command you can mirror the LVM partition.