2011-10-08

Migrating logical volumes between volume groups

Some time ago I added a second hard disk to my machine and I joined it to the root volume group. I've also created a logical volume with a /data filesystem on it.
Now I need to move that data disk to a new machine, but as long as it's part of the root volume group I can't easily move it without making the whole machine unbootable.
These are the steps on a Red Hat 5.4 machine:
First, make sure that the disk you want to migrate does not contains logical volumes that are spanned on other disks also: I checked it with the system-config-lvm GUI, that shows how logical volumes are mapped to physical volumes, and I saw that my logical volume lv_data was entirely on /dev/sdb.

This is the initial fstab with the /data mounted from VolGroup00

/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0
/dev/VolGroup00/lv_data         /data           ext3    defaults        1 2

first, unmount the /data filesystem as this whole operation can't be done online.

[root@testlvm /]#umount /data

deactivate the logical volume

[root@testlvm /]#lvchange -an /dev/VolGroup00/lv_data

now split the root volume group creating a new VG_data

[root@testlvm /]# vgsplit VolGroup00 VG_data /dev/sdb
  New volume group "VG_data" successfully split from "VolGroup00"

you can see that the logical volume lv_data now belongs to the newly created volume group.
if you get an error, read at the bottom of this post.

[root@testlvm /]# vgdisplay -v
    Finding all volume groups
    Finding volume group "VG_data"
  --- Volume group ---
  VG Name               VG_data
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               1.97 GB
  PE Size               32.00 MB
  Total PE              63
  Alloc PE / Size       63 / 1.97 GB
  Free  PE / Size       0 / 0
  VG UUID               67mnJx-WyyP-7XQz-D1na-CNhr-rjUJ-79V8Fu


  --- Logical volume ---
  LV Name                /dev/VG_data/lv_data
  VG Name                VG_data
  LV UUID                lcwb7r-n0YZ-lslK-HoqE-VL6V-IMNl-rIY6N0
  LV Write Access        read/write
  LV Status              NOT available
  LV Size                1.97 GB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto


  --- Physical volumes ---
  PV Name               /dev/sdb
  PV UUID               uwiyVE-Hkvt-dG3c-x1Ak-KBng-T91O-5Xuz2l
  PV Status             allocatable
  Total PE / Free PE    63 / 0


    Finding volume group "VolGroup00"
  --- Volume group ---
  VG Name               VolGroup00
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.88 GB
  PE Size               32.00 MB
  Total PE              316
  Alloc PE / Size       316 / 9.88 GB
  Free  PE / Size       0 / 0
  VG UUID               CJqD4r-kRQM-Cj9e-XwOr-6dn8-TkPH-MfTTyZ


  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                d4iToX-8m0c-jf9s-sMMN-VDpO-brE8-4kWhf2
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                7.91 GB
  Current LE             253
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0


  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                kEzo7d-AHuv-xiPD-3Qbo-v8yY-6w1D-DFMIwN
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.97 GB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1


  --- Physical volumes ---
  PV Name               /dev/sda2
  PV UUID               7bPvQd-H8yE-v00N-asPT-WQ92-6oCS-bBh6nj
  PV Status             allocatable
  Total PE / Free PE    316 / 0

bring back online the logical volume

[root@testlvm /]#lvchange -ay /dev/VG_data/lv_data

note that you can't use TAB to complete the path of the logical volume, because the dev node won't be created until you activate it, so just type it as it is.

edit /etc/fstab with new new mount point


/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0
/dev/VG_data/lv_data            /data           ext3    defaults        1 2

and finally mount the /data filesystem from his new home

[root@testlvm /]#mount /data

now you can safely umount it, export the volume group and reimport it on another machine.

Note that as of Red Hat 5.4 you may get a an error on the vgsplit command if the target volume group already exists and has active logical volumes.

  Logical volumes in "VolGroup00" must be inactive


The error message is somewhat misleading: you'll get it even if the logical volumes you are migrating are already inactive.

Workaround: just make it in 2 steps: first split into a new "temp" volume group, then vgmerge the "temp" volume group into the target volume group.

2 comments:

  1. What if the server you want to get the LV off is not accessible?
    Thanks!

    ReplyDelete
    Replies
    1. You'll have to connect the disk from the old server to a new one

      Delete