Linux RAID and LVM configuration
Recently I have had the opportunity to learn how to setup RAID and LVM on Linux. Specifically on Ubuntu 6.06.
It is striking how easy it is to setup once you know the words to use. It is just as striking to see how all the pieces plug together… like legos. The things made: partitions, RAIDs, logical volumes, these are all compatible parts that can be plugged together in many different ways. It is a beautiful thing.
Here is a script to play with these commands (please don’t run this on your system unless you understand basically what it does, it could mess up your system):
# create some loopback devices to play with
# these are simple files
# that can be used like devices
# this is just for testing
head -c 10000000 < /dev/zero > test_dev0
head -c 10000000 < /dev/zero > test_dev1
losetup -f
losetup /dev/loop0 test_dev0
losetup /dev/loop1 test_dev1
# partition the devices
sfdisk -f -S16065 -uS /dev/loop0 << EOF
63,16065,FD
EOF
sfdisk -f -uS /dev/loop1 << EOF
63,16065,FD
EOF
# create a RAID out of the two devices
mdadm --create --verbose /dev/md2 --level=1 \
--raid-devices=2 /dev/loop0 /dev/loop1
# create an LVM physical volume from the RAID
pvcreate /dev/md2
# create an LVM volume group
# and put the physical volume in it
vgcreate demo_vg /dev/md2
# create an LVM logical volume in the volume group
lvcreate --size 4M -n demo_lv demo_vg
# create a filesystem in the logical volume
# the -j option creates an ext3 file system
mkfs -j /dev/mapper/demo_vg-demo_lv
# make a directory to mount the new file system
mkdir demo
# mount the file system
mount /dev/mapper/demo_vg-demo_lv demo
A few notes on this:
Some of the loopback commands came from www.linux.com
Normally you would create a device by installing a disk, rather than the loopback device commands that are at the beginning. However, this is a good way to get experience with the commands before working on real disks.
The script partitions the devices using sfdisk. I used this command because it was something I could script. In practice I use cfdisk to partition the drives. The key steps are to setup the sizes of the partitions and to set the partition type. In this case set it to FD, to mark it as a Linux RAID partition. I recommend that all of the partitions that are placed into a RAID together be of the exact same size.
The RAID created in the script is named /dev/md2. This name depends on what is available on your system.
For a real system, you will want to get the mount to be performed via /etc/fstab so it is automatic.
Some commands to see the creation:
# see loopback device losetup /dev/loop0 # see partition table (actually this seems to not work # with the loopback device) sfdisk -l /dev/loop0 # see the RAID cat /proc/mdstat # list all physical volumes pvscan # list all volume groups vgs # list all logical volumes lvs # another way of seeing logical volumes ls /dev/mapper # see what is mounted mount
A script to tear down the demo file system:
Comments (0)umount demo rmdir demo lvremove -f /dev/demo_vg/demo_lv vgremove demo_vg mdadm --stop /dev/md2 losetup -d /dev/loop0 losetup -d /dev/loop1 rm test_dev0 rm test_dev1











