Linux logical volume Management
Linux logical volume Management
1. What is a logical volume?
LVM is short for Logical Volume Manager. It is an abstraction layer built on a physical storage device that allows you to generate Logical storage volumes, it provides better flexibility than directly using physical storage for management.
LVM virtualizes storage and uses logical volumes. You are not limited by the size of physical disks. In addition, storage settings related to hardware are hidden by LVM, you do not need to stop the application or uninstall the file system to adjust the volume size or data migration. this can reduce operation costs.
2. What are the advantages of logical volume?
1). Flexible capacity.
When using logical volumes, the file system can be extended to multiple disks. You can combine multiple disks or disk partitions into a single logical volume.
2). scalable storage pool.
You can use simple commands to expand or reduce the size of logical volumes without reformatting or partitioning disk devices.
3). Online Data redistribution.
You can move data online and re-allocate data when the disk is online. For example, you can change the hot-swappable disk online.
4). Convenient device naming
The logical volume can start all the names in a convenient way.
5). Disk partitioning.
You can generate a Logical Disk and its data can be stored in two or more disks in a block manner. This significantly improves data throughput.
6). Image volume
Logical volumes provide a convenient way to map your data.
7). Volume Snapshot
Using Logical volumes, you can obtain device snapshots for consistent backup or test data update without affecting real data.
3. Basic Terms of LVM
As mentioned above, LVM is a logical layer added to physical storage to shield the following hardware storage devices from the file system, provide an abstract disk volume, and create a file system on the disk volume. First, we will discuss the following LVM terms:
* The Physical Media)
This refers to the storage devices of the system, such as/dev/hda1 and/dev/sda. It is the storage unit at the lowest layer of the storage system.
* Physical Volume (PV, Physical Volume)
A physical volume is a disk, a disk partition, or a device (such as RAID) with the same functions as a disk partition. It is the basic storage Logical Block of LVM, however, compared with basic physical storage media (such as partitions and disks), it contains management parameters related to LVM. Currently, LVM allows you to save zero to two copies of the metadata of this physical volume on each physical volume. the default value is 1, which is saved at the beginning of the device. when the value is 2, the second backup is saved at the end of the device.
* Volume Group (VG, Volume Group)
An LVM volume group is similar to a physical hard disk in a non-LVM system and consists of physical volumes. One or more LVM partitions (logical volumes) can be created in a volume group. An LVM volume group consists of one or more physical volumes.
* Logical Volume (LV, Logical Volume)
The logical volume of LVM is similar to the hard disk partition in a non-LVM system. A file system (such as/home or/usr) can be built on the logical volume ).
* Linear logical volume (Linear Volumes)
A linear logical volume aggregates multiple physical volumes into one logical volume. For example, if you have two 60 GB hard disks, you can generate a GB logical volume.
* Fragmented Logical volume (Striped Logical Volumes)
When you write data to this logical volume, the file system can place the data in multiple physical volumes. for a large number of connection read/write operations, it can improve data I/O efficiency.
* Mirrored Logical Volumes)
The image stores consistent data on different devices. The data is written to the original device and the image device at the same time. It provides fault tolerance between devices.
* Snapshot volume (Snapshot Volumes)
The Snapshot volume provides a virtual image of a device at a specific moment. When the snapshot starts, it copies a copy of the changes to the current data area. As a result, the snapshot volume preferentially performs these changes, so he can reconstruct the status of the current device.
* PE (physical extent)
Each Physical volume is divided into basic units called Physical Extents. pes with unique numbers are the smallest units that can be addressed by LVM. The PE size is configurable. The default value is 4 MB.
* LE (logical extent)
Logical volumes are also divided into the addressable basic units called LE (Logical Extents. In the same volume group, the LE size and PE are the same and one-to-one correspondence.
The non-LVM system stores the metadata that contains the partition information in the partition table at the starting position of the partition, metadata related to logical volumes and volume groups is also stored in the VGDA (volume group descriptor area) at the beginning of the physical volume. VGDA includes PV descriptor, VG descriptor, LV descriptor, and PE descriptor. When LVM is started, the system activates the VG and loads the VGDA to the memory to identify the actual physical storage location of the LV. When the system performs I/O operations, it accesses the actual physical location based on the ing mechanism established by VGDA.
4. How to use it?
1) Add a disk (/dev/sdb)
2) create a physical volume
Pvcreate/dev/sdb
Pvs
Pvdisplay
3) create a volume group
Vgcreate vg1/dev/sdb
Vgs
Vgdisplay
4) create a logical volume
Lvcreate-n lv1-L 100 m vg1
Lvs
Lvdisplay
5) format the logical volume
Mkfs. ext4/dev/vg1/lv1
6) Mount and use logical volumes
Mount/dev/vg1/lv1/mnt
7) view mounting information
Mount
8) view disk usage
Df-h
Fdisk-l
9) delete LVM
9.1 Delete LV: lvremove/dev/vg1/lv1
9.2 Delete VG: vgremove vg1
9.3 delete a physical volume: pvremove/dev/sdb
10) stretch the logical volume:
10.1 check sufficient free space in VG: vgdisplay
10.2 extended logical volume: lvextend-L + 50 m/dev/vg1/lv1
10.3 view the Expanded size: lvdisplay
10.4 Update file system: resize2fs/dev/vg1/lv1
10.5 view the updated file system: df-h
11) stretch the volume group:
11.1 pvcreate/dev/sdc
11.2 vgextend vg1/dev/sdc
11.3 vgdisplay
12) narrow down the logical volume:
12.1 uninstall logical volume: umount/dev/vg1/lv1
12.2 zoom out the file system: resize2fs/dev/vg1/lv1 (run e2fsck-f/dev/vg1/lv1 first)
12.3 LV: lvreduce-L-50 m/dev/vg1/lv1
12.4 check the reduced LV: lvdisplay
12.5 re-mount: mount/dev/vg1/lv1/mnt
13) shrink the volume group:
13.1 remove PV from the volume group: vgreduce vg1/dev/sdc
13.2 view the scaled-out volume group size: vgdisplay