#2 RAID + Device-Mapper / LVM2

Using Linux Logical Volume Management and RAID on top of ordinary devices offers powerful administrative as well as performance benefits. One thing I find myself looking up once a year or so (when storage is filling up) is some brief step-by-step snipplets for LVM2.

Redundant Arrays of Inexpensive Disks

RAID setups allow combining multiple, inexpensive disks to a combined storage. This is usually used to increase the storage size for database applications or other content required to be processed in it's entity or to protect against disk failures by redundantly storing the information and/or parity information on multiple disks.

To create a software RAID under Linux with the new-style mdadm utilities with 4 disks and RAID5 the command simply is:

> mdadm -v --create /dev/md0 --level=raid5 --raid-devices=4 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1
mdadm: layout defaults to left-symmetric
mdadm: chunk size defaults to 64K
mdadm: size set to 472761472K
mdadm: array /dev/md0 started.

or a more lightweight (SOHO :-) setup with just 2 disks for a RAID1 mirror setup:

> mdadm -v --create /dev/md0 --level=raid1 --raid-devices=2 /dev/sda1 /dev/sdb1

On initial creation the array will be sychronizing and the progress can be monitor, for example via /etc/proc:

> cat /proc/mdstat
Personalities : [linear] [raid0] [raid1] [raid5]
md0 : active raid1 hdc1[1] hdd2[0]
156288256 blocks [2/2] [UU]
[=============>.......] resync = 66.8% (104484608/156288256) finish=21.7min speed=39711K/sec
unused devices: <none>

Logical Volume Management

Logical Volume Management allows to break thru the static, and often limitting historic partitioning of disks. Be it the PC map with 4 primary + n logical parition, the Sun or Apple partition maps, or others. LVM allows to flexible size, add and resize logical volumes. Thefore the LVM is given the control over multiple block storages and later used to flexible allocate chunks. Depending on the actual filesystem used on the logical volumes, most modern ones can be even resized, often grown, some even shrinked (more details on that below).

To initialize a disk or partition for use by LVM pvcreate must be used. Each of those physical volumes can be a disk partition, whole disk, meta device, or loopback file. In this example we want to use the previously created RAID device:

> pvcreate /dev/md0
Physical volume "/dev/md0" successfully created

default stripe size is 4MB:

Next, the logical volume group has to be initialized with vgcreate:

> vgcreate -s 64M lvm0 /dev/md0
Volume group "lvm0" successfully created

And finally the logical volume group can be devided (sliced) into the individual volumes as required with lvcreate:

> lvcreate --size 128G -n home lvm0
Logical volume "home" created

The resulting device nodes can be used, usually by creating and mounting a filesystem as usually:

> mkfs /dev/lvm0/home
mkfs version 1.1.11, 05-Jun-2006
Warning! All data on device /dev/lvm0/home will be lost!

Format completed successfully.

134217728 kilobytes total disk space.
> mount /dev/lvm0/home /home

Of course additional volumes can be added:

> lvcreate --size 2G -n www lvm0
Logical volume "www" created

> mkfs /dev/lvm0/www
> mount /dev/lvm0/www /var/lib/htdocs

To enlarge (extend, grow) an existing lvm both, the logical volume as well as the underlying filesystem have to be resize. On the LVM side the commands to archive this are lvextend and lvreduce plus the filesystem specific tool like resize2fs or resize_reiserfs. To extend the just created "www" volume, formated with ext3 by 1GB the command sequence would be:

> lvextend -L+1G /dev/lvm0/www
lvextend -- extending logical volume "/dev/lvm0/www" to 3 GB
> umount /dev/lvm0/www
> resize2fs /dev/lvm0/www
> mount /dev/lvm0/www /var/lib/htdocs

Note that unless you have a "online ext2/3 resize patched kernel" you need to unmount the often used ext2/3 filesystem. Some filesystems, such as jfs, reiserfs and xfs can be resized while mounted (online).

External links

The Author

René Rebe studied computer science and digital media science at the University of Applied Sciences of Berlin, Germany. He is the founder of the T2 Linux SDE, ExactImage, and contributer to various projects in the open source ecosystem for more than 20 years, now. He also founded the Berlin-based software company ExactCODE GmbH. A company dedicated to reliable software solutions that just work, every day.