LVM(逻辑卷)的产生是因为传统的分区一旦分区好后就无法在线扩充空间,也存在一些工具能实现在线扩充空间但是还是会面临数据损坏的风险;传统的分区当分区空间不足时,一般的解决办法是再创建一个更大的分区将原分区卸载然后将数据拷贝到新分区,但是在企业的生产系统往往不允许停机或者允许停机的时间很短,LVM就能很好的解决在线扩充空间的问题,而且不会对数据造成影响,LVM还能通过快照在备份的过程中保证日志文件和表空间文件在同一时间点的一致性。
在LVM中PE(Physical Extend)是卷的最小单位,默认4M大小,就像我们的数据是以页的形式存储一样,卷就是以PE的形式存储。PV(Physical Volume)是物理卷,如果要使用逻辑卷,首先第一步操作就是将物理磁盘或者物理分区格式化成PV,格式化之后PV就可以为逻辑卷提供PE了。VG(Volume Group)是卷组,VG就是将很多PE组合在一起生成一个卷组,当然这里的PE是可以跨磁盘的,如果当前服务器磁盘空间不足就可以增加一个新磁盘对当前系统不会产生任何影响。LV(Logical Volume)是逻辑卷,逻辑卷最终是给用户使用的,前面几个都是为创建逻辑卷做的准备,创建逻辑卷的大小只要不超过VG剩余空间就可以。
1.4:建立LVM分区管理机制的过程
2.1:我们单独给虚拟机增加一块磁盘,对其进行分区处理
2.2:查看我们新添加的磁盘
2.3:使用fdisk对新添加的磁盘进行分区(将新增的磁盘分成三个区)
2.4:使用pvcreate将分区划分为物理卷PV(物理卷不能直接使用)
创建物理卷
root@ubuntu:~# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
root@ubuntu:~# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created
查看物理卷列表
root@ubuntu:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 3.00g 3.00g
/dev/sdb2 lvm2 --- 3.00g 3.00g
查看物理卷详情
root@ubuntu:~# pvdisplay
"/dev/sdb2" is a new physical volume of "3.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 3.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID vzvKtd-iD8x-GkRi-z71g-6CnR-EdkF-JxSIk6
"/dev/sdb1" is a new physical volume of "3.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 3.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID xczBTT-Qac2-yFK7-UZn3-f01F-hb07-ZD4DYT
2.5:使用vgcreate将物理卷划为卷组PV-VG(1个VG下可创建多个LV)
创建卷组
可以使用 -s 参数指定PE的大小,必须为4的倍数
root@ubuntu:~# vgcreate HostVG01 /dev/sdb1
Volume group "HostVG01" successfully created
root@ubuntu:~# vgcreate HostVG02 -s 16M /dev/sdb2
Volume group "HostVG02" successfully created
root@ubuntu:~#
查看卷组列表
root@ubuntu:~# vgs
VG #PV #LV #SN Attr VSize VFree
HostVG01 1 0 0 wz--n- <3.00g <3.00g
HostVG02 1 0 0 wz--n- 2.98g 2.98g
root@ubuntu:~#
查看卷组详情
root@ubuntu:~# vgs
VG #PV #LV #SN Attr VSize VFree
HostVG01 1 0 0 wz--n- <3.00g <3.00g
HostVG02 1 0 0 wz--n- 2.98g 2.98g
root@ubuntu:~#
root@ubuntu:~# vgdisplay
--- Volume group ---
VG Name HostVG02
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 2.98 GiB
PE Size 16.00 MiB # 我们可以看到我们创建的卷组2指定的PE大小为16M
Total PE 191
Alloc PE / Size 0 / 0
Free PE / Size 191 / 2.98 GiB
VG UUID Wr0uIM-ddav-nu93-M3H1-J07b-ryCV-mJLJvR
--- Volume group ---
VG Name HostVG01
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size <3.00 GiB
PE Size 4.00 MiB
Total PE 767
Alloc PE / Size 0 / 0
Free PE / Size 767 / <3.00 GiB
VG UUID 2mEweo-f39v-dmjX-ikbh-gNDL-F6Mh-bsG0W0
2.6:使用卷组创建逻辑卷PV-VG-LV(逻辑卷可以挂载)
创建逻辑卷
-n:指定逻辑卷名称
-L:指定逻辑卷大小
root@ubuntu:~# lvcreate -n lv01-hostvg01 -L 128M HostVG01
Logical volume "lv01-hostvg01" created.
root@ubuntu:~# lvcreate -n lv02-hostvg02 -L 64M HostVG02
Logical volume "lv02-hostvg02" created.
root@ubuntu:~#
查看逻辑卷列表
root@ubuntu:~# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv01-hostvg01 HostVG01 -wi-a----- 128.00m
lv02-hostvg02 HostVG02 -wi-a----- 64.00m
root@ubuntu:~#
查看逻辑卷详情
root@ubuntu:~# lvdisplay
--- Logical volume ---
LV Path /dev/HostVG02/lv02-hostvg02
LV Name lv02-hostvg02
VG Name HostVG02
LV UUID WJqpL2-dpja-E3Tz-YLmO-dzkB-alUM-YDyd9S
LV Write Access read/write
LV Creation host, time ubuntu, 2020-06-30 16:08:14 +0800
LV Status available
# open 0
LV Size 64.00 MiB
Current LE 4
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
--- Logical volume ---
LV Path /dev/HostVG01/lv01-hostvg01
LV Name lv01-hostvg01
VG Name HostVG01
LV UUID 19ey43-fAgf-8JjX-RhTL-1w76-tx0W-hhE31s
LV Write Access read/write
LV Creation host, time ubuntu, 2020-06-30 16:07:26 +0800
LV Status available
# open 0
LV Size 128.00 MiB
Current LE 32
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
root@ubuntu:~#
2.7:将逻辑卷格式化为mkfs.ext4格式,然后挂载mount到对应目录
格式化逻辑卷
root@ubuntu:~# mkfs.ext4 /dev/HostVG01/lv01-hostvg01
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 131072 1k blocks and 32768 inodes
Filesystem UUID: 9bd4f0fe-aca2-49db-b325-9caadb109df0
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
root@ubuntu:~# mkfs.ext4 /dev/HostVG02/lv02-hostvg02
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 65536 1k blocks and 16384 inodes
Filesystem UUID: 41717f1b-de5d-463d-9b8d-5bd0354454bf
Superblock backups stored on blocks:
8193, 24577, 40961, 57345
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
root@ubuntu:~#
创建空目录并挂载逻辑卷
root@ubuntu:~# mkdir /lv0{1,2}
root@ubuntu:~# ls /lv0*
/lv01:
/lv02:
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~# mount /dev/HostVG01/lv01-hostvg01 /lv01
root@ubuntu:~# mount /dev/HostVG02/lv02-hostvg02 /lv02
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~# df -Th /lv0*
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/HostVG01-lv01--hostvg01 ext4 120M 1.6M 110M 2% /lv01
/dev/mapper/HostVG02-lv02--hostvg02 ext4 58M 1.3M 53M 3% /lv02
root@ubuntu:~#
2.8:LV扩容
lv02-hostvg02
进行扩容
-L:指定扩容容量 1G表示扩容后的大小,如果是+1G,则表示再原基础上增加1G
-r:resize,重置大小
root@ubuntu:~# lvextend -L 1G -r /dev/HostVG02/lv02-hostvg02
Size of logical volume HostVG02/lv02-hostvg02 changed from 64.00 MiB (4 extents) to 1.00 GiB (64 extents).
Logical volume HostVG02/lv02-hostvg02 successfully resized.
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/mapper/HostVG02-lv02--hostvg02 is mounted on /lv02; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 8
The filesystem on /dev/mapper/HostVG02-lv02--hostvg02 is now 1048576 (1k) blocks long.
root@ubuntu:~#
root@ubuntu:~# df -Th /lv0*
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/HostVG01-lv01--hostvg01 ext4 120M 1.6M 110M 2% /lv01
/dev/mapper/HostVG02-lv02--hostvg02 ext4 988M 2.7M 943M 1% /lv02
root@ubuntu:~#
2.9:LV缩容(缩容一般有条件,如果逻辑卷下有文件,扩容很可能导致文件损坏)
此处默认为无条件缩容,因为我们知道此时逻辑卷lv01-hostvg01为空
root@ubuntu:~# lvreduce -L 64M -r /dev/HostVG01/lv01-hostvg01
Do you want to unmount "/lv01" ? [Y|n] y
fsck from util-linux 2.31.1
/dev/mapper/HostVG01-lv01--hostvg01: 11/32768 files (0.0% non-contiguous), 9787/131072 blocks
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/mapper/HostVG01-lv01--hostvg01 to 65536 (1k) blocks.
The filesystem on /dev/mapper/HostVG01-lv01--hostvg01 is now 65536 (1k) blocks long.
Size of logical volume HostVG01/lv01-hostvg01 changed from 128.00 MiB (32 extents) to 64.00 MiB (16 extents).
Logical volume HostVG01/lv01-hostvg01 successfully resized.
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~# df -Th /lv0*
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/HostVG01-lv01--hostvg01 ext4 58M 1.3M 53M 3% /lv01
/dev/mapper/HostVG02-lv02--hostvg02 ext4 988M 2.7M 943M 1% /lv02
root@ubuntu:~#
2.10:VG扩容
首先查询可用的PV,使用PV对VG进行扩容
root@ubuntu:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 HostVG01 lvm2 a-- <3.00g 2.93g
/dev/sdb2 HostVG02 lvm2 a-- 2.98g 1.98g
我们发现目前仅有的两个PV都已经划分给VG了,所以我们需要创建新的PV,创建新的PV就必须有可用的分区,刚好我们之前创建了三个分区,有一个sdb3的还没使用
root@ubuntu:~# lsblk
loop0 7:0 0 96.5M 1 loop /snap/core/9436
...
...
loop18 7:18 0 2.2M 1 loop /snap/gnome-system-monitor/148
sda 8:0 0 20G 0 disk
└─sda1 8:1 0 20G 0 part /
sdb 8:16 0 10G 0 disk
├─sdb1 8:17 0 3G 0 part
│ └─HostVG01-lv01--hostvg01 253:0 0 64M 0 lvm /lv01
├─sdb2 8:18 0 3G 0 part
│ └─HostVG02-lv02--hostvg02 253:1 0 1G 0 lvm /lv02
└─sdb3 8:19 0 3.9G 0 part
sr0 11:0 1 1024M 0 rom
root@ubuntu:~#
将可用分区sbd3划分为物理卷PV
root@ubuntu:~# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created.
root@ubuntu:~#
root@ubuntu:~# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 HostVG01 lvm2 a-- <3.00g 2.93g
/dev/sdb2 HostVG02 lvm2 a-- 2.98g 1.98g
/dev/sdb3 lvm2 --- <3.88g <3.88g
root@ubuntu:~#
现在我们有了可用物理卷PV,给HostVG02卷组扩容
root@ubuntu:~# vgs
VG #PV #LV #SN Attr VSize VFree
HostVG01 1 1 0 wz--n- <3.00g 2.93g
HostVG02 1 1 0 wz--n- 2.98g 1.98g
root@ubuntu:~# vgextend HostVG02 /dev/sdb3
Volume group "HostVG02" successfully extended
root@ubuntu:~# vgs
VG #PV #LV #SN Attr VSize VFree
HostVG01 1 1 0 wz--n- <3.00g 2.93g
HostVG02 2 1 0 wz--n- <6.86g <5.86g
root@ubuntu:~#
3.1:创建LVM流程
3.2:删除LVM流程