Linux-使用 growpart 工具自动扩容磁盘分区
Linux-使用 growpart 工具自动扩容磁盘分区
- 需要预先将云磁盘在 web 控制台进行扩容
- growpart 为分区表修改工具
1.安装扩容工具
- centos7
# 安装 growpart
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum search growpart
yum install cloud-utils-growpart
# 分区表为 GPT 需要安装 gdisk
yum install gdisk -y
# 文件系统为 xfs 需要安装 xfsprogs
yum install xfsprogs -y
- ubuntu18
apt install -y cloud-guest-utils
2.检查并记录磁盘分区信息
- 包括磁盘分区的 容量,分区表类型,扩容前后扇区位置
# 检查分区使用情况
df -h
--------------------------------
[root@zuiyoujie ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 0 16G 0% /dev/shm
tmpfs 16G 50M 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/vda1 100G 3.2G 97G 4% /
tmpfs 3.2G 0 3.2G 0% /run/user/0
--------------------------------
# 磁盘分区信息
fdisk -l
# 磁盘容量
lsblk
--------------------------------
[root@zuiyoujie ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 100G 0 disk
└─vda1 253:1 0 100G 0 part /
vdb 253:16 0 200G 0 disk
└─vdb1 253:17 0 100G 0 part /data
--------------------------------
# 检查磁盘文件系统类型 file 命令查看分区文件
file -s /dev/nvme?n* # 针对 nvme 磁盘
file -s /dev/vdb
--------------------------------
[root@zuiyoujie ~]# file -s /dev/vdb
/dev/vdb: SGI XFS filesystem data (blksz 4096, inosz 512, v2 dirs)
--------------------------------
# 检查磁盘文件系统类型 blkid 查看分区信息
blkid
--------------------------------
[root@zuiyoujie ~]# blkid
/dev/vda1: UUID="dfca4c40-ed04-4c41-928f-d51dec01c3b3" TYPE="ext4"
/dev/vdb1: UUID="4a7b0493-f24d-43d5-b499-822df72e7867" TYPE="xfs"
--------------------------------
3.修改分区表
- SSD 磁盘
# 修改分区表信息 ssd 磁盘
growpart /dev/vdb 1
lsblk
- NVME 磁盘
# 修改分区表 nvme 磁盘(根磁盘)
growpart /dev/nvme1n1 1
lsblk
# 修改磁盘分区表(第二磁盘)
growpart /dev/nvme0n1 1
lsblk
4.扩容磁盘分区-刷新文件系统
- ext4 文件系统
# 扩容分区空间
resize2fs /dev/vdb1
# 扩容分区空间(根磁盘)
resize2fs /dev/nvme1n1p1
# 扩容分区空间(第二磁盘)
resize2fs /dev/nvme0n1p1
# 检查分区用量
df -h
--------------------
- xfs 文件系统
# 参数可以使用设备名 /dev/vdb 或者 挂载点 /data
df -h
xfs_growfs -d /data
xfs_growfs -d /dev/vdb
lsblk
df -h|grep /dev/vdb
5.实例演示
5.1.SSD 磁盘
- 实例演示:根分区由 8GB 扩容到 50GB
# 扩容前检查磁盘分区信息
-----------------
root@zuiyoujie:/opt/tools# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 7.8G 3.6G 3.9G 49% / # 检查磁盘分区信息
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 1.9G 12K 1.9G 1% /dev
tmpfs 377M 352K 377M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 1.9G 0 1.9G 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/vdb 15G 39M 15G 1% /mnt
root@zuiyoujie:/opt/tools# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 202:0 0 8G 0 disk
└─vda1 202:1 0 8G 0 part / # 检查磁盘块信息
vdb 202:16 0 15.3G 0 disk /mnt
vdc 202:32 0 15.3G 0 disk
root@zuiyoujie:/opt/tools# file -s /dev/vda
/dev/vda: x86 boot sector
-----------------
# 扩容分区并检查磁盘信息
root@zuiyoujie:/opt/tools# growpart /dev/vda 1
CHANGED: partition=1 start=16065 old: size=16755795 end=16771860 new: size=104840190,end=104856255
# 磁盘容量扩容已生效,分区表已变更
root@zuiyoujie:/opt/tools# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 202:0 0 50G 0 disk
└─vda1 202:1 0 50G 0 part / # 磁盘分区生效
vdb 202:16 0 15.3G 0 disk /mnt
vdc 202:32 0 15.3G 0 disk
# 此时磁盘可用容量未增加
root@zuiyoujie:/opt/tools# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 7.8G 3.6G 3.9G 49% / # 修改分区表,容量还未生效
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 1.9G 12K 1.9G 1% /dev
tmpfs 377M 352K 377M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 1.9G 0 1.9G 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/vdb 15G 39M 15G 1% /mnt
# 扩容磁盘分区并检查分区信息
root@zuiyoujie:/opt/tools# resize2fs /dev/vda1
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 4
The filesystem on /dev/vda1 is now 13105023 blocks long.
# 此时磁盘可用容量已增加
root@zuiyoujie:/opt/tools# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 50G 3.6G 44G 8% / # 扩容完毕,容量生效
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 1.9G 12K 1.9G 1% /dev
tmpfs 377M 352K 377M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 1.9G 0 1.9G 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/vdb 15G 39M 15G 1% /mnt
---------------------------
5.2.NVME 磁盘
- 根分区由 20GB 扩容到 100GB
# 扩容前检查磁盘状态
--------------------
root@zuiyoujie:/opt/tools# df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.8G 0 3.8G 0% /dev
tmpfs 764M 78M 687M 11% /run
/dev/nvme0n1p1 20G 15G 5.1G 74% /
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
tmpfs 764M 0 764M 0% /run/user/0
root@zuiyoujie:/opt/tools# file -s /dev/nvme?n*
/dev/nvme0n1: DOS/MBR boot sector
/dev/nvme0n1p1: Linux rev 1.0 ext4 filesystem data, UUID=01909065-0362-439c-9af2-29d0fc837f6b, volume name "cloudimg-rootfs"
(needs journal recovery) (extents)
(large files) (huge files)
-------------------
# 扩容后查看磁盘容量变更
root@zuiyoujie:/opt/tools# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 100G 0 disk # 磁盘大小已经修改为 100GB
└─nvme0n1p1 259:1 0 20G 0 part / # 分区大小为 20GB
# 修改分区表
root@zuiyoujie:/opt/tools# growpart /dev/nvme0n1 1
CHANGED: partition=1 start=2048 old: size=41940959 end=41943007 new: size=209713119,end=209715167
# 查看修改后的磁盘分区表
root@zuiyoujie:/opt/tools# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 100G 0 disk
└─nvme0n1p1 259:1 0 100G 0 part / # 分区表已经扩容(识别)为 100GB
# 此时磁盘分区可用容量未生效
root@zuiyoujie:/opt/tools# df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.8G 0 3.8G 0% /dev
tmpfs 764M 78M 687M 11% /run
/dev/nvme0n1p1 20G 15G 5.1G 74% / # 未执行 resize2fs 命令,磁盘容量未增加
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
tmpfs 764M 0 764M 0% /run/user/0
# 扩容磁盘分区
root@zuiyoujie:/opt/tools# resize2fs /dev/nvme0n1p1 # 执行扩容命令,扩容磁盘
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/nvme0n1p1 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 7
The filesystem on /dev/nvme0n1p1 is now 26214139 (4k) blocks long.
# 此时磁盘分区可用容量已经生效
root@zuiyoujie:/opt/tools# df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.8G 0 3.8G 0% /dev
tmpfs 764M 78M 687M 11% /run
/dev/nvme0n1p1 97G 15G 83G 15% / # 实际分区大小为 100GB
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
tmpfs 764M 0 764M 0% /run/user/0
------------------------------------------------
10.参考地址
- 扩展卷之后扩容文件系统(分区)容量
https://docs.aws.amazon.com/zh_cn/AWSEC2/latest/UserGuide/recognize-expanded-volume-linux.html#extend-linux-volume-partition
https://help.aliyun.com/document_detail/113316.html?spm=a2c4g.11186623.2.18.6fe75d6brpDtOv#concept-syg-jxz-2hb
作者:天生帅才 www.zuiyoujie.com
本文版权归作者和博客园共有,如果感觉有用可以随意打赏,感谢支持,欢迎转载