最近編譯伺服器磁盤空間滿了,導緻無法編譯,由于是搭建的虛拟伺服器,是以通過以下步驟對磁盤空間進行擴容。
為虛拟伺服器添加硬碟空間
由原來的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檔案增加該環境變量,讓其支援。