虚拟机磁盘扩容
2023-05-23 本文已影响0人
吓死我了_1799
参考链接:扩容分区和文件系统(Linux系统盘) (aliyun.com)
参考本文档前,请牢记,基于磁盘裸分区(无lvm)的扩容,仅能实现单块磁盘的最后一个分区扩容。
关于扩容磁盘前是否取消挂载取决于系统内核:
-
内核版本小于3.6:先取消挂载该分区,再修改分区表,最后扩容文件系统。
-
内核版本大于等于3.6:先修改对应分区表,再通知内核更新分区表,最后扩容文件系统
本文的操作基于centos7 ,内核3.10.0
涉及的工具:
- growpart (shell编写的扩容工具)
- sgdisk(gpt分区环境需要)
- xfsprogs
如果原磁盘为mbr分区,需求是扩容到大于2TB清下,在扩容分区操作前,先进行分区表格式转换,示例 :sgdisk -g /dev/vdb 转为gpt分区
-
第一步确认 现有云盘的分区信息:
使用任何你熟悉的分区管理工具查看,例如:
# fdisk -lu /dev/vdb
[root@localhost ~]# fdisk -lu
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors # sda 系统盘 , 41943040 为sda总扇区数
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 # 磁盘分区类型 dos 为MBR
Disk identifier: 0x000b07ba
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux # 扇区起始位置与终止位置, 这里的Linux 也代表这MBR分区类型
/dev/sda2 2099200 23070719 10485760 83 Linux # 终止位加1 为下一个分区的起始位
/dev/sda3 23070720 41943039 9436160 83 Linux # 最后一个分区的扇区终止位为磁盘总扇区数的前一位
# 请记录分区信息,扩容会用到分区扇区的起止位(越界会导致数据错乱,也是仅支持单块磁盘最后一个分区的扩容原因)
-
第二步 查看分区文件系统的UUID跟文件系统类型
[root@localhost ~]# blkid
/dev/sda1: UUID="bdc4c7a5-998e-4b7d-b451-780607fd3841" TYPE="xfs"
/dev/sda2: UUID="bd01a1e6-0111-47df-9e9e-e0b1e5b3f981" TYPE="xfs"
/dev/sda3: UUID="8990e898-9788-4b95-ac39-007e6a4c27d2" TYPE="xfs"
/dev/sr0: UUID="2017-09-05-14-14-50-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
查看分区挂载点(可选)
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 9.0G 843M 8.2G 10% / # sda 的最后一个分区ID为3 ,对应系统根分区
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda2 10G 76M 10G 1% /var
/dev/sda1 1014M 119M 896M 12% /boot
tmpfs 98M 0 98M 0% /run/user/0
-
第三步 虚机磁盘扩容
扩容步骤随各自云平台,这里不展示。
磁盘扩容后的扇区信息:
[root@localhost ~]# fdisk -lu
Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 sectors # 增加至40G
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: 0x000b07ba
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 23070719 10485760 83 Linux
/dev/sda3 23070720 41943039 9436160 83 Linux
-
第四步 扩容分区(growpart)
推荐使用该方法,减少人为误操作。
安装工具:centos系列
type growpart || yum install -y cloud-utils-growpart
type sgdisk || yum install -y gdisk
type xfsprogs || yum install -y xfsprogs
LC_ALL=en_US.UTF-8
安装工具:debian系列
apt-get update
type growpart || apt-get install -y cloud-guest-utils
type xfsprogs || apt-get install -y xfsprogs
type sgdisk || apt-get install -y gdisk
LC_ALL=en_US.UTF-8
对分区进行扩容
[root@localhost ~]# growpart /dev/sda 3 # 对sda的最后一个分区进行扩容,即为 ‘/’ 分区
CHANGED: partition=3 start=23070720 old: size=18872320 end=41943040 new: size=60815327 end=83886047
对文件系统扩容
[root@localhost ~]# xfs_growfs /
meta-data=/dev/sda3 isize=512 agcount=4, agsize=589760 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=2359040, 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 2359040 to 7601915
# 验证扩容情况
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 29G 844M 29G 3% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda2 10G 255M 9.8G 3% /var
/dev/sda1 1014M 119M 896M 12% /boot
tmpfs 98M 0 98M 0% /run/user/0
-
第四步 扩容分区(手动)
# 扩容sda 系统盘,查看分区文件系统(用于后面使用对应的文件系统扩容工具)
[root@localhost ~]# blkid
/dev/sda1: UUID="bdc4c7a5-998e-4b7d-b451-780607fd3841" TYPE="xfs"
/dev/sda2: UUID="bd01a1e6-0111-47df-9e9e-e0b1e5b3f981" TYPE="xfs"
/dev/sda3: UUID="8990e898-9788-4b95-ac39-007e6a4c27d2" TYPE="xfs"
# 查看挂载点及扩容前的文件系统大小(用于xfs扩容工具,扩容时指定的挂载点)
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 29G 844M 29G 3% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda2 10G 257M 9.8G 3% /var
/dev/sda1 1014M 119M 896M 12% /boot
tmpfs 98M 0 98M 0% /run/user/0
# 磁盘扩容后,查看新增的大小及扇区
[root@localhost ~]# fdisk -lu
Disk /dev/sda: 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
Disk label type: dos
Disk identifier: 0x000b07ba
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 23070719 10485760 83 Linux
/dev/sda3 23070720 83886046 30407663+ 83 Linux # 最后一个分区,对应根文件系统
# 开始扩容系统盘
[root@localhost ~]# fdisk -u /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): p #打印当前分区表信息
Disk /dev/sda: 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
Disk label type: dos
Disk identifier: 0x000b07ba
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 23070719 10485760 83 Linux
/dev/sda3 23070720 83886046 30407663+ 83 Linux
Command (m for help): d # 删除分区
Partition number (1-3, default 3): 3 # 默认执行 最后一个分区的删除
Partition 3 is deleted
Command (m for help): p # 验证分区删除操作
Disk /dev/sda: 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
Disk label type: dos
Disk identifier: 0x000b07ba
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 23070719 10485760 83 Linux
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): # 新建分区的编号,默认为之前删除的3号分区(也必须一致)
First sector (23070720-104857599, default 23070720): # 分区起始位,默认会采用原分区起始位(如不一致,请手动输入,原分区的起始位,必须一致)
Using default value 23070720
Last sector, +sectors or +size{K,M,G} (23070720-104857599, default 104857599): # 分区终止位,默认使用磁盘最大扇区数-1,按需填写
Using default value 104857599
Partition 3 of type Linux and of size 39 GiB is set
Command (m for help): p
Disk /dev/sda: 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
Disk label type: dos
Disk identifier: 0x000b07ba
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 23070719 10485760 83 Linux
/dev/sda3 23070720 104857599 40893440 83 Linux
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.
# 查看扩容后的分区表 (如果新分区表未生效执行,partprobe 或partx -u 通知内核重新扫描磁盘分区,扩容根分区请使用 “partx -u -n 3 /dev/sda”)
[root@localhost ~]# fdisk -lu
Disk /dev/sda: 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
Disk label type: dos
Disk identifier: 0x000b07ba
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 23070719 10485760 83 Linux
/dev/sda3 23070720 104857599 40893440 83 Linux
# 在线扩容必须执行分区扫描,否则扩容的容量,只有在下次重启生效
[root@localhost ~]# lsblk /dev/sda # 查看分区容量是否生效
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 10G 0 part /var
└─sda3 8:3 0 39G 0 part / # 根分区还是39G 未生效
[root@localhost ~]# partx -u -n 3 /dev/sda # 指定重新扫描 系统盘3号分区
[root@localhost ~]# lsblk /dev/sda
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 60G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 10G 0 part /var
└─sda3 8:3 0 49G 0 part / # 扩容容量生效
# 扩容根分区文件系统
[root@localhost ~]# xfs_growfs /
meta-data=/dev/sda3 isize=512 agcount=13, agsize=589760 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=7601915, 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 7601915 to 12844800
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 49G 845M 49G 2% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda2 10G 256M 9.8G 3% /var
/dev/sda1 1014M 119M 896M 12% /boot
tmpfs 98M 0 98M 0% /run/user/0
# 完成扩容
-
扩容(逻辑分区)
原理跟步骤与上面一致,这里简要说明下
[root@localhost ~]# fdisk -lu
Disk /dev/sdb: 17.2 GB, 17179869184 bytes, 33554432 sectors # 原8G,磁盘完成扩容后达到16G
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: 0xa4cc848f
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 4196351 1048576 83 Linux
/dev/sdb3 4196352 6293503 1048576 83 Linux
/dev/sdb4 6293504 16777215 5241856 5 Extended # 逻辑分区的扇区起止位
/dev/sdb5 6295552 8392703 1048576 83 Linux # 逻辑分区
/dev/sdb6 8394752 16777215 4191232 83 Linux # 磁盘的最后一个分区,只能扩容这个分区
# 注:逻辑分区的起止位与终止位,需要预留2048个扇区(按分区表的结构与约定偏移,进行分区对齐,提高性能)
# 如 扩展分区的起始位为 6293504,那么接下的第一个逻辑分区,在其偏移2048,即为第一个逻辑分区的起始位6295552,
# 终止位正常按分配的大小计算,这里为8392703。第二个逻辑分区,基于上一个分区的终止位向后偏移2048 + 1 ,即为起始位,
# 终止位为扩展分区的终止位。
先对逻辑分区进行扩容
[root@localhost ~]# growpart /dev/sdb 4
CHANGED: partition=4 start=6293504 old: size=10483712 end=16777216 new: size=27260895 end=33554399
# 验证逻辑分区扩容
[root@localhost ~]# fdisk -lu
Disk /dev/sdb: 17.2 GB, 17179869184 bytes, 33554432 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: 0xa4cc848f
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 4196351 1048576 83 Linux
/dev/sdb3 4196352 6293503 1048576 83 Linux
/dev/sdb4 6293504 33554398 13630447+ 5 Extended # 这里起止位已更新
/dev/sdb5 6295552 8392703 1048576 83 Linux
/dev/sdb6 8394752 16777215 4191232 83 Linux
# 扩容最后一个分区
[root@localhost ~]# growpart /dev/sdb 6
CHANGED: partition=6 start=8394752 old: size=8382464 end=16777216 new: size=25159647 end=33554399
# 验证分区大小
[root@localhost ~]# fdisk -lu
Disk /dev/sdb: 17.2 GB, 17179869184 bytes, 33554432 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: 0xa4cc848f
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 4196351 1048576 83 Linux
/dev/sdb3 4196352 6293503 1048576 83 Linux
/dev/sdb4 6293504 33554398 13630447+ 5 Extended
/dev/sdb5 6295552 8392703 1048576 83 Linux
/dev/sdb6 8394752 33554398 12579823+ 83 Linux
# 文件系统扩容
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 9.0G 844M 8.2G 10% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda2 10G 255M 9.8G 3% /var
/dev/sda1 1014M 119M 896M 12% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb1 1014M 33M 982M 4% /root/1
/dev/sdb2 1014M 33M 982M 4% /root/2
/dev/sdb3 1014M 33M 982M 4% /root/3
/dev/sdb5 1014M 33M 982M 4% /root/5
/dev/sdb6 4.0G 33M 4.0G 1% /root/6 # 挂载点 ,文件系统 xfs
# 扩容xfs文件系统,使用xfs_growfs
xfs_growfs /root/6
# 扩容ext4文件系统,使用 resize2fs
resize2fs /dev/sda3
-
GPT分区扩容(手动:推荐使用 growpart 工具的方式)
root@user1-PC:~# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p # 输出当前分区表
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sda: 215GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 525MB 524MB fat32 boot, esp
2 525MB 3747MB 3221MB ext4
3 3747MB 90.2GB 86.4GB ext4
(parted) unit s # 修改大小表示方法,使用扇区表示
(parted) p
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sda: 419430400s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 2048s 1026047s 1024000s fat32 boot, esp
2 1026048s 7317503s 6291456s ext4
3 7317504s 176156671s 168839168s ext4
(parted) resizepart 3 # 修改根分区容量
Warning: Partition /dev/sda3 is being used. Are you sure you want to continue?
Yes/No? Yes
End? [176156671s]? 419430399 # 手动指定终止扇区有问题,不符合约束
Error: Unable to satisfy all constraints on the partition.
(parted) resizepart 3 # 重新修改
Warning: Partition /dev/sda3 is being used. Are you sure you want to continue?
Yes/No? Yes
End? [176156671s]? 100% # 使用百分比的模式
(parted) p
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sda: 419430400s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 2048s 1026047s 1024000s fat32 boot, esp
2 1026048s 7317503s 6291456s ext4
3 7317504s 419430366s 412112863s ext4 # 根分区容量已被修改,终止位412112863
root@user1-PC:~# lsblk # 查看分区容量是否生效,如果没有生效参照前部分的partx 指令重新扫描分区
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 500M 0 part /boot/efi
├─sda2 8:2 0 3G 0 part /boot
└─sda3 8:3 0 196.5G 0 part / # 跟分区已扩容
sr0 11:0 1 1024M 0 rom
root@user1-PC:~# resize2fs /dev/sda3 # 执行ext4 文件系统扩容
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/sda3 is mounted on /; on-line resizing required
old_desc_blocks = 11, new_desc_blocks = 25
The filesystem on /dev/sda3 is now 51514096 (4k) blocks long.
root@user1-PC:~#
root@user1-PC:~#
root@user1-PC:~# df -h
文件系统 容量 已用 可用 已用% 挂载点
udev 4.0G 0 4.0G 0% /dev
tmpfs 809M 14M 795M 2% /run
/dev/sda3 193G 6.6G 178G 4% / # 查看扩容结果
tmpfs 4.0G 3.0M 4.0G 1% /dev/shm
tmpfs 5.0M 64K 5.0M 2% /run/lock
tmpfs 4.0G 0 4.0G 0% /sys/fs/cgroup
/dev/sda2 2.9G 96M 2.7G 4% /boot
/dev/sda1 500M 564K 499M 1% /boot/efi
tmpfs 809M 64K 809M 1% /run/user/106
tmpfs 809M 0 809M 0% /run/user/1000