This site is now 100% read-only, and retired.

XML logo

LVM Cookbook : For feedback?
Posted by Steve on Sat 10 Jun 2006 at 12:52
Tags: ,

This is a work-in-progress cookbook for LVM usage.

Comments welcome. I don't feel confident enough to post this without a few comments first since I rarely touch LVM and have to learn it from scratch each time.


LVM can be a little confusing to newcomers so I'm going to only cover the basics, give none of the background theory and assume you're happy to read some manpages.

There are some new pieces of terminology that you'll need to understand to make the best use of LVM. The most important things you must know are:

  • physical volumes
    • These are the physical disk partitions /dev/hdb, etc, and are what you'd be used to using when mounting/unmounting things. Using LVM we can combine multiple physical volumes into volume groups.
  • volume groups
    • A volume group is comprised of real physical volumes, and is the storage used to create logical volumes which you can create/resize/remove and use. You can consider a volume group as a "virtual partition" which is comprised of an arbitary number of physical volumes.
  • logical volumes
    • These are the volumes that you'll ultimately end up mounting upon your system. They can be added, removed, and resized on the fly. Since these are contained in the volume groups they can be bigger than any single physical volume you might have. (ie. 4x5Gb drives can be combined into one 20Gb volume group, and you can then create two 10Gb logical volumes.)

To use LVM you need to take at least one partition and add it to a volume group. Why would you do this? Well it would let you create new partitions on the fly, and make better use of your space.

In my case I have a laptop with the following setup:

    Name        Flags      Part Type  FS Type          [Label]        Size (MB)
 ------------------------------------------------------------------------------
    hda1        Boot        Primary   Linux ext3       [/]              8000.01 
    hda2                    Primary   Linux swap / Solaris              1000.20
    hda3                    Primary   Linux LVM                        31007.57

Here I have a 7Gb root partition which contains my Debian GNU/Linux installation. I also have a 28Gb LVM partition. I've chosen this setup so that I can create a dedicated /home partition using LVM - and if I need more space I can extend it.

In this example hda1, hda2, and hda3 are all physical volumes. We'll initialize hda3, and then add it to a volume group:

root@lappy:~# pvcreate /dev/hda3

If you wanted to combine several disks, or partitions you could do the same for those:

root@lappy:~# pvcreate /dev/hdb
root@lappy:~# pvcreate /dev/hdc

Once we've initialised the partitions, or drives, we will go on to create a volume group which is built up of them:

root@lappy:~# vgcreate skx-vol /dev/hda3

Here "skx-vol" is the name of the volume group. (If you wanted to create a single volume spanning two disks you'd run "vgcreate skx-vol /dev/hdb /dev/hdc". There are a lot of options for specifying mirrors, etc. Raid-esque!)

If you've done this correctly you'll be able to see it included in the output of vgscan:

root@lappy:~# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "skx-vol" using metadata type lvm2

Now that we have a volume group, called skx-vol, we can actually start using it.

What we really want to do is create logical volumes which we can mount. If it turns out that our logical volumes were too small we can resize them in the future.

For test purposes we'll create a small volume with the name 'test':

root@lappy:~# lvcreate -n test --size 1g skx-vol
Logical volume "test" created

This logical volume has now appeared as /dev/skx-vol/test and is sized at 1Gb. It can be formatted and mounted just like any other partition:

root@lappy:~# mkfs.ext3 /dev/skx-vol/test
root@lappy:~# mkdir /home/test
root@lappy:~# mount /dev/skx-vol/test  /home/test

Cool, huh?

Now we get onto the fun stuff. Let us pretend that the test partition is full and we want to make it bigger. First of all we can look at how big it is at the moment with lvdisplay:

root@lappy:~# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/skx-vol/test
  VG Name                skx-vol
  LV UUID                J5XlaT-e0Zj-4mHz-wtET-P6MQ-wsDV-Lk2o5A
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                1.00 GB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           254:0
   

We see it is 1Gb in size (no suprise really!) before we go on to resize the volume remember we must unmount it first:

root@lappy:~# umount  /home/test/
root@lappy:~# lvextend -L+1g /dev/skx-vol/test 
Extending logical volume test to 2.00 GB
Logical volume test successfully resized

Looking at lvdisplay again we can see the volume was resized:

root@lappy:~# lvdisplay 
  --- Logical volume ---
  LV Name                /dev/skx-vol/test
  VG Name                skx-vg
  LV UUID                uh7umg-7DqT-G2Ve-nNSX-03rs-KzFA-4fEwPX
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                2.00 GB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     0
  Block device           254:0

The important thing to realise is that although the volume has been resized the ext3 filesystem on it has stayed unchanged. We need to resize the filesystem to actually fill the volume:

root@lappy:~# e2fsck -f /dev/skx-vol/test 
root@lappy:~# resize2fs /dev/skx-vol/test

Remount the logical volume and you'll discover it is now only half full instead of completely full!

If you get bored of the volume and its contents you can remove it with the lvremove command:

root@lappy:~# lvremove /dev/skx-vol/test
Do you really want to remove active logical volume "test"? [y/n]: y
Logical volume "test" successfully removed

Other userful commands include lvrename to change the name, and lvreduce to reduce the size of a volume.

Meta-Data

If you're worried about losing details about your volumes in the event of problems do not worry. The current state of the LVM setup upon your machine is maintained in the event of erros.

Running pvdisplay will allow you to see which physical volume(s) make up your volume group. In the case of our example we only used /dev/hda3 but we can confirm this:

root@lappy:~# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/hda3
  VG Name               skx-vol
  PV Size               28.88 GB / not usable 0   
  Allocatable           yes 
  PE Size (KByte)       4096
  Total PE              7392
  Free PE               5280
  Allocated PE          2112
  PV UUID               WyXQtL-OdT6-GnGd-edKF-tjRU-hoLA-RJuQ6x

If we ever lost this information we could find it contained in the file /etc/lvm/backup/skx-vol.

Similarly if we wanted to know which logical volumes we'd created we could example the directory /etc/lvm/archive. This contains numbered files containing backups of the operations we've conducted.

As an example we created the "test" volume, which we went on to resize. Here is the first section of /etc/lvm/archive/skx-vol_000009.vg:

# Generated by LVM2: Sat Jun 10 12:35:57 2006

contents = "Text Format Volume Group"
version = 1

description = "Created *before* executing 'lvcreate -n test --size 1g skx-vg'"

creation_host = "lappy" 
# Linux lappy 2.6.8-2-686 #1 Sat Jan 8 16:50:08 EST 2005 i686

Filesystems

When it comes to using LVM effectively it is worth considering the filesystem that you wish to use upon your logical volumes.

If you choose a filesystem which doesn't support resizing then increasing the size of your LVM volumes would be pointless. Here is a brief list of the resizable filesystems:

filesystem      while mounted   while unmounted

ext2fs          yes             yes
ext3fs          yes             yes
ReiserFS        yes             yes
JFS             yes             no
XFS             yes             no

If I've missed one you're familiar with please do let me know.

root@itchy:~# pvdisplay --columns
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/hda6  vol  lvm2 a-   139.69G 115.57G

Article

Thanks to all users giving feedback the simple introduction to working with LVM has now been published as a standalone article.

 

Comments on this Entry

Re: LVM Cookbook : For feedback?
Posted by eric (82.249.xx.xx) on Sat 10 Jun 2006 at 16:30
[ View Weblogs ]
I now use LVM on all my computers (servers with Xen, pc with Xen, laptop...) and it's pretty good, and so useful.

This draft seems good to me.

Maybe other important things to note:

  • how to save important information about your LVM VG/LV? There is information in /etc/lvm and /etc/lvmtab but is it sufficient? or do you need to use a command (i can't remember which but there is one)?
  • how to repair a system with LVM (for example with root partition) broken? It occured to me, and my debian install cd doesn't work. Now i use SystemRescueCD for this purpose.
  • when you have successfully booted on a liveCD with LVM support, or maybe reinstall your system and want to access to your LVM volumes, you need the command 'vgchange -ay'... Seems simple, but a little bit hard to find where your system is out-of-order... (got the problem few weeks or months ago after an upgrade in testing branch)
All this is about disaster recovery and system repair, but i think is important to write it down, because on the net you can find many LVM howtos, but none about 'my system is broken, what can i do now to recover my data?'.

:eric:
http://blog.sietch-tabr.com

[ Parent ]

Re: LVM Cookbook : For feedback?
Posted by Steve (62.30.xx.xx) on Sat 10 Jun 2006 at 16:35
[ View Weblogs ]

Interesting feedback, thanks.

I will try to find some good guides talking about disaster recovery to read. Partly to include the information, and partly in case I need it ;)

I almost went for an LVM root partition, but in the end I just wasn't brave enough. I hope that this would reduce the need for a lot of diaster recovery.

Steve

[ Parent ]

Posted by eric (82.249.xx.xx) on Sat 10 Jun 2006 at 16:46
[ View Weblogs ]
I used an LVM root and swap partitions on my laptop, with Ubuntu on it. It works well, but as i wanted to test the new live CD / installation CD when it was only on beta, and because it didn't (and the final one still doesn't) support installation on LVM, i started from scratch with a classic root partition.

At work, on unstable Xen3... hum i can't remember, but i think i also choosed the security option, and put my / on normal partition. But i use LVM for all the rest : domains root and swap, data, etc. (parenthesis: and xen-tools LVM support is cool! :)

[ Parent ]

Re:
Posted by Steve (62.30.xx.xx) on Sat 10 Jun 2006 at 17:09
[ View Weblogs ]

(parenthesis: and xen-tools LVM support is cool! :)

Thanks.

That is one of the things that has made me reinstall my laptop with LVM in the first place.

The 2.0 release has much better integrated LVM support - although it won't be released for a while yet. I need to sort out the new website, and do some testing/documentation writing.

Steve

[ Parent ]

Re: LVM Cookbook : For feedback?
Posted by Anonymous (84.108.xx.xx) on Sat 24 Jun 2006 at 20:17
Heya

I remember using LVM on HPUX systems back when at work,
it seems to me it's pretty much 1:1 on how u admin it and use it

the cookbook is excellent, explanations and the way you wrote it is very simple
to follow and understand


might be a good idea to add a lil' section about extending volume-groups (if adding more hd's in the future etc)

[ Parent ]

Re: LVM Cookbook : For feedback?
Posted by Anonymous (95.176.xx.xx) on Mon 27 Sep 2010 at 18:31
Hi,



In the case you have lost all partitions on your disk and rerinstall a new system, taking care to not touch the lvm partitions. Then, if they are lost, you can recover the partition with testdisk which works fine even with lvm type partitions. But then, if /etc/lvm have been lost also, the vgchange -ay command does not work. Well it works but tells you there are no logical volume.

I wonder how to recover the mapping of the logical volumes within the partition....

[ Parent ]

Re: LVM Cookbook : For feedback?
Posted by eric (82.249.xx.xx) on Sat 10 Jun 2006 at 16:38
[ View Weblogs ]
other things :
  • about the theory, maybe you can find a page with some, with nice images, and make a link to it (i got one, but it's in french :(
  • about the resizing of volumes and filesystems, it could be interesting to list the FS available on linux which can be resized (reiserfs, xfs, jfs?)

:eric:
http://blog.sietch-tabr.com

[ Parent ]

Re: LVM Cookbook : For feedback?
Posted by daemon (198.54.xx.xx) on Sat 10 Jun 2006 at 22:13
[ View Weblogs ]
I'm far from being an expert on these things, so I might have it the wrong way round, but your terminology section seems to be a bit skew -- I would have rather described volume groups as "virtual disks" rather than "virtual partitions", and logical volumes as the "virtual partitions" instead. That makes more sense to me with my level of understanding of the way LVM works.

You can map an FS to a LV 1:1, much like an FS and a partition, but you can also have many FS's in one VG, much like you can have many FS's on one disk (N:1). Does that make any sense, or is should I just go sleep it off?

Very nice otherwise.

Cheers.

[ Parent ]

Re: LVM Cookbook : For feedback?
Posted by Steve (62.30.xx.xx) on Sun 11 Jun 2006 at 12:38
[ View Weblogs ]

I think you're making perfect sense, so I'll make sure I update this when I've had some more sleep and experimentation.

Thanks for the comment.

Steve

[ Parent ]

Re: LVM Cookbook : For feedback?
Posted by Anonymous (84.85.xx.xx) on Mon 26 Jun 2006 at 12:53
It could be worth mentioning the LVM support in the Etch Debian Installer. With the upcoming Beta3 (currently daily images) the support is IMHO fairly mature (the user interface has been improved a lot since Beta2. You can flexibly define your VGs and LVs manually during partitioning.
You can also let the installer setup LVM automatically using recipes (limited to 1 disk). That option can now be preseeded too.

You can manually set up LVM over RAID using the installer.

Manually setting up of encrypted partitions on LVM will also be supported in Beta3 and a component to do this automatically using recipes is being worked on.

Cheers,
Frans Pop

[ Parent ]

Re: LVM Cookbook : For feedback?
Posted by plovs_ (62.85.xx.xx) on Mon 26 Jun 2006 at 13:59
This looks really usefull, some notes:

You might note that XFS and JFS cannot be reduced in size, ext2/3 and reiserfs can. (Afaik)

The evms tools make working with lvm a bit easier.

, AP

[ Parent ]