Linux lvm – Logical Volume Manager

Note : This article is from http://www.linuxconfig.org/Linux_lvm_-_Logical_Volume_Manager

This article describes a basic logic behind a Linux logical volume manager by showing real examples of configuration and usage. Despite the fact that Debian Linux will be used for this tutorial, you can also apply the same command line syntax with other Linux distributions such as Red Hat, Mandriva, SuSe Linux and others.

Contents

[hide]

This is what we are going to do

Image:lvm.jpg

Create Partitions

For this Linux lvm example you need a unpartitioned hard disk /dev/sdb. First you need to create physical volumes. To do this you need partitions or a whole disk. It is possible to run pvcreate command on /dev/sdb, but I prefer to use partitions and from partitions I later create physical volumes.

fdis -l

Use your preferred partitioning tool to create partitions. In this example I have used cfdisk.

cfdisk

Partitions are ready to use.

fdisk -l

Create physical volumes

Use the pvcreate command to create physical volumes.

# pvcreate /dev/sdb1<br /># pvcreate /dev/sdb2<br /></pre> <p>The pvdisplay command displays all physical volumes on your system. </p> <pre># pvdisplay<br /></pre> <p>Alternatively the following command should be used: </p> <pre># pvdisplay /dev/sdb1<br /></pre> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager05.jpg" class="image" title="Create physical volumes with pvcreate"><img alt="Create physical volumes with pvcreate" longdesc="/Image:Logicalvolumemanager05.jpg" src="http://www.linuxconfig.org/images/f/fc/Logicalvolumemanager05.jpg" width="508" height="368" /></a> </dd></dl> <a name="Create_Virtual_Group"></a><h2> <span class="mw-headline">Create Virtual Group</span></h2> <p>At this stage you need to create a virtual group which will serve as a container for your physical volumes. To create a virtual group with the name "mynew_vg" which will include /dev/sdb1 partition, you can issue the following command: </p> <pre># vgcreate mynew_vg /dev/sdb1<br /></pre> <p>To include both partitions at once you can use this command: </p> <pre># vgcreate mynew_vg /dev/sdb1 /dev/sdb2<br /></pre> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager06.jpg" class="image" title="Create Virtual Group"><img alt="Create Virtual Group" longdesc="/Image:Logicalvolumemanager06.jpg" src="http://www.linuxconfig.org/images/0/00/Logicalvolumemanager06.jpg" width="508" height="329" /></a> </dd></dl> <p>Feel free to add new physical volumes to a virtual group by using the vgextend command. </p> <pre># vgextend mynew_vg /dev/sdb2<br /></pre> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager07.jpg" class="image" title="virtual group extend"><img alt="virtual group extend" longdesc="/Image:Logicalvolumemanager07.jpg" src="http://www.linuxconfig.org/images/6/60/Logicalvolumemanager07.jpg" width="508" height="329" /></a> </dd></dl> <a name="Create_Logical_Volumes"></a><h2> <span class="mw-headline">Create Logical Volumes</span></h2> <p>From your big cake (virtual group) you can cut pieces (logical volumes) which are treated as a partitions for your linux system. To create a logical volume, named "vol01", with a size of 400 MB from the virtual group "mynew_vg" use the following command: </p> <ul><li> create a logical volume of size 400 MB -L 400 </li><li> create a logical volume of size 4 GB -L 4G </li></ul> <pre># lvcreate -L 400 -n vol01 mynew_vg<br /></pre> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager08.jpg" class="image" title="Create Logical Volumes"><img alt="Create Logical Volumes" longdesc="/Image:Logicalvolumemanager08.jpg" src="http://www.linuxconfig.org/images/e/e9/Logicalvolumemanager08.jpg" width="508" height="238" /></a> </dd></dl> <p>In this case you have created a logical volume with a size of 1GB and the name of vol02 </p> <pre># lvcreate -L 1000 -n vol02 mynew_vg<br /></pre> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager09.jpg" class="image" title="create logical vol01"><img alt="create logical vol01" longdesc="/Image:Logicalvolumemanager09.jpg" src="http://www.linuxconfig.org/images/a/a0/Logicalvolumemanager09.jpg" width="508" height="420" /></a> </dd></dl> <p>Note the free size in virtual group. </p> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager10.jpg" class="image" title="display virtual group"><img alt="display virtual group" longdesc="/Image:Logicalvolumemanager10.jpg" src="http://www.linuxconfig.org/images/6/6a/Logicalvolumemanager10.jpg" width="508" height="303" /></a> </dd></dl> <a name="Create_File_system_on_logical_volumes"></a><h2> <span class="mw-headline">Create File system on logical volumes</span></h2> <p>The logical volume is almost ready to use. All you need to do is to create a filesystem.: </p> <pre># mkfs.ext3 -m 0 /dev/mynew_vg/vol01<br /></pre> <p>the -m option specifies the percentage reserved for the super-user, set this to 0 if you wish not to waste any space, the default is 5%. </p> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager11.jpg" class="image" title="create file system"><img alt="create file system" longdesc="/Image:Logicalvolumemanager11.jpg" src="http://www.linuxconfig.org/images/4/46/Logicalvolumemanager11.jpg" width="508" height="277" /></a> </dd></dl> <a name="Edit_.2Fetc.2Ffstab"></a><h2> <span class="mw-headline">Edit /etc/fstab</span></h2> <p>Add a entry for your newly created logical volume into /etc/fstab </p> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager12.jpg" class="image" title="edit /etc/fstab"><img alt="edit /etc/fstab" longdesc="/Image:Logicalvolumemanager12.jpg" src="http://www.linuxconfig.org/images/4/45/Logicalvolumemanager12.jpg" width="508" height="277" /></a> </dd></dl> <a name="Mount_logical_volumes"></a><h3> <span class="mw-headline">Mount logical volumes</span></h3> <p>Before you mount do not forget to create a mount point. </p> <pre># mkdir /home/foobar<br /></pre> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager13.jpg" class="image" title="mount logical volumes"><img alt="mount logical volumes" longdesc="/Image:Logicalvolumemanager13.jpg" src="http://www.linuxconfig.org/images/a/ae/Logicalvolumemanager13.jpg" width="508" height="108" /></a> </dd></dl> <a name="Extend_logical_volume"></a><h2> <span class="mw-headline">Extend logical volume</span></h2> <p>The biggest advantage of logical volume manager is that you can extend your logical volumes any time you are running out of the space. To increase the size of a logical volume by another 800 MB you can run this command: </p> <pre># lvextend -L +800 /dev/mynew_vg/vol01<br /></pre> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager14.jpg" class="image" title="Image:logicalvolumemanager14.jpg"><img alt="Image:logicalvolumemanager14.jpg" longdesc="/Image:Logicalvolumemanager14.jpg" src="http://www.linuxconfig.org/images/b/b4/Logicalvolumemanager14.jpg" width="616" height="108" /></a> </dd></dl> <p>The command above does not actually increase the physical size of volume, to do that you need to: </p> <pre># resize2fs  /dev/mynew_vg/vol01<br /></pre> <p>Look at the figure below to see what problems you may encounter when extending a volume: </p> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager15.jpg" class="image" title="extend logical volume"><img alt="extend logical volume" longdesc="/Image:Logicalvolumemanager15.jpg" src="http://www.linuxconfig.org/images/6/67/Logicalvolumemanager15.jpg" width="616" height="537" /></a> </dd></dl> <a name="Remove_logical_volume"></a><h2> <span class="mw-headline">Remove logical volume</span></h2> <p>The command lvremove can be used to remove logical volumes. Make sure that before you attempt to remove logical volumes your logical volume does not have any valuable data stored on it, moreover make sure the volume is unmounted. </p> <pre># lvdisplay<br /></pre> <dl><dd><a href="http://www.linuxconfig.org/Image:Logicalvolumemanager16.jpg" class="image" title="display volume group"><img alt="display volume group" longdesc="/Image:Logicalvolumemanager16.jpg" src="http://www.linuxconfig.org/images/b/ba/Logicalvolumemanager16.jpg" width="616" height="394" /></a> </dd></dl> <pre># lvremove /dev/mynew_vg/vol02<br />
remove logical volume

Other Topics

Discussion

Alexander.Orlov 10:35, 30 August 2007 (CDT) Any scenario ideas how/if/why LVM can be combined with GFS or OCFS? Will LVM become obsolete when the networks are fast enough to carry large amounts of data?

A FAQ list for the tutorial

  • Is it possible/advisable to use LVM volumes as swap?
  • Is it possible and/or advisable to use LVMs for external USB storage devices used for backups?
  • Whether it is a good idea to use encrypted LVM volumes; especially encrypted LVM software containing volumes.

I have no answers to these questions yet. Alexander.Orlov 10:35, 30 August 2007 (CDT)

External Links

Technorati Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *