Kali虚拟机的扩容经历

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,我们打开虚拟机看看去。

vmvare拓展

打开后,进入文件管理去查看磁盘信息,

看来直接点这个扩展后,并没有直接加到虚拟机的镜像存储中。貌似是没有分配。

那我们添加的容量到底去哪了???

查了一下,发现不是没有加上去,而是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分区就建立完成了。

0x05 参考资料

  1. mkfs命令

  2. fdisk命令

  3. 【Linux】虚拟机中Linux扩容

  4. 安装Linux系统至少需要两个分区,一个是系统挂载点“/”根目录分区,一个“SWAP“交换分区!

  5. kali虚拟机根目录扩容

  6. 查看修改swap空间大小

原文地址:https://www.cnblogs.com/delongzhang/p/12380943.html