LVM manages disk drives. It allows multiple drives and partitions to be combined into larger volume groups, assists in making backups through a snapshot, and allows for dynamic volume resizing. It can also provide mirroring similar to a RAID 1 array.
A complete discussion of LVM is beyond the scope of this introduction, but basic concepts are presented below.
To run any of the commands presented here, the LVM2-2.03.27 package must be
installed. All commands must be run as the root
user.
Management of disks with lvm is accomplished using the following concepts:
These are physical disks or partitions such as /dev/sda3 or /dev/sdb.
These are named groups of physical volumes that can be manipulated by the administrator. The number of physical volumes that make up a volume group is arbitrary. Physical volumes can be dynamically added or removed from a volume group.
Volume groups may be subdivided into logical volumes. Each logical volume can then be individually formatted as if it were a regular Linux partition. Logical volumes may be dynamically resized by the administrator according to need.
To give a concrete example, suppose that you have two 2 TB disks.
Also suppose a really large amount of space is required for a very
large database, mounted on /srv/mysql
.
This is what the initial set of partitions would look like:
Partition Use Size Partition Type
/dev/sda1 /boot 100MB 83 (Linux)
/dev/sda2 / 10GB 83 (Linux)
/dev/sda3 swap 2GB 82 (Swap)
/dev/sda4 LVM remainder 8e (LVM)
/dev/sdb1 swap 2GB 82 (Swap)
/dev/sdb2 LVM remainder 8e (LVM)
First initialize the physical volumes:
pvcreate /dev/sda4 /dev/sdb2
A full disk can be used as part of a physical volume, but beware that the pvcreate command will destroy any partition information on that disk.
Next create a volume group named lfs-lvm:
vgcreate lfs-lvm /dev/sda4 /dev/sdb2
The status of the volume group can be checked by running the command vgscan. Now create the logical volumes. Since there is about 3900 GB available, leave about 900 GB free for expansion. Note that the logical volume named mysql is larger than any physical disk.
lvcreate --name mysql --size 2500G lfs-lvm lvcreate --name home --size 500G lfs-lvm
Finally the logical volumes can be formatted and mounted. In this example, the jfs file system (jfsutils-1.1.15) is used for demonstration purposes.
mkfs -t ext4 /dev/lfs-lvm/home mkfs -t jfs /dev/lfs-lvm/mysql mount /dev/lfs-lvm/home /home mkdir -p /srv/mysql mount /dev/lfs-lvm/mysql /srv/mysql
It may be needed to activate those logical volumes, for them to
appear in /dev
. They can all be
activated at the same time by issuing, as the root
user:
vgchange -a y
A LVM logical volume can host a root filesystem, but requires the use
of an initramfs (initial RAM file system). The initramfs proposed in
the
section called “About initramfs” allows to pass the lvm volume in
the root=
switch of the
kernel command line.
If not using an initramfs, there is a race condition in systemd preventing mounting logical volumes
through /etc/fstab
. You must create a
“mount” unit
(see systemd.mount(5))
as in the following example, which mounts the /home
directory automatically at boot:
cat > /etc/systemd/system/home.mount << EOF
[Unit]
Description=Mount the lvm volume /dev/lfs-lvm/home to /home
[Mount]
What=/dev/lfs-lvm/home
Where=/home
Type=ext4
Options=default
[Install]
WantedBy=multi-user.target
EOF
The name of the unit must be the name of the mount point with the `/' character replaced by `-', omitting the leading one.
Next the unit must be enabled with:
systemctl enable home.mount
For more information about LVM, see the LVM HOWTO and the lvm man pages. A good in-depth guide is available from RedHat®, although it makes sometimes reference to proprietary tools.