操作场景
弹性云主机创建成功后,如果发现系统盘分区的容量大小和实际创建的系统盘大小不一致,可以将空白分区扩容到根分区,扩容系统盘的空白空间。
本节介绍了怎样将空白分区在线扩容到非末尾的root分区。
操作步骤
以CentOS 6.5 64bit 100GB系统盘为例,root分区在非末尾分区(e.g: /dev/xvda1: root,/dev/xvda2: swap)的扩容场景。
- 执行以下命令,查询扩容系统盘的分区情况。
parted -l /dev/xvda
[root@sluo-ecs-a611 ~]# parted -l /dev/xvda
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 41.0GB 40.9GB primary ext4 boot
2 41.0GB 42.9GB 2000MB primary linux-swap(v1)
其中,第一分区是根分区,第二分区是swap分区。
- 执行以下命令,编辑fstab分区表,删除swap分区的挂载信息。
a. 执行以下命令,查询fstab分区表。
tail -n 3 /etc/fstab
[root@sluo-ecs-a611 ~]# tail -n 3 /etc/fstab
UUID=7c4fce5d-f8f7-4ed6-8463-f2bd22d0ddea / ext4 defaults 1 1
UUID=5de3cf2c-30c6-4fb2-9e63-830439d4e674 swap swap defaults 0 0
b. 执行以下命令,编辑fstab分区表,并删除swap分区的挂载信息。
vi /etc/fstab
tail -n 3 /etc/fstab
[root@sluo-ecs-a611 ~]# vi /etc/fstab
[root@sluo-ecs-a611 ~]# tail -n 3 /etc/fstab
UUID=7c4fce5d-f8f7-4ed6-8463-f2bd22d0ddea / ext4 defaults 1 1
- 执行以下命令,关闭swap。
swapoff -a
- 删除swap分区。
a. 执行以下命令,查看分区。
parted /dev/xvda
[root@sluo-ecs-a611 ~]# parted /dev/xvda
GNU Parted 3.1
Using /dev/xvda
Welcome to GNU Parted! Type ´help´ to view a list of commands.
(parted) help
align-check TYPE N check partition N for TYPE(min|opt) alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a
particular partition
quit exit program
rescue START END rescue a lost partition near START and END
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
(parted)
b. 按“p”。
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 41.0GB 40.9GB primary ext4 boot
2 41.0GB 42.9GB 2000MB primary linux-swap(v1)
c. 执行以下命令,删除swap分区。
rm 2
(parted) rm2
d. 按“p”。
(parted) p
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 41.0GB 40.9GB primary ext4 boot
e. 执行以下命令,退出fstab分区表。
quit
(parted) quitInformation: You may need to update /etc/fstab.
- 执行以下命令,查询删除swap分区后的分区情况。
parted -l /dev/xvda
[root@sluo-ecs-a611 ~]# parted -l /dev/xvda
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 41.0GB 40.9GB primary ext4 boot
- 执行以下命令,安装growpart工具。
工具growpart可能集成在cloud-utils-growpart/cloud-utils/cloud-initramfs-tools/cloud-init包里,可以直接执行命令yum install cloud-*确保growpart命令可用即可。
yum install cloud-utils-growpart
- 执行以下命令,使用工具growpart将第一分区的root分区进行扩容。
growpart /dev/xvda 1
[root@sluo-ecs-a611 ~]# growpart /dev/xvda 1
CHANGED: partition=1 start=2048 old: size=79978496 end=79980544 new: size=209710462,end=209712510
- 执行以下命令,检查在线扩容是否成功。
[root@sluo-ecs-a611 ~]# parted -l /dev/xvda
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 107GB 107GB primary ext4 boot
- 执行以下命令,扩容文件系统。
resize2fs -f $分区名
假设分区名为/dev/xvda1,则命令如下:
[root@sluo-ecs-a611 ~]# resize2fs -f /dev/xvda1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 3
....
[root@sluo-ecs-a611 ~] # df -hT //检查文件系统扩容情况