<HELPER-TINY T2 Knowledge Base - Device Mapper and LVM2>

<HELPER-MENU-DEFAULT>
</HELPER-MENU>

<div class="sidemenu">
<INCLUDE documentation/kb/menu.inc>
</div>

<div class="page" id="page">

<h2>#2 RAID + Device-Mapper / LVM2</h2>
<time datetime="2008-02-13">2008-02-13, by René Rebe</time>

<p>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.</p>

<h3>Redundant Arrays of Inexpensive Disks</h3>

<p>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.</p>

<!--
table raid level:

RAID 0: stripe
RAID 1: mirror
RAID 5: distributed parity (n + 1)
RAID 10: mirror and stirpe together (1 + 0)-->

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

<div class="screen">
&gt; mdadm -v --create /dev/md0 --level=raid5 --raid-devices=4 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1<br>
mdadm: layout defaults to left-symmetric<br>
mdadm: chunk size defaults to 64K<br>
mdadm: size set to 472761472K<br>
mdadm: array /dev/md0 started.
</div>

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

<div class="screen">
&gt; mdadm -v --create /dev/md0 --level=raid1 --raid-devices=2 /dev/sda1 /dev/sdb1
</div>

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

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

<h3>Logical Volume Management</h3>

<p>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).</p>

<p>To initialize a disk or partition for use by LVM
<b>pvcreate</b> 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:</p>

<div class="screen">
&gt; pvcreate /dev/md0<br>
  Physical volume "/dev/md0" successfully created<br>
<br>
default stripe size is 4MB:
</div>

<p>Next, the logical volume group has to be initialized
with <b>vgcreate</b>:</p>

<div class="screen">
&gt; vgcreate -s 64M lvm0 /dev/md0<br>
  Volume group "lvm0" successfully created<br>
</div>

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

<div class="screen">
&gt; lvcreate --size 128G -n home lvm0<br>
  Logical volume "home" created
</div>

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

<div class="screen">
&gt; mkfs /dev/lvm0/home<br>
mkfs version 1.1.11, 05-Jun-2006<br>
Warning!  All data on device /dev/lvm0/home will be lost!<br>
<br>
Format completed successfully.<br>
<br>
134217728 kilobytes total disk space.</br>
&gt; mount /dev/lvm0/home /home
</div>

<p>Of course additional volumes can be added:</p>

<div class="screen">
&gt; lvcreate --size 2G -n www lvm0<br>
  Logical volume "www" created<br>
<br>
&gt; mkfs /dev/lvm0/www<br>
&gt; mount /dev/lvm0/www /var/lib/htdocs
</div>

<p>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 <b>lvextend</b> and <b>lvreduce</b> plus the filesystem
specific tool like <b>resize2fs</b> or <b>resize_reiserfs</b>.
To extend the just created "www" volume, formated with ext3
by 1GB the command sequence would be:</p>

<div class="screen">
&gt; lvextend -L+1G /dev/lvm0/www<br>
lvextend -- extending logical volume "/dev/lvm0/www" to 3 GB<br>
&gt; umount /dev/lvm0/www<br>
&gt; resize2fs /dev/lvm0/www<br>
&gt; mount /dev/lvm0/www /var/lib/htdocs
</div>

<p>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).</p>

<H3>External links</H3>

<ul>
<li><a href="http://www.redhat.com/magakb/009jul05/features/lvm2/">RedHat magazine article about LVM2</a></li>
<li><a href="http://www.gagme.com/greg/linux/raid-lvm.php">Managing RAID and LVM with Linux</a></li>
</ul>

<h3>The Author</h3>

<INCLUDE kb/rene.inc>

</div>

</HELPER>