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 security for data losing. I used two disks and afterwards I migrated the data from the old home to the mirror.

I encourage recommend people do this, because is easier than it seems, and will not take more than ten minutes.

Before doing anything , it is needed download lvm2 package, and that is easy with Debian:

hlab:# aptitude install lvm2

Now it should be prepared two hard drives with exactly the same partition table, so here is the way :

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

  2. 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

  3. Volumes groups

  4. 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
    -A y indicates we want to use autobackup functionality.
    As usual in the above examples , you can list the result with vgdisplay

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

  5. Logical volumes

  6. Now is the time for setting the mirror. Here is the command

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

    -m1: indicates mirror
    –nosyn: if there is data, no sync.
    -L: size of partition
    –mirror-log core:But default you need a third partition for keeping the logs, so instead of creating another partition, I decide to do not do it.
    -n: name of the the logical volume.
    vols: name of the volume group.

    At this point is possible to show the created mirror, so here is the command:

    hlab:# lvs -a -o +devices
    LV VG Attr LSize Origin Snap% Move Log Copy% Convert Devices
    home vols mwi-ao 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”:

    m: mirror, if the letter was capitalized it would mean that was used the option –nosync. In this case I had not any data.
    w: indicates the volume is writable. If it appears a “r” the volume is read-only.
    ao: Partition is actived(a). Device is open(o).

  7. Data migration

  8. Well, this is the last point, and know only is needed to copy the home data and add a line to /etc/fstab for getting the mirror working out. 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 -)

    In resume, having a mirror with LVM is cheap and pretty straightforward. Besides you never know when Murphy’s law can act, so it is better being one step ahead.

0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment