实验环境:
虚拟机软件:VMware 10.0.4
系统版本: CentOS 6.9
工具安装
安装:growpart
安装命令:
yum install -y cloud-utils-growpart
注
可能出现:
No package cloud-initramfs-growroot available.的情况
需要先安装yum的扩展(如果成功安装则忽略)
yum install -y epel-release
安装完成后再安装growpart
输入growpart -h
出现如下表示安装成功
[root@localhost ~]# growpart -h
growpart disk partition
rewrite partition table so that partition takes up all the space it can
options:
-h | --help print Usage and exit
--fudge F if part could be resized, but change would be
less than 'F', do not resize (default: 20480)
-N | --dry-run only report what would be done, show new 'sfdisk -d'
-v | --verbose increase verbosity / debug
-u | --update R update the the kernel partition table info after growing
this requires kernel support and 'partx --update'
R is one of:
- 'auto' : [default] update partition if possible
- 'force' : try despite sanity checks (fail on failure)
- 'off' : do not attempt
- 'on' : fail if sanity checks indicate no support
Example:
- growpart /dev/sda 1
Resize partition 1 on /dev/sda
扩容分区
步骤1:
输入命令:
df -Thl
查看当前硬盘使用情况
[root@localhost ~]# df -hTl
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 38G 6.2G 29G 18% /
/dev/sda1 ext4 283M 67M 201M 25% /boot
tmpfs tmpfs 494M 72K 494M 1% /dev/shm
从上面输出的内容可以得到几个信息:
1.根目录(/)是在/dev/sda2上
2./dev/sda2 格式为ext4
3./dev/sda2当前容量为38G
步骤2:
输入命令:
fdisk -l
[root@localhost ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003d8f4
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 4969 39603200 83 Linux
/dev/sda3 4969 5222 2031616 82 Linux swap / Solaris
由上面的内容可以得到以下信息:
1.查看物理容量为,53.7GB,原本的物理容量为40+GB,提升虚拟机物理容量后可以正常识别出扩容后的物理容量
2.可以看到存在一个 /dev/sda3 标识为swap的空间,这是由于打开了swap功能(类似于win系统上的虚拟内容)
3.由步骤1可知根目录(/)在dev/sda2 上,当前还可以看到有sda3的存在,sda2 不是在最末尾分区
步骤3:
删除swap占用容量
输入命令:
vim /etc/fstab
注释 swap设备:
UUID=bfcb57bb-a94b-44ee-b5b4-2505d49b5a40 / ext4 defaults 1 1
UUID=1e402b3b-48a6-4e35-bd55-5b33ebd476e7 /boot ext4 defaults 1 2
#UUID=216bef93-c3ac-4b9c-b819-5de8f6768e53 swap swap defaults 0 0
输入命令:wq保存
输入命令:关闭swap
swapoff -a
输入命令查看硬盘的分区情况
parted /dev/sda
返回:
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
输入p
(parted) p
返回
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 316MB 315MB primary ext4 boot
2 316MB 40.9GB 40.6GB primary ext4
3 40.9GB 42.9GB 2080MB primary linux-swap(v1)
可以看到swap占用的分区是3,移除sda3占用,所以输入rm 3
(parted) rm 3
返回
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it
may not reflect all of your changes until after reboot.
输入quit退出
(parted) quit
输入 fdisk -l
[root@localhost ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003d8f4
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 4969 39603200 83 Linux
可以看到已经不存在/dev/sda3这个分区
步骤4:
输入命令
growpart /dev/sda 2
出现
[root@localhost ~]# growpart /dev/sda 2
CHANGED: partition=2 start=616448 old: size=79206400 end=79822848 new: size=104239807,end=104856255
表示分区扩充容量成功
如果出现表示当前扩展这个分区不是在分区尾部,所以无法将剩余硬盘容量划分到当前设备上
The filesystem is already XXXXXXXXX blocks long. Nothing to do!
成功划分容量后,这边需要对CentOS重启一下
输入命令:
reboot
步骤5:
使用resize2fs工具扩容文件系统
resize2fs /dev/sda2
返回如下结果表示扩容成功
[root@localhost ~]# resize2fs /dev/sda2
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old desc_blocks = 3, new_desc_blocks = 4
Performing an on-line resize of /dev/sda2 to 13029975 (4k) blocks.
The filesystem on /dev/sda2 is now 13029975 blocks long.
重新用硬盘容量查看工具
df -Thl
得到如下结果
[root@localhost ~]# df -Thl
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 49G 6.3G 41G 14% /
tmpfs tmpfs 494M 72K 494M 1% /dev/shm
/dev/sda1 ext4 283M 67M 201M 25% /boot
可以看到设备 /dev/sda2已经成功扩容成功
至此CentOS扩容分区操作就结束了