# LVM扩容

1、执行`fdisk /dev/vda`命令对磁盘剩余空间进行分区；当遇到`Command (m for help):`时输入`n`，表示新建分区；然后当需要输入时，直接按Enter键；当再次遇到`Command (m for help):`时输入`w`，表示写入保存。

```
[root@host-192-168-60-16 ~]# fdisk /dev/vda
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.


Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 
Using default response p
Partition number (3,4, default 3): 
First sector (20971520-104857599, default 20971520): 
Using default value 20971520
Last sector, +sectors or +size{K,M,G} (20971520-104857599, default 104857599): 
Using default value 104857599
Partition 3 of type Linux and of size 40 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
```

2、执行以下命令更新磁盘分区表

```
[root@host-192-168-60-16 ~]# partprobe
```

3、查看刚刚新建的分区，发现名字为`vda3`，大小为40G

```
[root@host-192-168-60-16 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1  458K  0 rom  
vda             252:0    0   50G  0 disk 
├─vda1          252:1    0    1G  0 part /boot
├─vda2          252:2    0    9G  0 part 
│ ├─centos-root 253:0    0    8G  0 lvm  /
│ └─centos-swap 253:1    0    1G  0 lvm  [SWAP]
└─vda3          252:3    0   40G  0 part
```

4、使用`vda3`分区创建一个pv

```
[root@host-192-168-60-16 ~]# pvcreate /dev/vda3
```

5、把`/dev/vda3`这个pv加入到`centos`这个vg中，此时vg的空间会增加40G

```
[root@host-192-168-60-16 ~]# vgextend centos /dev/vda3
```

6、查看`centos`这个vg的大小，发现为48.99G

```
[root@host-192-168-60-16 ~]# 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                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               48.99 GiB
  PE Size               4.00 MiB
  Total PE              12542
  Alloc PE / Size       2303 / <9.00 GiB
  Free  PE / Size       10239 / <40.00 GiB
  VG UUID               JuOM4n-bUuZ-eSbn-33eU-Q01c-aXcC-8CMiMD
```

7、扩展`/dev/centos/root`

在第`6`步中我们看到`centos`这个vg的总大小为48.99GiB，`Free PE`为10239 ，`Free Size`小于40.00GiB。我们把所有`Free PE`都加到`/dev/centos/root`这个lv下

```
[root@host-192-168-60-16 ~]# lvextend -l +10239 /dev/centos/root
```

注意：如果只是想把`/dev/centos/root`扩展到指定大小，比如40G，那么可以执行如下命令：

```
$ lvextend -L 40G /dev/centos/root
```

8、执行以下命令使根目录生效

```
[root@host-192-168-60-16 ~]# xfs_growfs /
```

9、查看根目录大小

```
[root@host-192-168-60-16 ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        47G  1.3G   46G   3% /
devtmpfs                devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs                   tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs                   tmpfs     3.9G  8.6M  3.9G   1% /run
tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1               xfs      1014M  148M  867M  15% /boot
tmpfs                   tmpfs     783M     0  783M   0% /run/user/0
```

## Reference

* <http://man.linuxde.net/lvextend>
