LVM逻辑卷管理是Linux对磁盘分区进行管理的一种机制,普通磁盘无法实现动态扩展,而LVM就是将物理磁盘融合成一个巨大的存储池,用户可以按需求动态的调整磁盘的容量,使磁盘容量更好的被利用。
LVM的组成部分:
-
fs:file system文件系统。
-
pv(physical volume物理卷):物理卷是底层真正提供容量,一个物理卷可以是整个磁盘、或磁盘上的分区。
-
vg(volume group 卷组):卷组是可用物理硬盘的集合,可以逻辑地看成一块大硬盘,一个卷组由一个或多个物理卷组成。
-
lv(logical volume 逻辑卷):每个卷组中可以定义一个或多个逻辑卷,逻辑卷是位于物理分区上的信息集合。
-
PE(Physical Extend),我们的LVM磁盘管理中PE的默认大小是4M大小(可以指定大小),其实PE就是我们逻辑卷管理的最基本单位(一旦确定将不能改变)。比如说我有一个400M的硬盘,那么在将其格式化成PV的时候,其实际就是将这块物理硬盘划分成了100个的PE,因为PE默认的大小就是4M。
操作的前提是要安装工具lvm,一般的发行版里面都内置有该工具。
1、常用的lvm部署命令
任务 | PV阶段 | VG阶段 | LV阶段 | fileSystem(XFS/EXT4) |
---|---|---|---|---|
搜寻(scan) | pvscan | vgscan | lcscan | lsblk,blkid |
建立(create) | pvcreate | vgcreate | lvcreate | mkfs.xfs/mkfs.ext4 |
列出(display) | pvdisplay/pvs | vgdisplay/vgs | lvdisplay/lvs | df,mount |
增加(extend) | vgextend | lvextend(lvresize) | xfs_growfs/resize2fs | |
减少(reduce) | vgreduce | lvreduce(lvresize) | 不支持/resize2fs | |
删除(remove) | pvremove | vgremove | lvremove | umount,重新格式化 |
改变容量(resize) | lvresize | xfs_growfs/resize2fs | ||
改变属性(attribute) | pvchange | vgchange | lvchange | /etc/fstab,remount |
2、物理卷PV操作
如果磁盘下没有分区,可以使用磁盘名称,如果下面有分区,必须使用分区名称。
root@lckd:/dev# pvremove /dev/sdc1 /dev/sdd1 /dev/sde #添加3块磁盘创建物理卷Labels on physical volume "/dev/sdc1" successfully wiped.Labels on physical volume "/dev/sdd1" successfully wiped.Labels on physical volume "/dev/sde" successfully wiped.
root@lckd:/dev# pvs #查看物理卷PV VG Fmt Attr PSize PFree /dev/sdc1 lvm2 --- <10.00g <10.00g/dev/sdd1 lvm2 --- <15.00g <15.00g/dev/sde lvm2 --- 20.00g 20.00g
root@lckd:/dev# pvremove /dev/sde #删除其中一块磁盘Labels on physical volume "/dev/sde" successfully wiped.
root@lckd:/dev# pvsPV VG Fmt Attr PSize PFree /dev/sdc1 lvm2 --- <10.00g <10.00g/dev/sdd1 lvm2 --- <15.00g <15.00g
3、卷组VG操作
- 创建VG卷组
命令格式:
vgcreate -s [指定PE大小] [VG卷组名] [分区路径] [分区路径][.....]
root@lckd:/dev# pvsPV VG Fmt Attr PSize PFree /dev/sdc1 lvm2 --- <10.00g <10.00g/dev/sdd1 lvm2 --- <15.00g <15.00g/dev/sde lvm2 --- 20.00g 20.00g
root@lckd:/dev# vgcreate -s 4M my_vg /dev/sdc1 /dev/sdd1Volume group "my_vg" successfully created
root@lckd:/dev# vgsVG #PV #LV #SN Attr VSize VFree my_vg 2 0 0 wz--n- 24.99g 24.99g
不加-s默认就是4M。
- 添加新的PV到VG卷组
命令格式:
vgextend [卷组名] [物理卷分区]
root@lckd:/dev# vgextend my_vg /dev/sdeVolume group "my_vg" successfully extended
root@lckd:/dev# vgsVG #PV #LV #SN Attr VSize VFree my_vg 3 0 0 wz--n- <44.99g <44.99g
root@lckd:/dev# ^C
root@lckd:/dev# pvsPV VG Fmt Attr PSize PFree /dev/sdc1 my_vg lvm2 a-- <10.00g <10.00g/dev/sdd1 my_vg lvm2 a-- <15.00g <15.00g/dev/sde my_vg lvm2 a-- <20.00g <20.00g
- 移除单个PV
在VG卷组里移除一个PV,命令格式如下:
vgreduce [卷组名] [物理卷分区]
root@lckd:/dev# pvsPV VG Fmt Attr PSize PFree /dev/sdc1 my_vg lvm2 a-- <10.00g <10.00g/dev/sdd1 my_vg lvm2 a-- <15.00g <15.00g/dev/sde my_vg lvm2 a-- <20.00g <20.00g
root@lckd:/dev# vgreduce my_vg /dev/sdeRemoved "/dev/sde" from volume group "my_vg"
root@lckd:/dev# pvsPV VG Fmt Attr PSize PFree /dev/sdc1 my_vg lvm2 a-- <10.00g <10.00g/dev/sdd1 my_vg lvm2 a-- <15.00g <15.00g/dev/sde lvm2 --- 20.00g 20.00g
- 移除整个VG卷组
移除整个卷组,命令格式如下:
vgremove [卷组名]
root@lckd:/dev# vgremove my_vgVolume group "my_vg" successfully removed
root@lckd:/dev# pvsPV VG Fmt Attr PSize PFree /dev/sdc1 lvm2 --- <10.00g <10.00g/dev/sdd1 lvm2 --- <15.00g <15.00g/dev/sde lvm2 --- 20.00g 20.00g
- 移除空闲VG卷组
将卷组中空闲的PV从VG中移除,也就是如果该卷组空间为空,则移除掉,命令格式如下:
vgreduce -a [卷组名]
root@lckd:/dev# pvsPV VG Fmt Attr PSize PFree /dev/sdc1 my_vg lvm2 a-- <10.00g <10.00g/dev/sdd1 my_vg lvm2 a-- <15.00g <15.00g/dev/sde my_vg lvm2 a-- <20.00g <20.00g
root@lckd:/dev# vgreduce -a my_vgRemoved "/dev/sdc1" from volume group "my_vg"Removed "/dev/sdd1" from volume group "my_vg"Can't remove final physical volume "/dev/sde" from volume group "my_vg"
root@lckd:/dev# pvsPV VG Fmt Attr PSize PFree /dev/sdc1 lvm2 --- <10.00g <10.00g/dev/sdd1 lvm2 --- <15.00g <15.00g/dev/sde my_vg lvm2 a-- <20.00g <20.00g
分区为空的可以删除,整块磁盘为空的不能删除,要删除空的整块磁盘不加-a。
4、逻辑卷LV操作
- 创建逻辑卷
命令格式如下:
lvcreate -L [指定大小] -n [LV名字] [VG卷组:从哪个卷组里划分]
root@lckd:/dev# lvcreate -L 5G -n my_lv my_vg
WARNING: ext4 signature detected on /dev/my_vg/my_lv at offset 1080. Wipe it? [y/n]: yWiping ext4 signature on /dev/my_vg/my_lv.Logical volume "my_lv" created.
root@lckd:/dev# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convertmy_lv my_vg -wi-a----- 5.00g
root@lckd:/dev# vgsVG #PV #LV #SN Attr VSize VFree my_vg 2 1 0 wz--n- 29.99g 24.99g
- 格式化并挂载LV
测试LV是否正常工作,在创建好LVM以后需要手动格式化。
root@lckd:/home/lckd# mkdir new
root@lckd:/home/lckd# mkfs -t xfs /dev/mapper/my_vg-my_lv
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 1310720 4k blocks and 327680 inodes
Filesystem UUID: 99879dd1-00ee-4b72-930f-02782b9cd095
Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done root@lckd:/home/lckd# mount /dev/mapper/my_vg-my_lv /home/lckd/new/
root@lckd:/home/lckd# df -Th
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 792M 1.6M 791M 1% /run
/dev/sda2 ext4 49G 9.6G 37G 21% /
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sdb1 ext4 147G 2.1G 137G 2% /opt/Kingbase/ES/V8
tmpfs tmpfs 792M 4.0K 792M 1% /run/user/1000
/dev/mapper/my_vg-my_lv xfs 4.9G 24K 4.6G 1% /home/lckd/new
root@lckd:/home/lckd#
注意: 想让挂载长期生效,需要写入到配置文件/etc/fstab中。
root@lckd:/home/lckd# echo "/dev/mapper/my_vg-my_lv /home/lckd/new xfs defaults 0 0" >> /etc/fstab
root@lckd:/home/lckd# cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during curtin installation
/dev/disk/by-uuid/f0f129b3-a477-42e6-9ea2-4d7af21ea13c / ext4 defaults 0 1
/swap.img none swap sw 0 0
UUID=ccf80b6a-fbc8-481e-9736-5b9a9c2f85cf /opt/Kingbase/ES/V8 ext4 defaults 0 2
/dev/mapper/my_vg-my_lv /home/lckd/new xfs defaults 0 0
root@lckd:/home/lckd#
- LV容量增加(扩容)
将LV的容量增加5G的空间,需要注意扩展要先扩展LVM,然后再扩展文件系统,命令格式如下:
lvextend -L +5G[增加容量,可以是M,G] /dev/my_vg/my_lv[lv路径]
root@lckd:/home/lckd# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convertmy_lv my_vg -wi-ao---- 5.00g
root@lckd:/home/lckd# vgsVG #PV #LV #SN Attr VSize VFree my_vg 2 1 0 wz--n- 29.99g 24.99g
root@lckd:/home/lckd# lvextend -L +5G /dev/mapper/my_vg-my_lv #执行增加命令,从VG卷组划分5GSize of logical volume my_vg/my_lv changed from 5.00 GiB (1280 extents) to 10.00 GiB (2560 extents).Logical volume my_vg/my_lv successfully resized.
root@lckd:/home/lckd# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convertmy_lv my_vg -wi-ao---- 10.00g
root@lckd:/home/lckd# vgsVG #PV #LV #SN Attr VSize VFree my_vg 2 1 0 wz--n- 29.99g 19.99g
root@lckd:/home/lckd# xfs_growfs /dev/mapper/my_vg-my_lv #扩展文件系统
meta-data=/dev/mapper/my_vg-my_lv isize=512 agcount=4, agsize=327680 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=1, sparse=1, rmapbt=0= reflink=1 bigtime=0 inobtcount=0
data = bsize=4096 blocks=1310720, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log 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 1310720 to 2621440
root@lckd:/home/lckd# df -Th
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 792M 1.6M 791M 1% /run
/dev/sda2 ext4 49G 9.6G 37G 21% /
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sdb1 ext4 147G 2.1G 137G 2% /opt/Kingbase/ES/V8
tmpfs tmpfs 792M 4.0K 792M 1% /run/user/1000
/dev/mapper/my_vg-my_lv xfs 10G 104M 9.9G 2% /home/lckd/new
注意: 执行了扩展文件系统的命令之后,df -h看到的容量才会增加。xfs类型的系统使用命令xfs_growfs(后面加lv路径和挂载路径都可以),ext类型的文件系统使用命令resize2fs -f /dev/mapper/my_vg-my_lv。
- LV容量缩小(收缩)
将LV的容量缩小6G的空间,缩小要先卸载分区并检查分区,然后缩小文件系统,最后再缩小LVM。
root@lckd:/home/lckd# df -Th
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 792M 1.6M 791M 1% /run
/dev/sda2 ext4 49G 9.6G 37G 21% /
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sdb1 ext4 147G 2.1G 137G 2% /opt/Kingbase/ES/V8
tmpfs tmpfs 792M 4.0K 792M 1% /run/user/1000
/dev/mapper/my_vg-my_lv xfs 10G 104M 9.9G 2% /home/lckd/new
root@lckd:/home/lckd# umount /dev/mapper/my_vg-my_lv
root@lckd:/home/lckd# e2fsck -f /dev/mapper/my_vg-my_lv #检查文件系统
root@lckd:/home/lckd# lvreduce -L 4G /dev/mapper/my_vg-my_lv #缩小LV WARNING: Reducing active logical volume to 4.00 GiB.THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce my_vg/my_lv? [y/n]: ySize of logical volume my_vg/my_lv changed from 10.00 GiB (2560 extents) to 4.00 GiB (1024 extents).Logical volume my_vg/my_lv successfully resized.
root@lckd:/home/lckd# mkfs.xfs -f /dev/mapper/my_vg-my_lv
meta-data=/dev/mapper/my_vg-my_lv isize=512 agcount=4, agsize=262144 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=1, sparse=1, rmapbt=0= reflink=1 bigtime=0 inobtcount=0
data = bsize=4096 blocks=1048576, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
root@lckd:/home/lckd# mount /dev/mapper/my_vg-my_lv /home/lckd/new/
root@lckd:/home/lckd# df -Th #再次查看分区变化
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 792M 1.6M 791M 1% /run
/dev/sda2 ext4 49G 9.6G 37G 21% /
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sdb1 ext4 147G 2.1G 137G 2% /opt/Kingbase/ES/V8
tmpfs tmpfs 792M 4.0K 792M 1% /run/user/1000
/dev/mapper/my_vg-my_lv xfs 4.0G 61M 4.0G 2% /home/lckd/new
如果是ext类型的文件系统,在执行lvreduce命令之前执行命令:resize2fs -f /dev/mapper/my_vg-my_lv 4G(减小后的大小),后面挂载之前就不用再重新格式化lv了,直接挂载就行。
- 对LV拍摄快照
LV支持快照的拍摄与恢复,快照拍摄可以使用lvcreate命令。命令格式如下:
lvcreate [-s 快照] -n [快照名] -L [快照大小] [指定分区]
root@lckd:/home/lckd/new# lvcreate -s -n my_back -L 10M /dev/mapper/my_vg-my_lv Rounding up size to full physical extent 12.00 MiBLogical volume "my_back" created.
root@lckd:/home/lckd/new# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convertmy_back my_vg swi-a-s--- 12.00m my_lv 0.00 my_lv my_vg owi-aos--- 4.00g
-
对LV恢复快照
恢复LV快照时,第一步需要创建一个挂载点,然后将my_back挂载到挂载点上,从中拷贝出来即可。
root@lckd:/home/lckd/new# rm ./*
root@lckd:/home/lckd/new# df -Th
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 792M 1.6M 791M 1% /run
/dev/sda2 ext4 49G 9.6G 37G 21% /
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sdb1 ext4 147G 2.1G 137G 2% /opt/Kingbase/ES/V8
tmpfs tmpfs 792M 4.0K 792M 1% /run/user/1000
/dev/mapper/my_vg-my_lv xfs 4.0G 61M 4.0G 2% /home/lckd/new
root@lckd:/home/lckd/new# mount -o nouuid /dev/mapper/my_vg-my_back /home/lckd/back/
root@lckd:/home/lckd/new# ls /home/lckd/back/
555.txt
root@lckd:/home/lckd/new# df -TH
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 831M 1.7M 829M 1% /run
/dev/sda2 ext4 53G 11G 40G 21% /
tmpfs tmpfs 4.2G 0 4.2G 0% /dev/shm
tmpfs tmpfs 5.3M 0 5.3M 0% /run/lock
/dev/sdb1 ext4 158G 2.3G 148G 2% /opt/Kingbase/ES/V8
tmpfs tmpfs 831M 4.1k 831M 1% /run/user/1000
/dev/mapper/my_vg-my_back xfs 4.3G 64M 4.3G 2% /home/lckd/back
/dev/mapper/my_vg-my_lv xfs 4.3G 64M 4.3G 2% /home/lckd/new
因为 XFS 不允许相同的 UUID 文件系统的挂载,因此我们得要加上那个 nouuid 的参数,让文件系统忽略相同的 UUID 所造成的问题! 没办法啊!因为快照出来的文件系统当然是会一模一样的!