# CentoS 7 系统 扩容根分区

#### 博客作者：联系请[点击](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)，搬运不易，希望请作者喝咖啡，可以点击[联系博客作者](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)

## 需求场景：

{% hint style="success" %}
需要将客户物理机磁盘剩余的空闲空间分区，合并到逻辑卷，扩展根分区
{% endhint %}

## **当前环境：**

```sh
[root@master ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   10T  9.5T  549G   95% /
devtmpfs                  16G     0   16G    0% /dev
tmpfs                     16G     0   16G    0% /dev/shm
tmpfs                     16G  9.7M   16G    1% /run
tmpfs                     16G     0   16G    0% /sys/fs/cgroup
/dev/vda2                2.0G  166M  1.9G    9% /boot
tmpfs                    3.2G     0  3.2G    0% /run/user/0
```

可以看到vda 磁盘有20T 还有10T空闲空间没有分配

```sh
[root@master ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom  
vda             252:0    0 19.8T  0 disk 
├─vda1          252:1    0    1M  0 part 
├─vda2          252:2    0    2G  0 part /boot
└─vda3          252:3    0   10T  0 part 
   └─centos-root 253:0   0   10T  0 lvm  /
```

## 操作步骤：

### 第一步：将剩余空间分区

<pre class="language-sh"><code class="lang-sh">[root@master ~]# parted /dev/vda

(parted) <a data-footnote-ref href="#user-content-fn-1">print</a>                                                            
错误: 备份 GPT 表不像应该的那样出现在磁盘的末尾。这可能意味这其它操作系统相信磁盘小一些。通过将备份移动到末尾 (并删除旧备份) 来修正？

修正/Fix/忽略/Ignore/放弃/Cancel? Fix                                     
警告: Not all of the space available to /dev/vda appears to be used, you can fix the GPT to use all of the
space (an extra 1468006400 blocks) or continue with the current setting? 

修正/Fix/忽略/Ignore? Fix                                                 
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 21.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start   End     Size    File system  Name                  标志
 1      1049kB  211MB   210MB   fat16        EFI System Partition  启动
 2      211MB   1285MB  1074MB  xfs
 3      1285MB  537GB   536GB                                      lvm

(parted) mkpart                                                           
分区名称？  []? 4                                                         
文件系统类型？  [ext2]? xfs                                               
起始点？ 11.0TB                                                            
结束点？ 21.7TB
                                                           
(parted) print                                                            
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 21.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start   End     Size    File system  Name  标志
 1      1049kB  2097kB  1049kB                     bios_grub
 2      2097kB  2150MB  2147MB  xfs
 3      2150MB  11.0TB  11.0TB                     lvm
 4      11.0TB  21.7TB  10.7TB               4
(parted) quit                                                             
信息: You may need to update /etc/fstab.
</code></pre>

{% hint style="info" %}
说明：parted 分区退出自动保存，不像fdisk需要保存
{% endhint %}

### 第二步：格式化分区

```sh
[root@master ~]# mkfs.xfs /dev/vda4
```

查看当前逻辑卷

```sh
[root@master ~]# vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  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               10 TiB
  PE Size               4.00 MiB
  Total PE              5180926
  Alloc PE / Size       5180926 / 10 TiB
  Free  PE / Size       0 / 0   
  VG UUID               jw3yqY-Ssfb-NNu3-iwDN-4CV2-d4Nq-uFL0So
```

### 第三步：逻辑化分区/dev/vda4，准备给centos逻辑卷扩容

```sh
[root@master ~]# pvcreate /dev/vda4
WARNING: xfs signature detected on /dev/vda4 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/vda4.
  Physical volume "/dev/vda4" successfully created.
```

### 第四步：开始扩容

```sh
[root@master ~]# vgextend centos /dev/vda4
  Volume group "centos" successfully extended
[root@master ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  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.76 TiB
  PE Size               4.00 MiB
  Total PE              5180926
  Alloc PE / Size       5180926 / 19.76 TiB
  Free  PE / Size       0 / 0   
  VG UUID               jw3yqY-Ssfb-NNu3-iwDN-4CV2-d4Nq-uFL0So
```

{% hint style="info" %}
说明：磁盘空闲空间增长到19.76T,说明扩容成功
{% endhint %}

### 第五步：扩容/dev/mapper/centos-root逻辑卷分区

```sh
[root@master ~]# lvextend -l +100%FREE  /dev/mapper/centos-root 
  Size of logical volume centos/root changed from <10.00 TiB (2620927 extents) to 19.76 TiB (5180926 extents).
  Logical volume centos/root successfully resized.
```

### 第六步：更新逻辑卷分区的大小

```sh
[root@master ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=10, agsize=268435455 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2683829248, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=521728, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2683829248 to 5305268224
```

### 第七步：检查是否扩容更新成功

```sh
[root@master ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   20T  9.5T   11T   48% /
devtmpfs                  16G     0   16G    0% /dev
tmpfs                     16G     0   16G    0% /dev/shm
tmpfs                     16G  9.7M   16G    1% /run
tmpfs                     16G     0   16G    0% /sys/fs/cgroup
/dev/vda2                2.0G  166M  1.9G    9% /boot
tmpfs                    3.2G     0  3.2G    0% /run/user/0
```

[^1]:


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang.gitbook.io/about-the-author/yun-wei-ri-chang-ji-lu/centos-7-xi-tong-kuo-rong-gen-fen-qu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
