A Simple Introduction to Working with LVM (mirror)

Note: I refer to this article all the time, so I’ve decided to mirror it here on my site. It comes from the now-defunct Debian Administration website.

A simple introduction to working with LVM

Posted by Steve on Wed 28 Jun 2006 at 21:22

The logical volume manager allows you to create and manage the storage of your servers in a very useful manner; adding, removing, and resizing partitions on demand. Getting started with LVM can be a little confusing to newcomer so this guide intends to show the basics in a simple manner.

There several 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 your physical disks, or disk partitions, such as /dev/hda or /dev/hdb1. These 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.)

Logically these are stacked from top to bottom like this:


Dia source file

Creating A Volume Group

To use LVM you need to take at least one partition, initialise it for use with LVM and then include it in 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                            31007.57

Here I have a 7Gb root partition which contains my Debian GNU/Linux installation. I also have a 28Gb partition which will be used by LVM. 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 as a physical volume:

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 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“.)

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.

Working with logical volumes

What we really want to do is create logical volumes which we can mount and actually use. In the future if we run out of space on this volume we can resize it to gain more storage. Depending on the filesystem you’ve chosen you can even do this on the fly!

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 command creates a volume of size 1Gb with the name test hosted on the LVM volume group skx-vol.

The logical volume will now be accessible via /dev/skx-vol/test, and may 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 should 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

(It is possible to resize ext3 filesystems whilst they’re mounted, but I’d still suggest doing it offline as that is less scary.)

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.

Mounting Logical Volumes

In the previous section we showed how you could mount a logical volume, with a command like this:

mount /dev/skx-vol/test  /home/test

If you want your partition to be mounted at boot-time you should update your /etc/fstab to contain an entry like this:

/dev/skx-vol/home    /home       ext3  noatime  0 2
/dev/skx-vol/backups /backups    ext3  noatime  0 2

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 errors.

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 if you’re using more volumes it might be useful to take a look at them with the pvdisplay command:

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      increase while mounted  increase while unmounted  decrease

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

Note that some filesystems can be increased in size, but cannot be reduced.

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

Closing Comments

If you’re ready to make the jump to LVM and don’t have a lot of space handy for allocating to LVM then it might make sense to reinstall your system. The Debian installer has excellent support for creating LVM setups.

We’ve not really covered advanced usage in this introduction but there is a lot of readable and useful documentation available if you’re prepared to search for it. The most obvious starting point is the LVM howto.

Environment Canada Forecast from the Command Line

Here’s how to get an Environment Canada weather forecast from the command line. First, some sample output:

darren@unity:~% forecast
Edmonton (Int'l Aprt) - Weather - Environment Canada
No watches or warnings in effect, Edmonton (Int'l Aprt)
Current Conditions: Mostly Cloudy, -6.0 C
Monday: Chance of flurries. Temperature steady near -5. POP 30%
Monday night: Chance of flurries. Low -13. POP 30%
Tuesday: Chance of flurries. High -7. POP 30%
Tuesday night: Clear. Low -15.
Wednesday: Sunny. High -8.
Wednesday night: Clear. Low -16.
Thursday: Sunny. High -8.
Thursday night: Clear. Low -11.
Friday: Sunny. High +2.
Friday night: Clear. Low -10.
Saturday: Sunny. High +2.
Saturday night: Clear. Low -9.
Sunday: Sunny. High zero.

The code grabs the following RSS URL and then parses and formats it. Change the province and city code as needed:

And here’s the code, which colourizes the current day using ANSI escape sequences:

#!/bin/sh
#
# Gets and parses the forecast from the Weather Office RSS
#

URL='https://weather.gc.ca/rss/city/ab-71_e.xml'
DAY=`date +%A`

curl -s $URL | grep title | cut -c 9- | \
sed -e 's,</title>,,g' \
-e 's,°, ,g' \
-e "s,$DAY,\x1B[1;34m$DAY\x1B[0m,g" \
-e "s,Current Conditions,\x1B[1;35mCurrent Conditions\x1B[0m,g" \
-e "s,minus ,-,g" \
-e "s,plus ,+,g" \

DNS-based mitigation for Samsung SwiftKey keyboard vulnerability

I was just listening to the discussion of the Samsung SwiftKey keyboard vulnerability from Security Now! episode 513, and I came up with a simple DNS-based mitigation that a user could implement to protect themselves.

The Vulnerability

Without any user interaction, the user’s phone makes a plaintext http GET request to a SwiftKey update server, and this request can be hijacked and malicious code injected into the phone by any man-in-the-middle bad actor. According to NowSecure, the discoverer of the vulnerability, the request looks like this:

GET http://skslm.swiftkey.net/samsung/downloads/v1.3-USA/az_AZ.zip

DNS-based Mitigation

With a rooted Android phone, a user could edit their /etc/hosts file to redirect the hostname of the update server (skslm.swiftkey.net) to localhost, preventing the http GET request from ever leaving the phone. In other words, the user is hijacking the request to the update server before a bad guy gets the opportunity to do the same.

With a non-rooted phone, there are DNS Resolver apps that can be installed that do the same kind of redirection to localhost.

Will this kind of mitigation work? Since I don’t have an Android phone to test against, this is just a thought experiment for myself.

Backup Your Dropbox Files with rdiff-backup

The Problem

Teresa and I use a single Dropbox account to share files between our computers. I also use the same account to store (and sync) plain-text notes on my iPad and iPhone (I use the apps PlainText and iA Writer). In case things go wrong with these apps, the syncing, or with Dropbox itself, I want to backup my Dropbox files and keep past snapshots of the backups so I can go back in time.

The Solution

rdiff-backup can do this. It is a command-line tool written in Python that:

…backs up one directory to another, possibly over a network. The target directory ends up a copy of the source directory, but extra reverse diffs are stored in a special subdirectory of that target directory, so you can still recover files lost some time ago. The idea is to combine the best features of a mirror and an incremental backup.

To make this all happen, I have Dropbox installed, signed-in, and running on my Linux desktop/server, which runs Ubuntu 11.04 Natty with Gnome 2.

Install rdiff-backup thusly:

    # aptitude install rdiff-backup

I use the directory /backup/ to hold all my backup targets, so I can run rdiff-backup like this:

    $ rdiff-backup  \
        --exclude $HOME/Dropbox/.dropbox \
        --exclude $HOME/Dropbox/.dropbox.cache \
        $HOME/Dropbox /backup/Dropbox

Every time I run rdiff-backup like this, it creates a new snapshot of my Dropbox files. Old snapshots are kept until I decide to purge them (if at all). To purge any snapshots older than two months, for example, I run this command:

    $ rdiff-backup --force --remove-older-than 2M /backup/Dropbox

I run the above two commands in an @hourly crontab script to keep this all happening automatically.

Browsing Past Snapshots

rdiff-backup has its own commands for digging into the files stored in the past snapshots, but it requires exactly knowing the filenames and backup times. Another tool, rdiff-backup-fs solves this problem by mounting the rdiff-backup backup directory as a FUSE filesystem, allowing me to grep and find my way through a directory tree of all snapshots.

After installing FUSE and rdiff-backup-fs, I mount my Dropbox snapshot tree with this command:

    $ rdiff-backup-fs ~/mnt /backup/Dropbox

Note that the order of the arguments for mounting source and target are backwards compared to the canonical mount command.

A long listing of my 10-oldest snapshots looks like this:

    $ ls -lF ~/mnt/ | head -10
    total 0
    dr-xr-xr-x 1 root root 4096 2013-03-10 13:52 2013-01-06T05:00:01/
    dr-xr-xr-x 1 root root 4096 2013-03-10 13:52 2013-01-06T06:00:01/
    dr-xr-xr-x 1 root root 4096 2013-03-10 13:52 2013-01-06T07:00:01/
    dr-xr-xr-x 1 root root 4096 2013-03-10 13:52 2013-01-06T08:00:01/
    dr-xr-xr-x 1 root root 4096 2013-03-10 13:52 2013-01-06T09:00:01/
    dr-xr-xr-x 1 root root 4096 2013-03-10 13:52 2013-01-06T10:00:01/
    dr-xr-xr-x 1 root root 4096 2013-03-10 13:52 2013-01-06T11:00:01/
    dr-xr-xr-x 1 root root 4096 2013-03-10 13:52 2013-01-06T12:00:01/
    dr-xr-xr-x 1 root root 4096 2013-03-10 13:52 2013-01-06T13:00:01/

I can then explore all my snapshots at once with any tools wish.

When done, I unmount the rdiff-backup-fs filesystem with:

    $ /bin/fusermount -u ~/mnt

Dell laptop BIOS update using FreeDOS and ISO Master

I recently needed to update the BIOS on a Dell Inspiron 630m laptop. The file, available from Dell support, is a DOS executable named MX51_A04.EXE.

I had two problems with this file: (1) Windows would not boot, so I couldn’t run the file using Windows. (2) The laptop had no floppy drive, so I couldn’t easily boot into DOS.

Now, one can solve this problem by booting from a FreeDOS LiveCD to run the file. But then you have to figure out how to get and run the MX51_A04.EXE file from within the FreeDOS environment. Various websites suggested methods using USB flash drives, but I couldn’t get this to work.

Instead, I was able to add the file to the LiveCD ISO image before I burnt the CD. Here’s how it worked:

  1. Download the FreeDOS LiveCD called fdfullcd.iso (153MB).
  2. Under Linux or Windows, install and run ISO Master.
  3. Load the fdfullcd.iso in ISO Master and then add the MX51_A04.EXE file to it.
  4. Save the modified ISO under a new name.
  5. Burn the modified ISO to a CD and boot from that.
  6. When you boot the laptop using this modified FreeDOS LiveCD, be sure to choose the LiveCD mode and not the install option.
  7. Once you have a DOS prompt, the command X: will switch you to the X: drive, where you’ll find the contents of the CD and the BIOS update file.
  8. Run it, cross your fingers, and reboot.

A Better Online Dictionary

[die.net]

I like using the online dictionary at die.net because it’s fast and clean.

It’s easy to query, also. Just append your word to the end of the URL. For example:

It describes itself as offering “free cross-referenced definitions, spelling correction, and word searches from WordNet, Webster’s, FOLDOC, and a variety of specialized sources.”

In the “spam” entry above, some of the sources include the Free On-Line Dictionary of Computing, the Virtual Entity of Relevant Acronyms, and the Jargon File.

Which editor should I learn?

On serverfault.com, Rory McCann asked, “What’s the best terminal editor to suggest to a Unix newbie? i.e. not vi or Emacs.”

This answer, which purposefully ignores the original poster’s restriction, says it best:

My take is still Emacs or vi. Even for a beginner.

Why?

Because time invested in learning an editor is productive only as long as you keep using that editor. All those less expressive options are poor choices for the long run, and will be abandoned eventually. At which point the time spent learning them is wasted, and the user still has to learn Emacs or vi.

In other words, the best (most expressive) tool for the job is one of Emacs or vi, and so you’ll eventually switch to one of them. It ultimately doesn’t matter which one you choose, but you would be smart to invest yourself into learning one of them.

For the record, I’m a vim user, and I love using it.

How to Disable Autosave in WordPress

The autosave feature in recent versions of WordPress (versions 2.5–2.7) is actually a misfeature:

… A misfeature is not a bug. Nor is it a simple unforeseen side effect; the term implies that the feature in question was carefully planned, but its long-term consequences were not accurately or adequately predicted (which is quite different from not having thought ahead at all).

The improper functioning of the WordPress autosave has bitten me several times. It’s supposed to prevent you from losing work by periodically saving your blog edits in the background, when in fact it has caused me to lose work by its very operation.

[WordPress Logo Inverted]

Basically, the most recent edits made to a blog entry often get dropped when you go to “Preview” or “Publish” the entry. In other words, during either of these two operations, it reverts you to what it had autosaved in the past and the new edits are lost. The frustrating thing is that most users would expect the “Preview” operation if not the “Publish” operation to properly save what’s in the edit box. So often, you might end up publishing an incomplete or incorrect version of your blog entry without even knowing it.

This is madness. Let’s stop it.

Find the following four files in the wp-admin/ directory of your WordPress installation:

  1. page-new.php
  2. page.php
  3. post-new.php
  4. post.php

and comment out the following line:

wp_enqueue_script('autosave')

by changing it to:

//wp_enqueue_script('autosave')

This will disable the autosave feature in the WordPress user interface.

A secondary part of the solution, too, is to always hit “Save Draft” before hitting “Preview”. I’m not sure if this is strictly necessary, but now I’m paranoid.

Thanks to Allen Day and William Lone for showing me how to do this.