Loveit Very Much

Posted by Dick on October 10, 2005

Since leaving FreeBSD and the kids to shack up with that slut Debian I have grown to love LVM . It makes up for swapping pf for iptables.

Nothing in it is unique to linux, but it’s very very easy to use.
A lot of people still don’t seem to see the point.

‘executive summary’

If you have no idea what LVM is, this won’t make any sense. Nitty gritty at the howto , but in brief:

  • logical volume (LV) : virtual partition. acts like /dev/hdaX. You newfs and mount it.
  • volume group (VG): a load of disk space. chop it up to make LVs.
  • physical volume (PV): real physical partition ( /dev/hdaX ). These are what you make VGs out of.

i.e. it’s an abstraction layer for disk partitions. With that in mind:

#1 – online resize

Today I ran out of space on /home while trying to grab an ISO. Traditionally fixing this involves:

  1. suing, making and chowning /usr/local/rasputnik, adding to backup scripts and symlinking or
  2. trawling through /home doing a cleanup or
  3. finding free space to make a new /home and copying everything over (then trying to find a use for the old /home partition)

neither of which I fancy today, having a monster hangover and a shoulder that feels like I spent the night in police custody.

In this brave new world I just do

$ sudo lvextend -L +1G /dev/biglv/sargehome
$ resize_reiserfs /dev/biglv/sargehome

(oh, did I forget to mention umounting /home? No :D.) Hey presto! An extra gig ready to fill with por business data.

#2 – non-destructive shrinking

With Reiserfs you can shrink the partitions too, if you don’t mind umounting them first. Handy when you find that 2Gb /boot isn’t as essential as you thought it might be 6 months ago

#3 – indirection

Ah, like RESTful urls for partitions.

/dev/sargeVG/homeLV

doesn’t need docs stored elsewhere to say what it’s for. It also protects you from drives being renumbered, etc. since vgscan(8) finds the PVs by UUID rather than device name at boot.

#4 – easy to grow

To make use of extra storage:

  • Stick extra disk in
  • create some PVs
  • add them to your existing VG(s)

and the space is available to grow your existing partitions onto (see #1 above).

#5 – well supported

Most Linuxes (no, redhat doesn’t count) support LVM out of the box – well, initrd – so two installs can share a volume group, and even LVs (shared /home for example).

There are even grub patches to boot from LVM , although personally I prefer a physical /boot.

#6 – reversibility

Internally, LVs are made up of extents, fixed size blocks of data that are scattered around your various PVs.

Suppose you need to free some physical disk space – to install some non-LVM aware OS, or you want to sell a hard drive, whatever.

pvmove(8) can migrate your extents from one PV to another , so you can ‘empty’ the PVs that are in the way. It’s a bit slow, but you can do it without any downtime, so who cares?

(NB: this isn’t supposed to work on 2.6, but seems to.)

$ sudo pvmove /dev/hdd6 # try to empty the partition
mirror: Required device-mapper target(s) not detected in your kernel
$ sudo modprobe dm-mapper
$ sudo pvmove /dev/hdd6 # empty it out
$  sudo vgreduce -a bigVG # '-a' == drop all empty PVs
 Physical volume "/dev/hda5" still in use
 Physical volume "/dev/hda6" still in use
 Removed "/dev/hdd5" from volume group "planb-vg"
 Physical volume "/dev/hdd6" still in use
$ vgscan # without this, pvremove wanted a '-ff' option
$ sudo pvremove /dev/hdd5 # wipe the 'i am a PV' label from the partition
$ # /dev/hdd6 now free to format

You could also spread out PVs across multiple spindles/controllers for a quick speed boost (it probably makes more sense to combine LVM with RAID in that case).

#7 – LVM snapshots

Obviously great for hot backups, but LVM2 makes its snapshot read/write by default, which allows all sorts of wierd tricks

( Haven’t tried this, and I’ve heard rumours it’s unsupported by the 2.6 device mapper )

That’s it for starters

All this just applies to workstations. At the server end, I don’t see how you could do without LVM - especially if you use a SAN. If I think of any more, I’ll let you know.