CentOS 7.x 扩容

CentOS 7.x 扩容 #

2018-12-10 11:02

CentOS 增加磁盘之后,还需要对空间进行扩容。

首先查看下系统现在的使用空间,发现只有50G左右。

# df -lh

再来看下已挂载的磁盘。

# fdisk -l

可以看到这么一个数据,说明现在挂载了一个53.7 GB的磁盘没用上。

Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

接下来对这个磁盘进行分区。

# fdisk /dev/sdb
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xe807ee1a.

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

Command 输入n,其它的都默认值即可。完成之后,再输入w进行写入操作。

分区完毕,开始卷扩容,卷扩容使用lvm

# lvm
lvm> pvcreate /dev/sdb1  //初始化刚刚的分区sdb1
  Physical volume "/dev/sdb1" successfully created

此处可能报错 “Device /dev/sdb1 not found.”
原因是“内核没有成功同步分区信息”,手动同步下

# partprobe

继续扩容

lvm> vgextend centos /dev/sdb1  //将刚初始化过的分区加入到虚拟卷组centos
  Volume group "centos" successfully extended
lvm> vgdisplay -v
    Using volume group(s) on command line.
    Found same device /dev/sda2 with same pvid eo1BSOY8EOTv6Q2XzBjI0chqCjZ7Lipo
    Found same device /dev/sdb1 with same pvid he5pJcr6EcrzyaNmdXM1TgLrtVwSp7W7
  --- 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               99.50 GiB
  PE Size               4.00 MiB
  Total PE              25473
  Alloc PE / Size       12674 / 49.51 GiB
  Free  PE / Size       12799 / 50.00 GiB
	……

使用vgdisplay -v ,注意看最后Free PE / Size 12799 / 50.00 GiB,这意味着我们可以进行扩容的空间,记下12799这个值。

lvm> lvextend -l+12799 /dev/mapper/centos-root  //扩容已有卷的容量
  Size of logical volume centos/root changed from 45.51 GiB (11650 extents) to 95.50 GiB (24449 extents).
  Logical volume root successfully resized.
lvm> pvdisplay  //查看卷容量
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               49.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              12674
  Free PE               0
  Allocated PE          12674
  PV UUID               eo1BSO-Y8EO-Tv6Q-2XzB-jI0c-hqCj-Z7Lipo

  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               centos
  PV Size               50.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              12799
  Free PE               0
  Allocated PE          12799
  PV UUID               he5pJc-r6Ec-rzya-NmdX-M1Tg-LrtV-wSp7W7

最后,文件系统扩容。/dev/mapper/centos-root是df -h查看到根目录的挂载点。

# xfs_growfs /dev/mapper/centos-root

CentOS 6.x这里使用的命令是resize2fs /dev/mapper/centos-root,有点不一样,注意下。

再次使用df -lh,跟文章开头相比。我们的空间已经增大了50G:-)

/dev/mapper/centos-root   96G  1.7G   94G   2% /