Common RAID and LVM RAID1 Setup

No Gravatar

My last RAID discussion was about growing the size of an existing RAID1 partition. I thought I’d back up a little bit and show an example of how we get a RAID1 in the first place.

The debian installer is a bit strange, I prefer to do things CLI. There’s one gotcha that the installer doesn’t handle: If you want grub2 to properly install with all these RAID partitions, don’t fill the first 63 sectors. Leave the first 63 sectors as unpartitioned space. Grub can then install to the MBR. This is because Grub can’t embed itself in a RAID partition. It can boot from a RAID partition, just not embed itself inside it. If you’re getting an error during the grub install step that it can’t embed ( /usr/sbin/grub-setup: error: embedding is not possible ) , this is why. Either make your first partition an ext3 (/boot is a good candidate), or just leave that space blank.

I prefer to not use the installer to setup my partitions. CFdisk is easy. I use system rescue cd to setup partitions with CLI tools, then boot the debian installer. Easier to keep straight in my head that way. http://www.sysresccd.org

Boot the live cd, drop to a terminal. I’m gonna assume you only have two drives, and want to put the whole thing into a RAID1. Use fdisk to make the following partitions:

Swap: usually 1xram size (because we’ve got two  swap partitions), stick it at the end of the available space
boot: make a new partition, set the type as RAID autodetect, perhaps 250mb or so, could be a lot smaller. Stick it at the beginning of the space starting at sector 64. Make this partition bootable.
RAID: just make a new partition, I just fill the remainder of the available space, set the type as RAID Autodetect

Now save your new partition scheme. Reboot if it asks you to, sometimes the kernel doesn’t get the update for whatever reason. Now copy that partition scheme over to the other drive:

sfdisk -d /dev/sda > ~/sda.table
sfdisk /dev/sdb < ~/sda.table

Now comes the RAID part (note: you need mdadm and lvm2, if your livecd doesn’t include them, figure that out first or use the terminal from the debian installer):
Lets assume you ended up with this:
/dev/sda1 = your RAID partition
/dev/sda2 = your boot partition
/dev/sdb1 = your RAID partition
/dev/sdb2 = your boot partition

mdadm --create /dev/md0 --chunk=4 --level=0 --raid-devices=2 /dev/sda2 /dev/sdb2
mdadm --create /dev/md1 --chunk=4 --level=0 --raid-devices=2 /dev/sda1 /dev/sdb1

Now for the LVM. I stick an LVM group/volume on each RAIDpartition I make, for the future flexibility. There’s no reason not to, and if you need it in the future, you’ve got it.

pvcreate /dev/md0 # initialize the raid device for future lvm use
pvcreate /dev/md1 
vgcreate -s 64M files_vg /dev/md0  #create a volume group to put volumes in.
                                    # -s 64M means you're limited to 4TB,
                                    # but you can store an entire 4gb
                                    # dvd on it in one piece.
vgcreate boot_vg /dev/md1
lvcreate -nfiles_lv files_vg      #create a volume on the group. I'm gonna
                                  #fill it right up so not specifying size.
lvcreate -n boot_lv boot_vg
mkfs.ext3 /dev/files_vg/files_lv #make the file system. A nice side effect
                                  #of lvm is you get to name your devices.
mkfs.ext3 /dev/boot_vg/boot_lv

Reboot into your debian installer. Choose manual partitioning, and use your files_lv lvm volume as ext3 mounted on /, and the boot_lv mounted on /boot Don’t forget to use your swap partitions (both of them) as swap. Install debian, reboot into your fresh system, and don’t forget to run grub-install on both harddrives so either one is capable of booting the machine.

And finally, some useful mdadm commands:

cat /proc/mdstat   #health of the arrays. You want [UU] for both. If you have [U_] one device
                   #dropped out of the array
mdadm --manage /dev/md0 --fail /dev/sda1
mdadm --manage /dev/md0 --remove /dev/sda1   #use these two to remove a device from the array.
                                             # useful for testing your raid setup perhaps.
mdadm --zero-superblock /dev/sda1  #prepare sda1 for reattachment to the array
mdadm --manage /dev/md0 --add /dev/sda1 #add sda1 back to the array, it will
                                        #then sync
watch -n 10 cat /proc/mdstat         #watch that looong resync

And finally, look into setting up mdadm to email you when things go wrong. Pretty handy getting notified when one of your drives hiccups and needs some attention.

Tags: , , ,

Leave a Reply