LinuxDevCenter.com

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things Linux across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to Linux Subscribe to Newsletters

Managing Disk Space with LVM
Pages: 1, 2, 3, 4

Growth and Reallocation

Suppose that over the next year, the storage system fills up and needs to be expanded. Initially, you can begin allocating the unallocated space. For instance, to increase the amount of space available for shared files from 10GB to 15GB, run a command such as:



# lvextend -L15G /dev/datavg/sharelv
# resize_reiserfs /dev/datavg/sharelv

But over time, all the unallocated disk space will be used. One solution is to replace the four 250G drives with larger 800G ones.

In the case where you use RAID 1, migration is straightforward. Use mdadm to mark one drive of each of the RAID 1 mirrors as failed, and then remove them:

# mdadm --manage /dev/md0 --fail /dev/sda1
# mdadm --manage /dev/md0 --remove /dev/sda1
# mdadm --manage /dev/md0 --fail /dev/sdc1
# mdadm --manage /dev/md0 --remove /dev/sdc1

Pull out the sda and sdc hard drives and replace them with two of the new 800G drives. Split each 800G drive into a 250G partition and a 550G partition using fdisk, and add the partitions back to md0 and md1:

# fdisk /dev/sda
# fdisk /dev/sdc
# mdadm --manage /dev/md0 --add /dev/sda1
# mdadm --manage /dev/md1 --add /dev/sdc1

Repeat the above process with sdd and sdb to move them to the other two new drives, then create a third and fourth RAID device, md2 and md3, using the new space:

# mdadm --create /dev/md2 -a -l 1 -n 2 /dev/sda2 /dev/sdd2
# mdadm --create /dev/md3 -a -l 1 -n 2 /dev/sdb2 /dev/sdc2

Finally, add these to LVM:

# pvcreate /dev/md2 /dev/md3
# vgextend datavg /dev/md2 /dev/md3

The file server now has 1.6TB of fully redundant storage.

LVM and Desktops

So far, we've talked only about LVM and RAID for secondary disk space via a standalone file server, but what if you want to use LVM to manage the space on a regular desktop system? It can work, but there are some considerations to take into account.

First, the installation and upgrade procedures for some Linux distributions don't handle RAID or LVM, which may present complications. Many of today's distros do support it, and even provide tools to assist in creating and managing them, so check this first.

Second, having the root filesystem on LVM can complicate recovery of damaged file systems. Because boot loaders don't support LVM yet, you must also have a non-LVM /boot partition (though it can be on a RAID 1 device).

Third, you need some spare unallocated disk space for the new LVM partition. If you don't have this, use parted to shrink your existing root partition, as described in the LVM HOWTO.

For this example, assume you have your swap space and /boot partitions already set up outside of LVM on their own partitions. You can focus on moving your root filesystem onto a new LVM partition in the partition /dev/hda4. Check that the filesystem type on hda4 is LVM (type 8e).

Initialize LVM and create a new physical volume:

# vgscan
# pvcreate /dev/hda4
# vgcreate rootvg /dev/hda4

Now create a 5G logical volume, formatted into an xfs file system:

# lvcreate rootvg ---name rootlv -size 5G
# mkfs.xfs /dev/rootvg/rootlv

Copy the files from the existing root file system to the new LVM one:

# mkdir /mnt/new_root
# mount /dev/rootvg/rootlv /mnt/new_root
# cp -ax /. /mnt/new_root/

Next, modify /etc/fstab to mount / on /dev/rootvg/root instead of /dev/hda3.

The trickiest part is to rebuild your initrd to include LVM support. This tends to be distro-specific, but look for mkinitrd or yaird. Your initrd image must have the LVM modules loaded or the root filesystem will not be available. To be safe, leave your original initrd image alone and make a new one named, for example, /boot/initrd-lvm.img.

Finally, update your bootloader. Add a new section for your new root filesystem, duplicating your original boot stanza. In the new copy, change the root from /dev/hda3 to /dev/rootvg/rootlv, and change your initrd to the newly built one. If you use lilo, be sure to run lilo once you've made the changes. For example, with grub, if you have:

title=Linux
  root (hd0,0)
  kernel /vmlinuz root=/dev/hda3 ro single
  initrd /initrd.img

add a new section such as:

title=LinuxLVM

  root (hd0,0)
  kernel /vmlinuz root=/dev/rootvg/root ro single
  initrd /initrd-lvm.img

Conclusion

LVM is only one of many enterprise technologies in the Linux kernel that has become available for regular users. LVM provides a great deal of flexibility with disk space, and combined with RAID 1, NFS, and a good backup strategy, you can build a bulletproof, easily managed way to store, share, and preserve any quantity of files.

Bryce Harrington is a Senior Performance Engineer at the Open Source Development Labs in Beaverton, Oregon.

Kees Cook is the senior network administrator at OSDL.


Return to the Linux DevCenter.


Has LVM saved your life? Do you have a tip to share? It's discussion time.
You must be logged in to the O'Reilly Network to post a talkback.
Post Comment
Full Threads Oldest First

Showing messages 1 through 11 of 11.

  • simple logical volume extension
    2008-01-29 16:54:01  dustinkirkland [Reply | View]

    Good article, guys. I've used this guide as a reference on several occasions. Thanks.

    I ran across a simple use case that wasn't explicitly covered in the article, but I was able to piece together the required commands from various examples in the article. I thought I'd share concisely.

    Given:

    • A single disk (/dev/sda), partitioned to a Physical Volume (/dev/sda2) in an active Volume Group (VolGroup00), and allocated to a Logical Volume (/dev/VolGroup00/LogVol00)

    • An active filesystem (ext3) mounted (/) on that Logical Volume, where more disk space is required

    • No RAID involved



    Wanted:

    • To add disk space (/dev/sdb) to the mounted filesystem (/)



    Procedure:

    • Partition the disk (/dev/sdb1), mark as type 8e

      # fdisk /dev/sdb


    • Create the physical volume

      # pvcreate /dev/sdb1


    • Extend the volume group by adding the new physical volume

      # vgextend VolGroup00 /dev/sdb1


    • Check the new volume group size

      # vgdisplay | grep "VG Size"


    • Extend the logical volume to the new total available

      # lvextend -L 28G /dev/VolGroup00/LogVol00


    • Find the filesystem mount point

      # mount


    • Perform an online resize of the filesystem to the max available

      # ext2online -v /dev/mapper/VolGroup00-LogVol00


    • Now check your disk free

      # df -h


  • Steve Mallett photo lvcreate --name medialv --size 400G
    2007-11-01 15:09:42  Steve Mallett | O'Reilly Blogger [Reply | View]

    I had to add the name of my volume group to the end of this:

    lvcreate --name medialv --size 400G "name of vg"

    for it to work.
  • Umounting /usr
    2006-05-02 17:10:16  pretty-boy [Reply | View]

    I have been using LVM on the desktop for quite a while for many of the reasons given in the article.

    One problem that has arisen lately is trying to change (increase) the size of /usr using LVM and resizing the fs.

    Recent version of bash now link into the glibc locale funstions. These in turn have links back into /usr/lib/locale. This means that even when pulling the system down to single user, as root you cannot umount /usr. And therefore you cannot resize it without rebooting into a rescue system.

    Unix type systems have had the advantage of not requiring numerous reboots for system admin - but this seems to be changing.
  • Alternate RAID-1 migration
    2006-04-30 19:04:48  jrzagar [Reply | View]

    Instead of ending up with four raid-1 devices, wouldn't it be easier to do the following?

    Use mdadm to mark one drive of each of the RAID 1 mirrors as failed, and then remove them:

    # mdadm --manage /dev/md0 --fail /dev/sda1
    # mdadm --manage /dev/md0 --remove /dev/sda1
    # mdadm --manage /dev/md0 --fail /dev/sdc1
    # mdadm --manage /dev/md0 --remove /dev/sdc1

    Pull out the sda and sdc hard drives and replace them with two of the new 800G drives. Create 800G partitions on each drive and create md2:

    # fdisk /dev/sda
    # fdisk /dev/sdc
    # mdadm --create /dev/md2 --level=1 \
    --raid-devices=2 /dev/sd[ac]1--add /dev/sda1

    Once you've activated the array, you can extend the volume group, move the logical volumes to the new drives, and remove the old raid arrays from the volume group:

    # vgextend datavg /dev/md2
    # pvmove /dev/md0 /dev/md2
    # vgreduce datavg /dev/md0
    # pvmove /dev/md1 /dev/md2
    # vgreduce datavg /dev/md1

    Now you can replace the hard drives for md[01], delete md0, recreate md1 with the second pair of 800G drives, and add the new md1 to datavg.
    • Alternate RAID-1 migration
      2006-04-30 23:37:05  bryceharrington [Reply | View]

      Yes, there's definitely a few approaches to doing this, and like kees said, it's going to be driven by personal preference.

      Note that part of the reason we picked this approach was just that it could be explained more concisely than other options we were considering.
      • Alternate RAID-1 migration
        2006-05-11 14:40:36  gr34t [Reply | View]

        "Note that part of the reason we picked this approach was just that it could be explained more concisely than other options we were considering."

        i agree with you my friend!!! take a look here:

        http://www.filesharing-p2p.to/archive/english-subforum/p2p-news/dmca-20-t-41626.html
      • Alternate RAID-1 migration
        2006-05-02 05:43:19  Tormak [Reply | View]

        Bryce, you should let people know that how you've done it is going to be performance impacting due to device contention.
        (As a performance engineer I'm sure you know this ;)

        The parent's method is a much better approach.
    • Alternate RAID-1 migration
      2006-04-30 19:45:07  keescook [Reply | View]

      Excellent observation! :)

      Doing it this way or the other is a matter of preference. Some people like maintaining the size of the original component devices, and some like maintaining the number of the original component devices.

      For the purposes of the article, showing how to end up with more devices seemed to serve as a better teaching example, as it tangentially demonstrates how to just plain add more disks. :)
  • Root filesystem on LVM should be considered dangerous
    2006-04-30 18:28:19  jrzagar [Reply | View]

    The main advantage of having the root filesystem under LVM is that you can easily migrate your root filesystem to a new hard drive once you've added it to the volume group.

    However, having the root filesystem under LVM creates new modes of failure that are extremely difficult to recover from...

    The scenario quoted by the author, a corrupted filesystem, can be handled through an Ubuntu Live CD as it supports LVM and you will be able to fix broken filesystems.

    A more catastrophic failure would be if the volume group itself became corrupted. How would you go about fixing a filesystem in a VG when the VG itself can't be activated? The answer is: with extreme difficulty, if it's possible at all.

    My recommendations would be :
    1) avoid using LVM for the root filesystem (recovery==easy),
    2) if root is under LVM, don't let that VG span multiple disk devices (recovery==hard), and
    3) if the VG *does* have multiple devices, don't let the logical volume with the root filesystem span more than one device (recovery==extremely difficult, but still possible).

    Violate all three of these recommendations, and you're stuck doing a sector-by-sector forensic reconstruction to get your data back.
    • Root filesystem on LVM should be considered dangerous
      2006-04-30 19:38:41  keescook [Reply | View]

      Yup, adding extra layers will always make dealing with failures more complex. I find the flexibility outweighs the complexity, though. That said, I personally don't recommend ever running without mirroring, especially on a root filesystem. And, I recommend doing regular backups with something nice like dirvish (http://dirvish.org/).
  • small correction: pvcreate
    2006-04-29 08:50:22  keescook [Reply | View]

    The example given for the very first "pvcreate" command has a typo. It should read:

    pvcreate /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1

    Note the partition number after the drive name, without this, you won't benefit from having the partition type detected at boot-time.


Tagged Articles

Post to del.icio.us

This article has been tagged:

linux

Articles that share the tag linux:

Managing Disk Space with LVM (74 tags)

Use Your Digital Camera with Linux (60 tags)

mdadm: A New Tool For Linux Software RAID Management (59 tags)

Asterisk: A Bare-Bones VoIP Example (43 tags)

View All

lvm

Articles that share the tag lvm:

Managing Disk Space with LVM (56 tags)

View All

storage

Articles that share the tag storage:

Managing Disk Space with LVM (37 tags)

Keeping Your Life in Subversion (4 tags)

Speeding up Linux Using hdparm (3 tags)

Using Amazon S3 from Perl (3 tags)

A First Look at the Kowari Triplestore (2 tags)

View All

howto

Articles that share the tag howto:

Rolling with Ruby on Rails (258 tags)

From Weblog to CMS with WordPress (98 tags)

Top Ten Digital Photography Tips (92 tags)

Top Ten Mac OS X Tips for Unix Geeks (79 tags)

View All

sysadmin

Articles that share the tag sysadmin:

Building a FreeBSD Build System (30 tags)

Best Windows Admin Downloads (30 tags)

Managing Disk Space with LVM (26 tags)

The Ultimate Free Windows Toolkit (21 tags)

Six Things First-Time Squid Administrators Should Know (20 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com