CentOS7 扩展挂载点容量

记录CentOS7扩容的过程

CentOS7 扩展挂载点容量

一、前述

环境:
CentOS Linux release 7.9.2009 (Core)

有一台服务器专门用于存储备份文件,发现磁盘空间不足,决定将此机器扩容,避免备份失败的问题。

二、动手实践

1、查看信息

(1)查看挂载点信息

[root@localhost ~]# df -h 
文件系统               容量  已用  可用 已用% 挂载点
devtmpfs               3.9G     0  3.9G    0% /dev
tmpfs                  3.9G   12K  3.9G    1% /dev/shm
tmpfs                  3.9G  9.0M  3.9G    1% /run
tmpfs                  3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/rhel-root  692G  655G   37G   95% /
/dev/sdb               197G   39G  149G   21% /docbak
/dev/mapper/rhel-home  100G  1.3G   99G    2% /home
/dev/sda1              497M  167M  330M   34% /boot
tmpfs                  783M     0  783M    0% /run/user/0

可以发现挂载点/剩余37G。

(2)查看磁盘信息

[root@localhost data]# lsblk
NAME          MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
fd0             2:0    1     4K  0 disk 
sda             8:0    0   800G  0 disk 
├─sda1          8:1    0   500M  0 part /boot
├─sda2          8:2    0 499.5G  0 part 
│ ├─rhel-root 253:0    0 691.6G  0 lvm  /
│ ├─rhel-swap 253:1    0   7.9G  0 lvm  [SWAP]
│ └─rhel-home 253:2    0   100G  0 lvm  /home
└─sda3          8:3    0   300G  0 part 
  └─rhel-root 253:0    0 691.6G  0 lvm  /
sdb             8:16   0   200G  0 disk /docbak
sr0            11:0    1  1024M  0 rom

2、扩展硬盘空间

在VMware中修改硬盘容量,原来800G,修改到900G。修改完成后,shutdown -r now重启操作系统。

3、对扩容的硬盘进行分区、格式化

(1)查看调整后的磁盘信息

[root@localhost ~]# fdisk -l

磁盘 /dev/sdb:214.7 GB, 214748364800 字节,419430400 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/sda:966.4 GB, 966367641600 字节,1887436800 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000c7640

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048  1048575999   523774976   8e  Linux LVM
/dev/sda3      1048576000  1677721599   314572800   8e  Linux LVM

磁盘 /dev/mapper/rhel-root:742.6 GB, 742630883328 字节,1450450944 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/rhel-swap:8455 MB, 8455716864 字节,16515072 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/rhel-home:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

发现/dev/sda空间已扩展。

(2)磁盘分区

[root@localhost ~]# fdisk /dev/sda

键入一下参数:

p       查看已分区数量(/dev/sda1 /dev/sda2 /dev/sda3)
n       新增加一个分区
p       分区类型,选择主分区
        分区号选4(1/2/3已占用,见上)
回车     默认选择(起始扇区)
回车     默认选择(结束扇区)
t       修改分区类型
       选分区4
8e     修改为LVM(8e就是LVM)
w       写分区表,完成后退出fdisk命令

使用shutdown -r now重启机器。

格式化/dev/sda4分区

[root@localhost ~]# mkfs.ext4 /dev/sda4
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: 完成                            
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2174746624
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

4、添加到已有的LVM组,实现扩容

进入LVM管理

[root@localhost ~]# lvm

LVM基础知识:http://blog.csdn.net/wuweilong/article/details/7565530

在lvm>模式下执行命令:

(1)初始化刚创建的分区

lvm> pvcreate /dev/sda4
WARNING: ext4 signature detected on /dev/sda4 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sda4.
  Physical volume "/dev/sda4" successfully created.

(2)查看卷组信息

lvm> vgdisplay 
  --- Volume group ---
  VG Name               rhel
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  9
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               799.50 GiB
  PE Size               4.00 MiB
  Total PE              204673
  Alloc PE / Size       204673 / 799.50 GiB
  Free  PE / Size       0 / 0   
  VG UUID               d3fhu5-ruNq-0iRq-69ZC-u0yT-JwTW-Y9T8Hl

请记住VG Name,这里是rhel

(3)将初始化的分区加入VG卷组

rhel 为VG Name,/dev/sda4为创建的分区。
lvm> vgextend rhel /dev/sda4
  Volume group "rhel" successfully extended

(4)扩展已有卷的容量

该命令将使用卷组中100%的空闲空间

lvm> lvextend -l +100%FREE /dev/mapper/rhel-root
  Size of logical volume rhel/root changed from <691.63 GiB (177057 extents) to 791.62 GiB (202656 extents).
  Logical volume rhel/root successfully resized.

(5)查看卷容量并退出

lvm> pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               rhel
  PV Size               499.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              127874
  Free PE               0
  Allocated PE          127874
  PV UUID               nzzVmz-PJBf-3p7W-Z2lW-8Oq0-IlwE-NbBycQ
   
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               rhel
  PV Size               300.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              76799
  Free PE               0
  Allocated PE          76799
  PV UUID               U9wmoC-CgxT-Hs2s-Qo4f-vVDw-kCE9-foAS6a
   
  --- Physical volume ---
  PV Name               /dev/sda4
  VG Name               rhel
  PV Size               100.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              25599
  Free PE               0
  Allocated PE          25599
  PV UUID               nFIosU-cXRP-e6hh-CYwv-Go46-9pLv-sak605

(6)退出LVM

lvm> quit
  Exiting.

5、文件系统扩容

上面的步骤只是卷扩容了,文件系统还没实现真正扩容
CentOS 7: 由于使用的是 XFS

[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
xfs_growfs: /dev/mapper/centos-root is not a mounted XFS filesystem

报了异常,参数应该换为挂载点/

6、查看扩容后信息

(1)查看挂载点信息

[root@localhost ~]# df -h
文件系统               容量  已用  可用 已用% 挂载点
devtmpfs               3.9G     0  3.9G    0% /dev
tmpfs                  3.9G   12K  3.9G    1% /dev/shm
tmpfs                  3.9G  9.0M  3.9G    1% /run
tmpfs                  3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/rhel-root  792G  655G  137G   83% /
/dev/sdb               197G   39G  149G   21% /docbak
/dev/mapper/rhel-home  100G  1.3G   99G    2% /home
/dev/sda1              497M  167M  330M   34% /boot
tmpfs                  783M     0  783M    0% /run/user/0

(2)查看逻辑卷信息

[root@localhost ~]# lvm
lvm> lvdisplay 
  --- Logical volume ---
  LV Path                /dev/rhel/swap
  LV Name                swap
  VG Name                rhel
  LV UUID                xLcNX4-by9C-Z0BP-3f3B-cIec-0Kkc-QJ3nEZ
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-06-12 12:16:59 +0800
  LV Status              available
  # open                 2
  LV Size                <7.88 GiB
  Current LE             2016
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/rhel/root
  LV Name                root
  VG Name                rhel
  LV UUID                SMbaji-IaiM-iKBP-G9UW-9rNp-NCQ9-X8ee39
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-06-12 12:17:04 +0800
  LV Status              available
  # open                 1
  LV Size                791.62 GiB
  Current LE             202656
  Segments               5
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/rhel/home
  LV Name                home
  VG Name                rhel
  LV UUID                SiC69c-paWj-pcBd-L1ez-6zWK-kw5z-LUekSB
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-06-12 13:07:40 +0800
  LV Status              available
  # open                 1
  LV Size                100.00 GiB
  Current LE             25600
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

扩容挂载点容量的过程已完毕!