LVM and cloning HDs

I agree with one of the previous answers -- running dd on the whole disk is not a great solution here. If the reason you're stuck on dd has to do with backing up another OS, then you can use dd only for those other OS partitions, and rsync for the linux partitions.

Part of the problem is that if you run dd of the whole disk while you have filesystems up and mounted from it, there is no way to ensure the backup is consistent. It may work 9 times out of 10, but under any load, the resultant backup could be corrupted. The only way to ensure this doesn't happen is to unmount all filesystems and deactivate all volume groups for the entire time that the dd is running. Not very convenient if your / is mounted from LVM.

In any event, if you insist on sticking with this approach, the tool you're looking for to rename a duplicated vg is called "vgimportclone". It's intended for use with device level snapshots, but it will work with dd as well.


Closing this issue, after a year. I have written the answer in my personal website. I hope it is useful to somebody else.

I use several levels of backup, from rsync to an external disk to rsync+ZFS over a remote computer. One of the backups I do is a -from time to time- "dd" from my laptop harddisk to an external USB attached (connected only for the duration of the backup procedure). I do this, for instance, before taking my laptop for a trip.

This procedure worked well for ages. Until I started using LVM on my laptop.

If you use LVM on your computer, you copy your harddisk using dd (booting from a LiveCD) and you plug the external harddisk while the computer is running normally, later, YOU WILL CORRUPT YOUR DATA, EVEN LOSE ALL YOUR FILESYSTEM BEYOND ANY REPAIR!!!.

Why?. Because when you plug the external harddisk the OS will see the same LVM configuration in both disks and will naively assume that both disks are the same, accessible via a multipath link, so it will spread reads and writes between both disks, irredeemably corrupting BOTH. You will trash both your live data and your backup!.

This happened to me once. I lost data. I had other backups, for a few days before, so I didn't lose anything very important, but it was quite annoying and exposed a flaw in my backup strategy.

Solution: Make sure the LVM configuration in the backup disk is different. The problem is... How to do it?

I posted the question on superuser.com (Stack Exchange) but I didn't get any good answer. So I developed my own procedure and, after more than a year of battletesting it, I am posting it here and closing the original question:

  • Be sure that if something goes bad, your computer will not boot automatically. In my case, my laptop doesn't boot automatically if the power goes off and on again. Moreover, the harddisk is encrypted, so it will ask for a password.

    This is necessary because you can corrupt BOTH disks if you reboot with the backup disk attached before completing the procedure.

  • Boot from a Live CD. You can not do a "dd" from a live partition, if you expect to be able to recover your data :-).

    Something to try in the future would be to use LVM snapshots to be able to do the backup while I am using the computer, but then I have the duplicate LVM issue, that I am trying to avoid here. Another option would be to reconfigure LVM to mirror the data live to the external harddisk, and break the mirror after the sync is complete. But that sounds risky, and wouldn't backup my Windows partition or the data I don't keep in LVM volumes.

  • After booting the LiveCD, plug the external USB harddisk.

  • Login as "root" and execute vgchange -a n. This command will disable LVM in both disks. I execute the command a couple of times, to be sure.

  • Be sure /dev/sda is the source (internal harddisk) and /dev/sdb is the destination (USB external harddisk). For instance, do dd if=/dev/sdb of=/dev/null and check which harddisk LED blinks.

  • When you are sure about which disk is which, you do the copy with dd if=/dev/sda of=/dev/sdb bs=65536. With my configuration, the backup takes four hours. My internal harddisk is 500GB, and my USB is copying at 35MB/s. I do this at night, while I sleep.

    Your external USB harddisk must be, AT LEAST, as big as your internal harddisk. It is obvious, isn't it?

  • After this copy is done, you have an exact clone of your internal harddisk. The backup is done. But now you will have an issue if you ever plug this harddisk to your computer while you are working with your internal harddisk, as already described. We need to change the LVM configuration.

  • Now, after the "dd" is done, do "sync" and unplug the external UDB harddisk.

  • You execute pvchange --uuid /dev/sda*. This command will change the UUID of all the physical volumes in your internal harddisk. This command is safe even if you have partitions that are not physical volumes for LVM, because they will be automatically and safely skipped.

    The system knows which partitions are physical volumes because the partition type and because you executed "pvcreate" when you created the LVM.

  • You execute vgchange -u LVM. This command will change the UUID of the LVM volume group. My volume group is called "LVM", by the way.

  • You execute vgscan. This command will scan internal harddisk (the only currently attached) and will locate your LVM volume group there.

  • You execute vgrename LVM LVM2, to change the name of your Volume Group.

  • Now you plug your external USB harddisk.

  • You "vgscan" again, this time to locate the Volume Group in the external USB harddisk. Now you will have two Volume Groups. One called "LVM2" in your internal harddisk and other called "LVM" in your USB external harddisk.

  • Rename the Volume Group in the external USB harddisk with vgrename LVM LVM_BACKUP.

  • And rename the Volume Group in the internal harddisk back to the original name: vgrename LVM2 LVM.

  • You are done. You can review the situation with vgdisplay -v.

    You will see that Logical Volumes UUIDs are the same in both Volume Groups, but that doesn't seem to cause any problem, and I don't know how to change them.

  • Unplug your external USB harddisk, store it in a safe place, reboot your computer, eject the LiveCD and go back to business.


Studing the issue carefully, I think the right approach would be:

  1. Plug the external HD.

  2. Desactivate the LVM management of the second HD: "vgchange -a n /dev/LVM2".

  3. Clone the harddisk, with "dd".

  4. For every physical volume, generate new UUIDs for the clone disk, with "pvchange -u /dev/hdb*".

  5. Change the VG UUID with "vgchange -u LVM". This will change the UUID of one of the VG, could be the original or the clone. It doesn't matter. Being different is enough.

  6. Change the name of the VG of the cloned disk from "LVM" to "LVM2", with "vgrename". I don't know if this would work without a previous "vgscan" to "discover" the new VG.

Would this work?.