Linux磁盘管理之LVM的使用
一、lvm介绍
在我们管理linux磁盘的时候,通常会遇到这么一种情况。在最初规划linux的磁盘的时候,我们给某个分区划分了一定量的磁盘空间,使用一段时间后,发现我们规划的磁盘空间不足了,这个时候怎么办?我们肯定要想办法给扩容呀,找一块磁盘分区,格式化,然后挂载,然后把数据拷贝到新加的磁盘上,然后把原有的磁盘卸载掉,通常我们是这样做的。这样一来虽然达到了扩容的目的,但是费时费力;如果第二次扩容,空间还是不够,我们又要重复上面的步骤;如果分配的磁盘空间过大,使用了很久才发现它根本用不到那么多空间,这个时候是不是又要调整呀,听着就感觉很繁琐。这时候你肯定要想有没有那种可挂载上一块盘,我们利用某种管理手段,可以在线的调整磁盘的空间,这样有节约了时间,也可实现弹性管理磁盘。对,lvm就有这样的功能。
lvm(logical volume manager)逻辑卷管理,它可以弹性的管理文件系统的容量,lvm的工作原理是将一个或多个底层块设备组织成一个逻辑设备,这个逻辑设备可以在未来新增或移除其他的分区到这个逻辑设备中,从而实现了弹性管理磁盘。
pv:物理卷(physical volume);物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如raid),是lvm的基本储存逻辑块,但和基本的物理存储介质(如分区,磁盘等)比较,却包含有与lvm相关的管理参数
vg:卷组(volume group)lvm卷组类似于非lvm系统中的物理硬盘,尤其物理卷组成,可以在卷组上创建一个或多个lvm分区(逻辑卷),lvm卷组由一个或多个物理卷组成
lv:逻辑卷(logical volume)lvm的逻辑卷类似非lvm系统中的硬盘分区,在逻辑卷之上可以创建文件系统。
lvm就是将一些零碎的磁盘分区(pv)合并成一个较大的磁盘(vg),然后在根据需要对这个较大的磁盘(vg)进行划分成不同的大小分区(lv),这些小的分区(lv)是可以动态的扩展和缩小的,这就是lvm的逻辑卷管理过程。所谓lvm的大磁盘就是将许多pv整合成的vg,那么这个大磁盘最大可达到多少容量呢?我们知道在文件系统里面数据存储的最小单位是块(block),不过在lvm当中数据储存的最小单位是pe,pe的的作用同block的作用一样,需要注意到是lvm默认使用的pe大小是4m,而lvm的vg最多能包含65534个pe,也就是说默认lvm的vg会有4×65534m/1024m=256g,所以pe的大小决定了vg的最大容量,当然我们在创建vg的时候可指定pe大小,从而来决定vg的大小。
二、lvm实现
1)前期准备几块硬盘或者分区来模拟pv
[root@test-centos7-node1 ~]# lsblk name maj:min rm size ro type mountpoint sda 8:0 0 50g 0 disk ├─sda1 8:1 0 2g 0 part /boot ├─sda2 8:2 0 2g 0 part [swap] └─sda3 8:3 0 46g 0 part / sdb 8:16 0 50g 0 disk sr0 11:0 1 792m 0 rom [root@test-centos7-node1 ~]# fdisk /dev/sdb 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. device does not contain a recognized partition table building a new dos disklabel with disk identifier 0x6180123f. command (m for help): p disk /dev/sdb: 53.7 gb, 53687091200 bytes, 104857600 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: 0x6180123f device boot start end blocks id system command (m for help): n partition type: p primary (0 primary, 0 extended, 4 free) e extended select (default p): p partition number (1-4, default 1): first sector (2048-104857599, default 2048): using default value 2048 last sector, +sectors or +size{k,m,g} (2048-104857599, default 104857599): +10g partition 1 of type linux and of size 10 gib is set command (m for help): p disk /dev/sdb: 53.7 gb, 53687091200 bytes, 104857600 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: 0x6180123f device boot start end blocks id system /dev/sdb1 2048 20973567 10485760 83 linux command (m for help): t selected partition 1 hex code (type l to list all codes): 8e changed type of partition 'linux' to 'linux lvm' command (m for help): n partition type: p primary (1 primary, 0 extended, 3 free) e extended select (default p): p partition number (2-4, default 2): first sector (20973568-104857599, default 20973568): using default value 20973568 last sector, +sectors or +size{k,m,g} (20973568-104857599, default 104857599): +10g partition 2 of type linux and of size 10 gib is set command (m for help): t partition number (1,2, default 2): hex code (type l to list all codes): 8e changed type of partition 'linux' to 'linux lvm' command (m for help): n partition type: p primary (2 primary, 0 extended, 2 free) e extended select (default p): p partition number (3,4, default 3): first sector (41945088-104857599, default 41945088): using default value 41945088 last sector, +sectors or +size{k,m,g} (41945088-104857599, default 104857599): +10g partition 3 of type linux and of size 10 gib is set command (m for help): t partition number (1-3, default 3): hex code (type l to list all codes): 8e changed type of partition 'linux' to 'linux lvm' command (m for help): p disk /dev/sdb: 53.7 gb, 53687091200 bytes, 104857600 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: 0x6180123f device boot start end blocks id system /dev/sdb1 2048 20973567 10485760 8e linux lvm /dev/sdb2 20973568 41945087 10485760 8e linux lvm /dev/sdb3 41945088 62916607 10485760 8e linux lvm command (m for help): w the partition table has been altered! calling ioctl() to re-read partition table. syncing disks. [root@test-centos7-node1 ~]# lsblk name maj:min rm size ro type mountpoint sda 8:0 0 50g 0 disk ├─sda1 8:1 0 2g 0 part /boot ├─sda2 8:2 0 2g 0 part [swap] └─sda3 8:3 0 46g 0 part / sdb 8:16 0 50g 0 disk ├─sdb1 8:17 0 10g 0 part ├─sdb2 8:18 0 10g 0 part └─sdb3 8:19 0 10g 0 part sr0 11:0 1 792m 0 rom [root@test-centos7-node1 ~]#
说明:这里是用的分区来模拟硬盘当作pv,这里需要注意一点,就是我们分区完保存后,可能存在不同不到问题,就是我们分区完用lsblk看不到分区,可用 partprobe来同步分区信息。centos6 使用partx -a /dev/sdb来增加分区同步到内存,如果是删除某个分区,就需要执行partx -d -nr # /dev/sdb 此命令表示从/dev/sdb的分区信息中删除sdb#分区信息,并同步到内存中。这里还需要注意一点的是我们分区的类型要选8e 表示linux lvm
2)装lvm2这个包
[root@test-centos7-node1 ~]# lvs -bash: lvs: command not found [root@test-centos7-node1 ~]# yum info lvm2 loaded plugins: fastestmirror loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com available packages name : lvm2 arch : x86_64 epoch : 7 version : 2.02.185 release : 2.el7_7.2 size : 1.3 m repo : updates/7/x86_64 summary : userland logical volume management tools url : http://sources.redhat.com/lvm2 license : gplv2 description : lvm2 includes all of the support for handling read/write operations on : physical volumes (hard disks, raid-systems, magneto optical, etc., : multiple devices (md), see mdadm(8) or even loop devices, see : losetup(8)), creating volume groups (kind of virtual disks) from one : or more physical volumes and creating one or more logical volumes : (kind of logical partitions) in volume groups. [root@test-centos7-node1 ~]# yum install lvm2 -y loaded plugins: fastestmirror loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com resolving dependencies --> running transaction check ---> package lvm2.x86_64 7:2.02.185-2.el7_7.2 will be installed --> processing dependency: lvm2-libs = 7:2.02.185-2.el7_7.2 for package: 7:lvm2-2.02.185-2.el7_7.2.x86_64 --> processing dependency: device-mapper-persistent-data >= 0.7.0-0.1.rc6 for package: 7:lvm2-2.02.185-2.el7_7.2.x86_64 --> processing dependency: liblvm2app.so.2.2(base)(64bit) for package: 7:lvm2-2.02.185-2.el7_7.2.x86_64 --> processing dependency: libdevmapper.so.1.02(dm_1_02_141)(64bit) for package: 7:lvm2-2.02.185-2.el7_7.2.x86_64 --> processing dependency: libdevmapper-event.so.1.02(base)(64bit) for package: 7:lvm2-2.02.185-2.el7_7.2.x86_64 --> processing dependency: liblvm2app.so.2.2()(64bit) for package: 7:lvm2-2.02.185-2.el7_7.2.x86_64 --> processing dependency: libdevmapper-event.so.1.02()(64bit) for package: 7:lvm2-2.02.185-2.el7_7.2.x86_64 --> running transaction check ---> package device-mapper-event-libs.x86_64 7:1.02.158-2.el7_7.2 will be installed ---> package device-mapper-libs.x86_64 7:1.02.140-8.el7 will be updated --> processing dependency: device-mapper-libs = 7:1.02.140-8.el7 for package: 7:device-mapper-1.02.140-8.el7.x86_64 ---> package device-mapper-libs.x86_64 7:1.02.158-2.el7_7.2 will be an update ---> package device-mapper-persistent-data.x86_64 0:0.8.5-1.el7 will be installed ---> package lvm2-libs.x86_64 7:2.02.185-2.el7_7.2 will be installed --> processing dependency: device-mapper-event = 7:1.02.158-2.el7_7.2 for package: 7:lvm2-libs-2.02.185-2.el7_7.2.x86_64 --> running transaction check ---> package device-mapper.x86_64 7:1.02.140-8.el7 will be updated ---> package device-mapper.x86_64 7:1.02.158-2.el7_7.2 will be an update ---> package device-mapper-event.x86_64 7:1.02.158-2.el7_7.2 will be installed --> finished dependency resolution dependencies resolved ============================================================================================================================ package arch version repository size ============================================================================================================================ installing: lvm2 x86_64 7:2.02.185-2.el7_7.2 updates 1.3 m installing for dependencies: device-mapper-event x86_64 7:1.02.158-2.el7_7.2 updates 190 k device-mapper-event-libs x86_64 7:1.02.158-2.el7_7.2 updates 189 k device-mapper-persistent-data x86_64 0.8.5-1.el7 base 423 k lvm2-libs x86_64 7:2.02.185-2.el7_7.2 updates 1.1 m updating for dependencies: device-mapper x86_64 7:1.02.158-2.el7_7.2 updates 294 k device-mapper-libs x86_64 7:1.02.158-2.el7_7.2 updates 322 k transaction summary ============================================================================================================================ install 1 package (+4 dependent packages) upgrade ( 2 dependent packages) total download size: 3.8 m downloading packages: delta rpms disabled because /usr/bin/applydeltarpm not installed. (1/7): device-mapper-event-1.02.158-2.el7_7.2.x86_64.rpm | 190 kb 00:00:00 (2/7): device-mapper-event-libs-1.02.158-2.el7_7.2.x86_64.rpm | 189 kb 00:00:00 (3/7): device-mapper-libs-1.02.158-2.el7_7.2.x86_64.rpm | 322 kb 00:00:00 (4/7): lvm2-2.02.185-2.el7_7.2.x86_64.rpm | 1.3 mb 00:00:00 (5/7): device-mapper-1.02.158-2.el7_7.2.x86_64.rpm | 294 kb 00:00:00 (6/7): lvm2-libs-2.02.185-2.el7_7.2.x86_64.rpm | 1.1 mb 00:00:00 (7/7): device-mapper-persistent-data-0.8.5-1.el7.x86_64.rpm | 423 kb 00:00:00 ---------------------------------------------------------------------------------------------------------------------------- total 4.3 mb/s | 3.8 mb 00:00:00 running transaction check running transaction test transaction test succeeded running transaction updating : 7:device-mapper-1.02.158-2.el7_7.2.x86_64 1/9 updating : 7:device-mapper-libs-1.02.158-2.el7_7.2.x86_64 2/9 installing : 7:device-mapper-event-libs-1.02.158-2.el7_7.2.x86_64 3/9 installing : 7:device-mapper-event-1.02.158-2.el7_7.2.x86_64 4/9 installing : 7:lvm2-libs-2.02.185-2.el7_7.2.x86_64 5/9 installing : device-mapper-persistent-data-0.8.5-1.el7.x86_64 6/9 installing : 7:lvm2-2.02.185-2.el7_7.2.x86_64 7/9 created symlink from /etc/systemd/system/sysinit.target.wants/lvm2-lvmpolld.socket to /usr/lib/systemd/system/lvm2-lvmpolld.socket. cleanup : 7:device-mapper-1.02.140-8.el7.x86_64 8/9 cleanup : 7:device-mapper-libs-1.02.140-8.el7.x86_64 9/9 verifying : 7:device-mapper-libs-1.02.158-2.el7_7.2.x86_64 1/9 verifying : 7:device-mapper-1.02.158-2.el7_7.2.x86_64 2/9 verifying : device-mapper-persistent-data-0.8.5-1.el7.x86_64 3/9 verifying : 7:lvm2-2.02.185-2.el7_7.2.x86_64 4/9 verifying : 7:lvm2-libs-2.02.185-2.el7_7.2.x86_64 5/9 verifying : 7:device-mapper-event-1.02.158-2.el7_7.2.x86_64 6/9 verifying : 7:device-mapper-event-libs-1.02.158-2.el7_7.2.x86_64 7/9 verifying : 7:device-mapper-1.02.140-8.el7.x86_64 8/9 verifying : 7:device-mapper-libs-1.02.140-8.el7.x86_64 9/9 installed: lvm2.x86_64 7:2.02.185-2.el7_7.2 dependency installed: device-mapper-event.x86_64 7:1.02.158-2.el7_7.2 device-mapper-event-libs.x86_64 7:1.02.158-2.el7_7.2 device-mapper-persistent-data.x86_64 0:0.8.5-1.el7 lvm2-libs.x86_64 7:2.02.185-2.el7_7.2 dependency updated: device-mapper.x86_64 7:1.02.158-2.el7_7.2 device-mapper-libs.x86_64 7:1.02.158-2.el7_7.2 complete! [root@test-centos7-node1 ~]#
说明:最小化安装系统默认系统上没有安装这个包的,所以我们需要把这个包装上才可创建lvm
3)创建pv
[root@test-centos7-node1 ~]# pvcreate /dev/sdb{1,2,3} physical volume "/dev/sdb1" successfully created. physical volume "/dev/sdb2" successfully created. physical volume "/dev/sdb3" successfully created. [root@test-centos7-node1 ~]# pvscan pv /dev/sdb1 lvm2 [10.00 gib] pv /dev/sdb3 lvm2 [10.00 gib] pv /dev/sdb2 lvm2 [10.00 gib] total: 3 [30.00 gib] / in use: 0 [0 ] / in no vg: 3 [30.00 gib] [root@test-centos7-node1 ~]# pvs pv vg fmt attr psize pfree /dev/sdb1 lvm2 --- 10.00g 10.00g /dev/sdb2 lvm2 --- 10.00g 10.00g /dev/sdb3 lvm2 --- 10.00g 10.00g [root@test-centos7-node1 ~]# pvdisplay "/dev/sdb1" is a new physical volume of "10.00 gib" --- new physical volume --- pv name /dev/sdb1 vg name pv size 10.00 gib allocatable no pe size 0 total pe 0 free pe 0 allocated pe 0 pv uuid msszes-ypdm-ygql-7rgg-b6gx-q4xu-t8m1gz "/dev/sdb3" is a new physical volume of "10.00 gib" --- new physical volume --- pv name /dev/sdb3 vg name pv size 10.00 gib allocatable no pe size 0 total pe 0 free pe 0 allocated pe 0 pv uuid bmmx8p-zu9o-ns61-g9du-ikmr-nmzo-crcjlr "/dev/sdb2" is a new physical volume of "10.00 gib" --- new physical volume --- pv name /dev/sdb2 vg name pv size 10.00 gib allocatable no pe size 0 total pe 0 free pe 0 allocated pe 0 pv uuid lpynpw-twlu-fxwg-olct-ja2a-r5ui-fvqwlj [root@test-centos7-node1 ~]#
说明:pvcreate 命令表示创建pv 后面跟硬盘或分区对应的设备文件,pvscan 命令作用是搜索系统里面任何具有pv属性的磁碟,pv属性就是通过pvcreate创建过的磁碟,pvs是查看pv简要情况,pvdisplay 查看pv的详细信息。当然我们要移除某块盘,让其不具备pv属性,可以用pvremove + 磁盘的设备文件路径
[root@test-centos7-node1 ~]# pvremove /dev/sdb3 labels on physical volume "/dev/sdb3" successfully wiped. [root@test-centos7-node1 ~]# pvs pv vg fmt attr psize pfree /dev/sdb1 lvm2 --- 10.00g 10.00g /dev/sdb2 lvm2 --- 10.00g 10.00g [root@test-centos7-node1 ~]#
说明:移除pv的前提是它没有在任何vg里面才可做移除,否则移除不了
4)创建vg
[root@test-centos7-node1 ~]# pvs pv vg fmt attr psize pfree /dev/sdb1 lvm2 --- 10.00g 10.00g /dev/sdb2 lvm2 --- 10.00g 10.00g [root@test-centos7-node1 ~]# pvcreate /dev/sdb3 physical volume "/dev/sdb3" successfully created. [root@test-centos7-node1 ~]# pvs pv vg fmt attr psize pfree /dev/sdb1 lvm2 --- 10.00g 10.00g /dev/sdb2 lvm2 --- 10.00g 10.00g /dev/sdb3 lvm2 --- 10.00g 10.00g [root@test-centos7-node1 ~]# vgcreate test -s 8m /dev/sdb{1,2,3} volume group "test" successfully created [root@test-centos7-node1 ~]# vgs vg #pv #lv #sn attr vsize vfree test 3 0 0 wz--n- <29.98g <29.98g [root@test-centos7-node1 ~]# vgscan reading volume groups from cache. found volume group "test" using metadata type lvm2 [root@test-centos7-node1 ~]# vgdisplay --- volume group --- vg name test system id format lvm2 metadata areas 3 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 3 act pv 3 vg size <29.98 gib pe size 8.00 mib total pe 3837 alloc pe / size 0 / 0 free pe / size 3837 / <29.98 gib vg uuid 31vxap-l5jb-8yxm-25m2-ldd0-ee8r-sdtjum [root@test-centos7-node1 ~]# vgreduce test /dev/sdb3 removed "/dev/sdb3" from volume group "test" [root@test-centos7-node1 ~]# vgs vg #pv #lv #sn attr vsize vfree test 2 0 0 wz--n- 19.98g 19.98g [root@test-centos7-node1 ~]# vgcreate test2 /dev/sdb3 volume group "test2" successfully created [root@test-centos7-node1 ~]# vgs vg #pv #lv #sn attr vsize vfree test 2 0 0 wz--n- 19.98g 19.98g test2 1 0 0 wz--n- <10.00g <10.00g [root@test-centos7-node1 ~]# vgdisplay test2 --- volume group --- vg name test2 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 <10.00 gib pe size 4.00 mib total pe 2559 alloc pe / size 0 / 0 free pe / size 2559 / <10.00 gib vg uuid rc94sp-jalk-63rw-n5xz-qb0d-grnt-itrmvt [root@test-centos7-node1 ~]# vgremove test2 volume group "test2" successfully removed [root@test-centos7-node1 ~]# vgs vg #pv #lv #sn attr vsize vfree test 2 0 0 wz--n- 19.98g 19.98g [root@test-centos7-node1 ~]#
说明:同样的vgcreate 表示创建vg 其用法格式是vgcreate vgname [option] 储存设备路径 其中-s表示指定pe的大小,vgname表示vg的名称,需要说明的是,指定名称不能和系统现有的vg名称相同;vgs查看系统上所有vg的简要信息,vgscan搜寻系统上面是否有vg,vgdisplay查看所有vg的详细信息,当然此例中只有一个,我们也可在后面指定查看某个vg的详细信息,只需要指定其vg名称即可;有关vg的命令有很多其中vgreduce的作用是在vg内移除pv,vgremove的作用是删除一个vg,这两个命令都需要在其后指定要操作的vg名称
5)创建lv
[root@test-centos7-node1 ~]# lvs [root@test-centos7-node1 ~]# lvcreate -l 2g -n aaa test logical volume "aaa" created. [root@test-centos7-node1 ~]# lvcreate -l 3g -n bbb test logical volume "bbb" created. [root@test-centos7-node1 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert aaa test -wi-a----- 2.00g bbb test -wi-a----- 3.00g [root@test-centos7-node1 ~]# lvscan active '/dev/test/aaa' [2.00 gib] inherit active '/dev/test/bbb' [3.00 gib] inherit [root@test-centos7-node1 ~]# lvdisplay --- logical volume --- lv path /dev/test/aaa lv name aaa vg name test lv uuid r9weh0-d85b-rmcw-oysm-daqb-giqh-rxxitp lv write access read/write lv creation host, time test-centos7-node1, 2020-01-06 09:05:23 -0500 lv status available # open 0 lv size 2.00 gib current le 256 segments 1 allocation inherit read ahead sectors auto - currently set to 8192 block device 253:0 --- logical volume --- lv path /dev/test/bbb lv name bbb vg name test lv uuid lywvzj-ogpf-nwtf-mja6-xwwl-a24w-bsljou lv write access read/write lv creation host, time test-centos7-node1, 2020-01-06 09:05:34 -0500 lv status available # open 0 lv size 3.00 gib current le 384 segments 1 allocation inherit read ahead sectors auto - currently set to 8192 block device 253:1 [root@test-centos7-node1 ~]# lvresize -l +4g /dev/test/aaa size of logical volume test/aaa changed from 2.00 gib (256 extents) to 6.00 gib (768 extents). logical volume test/aaa successfully resized. [root@test-centos7-node1 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert aaa test -wi-a----- 6.00g bbb test -wi-a----- 3.00g [root@test-centos7-node1 ~]# lvresize -l -1g /dev/test/bbb warning: reducing active logical volume to 2.00 gib. this may destroy your data (filesystem etc.) do you really want to reduce test/bbb? [y/n]: y size of logical volume test/bbb changed from 3.00 gib (384 extents) to 2.00 gib (256 extents). logical volume test/bbb successfully resized. [root@test-centos7-node1 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert aaa test -wi-a----- 6.00g bbb test -wi-a----- 2.00g [root@test-centos7-node1 ~]# lvreduce -l 3g /dev/test/aaa warning: reducing active logical volume to 3.00 gib. this may destroy your data (filesystem etc.) do you really want to reduce test/aaa? [y/n]: y size of logical volume test/aaa changed from 6.00 gib (768 extents) to 3.00 gib (384 extents). logical volume test/aaa successfully resized. [root@test-centos7-node1 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert aaa test -wi-a----- 3.00g bbb test -wi-a----- 2.00g [root@test-centos7-node1 ~]# lvreduce -l -1g /dev/test/aaa warning: reducing active logical volume to 2.00 gib. this may destroy your data (filesystem etc.) do you really want to reduce test/aaa? [y/n]: y size of logical volume test/aaa changed from 3.00 gib (384 extents) to 2.00 gib (256 extents). logical volume test/aaa successfully resized. [root@test-centos7-node1 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert aaa test -wi-a----- 2.00g bbb test -wi-a----- 2.00g [root@test-centos7-node1 ~]# lvremove /dev/test/bbb do you really want to remove active logical volume test/bbb? [y/n]: y logical volume "bbb" successfully removed [root@test-centos7-node1 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert aaa test -wi-a----- 2.00g [root@test-centos7-node1 ~]#
说明:lvcreate 命令作用是创建lv 其中-l表示指定lv的大小,-n表示lv的名称 最后跟vg的名称,表示从那个vg上创建lv;lvs 命令作用是查看系统所有的lv简要信息 ;lvdisplay 查看lv的详细信息,后面可以跟vg的名称表示查看指定vg上的所有lv的详细信息;lvreduce 命令作用是缩减指定lv里的空间,如果指定的大小是负数如-3m 表示缩减3m 如果指定的是 3m 则表示缩减至3m ,这个需要注意,还有这个命令指定大小不能以类似+4m来指定;lvresize此命令的作用是调整lv的容量大小,这个命令指定调整大小+表示增加多少,-表示减少多少,若没有+/-表示调整到指定大小,lvresize和lvreduce这两个命令后面都是跟的lv对应设备的路径(其实不是真的设备路径,是设备的软连接路径),不能指定其名称;lvremove命令作用是删除指定的lv,若指定的是vg名,表示删除该vg上的所有lv,若需要删除某个lv 需要指定其对应设备路径。
到此lvm就已经实现,后续就可以直接格式化,然后挂载使用了
[root@test-centos7-node1 ~]# vgs vg #pv #lv #sn attr vsize vfree test 2 0 0 wz--n- 19.98g 19.98g [root@test-centos7-node1 ~]# lvcreate -l 100%free -n xxx test warning: xfs signature detected on /dev/test/xxx at offset 0. wipe it? [y/n]: y wiping xfs signature on /dev/test/xxx. logical volume "xxx" created. [root@test-centos7-node1 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert xxx test -wi-a----- 19.98g [root@test-centos7-node1 ~]# mkfs.xfs -f /dev/test/xxx meta-data=/dev/test/xxx isize=512 agcount=4, agsize=1309696 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=5238784, 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@test-centos7-node1 ~]# lsblk name maj:min rm size ro type mountpoint sda 8:0 0 50g 0 disk ├─sda1 8:1 0 2g 0 part /boot ├─sda2 8:2 0 2g 0 part [swap] └─sda3 8:3 0 46g 0 part / sdb 8:16 0 50g 0 disk ├─sdb1 8:17 0 10g 0 part │ └─test-xxx 253:0 0 20g 0 lvm ├─sdb2 8:18 0 10g 0 part │ └─test-xxx 253:0 0 20g 0 lvm └─sdb3 8:19 0 10g 0 part sr0 11:0 1 792m 0 rom [root@test-centos7-node1 ~]# mount /dev/test/xxx /mnt [root@test-centos7-node1 ~]# df -h filesystem size used avail use% mounted on /dev/sda3 46g 1.6g 45g 4% / devtmpfs 1.9g 0 1.9g 0% /dev tmpfs 1.9g 0 1.9g 0% /dev/shm tmpfs 1.9g 8.6m 1.9g 1% /run tmpfs 1.9g 0 1.9g 0% /sys/fs/cgroup /dev/sda1 2.0g 135m 1.9g 7% /boot tmpfs 378m 0 378m 0% /run/user/0 /dev/mapper/test-xxx 20g 33m 20g 1% /mnt [root@test-centos7-node1 ~]#
三、扩容lv
1)创建pv,并将其加入到vg中
[root@test-centos7-node1 ~]# pvs pv vg fmt attr psize pfree /dev/sdb1 test lvm2 a-- 9.99g 0 /dev/sdb2 test lvm2 a-- 9.99g 0 [root@test-centos7-node1 ~]# vgdisplay --- volume group --- vg name test system id format lvm2 metadata areas 2 metadata sequence no 28 vg access read/write vg status resizable max lv 0 cur lv 1 open lv 1 max pv 0 cur pv 2 act pv 2 vg size 19.98 gib pe size 8.00 mib total pe 2558 alloc pe / size 2558 / 19.98 gib free pe / size 0 / 0 vg uuid 31vxap-l5jb-8yxm-25m2-ldd0-ee8r-sdtjum [root@test-centos7-node1 ~]#
说明:从上面的信息可以看到vg的大小是19.98g 也就是两块pv的大小。现在我们新创建一个pv,然后加到vg里
[root@test-centos7-node1 ~]# pvs pv vg fmt attr psize pfree /dev/sdb1 test lvm2 a-- 9.99g 0 /dev/sdb2 test lvm2 a-- 9.99g 0 [root@test-centos7-node1 ~]# pvcreate /dev/sdb3 physical volume "/dev/sdb3" successfully created. [root@test-centos7-node1 ~]# pvs pv vg fmt attr psize pfree /dev/sdb1 test lvm2 a-- 9.99g 0 /dev/sdb2 test lvm2 a-- 9.99g 0 /dev/sdb3 lvm2 --- 10.00g 10.00g [root@test-centos7-node1 ~]# vgs vg #pv #lv #sn attr vsize vfree test 2 1 0 wz--n- 19.98g 0 [root@test-centos7-node1 ~]# vgextend test /dev/sdb3 volume group "test" successfully extended [root@test-centos7-node1 ~]# vgs vg #pv #lv #sn attr vsize vfree test 3 1 0 wz--n- <29.98g 9.99g [root@test-centos7-node1 ~]# vgdisplay --- volume group --- vg name test system id format lvm2 metadata areas 3 metadata sequence no 31 vg access read/write vg status resizable max lv 0 cur lv 1 open lv 1 max pv 0 cur pv 3 act pv 3 vg size <29.98 gib pe size 8.00 mib total pe 3837 alloc pe / size 2558 / 19.98 gib free pe / size 1279 / 9.99 gib vg uuid 31vxap-l5jb-8yxm-25m2-ldd0-ee8r-sdtjum [root@test-centos7-node1 ~]#
说明:vgextend命令作用是将指定pv加入到指定vg中,vg指定其名称即可,pv需指定其设备路径
2)将新加的vg的pe加入到xxx lv里
[root@test-centos7-node1 ~]# vgs vg #pv #lv #sn attr vsize vfree test 3 1 0 wz--n- <29.98g 9.99g [root@test-centos7-node1 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert xxx test -wi-ao---- 19.98g [root@test-centos7-node1 ~]# lvdisplay --- logical volume --- lv path /dev/test/xxx lv name xxx vg name test lv uuid ihoo41-gh1l-eshk-ilxy-ugff-j6ui-umvwhf lv write access read/write lv creation host, time test-centos7-node1, 2020-01-06 10:00:08 -0500 lv status available # open 1 lv size 19.98 gib current le 2558 segments 2 allocation inherit read ahead sectors auto - currently set to 8192 block device 253:0 [root@test-centos7-node1 ~]# lvextend -l +100%free /dev/test/xxx size of logical volume test/xxx changed from 19.98 gib (2558 extents) to <29.98 gib (3837 extents). logical volume test/xxx successfully resized. [root@test-centos7-node1 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert xxx test -wi-ao---- <29.98g [root@test-centos7-node1 ~]# lvdisplay --- logical volume --- lv path /dev/test/xxx lv name xxx vg name test lv uuid ihoo41-gh1l-eshk-ilxy-ugff-j6ui-umvwhf lv write access read/write lv creation host, time test-centos7-node1, 2020-01-06 10:00:08 -0500 lv status available # open 1 lv size <29.98 gib current le 3837 segments 3 allocation inherit read ahead sectors auto - currently set to 8192 block device 253:0 [root@test-centos7-node1 ~]# vgs vg #pv #lv #sn attr vsize vfree test 3 1 0 wz--n- <29.98g 0 [root@test-centos7-node1 ~]# lsblk name maj:min rm size ro type mountpoint sda 8:0 0 50g 0 disk ├─sda1 8:1 0 2g 0 part /boot ├─sda2 8:2 0 2g 0 part [swap] └─sda3 8:3 0 46g 0 part / sdb 8:16 0 50g 0 disk ├─sdb1 8:17 0 10g 0 part │ └─test-xxx 253:0 0 30g 0 lvm /mnt ├─sdb2 8:18 0 10g 0 part │ └─test-xxx 253:0 0 30g 0 lvm /mnt └─sdb3 8:19 0 10g 0 part └─test-xxx 253:0 0 30g 0 lvm /mnt sr0 11:0 1 792m 0 rom [root@test-centos7-node1 ~]# df -h filesystem size used avail use% mounted on /dev/sda3 46g 1.6g 45g 4% / devtmpfs 1.9g 0 1.9g 0% /dev tmpfs 1.9g 0 1.9g 0% /dev/shm tmpfs 1.9g 8.6m 1.9g 1% /run tmpfs 1.9g 0 1.9g 0% /sys/fs/cgroup /dev/sda1 2.0g 135m 1.9g 7% /boot tmpfs 378m 0 378m 0% /run/user/0 /dev/mapper/test-xxx 20g 33m 20g 1% /mnt [root@test-centos7-node1 ~]#
说明:可看到lvs里的空间已经加上去,lvextend命令作用是将指定vg里的空间添加到指定的lv中,上面的例子是把vg里的所有空闲空间添加到/dev/test/xxx里,当然还有一种情况在vg还有空余的空间,我们可以用lvresize 来调整lv的大小;从上面的信息可以看到我们把新的空间加入到vg和lv中后,df 看挂载点空间却没有增加,接下来我们通过resize2fs将文件系统同步到增大的空间。
[root@test-centos7-node1 ~]# resize2fs /dev/test/xxx resize2fs 1.42.9 (28-dec-2013) resize2fs: bad magic number in super-block while trying to open /dev/test/xxx couldn't find valid filesystem superblock. [root@test-centos7-node1 ~]# df -h filesystem size used avail use% mounted on /dev/sda3 46g 1.6g 45g 4% / devtmpfs 1.9g 0 1.9g 0% /dev tmpfs 1.9g 0 1.9g 0% /dev/shm tmpfs 1.9g 8.6m 1.9g 1% /run tmpfs 1.9g 0 1.9g 0% /sys/fs/cgroup /dev/sda1 2.0g 135m 1.9g 7% /boot tmpfs 378m 0 378m 0% /run/user/0 /dev/mapper/test-xxx 20g 33m 20g 1% /mnt [root@test-centos7-node1 ~]#
说明:看来用此命令不行,因为resize2fs这个命令针对ext系列的文件系统,xfs需要用 xfs_growfs来调整
[root@test-centos7-node1 ~]# xfs_growfs /dev/test/xxx meta-data=/dev/mapper/test-xxx isize=512 agcount=4, agsize=1309696 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=5238784, 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 5238784 to 7858176 [root@test-centos7-node1 ~]# df -h filesystem size used avail use% mounted on /dev/sda3 46g 1.6g 45g 4% / devtmpfs 1.9g 0 1.9g 0% /dev tmpfs 1.9g 0 1.9g 0% /dev/shm tmpfs 1.9g 8.6m 1.9g 1% /run tmpfs 1.9g 0 1.9g 0% /sys/fs/cgroup /dev/sda1 2.0g 135m 1.9g 7% /boot tmpfs 378m 0 378m 0% /run/user/0 /dev/mapper/test-xxx 30g 33m 30g 1% /mnt [root@test-centos7-node1 ~]#
四、缩减lv(xfs文件系统)
1)卸载已经挂好的lv
[root@test-centos7-node1 ~]# df -h filesystem size used avail use% mounted on /dev/sda3 46g 1.6g 45g 4% / devtmpfs 1.9g 0 1.9g 0% /dev tmpfs 1.9g 0 1.9g 0% /dev/shm tmpfs 1.9g 8.6m 1.9g 1% /run tmpfs 1.9g 0 1.9g 0% /sys/fs/cgroup /dev/sda1 2.0g 135m 1.9g 7% /boot tmpfs 378m 0 378m 0% /run/user/0 /dev/mapper/test-xxx 30g 33m 30g 1% /mnt [root@test-centos7-node1 ~]# umount /mnt [root@test-centos7-node1 ~]# df -h filesystem size used avail use% mounted on /dev/sda3 46g 1.6g 45g 4% / devtmpfs 1.9g 0 1.9g 0% /dev tmpfs 1.9g 0 1.9g 0% /dev/shm tmpfs 1.9g 8.6m 1.9g 1% /run tmpfs 1.9g 0 1.9g 0% /sys/fs/cgroup /dev/sda1 2.0g 135m 1.9g 7% /boot tmpfs 378m 0 378m 0% /run/user/0 [root@test-centos7-node1 ~]#
2)强制进行磁盘检测
[root@test-centos7-node1 ~]# xfs_repair -n /dev/test/xxx phase 1 - find and verify superblock... phase 2 - using internal log - zero log... - scan filesystem freespace and inode maps... - found root inode chunk phase 3 - for each ag... - scan (but don't clear) agi unlinked lists... - process known inodes and perform inode discovery... - agno = 0 - agno = 1 - agno = 2 - agno = 3 - process newly discovered inodes... phase 4 - check for duplicate blocks... - setting up duplicate extent list... - check for inodes claiming duplicate blocks... - agno = 0 - agno = 1 - agno = 2 - agno = 3 no modify flag set, skipping phase 5 phase 6 - check inode connectivity... - traversing filesystem ... - traversal finished ... - moving disconnected inodes to lost+found ... phase 7 - verify link counts... no modify flag set, skipping filesystem flush and exiting. [root@test-centos7-node1 ~]#
说明:ext系列文件系统可e2fsck命令检查
3)调整lv大小
[root@test-centos7-node1 ~]# lvreduce -l -10g /dev/test/xxx warning: reducing active logical volume to <19.98 gib. this may destroy your data (filesystem etc.) do you really want to reduce test/xxx? [y/n]: y size of logical volume test/xxx changed from <29.98 gib (3837 extents) to <19.98 gib (2557 extents). logical volume test/xxx successfully resized. [root@test-centos7-node1 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert xxx test -wi-a----- <19.98g [root@test-centos7-node1 ~]# lvdisplay --- logical volume --- lv path /dev/test/xxx lv name xxx vg name test lv uuid uhzzaz-odd9-ibwy-2z1z-zmny-ezf7-0lxert lv write access read/write lv creation host, time test-centos7-node1, 2020-01-06 10:32:42 -0500 lv status available # open 0 lv size <19.98 gib current le 2557 segments 2 allocation inherit read ahead sectors auto - currently set to 8192 block device 253:0 [root@test-centos7-node1 ~]#
4)挂载
[root@test-centos7-node1 ~]# mount /dev/test/xxx /mnt mount: /dev/mapper/test-xxx: can't read superblock [root@test-centos7-node1 ~]#
说明:做到这里发现一个问题,xfs文件系统是不支持逻辑卷缩减的,为什么呢?xfs的文件系统调整命令是xfs_growfs 这个命令只支持增大的操作。我们在调整lv大小的时候实际上是把文件系统给破坏了,要想缩减空间,就必须重新格式化后在挂载,这样一来如果原有的盘里有数据就丢了。所以xfs上做lv的缩减没有意义
[root@test-centos7-node1 ~]# mkfs.xfs /dev/test/xxx -f meta-data=/dev/test/xxx isize=512 agcount=4, agsize=1309184 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=5236736, 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@test-centos7-node1 ~]# mount /dev/test/xxx /mnt [root@test-centos7-node1 ~]# df -h filesystem size used avail use% mounted on /dev/sda3 46g 1.6g 45g 4% / devtmpfs 1.9g 0 1.9g 0% /dev tmpfs 1.9g 0 1.9g 0% /dev/shm tmpfs 1.9g 8.6m 1.9g 1% /run tmpfs 1.9g 0 1.9g 0% /sys/fs/cgroup /dev/sda1 2.0g 135m 1.9g 7% /boot tmpfs 378m 0 378m 0% /run/user/0 /dev/mapper/test-xxx 20g 33m 20g 1% /mnt [root@test-centos7-node1 ~]
五、缩减lv(ext系列文件系统)
1)卸载已挂载的lv
[root@test-centos7-node1 ~]# df -th filesystem type size used avail use% mounted on /dev/sda3 xfs 46g 1.6g 45g 4% / devtmpfs devtmpfs 1.9g 0 1.9g 0% /dev tmpfs tmpfs 1.9g 0 1.9g 0% /dev/shm tmpfs tmpfs 1.9g 8.6m 1.9g 1% /run tmpfs tmpfs 1.9g 0 1.9g 0% /sys/fs/cgroup /dev/sda1 xfs 2.0g 135m 1.9g 7% /boot tmpfs tmpfs 378m 0 378m 0% /run/user/0 /dev/mapper/test-xxx ext4 20g 45m 19g 1% /mnt [root@test-centos7-node1 ~]# umount /mnt [root@test-centos7-node1 ~]# df -th filesystem type size used avail use% mounted on /dev/sda3 xfs 46g 1.6g 45g 4% / devtmpfs devtmpfs 1.9g 0 1.9g 0% /dev tmpfs tmpfs 1.9g 0 1.9g 0% /dev/shm tmpfs tmpfs 1.9g 8.6m 1.9g 1% /run tmpfs tmpfs 1.9g 0 1.9g 0% /sys/fs/cgroup /dev/sda1 xfs 2.0g 135m 1.9g 7% /boot tmpfs tmpfs 378m 0 378m 0% /run/user/0 [root@test-centos7-node1 ~]#
2)强制执行磁盘检测
[root@test-centos7-node1 ~]# e2fsck -f /dev/test/xxx e2fsck 1.42.9 (28-dec-2013) 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/test/xxx: 11/1310720 files (0.0% non-contiguous), 126322/5236736 blocks [root@test-centos7-node1 ~]#
说明:这一步我们在不执行的话,直接下一步它也会提示我们先做磁盘检测
3)调整文件系统大小
[root@test-centos7-node1 ~]# resize2fs /dev/test/xxx 12g resize2fs 1.42.9 (28-dec-2013) resizing the filesystem on /dev/test/xxx to 3145728 (4k) blocks. the filesystem on /dev/test/xxx is now 3145728 blocks long. [root@test-centos7-node1 ~]#
4)调整lv的大小
[root@test-centos7-node1 ~]# lvreduce -l 12g /dev/test/xxx warning: reducing active logical volume to 12.00 gib. this may destroy your data (filesystem etc.) do you really want to reduce test/xxx? [y/n]: y size of logical volume test/xxx changed from <19.98 gib (2557 extents) to 12.00 gib (1536 extents). logical volume test/xxx successfully resized. [root@test-centos7-node1 ~]# lvs lv vg attr lsize pool origin data% meta% move log cpy%sync convert xxx test -wi-a----- 12.00g [root@test-centos7-node1 ~]#
说明:这里缩减到的空间需同文件系统的大小一致或者缩减后的空间比文件系统调整的空间要大才行,这样才不能损坏文件系统,切记不能比文件系统调整的空间还小,小了,在缩减空间的时候就破坏了文件系统,会导致后续的挂载无法进行。
5)挂载
[root@test-centos7-node1 ~]# mount /dev/test/xxx /mnt [root@test-centos7-node1 ~]# df -h filesystem size used avail use% mounted on /dev/sda3 46g 1.6g 45g 4% / devtmpfs 1.9g 0 1.9g 0% /dev tmpfs 1.9g 0 1.9g 0% /dev/shm tmpfs 1.9g 8.6m 1.9g 1% /run tmpfs 1.9g 0 1.9g 0% /sys/fs/cgroup /dev/sda1 2.0g 135m 1.9g 7% /boot tmpfs 378m 0 378m 0% /run/user/0 /dev/mapper/test-xxx 12g 41m 12g 1% /mnt [root@test-centos7-node1 ~]#
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。