The Linux Page

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: 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%
  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: from Novell)

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


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:

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:


Syndicate content

SMS From Me Logo

SMS From Me

To automatically start one on one conversations with your online leads.



Terms of Site Index

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

  • Solaris
  • access
  • authentication
  • duplicate
  • number

    All software make use of numbers. Everything is a number. The most basic number in a computer is 0 or 1. This is called a bit. These are represented with electricity. Although in most cases we see it as 0 - Ground and 1 - Voltage (i.e. 1 volt), the bit representation in software and in hardware may be interpreted either way (i.e. a 0 could mean that the voltage is 1V and not 0V.)

    Combining these zeroes and ones we offer end users to handle much larger numbers. With 8 bits, you can have numbers from 0 to 255 (unsigned) or -128 to +127 (signed.) Now a day, computers can handle a much larger number of bits in one cycle. Most processors use 64 bits but they can calculate numbers on 128, 256, and for some 1024 bits at once. Also with parallelism, the size can be viewed as even larger (i.e. handling a 64 bit number in 1,536 threads like on my old nVidra Quadro 600 is equivalent to one large number of 98,304 bits! That would be 2 power 98,304 possibilitie or about 2.8359e+29592 in decimal.)

    Integers are easy to handle. Although when working on math problems you generally see the set of avaialble numbers as equivalent to N although mathematicians know that computers can really only handle a limited set of numbers. For example, on a 64 bit computer, the usual range is -9223372036854775808 to 9223372036854775807, This is generally enough although at times some equations have to be reworked to avoid really large or small intermediate numbers that work fine in math equations, but not so well on computers.

    Now, math also includes other sets of numbers such as D, R, and C. Computers do not offer any way to represent numbers in R or C but they can offer D to some extend. These numbers are called floating point numbers because we do math using an exponent. The exponent makes the decimal point "float" in any location as the number used for the exponent offers. Using a 64 bit floating point, you can have positive and negative numbers with precision varing betwee 10-308 and 10+308. This includes a positive zero (+0) and a negative zero (-0), which is import in a few cases (although +0 = -0 is true, you can get the sign of a number and distinguish both zeroes). Note that at first decimal numbers were going to also have a positive and negative zero, but it was instead decided to have one more negative number (remember, with 8 bits we have signed numbers from -128 to +127, this is because in the positive numbers we have a 0 which we don't have in the negative numbers.)