Add new hard drive to mirror existing LVM drive with raid1
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.
Solution 1:
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/newdrive
or usesfdisk -d /dev/sda | sfdisk /dev/sdb
assuming sdb is your new drive) - initialise new partition(s) as lvm2 pv(s) (
pvcreate /dev/newpartition
) - 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 available
then 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 (resize2fs
)).
Solution 2:
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
source: http://ubuntuforums.org/showthread.php?t=1703904
http://www.tek-tips.com/viewthread.cfm?qid=1318503&page=1