what is lvm in linux

青灯夜游
青灯夜游Original
2022-02-24 15:08:537509browse

In Linux, the full name of LVM is "Logical Volume Manager", which means "logical volume management" in Chinese. It is a mechanism for managing disk partitions in the Linux environment; LVM is built on the hard disk and partitions. A logical layer that can be used to increase the flexibility of disk partition management.

what is lvm in linux

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What is LVM

LVM (Logical Volume Manager), which is logical volume management, is a method for managing disk partitions in the Linux environment. As a mechanism, LVM is a logical layer built on the hard disk and partition to improve the flexibility of disk partition management. LVM system administrators can easily manage disk partitions, such as connecting several disk partitions into a volume group to form a storage pool. Administrators can freely create logical volumes on volume groups and further create file systems on logical volume groups. Administrators can easily adjust the size of storage volume groups through LVM, and can name, manage and allocate disk storage according to groups. When a new disk is added to the system, the LVM administrator does not have to move the disk's files to the new disk to make full use of the new storage space, but can directly extend the file system across the disk.

Generally speaking, physical disks or partitions are separated, data cannot span disks or partitions, and the size of each disk or partition is fixed, so re-adjustment is troublesome. LVM can integrate these underlying physical disks or partitions, abstract them into capacity resource pools, and divide them into logical volumes for use by the upper layer. Its main function is that it can be used without shutting down or reformatting (to be precise, The size of the logical volume can be flexibly adjusted (the original part does not need to be formatted, only the new part is formatted).

The implementation process of LVM is as follows:

what is lvm in linux

LVM noun explanation

PV(physical volume): The physical volume is at the bottom of the logical volume management system and can be the entire physical hard disk or a partition on the actual physical hard disk. It just sets aside a special area in the physical partition to record management parameters related to LVM.

VG (volume group): Volume groups are created on physical volumes. A volume group must include at least one physical volume. After the volume group is created, volumes can be dynamically added to the volume group. , there can be multiple volume groups in a logical volume management system project.

LV(logical volume): Logical volumes are built on volume groups. Unallocated space in volume groups can be used to create new logical volumes. Logical volumes can be dynamically expanded and shrunk after they are created. space.

PE(physical extent): The physical area is the smallest storage unit that can be allocated in the physical volume. The physical area size is specified when establishing the volume group. Once determined, it cannot be changed. The physical area size of all physical volumes must be consistent. After a new pv is added to the vg, the pe size is automatically changed to the pe size defined in the vg.

LE(logical extent): The logical area is the smallest storage unit available for allocation in the logical volume. The size of the logical area depends on the size of the physical area in the volume group where the logical volume is located. Due to kernel limitations, a logical volume (Logic Volume) can only contain up to 65536 PE (Physical Extent), so the size of a PE determines the maximum capacity of the logical volume. A 4 MB (default) PE determines the maximum capacity of a single PE. The maximum capacity of a logical volume is 256 GB. If you want to use a logical volume larger than 256G, you need to specify a larger PE when creating a volume group. In Red Hat Enterprise Linux AS 4 the PE size ranges from 8 KB to 16GB and must always be a multiple of 2.

LVM writing mode

LVM has two writing modes: linear mode and stripe mode.

  • Linear mode means writing to another device after writing one device

  • The stripe mode is somewhat similar to RAID0, that is, the data is Scattered writing to each LVM member device.

    Because data in stripe mode is not secure, and LVM does not emphasize read and write performance, LVM defaults to linear mode, so that even if one device is broken, the data on other devices is still there.

How LVM works

LVM maintains a metadata at the head of each physical volume. The metadata contains the information of the entire VG (volume group: volume group), including the layout configuration of each VG, the number of PV (physical volume: physical volume), the number of LV (logical volume: logical volume), and each Mapping relationship from PE (physical extends: physical extension unit) to LE (logical extends: physical extension unit). The information in the header of each PV in the same VG is the same, which facilitates data recovery in case of failure.

LVM provides the LV layer for the upper file system, hiding the operation details. For the file system, the operation of LV is no different from the original operation of partition. When writing to the LV, LVM locates the corresponding LE and writes the data to the corresponding PE through the mapping table in the PV header. The biggest feature of LVM is that it can dynamically manage disks. Because the size of the logical volume can be dynamically adjusted without losing existing data. If we add a new hard disk, it will not change the existing upper logical volume. The key is to establish a mapping relationship between PE and LE. Different mapping rules determine different LVM storage models. LVM supports stripe and mirror of multiple PVs.

Advantages and disadvantages of LVM

Advantages:

  • File system Can span multiple disks, so the file system size is not limited by the physical disk.

  • The size of the file system can be dynamically expanded while the system is running.

  • You can add new disks to the LVM storage pool.

  • Important data can be redundantly mirrored to multiple physical disks.

  • You can easily export the entire volume group to another machine.

Disadvantages:

  • The reducevg command must be used when removing a disk from a volume group (this command Requires root privileges and is not allowed in snapshot volume groups).

  • When one disk in a volume group becomes damaged, the entire volume group is affected.

  • Due to the addition of additional operations, storage performance is affected.

Method to create PV/VG/LV

1. The system type is set to Linux LVM, and its system ID is 8e. Use the t command in the fdisk tool to set

[root@localhost ~]# fdisk /dev/sdb 
...
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (20973568-62914559, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-62914559, default 62914559): +5G
...
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e  # 指定system id为8e
Changed type of partition 'Linux' to 'Linux LVM'
...
Command (m for help): p
...
/dev/sdb1            2048    20973567    10485760   8e  Linux LVM
/dev/sdb2        20973568    31459327     5242880   8e  Linux LVM
Command (m for help): w
...

2. Initialize each physical disk or partition to PV (physical volume, physical volume). Volume)

The commands that can be used at this stage are pvcreate, pvremove, pvscan, pvdisplay (pvs)

1) pvcreate: Create a physical volume

Usage: pvcreate [option] DEVICE

Options:

  • -f: Force the creation of logical volumes without user confirmation

  • -u: Specify the UUID of the device

  • -y: Answer yes to all questions

Examplepvcreate /dev/sdb1 /dev/sdb2

2) pvscan: Scan all physical volumes on the current system

Usage: pvscan [option]

Options:

  • -e: Only display physical volumes belonging to the output volume group

  • -n: Only display physical volumes that do not belong to any volume group

  • -u: Display UUID

3) pvdisplay: Display the attributes of the physical volume

Usage: pvdisplay [PV_DEVICE]

4 ) pvremove: Delete the physical volume information so that it is no longer considered a physical volume

Usage: pvremove [option] PV_DEVICE

Options:

  • -f: Forced deletion

  • -y: Answer yes to all questions

Examplepvremove /dev/sdb1

5) pv creation and deletion example

[root@localhost ~]# pvcreate /dev/sdb{1,2}  # 将两个分区初始化为物理卷
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.
[root@localhost ~]# pvscan 
  PV /dev/sdb2                      lvm2 [5.00 GiB]
  PV /dev/sdb1                      lvm2 [10.00 GiB]
  Total: 2 [15.00 GiB] / in use: 0 [0   ] / in no VG: 2 [15.00 GiB]
[root@localhost ~]# pvdisplay /dev/sdb1   # 显示物理卷sdb1的详细信息
  "/dev/sdb1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   # 由于PE是在VG阶段才划分的,所以此处看到的都是0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               GrP9Gi-ubau-UAcb-za3B-vSc3-er2Q-MVt9OO
   
[root@localhost ~]# pvremove /dev/sdb2   # 删除sdb2的物理卷信息
  Labels on physical volume "/dev/sdb2" successfully wiped.
[root@localhost ~]# pvscan     # 可以看到PV列表中已无sdb2
  PV /dev/sdb1                      lvm2 [10.00 GiB]
  Total: 1 [10.00 GiB] / in use: 0 [0   ] / in no VG: 1 [10.00 GiB]
[root@localhost ~]# pvcreate /dev/sdb2 
  Physical volume "/dev/sdb2" successfully created.

3. Create VG (volume group, volume Group).

The volume group integrates multiple physical volumes (shielding the underlying details) and divides PE (physical extend)

PE is the smallest storage unit in the physical volume, somewhat similar For blocks in the file system, the PE size can be specified, and the default is 4M. The commands used at this stage include vgcreate, vgscan, vgdisplay, vgextend, vgreduce

##1) vgcreate: Create a volume group

Usage:

vgcreate [option] VG_NAME PV_DEVICE

Options:

  • -s: PE size of the physical volume in the volume group, the default is 4M

  • -l: The maximum number of logical volumes allowed to be created on the volume group

  • -p: The maximum number of logical volumes allowed to be added at the volume level Maximum number of physical volumes

Example

vgcreate -s 8M myvg /dev/sdb1 /dev/sdb2

2) vgscan: Find the existence in the system LVM volume group and display the found volume group list

3) vgdisplay: Display volume group attributes

Usage:

vgdisplay [option] [VG_NAME]

Options:

  • -A: Only display information about active volume groups

  • -s: Use short format output information

4) vgextend: Dynamically extend the LVM volume group, which increases the capacity of the volume group by adding physical volumes to the volume group

Usage:

vgextend VG_NAME PV_DEVICE

  例 vgextend myvg /dev/sdb3

5)vgreduce:通过删除LVM卷组中的物理卷来减少卷组容量,不能删除LVM卷组中剩余的最后一个物理卷

 用法:vgreduce VG_NAME PV_DEVICE

6)vgremove:删除卷组,其上的逻辑卷必须处于离线状态

  用法:vgremove [-f] VG_NAME

  •   -f:强制删除

7)vgchange:常用来设置卷组的活动状态

  用法:vgchange -a n/y VG_NAME

  •   -a n为休眠状态,休眠之前要先确保其上的逻辑卷都离线;

  •   -a y为活动状态

8)vg创建例子

[root@localhost ~]# vgcreate -s 8M myvg /dev/sdb{1,2}
  Volume group "myvg" successfully created
[root@localhost ~]# vgscan
  Reading volume groups from cache.
  Found volume group "myvg" using metadata type lvm2
[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               14.98 GiB
  PE Size               8.00 MiB
  Total PE              1918
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1918 / 14.98 GiB
  VG UUID               aM3RND-aUbQ-7RjC-dCci-JiS4-Oj2Z-wv9poA

4、在卷组上创建LV(logical volume,逻辑卷)

为了便于管理,逻辑卷对应的设备文件保存在卷组目录下,为/dev/VG_NAME/LV_NAME。LV中可以分配的最小存储单元称为LE(logical extend),在同一个卷组中,LE的大小和PE是一样的,且一一对应。这一阶段用到的命令有lvcreate、lvscan、lvdisplay、lvextend、lvreduce、lvresize

1)lvcreate:创建逻辑卷或快照

  用法:lvcreate [选项] [参数]

  选项:

  •       -L:指定大小

  •       -l:指定大小(LE数)

  •       -n:指定名称

  •       -s:创建快照

  •       -p r:设置为只读(该选项一般用于创建快照中)

  注:使用该命令创建逻辑卷时当然必须指明卷组,创建快照时必须指明针对哪个逻辑卷         

  例 lvcreate -L 500M -n mylv myvg

2)lvscan:扫描当前系统中的所有逻辑卷,及其对应的设备文件

3)lvdisplay:显示逻辑卷属性

  用法:lvdisplay [/dev/VG_NAME/LV_NAME]

4)lvextend:可在线扩展逻辑卷空间

  用法:lvextend -L/-l 扩展的大小 /dev/VG_NAME/LV_NAME  

  选项:

  •       -L:指定扩展(后)的大小。例如,-L +800M表示扩大800M,而-L 800M表示扩大至800M

  •       -l:指定扩展(后)的大小(LE数)

  例 lvextend -L 200M /dev/myvg/mylv

5)lvreduce:缩减逻辑卷空间,一般离线使用

 用法:lvexreduce -L/-l 缩减的大小 /dev/VG_NAME/LV_NAME  

  选项:

  •       -L:指定缩减(后)的大小

  •       -l:指定缩减(后)的大小(LE数)

  例 lvreduce -L 200M /dev/myvg/mylv

6)lvremove:删除逻辑卷,需要处于离线(卸载)状态

  用法:lvremove [-f] /dev/VG_NAME/LV_NAME

  •   -f:强制删除

7)lv创建例子

[root@localhost ~]# lvcreate -L 2G -n mylv myvg  
  Logical volume "mylv" created.
[root@localhost ~]# lvscan 
  ACTIVE            '/dev/myvg/mylv' [2.00 GiB] inherit
[root@localhost ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                2lfCLR-UEhm-HMiT-ZJil-3EJm-n2H3-ONLaz1
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2019-07-05 13:42:44 +0800
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

5、格式化逻辑卷并挂载

[root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylv
...
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
...
[root@localhost ~]# mkdir /data
[root@localhost ~]# mount
mount mountpoint
[root@localhost ~]# mount /dev/myvg/mylv /data
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 1.5G 49G 3% /
devtmpfs 903M 0 903M 0% /dev
tmpfs 912M 0 912M 0% /dev/shm
tmpfs 912M 8.6M 904M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
tmpfs 183M 0 183M 0% /run/user/0
/dev/mapper/myvg-mylv 2.0G 6.0M 1.8G 1% /data

相关推荐:《Linux视频教程

The above is the detailed content of what is lvm in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn