Home  >  Article  >  Operation and Maintenance  >  What is the lvm partition of linux?

What is the lvm partition of linux?

藏色散人
藏色散人Original
2023-03-24 09:48:291678browse

Linux's lvm partition refers to "logical volume management". The full English name of lvm is "Logical Volume Manager", which is a mechanism for managing disk partitions in the Linux environment; LVM is built on the hard disk and A logical layer above partitions to improve the flexibility of disk partition management.

What is the lvm partition of linux?

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

1. What is LVM

LVM (Logical Volume Manager), that is, logical volume management, is a mechanism for managing disk partitions in the Linux environment. LVM is built on the hard disk and partition A logical layer on top 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 the lvm partition of linux?

2. LVM terminology 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, 4 MB (default ) determines the maximum capacity of a single logical volume to 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.

3. LVM writing mode

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

  • Linear mode means writing to one device before writing to another device
  • Stripe mode is somewhat similar to RAID0, that is, data is written to each LVM member device in a distributed manner.
    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.

4. How LVM works

LVM maintains a metadata at the head of each physical volume, and each metadata contains the entire VG (volume group: volume group ) information, 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 PE (physical extends: physical expansion unit) to LE (logical extends: mapping relationship of 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.

5. Advantages and Disadvantages of LVM

Advantages:

  • The 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.
  • You can redundant important data to multiple physical disks in a mirroring manner.
  • 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 use).
  • When one disk in a volume group is damaged, the entire volume group will be affected.
  • Due to the addition of additional operations, storage performance is affected.

6. Method of creating PV/VG/LV

1. Set the system type of each physical disk or partition 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): 2First sector (20973568-62914559, default 20973568): 
Using default value 20973568Last sector, +sectors or +size{K,M,G} (20973568-62914559, default 62914559): +5G
...
Command (m for help): t
Partition number (1,2, default 2): 2Hex code (type L to list all codes): 8e  # 指定system id为8eChanged 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 into a PV (physical volume)

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

1) pvcreate: Create a physical volume

用法:pvcreate [option] DEVICE

  选项:

      -f:强制创建逻辑卷,不需用户确认

      -u:指定设备的UUID

      -y:所有问题都回答yes

  例 pvcreate /dev/sdb1 /dev/sdb2

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

用法:pvscan [option]

  选项:

      -e:仅显示属于输出卷组的物理卷

      -n:仅显示不属于任何卷组的物理卷

      -u:显示UUID

3) pvdisplay: Display the properties of the physical volume

 用法:pvdisplay [PV_DEVICE]

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

 用法:pvremove [option] PV_DEVICE

  选项:

      -f:强制删除

      -y:所有问题都回答yes

  例 pvremove /dev/sdb1

5) PV creation and deletion examples

[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 groups integrate multiple physical volumes (shielding the underlying details) and divide PE (physical extend)

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

1) vgcreate: Create a volume group

 用法:vgcreate [option] VG_NAME PV_DEVICE

  选项:

      -s:卷组中的物理卷的PE大小,默认为4M

      -l:卷组上允许创建的最大逻辑卷数

      -p:卷级中允许添加的最大物理卷数

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

2) vgscan: Find LVM volume groups that exist in the system and display the list of found volume groups

3) vgdisplay: Display volume group attributes

  用法:vgdisplay [option] [VG_NAME]

  选项:

      -A:仅显示活动卷组的信息

      -s:使用短格式输出信息

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

 用法:vgextend VG_NAME PV_DEVICE

  例 vgextend myvg /dev/sdb3

5) vgreduce: Reduce volume group capacity by deleting physical volumes in the LVM volume group. The last remaining physical volume in the LVM volume group cannot be deleted

 用法:vgreduce VG_NAME PV_DEVICE

6) vgremove: delete the volume group, the logical volume on it must be offline

  用法: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

PS:更新

一、LV逻辑卷扩容后,必须对挂载目录在线扩容。

使用 resize2fs或xfs_growfs 对挂载目录在线扩容
resize2fs 针对文件系统ext2 ext3 ext4
xfs_growfs 针对文件系统xfs

xfs在线扩容

xfs_growfs /dev/mapper/vg--BHG-lv01
meta-data=/dev/mapper/vg--BHG-lv01 isize=512    agcount=4, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0data     =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blksnaming   =version 2              bsize=4096   ascii-ci=0 ftype=1log      =internal               bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1realtime =none                   extsz=4096   blocks=0, rtextents=0data blocks changed from 128000 to 256000

ext4在线扩容

[root@localhost /]# resize2fs /dev/mapper/vg--BHG-lv02
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/vg--BHG-lv02 is mounted on /BHGPOS-data; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/mapper/vg--BHG-lv02 is now 5242880 blocks long.

相关推荐:《Linux视频教程

The above is the detailed content of What is the lvm partition of 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
Previous article:What does linux io mean?Next article:What does linux io mean?