367 lines
14 KiB
HTML
367 lines
14 KiB
HTML
<!--startcut ==============================================-->
|
|
<!-- *** BEGIN HTML header *** -->
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<HTML><HEAD>
|
|
<title>Using the Logical Volume Manager LG #84</title>
|
|
</HEAD>
|
|
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
|
|
ALINK="#FF0000">
|
|
<!-- *** END HTML header *** -->
|
|
|
|
<!-- *** BEGIN navbar *** -->
|
|
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="tougher.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue84/vinayak.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../lg_faq.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="lg_backpage.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
|
|
<!-- *** END navbar *** -->
|
|
|
|
<!--endcut ============================================================-->
|
|
|
|
<TABLE BORDER><TR><TD WIDTH="200">
|
|
<A HREF="http://www.linuxgazette.com/">
|
|
<IMG ALT="LINUX GAZETTE" SRC="../gx/2002/lglogo_200x41.png"
|
|
WIDTH="200" HEIGHT="41" border="0"></A>
|
|
<BR CLEAR="all">
|
|
<SMALL>...<I>making Linux just a little more fun!</I></SMALL>
|
|
</TD><TD WIDTH="380">
|
|
|
|
|
|
<CENTER>
|
|
<BIG><BIG><STRONG><FONT COLOR="maroon">Using the Logical Volume Manager</FONT></STRONG></BIG></BIG>
|
|
<BR>
|
|
<STRONG>By <A HREF="../authors/vinayak.html">Vinayak Hegde</A></STRONG>
|
|
</CENTER>
|
|
|
|
</TD></TR>
|
|
</TABLE>
|
|
<P>
|
|
|
|
<!-- END header -->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<h1> The Problem </h1>
|
|
|
|
<p>
|
|
One of the biggest problems faced by a linux user is the problem
|
|
of estimating and allocating enough disk space to partitions when setting
|
|
up a linux box. It does not matter much whether he is a system administrator
|
|
looking after a server farm or an intermediate/power user of linux who has
|
|
realized that he is going to run out of disk space. Sounds familiar
|
|
doesn't it? Then starts the struggle to overcome the problem. Aha,
|
|
the user has a brain-wave and problem is solved (after some sleepless nights)
|
|
by using some non-elegant methods (read dirty hacks) like symlinks spanning
|
|
partitions or using some partition resizing tools like parted. But these are
|
|
only generally temporary solutions and we are faced with the same problem again.
|
|
</p>
|
|
<p>
|
|
How you wish that this problem could be solved!! The hacker in you
|
|
wishes that you had a system on which you can experiment freely regardless
|
|
of disk space and you could add or delete disk space as and when required.
|
|
If you are a system administrator of a site with a number of servers which
|
|
are always connected to the Internet, the stakes are all the more higher.
|
|
Each minute of downtime causes losses. Even the danger of customers going
|
|
away from your site. You can ill afford to reboot the server after you make
|
|
changes to the partition table every time this scenario arises. LVM can be a
|
|
lifesaver in such situations.
|
|
</p>
|
|
|
|
<h1> Introduction to LVM </h1>
|
|
|
|
<p>
|
|
The Linux LVM can make your life a little easier. LVM takes a higher
|
|
level view of storage space as compared to that of partitions and hard disks.
|
|
Read on to discover how. LVM was introduced into the main kernel source tree
|
|
from 2.4.x series onwards. Before we move on to LVM, let us have a look at
|
|
some of the concepts and terminology that will be used.
|
|
</p>
|
|
|
|
<center> <img alt="Terminology of LVM" src="misc/vinayak/LVM_1.png"> </center>
|
|
|
|
<ul>
|
|
<li> <h3> Physical Volume </h3>
|
|
<b> Physical Volume </b> generally refers to the hard disk partitions
|
|
or a device that looks (logically) similar to a hard disk partition such as a
|
|
RAID device.
|
|
<li> <h3> Logical Volume </h3>
|
|
One or many physical volumes make up a <b> Logical Volume </b>. In LVM,
|
|
a logical volume is similar to a hard disk partition in non-LVM systems. The
|
|
logical volume can contain a file-system e.g. /home or /usr.
|
|
<li> <h3> Volume Groups </h3>
|
|
One or many such logical volumes make up a <b> Volume Group </b>. For
|
|
LVM , a volume group is similar to a a physical hard disk in a non-LVM system.
|
|
The volume group brings together many logical volume to form one administrative
|
|
unit.
|
|
</ul>
|
|
|
|
<h1> How LVM works </h1>
|
|
<p>
|
|
Now that we have got a grip on the terminology of LVM, let us see
|
|
how it actually works. Each physical volume is divided into a number of
|
|
basic units called as <b> Physical Extents (PE) </b>. The size of a physical
|
|
extent is variable but same for physical volumes belonging to a volume group.
|
|
Within one physical volume, every PE has a unique number. The PE is the
|
|
smallest unit that can be addressed by a LVM on a physical storage.
|
|
</p>
|
|
<p>
|
|
Again each logical volume is divided into a number of basic
|
|
addressable units called as <b> Logical Extents (LE) </b>. In the same volume
|
|
group the size of the logical extent is same as that of the physical extent.
|
|
Obviously, the size of LEs is same for all the logical volumes of a volume
|
|
group.
|
|
</p>
|
|
<p>
|
|
Each PE has a unique number on a physical volume but not necessarily
|
|
for a logical volume. This is because a logical volume can be made up of
|
|
several physical volumes in which case the uniqueness of PE IDs is not
|
|
possible. Hence the LE IDs are used for identifying the LE as well as the
|
|
particular PE associated with it. As has been noted earlier there is 1:1
|
|
mapping between the LEs and PEs. Every time the storage area is accessed the
|
|
address or the IDs of the LE is used to actually write the data onto the
|
|
physical storage.
|
|
</p>
|
|
<p>
|
|
You might be wondering by now, where all the meta-data about the
|
|
logical volume and volume groups is stored. As a analogy, the data about
|
|
the partitions is stored in the partition table in non-LVM systems. The
|
|
<b> Volume Group Descriptor Area (VGDA) </b> functions similar to the
|
|
partition table for LVM. It is stored at the beginning at the beginning
|
|
of each physical volume.
|
|
</p>
|
|
<p>
|
|
The VGDA consists of the following information :-
|
|
<ol>
|
|
<li> one PV descriptor
|
|
<li> one VG descriptor
|
|
<li> the LV descriptors
|
|
<li> several PE descriptors.
|
|
</ol>
|
|
</p>
|
|
<p>
|
|
When the system boots the LVs and the VGs are activated and the VGDA is loaded
|
|
into memory. The VGDA helps to identify where the LVs are actually stored.
|
|
When the system wants to access the storage device, the mapping mechanism
|
|
(constructed with the help of VGDA) is used to access the actual physical
|
|
location to perform I/O operation.
|
|
</p>
|
|
|
|
<h1> Getting down to work </h1>
|
|
<p>
|
|
Let us now see how to use LVM :-
|
|
<p>
|
|
<h2> Step 1 -Configure the kernel </h2>
|
|
<p>
|
|
Before we begin to install LVM there are some prerequisites:- <br>
|
|
your kernel should have the LVM module configured.
|
|
<pre>
|
|
This can be done as follows:-
|
|
# cd /usr/src/linux
|
|
# make menuconfig
|
|
|
|
under the Submenu:-
|
|
Multi-device Support (RAID and LVM) -->
|
|
|
|
enable the following two options:-
|
|
[*] Multiple devices driver support (RAID and LVM)
|
|
and
|
|
<*> Logical volume manager (LVM) Support.
|
|
</pre>
|
|
</p>
|
|
|
|
<h2> Step2 - Check the Amount of Disk Space free on your drive </h2>
|
|
<p>
|
|
This can be done using the following command:-
|
|
<pre>
|
|
# df -h
|
|
Filesystem Size Used Avail Use% Mounted on
|
|
/dev/hda1 3.1G 2.7G 398M 87% /
|
|
/dev/hda2 4.0G 3.2G 806M 80% /home
|
|
/dev/hda5 2.1G 1.0G 1.1G 48% /var
|
|
</pre>
|
|
|
|
<h2> Step 3 - Create LVM partitions on your hard disk </h2>
|
|
<p>
|
|
Use fdisk or any other partition utility to create the LVM partitions.
|
|
The partition type of linux LVM is 8e.
|
|
|
|
<pre>
|
|
# fdisk /dev/hda
|
|
press p (to print the partition table)
|
|
and n (to create a new partition)
|
|
</pre>
|
|
|
|
After the creation of the Linux LVM partition. Print the partition
|
|
table. It will look something like this:-
|
|
|
|
<pre>
|
|
Device Boot Start End Blocks Id System
|
|
/dev/hda1 * 1 506 4064413+ 83 Linux
|
|
/dev/hda2 507 523 136552+ 5 Extended
|
|
/dev/hda5 507 523 136521 82 Linux swap
|
|
/dev/hda6 524 778 2048256 8e Linux LVM
|
|
/dev/hda7 779 1033 2048256 8e Linux LVM
|
|
</pre>
|
|
</p>
|
|
|
|
<h2> Step 4 - Create physical Volumes </h2>
|
|
<p>
|
|
<pre>
|
|
# pvcreate /dev/hda6
|
|
pvcreate -- -physical volume "/dev/hda6" successfully created
|
|
|
|
# pvcreate /dev/hda7
|
|
pvcreate- -- physical volume "/dev/hda7" successfully created
|
|
</pre>
|
|
The above command creates a volume group descriptor at the start
|
|
of the partition.
|
|
</p>
|
|
|
|
<h2> Step 5 - Create Volume Groups </h2>
|
|
<p>
|
|
Create a new volume group and add the two physical volumes to it
|
|
in the following way.
|
|
<pre>
|
|
# vgcreate test_lvm /dev/hda6 /dev/hda7
|
|
vgcreate- -- INFO: using default physical extent size 4 MB
|
|
vgcreate- -- INFO: maximum logical volume size is 255.99 Gigabyte
|
|
vgcreate- -- doing automatic backup of volume group "test_lvm"
|
|
vgcreate- -- volume group "test_lvm" successfully created and activated
|
|
</pre>
|
|
This will create a volume group named test_lvm containing the physical
|
|
volumes /dev/hda6 and /dev/hda7. We can also specify the extent size
|
|
with this command if the extent size of 4MB is not suitable for our
|
|
purpose.
|
|
<br>
|
|
Activate the volume groups using the command
|
|
<pre>
|
|
# vgchange -ay test_lvm
|
|
</pre>
|
|
<br>
|
|
The command "vgdisplay" is used to see the details regarding the volume
|
|
groups created on your system.
|
|
<pre>
|
|
# vgdisplay
|
|
--- Volume group ---
|
|
VG Name test_lvm
|
|
VG Access read/write
|
|
VG Status available/resizable
|
|
VG # 0
|
|
MAX LV 256
|
|
Cur LV 1
|
|
Open LV 0
|
|
MAX LV Size 255.99 GB
|
|
Max PV 256
|
|
Cur PV 2
|
|
Act PV 2
|
|
VG Size 3.91 GB
|
|
PE Size 4 MB
|
|
Total PE 1000
|
|
Alloc PE / Size 256 / 1 GB
|
|
Free PE / Size 744 / 2.91 GB
|
|
VG UUID T34zIt-HDPs-uo6r-cBDT-UjEq-EEPB-GF435E
|
|
</pre>
|
|
|
|
<h2> Step 6 - Create Logical Volumes </h2>
|
|
The lvcreate command is used to create logical volumes in
|
|
volume groups.
|
|
<pre>
|
|
# lvcreate -L2G -nlogvol1 test_lvm
|
|
</pre>
|
|
|
|
<h2> Step 7 - Create a file system </h2>
|
|
<p>
|
|
Now you need to build a filesystem on this logical volume. We have
|
|
chosen to make the reiserfs journalling filesystem on the logical
|
|
volume.
|
|
<pre>
|
|
# mkreiserfs /dev/test_lvm/logvol1
|
|
</pre>
|
|
Mount the newly created filesystem using the mount command.
|
|
<pre>
|
|
# mount -t reiserfs /dev/test_lvm/logvol1 /mnt/lv1
|
|
</pre>
|
|
|
|
<h2> Step 8 - Add entries to /etc/fstab and /etc/lilo.conf </h2>
|
|
<p>
|
|
Add the following entry to /etc/fstab so that the filesystem is
|
|
mounted at boot.
|
|
<pre>
|
|
/dev/test_lvm/logvol1 /mnt/lv1 reiserfs defaults 1 1
|
|
</pre>
|
|
copy the recompiled kernel if you have not replaced your
|
|
original kernel with it yet so u have the option of using LVM
|
|
or not using it.
|
|
<pre>
|
|
image = /boot/lvm_kernel_image
|
|
label = linux-lvm
|
|
root = /dev/hda1
|
|
initrd = /boot/init_image
|
|
ramdisk = 8192
|
|
</pre>
|
|
After adding the above lines reinstall lilo by using
|
|
<pre>
|
|
# /sbin/lilo
|
|
</pre>
|
|
|
|
<h2> Step 9 - Resizing logical volumes </h2>
|
|
Logical volumes can be resized easily using the lvextend command.
|
|
<pre>
|
|
# lvextend -L+1G /dev/test_lvm/logvol1
|
|
lvextend -- extending logical volume "/dev/test_lvm/logvol1" to 3GB
|
|
lvextend -- doing automatic backup of volume group "test_lvm"
|
|
lvextend -- logical volume "/dev/test_lvm/logvol1" successfully extended
|
|
</pre>
|
|
|
|
Similarly logical volumes can be reduced by using the following command
|
|
<pre>
|
|
# lvreduce -L-1G /dev/test_lvm/lv1
|
|
lvreduce -- -Warning: reducing active logical volume to 2GB
|
|
lvreduce- -- This may destroy your data (filesystem etc.)
|
|
lvreduce -- -do you really want to reduce "/dev/test_lvm/lv1"? [y/n]: y
|
|
lvreduce- -- doing automatic backup of volume group "test_lvm"
|
|
lvreduce- -- logical volume "/dev/test_lvm/lv1" successfully reduced
|
|
</pre>
|
|
|
|
<h1> Conclusion </h1>
|
|
<p>
|
|
As we can see from the above discussion LVM is quite extensible and
|
|
pretty straightforward to use. After the volume groups have been set
|
|
up. It is pretty easy to resize logical volumes as per requirements.
|
|
</p>
|
|
|
|
<h1> Resources </h1>
|
|
<ul>
|
|
<li> <a href="http://www.sistina.com/products_lvm.htm"> The LVM Homepage</a>
|
|
<li> <a href="http://www.tldp.org/HOWTO/LVM-HOWTO/index.html"> The LVM HOWTO </a>
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- *** BEGIN copyright *** -->
|
|
<hr>
|
|
<CENTER><SMALL><STRONG>
|
|
Copyright © 2002, Vinayak Hegde.
|
|
Copying license <A HREF="../copying.html">http://www.linuxgazette.com/copying.html</A><BR>
|
|
Published in Issue 84 of <i>Linux Gazette</i>, November 2002
|
|
</STRONG></SMALL></CENTER>
|
|
<!-- *** END copyright *** -->
|
|
<HR>
|
|
|
|
<!--startcut ==========================================================-->
|
|
<CENTER>
|
|
<!-- *** BEGIN navbar *** -->
|
|
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="tougher.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue84/vinayak.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../lg_faq.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="lg_backpage.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
|
|
<!-- *** END navbar *** -->
|
|
</CENTER>
|
|
</BODY></HTML>
|
|
<!--endcut ============================================================-->
|