逻辑卷管理器总结
Posted chailonggang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了逻辑卷管理器总结相关的知识,希望对你有一定的参考价值。
逻辑卷管理器总结
内容概述:
- 创建逻辑卷
- 逻辑卷的扩展
- 逻辑卷的缩减
- 逻辑卷管理
- 逻辑卷快照
- 拆卸更换硬盘
- 删除逻辑卷
LVM: Logical Volume Manager 可以允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小
允许在多个物理设备间重新组织文件系统
LVM可以弹性的更改LVM的容量
通过交换PE来进行资料的转换,将原来LV内的PE转移到其他的设备中以降低LV的容量,或将其他设备
中的PE加到LV中以加大容量
实现过程:
将设备指定为物理卷
用一个或者多个物理卷来创建一个卷组,物理卷是用固定大小的物理区域(Physical Extent, PE)来定义的
在物理卷上创建的逻辑卷, 是由物理区域(PE)组成
可以在逻辑卷上创建文件系统并挂载
范例:创建逻辑卷及逻辑卷的扩展
创建逻辑卷的相关工具(来自于lvm2包)
[root@centos8 ~]#yum -y install lvm
[16:16:03 root@centos8 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 50G 0 part /data
sdb 8:16 0 2G 0 disk
sdc 8:32 0 10G 0 disk
└─sdc1 8:33 0 500M 0 part
sr0 11:0 1 7G 0 rom
1.把/dev/sdb /dev/sdc1创建为物理卷
[16:16:09 root@centos8 ~]#pvcreate /dev/sdb /dev/sdc1
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc1" successfully created.
[16:16:37 root@centos8 ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdb lvm2 --- 2.00g 2.00g
/dev/sdc1 lvm2 --- 500.00m 500.00m
2.创建卷组
[16:17:09 root@centos8 ~]#vgcreate vg_mysql /dev/sdb /dev/sdc1
Volume group "vg_mysql" successfully created
3.在物理卷上创建的逻辑卷
[16:22:32 root@centos8 ~]#lvcreate -n lv_data -L 1.2G vg_mysql
Rounding up size to full physical extent 1.20 GiB
Logical volume "lv_data" created.
4.指定文件系统
[16:26:29 root@centos8 ~]#mkfs.ext4 /dev/vg_mysql/lv_data
5.创建文件夹进行挂载
[16:29:14 root@centos8 ~]#mkdir /mysql/data -pv
mkdir: created directory ‘/mysql‘
mkdir: created directory ‘/mysql/data‘
6.查看逻辑卷UUID并写到/etc/fstab配置文件中使其持久挂载
[21:17:56 root@centos8 ~]#blkid /dev/mapper/vg_mysql-lv_data
/dev/mapper/vg_mysql-lv_data: UUID="60a87f5a-d686-4003-a06b-ecf353be6b63" TYPE="ext4"
[21:17:56 root@centos8 ~] vim /etc/fstab
UUID=60a87f5a-d686-4003-a06b-ecf353be6b63 /mysql/data ext4 defaults 0 0
7.使新挂载的逻辑卷生效
[16:31:57 root@centos8 ~]#mount -a
8.逻辑卷创建完成
[16:32:00 root@centos8 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 463M 0 463M 0% /dev
tmpfs 479M 0 479M 0% /dev/shm
tmpfs 479M 6.7M 472M 2% /run
tmpfs 479M 0 479M 0% /sys/fs/cgroup
/dev/sda2 100G 3.3G 97G 4% /
/dev/sda5 50G 390M 50G 1% /data
/dev/sda1 976M 112M 798M 13% /boot
tmpfs 96M 0 96M 0% /run/user/0
/dev/mapper/vg_mysql-lv_data 1.2G 3.7M 1.1G 1% /mysql/data
9.逻辑卷扩展 执行下面命令使其容量不足
[16:32:03 root@centos8 ~]#dd if=/dev/zero of=/mysql/data/big.img bs=1M count=900
900+0 records in
900+0 records out
943718400 bytes (944 MB, 900 MiB) copied, 2.85807 s, 330 MB/s
[16:34:40 root@centos8 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 463M 0 463M 0% /dev
tmpfs 479M 0 479M 0% /dev/shm
tmpfs 479M 6.7M 472M 2% /run
tmpfs 479M 0 479M 0% /sys/fs/cgroup
/dev/sda2 100G 3.3G 97G 4% /
/dev/sda5 50G 390M 50G 1% /data
/dev/sda1 976M 112M 798M 13% /boot
tmpfs 96M 0 96M 0% /run/user/0
/dev/mapper/vg_mysql-lv_data 1.2G 904M 200M 82% /mysql/data
10.把卷组剩余的容量全部扩展给逻辑卷
[16:36:19 root@centos8 ~]#lvextend -l +100%free /dev/mapper/vg_mysql-lv_data
Size of logical volume vg_mysql/lv_data changed from 1.20 GiB (308 extents) to 2.48 GiB (635 extents).
Logical volume vg_mysql/lv_data successfully resized.
11.同步文件系统 resize2fs命令只同步extx系列 如果是xfs文件系统用xfs_growfs /mountpoint 命令扩展
[16:37:27 root@centos8 ~]#resize2fs /dev/vg_mysql/lv_data
resize2fs 1.44.6 (5-Mar-2019)
Filesystem at /dev/vg_mysql/lv_data is mounted on /mysql/data; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg_mysql/lv_data is now 650240 (4k) blocks long.
12.容量扩展成功
[16:39:14 root@centos8 ~]#df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 463M 0 463M 0% /dev
tmpfs tmpfs 479M 0 479M 0% /dev/shm
tmpfs tmpfs 479M 6.7M 472M 2% /run
tmpfs tmpfs 479M 0 479M 0% /sys/fs/cgroup
/dev/sda2 xfs 100G 3.3G 97G 4% /
/dev/sda5 xfs 50G 390M 50G 1% /data
/dev/sda1 ext4 976M 112M 798M 13% /boot
tmpfs tmpfs 96M 0 96M 0% /run/user/0
/dev/mapper/vg_mysql-lv_data ext4 2.5G 904M 1.5G 39% /mysql/data
13.卷组没容量时把sdc剩余的空间再次扩展
[16:44:06 root@centos8 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 50G 0 part /data
sdb 8:16 0 2G 0 disk
└─vg_mysql-lv_data 253:0 0 2.5G 0 lvm /mysql/data
sdc 8:32 0 10G 0 disk
├─sdc1 8:33 0 500M 0 part
│ └─vg_mysql-lv_data 253:0 0 2.5G 0 lvm /mysql/data
└─sdc2 8:34 0 9.5G 0 part
sr0 11:0 1 7G 0 rom
14.把sdc2加进vg_mgssql也就是扩展卷组
[16:45:04 root@centos8 ~]#vgextend vg_mysql /dev/sdc2
Volume group "vg_mysql" successfully extended
15.同步文件系统 (加-r选项不仅扩展了空间也同步了文件系列 就不用再用resize2fs,xfs_growfs /mountpoint这两个命令单独同步了)
[16:46:16 root@centos8 ~]#lvextend -r -L +1G /dev/vg_mysql/lv_data
Size of logical volume vg_mysql/lv_data changed from 2.48 GiB (635 extents) to 3.48 GiB (891 extents).
Logical volume vg_mysql/lv_data successfully resized.
resize2fs 1.44.6 (5-Mar-2019)
Filesystem at /dev/mapper/vg_mysql-lv_data is mounted on /mysql/data; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/vg_mysql-lv_data is now 912384 (4k) blocks long.
16.容量扩展成功,如果容量又不足了重复此操作
[16:47:37 root@centos8 ~]#df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 463M 0 463M 0% /dev
tmpfs tmpfs 479M 0 479M 0% /dev/shm
tmpfs tmpfs 479M 6.7M 472M 2% /run
tmpfs tmpfs 479M 0 479M 0% /sys/fs/cgroup
/dev/sda2 xfs 100G 3.3G 97G 4% /
/dev/sda5 xfs 50G 390M 50G 1% /data
/dev/sda1 ext4 976M 112M 798M 13% /boot
tmpfs tmpfs 96M 0 96M 0% /run/user/0
/dev/mapper/vg_mysql-lv_data ext4 3.4G 905M 2.4G 28% /mysql/data
范例:比较逻辑和普通分区的速度 可以看出逻辑卷速度明显较快
[16:50:21 root@centos8 ~]#dd if=/dev/zero of=/mysql/data/test.img bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 1.21475 s, 884 MB/s
[16:51:07 root@centos8 ~]#dd if=/dev/zero of=/data/test.img bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 1.99995 s, 537 MB/s
范例:逻辑卷的缩减(注意:缩减有数据损坏的风险,建议先备份再缩减,xfs文件系统不支持缩减)
[10:08:46 root@centos8 ~]#blkid /dev/vg0/mysql
/dev/vg0/mysql: UUID="7a92ec02-a7cb-46a7-a869-357ef54d0bbc" TYPE="ext4"
[10:18:05 root@centos8 ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mysql vg0 -wi-a----- 2.99g
[10:11:17 root@centos8 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 463M 0 463M 0% /dev
tmpfs 479M 0 479M 0% /dev/shm
tmpfs 479M 6.7M 472M 2% /run
tmpfs 479M 0 479M 0% /sys/fs/cgroup
/dev/sda2 100G 2.3G 98G 3% /
/dev/sda5 50G 390M 50G 1% /data
/dev/sda1 976M 112M 798M 13% /boot
tmpfs 96M 0 96M 0% /run/user/0
/dev/mapper/vg0-mysql 2.9G 9.0M 2.8G 1% /data/mysql
1.取消挂载
[10:16:16 root@centos8 ~]#umount /data/mysql/
2.检查文件系统的完整性
[10:16:17 root@centos8 ~]#e2fsck -f /dev/vg0/mysql
e2fsck 1.44.6 (5-Mar-2019)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg0/mysql: 14/196224 files (0.0% non-contiguous), 31009/784384 blocks
3.缩减文件系统
[10:17:36 root@centos8 ~]#resize2fs /dev/vg0/mysql 1G
resize2fs 1.44.6 (5-Mar-2019)
Resizing the filesystem on /dev/vg0/mysql to 262144 (4k) blocks.
The filesystem on /dev/vg0/mysql is now 262144 (4k) blocks long.
4.缩减逻辑卷
[10:21:10 root@centos8 ~]#lvreduce -L 1G /dev/vg0/mysql
WARNING: Reducing active logical volume to 1.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/mysql? [y/n]: y
Size of logical volume vg0/mysql changed from 2.99 GiB (766 extents) to 1.00 GiB (256 extents).
Logical volume vg0/mysql successfully resized.
5.重新恢复挂载
[10:21:52 root@centos8 ~]#mount
[10:24:17 root@centos8 ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mysql vg0 -wi-ao---- 1.00g
[10:24:26 root@centos8 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 463M 0 463M 0% /dev
tmpfs 479M 0 479M 0% /dev/shm
tmpfs 479M 6.7M 472M 2% /run
tmpfs 479M 0 479M 0% /sys/fs/cgroup
/dev/sda2 100G 2.3G 98G 3% /
/dev/sda5 50G 390M 50G 1% /data
/dev/sda1 976M 112M 798M 13% /boot
tmpfs 96M 0 96M 0% /run/user/0
/dev/mapper/vg0-mysql 944M 7.5M 870M 1% /data/mysql
范例:拆卸更换硬盘以及删除逻辑卷
1.移走/dev/sdb1上面的数据
[11:35:22 root@centos8 mysql]#pvmove /dev/sdb1
/dev/sdb1:Moved:60.00%
/dev/sdb1:Moved:100.00%
2.从卷组中删除/dev/sdb1
[11:35:22 root@centos8 mysql]#vgreduce vg0 /dev/sdb1
Removed "/dev/sdb1" from volume group "vg0"
3.从物理卷中删除/dev/sdb1
[10:37:36 root@centos8 ~]#pvremove /dev/sdb1
Labels on physical volume "/dev/sdb1" successfully wiped.
[11:51:33 root@centos8 ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdc lvm2 --- <2.00g 520.00g
4.拆除/dev/sdb硬盘 更换新的硬盘(同上)
5.删除逻辑卷
1)取消挂载
[11:41:33 root@centos8 ~]#umount /data/*
2)删除逻辑卷
[11:50:27 root@centos8 ~]#lvremove /dev/vg0/*
Do you really want to remove active logical volume vg0/mysql? [y/n]: y
Logical volume "mysql" successfully removed
Do you really want to remove active logical volume vg0/binlog? [y/n]: y
Logical volume "binlog" successfully removed
3)删除卷组
[11:51:10 root@centos8 ~]#vgremove vg0
Volume group "vg0" successfully removed
4)删除物理卷
[11:51:39 root@centos8 ~]#pvremove /dev/sdc
Labels on physical volume "/dev/sdc" successfully wiped.
5)逻辑卷删除完成
[11:51:53 root@centos8 ~]#pvs
[11:51:56 root@centos8 ~]#blkid
/dev/sda1: UUID="377d3ae3-d461-4b9a-bc5d-361f0ea59252" TYPE="ext4" PARTUUID="6b9d8abc-01"
/dev/sda2: UUID="40baacb1-d7d2-4f10-9e4a-97aeb1fc149e" TYPE="xfs" PARTUUID="6b9d8abc-02"
/dev/sda3: UUID="bc09f73c-61fe-4ff0-b47e-5b177b0764bb" TYPE="swap" PARTUUID="6b9d8abc-03"
/dev/sda5: UUID="ce7a0445-cc13-4e9b-b52f-83b46bf24a4b" TYPE="xfs" PARTUUID="6b9d8abc-05"
/dev/sr0: UUID="2020-01-03-21-42-40-00" LABEL="CentOS-8-1-1911-x86_64-dvd" TYPE="iso9660" PTUUID="087a73cc" PTTYPE="dos"
/dev/sdb: PTUUID="88b1ac1c" PTTYPE="dos"
[12:00:43 root@centos8 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 2G 0 part [SWAP]
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 50G 0 part /data
sdb 8:16 0 20G 0 disk
sdc 8:32 0 2G 0 disk
sr0 11:0 1 7G 0 rom
逻辑卷快照工作原理
- 在生成快照时会分配给它一定的空间,但只有在原来的逻辑卷或者快照有所改变才会使用这些空间
- 当原来的逻辑卷中有所改变时,会将旧的数据复制到快照中
- 快照中只含有原来的逻辑卷中更改的数据或者自生成快照后的快照中更改的数据
快照特点:
- 备份速度快,瞬间完成
- 应用场景是测试环境,不能完成代替备份
- 快照后,逻辑卷的修改速度会一定有影响
范例:为现有逻辑卷创建快照
1.逻辑卷快照
mkfs.xfs /dev/vg0/data
mount /dev/vg0/data/ /mnt/data
2.创建逻辑卷现有快照
lvcreate -l 64 -s -n data-snapshot -p r /dev/vg0/data
3.挂载快照
mkdir -p /mnt/snap
mount -o ro /dev/vg0/data-snapshot /mnt/snap
4.恢复快照
umount /dev/vg0/data-snapshot
umount /dev/vg0/data
lvconvert --merge /dev/vg0/data-snapshot
5.删除快照
umount /mnt/databackup
lvremove /dev/vg0/databackup
以上是关于逻辑卷管理器总结的主要内容,如果未能解决你的问题,请参考以下文章