【Linux系列教程】逻辑卷扩容

本期教程为逻辑卷第二期教程点我跳转到第一期教程

具体要求如下:
上一题的mysql逻辑卷从30G扩容到40G

一、逻辑卷扩容

1.检查卷组是否还有10G容量

[root@localhost ~]# vgdisplay db | grep -i "free"
  Free  PE / Size       2358 / 9.21 GiB		#发现剩余空间不到10G,无法直接扩容

2.扩容卷组

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0  476M  0 part /boot
└─sda2            8:2    0 19.5G  0 part 
  ├─centos-root 253:0    0 17.5G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
└─db-mysql      253:2    0   30G  0 lvm  /mysql
sdc               8:32   0   20G  0 disk 
├─db-mysql      253:2    0   30G  0 lvm  /mysql
└─db-redis      253:3    0  800M  0 lvm  /redis
sdd               8:48   0   20G  0 disk	#手动再添加一块硬盘
[root@localhost ~]# pvcreate /dev/sdd	#给sdd硬盘创建PV
  Physical volume "/dev/sdd" successfully created.
  
[root@localhost ~]# vgextend db /dev/sdd 	#把sdd添加到db卷组中
  Volume group "db" successfully extended
  
[root@localhost ~]# vgdisplay db | grep -i "free"	#查看卷组剩余大小
  Free  PE / Size       7477 / <29.21 GiB	#可以看到成功扩容了20G

3.扩容逻辑卷

[root@localhost ~]# lvextend -L +10G /dev/db/mysql	#扩展10G空间
  Size of logical volume db/mysql changed from 30.00 GiB (7680 extents) to 40.00 GiB (10240 extents).
  Logical volume db/mysql successfully resized.

4.扩展文件系统ext4

[root@localhost ~]# resize2fs /dev/db/mysql
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/db/mysql is mounted on /mysql; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 5
The filesystem on /dev/db/mysql is now 10485760 blocks long.

5.检查是否成功扩容

[root@localhost ~]# df -hT | grep "mysql"
/dev/mapper/db-mysql    ext4       40G   48M   38G    1% /mysql		#发现成功扩容到40G

二、xfs文件系统扩容

要求如下:

扩容上一题的redis分区,从800M再扩容5G

1.检查卷组是否还有剩余容量

[root@localhost ~]# vgdisplay db | grep "Free"
  Free  PE / Size       4917 / <19.21 GiB	#剩余19G多

2.扩容redis逻辑卷

[root@localhost ~]# lvextend -L +5G /dev/db/redis 
  Size of logical volume db/redis changed from 800.00 MiB (200 extents) to 5.78 GiB (1480 extents).
  Logical volume db/redis successfully resized.

3.扩容xfs文件系统

[root@localhost ~]# xfs_growfs /dev/db/redis
meta-data=/dev/mapper/db-redis   isize=512    agcount=4, agsize=51200 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=204800, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 204800 to 1515520

4.检查是否成功扩容

[root@localhost ~]# df -hT | grep "redis"
/dev/mapper/db-redis    xfs       5.8G   34M  5.8G    1% /redis

[root@localhost ~]# lvscan
  ACTIVE            '/dev/db/mysql' [40.00 GiB] inherit
  ACTIVE            '/dev/db/redis' [5.78 GiB] inherit

三、缩减逻辑卷

1.将逻辑卷/dev/db/mysql缩减到10G

备份文件(重要)

使用cp或者tar命令备份要缩减逻辑卷里的数据

卸载逻辑卷

[root@localhost ~]# umount /dev/db/mysql

检查文件是否有损坏(重要)

[root@localhost ~]# e2fsck -f /dev/db/mysql 
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/db/mysql: 21/2949120 files (0.0% non-contiguous), 231191/11796480 blocks

先缩减文件系统大小

[root@localhost ~]# resize2fs /dev/db/mysql 10G 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/db/mysql to 2621440 (4k) blocks.
The filesystem on /dev/db/mysql is now 2621440 blocks long.

再缩减逻辑卷物理大小

[root@localhost ~]# lvreduce -L 10G /dev/db/mysql 
  WARNING: Reducing active logical volume to 10.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce db/mysql? [y/n]: y
  Size of logical volume db/mysql changed from 45.00 GiB (11520 extents) to 10.00 GiB (2560 extents).
  Logical volume db/mysql successfully resized.

重新挂载逻辑卷,验证数据

[root@localhost ~]# mount /dev/db/mysql /mysql/
[root@localhost ~]# ls /mysql/
10.sql  1.sql  2.sql  3.sql  4.sql  5.sql  6.sql  7.sql  8.sql  9.sql  lost+found

四、删除逻辑卷

1.删除逻辑卷

# lvremove <逻辑卷名称>

2.删除卷组

# vgremove <卷组名称>

3.删除物理卷

# pvremove <pv名称>