Learning Linux LVM, Part 1

From Funtoo
Jump to: navigation, search

Storage management magic with Logical Volume Management

Support Funtoo and help us grow! Donate $15 per month and get a free SSD-based Funtoo Virtual Container.

LVM intro

In this series, I'm going to show you how to install and use the new Logical Volume Management support built-in to the Linux kernel. If you've never used a form of LVM before, you're in for a treat; it's a wonderful technology. Before we actually get LVM up and running, I'm going to explain exactly what it is and how it works. Then, we'll be ready to test out LVM and get the most out of it.

If you're like me, then your experience with UNIX and Linux began on a PC platform, rather than on large, commercial UNIX servers and workstations. On the basic PC, we've always had to deal with partitioning our hard drives. PC people are generally well-acquainted with tools such as fdisk, which are used to create and delete primary and extended partitions on hard disks. Hard disk partitioning is an annoying but accepted part of the process of getting an operating system up and running.

Hard drive partitioning can be annoying because to do a good job you really need to accurately estimate how much space you'll need for each partition. If you make a poor estimation, your Linux system could possibly be crippled -- to fix the problem, it's possible that you might even need to perform a full system backup, wipe your hard drives clean, and then restore all your data to a new (and presumably better) partition layout. Ick! These are exactly the kinds of situations that sysadmins try their best to avoid in the first place.

While partitions were once static storage regions, thankfully, we now have a proliferation of PC repartitioning tools (PowerQuest's Partition Magic product is one of the most popular). These tools allow you to boot your system with a special disk and dynamically resize your partitions and filesystems. Once you reboot, you have newly resized partitions, hopefully getting you out of your storage crunch. These partition resizing tools are great and solve the problem storage management for some. But are they perfect? Not exactly.

Tools like Partition Magic are great for workstations, but aren't really adequate for servers. First of all, they require you to reboot your system. This is something most sysadmins desperately try to avoid doing. What if you simply can't reboot your machine every time your storage needs change, such as if your storage needs change dramatically on a weekly basis? What happens if you need to expand a filesystem so that it spans more than one hard drive, or what do you do if you need to dynamically expand or shrink a volume's storage capacity while allowing Apache to continue to serve Web pages? In a highly available, dynamic environment, a basic partition resizer just won't work. For these and other situations, Logical Volume Management is an excellent (if not perfect) solution.

Enter LVM

Now, let's take a look at how LVM solves these problems. To create an LVM logical volume, we follow a three-step process. First, we need to select the physical storage resources that are going to be used for LVM. Typically, these are standard partitions but can also be Linux software RAID volumes that we've created. In LVM terminology, these storage resources are called "physical volumes". Our first step in setting up LVM involves properly initializing these partitions so that they can be recognized by the LVM system. This involves setting the correct partition type if we're adding a physical partition, and running the pvcreate command.

Once we have one or more physical volumes initialized for use by LVM, we can move on to step two -- creating a volume group. You can think of a volume group as a pool of storage that consists of one or more physical volumes. While LVM is running, we can add physical volumes to the volume group or even remove them. However, we can't mount or create filesystems on a volume group directly. Instead, we can tell LVM to create one or more "logical volumes" using our volume group storage pool:

A volume group is created out of physical volumes

Creating an LVM logical volume is really easy, and once it's created we can go ahead and put a filesystem on it, mount it, and start using the volume to store our files. To create a logical volume, we use the lvcreate command, specifying the name of our new volume, the size we'd like the volume to be, and the volume group that we'd like this particluar logical volume to be part of. The LVM system will then allocate storage from the volume group we specify and create our new volume, which is now ready for use. Once created, we can put an ext2 or ReiserFS filesystem on it, mount it, and use it as we like.

Creating two logical volumes from our existing volume group


Behind the scenes, the LVM system allocates storage in equal-sized "chunks", called extents. We can specify the particular extent size to use at volume group creation time. The size of an extent defaults to 4Mb, which is perfect for most uses. One of the beauties of LVM is that the physical storage locations of the extents used for one of our logical volumes (in other words, what disk they're stored on) can be dynamically changed while our logical volume is mounted and in use! The LVM system ensures that our logical volumes continue to operate perfectly while allowing the administrator to physically change where everything is stored.

Of course, since everything is created out of equally-sized extents, it's really easy to allocate some additional extents for an already-existing logical volume -- in other words, dynamically "grow" the volume:

Adding additional extents from our volume group, expanding the size of our logical volume.

Once the logical volume has been expanded, you can then expand your ext2 or ReiserFS filesystem to take advantage of this new space. If you use a program such as resize_reiserfs, this filesystem expansion can also happen while the volume is mounted and being used! Truly amazing -- with LVM and online filesystem expansion utilties, it's no longer necessary to reboot your system or even drop to runlevel 1 to change your storage configuration.

The only time you need to shut down your system is when you need to add new physical disks. Once new disks have been added, you then can add these new physical volumes to your volume group(s) to create a fresh supply of extents.

Setting up LVM

OK, let's get LVM installed. LVM consists of two parts: a kernel part and a suite of user-space tools. You may already have LVM support available on your system, and if not, it's a simple matter to install the appropriate tools using your distribution's package manager. On Gentoo or Funtoo Linux, this is done as follows:

# emerge sys-fs/lvm2

If you compiled your kernel manually, you'll want to reconfigure and compile your kernel so that LVM support is enabled.

# cd /usr/src/linux
# make menuconfig

You'll find the LVM options under the "Multiple devices driver support (RAID and LVM)" section, under "Device Drivers":

Under Device Drivers-->Multiple devices driver support (RAID and LVM):

--- Multiple devices driver support (RAID and LVM)                     
{M}   RAID support                                                     
<M>     Linear (append) mode                                           
<M>     RAID-0 (striping) mode                                         
-M-     RAID-1 (mirroring) mode                                        
-M-     RAID-10 (mirrored striping) mode                               
-M-     RAID-4/RAID-5/RAID-6 mode                                      
<M>     Multipath I/O support                                          
<M>     Faulty test module for MD                                      
<M>   Block device as cache                                            
[ ]     Bcache debugging                                               
[ ]     Debug closures                                                 
<M>   Device mapper support                                            
[ ]     Device mapper debugging support                                
[ ]     Keep stack trace of persistent data block lock holders         
<M>     Crypt target support                                           
<M>     Snapshot target                                                
<M>     Thin provisioning target                                       
<M>     Cache target (EXPERIMENTAL)                                    
<M>       MQ Cache Policy (EXPERIMENTAL)                               
<M>       Cleaner Cache Policy (EXPERIMENTAL)                          
<M>     Era target (EXPERIMENTAL)                                      
<M>     Mirror target                                                  
<M>       Mirror userspace logging                                     
<M>     RAID 1/4/5/6/10 target                                         
<M>     Zero target                                                    
<M>     Multipath target                                               
<M>       I/O Path Selector based on the number of in-flight I/Os      
<M>       I/O Path Selector based on the service time                  
<M>     I/O delaying target                                            
[*]     DM uevents                                                     
<M>     Flakey target                                                  
<M>     Verity target support                                          
<M>     Switch target support (EXPERIMENTAL)

I recommend enabling all features. Also ensure that LVM is enabled for your initramfs. Remember that if you are putting any main filesystems on LVM, you will need LVM compiled into your kernel, rather than a module, or you'll need an LVM-aware initramfs.

Also be sure to enable any necessary startup scripts to initialize LVM. On many distributions, including Funtoo Linux, this is done for you, provided that sufficient kernel support is available. The basic commands that these scripts will run are the following, at boot:

/sbin/vgchange -a y

These lines will scan for all available volume groups and activate them. At shutdown, something like this will run:

/sbin/vgchange -a n

While this stuff is handled for you automatically, if you ever boot from a rescue CD or USB stick, you may need to type vgscan and vgchange -a y as root before your logical volumes are available for use.

That's it for this article. Next article, I'll show you how to create your own logical volumes and unleash the power of LVM. I'll see you then!


Read the next article in this series: Learning Linux LVM, Part 2


Browse all our available articles below. Use the search field to search for topics and keywords in real-time.

Article Subtitle
Article Subtitle
Awk by Example, Part 1 An intro to the great language with the strange name
Awk by Example, Part 2 Records, loops, and arrays
Awk by Example, Part 3 String functions and ... checkbooks?
Bash by Example, Part 1 Fundamental programming in the Bourne again shell (bash)
Bash by Example, Part 2 More bash programming fundamentals
Bash by Example, Part 3 Exploring the ebuild system
Funtoo Filesystem Guide, Part 1 Journaling and ReiserFS
Funtoo Filesystem Guide, Part 2 Using ReiserFS and Linux
Funtoo Filesystem Guide, Part 3 Tmpfs and Bind Mounts
Funtoo Filesystem Guide, Part 4 Introducing Ext3
Funtoo Filesystem Guide, Part 5 Ext3 in Action
GUID Booting Guide
Learning Linux LVM, Part 1 Storage management magic with Logical Volume Management
Learning Linux LVM, Part 2 The cvs.gentoo.org upgrade
Linux Fundamentals, Part 1
Linux Fundamentals, Part 2
Linux Fundamentals, Part 3
Linux Fundamentals, Part 4
Making the Distribution, Part 1
Making the Distribution, Part 2
Making the Distribution, Part 3
Maximum Swappage Getting the most out of swap
On screen annotation Write on top of apps on your screen
OpenSSH Key Management, Part 1 Understanding RSA/DSA Authentication
OpenSSH Key Management, Part 2 Introducing ssh-agent and keychain
OpenSSH Key Management, Part 3 Agent Forwarding
Partition Planning Tips Keeping things organized on disk
Partitioning in Action, Part 1 Moving /home
Partitioning in Action, Part 2 Consolidating data
POSIX Threads Explained, Part 1 A simple and nimble tool for memory sharing
POSIX Threads Explained, Part 2
POSIX Threads Explained, Part 3 Improve efficiency with condition variables
Sed by Example, Part 1
Sed by Example, Part 2
Sed by Example, Part 3
The Gentoo.org Redesign, Part 1 A site reborn
The Gentoo.org Redesign, Part 2 The Documentation System
The Gentoo.org Redesign, Part 3 The New Main Pages
The Gentoo.org Redesign, Part 4 The Final Touch of XML
Traffic Control
Windows 10 Virtualization with KVM
X Window System