LVM: Reusing physical volumes from another machine

Here I wish to demo how to dismantle LVM setup on one machine and reassemble it on another without losing filesystem data stored on the Physical Volumes.

This may be useful in places where block devices can be unmounted from one machine and remounted on another, say to recover from hardware failure on the first machine. (I plan to use it on Amazon AWS' EBS volumes)

The key is to use the -'-zero n' option when reassembling Physical Volumes in lvcreate, and to use the same --size option as the done for the previous LVM setup.

A Volume Group stores its metadata in the Physical Volumes, and hence if you manage to reassemble all Physical Volumes of a Volume Group, you get the original Volume Group back. (Per responses of 'comps' on Freenode's #lvm channel, and confirmed by the testing below)

Now it is up to the Logical Volume creator to carve out volumes of the same size and from the same locations of this Volume Group.

# Legend:
# LVM = Logical Volume Manager 2
# PV = Physical Volume
# VG = Volume Group
# LV = Logical Volume
# FS = FileSystem

# Make a temp directory to play in
mkdir ~/lvm_test
cd ~/lvm_test

MINOR_NUMBER_START=100
MAX_DEVICES=8
for (( i = $MINOR_NUMBER_START ;
 i < $MINOR_NUMBER_START+$MAX_DEVICES ;
 ++i )); do
 # Make a few empty files
 dd if=/dev/zero of=dev$i bs=1024 count=20480

 # Make a few loop devices
 sudo mknod -m660 lo$i b 7 $i
 sudo losetup lo$i dev$i

 # Create LVM PVs using these loop devices
 sudo pvcreate lo$i
done

# Create a VG over all the PVs, and name it vg1
sudo vgcreate vg1 lo*

# Create a LV from the VG, and name it lv1
sudo lvcreate --name lv1 vg1 --size 100M

# Make EXT3 FS on LV with name lv1
sudo mkfs.ext3 `sudo lvdisplay -c vg1/lv1 | cut -d : -f 1`

# Make a directory to act as a mount point
mkdir dir

# Mount the LV on the directory
sudo mount `sudo lvdisplay -c vg1/lv1 | cut -d : -f 1` dir

# Create a 20 MB file filled with random data on this new FS
sudo dd if=/dev/urandom of=dir/dd.out bs=1024 count=20480

# Make a backup of the file
cp dir/dd.out ./dd.out


# Now we perform the test where we remove all the LVs and PVs from the VG and
# the VG itself, and then use the PVs to create a new VG and LV that can be
# mounted with the filesystem intact.

# Teardown the FS, LV and VG, but DONT remove the PVs
sudo umount dir
sudo lvchange --available n vg1/lv1
sudo lvremove vg1/lv1
sudo vgremove vg1

# Create a new VG using existing PVs, and name it vg_new
sudo vgcreate vg_new lo*

# Create a new LV on this VG, but DONT zero the first 1kB block; In essence,
# retain the data on Physical Volumes.
# Make sure to use the same size as previous LV (lv1), else mounting of EXT3 FS
# won't work.
sudo lvcreate --zero n --name lv_new vg_new --size 100M

# Mount the LV on the directory; Note that we did not format.mkfs on the LV.
sudo mount `sudo lvdisplay -c vg_new/lv_new | cut -d : -f 1` dir

# Compare the backed-up copy of del.dd with the one just mounted from the new
# Logical Volume
sudo diff del.dd dir/del.dd

# 
# Teardown
sudo umount dir
sudo lvchange --available n vg_new/lv_new
sudo lvremove vg_new/lv_new
sudo vgremove vg_new

for (( i = $MINOR_NUMBER_START ;
 i < $MINOR_NUMBER_START+$MAX_DEVICES ;
 ++i )); do
 sudo pvremove lo$i
 sudo losetup -d lo$i
done

rm -rf ~/lvm_test

unset MINOR_NUMBER_START
unset MAX_DEVICES

No comments:

Post a Comment