LVM = Mirroring /home

I usually have my home directory in a different partition.Last week I had to reinstall my Debian, so I decided to add a little bit of redundance in order to prevent data losing. I had two disks, so I decided to migrate the data from the old home to the mirror.

I would recommend people trying this, because is easier than it seems, and will not take longer than ten minutes.

Before doing anything , it is needed download lvm2 package, if you work with Debian or a system based on:

hlab:# aptitude install lvm2
hlab:# sfdisk -d /dev/sda | sfdisk /dev/sdb

This will make the second disk’s partition table /dev/sdb identical to the the first hard drive /dev/sdb.

hlab:/mnt/vdata# sfdisk -d /dev/sda
Warning: extended partition does not start at a cylinder boundary.
DOS and Linux will interpret the contents differently.
# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=       63, size=976768002, Id= 5
/dev/sda2 : start=        0, size=        0, Id= 0
/dev/sda3 : start=        0, size=        0, Id= 0
/dev/sda4 : start=        0, size=        0, Id= 0
/dev/sda5 : start=      126, size=238275954, Id=83
/dev/sda6 : start=238276143, size=738491922, Id=83

1.Physical volumes

The first thing is to add physical volumes , and by physical I mean, either partitions or whole disks, here is the way:

hlab:# pvcreate /dev/sda5 /dev/sdb6
It's suitable to check the results:
hlab:# pvdisplay

--- Physical volume ---
PV Name               /dev/sda5
VG Name               volhome
PV Size               113.62 GB / not usable 1.68 MB
Allocatable           yes
PE Size (KByte)       4096
Total PE              29086
Free PE               926
Allocated PE          28160
PV UUID               1ZPA7A-gXwn-O0G6-my3b-PUVt-mk9o-0A4sW1

--- Physical volume ---
PV Name               /dev/sdc5
VG Name               volhome
PV Size               113.62 GB / not usable 1.68 MB
Allocatable           yes
PE Size (KByte)       4096
Total PE              29086
Free PE               926
Allocated PE          28160
PV UUID               0DfPHc-tXBn-dcYR-yKag-EL7j-RI4A-K9Eo0j

2. Volumes groups

The above step is just a conventionality ( I see it like that) for telling that you have available some partitions or disks. Now you can create some volumes, which will appear in /dev/volname just take a look to the command:

hlab:# vgcreate -A y vols /dev/sda5 /dev/sdb5 

If you are familiarized with ZFS, at this point we have like zfs pool.

3. Logical volumes Now is the time for setting the mirror. Here is the command

hlab:# lvcreate -m1 (--nosync)  -L 110G --mirror-log core  -n home  vols 

But default you need a third partition for keeping the logs, so instead of creating another partition, I omitted it.

At this point is possible to show the mirror:

hlab:# lvs -a -o +devices
LV              VG      Attr   LSize   Origin Snap%  Move Log Copy%  Convert Devices
home            vols <b>mwi-ao</b> 110.00G                        100.00         home_mimage_0(0),home_mimage_1(0)
[home_mimage_0] vols iwi-ao 110.00G                          /dev/sda5(0)
[home_mimage_1] vols iwi-ao 110.00G
/dev/sdc5(0)

The interesting part here, is the meaning of the attributes, you can make it out by means of “man”:

4. Data migration

The last point is to copy the home data and add a line to /etc/fstab . Instead of using a command such as cp -R I prefer using tar, because is cleaner and will copy absolutely all the files, here is the command:

First move your old home

hlab:#  mv /home /home_tmp
Second mount the partition 
/etc/fstab
/dev/vols/home /home         auto    rw
hlab:#  mount -a; cd /home_tmp
hlab:#  tar cvf - . (cd /home; tar xvf -)

Summing up, to have a mirror with LVM is cheap and pretty straightforward. However, LVM is just for Volumes, I guess the best way it would be using RAID linux (mdadm) and after you can use LVM over the raid.