I have a 320GB hard drive with an LVM2 volume group that was created as part of the default installation. If I put a second 320GB hard drive into the computer, can I make the second drive mirror the first using Linux software RAID1 or something similar? I tried adding a new RAID array in the Gnome Disk Utility, but it only gave me the option of creating an array with two empty partitions.
If anyone is still reading this older question, note that raid1 is now built-in to lvm2. To mirror an existing lv on a new drive using lvm2:
- install the new drive and create lvm2 partition(s) (at least the same size as your existing drive's lvm partition(s), or those you want to mirror in any case) (use
parted /dev/newdriveor use
sfdisk -d /dev/sda | sfdisk /dev/sdbassuming sdb is your new drive)
- initialise new partition(s) as lvm2 pv(s) (
- extend your existing lvm2 vg(s) to the new partition(s) (
vgextend /dev/myvg /dev/newpartition) and then first check and confirm that the number of extents on the new drive is the same or more (
pvdisplay) Free PE on new drive should be more than Total PE of the old drive.
for each lv that you want to mirror on the new partition:
lvconvert -m1 /dev/myvg/existinglv /dev/newpartition
(ie, convert my existing (linear) lv to a raid1 mirrored lv with 1 additional mirror leg, and allocate the new PEs required for the mirror leg on the given new pv/partition)
note that you will need some additional space on the existing and new lv, because lvm2 raid1 mirror segments (the default) store their logs on the same pv/segment as the mirror leg (you will need an additional 1 PE per mirror leg on both the existing and new pv).
If you get an error like
1 extents needed, but only 0 availablethen it might indicate a problem creating the log, and you might need to create some free space on the existing pv, possibly by shrinking the relevant lv to allow room (
lvreduce). If you do this, you might need to resize the underlying filesystem beforehand (
Firstly, you should backup your data just in case.
Also, make sure you read the steps carefully and know exactly what you're doing. I have never done this before, I just extracted the steps from some forum posts.
- With fdisk, create a new partition of type fd on your new drive, sdb.
- Create a degraded RAID1 array:
mdadm -C /dev/md0 --force --level=1 --raid-devices=2 /dev/sdb1 missing
- Create a file system on /dev/md0:
mkfs -t ext4 /dev/md0
- Now, /dev/md0 is currently a degraded raid1 with two drives, but one is missing.
- do a full copy from sda1 to md0,
dd if=/dev/sda1 of=/dev/md0
- Mount /dev/md0 and inspect.
Ok, at this point you should have a raid1 with all the correct information. Now you should configure your grub2 configuration, fstab, etc. Make sure you have all the correct kernel modules etc. Once this is done, turn off the computer, and physically disconnect drive0 (sda). Now, get the system up and running with md0 alone.
Once you have the computer fully operational without drive0 connected, turn it off again, and reattach sda.
- With fdisk, repartition /dev/sda with a partition of type fd.
- Add /dev/sda1 to your existing RAID1 array:
mdadm /dev/md0 --add /dev/sda1
- Done!. The array will start to 'rebuild' and you can check the status with
mdadm -D /dev/md0
I achieved this by backing up all of my data onto an external drive, reinstalling Ubuntu onto a raid array, and then restoring my backup in the new file system. In particular, I followed these steps:
Backup data using instructions at this forum thread using command
sudo tar cvpzf backup.tgz --exclude=/proc --exclude=/lost+found --exclude=backup.tgz --exclude=/mnt --exclude=/sys --exclude=/dev --exclude=/etc/fstab --exclude=/etc/mtab --exclude=/boot --exclude=/media /
replacing backup.tgz in both cases with the location of the backup
Turn computer off and install two hard drives with the smaller one being at least the size of the raid array (possibly including the original hard drive)
Boot into install disk and run the installer.
Using manual partitioning, make a partition in each hard drive for each partition in the final file system. For example, if you want your
/homepartition to be 100 GB, make a 100GB partition in each hard drive. Set all partitions that will be part of a RAID array to RAID autodetect (0xfd).
Choose to make raid arrays, and put each partition into a raid array with the corresponding partition on the other disk.
Finish the installation as normal and reboot into the fresh Ubuntu installation.
Mount the external drive if it is not already mounted and restore the backup as described in the forum thread above using the command
sudo tar xvpfz backup.tgz -C /
sudo update-gruband then reboot. The new operating system should now be exactly like the original.
I prefer this method for a few reasons:
Making file system level copies instead of disk level copies seems preferable in general
It does not depend on keeping the partition structure the same
Once I figured out how to do it, the entire process took less time than the disk level copy took
It allows the installer to create the correct entries for boot devices and mounted devices, which need to change because raid partitions are referred to differently than regular partitions are.