LVM: Working with Logical Volume Management
Introduction
Logical Volume Management (LVM) is a method and software for partitioning, concatenating, and utilizing storage spaces on a server. It allows flexible management, security, and online optimization of storage spaces in UNIX/Linux-type operating systems.
We also refer to it as Volume Manager.
Since LVM is not very simple to use, and since I don’t handle it every day either, I thought a small documentation was essential. I’ll fill it in as needed.
Here are the main files and folders used by LVM:
File/Folder | Description |
---|---|
/etc/lvm/lvm.conf | LVM configuration file |
/etc/lvm/cache.cache | Device name cache file |
/etc/lvm/backup | Folder containing automatic backups of VG metadata |
/etc/lvm/archive | Folder containing automatic archives of VG metadata |
/var/lock/lvm | Lock file to prevent simultaneous execution of multiple LVM tools, avoiding metadata corruption |
Creating a Partition
When you use fdisk to create a partition and assign it as LVM, it may not appear in the devices. To avoid rebooting to see it, simply run this command on the disk in question (sda for example):
|
|
If you then need to add it to the fstab, it’s preferable to use UUIDs. To locate them, there’s a command:
|
|
All you have to do is add a line like this to the fstab (/etc/fstab
):
|
|
Usage
Activating an LVM Partition
To activate an LVM partition, we’ll use pvcreate:
|
|
You can then see the status with the pvdisplay command.
Creating a Volume Group
We must then create a VG if we want to be able to create volumes:
|
|
You should put the name of the VG you want here instead of ‘my_vg’.
Creating Volumes
To create volumes:
|
|
So you need:
- my_lv1: the desired LV name
- -L 5G: the volume group size
- my_vg: the name of the VG on which the LV should be stored.
Resizing
Change block storage size (optional)
You may need to update the partition block size after updating the partition table. First get growpart
:
|
|
Now get the partition table:
|
|
Then run the following command to resize the partition (3 in this case):
|
|
This command will resize the third partition on the /dev/sda
disk. You can change the partition number and disk name as needed.
Increasing Size
To increase the size of a partition:
|
|
Then you just need to resize the filesystem (xfs_grow for example for XFS)
Reducing Size
Decreasing the size of a filesystem is a bit more delicate. Indeed, if you make the mistake of decreasing the size of the logical volume before reducing the content size (the filesystem itself), then you destroy your filesystem… same if you reduce the logical volume size too much.
To avoid any risk, I recommend using the following method (a bit longer than normal, but much more reliable):
- Reduce the filesystem size more than necessary
- Reduce the logical volume size to give it exactly the new desired size.
- Enlarge the filesystem so that it occupies all available space.
This way, the risk of error is much lower.
Reducing the FS
If you don’t want to do this operation manually, use a live CD with gparted included.
Caution, not all filesystems can be “reduced”. For ext3 and reiserfs, it works very well. Here’s an example with reiserfs…
|
|
In this example, the “ca” volume is in the volume group svg. On this logical volume exists a reiserfs filesystem of 512 MB size. However, I only use 230 MB. Moreover, I know I’ll never add anything to this volume. So I want to decrease its size to 256 MB (to leave a safety margin, and because it makes a round number ;) I start by unmounting the filesystem:
|
|
Then I will reduce the size of the filesystem, more than necessary. Rather than remove 256 MB, I’ll remove 258. I can do this because there’s 283 MB free… Obviously, removing more space than remains would be suicidal…
|
|
CAUTION: If you’re using ext3, you can’t indicate the amount of space to remove, you must give the final desired size (512-258). The right command would have been:
|
|
It’s possible that it asks you to run this command before. If that’s the case, do it:
|
|
Reducing the LV
In case you don’t see the LV in /dev/mapper, you’ll need to activate it:
|
|
Note: to deactivate an LV, change the “y” to “n”.
Now that the filesystem has decreased, we need to give the logical volume its new size, 256 MB instead of 512:
|
|
Just one last step, we tell the filesystem that it can automatically expand to take all available space. It should therefore be able to grow by 2 MB. It will find the exact size in blocks etc. on its own… We didn’t take the risk of making an error by reducing it “exactly” to the same size as the logical volume, because the slightest error could have corrupted the filesystem by a few blocks.
|
|
or, if you’re using ext3:
|
|
All that’s left is to remount the filesystem:
|
|
It’s done… The filesystem is now 256 MB, and we still have our 230 MB of data inside. Conclusion: Playing with the size of logical volumes works very well, you just have to take your time and not do anything silly :)
Volume Recovery
If I’m in recovery mode, and I want to mount my filesystems, how do I do it?
Make sure the module is properly loaded:
|
|
Scan all LVMs:
|
|
And mount the LV:
|
|
Once finished, you can deactivate your volume:
|
|
Exporting/Importing a VG
- You can completely unmount a volume group like this:
|
|
This will ensure that nothing is mounted and active on your machine.
- To remount it, it’s quite simple:
|
|
Then activate the vg to display the volumes:
|
|
Scanning New LVM Volumes
You can verify that there’s a new volume by scanning the PVs:
|
|
The new volumes will then appear.
Disk Replacement
If you have a VG with multiple disks in it. One of them is defective and you’ve added its replacement to the VG. There is a hot solution to remove the defective disk from the VG without data loss:
|
|
All that’s left is to remove the disk.
Controlling Volume Visibility
You can disable visibility of a volume like this:
|
|
You can enable visibility of a volume this way:
|
|
You can force an lv to be read-only accessible:
|
|
Or writable:
|
|
Snapshot
Creating a Snapshot
To create a snapshot of an existing LV:
|
|
Here I just created a 4G Snapshot.
Deleting the Snapshot
If I’m satisfied with the changes made, I want to delete the snapshot:
|
|
Rolling Back Changes
If I’m not satisfied with the changes and want to roll back from the snapshot, I’ll need to unmount the partition and revert:
|
|
The old partition will be remounted at the right place.
Dumping the Configuration
You may need to dump the LVM configuration. To do this, run:
|
|
Viewing Locks
To view current locks on LVMs, use this command:
|
|
Hot FSCK on ext3/4
You’ve dreamed of it, right? It is indeed possible to do hot fsck thanks to LVM snapshots. Here’s a small script that allows scanning all ext3 and ext4 type LVs and defragmenting them (Note: You need 10% free space compared to the largest LV in your VGs):
|
|
Having a Nice GUI
Well, for LVM and Red Hat connoisseurs, you certainly know the system-config-lvm
command. For Debian it would be cool if we could have it! And well, bibi got the solution:
|
|
All that’s left is to launch and say “woooow”:
|
|
FAQ
I Have a UUID Conflict
If you have the misfortune of running into a UUID conflict, you can regenerate one via the following command:
|
|
Resources
Last updated 15 Sep 2014, 11:24 CEST.