LVM(Logic Volume Manager)即逻辑卷管理器,是linux系统用户对硬盘分区管理的一种机制。创建初衷是为了解决硬盘设备在创建完分区后不易修改分区的问题,LVM技术是在硬盘分区和文件系统之间加了一个逻辑层,他提供了一个抽象的卷组,可以把多个硬盘进行卷组合并,这样一来,用户就不用担心物理硬盘设备的底层架构布局,可以轻松实现对硬盘分区的动态调整。简而言之就是动态添加、缩减空间,而不会影响原有数据。
PV(Physical Volume):物理卷
VG(Volume Group):卷组
LV(Logical Volume):逻辑卷
PE(Physical Extent):基本单元
物理卷处于LVM中的最底层,可以将其理解为物理硬盘、硬盘分区或者磁盘阵列,物理卷可以理解为一个磁盘分区,创建物理卷时指定磁盘分区。卷组是建立在物理卷之上的,一个卷组可以包含多个物理卷,卷组创建之后也可以继续向其中添加物理卷。逻辑卷是用卷组中空闲的资源建立的,而且逻辑卷在建立后可以动态地扩建或者缩小空间。基本原理如图:(图片源自网络)
常用命令:
功能 PV管理命令 VG管理命令 LV管理命令
scan 扫描 pvscan vgscan lvscan
create 创建 pvcreate vgcreate lvcreate
display 显示 pvdisplay vgdisplay lvdisplay
remove 移除 pvremove vgremove lvremove
extend 扩展 vgextend lvextend(lvresize)
reduce 减少 vgreduce lvreduce(lvresize)
resize改变容量 lvresize
attribute 改变属性 pvchange vgchange lvchange
本次实操是在云平台上的磁盘上进行lvm的创建及空间分配,磁盘为/dev/vdb
依次输入:fdisk /dev/vdb
n:新建分区
p:主分区
默认回车
默认回车
t:调整分区类型
8e:调整为lvm类型分区
w:保存
[root@localhost ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xd3ff2d0b.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-419430399, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399):
Using default value 419430399
Partition 1 of type Linux and of size 200 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created.
#查看pv信息
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 centos lvm2 a-- <19.00g 0
/dev/vdb1 lvm2 --- <200.00g <200.00g
[root@localhost ~]# vgcreate vg_test /dev/vdb1 #第一个参数为vg名称,第二个参数为加入vg的pv
Volume group "vg_test" successfully created
#查看vg信息
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg_test 1 0 0 wz--n- <200.00g <200.00g
[root@localhost ~]# lvcreate -n lv_test -l 100%FREE vg_test #创建名为lv_test的逻辑卷,并分配名为vg_test的卷组内所有分区空间的全部空闲空间
Logical volume "lv_test" created.
[root@localhost ~]# mkfs -t ext4 /dev/mapper/vg_test-lv_test
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
13107200 inodes, 52427776 blocks
2621388 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2199912448
1600 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
#再次查看磁盘信息,就能看到这一个逻辑卷了
[root@localhost ~]# fdisk -l
#省略了部分输出
Disk /dev/mapper/vg_test-lv_test: 214.7 GB, 214744170496 bytes, 419422208 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
挂载磁盘不用磁盘名的方式,使用UUID挂载,更加可靠。
[root@localhost ~]# blkid #查看创建的lv的UUID
/dev/mapper/vg_test-lv_test: UUID="cdab99dd-fbf4-45f0-86ff-f55728d186cb" TYPE="ext4"
[root@localhost ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Aug 19 12:09:37 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=4a6e8295-a8c1-479c-9890-cbab68fdfd68 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
UUID=567b727c-af56-4d76-ae44-fb51b3c3944d /home/software ext4 defaults 0 0
#### /home/software 为要挂载的目录
#挂载立即生效,不用重启
[root@localhost ~]# mount -a地方-h
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_test-lv_test 197G 61M 187G 1% /home/software