LVM Lecturs
LVM Lecturs
Storage volumes created under the control of the logical volume manager can be
resized and moved around almost at will, although this may need some upgrading of
file system tools.
You can think of LVM as "dynamic partitions", meaning that you can
create/resize/delete LVM "partitions" (they're called "Logical Volumes" in LVM-speak)
from the command line while your Linux system is running: no need to reboot the
system to make the kernel aware of the newly-created or resized partitions
2
Benefits
If you have more than one hard-disk, Logical Volumes can extend over more than one
disk: i.e., they are not limited by the size of one single disk, rather by the total
aggregate size.
You can set up "striped" LVs, so that I/O can be distributed to all disks hosting the LV in
parallel. (Similar to RAID-0, but a bit easier to set-up.)
You can create a (read-only) snapshot of any LV. You can revert the original LV to the
snapshot at a later time, or delete the snapshot if you no longer need it. This is handy
for server backups for instance (you cannot stop all your applications from writing, so
you create a snapshot and backup the snapshot LV), but can also be used to provide a
"safety net" before a critical system upgrade (clone the root partition, upgrade, revert
if something went wrong).
3
Physical Volumes
To use the device for an LVM logical volume the device must be initialized as a physical
volume (PV). Initializing a block device as a physical volume places a label near the
start of the device.
By default, the LVM label is placed in the second 512-byte sector. You can overwrite
this default by placing the label on any of the first 4 sectors.
An LVM label provides correct identification and device ordering for a physical device,
since devices can come up in any order when the system is booted. An LVM label
remains persistent across reboots and throughout a cluster.
5
Volume Groups
Physical volumes are combined into volume groups (VGs). This creates a pool of disk
space out of which logical volumes can be allocated.
Within a volume group, the disk space available for allocation is divided into units of a
fixed-size called extents. An extent is the smallest unit of space that can be allocated,
Within a physical volume, extents are referred to as physical extents.
6
In LVM, a volume group is divided up into logical volumes. There are three
types of LVM logical volumes:
Linear Volumes
When you write data to an LVM logical volume, the file system lays the data
out across the underlying physical volumes. You can control the way the
data is written to the physical volumes by creating a striped logical volume.
For large sequential reads and writes, this can improve the efficiency of the
data I/O.
Striping enhances performance by writing data to a predetermined number
of physical volumes in round-round fashion. With striping, I/O can be done in
parallel. In some situations, this can result in near-linear performance gain
for each additional physical volume in the stripe.
9
A mirror maintains identical copies of data on different devices. When data is written to one
device, it is written to a second device as well, mirroring the data. This provides protection
for device failures. When one leg of a mirror fails, the logical volume becomes a linear
volume and can still be accessed. LVM supports mirrored volumes. When you create a
mirrored logical volume, LVM ensures that data written to an underlying physical volume is
mirrored onto a separate physical volume. With LVM, you can create mirrored logical
volumes with multiple mirrors.
11
Snapshot Volumes
The LVM snapshot feature provides the ability to create virtual images of a device
at a particular instant without causing a service interruption. When a change is
made to the original device (the origin) after a snapshot is taken, the snapshot
feature makes a copy of the changed data area as it was prior to the change so
that it can reconstruct the state of the device. LVM snapshots are not supported
across the nodes in a cluster. You cannot create a snapshot volume in a clustered
volume group. LVM snapshots are not supported for LVM mirrored logical
volumes.
13
The following command initializes /dev/sdd1, /dev/sde1, and /dev/sdf1 for use as
LVM physical volumes.
pvcreate /dev/sdd1 /dev/sde1 /dev/sdf1
14
There are two commands you can use to display properties of LVM volume groups:
vgs and vgdisplay.
•You can also use this command to change several volume group parameters for an
existing volume group. The following command changes the maximum number of
logical volumes of volume group vg00 to 128.
•vgchange -l 128 /dev/vg00
17
•When you create a volume group it is, by default, activated. This means that the
logical volumes in that group are accessible and subject to change.
•There are various circumstances for which you you need to make a volume group
inactive and thus unknown to the kernel. To deactivate or activate a volume group,
use the -a (--available) argument of the vgchange command. The following
example deactivates the volume group my_volume_group.
•vgchange -a n my_volume_group
18
•vgremove officevg
• Either of the following commands renames the existing volume group vg02 to
my_volume_group
• You can move an entire LVM volume group to another system. It is recommended
that you use the vgexport and vgimport commands when you do this.
• To move a volume group form one system to another, perform the following
steps:
Make sure that no users are accessing files on the active volumes in the volume
group, then unmount the logical volumes.
Use the -a n argument of the vgchange command to mark the volume group as
inactive, which prevents any further activity on the volume group.
Use the vgexport command to export the volume group. This prevents it from
being accessed by the system from which you are removing it. After you export
the volume group, the physical volume will show up as being in an exported
volume group when you execute the pvscan command, as in the following
22
To move a volume group form one system to another, perform the following steps:
pvscan
PV /dev/sda1 is in exported VG myvg [17.15 GB / 7.15 GB free]
PV /dev/sdc1 is in exported VG myvg [17.15 GB / 15.15 GB free]
PV /dev/sdd1 is in exported VG myvg [17.15 GB / 15.15 GB free] ...
When the system is next shut down, you can unplug the disks that constitute the volume
group and connect them to the new system.
When the disks are plugged into the new system, use the vgimport command to import the
volume group, making it accessible to the new system.
Activate the volume group with the -a y argument of the vgchange command.
Mount the file system to make it available for use.
23
The following command creates a striped logical volume across 2 physical volumes with a
stripe of 64kB. The logical volume is 50 gigabytes in size, is named gfslv, and is carved out
of volume group vg0.
lvcreate -L 50G -i2 -I64 -n gfslv vg0
Physical Volumes
• Metadata
The LVM metadata contains the configuration details of the LVM volume groups on
your system. By default, an identical copy of the metadata is maintained in every
metadata area in every physical volume within the volume group. LVM metadata is
small and stored as ASCII.
Currently LVM allows you to store 0, 1 or 2 identical copies of its metadata on each
physical volume. The default is 1 copy. Once you configure the number of metadata
copies on the physical volume, you cannot change that number at a later time. The
first copy is stored at the start of the device, shortly after the label. If there is a second
copy, it is placed at the end of the device. If you accidentally overwrite the area at the
beginning of your disk by writing to a different disk than you intend, a second copy of
the metadata at the end of the device will allow you to recover the metadata.
25
Physical Volumes
• Metadata
If a volume group contains many physical volumes, having many redundant copies of
the metadata is inefficient. It is possible to create a physical volume without any
metadata copies by using the --metadatacopies 0 option of the pvcreate command.
Once you have selected the number of metadata copies the physical volume will
contain, you cannot change that at a later point. Selecting 0 copies can result in faster
updates on configuration changes. Note, however, that at all times every volume
group must contain at least one physical volume with a metadata area (unless you are
using the advanced configuration settings that allow you to store volume group
metadata in a file system). If you intend to split the volume group in the future, every
volume group needs at least one metadata copy.
26
Physical Volumes
• Metadata
27
By default, the pvcreate command places the physical volume label in the 2nd 512-
byte sector. This label can optionally be placed in any of the first four sectors, since the
LVM tools that scan for a physical volume label check the first 4 sectors. The physical
volume label begins with the string LABELONE.
Metadata Contents
The volume group metadata contains:
Sometimes, the volume group metadata area of a physical volume could be overwritten or
deleted. An error message indicating that the metadata area is incorrect, or that the system
was unable to find a physical volume with a particular UUID is displayed.
Symptoms
The following example shows the kind of output that might be displayed if the metadata area
is missing or corrupted.
• lvs -a -o +devices
Couldn't find device with uuid 'FdfDh3-zhog-iVI8-7qTD-S5BI-HSURYYM5Sk'.
Couldn't find all physical volumes for volume group VG.
Couldn't find device with uuid 'FdfDh3-zhog-iVI8-7qTD-S5BI-HSURYYM5Sk'.
Couldn't find all physical volumes for volume group VG.
30
pvscan
cat /proc/partitions
31
You can recover the data from the physical volume by writing a new metadata area on
the physical volume and specifying the same UUID as the lost metadata.
Warning: Do not attempt this procedure with a working Logical Volume Manager
(LVM)
logical volume. Data would be lost if you specify the incorrect UUID.
1. You can find the UUID of the physical volume that was overwritten in the
/etc/lvm/archive directory. Check the VolumeGroupName_xxxx.vg file for the last
known valid archived LVM metadata for that volume group.
Alternately, you can deactivate the volume and set the partial
(-P) argument to obtain the UUID of the physical volume that is missing or corrupted.
2. Use the --uuid and --restorefile arguments with the pvcreate command to restore the physical
volume. The pvcreate command overwrites only the LVM metadata areas and does not affect the
existing data areas.
3. Run the vgcfgrestore command to restore the metadata of the volume group.
vgcfgrestore VG
Restored volume group VG
# lvs -a -o +devices
Physical Volumes
• Metadata