Syndicate content

Creating a drive mirror with LVM, including exact partition cloning

LVM today can create a mirror of your data without having the need for mdraid (or hardware raid).

Problem is, I did not find any documentation that makes sense and there doesn't seem to be a graphical interface that can help you with that (at least not under Ubuntu Gnome.)

So... I decided to spend the time because that's probably less time anyway than trying to create and maintain and md device.

The fact is, it is rather simple, but requires quite a bit of knowledge.

Copying the Partitions Exactly

First, I wanted the partitions to be exactly the same and using fdisk did not work. It did not want to create all the partitions saying that some were overlapping others. I had to use sfdisk instead. There are the 3 commands you need:

sfdisk -d /dev/sda >partitions.txt
vim partitions.txt
sfdisk /dev/sdb <partitions.txt

The vim in the middle is used to edit the file and replace all /dev/sda instances with /dev/sdb (obviously, you must map that to your hard drives!) You could instead use an sed command like this:

sed -e 's/sda/sdb/' sda-partitions.txt >sdb-partitions.txt

Notice that the file names are different so your sfdisk would need to use those other filenames.

(Source: https://www.sharktooth.de/doku.php/linux:clone_disc_partition on a German website, but written in English)

The following is a sample partition that was converted from /dev/sda to /dev/sdb (i.e. this is AFTER the vim edit):

# partition table of /dev/sdb
unit: sectors

/dev/sdb1 : start=     2048, size=   497664, Id=83, bootable
/dev/sdb2 : start=   501758, size=3906527234, Id= 5
/dev/sdb3 : start=        0, size=        0, Id= 0
/dev/sdb4 : start=        0, size=        0, Id= 0
/dev/sdb5 : start=   501760, size=3906527232, Id=8e

Once I had the correct partitions, off I went to create a mirror of my 1 LVM partition.

Adding the New Drive as a LVM Mirror (-m1)

Now that we have a /dev/sdb5 partition which is an exact duplicate of our /dev/sda5 partition (parametrically) we can add it to the array as a mirror.

First check what you have:

lvm> pvscan
  PV /dev/sda5   VG isabelle   lvm2 [1.82 TiB / 132.00 MiB free]
  Total: 1 [1.82 TiB] / in use: 1 [1.82 TiB] / in no VG: 0 [0   ]

Assuming you just installed a system and used the default Ubuntu setup, you have one virtual group on /dev/sda5 that looks like this (drive sizes will vary though.) Here we see that the name of the virtual group is "isabelle".

We want to add the /dev/sdb5 physical device to that group. First create the physical device within LVM with:

lvm> pvcreate /dev/sdb5
  Writing physical volume data to disk "/dev/sdb5"
  Physical volume "/dev/sdb5" successfully created

This adds the partition /dev/sdb5 to the LVM array. We can do a pvscan again and see that the new physical drive exists in the LVM system:

lvm> pvscan
  PV /dev/sda5   VG isabelle        lvm2 [1.82 TiB / 132.00 MiB free]
  PV /dev/sdb5                      lvm2 [1.82 TiB]
  Total: 2 [3.64 TiB] / in use: 1 [1.82 TiB] / in no VG: 1 [1.82 TiB]

We notice that /dev/sdb5 is not part of any "VG" (virtual group). We use the vgextend command to add it to the existing VG:

lvm> vgextend isabelle /dev/sdb5
  Volume group "isabelle" successfully extended

Obviously, you want to change the name "isabelle" with the name of your VG. With Ubuntu they set it up to the name of your computer. To make sure it was properly added, we can use pvscan again:

lvm> pvscan
  PV /dev/sda5   VG isabelle   lvm2 [1.82 TiB / 132.00 MiB free]
  PV /dev/sdb5   VG isabelle   lvm2 [1.82 TiB / 1.82 TiB free]
  Total: 2 [3.64 TiB] / in use: 2 [3.64 TiB] / in no VG: 0 [0   ]

As we can see the new partition adds a lot of free space (1.82Tb free). That will be used as we create the mirror.

lvm> lvconvert -m1 /dev/isabelle/root
  isabelle/root: Converted: 0.0%
  [...snip...]
  isabelle/root: Converted: 100.0%

As proposed in the comment by Brent below, adding a few parameters may help in not having to wait while rebuilding the mirrors:

lvm>  lvconvert -m 1 --mirrorlog mirrored --alloc anywhere /dev/isabelle/root

Here we convert the virtual group into a mirror level 1 group (i.e. RAID1). which has the effect of duplicating the hard drives immediately—which is a problem when the partitions are nearly 2Tb! Probably about 16 hours 2 days of work! (might be because the kids were playing on the computer while it was making the copy though.) md is better here... it does that in the background!

I don't have the final details yet, but one can use the lvs command with options as below to see that the mirror is indeed in place and what drives participate.

lvm> lvs -a -o +devices
  LV     VG       Attr     LSize  Pool Origin Data%  Move Log Copy%  Convert Devices          
  root   isabelle -wi-ao--  1.80t                                            /dev/sda5(0)     
  swap_1 isabelle -wi-ao-- 15.97g                                            /dev/sda5(472748)

The final output once you have a mirror looks like this:

lvm> lvs -a -o +devices
  LV              VG       Attr     LSize  Pool Origin Data%  Move Log       Copy%  Convert Devices                          
  root            isabelle mwi-aom-  1.80t                         root_mlog 100.00         root_mimage_0(0),root_mimage_1(0)
  [root_mimage_0] isabelle iwi-aom-  1.80t                                                  /dev/sda5(0)                     
  [root_mimage_1] isabelle iwi-aom-  1.80t                                                  /dev/sdb5(0)                     
  [root_mlog]     isabelle lwi-aom-  4.00m                                                  /dev/sda5(476837)                
  swap_1          isabelle -wi-ao-- 15.97g                                                  /dev/sda5(472748)                

Note that the size of swap_1 is a 1 to 1 map of the memory (these computers have a total of 16Gb of RAM.)

(Source: http://www.novell.com/support/kb/doc.php?id=7009321 from Novell)

Hopefully you understand what you've just done... so you can repeat it for each virtual partition.

Warning

One thing to keep in mind: the LVM mirror does NOT mirror your /boot partition automatically. This being said, you won't lose your data, but you won't be able to reboot unless you correctly duplicate everything. This requires the /boot and grub proper installation which is not yet covered here.

Resizing an LVM partition

This is annoying! You need to unmount a partition to resize it. That's not good, is it!? I thought that LVM would work without such drawback. The problem being that you cannot resize your root partition unless you boot on a CD or in another operating system (i.e. another Ubuntu install that boot with another partition.)

This being said, LVM still has advantages. Anyway, since I could not resize my root partition, I decided to repeat my duplication the same way as the first time. So it's going to take another 2 days. 8-P

For more info on resizing, check this page out: http://blog.shadypixel.com/how-to-shrink-an-lvm-volume-safely/

Re: Creating a drive mirror with LVM, including exact ...

Thank you for this! Very helpful. It seems that much of the advice out there is still to use the old Linux madam raid, but why do that when LVM can handle it with way more flexibility?

One comment: you might want to add a few extra arguments to the lvconvert command, to guard against unwieldy wait times after a system crash, as the mirrors rebuild:

lvconvert -m 1 --mirrorlog mirrored --alloc anywhere /dev/isabelle/root

I found that tidbit here: http://www.sandelman.ca/mcr/blog/lvm/

Cheers

Syndicate content

Diverse Realty

Diverse Realty Team

Want a New Home?
Want to Sell Your House?

Call Alex at
+1 (916)
220 6482

Alexis Wilke, Realtor
Salesperson
Lic. # 02024063

Cory Marcus, Broker
Lic. # 01079165

     

Terms of Site Index

Find the page/content you are looking for with our index.