Kali虛擬機的擴容經歷
0x01 起因
更新了一下軟件包,竟然提示我空間不足。

升級了 687 個軟件包,新安裝了 82 個軟件包,要卸載 0 個軟件包,有 8 個軟件包未被升級。
需要下載 1,578 MB/1,581 MB 的歸檔。
解壓縮后會消耗 559 MB 的額外空間。
E: 您在 /var/cache/apt/archives/ 上沒有足夠的可用空間。
磁盤信息如下:
root@dillon:/# df -h
文件系統 容量 已用 可用 已用% 掛載點
udev 1.9G 0 1.9G 0% /dev
tmpfs 392M 12M 380M 4% /run
/dev/sda1 16G 15G 795M 95% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
tmpfs 392M 16K 392M 1% /run/user/132
tmpfs 392M 40K 392M 1% /run/user/0
tmpfs 392M 0 392M 0% /run/user/1000
於是就想着,擴容一下。
0x02 操作過程
擴充磁盤容量
我以為在虛擬機關機狀態下進入虛擬機設置,直接點擴展即可,可事實卻非如此。對此我只能說聲呵呵🙂。

擴到128G,我們打開虛擬機看看去。

打開后,進入文件管理去查看磁盤信息,

看來直接點這個擴展后,並沒有直接加到虛擬機的鏡像存儲中。貌似是沒有分配。
那我們添加的容量到底去哪了???
查了一下,發現不是沒有加上去,而是Linux沒有識別。
雖然已經擴大了磁盤,但是由於還沒有經過分區,指定文件系統,所以Linux操作系統無法識別。
分區
那么我們該進行分區操作了。
輸入
fdisk /dev/sda
進入
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):
輸入p
顯示當前的分區狀況
Command (m for help): p
Disk /dev/sda: 128 GiB, 137438953472 bytes, 268435456 sectors
Disk model: VMware Virtual S
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: 0xf7e82e2b
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 33554431 33552384 16G 83 Linux
/dev/sda2 33556478 41940991 8384514 4G 5 Extended
/dev/sda5 33556480 41940991 8384512 4G 82 Linux swap / Solaris
輸入F
查看可用的未分區空間
Unpartitioned space /dev/sda: 108 GiB, 115965165568 bytes, 226494464 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Start End Sectors Size
41940992 268435455 226494464 108G
輸入n
添加一個新分區
Command (m for help): n
Partition type
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p):
選擇中輸入p
創建主分區
其他操作默認
Command (m for help): n
Partition type
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): p
Partition number (3,4, default 3):
First sector (41940992-268435455, default 41940992):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (41940992-268435455, default 268435455):
Created a new partition 3 of type 'Linux' and of size 108 GiB.
輸入w保存該分區
Command (m for help): w
The partition table has been altered.
Syncing disks.
格式化新增的分區
通過上面的操作,我們已經划分了新的分區
evice Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 33554431 33552384 16G 83 Linux
/dev/sda2 33556478 41940991 8384514 4G 5 Extended
/dev/sda3 41940992 268435455 226494464 108G 83 Linux
/dev/sda5 33556480 41940991 8384512 4G 82 Linux swap / Solaris
Partition table entries are not in disk order.
我們要把它格式化為ext4文件系統格式,供數據的存儲與讀取。
mkfs -t ext4 /dev/sda3
掛載
mount /dev/sda3 /home/mydisk
在/etc/fstab
中添加 即可開機自動掛載。
/dev/sda3 /home/mydisk ext4 defaults 0 0
至此,我們擴容完畢。
0x03 原來有磁盤管理軟件🌚
那么我們就不必用命令了。
擴充磁盤容量后,打開系統。
打開磁盤管理軟件。
具體界面如下,

在這里面,我們就可以進行格式化,掛載等等操作。
0x04 擴容根目錄
經過以上操作我們,可以發現掛載分區后根目錄的容量卻沒有增加,而是相當於新加了一塊磁盤。
而要增加系統根目錄該如何處理呢?
下面是我的操作方式。
刪除Swap分區和拓展分區
這樣就剩下根目錄了。就可以增加根目錄的容量了。
增加根目錄容量
剩下一部分來做交換分區。
新建Swap分區
fdisk /dev/sda
輸入p
查看磁盤信息
Command (m for help): p
Disk /dev/sda: 128 GiB, 137438953472 bytes, 268435456 sectors
Disk model: VMware Virtual S
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: 0xf7e82e2b
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 236331007 236328960 112.7G 83 Linux
輸入n
新建一個分區
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):
選擇e
,創建一個邏輯分區。
其他默認即可。
輸入t
,修改分區id為swap分區默認的82
Hex code (type L to list all codes): 82
Changed type of partition 'Extended' to 'Linux swap / Solaris'.
輸入w重寫分區表。
同步內存和分區表信息
partprobe
格式化為swap分區
mkswap /dev/sda2
root@dillon:~# mkswap /dev/sda2
mkswap: /dev/sda2: warning: don't erase bootbits sectors
(dos partition table detected). Use -f to force.
Setting up swapspace version 1, size = 15.3 GiB (16437473280 bytes)
no label, UUID=0e45c708-e064-48a7-be28-fe748987e02a
打開swap分區
swapon /dev/sda2
添加開機自動掛載swap分區
vim /etc/fstab
在fstab中添加下面的一條記錄
/dev/sda2 swap swap defaults 0 0
掛載分區
mount -a
這時swap分區就建立完成了。
