CentOS 7 根分区扩容

Overview

1. 前言

以UEFI模式安装CentOS 7时,默认会划出几个分区:

  • /boot:xfs
  • /boot/efi:vfat
  • /:lvm+xfs
  • /home:lvm+xfs
  • /swap:lvm+swap

其中前两个分区大小固定,只用于存放用于启动系统的相关内核文件,后三个分区默认使用lvm逻辑卷。

lvm是内核块设备和物理设备之间的一个抽象层,可以将多块物理磁盘组合起来形成一个存储池,也支持不停机的情况下动态调整逻辑卷大小。

博主自用的VPS和开发机一般都直接使用root账号,root账户文件、默认软件安装位置、docker镜像等也存储在根分区下,因为根分区空间告急,于是尝试了下删除无用逻辑卷并扩容根分区,调整后的结果如下:

 1➜  ~ lsblk
 2NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
 3sr0              11:0    1  1024M  0 rom
 4sda               8:0    0   512G  0 disk
 5├─sda2            8:2    0     1G  0 part /boot
 6├─sda3            8:3    0 510.8G  0 part
 7│ └─centos-root 253:0    0 510.8G  0 lvm  /
 8└─sda1            8:1    0   200M  0 part /boot/efi
 9➜  ~ df -Th
10Filesystem              Type      Size  Used Avail Use% Mounted on
11devtmpfs                devtmpfs  7.9G     0  7.9G   0% /dev
12tmpfs                   tmpfs     7.9G     0  7.9G   0% /dev/shm
13tmpfs                   tmpfs     7.9G  8.5M  7.9G   1% /run
14tmpfs                   tmpfs     7.9G     0  7.9G   0% /sys/fs/cgroup
15/dev/mapper/centos-root xfs       511G   12G  499G   3% /
16tmpfs                   tmpfs     7.9G  4.0K  7.9G   1% /tmp
17/dev/sda2               xfs      1014M  125M  889M  13% /boot
18/dev/sda1               vfat      200M   12M  189M   6% /boot/efi
19tmpfs                   tmpfs     1.6G     0  1.6G   0% /run/user/0
20➜  ~ cat /etc/fstab
21
22#
23# /etc/fstab
24# Created by anaconda on Mon Jun 24 22:51:30 2019
25#
26# Accessible filesystems, by reference, are maintained under '/dev/disk'
27# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
28#
29/dev/mapper/centos-root /                       xfs     defaults        0 0
30UUID=3e56effb-dcb3-4af5-ba4f-fb4ecda29738 /boot                   xfs     defaults        0 0
31UUID=36E8-0DB4          /boot/efi               vfat    umask=0077,shortname=winnt 0 0

2. 操作步骤

2.1 home分区卸载与备份

博主的home目录中无数据,因此跳过备份,如果有相关程序使用home目录,也需要先关闭。

执行以下命令卸载/home分区

1umount /home

2.2 swap分区关闭与卸载

这里直接关闭掉swap分区,后续有需要时使用swap文件代替。

1swapoff -a
2umount /swap

2.3 /etc/fstab 修改

/etc/fstab 文件中保存着开机时相关分区的挂载配置,编辑该文件,删除掉home目录与swap分区的挂载配置。

2.4 lvm删除与扩容

使用lvs命令获取逻辑卷信息

1➜  ~ lvs
2  LV   VG     Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
3  root centos -wi-ao---- <100.0g
4  home centos -wi-ao---- <400.0g
5  swap centos -wi-ao---- <10.8g

删除home与swap逻辑卷

1lvremove /dev/mapper/centos-home
2lvremove /dev/mapper/centos-swap

根分区逻辑卷扩容,分配所有空闲空间

1lvresize -l +100%FREE /dev/mapper/centos-root

2.5 xfs扩容

lvm卷扩容后还需要执行xfs扩容,才能使用新分配的空间

1xfs_growfs /dev/mapper/centos-root

2.6 配置同步

1dracut --regenerate-all --force

3. 非LVM模式的分区扩容

使用官方镜像安装到物理机时,默认会使用LVM,通常稀里糊涂的一路下来就使用了LVM。但是使用VPS或者官方提供的Cloud镜像等面向虚拟化的场景时,就不会启用LVM了,这个时候需要手动分区,使用 fdisk 删除无用的分区,再进行扩容。

3.1 获取分区信息

 1➜  ~ fdisk -l
 2Disk /dev/vda: 23.6 GB, 23622320128 bytes, 46137344 sectors
 3Units = sectors of 1 * 512 = 512 bytes
 4Sector size (logical/physical): 512 bytes / 512 bytes
 5I/O size (minimum/optimal): 512 bytes / 512 bytes
 6Disk label type: dos
 7Disk identifier: 0x000958c2
 823067648
 9   Device Boot      Start         End      Blocks   Id  System
10/dev/vda1   *        2048    46069375    23033664   83  Linux
11/dev/vda2        46069376    46134911       32768   82  Linux swap / Solaris

可以看到 /dev/vda 被划分为两个分区,由于存储空间是连续的,我们可以直接删除两个分区后重建分区。

3.2 分区删除与重建

 1➜  ~ fdisk /dev/vda                                                                      
 2Welcome to fdisk (util-linux 2.23.2).                                                    
 3                                                                                         
 4Changes will remain in memory only, until you decide to write them.                      
 5Be careful before using the write command.                                               
 6                                                                                                                                                     
 7Command (m for help): p                                                                  
 8                                                                                         
 9Disk /dev/vda: 23.6 GB, 23622320128 bytes, 46137344 sectors                              
10Units = sectors of 1 * 512 = 512 bytes                                                   
11Sector size (logical/physical): 512 bytes / 512 bytes                                    
12I/O size (minimum/optimal): 512 bytes / 512 bytes                                        
13Disk label type: dos                                                                     
14Disk identifier: 0x000958c2                                                              
15                                                                                         
16   Device Boot      Start         End      Blocks   Id  System                           
17/dev/vda1   *        2048    46069375    23033664   83  Linux                            
18/dev/vda2        46069376    46134911       32768   82  Linux swap / Solaris             
19                                                                                         
20Command (m for help): d                                                                  
21Partition number (1,2, default 2): 2                                                     
22Partition 2 is deleted                                                                   
23                                                                                         
24Command (m for help): d                                                                  
25Selected partition 1                                                                     
26Partition 1 is deleted                                                                                                                                                                                               
27Command (m for help): p                                                                  
28                                                                                         
29Disk /dev/vda: 23.6 GB, 23622320128 bytes, 46137344 sectors                              
30Units = sectors of 1 * 512 = 512 bytes                                                   
31Sector size (logical/physical): 512 bytes / 512 bytes                                    
32I/O size (minimum/optimal): 512 bytes / 512 bytes                                        
33Disk label type: dos                                                                     
34Disk identifier: 0x000958c2                                                              
35                                                                                         
36   Device Boot      Start         End      Blocks   Id  System                           
37                                                                                         
38Command (m for help): n                                                                  
39Partition type:                                                                          
40   p   primary (0 primary, 0 extended, 4 free)                                           
41   e   extended                                                                          
42Select (default p):                                                                      
43Using default response p                                                                 
44Partition number (1-4, default 1):                                                       
45First sector (2048-46137343, default 2048):                                              
46Using default value 2048                                                                 
47Last sector, +sectors or +size{K,M,G} (2048-46137343, default 46137343):                 
48Using default value 46137343                                                             
49Partition 1 of type Linux and of size 22 GiB is set                                      
50                                                                                         
51Command (m for help): w                                                                  
52The partition table has been altered!                                                    
53                                                                                         
54Calling ioctl() to re-read partition table.                                              
55                                                                                         
56WARNING: Re-reading the partition table failed with error 16: Device or resource busy.   
57The kernel still uses the old table. The new table will be used at                       
58the next reboot or after you run partprobe(8) or kpartx(8)                               
59Syncing disks.                                                                                                                                            
60➜  ~ lsblk                                                                               
61NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT                                               
62sr0     11:0    1 1024M  0 rom                                                           
63vda    253:0    0   22G  0 disk                                                          
64├─vda2 253:2    0   32M  0 part                                                          
65└─vda1 253:1    0   22G  0 part /  

操作过程都在日志中,我们首先删除了两个分区,然后从vda1起始扇区开始,分配全部空间。

扩容后,由于系统仍旧缓存着老分区表,执行 lsblk 会发现还存在两个分区,重启后才会同步。

3.3 ext4扩容

 1➜  ~ lsblk
 2NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
 3sr0     11:0    1 1024M  0 rom
 4vda    253:0    0   22G  0 disk
 5└─vda1 253:1    0   22G  0 part /
 6➜  ~ resize2fs /dev/vda1
 7resize2fs 1.42.9 (28-Dec-2013)
 8Filesystem at /dev/vda1 is mounted on /; on-line resizing required
 9old_desc_blocks = 3, new_desc_blocks = 3
10The filesystem on /dev/vda1 is now 5766912 blocks long.
11➜  ~ fdisk -l
12
13Disk /dev/vda: 23.6 GB, 23622320128 bytes, 46137344 sectors
14Units = sectors of 1 * 512 = 512 bytes
15Sector size (logical/physical): 512 bytes / 512 bytes
16I/O size (minimum/optimal): 512 bytes / 512 bytes
17Disk label type: dos
18Disk identifier: 0x000958c2
19
20   Device Boot      Start         End      Blocks   Id  System
21/dev/vda1            2048    46137343    23067648   83  Linux

重启后会发现vda2消失了,只剩下vda1,这里使用的是ext4文件系统,需要执行 resize2fs 命令扩容文件系统。

整个步骤跟LVM模式扩容类似,但操作起来的风险就大了很多,这种情况只适用于同一磁盘上的分区扩容,而LVM可以实现不停机的跨物理盘扩容。

comments powered by Disqus