This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: High Performance Computing

Pain-free disk space management with LVM

By Shashank Sharma on August 21, 2007 (9:00:00 PM)

Share    Print    Comments   

Managing disk space used to be a royal pain for admins and users. Running out of disk space often meant reinstalling Linux or spending a few hours with tools like Parted to resize partitions. However, using the Logical Volume Manager (LVM) tools, you can grow, shrink, and manage disk space with very little hassle.

Understanding LVM

Before jumping into the details of setting up LVM, you need to familiarize yourself with LVM terminology. LVM refers to a typical partition as a Physical Volume (PV). A Volume Group (VG) comprises one or more physical volumes. Each volume group must be divided into Logical Volumes (LV).

A logical volume functions like a normal partition -- they have a filesystem such as Ext3, and a mount point. You can think of the volume group as a virtual hard disk. A logical volume is thus a virtual partition on your virtual hard disk.

Getting started with LVM

You need to install the lvm2 package before you can start with LVM. Debian and Fedora users can use apt-get and yum respectively to install lvm2.

LVM was added to the Linux kernel in the 2.4.x branch, so almost all Linux distributions support it. Several distributions, like Fedora and Debian, allow you to use LVM when partitioning during the installation itself. Also, some distributions such as Fedora and SUSE offer graphical tools to manage your LVM layout.

In this article, I will focus on how you can create a LVM layout and then mount it under your home directory, so as to use it in future. My root partition (/) is sda1, and sda2 is swap. I also have some unused space available on my disk, which will be used to create a partition. If you already have a spare partition, you can simply change its system ID to LVM using fdisk (or another partitioning tool) and then move on to creating the volume group.

To start partitioning, run fdisk on the target disk -- in this case, /dev/sda. You could also use GParted, if you are more comfortable with a graphical tool.

fdisk /dev/sda

You'll see a message like this, with a prompt for a command:

The number of cylinders for this disk is set to 1958.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): 

Note that the number of cylinders will vary. You'll want to enter n as the command to create a new partition. Then, enter p to create a primary partition, and then give it a partition number, and you'll probably want to accept the default values for the first and last cylinder.

We have now created a primary partition. The next step is to change the partition type to LVM. That is, we need to change the partition's system id value to 8e, instead of the default 83 assigned to new partitions:

Use t to tell fdisk you want to change the system ID, then select the partition number you want and enter type 8e as the partition type. After you hit Enter, fdisk will confirm the partition type (Linux LVM).

You can check the partition table to ensure the change in the system id. Press p to print the partition table:

Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1020     8193118+  83  Linux
/dev/sda2            1021        1173     1228972+  82  Linux swap / Solaris
/dev/sda3            1174        1958     3092512+  8e  Linux LVM

Of course, the actual information will differ for you. Type w and then press Enter to save the partition table. The new partition table will be used after you restart the computer.

Creating a volume group

Remember what I said about physical volumes and volume groups? A volume group comprises one or more physical volumes. Before we create a volume group, we need to initialize our physical volumes. First you'll need to reboot to ensure the new partition is used. Then open a terminal window and run this command as root, or with sudo:

pvcreate /dev/sda3

This initializes the physical volume. We will now create a volume group that uses this physical volume. You need to provide a unique name to your volume group. The command vgcreate home2 /dev/sda3 will create a volume group called home2 that comprises /dev/sda3.

You can also use multiple physical volumes to make up a volume group. For example, vgcreate home3 /dev/sda3 /dev/sda5 will create a volume group comprising the sda3 and the sda5 physical volumes. A volume group can also span multiple hard disks: vgcreate home4 /dev/sda /dev/sdb will create a volume group spanning two hard disks. The vgscan command can be used to list all the volume groups.

Now that we have a volume group, it's time to split it into logical volumes. You can create multiple logical volumes within a volume group or use the entire space to create just one. The command lvcreate -n downloads --size 1G home2 will create a 1GB logical volume called downloads within the home2 volume group. The -n option is used to provide a name for your logical volume.

You need both the logical volume name and the name of the volume group to mount the logical volume as discussed below. You can use the lvdisplay command to see the properties of your logical volume.

Before you can begin using the new logical volume, you need to format it and mount it. The logical volume is available as /dev/home2/downloads (that is, /dev/volumegroup/logicalvolume). Run the command mkfs.ext3 /dev/home2/downloads as the root user to format your logical volume with an Ext3 filesystem.

Next create a folder in the home directory with the mkdir /home/downloads command and mount your logical volume with the command mount -t ext3 /dev/home2/downloads /home/downloads. The mount point doesn't necessarily need to have the same name as the logical volume, but it's best you use a uniform name to avoid confusion. You can even add a line to the /etc/fstab file so that the downloads logical volume is mounted automatically after each reboot:

/dev/home2/downloads     /home/downloads     ext3     defaults     1 2

You can now use the downloads logical volume to store data.

Resizing logical volumes

It is safe to assume that you might run out of space on this logical volume. In such a scenario, you can increase the size of the logical volume using the lvextend command.

But, for this to work, you must have available space in your volume group. Don't despair if you've run out of space on your volume group. Jump to the next section that discusses adding physical volumes to your volume group. In the example above, I created a 1GB logical volume in a volume group with 2GB of space.

Therefore, I can extend the size of my logical volume by 1GB. Before resizing, you must unmount the logical volume. While an Ext3 filesystem can be resized on the fly, it's better to be safe. Let's unmount the drive first, using umount /home/downloads. This will unmount the logical volume if you have added an entry in /etc/fstab. If not, run umount /dev/home2/downloads to unmount it.

The command lvextend -L +1G /dev/home2/downloads will increase the size of the logical volume by 1GB, and you should see a success message like this:

  Extending logical volume downloads to 2.00 GB
  Logical volume downloads successfully resized

You can use the lvdisplay command to make sure the size has increased. You next need to expand the filesystem to fit in the resized logical volume. The command resize2fs /dev/home2/downloads will resize the ext3 filesystem on the logical volume, and you can begin using it.

Similar to the lvextend command is the lvreduce command. It can be used to shrink the size of your logical volumes. This is considered risky, as there's a possibility you might lose the data on your logical volume. You must resize the filesystem on the logical volume using the resize2fs command before using the lvreduce command.

Using lvreduce -L -500M /dev/home2/downloads will reduce the size of the logical volume by 500 MB. lvreduce is not a very safe command, and you shouldn't drastically reduce your logical volume, without first backing up all your data.

Modifying volume groups

But what happens when your logical volume has taken up all the space in your volume group? You can add more physical volumes to an existing volume group, and then increase the size of your logical volume as discussed already.

First, unmount the logical volumes within your volume group. Assuming you already have physical volumes available, you can use the command vgextend home2 /dev/sda5 /dev/sda7 to add the physical volumes sda5 and sda7 to the existing volume group home2. The volume group home2 is now made up of sda3, sda5, and sda7.

To remove a volume group, you must first of all remove the logical volumes it contains. Unmount your logical volume, and run the command lvremove /dev/home2/downloads. This command will remove the logical volume and all the data it contains. You can now remove the volume group by using the vgremove command: vgremove home2.

Conclusion

Since converting to LVM, I have slept peacefully knowing that anytime I run out of disk space, I can simply add another partition to my volume group and increase the size of my logical volumes.

The fact that the partition I add to the existing volume group doesn't need to be on the same physical hard disk is also an advantage. Remember, with LVM you can not only increase the size of a logical volume, but also the size of the volume group. Obviously, to increase the size of the logical volume you must have free space in the volume group and to increase the size of the volume group you must have unused physical volumes.

Shashank Sharma specializes in writing about free and open source software for new users and moderates the Linux.com forum boards. He is the coauthor of Beginning Fedora, published by Apress.

Share    Print    Comments   

Comments

on Pain-free disk space management with LVM

Note: Comments are owned by the poster. We are not responsible for their content.

Poorly written article

Posted by: Anonymous [ip: 68.126.176.174] on August 21, 2007 09:30 PM
These inferior articles authored by underqualified individuals only reinforce the idea that it's all about getting Linux.com to pay the authors.

#

Re: Poorly written article

Posted by: Anonymous [ip: 123.51.4.15] on August 23, 2007 09:10 AM
Authored your very own tome on Linux before, have you?

#

Re: Poorly written article

Posted by: Anonymous [ip: 123.200.203.202] on August 23, 2007 12:48 PM
Well, I thought it was pretty good. I don't understand your point, junior.

#

Pain-free disk space management with LVM

Posted by: Anonymous [ip: 121.6.179.87] on August 22, 2007 01:49 AM
This is a good article for a beginner, what problems do you see with the article? You look stupid if you badmouth another person without stating facts.

#

What about the possibility of data corruption in PV's?

Posted by: Anonymous [ip: 121.1.46.54] on August 22, 2007 03:03 AM
Like anything with moving parts, hard disks eventually degrade and the result is usually catastrophic. What would happen if one of the PV's fail? The setup described by the author, spreading one logical partition across several physical partitions across several drives, reminds me of RAID0. If one disk fails in the array, your entire logical partition is screwed. I would like to see another article dealing with this possibility.

#

Re: What about the possibility of data corruption in PV's?

Posted by: Anonymous [ip: 202.254.67.228] on August 22, 2007 08:31 AM
You can create PV`s on RAID1
example
pvcreate /dev/md0

#

Pain-free disk space management with LVM

Posted by: Anonymous [ip: 86.121.29.145] on August 22, 2007 07:24 AM
I am also interested to know what happens if one of the hard drives fail, is there any possibility of circumventing that ?

#

Re: Pain-free disk space management with LVM

Posted by: Anonymous [ip: 202.254.67.228] on August 22, 2007 08:28 AM
Yes you can use LVM on RAID 5 10 1 etc but recovery will be on different layer

#

LVM speed > ?????

Posted by: Anonymous [ip: 202.254.67.228] on August 22, 2007 08:29 AM
Problem with LVM2 is it reduces performance with 20-30% on read it is useful only on RAID5 matrix with many hdd

#

Pain-free disk space management with LVM

Posted by: Anonymous [ip: 194.206.146.249] on August 22, 2007 08:45 AM
And what about perfs ?

#

You don't need to unmount to extend a Volume Group

Posted by: Anonymous [ip: 85.48.161.34] on August 22, 2007 10:03 AM
When you need to add a new PV to a VG, you don't need to unmount all LVs in that VG. Just execute vgextend with the new PV, and all of its extents will be instantly available.

#

Pain-free disk space management with LVM

Posted by: Anonymous [ip: 24.5.207.45] on August 22, 2007 04:23 PM
Does anyone proof-read this stuff? The two paragraphs under "Understanding LVM" are contradictory nonsense. It's pointless to read further.

#

What do you mean "contradictory nonsense"?

Posted by: Anonymous [ip: 70.126.228.159] on August 23, 2007 02:03 AM
The two paragraphs are fine. LVM adds a layer of abstraction so that your OS can use "partitions" that do not have to correspond directly to the partitions on the disk. The descriptions of the terms seem fine to me - I have used LVM for a couple of years and think it is very useful. However, there are smart and silly ways to use it.

I don't think it is a good idea to mix several disks into a large volume group and dole out logical volumes from there - as other posters have said, you are creating the opposite of redundancy - introducing multiple points of failure.

I think LVM makes great sense for dividing a single disk much more flexibly than with multiple traditional partitions. At least the effect of that disk going down would not be any different. As for any performance penalty, I think the ability to resize partitions easily would usually be more than worth it.

The combination of LVM and RAID1 is, I think, a good choice for desktops. Hard drives are cheap, and the redundancy is good protection against data loss from drive failure (but of course does not protect against several other ways to lose one's data!). I usually partition two identical disks with a small partion, a large partition, and leave some empty space. The small partitions get mirrored into md0, which I use for /boot, and the large ones get mirrored into md0, which is the volume group for all of the rest of the partions. It seems to be a flexible and reliable arrangement, and it did indeed preserve my data flawlessly when one of the disks suddenly died.

#

Pain-free disk space management with LVM

Posted by: Anonymous [ip: 71.131.183.30] on August 22, 2007 08:12 PM
The real problem with LVM - as it is with Windows "dynamic disks" is the problem of recovery when a drive goes down. While backup is the only sure recovery method, it is desirable to have tools that can recover such systems if necessary - simply because some people won't backup and will end up needing recovery. For a consultant with such clients, tools are necessary.

It is possible apparently to recover from a corrupted LVM or downed drive, but it also apparently is much more difficult than recovering a normal file system failure.

These points should always be discussed when considering a non-standard file system layout.

#

ZFS

Posted by: Anonymous [ip: 74.57.130.104] on August 23, 2007 04:35 AM
When can I use ZFS in linux natively free as promised ?

#

Re: ZFS

Posted by: Anonymous [ip: 10.100.209.150] on August 29, 2007 10:11 AM
As promised by whom?
I think the kernel devs are unhappy with the layer violations ZFS has (much like they were/are with reiser4).
There's also the incompatibility in licensing, GLPv2 vs CDDL

- Peder

#

Pain-free disk space management with LVM

Posted by: Anonymous [ip: 71.16.89.242] on August 23, 2007 01:43 PM
Good beginners article. Only problem I see is with the Logical Volume extension. Lvextend will only extend the logical volume not the filesystem. For that you need to run the resizefs command.

#

Pain-free disk space management with LVM

Posted by: Anonymous [ip: 209.203.83.178] on August 23, 2007 04:40 PM
Given that resizing a volume, without a commensurate upgrade to the storage hardware, should be an exceptional event, and given that LVM configurations are vastly more problematic to debug when problems occur, I recommend avoiding LVM, with one very significant exception. The snapshotting capability in LVM is fabulous. If you need to snapshot a large filesystem, especially for backing up, by all means use LVM, carefully.

#

ugh, vgscan to list volume groups? NO

Posted by: Anonymous [ip: 204.26.30.5] on August 23, 2007 10:20 PM
I dont think thats what vgscan is for.

vgdisplay is used for that

#

Pain-free disk space management with LVM

Posted by: Anonymous [ip: 81.6.251.105] on August 29, 2007 03:27 PM
The downside of lvm is that when a physical disk goes down,
it screws the logical disk and FC7 doesn't want to know.

Steer well clear is my advice, there's no undo button with lvm

#

Pain-free disk space management with LVM

Posted by: Anonymous [ip: 88.252.30.69] on December 12, 2007 02:19 PM
<a href="http://r10noktanet-seoyarismasi.blogspot.com" title="www.r10.net küresel ısınmaya hayır seo yarışması">www.r10.net küresel ısınmaya hayır seo yarışması</a>

#

Pain-free disk space management with LVM

Posted by: sernak plywood on December 12, 2007 02:52 PM
<a href="http://www.sernak.com/"> Sernak Plywood </a>

<a href="http://www.argplywood.com/"> ARGE Yapi </a>

#

Re: Pain-free disk space management with LVM

Posted by: Anonymous [ip: 88.244.240.178] on December 26, 2007 01:21 PM
Thank you

<a href="http://www.koreanplywood.com/ "> Korean Plywood </a>

#

Pain-free disk space management with LVM

Posted by: Anonymous [ip: 78.183.218.159] on December 23, 2007 01:34 PM
<a href="http://www.tvcanliyayin.com" >Canlı Tv</a>
<a href="http://www.tvcanliyayin.net" >Tv İzle</a>
<a href="http://www.konusarock.org" >Rock</a>
<a href="http://www.tvcanliyayin.com" >Canli Tv İzle</a>
<a href="http://www.tvcanliyayin.com" >Online Tv</a>
<a href="http://www.tvcanliyayin.com" >Tv İzle</a>
<a href="http://www.tvcanliyayin.com" >İzle Tv</a>
<a href="http://www.konusarock.org" >Emo</a>
<a href="http://www.konusarock.org" >Punk</a>
<a href="http://www.konusarock.org" >Rock'in Coke</a>
<a href="http://www.konusarock.org" >Teoman</a>
<a href="http://www.konusarock.org" >Şebnem Ferah</a>
<a href="http://www.hostsahibi.net" >Hosting</a>
<a href="http://www.hostsahibi.net" >Asp Hosting</a>
<a href="http://www.hostsahibi.net" >Php Hosting</a>
<a href="http://www.hostsahibi.net" >Oyun Hosting</a>
<a href="http://www.hostsahibi.net" >Sunucu Hosting</a>
<a href="http://www.hostsahibi.net" >.net Hosting</a>
<a href="http://www.hostsahibi.org" >Asp ve php Hosting</a>
<a href="http://www.hostsahibi.org" >web Hosting</a>
<a href="http://www.hostsahibi.info" >domain ve Hosting</a>
<a href="http://www.hostsahibi.info" >Domain</a>
<a href="http://www.tvcanliyayin.net" >Online Dizi</a>
<a href="http://www.tvcanliyayin.net" >Dizi İzle</a>
<a href="http://www.tvcanliyayin.net" >Canli Dizi İzle</a>
<a href="http://www.tvcanliyayin.net" >Tv</a>
<a href="http://www.e-okul.us" >E-okul</a>
<a href="http://www.e-okul.us" >e-okul sistemi</a>
<a href="http://www.e-okul.us" >E-okul bilgi</a>
<a href="http://www.e-okul.us" >E-okul hakkında</a>

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya