LVM
逻辑卷 (Logical Volume) 是由逻辑磁盘形成的虚拟盘, 也可称为磁盘分区.
用途: 解决分区空间不够用或者分区空间绰绰有余的情况.
逻辑卷的工作原理: 逻辑卷 (lv) 是将几个磁盘分区或者块设备组织起来形成一个大的扩展分区 (卷组), 该扩展分区不能直接用, 需要将其划分成逻辑卷(lv) 才能使用, lv 可以格式化成不同的文件系统, 挂载后直接使用. lv 的扩展和缩减是不会影响原有数据的, 但逻辑卷缩减的风险大于逻辑卷扩展的风险. 逻辑卷可以支持快照功能.
创建 LVM
1. 首先创建两个分区 / 硬盘来, 下例为 sdb1 分区和 sdc1 分区
- [root@centos7 ~]#fdisk -l /dev/sdb /dev/sdc
- Disk /dev/sdb: 161.1 GB, 161061273600 bytes, 314572800 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: 0x90c3db64
- Device Boot Start End Blocks Id System
- /dev/sdb1 2048 41945087 20971520 8e Linux LVM
- Disk /dev/sdc: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000c72ff
- Device Boot Start End Blocks Id System
- /dev/sdc1 2048 41945087 20971520 8e Linux LVM
2. 创建 PV 使用 pvcreate/dev/DEVICE 把两个分区创建为 PV, 用 pvs/pvdisplay 查看 PV 信息.
- [root@centos7 ~]#pvcreate /dev/sdb1 /dev/sdc1
- Physical volume "/dev/sdb1" successfully created.
- Physical volume "/dev/sdc1" successfully created.
- [root@centos7 ~]#pvdisplay
- "/dev/sdc1" is a new physical volume of "20.00 GiB"
- --- NEW Physical volume ---
- PV Name /dev/sdc1
- VG Name
- PV Size 20.00 GiB
- Allocatable NO
- PE Size 0
- Total PE 0
- Free PE 0
- Allocated PE 0
- PV UUID gIYSbC-QrkI-MRto-xMdP-PJoX-vq40-0JUhSK
- "/dev/sdb1" is a new physical volume of "20.00 GiB"
- --- NEW Physical volume ---
- PV Name /dev/sdb1
- VG Name
- PV Size 20.00 GiB
- Allocatable NO
- PE Size 0
- Total PE 0
- Free PE 0
- Allocated PE 0
- PV UUID ONZUYP-4RfT-HDny-c2QD-yi8R-ecsF-n76KkI
3. 创建 VG vgcreate "vgname" /dev/sd 创建 VG, 加 - s 可以指定 PE 大小, vgs/vgdisplay 查看 VG 信息
- [root@centos7 ~]#vgcreate vg1 /dev/sdb1 /dev/sdc1
- Volume group "vg1" successfully created
- [root@centos7 ~]#vgs
- VG #PV #LV #SN Attr VSize VFree
- vg1 2 0 0 wz--n- 39.99g 39.99g
4. 创建 LV lvcreate 常见选项如下
-n 指定 LV 的名称
-L 指定 lv 的大小
- [root@centos7 ~]#lvcreate -n lv_mysql -L 15G vg1
- Logical volume "lv_mysql" created.
- [root@centos7 ~]#lvdisplay
- --- Logical volume ---
- LV Path /dev/vg1/lv_mysql
- LV Name lv_mysql
- VG Name vg1
- LV UUID jycUPI-cTfZ-n1Pe-aHA7-g16p-2PMH-PkAA1F
- LV Write Access read/write
- LV Creation host, time centos7.5.localdomain, 2018-10-23 15:36:57 +0800
- LV Status available
- # open 0
- LV Size 15.00 GiB
- Current LE 3840
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 8192
- Block device 253:0
5. 设置文件系统, 挂载.
- [root@centos7 ~]#mkfs.ext4 /dev/vg1/lv_mysql
- [root@centos7 ~]#mount /dev/vg1/lv_mysql /mnt
删除 LV VG PV 命令
删除逻辑卷
lvremove/dev/VG_NAME/LV_NAME
删除卷组
先做 pvmove, 再做 vgremove
删除 pv
pvremove/dev/DEVICE
扩展 PVM
1. 如果正在使用的某个 lv 空间快要满的时候, 可以给 lv 增加空前, 前提是 VG 里有空闲的空间. lvextend -l 可以指定 pe 大小扩展空间, 可以用百分百表示,-L 直接写单位.
- [root@centos7 ~]#lvextend -l +50%FREE /dev/vg1/lv_mysql
- [root@centos7 ~]#lvextend -L +1G /dev/vg1/lv_mysql
2. 指定完需要增加空间的大小之后用 xfs_growfs 更新, etx 系列的文件系统用 resize2fs
- [root@centos7 ~]#xfs_growfs /dev/vg1/lv_mysql
- meta-data=/dev/mapper/vg1-lv_mysql isize=512 agcount=4, agsize=983040 blks
- = sectsz=512 attr=2, projid32bit=1
- = crc=1 finobt=0 spinodes=0
- data = bsize=4096 blocks=3932160, imaxpct=25
- = sunit=0 swidth=0 blks
- naming =version 2 bsize=4096 ascii-ci=0 ftype=1
- log =internal bsize=4096 blocks=2560, version=2
- = sectsz=512 sunit=0 blks, lazy-count=1
- realtime =none extsz=4096 blocks=0, rtextents=0
- data blocks changed from 3932160 to 10483712
为卷组扩展空间
1. 如果卷组的的空间不够了也可以扩展, 先看下卷组空间可用空间已经为 0
- [root@centos7 ~]#vgs
- VG #PV #LV #SN Attr VSize VFree
- vg1 2 1 0 wz--n- 39.99g 0
2. 先找一块硬盘, 把他设置为 PV,pvs 显示 sde 硬盘没有加入到任何卷组.
- [root@centos7 ~]#pvcreate /dev/sde
- WARNING: dos signature detected on /dev/sde at offset 510. Wipe it? [y/n]: y
- Wiping dos signature on /dev/sde.
- Physical volume "/dev/sde" successfully created.
- [root@centos7 ~]#pvs
- PV VG Fmt Attr PSize PFree
- /dev/sdb1 vg1 lvm2 a-- <20.00g 0
- /dev/sdc1 vg1 lvm2 a-- <20.00g 0
- /dev/sde lvm2 --- 50.00g 50.00g
3. 把硬盘加入到指定的卷组 vgextend
- [root@centos7 ~]#vgextend vg1 /dev/sde
- Volume group "vg1" successfully extended
- [root@centos7 ~]#vgs
- VG #PV #LV #SN Attr VSize VFree
- vg1 3 1 0 wz--n- <89.99g <50.00g
4. 卷组扩展完毕之后就可以再给卷组里其他逻辑卷扩展空间了
lvextend -r -L 100G /dev/vg1/lv_mysql
5. 删除卷组里的 pv 设备 vgreduce
- [root@centos7 ~]#vgreduce vg1 /dev/sde
- Removed "/dev/sde" from volume group "vg1"
缩减逻辑卷
只有在 ext 系列上才可以缩减逻辑卷, 在 xfs 的文件系统上不支持此操作.
操作步骤
1, 取消挂载
2, 先缩减文件系统
3, 再缩减逻辑卷大小
(1)卸载逻辑卷
(2)缩减文件系统为 20Gresize2fs
- [root@centos7 ~]#resize2fs /dev/vg1/lv_mysql 20G
- resize2fs 1.42.9 (28-Dec-2013)
- resize2fs: New size smaller than minimum (64422)
3. 缩减逻辑卷 lvreduce
- [root@centos7 ~]#lvreduce -L 20G /dev/vg1/lv_mysql
- WARNING: Reducing active logical volume to 20.00 GiB.
- THIS MAY DESTROY YOUR DATA (filesystem etc.)
- Do you really want to reduce vg1/lv_mysql? [y/n]: y
- Size of logical volume vg1/lv_mysql changed from 39.99 GiB (10238 extents) to 20.00 GiB (5120 extents).
- Logical volume vg1/lv_mysql successfully resized.
4. 重新挂载逻辑卷
逻辑卷命令
lvextend -r -L : 扩展逻辑卷空间使用
lvremove : 删除逻辑卷
resize2fs : 缩减文件系统大小
lvreduce -L: 缩减逻辑卷大小
vgextend : 扩展卷组, 把新的 pv 加入到卷组
- resize2fs /dev/vg/ ext4
- xfs_growfs /dev/vg/ xfs
vgreduce : 删除卷组里的 pv 设备
vgrename : 卷组改名
pvmove : 可以把 pv 上使用的 pe 迁移到相同卷组的其他 pv 上
pcscan : 扫描
迁移逻辑卷
- [root@centos7 ~]#pvmove /dev/sdc1
- /dev/sdc1: Moved: 100.00%
迁移 vg1 卷组, 查看下卷组下的 pv
- [root@centos7 ~]#pvdisplay
- --- Physical volume ---
- PV Name /dev/sdb1
- VG Name vg1
- PV Size 20.00 GiB / not usable 4.00 MiB
- Allocatable yes
- PE Size 4.00 MiB
- Total PE 5119
- Free PE 2559
- Allocated PE 2560
- PV UUID ONZUYP-4RfT-HDny-c2QD-yi8R-ecsF-n76KkI
1. 首先取消挂载
2. 把卷组设置为禁用状态 vgchange-an 激活为 - ay
- [root@centos7 ~]#vgchange -an vg1
- 0 logical volume(s) in volume group "vg1" now active
3. 把卷组下的逻辑卷设置为导出状态 vgexport vg1 导入为 vgimport vg
- [root@centos7 ~]#vgexport vg1
- Volume group "vg1" successfully exported
- [root@centos7 ~]#lvdisplay
- Volume group vg1 is exported
4. 设置为导出状态之后, 就可以把磁盘拔下来插入到其他服务器上面, 然后再把导出状态改为导入, 把禁用状态激活即可
LVM 快照
快照就是将当时的系统信息记录下来, 就好像照相一般, 若将来有任何数据改动了, 则原始数据会被移动到快照区, 没有改动的区域则由快照区和文件系统共享.
建立快照的卷大小只需要原始逻辑卷的 15%~20% 就够了, 也可以使用 lvextend 放大快照
快照只会保留原有的文件, 新建的文件没有
创建一个快照
基于 ext 系统
也是用 lvcreate; -s 指明是一个逻辑卷的快照
- [root@centos7 lv]#lvcreate -n kuaizhao -s -L 2G /dev/vg1/lv_mysql
- Logical volume "kuaizhao" created.
- root@centos7 lv]#lvdisplay
- --- Logical volume ---
- LV Path /dev/vg1/lv_mysql
- LV Name lv_mysql
- VG Name vg1
- LV UUID jycUPI-cTfZ-n1Pe-aHA7-g16p-2PMH-PkAA1F
- LV Write Access read/write
- LV Creation host, time centos7.5.localdomain, 2018-10-23 15:36:57 +0800
- LV snapshot status source of
- kuaizhao [active]
- LV Status available
- # open 1
- LV Size 10.00 GiB
- Current LE 2560
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 8192
- Block device 253:0
- --- Logical volume ---
- LV Path /dev/vg1/kuaizhao
- LV Name kuaizhao
- VG Name vg1
- LV UUID dk8cT0-Ykl5-p3CW-LGsc-KxIA-3oMl-iESK0J
- LV Write Access read/write
- LV Creation host, time centos7.5.localdomain, 2018-10-23 17:51:11 +0800
- LV snapshot status active destination for lv_mysql
- LV Status available
- # open 0
- LV Size 10.00 GiB
- Current LE 2560
- COW-table size 2.00 GiB
- COW-table LE 512
- Allocated to snapshot 0.01%
- Snapshot chunk size 4.00 KiB
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 8192
- Block device 253:3
还原快照
还原快照先取消挂载
还原快照用 lvconvert--merge + 快照路径命令可以把快照还原到原来的逻辑卷
- [root@centos7 mnt]#lvconvert --merge /dev/vg1/kuaizhao
- Merging of volume vg1/kuaizhao started.
- vg1/lv_mysql: Merged: 100.00%
基于 xfs
xfs 系统里创建好快照之后, 在挂载的时候要使用 mount -o nouuid 不检查 uuid, 因为创建的快照 UUID 和逻辑卷的 UUID 是相同的
删除快照
lvremove /dev/vg1/kuaizhao
来源: http://www.bubuko.com/infodetail-2886633.html