lvm概念
- LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制。LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区,如:将若干个磁盘分区连接为一个整块的卷组 (volumegroup),形成一个存储池。管理员可以在卷组上随意创建逻辑卷组(logicalvolumes),并进一步在逻辑卷组上创建文件系统。管理员通过LVM可以方便的调整存储卷组的大小,并且可以对磁盘存储按照组的方式进行命名、管理和分配。
创建LVM
- 第一步:把/dev/vdb5更改分区类型为8e
[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.
Command (m for help): t
Partition number (1-5, default 5): 5
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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
Disk label type: dos
Disk identifier: 0x3250bf3e
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 616447 204800 83 Linux
/dev/vdb3 616448 1230847 307200 83 Linux
/dev/vdb4 1230848 20971519 9870336 5 Extended
/dev/vdb5 1232896 1437695 102400 8e Linux LVM
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe
- 第二步:把物理分区做成物理卷
pvcreate /dev/vdb5 ##创建
pvs ##查看pv
- 第三步:创建卷组
vgcreate vg0 /dev/vdb5 ##卷组名称随意
vgs ##查看卷组
- 第四步:创建逻辑卷
lvcreate -L 10M -n lv0 vg0 ##-L逻辑卷容量的大小,-n表示逻辑卷的名称。
- 第五步:格式化逻辑卷
mkfs.xfs /dev/vg0/lv0
- 第六步:挂载逻辑卷
mount /dev/vg0/lv0 /mnt
扩展LVM
- 第一种情况:当卷组vg上有剩余空间时,先扩展逻辑卷,再扩展文件系统
lvextend -L 50M /dev/vg0/lv0 ##扩展到50M
xfs_growfs /dev/vg0/lv0 ##扩展文件系统
- 第二种情况:当卷组vg中的容量不够时,先扩展物理卷pv的大小,增加新的物理卷pv到物理卷组vg中
(1)添加新分区
[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.
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (1439744-20971519, default 1439744):
Using default value 1439744
Last sector, +sectors or +size{K,M,G} (1439744-20971519, default 20971519): +100M
Partition 6 of type Linux and of size 100 MiB is set
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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
Disk label type: dos
Disk identifier: 0x3250bf3e
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 616447 204800 83 Linux
/dev/vdb3 616448 1230847 307200 83 Linux
/dev/vdb4 1230848 20971519 9870336 5 Extended
/dev/vdb5 1232896 1437695 102400 8e Linux LVM
/dev/vdb6 1439744 1644543 102400 83 Linux
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# partprobe
(2)创建新的物理卷
pvcreate /dev/vdb6
(3)将新的物理卷添加到物理卷组中
vgextend vg0 /dev/vdb6
(4)扩展逻辑卷,再扩展文件系统
lvextend -L 150M /dev/vg0/lv0
xfs_growfs /dev/dev/vg0/lv0
基于ext4格式的LVM拉伸与缩减
- xfs只支持拉神,不支持压缩。而ext4支持拉伸和压缩。
拉伸LVM
- 第一步:要先卸载,重新格式化逻辑卷,再挂载逻辑卷
umount /mnt
mkfs.ext4 /dev/vg0/lv0 ##将逻辑卷格式化为ext4格式
mount /dev/vg0/lv0 /mnt
blkid
- 第二步:拉伸逻辑卷
lvextend -L 160M /dev/vg0/lv0
- 第三步:扩展文件系统
resize2fs /dev/vg0/lv0 ##resize2fs:调整ext4文件系统的大小,在未挂载情况下,放大或缩小文件系统的大小
缩减LVM
- 第一步:缩减逻辑卷不能在线缩减,所以要先解挂,一定要先取消挂载!
umount /dev/vg0/lv0 ##先卸载
- 第二步:对逻辑卷强制执行文件系统检测
e2fsck -f /dev/vg0/lv0 ##检测文件系统
- 第三步:缩减文件系统
resize2fs /dev/vg0/lv0 80M ##缩减文件系统到80M
- 第四步:挂载并缩减逻辑卷
mount /dev/vg0/lv0 /mnt/
lvreduce -L 100M /dev/vg0/lv0 ##缩减lv0到80M
缩减,删除VG(节省资源池)
- 第一步:将要删除的设备上数据迁移到另一个设备
pvmove /dev/vdb6 /dev/vdb5
- 第二步:将设备从卷组中删除
vgreduce vg0 /dev/vdb6
- 第三步:移除设备
pvremove /dev/vdb6
逻辑卷的快照
- 逻辑卷快照是 LVM 存储的另一项灵活功能。
这项功能很类似于虚拟机软件的还原时间点功能。例如可以对某一个LV逻辑卷设备做一次快照,如果今后发现数据被改错了,咱们可以把之前做好的快照卷进行覆盖还原。 - 特点:
1.快照卷的容量必须与逻辑卷的容量相同
2.快照卷仅一次有效,在执行还原后就自动删除了
创建逻辑卷的快照
lvcreate -L 20M -n lv0-backup -s /dev/vg0/lv0 ##创建逻辑卷快照
mount /dev/vg0/lv0-backup /mnt/ ##挂载
删除逻辑卷的快照
umount /mnt/
lvremove /dev/vg0/lv0-backup
逻辑卷的删除
umount /mnt/ ##取消挂载
lvremove /dev/vg0/lv0-backup ##删除快照
lvremove /dev/vg0/lv0 ##删除逻辑卷
vgremove vg0 ##删除物理卷组
pvremove /dev/vdb2 ##删除物理卷