最近编译服务器磁盘空间满了,导致无法编译,由于是搭建的虚拟服务器,所以通过以下步骤对磁盘空间进行扩容。
为虚拟服务器添加硬盘空间
由原来的100G增大到300G

重新划分分区
root@debian:/home/tester# fdisk -l
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0ab29950
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 207716349 207714302 99G 83 Linux
/dev/sda2 207716350 209713151 1996802 975M 5 Extended
/dev/sda5 207716352 209713151 1996800 975M 82 Linux swap / Solaris
可以看到sda分区下有300G的空间,但sda1、sda2、sda3下面总共加起来才100G,说明已经添加成功,但并没有分配到现有分区。
通过fdisk命令对分区进行重新划分
root@debian:/home/tester# fdisk /dev/sda
Welcome to fdisk (util-linux 2.33.1).
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,5, default 5): 1 #删除的分区序号
Partition 1 has been deleted.
Command (m for help): d #删除现有分区
Partition number (2,5, default 5): 2 #删除的分区序号
Partition 2 has been deleted.
Command (m for help): d #删除现有分区
No partition is defined yet!
Command (m for help): 5 #删除的分区序号
5: unknown command
Command (m for help): F # 查看分区的块序号
Unpartitioned space /dev/sda: 300 GiB, 322121498624 bytes, 629143552 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Start End Sectors Size
2048 629145599 629143552 300G
Command (m for help): n # 重新创建分区
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): #默认P,可不填内容
Using default response p.
Partition number (1-4, default 1): #默认1,可不填
First sector (2048-629145599, default 2048): #默认最早的可分配区块,可不填
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-629145599, default 629145599): 627145599 # 需要填写,另外需要留大概1G的空间给sda2分区
Created a new partition 1 of type 'Linux' and of size 299 GiB.
Partition #1 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: y # 确认通过
The signature will be removed by a write command.
Command (m for help): n # 创建sda2分区
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (2-4, default 2): 2
First sector (627145600-629145599, default 627146752):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (627146752-629145599, default 629145599):
Created a new partition 2 of type 'Linux' and of size 976 MiB.
Command (m for help): w # 将上述修改写入磁盘
The partition table has been altered.
Failed to remove partition 5 from system: 设备或资源忙
Failed to update system information about partition 1: 设备或资源忙
The kernel still uses the old partitions. The new table will be used at the next reboot.
Syncing disks.
让新的分区生效
root@debian:/home/tester# fdisk -l
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0ab29950
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 627145599 627143552 299G 83 Linux
/dev/sda2 627146752 629145599 1998848 976M 83 Linux
可以看到,经过上一步后sda1已经可以看到有299G,但df查看sda1还是只有99G,说明还没有生效。此时就需要查看磁盘的类型是什么决定是使用resize2fs命令还是xfs_growfs让其生效。
root@debian:/home/tester# df -PTh
文件系统 类型 容量 已用 可用 已用% 挂载点
udev devtmpfs 7.9G 0 7.9G 0% /dev
tmpfs tmpfs 1.6G 8.7M 1.6G 1% /run
/dev/sda1 ext4 97G 72G 21G 79% /
tmpfs tmpfs 7.9G 0 7.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup
tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/
可以看出/dev/sda1是ext4格式,那就需要使用resize2fs命令,如果是xfs格式则需要使用xfs_growfs命令
root@debian:/home/tester# resize2fs /dev/sda1
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 13, new_desc_blocks = 38
The filesystem on /dev/sda1 is now 78392944 (4k) blocks long.
成功后,再使用df命令查看,就可以看到分配的/dev/sda1的空间已经分配成功,总空间已经达到300G
root@debian:/home/tester# df
文件系统 1K-块 已用 可用 已用% 挂载点
udev 8195812 0 8195812 0% /dev
tmpfs 1642484 8852 1633632 1% /run
/dev/sda1 308124984 75618208 218912528 26% /
tmpfs 8212408 0 8212408 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 8212408 0 8212408 0% /sys/fs/cgroup
tmpfs 1642480 0 1642480 0% /run/user/1000
常见问题
fdisk/resize2fs等命令有可能在你的系统中找不到,程序默认应该都是存在的,只是存放的路径未添加到环境变量中。
可以通过whereis + 命令 查看命令是否存在,和存在什么路径下。
然后通过echo $PATH命令确认上述的路径是否在存在,如果不存在可以通过修改/etc/profile文件增加该环境变量,让其支持。