在 VMware Player 7 里面安装了 CentOS 7,原先分配硬盘空间 20G,现不足,需扩展到 50G。
步骤分两步:在 VMware 中扩大硬盘配额,让 CentOS 识别新增硬盘空间。整个过程中,原有数据不会丢失。
别害怕,步骤很简单,文章这么长是因为我把所有输出信息都贴上来了,真正要你输入的命令的长度,还不如这句话长呢。
1 . VMware 中扩大硬盘配额
这个比较简单了,不必多说,见下图:
2 . 让 CentOS 识别新增硬盘空间
原先的分区是这样的,命令是fdisk -l
:
shell# fdisk -l
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: 0x00026f4b
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 4810751 2097152 82 Linux swap / Solaris
/dev/sda3 4810752 41943039 18566144 83 Linux
/dev/sda 上已经有 50GB 空间了,可是底下的分区表(这里关注的是第三行, /dev/sda3,因为挂载着根目录)里还不识别,需要进行分区,命令是fdisk /dev/sda
:
shell# 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.
把命令菜单输出出来,作为后续的参考,命令是m
:
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
把 /dev/sda3 分区删掉,命令依次是(注意根据实际情况修改)d, 3
:
Command (m for help): d
Partition number (1-3, default 3): 3
Partition 3 is deleted
新建分区,新分区的大小是扩展后的大小,命令依次是(注意根据实际情况修改)n, p, 3
:
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (4810752-104857599, default 4810752):
Using default value 4810752
Last sector, +sectors or +size{K,M,G} (4810752-104857599, default 104857599):
Using default value 104857599
Partition 3 of type Linux and of size 47.7 GiB is set
然后,将以上修改写入磁盘,命令是w
:
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.
重启系统。
。
。
。
重启以后,发现分区表已经改过来了,可是系统的可用空间还是那么大:
shell# fdisk -l
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: 0x00026f4b
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 4810751 2097152 82 Linux swap / Solaris
/dev/sda3 4810752 104857599 50023424 83 Linux
这时需要输入一条命令,让内核知道可用空间变大了:
shell# xfs_growfs /
meta-data=/dev/sda3 isize=256 agcount=4, agsize=1160384 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=4641536, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
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 4641536 to 12505856
现在,系统已经可以使用新增后的空间了。
题外话:CentOS 7 的默认文件系统是 xfs,所以这里用了xfs_growfs 命令。如果是别的文件系统,需要查询一下,比如 ext4 对应的命令应该是 resize2fs。可参考 redHat 的这篇文档。还有就是:
XFS 文件系统的大小不能减小。xfs_growfs 命令只能用于增大文件系统大小。