一、系统动态扩容磁盘

系统

原磁盘大小

扩容大小

centos7.5

20G

18G

本次实验为虚拟机,首先看看本机原来的磁盘大小

[root@123 ~]# lsblk
NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                   8:0    0   20G  0 disk    #sda总容量为20G
├─sda1                8:1    0    1G  0 part /boot
└─sda2                8:2    0   19G  0 part 
  ├─centos_123-root 253:0    0   17G  0 lvm  /
  └─centos_123-swap 253:1    0    2G  0 lvm  [SWAP]
sr0                  11:0    1 1024M  0 rom  
[root@123 ~]#

看到磁盘sda总容量为20G

接下来在虚拟机里面添加磁盘,容量为18G

centos 7 查看未分配的分区 centos如何查看未分配空间_centos


新增完成后重启虚拟机再次查看磁盘大小

[root@123 ~]# lsblk
NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                   8:0    0   20G  0 disk 
├─sda1                8:1    0    1G  0 part /boot
└─sda2                8:2    0   19G  0 part 
  ├─centos_123-root 253:0    0   17G  0 lvm  /
  └─centos_123-swap 253:1    0    2G  0 lvm  [SWAP]
sdb                   8:16   0   18G  0 disk 
sr0                  11:0    1 1024M  0 rom

已经看到sdb有个18G的容量

接下来进行磁盘分区为sdb

[root@123 ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0xbc621d21 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):1
起始 扇区 (2048-37748735,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-37748735,默认为 37748735):
将使用默认值 37748735
分区 1 已设置为 Linux 类型,大小设为 18 GiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@123 ~]#

centos 7 查看未分配的分区 centos如何查看未分配空间_linux_02


再次查看所有块设备信息,发现已经多出来一个sdb1的分区

[root@123 ~]# lsblk
NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                   8:0    0   20G  0 disk 
├─sda1                8:1    0    1G  0 part /boot
└─sda2                8:2    0   19G  0 part 
  ├─centos_123-root 253:0    0   17G  0 lvm  /
  └─centos_123-swap 253:1    0    2G  0 lvm  [SWAP]
sdb                   8:16   0   18G  0 disk 
└─sdb1                8:17   0   18G  0 part 
sr0                  11:0    1 1024M  0 rom

格式分区,将/dev/sdb1分区格式化xfs格式

[root@123 ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=1179584 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=4718336, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

创建物理卷,将/dev/sdb1分区加入到pv(会要确认,直接输入y)

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

列出物理卷信息,查询是否创建物理卷成功

[root@123 ~]# pvs
  PV         VG         Fmt  Attr PSize   PFree  
  /dev/sda2  centos_123 lvm2 a--  <19.00g      0 
  /dev/sdb1             lvm2 ---  <18.00g <18.00g
[root@123 ~]#

centos 7 查看未分配的分区 centos如何查看未分配空间_centos 7 查看未分配的分区_03


动态扩展LVM卷组,通过向卷组中添加物理卷来增加卷组的容量

Vg卷组扩容,将物理卷/dev/sdb1 加入centos_123卷组中

[root@123 ~]# vgextend centos_123 /dev/sdb1
  Volume group "centos_123" successfully extended
[root@123 ~]#

查询是否添加卷组成功

[root@123 ~]# vgs
  VG         #PV #LV #SN Attr   VSize  VFree  
  centos_123   2   2   0 wz--n- 36.99g <18.00g
[root@123 ~]#

centos 7 查看未分配的分区 centos如何查看未分配空间_linux_04


扩容LVM

给逻辑卷/dev/centos_123-root添加18G的大小(这里是添加了17.9G,添加18G提示容量不够,多多少少有一点点差异)

[root@123 ~]# lvextend -L +17.9G /dev/centos_123/root 
  Rounding size to boundary between physical extents: 17.90 GiB.
  Size of logical volume centos_123/root changed from <17.00 GiB (4351 extents) to <34.90 GiB (8934 extents).
  Logical volume centos_123/root successfully resized.
[root@123 ~]#

查看是否已经扩容到centos_123-root(已经从最开始的17G扩容到34.9G了)

[root@123 ~]# lsblk
NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                   8:0    0   20G  0 disk 
├─sda1                8:1    0    1G  0 part /boot
└─sda2                8:2    0   19G  0 part 
  ├─centos_123-root 253:0    0 34.9G  0 lvm  /
  └─centos_123-swap 253:1    0    2G  0 lvm  [SWAP]
sdb                   8:16   0   18G  0 disk 
└─sdb1                8:17   0   18G  0 part 
  └─centos_123-root 253:0    0 34.9G  0 lvm  /
sr0                  11:0    1 1024M  0 rom

再通过xfs_growfs自动扩展xfs文件系统到最大的可用大小

[root@123 ~]# xfs_growfs /dev/centos_123/root 
meta-data=/dev/mapper/centos_123-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 4455424 to 9148416

最后,通过df -TH 或者lsblk可用查看到具体扩容后的信息

二、系统挂载未分配硬盘空间

系统

总磁盘容量

被分配容量

剩余磁盘容量

centos7.5

100G

42G

58G

centos 7 查看未分配的分区 centos如何查看未分配空间_运维_05

  1. 先查看系统现容量
[root@test ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/centos_test-root   30G 1010M   29G   4% /
devtmpfs                      2.0G     0  2.0G   0% /dev
tmpfs                         2.0G     0  2.0G   0% /dev/shm
tmpfs                         2.0G   12M  2.0G   1% /run
tmpfs                         2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda2                     4.0G  130M  3.9G   4% /boot
tmpfs                         394M     0  394M   0% /run/user/0
[root@test ~]#

加起来刚好42G

  1. 查看分区情况fdisk –l
[root@test ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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
Disk label type: dos
Disk identifier: 0x000ae456

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048        6143        2048   83  Linux
/dev/sda2   *        6144     8394751     4194304   83  Linux
/dev/sda3         8394752    88102911    39854080   8e  Linux LVM

Disk /dev/mapper/centos_test-root: 32.2 GB, 32212254720 bytes, 62914560 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

Disk /dev/mapper/centos_test-swap: 8589 MB, 8589934592 bytes, 16777216 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

[root@test ~]#
  1. 增加分区
[root@test ~]# fdisk /dev/sda
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    #n代表新建分区
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p      #p代表主分区
Selected partition 4
First sector (88102912-209715199, default 88102912):     #回车默认大小
Using default value 88102912
Last sector, +sectors or +size{K,M,G} (88102912-209715199, default 209715199):  #回车默认大小
Using default value 209715199
Partition 4 of type Linux and of size 58 GiB is set

Command (m for help): t                   #t代表指定分区格式
Partition number (1-4, default 4): 4      #编号为4
Hex code (type L to list all codes): 8e   #8e为分区格式为LVM
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): w  #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.
  1. 最后再次查看(fdisk –l),就能看到我们增加的分区了。
[root@test ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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
Disk label type: dos
Disk identifier: 0x000ae456

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048        6143        2048   83  Linux
/dev/sda2   *        6144     8394751     4194304   83  Linux
/dev/sda3         8394752    88102911    39854080   8e  Linux LVM
/dev/sda4        88102912   209715199    60806144   8e  Linux LVM   #这个就是新加的编号为4的

Disk /dev/mapper/centos_test-root: 32.2 GB, 32212254720 bytes, 62914560 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

Disk /dev/mapper/centos_test-swap: 8589 MB, 8589934592 bytes, 16777216 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

[root@test ~]#
  1. 下一步,创建物理卷
[root@test ~]# pvcreate /dev/sda4
  Physical volume "/dev/sda4" successfully created.
  1. 添加物理卷(/dev/sda4)到卷组(centos_test)

这里先用vgdisplay 命令查看卷组名

[root@test ~]# vgdisplay 
  --- Volume group ---
  VG Name               centos_test     #这个就是组名
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               38.00 GiB
  PE Size               4.00 MiB
  Total PE              9729
  Alloc PE / Size       9728 / 38.00 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               WTpQtt-DChr-BV23-C836-AlQe-tSXK-RJ1GfA

添加添加物理卷(/dev/sda4)到卷组(centos_test)

[root@test ~]# vgextend centos_test /dev/sda4
  Volume group "centos_test" successfully extended
  1. 查看centos卷组的属性
[root@test ~]# vgdisplay 
  --- Volume group ---
  VG Name               centos_test
  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               95.99 GiB
  PE Size               4.00 MiB
  Total PE              24574
  Alloc PE / Size       9728 / 38.00 GiB
  Free  PE / Size       14846 / 57.99 GiB       #可以看到有58G的空闲空间可以扩展。
  VG UUID               WTpQtt-DChr-BV23-C836-AlQe-tSXK-RJ1GfA
   
[root@test ~]#
  1. 将空闲的空间都分配给root文件系统(/dev/mapper/centos_test-root路径用df -h查看)
[root@test ~]# lvextend -L +57.99G /dev/mapper/centos_test-root
  Rounding size to boundary between physical extents: 57.99 GiB.
  Size of logical volume centos_test/root changed from 30.00 GiB (7680 extents) to 87.99 GiB (22526 extents).
  Logical volume centos_test/root successfully resized.
[root@test ~]#
  1. 对root文件系统执行扩容
[root@test ~]# xfs_growfs /dev/mapper/centos_test-root
meta-data=/dev/mapper/centos_test-root isize=512    agcount=4, agsize=1966080 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=7864320, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=3840, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 7864320 to 23066624
[root@test ~]#
  1. 查看扩容后文件系统的硬盘使用
[root@test ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/centos_test-root   88G 1011M   87G   2% /              #已经加到88G了
devtmpfs                      2.0G     0  2.0G   0% /dev
tmpfs                         2.0G     0  2.0G   0% /dev/shm
tmpfs                         2.0G   12M  2.0G   1% /run
tmpfs                         2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda2                     4.0G  130M  3.9G   4% /boot
tmpfs                         394M     0  394M   0% /run/user/0
[root@test ~]#