Abstract
| Goal | Create and manage logical volumes containing file systems and swap spaces from the command line. |
| Objectives |
|
| Sections |
|
| Lab |
Managing Logical Volumes |
After completing this section, you should be able to:
Describe logical volume management components and concepts.
Implement LVM storage.
Display LVM component information.
Logical volumes and logical volume management make it easier to manage disk space. If a file system that hosts a logical volume needs more space, it can be allocated to its logical volume from the free space in its volume group and the file system can be resized. If a disk starts to fail, a replacement disk can be registered as a physical volume with the volume group and the logical volume's extents can be migrated to the new disk.
LVM Definitions
Physical devices are the storage devices used to save data stored in a logical volume. These are block devices and could be disk partitions, whole disks, RAID arrays, or SAN disks. A device must be initialized as an LVM physical volume in order to be used with LVM. The entire device will be used as a physical volume.
Physical volumes are the underlying "physical" storage used with LVM. You must initialize a device as a physical volume before using it in an LVM system. LVM tools segment physical volumes into physical extents (PEs), which are small chunks of data that act as the smallest storage block on a physical volume.
Volume groups are storage pools made up of one or more physical volumes. This is the functional equivalent of a whole disk in basic storage. A PV can only be allocated to a single VG. A VG can consist of unused space and any number of logical volumes.
Logical volumes are created from free physical extents in a volume group and provide the "storage" device used by applications, users, and the operating system. LVs are a collection of logical extents (LEs), which map to physical extents, the smallest storage chunk of a PV. By default, each LE maps to one PE. Setting specific LV options changes this mapping; for example, mirroring causes each LE to map to two PEs.
Creating LVM storage requires several steps. The first step is to determine which physical devices to use. After a set of suitable devices have been assembled, they are initialized as physical volumes so that they are recognized as belonging to LVM. The physical volumes are then combined into a volume group. This creates a pool of disk space out of which logical volumes can be allocated. Logical volumes created from the available space in a volume group can be formatted with a file system, activated as swap space, and mounted or activated persistently.
LVM provides a comprehensive set of command-line tools for implementing and managing LVM storage. These command-line tools can be used in scripts, making them suitable for automation.
The following examples use device vdb and its partitions to illustrate LVM commands.
In practice, these examples would need to use the correct devices for the disk and disk partitions that are being used by the system.
Use the lsblk, blkid, or cat /proc/partitions commands to identify the devices on your system.
To create a logical volume, perform the following steps:
Prepare the physical device.
Use parted, gdisk, or fdisk to create a new partition for use with LVM.
Always set the partition type to Linux LVM on LVM partitions; use 0x8e for MBR partitions.
If necessary, use partprobe to register the new partition with the kernel.
Alternatively, use a whole disk, a RAID array, or a SAN disk.
A physical device only needs to be prepared if there are none prepared already and a new physical volume is required to create or extend a volume group.
[root@host ~]#parted -s /dev/vdb mkpart primary 1MiB 769MiB[root@host ~]#parted -s /dev/vdb mkpart primary 770MiB 1026MiB[root@host ~]#parted -s /dev/vdb set 1 lvm on[root@host ~]#parted -s /dev/vdb set 2 lvm on
Create a physical volume.
Use pvcreate to label the partition (or other physical device) as a physical volume.
The pvcreate command divides the physical volume into physical extents (PEs) of a fixed size, for example, 4 MiB blocks.
You can label multiple devices at the same time by using space-delimited device names as arguments to pvcreate.
[root@host ~]#pvcreate /dev/vdb2 /dev/vdb1
This labels the devices /dev/vdb2 and /dev/vdb1 as PVs, ready for allocation into a volume group.
A PV only needs to be created if there are no PVs free to create or extend a VG.
Create a volume group.
Use vgcreate to collect one or more physical volumes into a volume group.
A volume group is the functional equivalent of a hard disk; you will create logical volumes from the pool of free physical extents in the volume group.
The vgcreate command-line consists of a volume group name followed by one or more physical volumes to allocate to this volume group.
[root@host ~]#vgcreate vg01 /dev/vdb2 /dev/vdb1
This creates a VG called vg01 that is the combined size, in PE units, of the two PVs /dev/vdb2 and /dev/vdb1.
A VG only needs to be created if none already exist. Additional VGs may be created for administrative reasons to manage the use of PVs and LVs. Otherwise, existing VGs can be extended to accommodate new LVs when needed.
Create a logical volume.
Use lvcreate to create a new logical volume from the available physical extents in a volume group.
At a minimum, the lvcreate command includes the -n option to set the LV name, either the -L option to set the LV size in bytes or the -l option to set the LV size in extents, and the name of the volume group hosting this logical volume.
[root@host ~]#lvcreate -n lv01 -L 700M vg01
This creates an LV called lv01, 700 MiB in size, in the VG vg01.
This command will fail if the volume group does not have a sufficient number of free physical extents for the requested size.
Note also that the size will be rounded to a factor of the physical extent size if the size cannot match exactly.
You can specify the size using the -L option, which expects sizes in bytes, mebibytes (binary megabytes, 1048576 bytes), gibibytes (binary gigabytes), or similar.
Alternatively, you can use the -l option, which expects sizes specified as a number of physical extents.
The following list provides some examples of creating LVs:
lvcreate -L 128M: Size the logical volume to exactly 128 MiB.
lvcreate -l 128 : Size the logical volume to exactly 128 extents.
The total number of bytes depends on the size of the physical extent block on the underlying physical volume.
Different tools display the logical volume name using either the traditional name, /dev/, or the kernel device mapper name, vgname/lvname/dev/mapper/.
vgname-lvname
Add the file system.
Use mkfs to create an XFS file system on the new logical volume.
Alternatively, create a file system based on your preferred file system, for example, ext4.
[root@host ~]#mkfs -t xfs /dev/vg01/lv01
To make the file system available across reboots, perform the following steps:
Use mkdir to create a mount point.
[root@host ~]#mkdir /mnt/data
Add an entry to the /etc/fstab file:
/dev/vg01/lv01 /mnt/data xfs defaults 1 2
Mounting a logical volume by name is equivalent to mounting by UUID because LVM finds its physical volumes based on a UUID even if you initially add them to the volume group by name.
Run mount /mnt/data to mount the file system that you just added in /etc/fstab.
[root@host ~]#mount /mnt/data
To remove all logical volume components, perform the following steps:
Prepare the file system.
Move all data that must be kept to another file system.
Use the umount command to unmount the file system and then remove any /etc/fstab entries associated with this file system.
[root@host ~]#umount /mnt/data
Removing a logical volume destroys any data stored on the logical volume. Back up or move your data before you remove the logical volume.
Remove the logical volume.
Use lvremove to remove a logical volume that is no longer needed.
DEVICE_NAME
[root@host ~]#lvremove /dev/vg01/lv01
Unmount the LV file system before running this command. The command prompts for confirmation before removing the LV.
The LV's physical extents are freed and made available for assignment to existing or new LVs in the volume group.
Remove the volume group.
Use vgremove to remove a volume group that is no longer needed.
VG_NAME
[root@host ~]#vgremove vg01
The VG's physical volumes are freed and made available for assignment to existing or new VGs on the system.
Remove the physical volumes.
Use pvremove to remove physical volumes that are no longer needed.
Use a space-delimited list of PV devices to remove more than one at a time.
This command deletes the PV metadata from the partition (or disk).
The partition is now free for reallocation or reformatting.
[root@host ~]#pvremove /dev/vdb2 /dev/vdb1
Use pvdisplay to display information about physical volumes.
To list information about all physical volumes, use the command without arguments.
To list information about a specific physical volume, pass that device name to the command.
[root@host ~]#pvdisplay /dev/vdb1--- Physical volume --- PV Name /dev/vdb1VG Name vg01
PV Size 768.00 MiB / not usable 4.00 MiB
Allocatable yes PE Size 4.00 MiB
Total PE 191 Free PE 16
Allocated PE 175 PV UUID JWzDpn-LG3e-n2oi-9Etd-VT2H-PMem-1ZXwP1
| |
| |
| |
It is also the multiplying factor when calculating the size of any value reported in PE units, such as Free PE; for example: 26 PEs x 4 MiB (the PE Size) equals 104 MiB of free space. A logical volume size is rounded to a factor of PE units. LVM sets the PE size automatically, although it is possible to specify it. | |
|
Use vgdisplay to display information about volume groups.
To list information about all volume groups, use the command without arguments.
To list information about a specific volume group, pass that VG name to the command.
[root@host ~]#vgdisplay vg01--- Volume group --- VG Name vg01System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 1016.00 MiB
PE Size 4.00 MiB Total PE 254
Alloc PE / Size 175 / 700.00 MiB Free PE / Size 79 / 316.00 MiB
VG UUID 3snNw3-CF71-CcYG-Llk1-p6EY-rHEv-xfUSez
Use lvdisplay to display information about logical volumes.
If you provide no argument to the command, it displays information about all LVs; if you provide an LV device name as an argument, the command displays information about that specific device.
[root@host ~]#lvdisplay /dev/vg01/lv01--- Logical volume --- LV Path /dev/vg01/lv01LV Name lv01 VG Name vg01
LV UUID 5IyRea-W8Zw-xLHk-3h2a-IuVN-YaeZ-i3IRrN LV Write Access read/write LV Creation host, time host.lab.example.com, 2019-03-28 17:17:47 -0400 LV Status available # open 1 LV Size 700 MiB
Current LE 175
Segments 1 Allocation inherit Read ahead sectors auto - current set to 256 Block device 252:0
Some tools may report the device name as | |
| |
| |
|