前言: 使用虚拟机创建系统后,/ 盘 想要扩容需要几步才能实现,下面将介绍具体流程
- 确定根分区磁盘以及分区号,和起始扇区和结束扇区
# 查看磁盘名称和分区
# 如下可看出根分区为 /dev/sda2 ,磁盘为sda
[root@192 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
└─sda2 8:2 0 19.8G 0 part /
sr0 11:0 1 4.4G 0 rom
# 查看磁盘分区起始扇区号和 结束扇区号(扩容时,结束扇区号会变大,这里根盘的结束扇区号为:20765696)
[root@192 ~]# fdisk -lDisk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000c795eDevice Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 41943039 20765696 83 Linux
- 扩容磁盘(扩容指定磁盘 这里为sda)
虚机必须关机后才能扩容!
3. 扩容分区
扩容分区需要先删除原来分区,删除原有分区不会丢失数据,真实环境尽量避免业务期扩容。
3.1. 查看磁盘是否被扩容
# 可以大致看出sda磁盘被扩容了20G,扇区个数从原来的41943040 变成了 83886080
[root@192 ~]# fdisk -lDisk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000c795eDevice Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 41943039 20765696 83 Linux
3.2. 删除需要被扩容的分区,并重新添加分区
在删除原有分区时不能,点击
w
保存,不然数据就没了!,正常来说需要卸载需要被扩容的分区,但是由于我需要扩容的是/ 分区,所以不能卸载。
[root@192 ~]# 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.Command (m for help): d # 删除分区
Partition number (1,2, default 2): 2 # 选择删除主分区,就是 / 分区
Partition 2 is deletedCommand (m for help): p # 打印分区Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000c795eDevice Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 LinuxCommand (m for help): n # 添加分区
Partition type:p primary (1 primary, 0 extended, 3 free)e extended
Select (default p): p # 添加主分区
Partition number (2-4, default 2): 2 # 添加原来的分区号
First sector (411648-83886079, default 411648): # 起始扇区号,默认即可
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-83886079, default 83886079): # 结束扇区号,默认即可
Using default value 83886079
Partition 2 of type Linux and of size 39.8 GiB is setCommand (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
3.3. 查看目前 / 分区大小,确认是否被扩容
[root@192 ~]# fdisk -lDisk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000c795eDevice Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 83886079 41737216 83 Linux
可以看出 /dev/sda2 的结束扇区号没有发生变化,这是因为文件系统还没有被扩容。
1) 查看分区的文件系统
# 可以看出 / 分区的文件系统 为 xfs
[root@192 ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 900M 0 900M 0% /dev
tmpfs tmpfs 910M 0 910M 0% /dev/shm
tmpfs tmpfs 910M 9.6M 901M 2% /run
tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda2 xfs 20G 1.3G 19G 7% /
/dev/sda1 xfs 197M 120M 77M 61% /boot
tmpfs tmpfs 182M 0 182M 0% /run/user/0
2) 扩大文件系统(不同的文件系统使用不同的命令进行扩容)
xfs
xfs_growfsext
resize2fs
# 扩大文件系统
[root@192 ~]# xfs_growfs /dev/sda2
meta-data=/dev/sda2 isize=512 agcount=4, agsize=1297856 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5191424, 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
# 查看扇区数,判断是否被扩容成功,/dev/sda2 结束扇区已经变成83886079,成功被扩容成功
[root@192 ~]# fdisk -lDisk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000c795eDevice Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 83886079 41737216 83 Linux
# 查看系统分区容量是否被扩容(这里没有被扩容的原因是需要重新挂载一下分区)
[root@192 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 900M 0 900M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda2 20G 1.3G 19G 7% /
/dev/sda1 197M 120M 77M 61% /boot
tmpfs 182M 0 182M 0% /run/user/0
- 重新挂载分区
由于 / 分区的特殊性,是不可能被重新挂载的,因为重新挂载就必须断开一次,所以这里直接重启
[root@192 ~]# reboot
# / 分区成功被扩容了
[root@192 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 900M 0 900M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.5M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda2 40G 1.3G 39G 4% /
/dev/sda1 197M 120M 77M 61% /boot
tmpfs 182M 0 182M 0% /run/user/0