<?xml version='1.0' encoding="UTF-8"?>
<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/lvm2.xml,v 1.10 2005/02/13 14:01:22 aaby Exp $ -->
<!DOCTYPE guide SYSTEM "/dtd/guide.dtd">

<guide link = "/doc/en/lvm2.xml">
<title>Gentoo LVM2 installation</title>
<author title="Author">
  <mail link="avi@CFFtechnologies.com">Avi Schwartz</mail>
</author>
<author title="Contributor">
  <mail link="rajiv@gentoo.org">Rajiv Manglani</mail>
</author>
<author title="Editor">
  <mail link="neysx@gentoo.org">Xavier Neys</mail>
</author>

<abstract>
This guide describes how to setup your Gentoo machine using the Logical Volume
Manager version 2 (LVM2).
</abstract>

<license/>

<version>2.0.6</version>
<date>2004-11-21</date>

<chapter>
<title>Introduction</title>
<section>
<body>

<p>
This guide is based on an example with two IDE hard disks.  It means that you
will more than likely need to change the drive, partition names and partition
sizes to match your own setup and needs.
</p>

<warn>
This document is not intended to be an LVM2 tutorial.  It serves as a
supplement to the Gentoo installation procedure as described in the <uri
link="/doc/en/handbook/handbook-x86.xml?part=1&amp;chap=0">Handbook, Part 
1</uri>. Make sure you <c>read</c> the Gentoo Installation Manual 
<c>before</c> you start your installation process.
</warn>

<note>
For a complete LVM HOWTO point your browser to
<uri>http://tldp.org/HOWTO/LVM-HOWTO</uri>
</note>

</body>
</section>
<section>
<title>Initial requirements</title>
<body>

<p>
If you do a fresh install of Gentoo, you will need to use a bootable CD with
LVM2 support such as a Gentoo LiveCD. You can find the LiveCD for an x86
architecture on our <uri
link="http://www.gentoo.org/main/en/mirrors.xml">mirrors</uri> under
<path>/releases/x86/2004.3/livecd</path>. Other architectures might
be supported as well.
</p>

<p>
If you install LVM2 on a currently running system with some spare hard disk
space, you will need to enable the LVM2 module (<path>dm-mod</path>). This
module is available in <path>gentoo-sources</path>, in
<path>development-sources</path> and in <path>gentoo-dev-sources</path>.
Compiling your kernel and getting LVM2 to work is covered later in this guide.
</p>

<p>
Not all 2.4 kernels provided by Gentoo support LVM2!
</p>

</body>
</section>
<section>
<title>Partitions</title>
<body>

<p>
Our example system has two IDE hard disks and will be partitioned as follows:
</p>

<ul>
  <li>/dev/hda1  --  /boot</li>
  <li>/dev/hda2  --  (swap)</li>
  <li>/dev/hda3  --  /</li>
  <li>/dev/hda4  --  Will be used by LVM2</li>
  <li>/dev/hdb1  --  Will be used by LVM2</li>
</ul>

<impo>
Pay attention to the partition names as it is easy to confuse the a's and b's,
and the partition numbers. One false move could wipe out the wrong partition.
You have been warned!
</impo>

<p>
OK, time to start...
</p>

</body>
</section>
</chapter>

<chapter>
<title>Installation</title>
<section>
<body>

<p>
Follow the handbook, but with the following amendments to chapter <c>4. 
Preparing the Disks</c>:
</p>

<p>
Use <c>fdisk</c> as described in the handbook, but use the partition scheme
mentioned above as an example. It is only <e>an example</e>, adapt it to your
own needs.
</p>

<p>
Create a small physical /boot partition (hda1). In this example, /boot will be
not managed by LVM2. This partition will contain your bootloader and your
kernel(s).  A 64MB partition should be well enough for quite a few kernel
generations.
</p>

<p>
Create a swap partition (hda2) and activate it.
</p>

<pre caption="Activating the swap partition">
# <i>mkswap /dev/hda2</i>
# <i>swapon /dev/hda2</i>
</pre>

<p>
Create a / (root) partition (hda3).  If you are interested in trying to put
your root partition under LVM management (which we do not recommend), see the
resources section at the end of this guide for a link to a mini-howto on how to
do this. The size of the root partition need  not be large if you will keep
<path>/opt /usr /home /var</path> and <path>/tmp</path> in an LVM2 Volume Group
(vg). In this case, 150M is sufficient.
</p>

<note>
It is <b>not</b> recommended to put the following directories in an 
LVM2 partition: <path>/etc</path>, <path>/lib</path>, <path>/mnt</path>, 
<path>/proc</path>, <path>/sbin</path>, <path>/dev</path>, and <path>/root</path>.
This way, you would still be able to log into your system (crippled, but 
still somewhat usable, as root) if something goes terribly wrong.
</note>

<p>
Assuming the /boot, swap and root partitions do not use the whole physical disk,
create a fourth partition on this disk and set it to type 8e (Linux LVM).
If you have more physical drives you would like to use with LVM, create 
one partition on each and give them the same type (8e).
</p>

<note>
Considering the huge size of current disks, you might consider splitting your
hard disks into smaller partitions instead of creating a big partition that
will be added to an LVM2 volume group in one block. LVM2 makes it easy to
extend your volumes after all. This leaves you some unallocated partitions you
might need to use outside of an LVM2 group. In short, don't use your disk space
until you know you need it. As an example, one contributor had split his
160&nbsp;Gb hard disk into 8 partitions of 20&nbsp;Gb each.
</note>

<p>
Load the LVM2 <path>dm-mod</path> module.
</p>

<pre caption="Loading the LVM2 module">
# <i>modprobe dm-mod</i>
</pre>

<p>
Scan and activate LVM:
</p>

<pre caption="Activating LVM">
<comment>(Avoid scanning your cdrom)</comment>
# <i>mkdir -p /etc/lvm</i>
# <i>echo 'devices { filter=["r/cdrom/"] }' >/etc/lvm/lvm.conf</i>
# <i>vgscan</i>
  Reading all physical volumes.  This may take a while...
  No volume groups found
<comment>(Make any previously set up volume groups available)</comment>
# <i>vgchange -a y</i>
</pre>

<p>
Prepare the partitions.
</p>

<pre caption="Preparing the partitions">
# <i>pvcreate /dev/hda4 /dev/hdb1</i>
  No physical volume label read from /dev/hda4
  Physical volume "/dev/hda4" successfully created
  No physical volume label read from /dev/hdb1
  Physical volume "/dev/hdb1" successfully created
</pre>

<p>
Setup a volume group. A volume group is the result of combining several
physical units into a single logical device.
</p>

<p>
In our example, <path>/dev/hda1</path>, <path>/dev/hda2</path> and
<path>/dev/hda3</path> are the <path>/boot</path>, swap and root partitions so
we need to combine <path>/dev/hda4</path> and <path>/dev/hdb1</path>. It can be
done with a single command, but, as an example, we will create our volume group
and extend it.
</p>

<pre caption="Creating and extending a volume group">
<comment>(Create a volume group named vg)</comment>
# <i>vgcreate vg /dev/hda4</i>
  /etc/lvm/backup: fsync failed: Invalid argument <comment>(Ignore this warning)</comment>
  Volume group "vg" successfully created
<comment>(Extending an existing volume group)</comment>
# <i>vgextend vg /dev/hdb1</i>
  /etc/lvm/backup: fsync failed: Invalid argument <comment>(Ignore this warning, again and later as well)</comment>
  Volume group "vg" successfully extended
</pre>

<p>
Create the logical volumes.  Logical volumes are the equivalent of partitions
you would create using fdisk in a non LVM2 environment. In our example, we
create the following partitions:
</p>

<table>
<tr>
  <th>Directory</th>
  <th>Size</th>
</tr>
<tr>
  <ti>/usr</ti>
  <ti>10 GB</ti>
</tr>
<tr>
  <ti>/home</ti>
  <ti>5 GB</ti>
</tr>
<tr>
  <ti>/opt</ti>
  <ti>5 GB</ti>
</tr>
<tr>
  <ti>/var</ti>
  <ti>10 GB</ti>
</tr>
<tr>
  <ti>/tmp</ti>
  <ti>2 GB</ti>
</tr>
</table>

<p>
Since we are going to use LVM2, we should not worry too much about partition
sizes because they can always be expanded as needed.
</p>

<note>
As Terje Kvernes commented, it is easier to increase the size of a partition
then to shrink it.  You might therefore want to start with smaller partitions
and increase their size as needed.
</note>

<pre caption="Creating and extending logical volumes">
# <i>lvcreate -L10G -nusr  vg</i>
  Logical volume "usr" created <comment>(Further similar messages not displayed)</comment>
# <i>lvcreate -L5G  -nhome vg</i>
# <i>lvcreate -L5G  -nopt  vg</i>
# <i>lvcreate -L10G -nvar  vg</i>
# <i>lvcreate -L2G  -ntmp  vg</i>
<comment>(As an example, let's extend a logical volume with 5 extra Gbytes)</comment>
# <i>lvextend -L+5G /dev/vg/home</i>
</pre>

<p>
Create filesystems on the logical volumes the same way you would on a regular
partition.  We use ext3 on the logical volumes but any filesystem of your
choice will work:
</p>

<pre caption="Creating the filesystems">
# <i>mke2fs -j /dev/vg/usr</i>
# <i>mke2fs -j /dev/vg/home</i>
# <i>mke2fs -j /dev/vg/opt</i>
# <i>mke2fs -j /dev/vg/var</i>
# <i>mke2fs -j /dev/vg/tmp</i>
</pre>

<p>
Mount your partitions as described in the handbook and mount your LVM2 logical
volumes as if they were partitions. Replace the usual <path>/dev/hdxx</path>
with <path>/dev/vg/logical_volumename</path>.
</p>

<pre caption="Mounting your logical volumes">
<comment>(Make sure you have mounted your root partition as described in the handbook first)</comment>
# <i>mkdir /mnt/gentoo/usr</i>
# <i>mount /dev/vg/usr /mnt/gentoo/usr</i>
# <i>mkdir /mnt/gentoo/home</i>
# <i>mount /dev/vg/home /mnt/gentoo/home</i>
# <i>mkdir /mnt/gentoo/opt</i>
# <i>mount /dev/vg/opt /mnt/gentoo/opt</i>
# <i>mkdir /mnt/gentoo/var</i>
# <i>mount /dev/vg/var /mnt/gentoo/var</i>
# <i>mkdir /mnt/gentoo/tmp</i>
# <i>mount /dev/vg/tmp /mnt/gentoo/tmp</i>
</pre>

<note>
The rest of the installation handbook is mostly unchanged so we shall not
walk you through it again except to point out differences.
</note>

<p>
When configuring your kernel, make sure to configure your kernel to
support LVM2 (not all 2.4 kernels do). Select the LVM2 module as follows:
</p>

<pre caption="Selecting the LVM2 module in a 2.4.x kernel">
Multi-device support (RAID and LVM)  ---&gt;
  [*] Multiple devices driver support (RAID and LVM)
  &lt; &gt;  RAID support
<comment>(Note that LVM is not selected on purpose, this was for LVM1)</comment>
  &lt; &gt;  Logical volume manager (LVM) support
  &lt;M&gt;  Device-mapper support
  &lt; &gt;   Mirror (RAID-1) support
</pre>

<pre caption="Selecting the LVM2 module in a 2.6.x kernel">
Device Drivers  ---&gt;
 Multi-device support (RAID and LVM)  ---&gt;
   [*] Multiple devices driver support (RAID and LVM)
   &lt; &gt;   RAID support
   &lt;M&gt;   Device mapper support
</pre>

<p>
The compiled module is called <path>dm-mod.ko</path>
</p>

<p>
After you have built your kernel and installed its modules, add the following
line to your <path>/etc/modules.autoload.d/kernel-{KV}</path> where {KV}
represents your kernel version (2.4 or 2.6) so that the LVM2 module gets loaded
when your machine is booted:
</p>

<pre caption="Adding the LVM2 module into /etc/modules.autoload.d/kernel-2.6">
# <i>nano -w /etc/modules.autoload.d/kernel-2.6</i>
<comment>(Add the following line)</comment>
dm-mod
</pre>

<p>
Now, install the lvm2 package.
</p>

<impo>
Make sure your <path>/usr/src/linux</path> link points to the kernel sources you
are using because the lvm2 ebuild depends on the device-mapper ebuild which
will check the presence of a required source file under
<path>/usr/src/linux/include/linux</path>.
</impo>

<pre caption="Emerging the LVM2 package">
# <i>emerge lvm2</i>
<comment>(At the time of writing, the stable version is 2.00.08.
With version 2.00.08, prevent lvm2 from probing your cdrom by doing:</comment>
# <i>echo 'devices { filter=["r/cdrom/"] }' >> /etc/lvm/lvm.conf</i>

<comment>(Versions 2.00.15 and later come with a /etc/lvm/lvm.conf
Edit your /etc/lvm/lvm.conf and follow the comments</comment>
# <i>nano -w /etc/lvm/lvm.conf</i>
</pre>

<p>
When editing your <path>/etc/fstab</path> file, follow the handbook and add
your LVM2 logical volumes as needed.  Again, here are a few lines needed for
our example:
</p>

<pre caption="Extract of /etc/fstab">
/dev/hda1     /boot   ext3    noauto,noatime 1 1
/dev/hda2     none    swap    sw             0 0
/dev/hda3     /       ext3    noatime        0 0
# Logical volumes
/dev/vg/usr   /usr    ext3    noatime        0 0
/dev/vg/home  /home   ext3    noatime        0 0
/dev/vg/opt   /opt    ext3    noatime        0 0
/dev/vg/var   /var    ext3    noatime        0 0
/dev/vg/tmp   /tmp    ext3    noatime        0 0
</pre>

<p>
When you reach the end of the installation part of the handbook, don't forget
to umount all your LVM2 logical volumes as well and for a good measure run the
following command before you reboot:
</p>

<pre caption="Shutting down LVM2">
# <i>vgchange -a n</i>
</pre>

<p>
Restart your machine and all partitions should be visible and mounted.
</p>

</body>
</section>
</chapter>

<chapter>
<title>Resources</title>
<section>
<body>

<ul>
  <li>
    The official <uri link="http://sources.redhat.com/lvm2">LVM2 home page</uri>
  </li>
  <li>
    The <uri link="http://tldp.org/HOWTO/LVM-HOWTO">LVM Howto</uri>
  </li>
  <li>
    Daniel Robbins's articles on LVM at IBM's DeveloperWorks:
    <uri>http://www-106.ibm.com/developerworks/linux/library/l-lvm/?dwzone=linux</uri>
    and 
    <uri>http://www-106.ibm.com/developerworks/linux/library/l-lvm2.html?dwzone=linux</uri>
  </li>
  <li>
    How to boot your root FS off of LVM1:
    <uri>http://www.the-infinite.org/archive/docs/lvm/howto-boot-off-root-lv.txt</uri>
  </li>
</ul>

</body>
</section>
</chapter>

<chapter>
<title>Acknowledgements</title>
<section>
<body>

<p>
Thanks <mail link="bangert@gentoo.org">Thilo Bangert</mail> and <mail
link="terjekv@math.uio.no">Terje Kvernes</mail> for their help and comments on
this document.
</p>

</body>
</section>
</chapter>
</guide>
