如何扩展Windows10/11下的VirtualBox虚拟机硬盘存储空间(附送虚拟机硬盘空间压缩方法)

虚拟机硬盘空间用着用着就不够了,这是常见问题,但是硬盘空间扩展始终不是个简单的事情,这里大概记录一下操作方法。

基本思路:

1、用virtualbox的管理工具对VDI文件进行扩展

2、用Ubuntu安装盘中的GParted工具对sdaxxx分区进行扩展

详细步骤如下:

1、确保你的virtualbox虚拟机硬盘为vdi格式的,如果不是,需要转成vdi的,具体可参考如下链接,本文的大部分操作也是来自此文:

2、用VBoxManage.exe扩展vdi

指令如下:

E:\zephyr_Ubuntu_22_04>”c:\Program Files\Oracle\VirtualBox\VBoxManage.exe” modifymedium zephyr_Ubuntu_22_04.vdi –resize 20000
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%

E:\zephyr_Ubuntu_22_04>

以上指令将vid文件扩展为20G。

3、从Ubuntu的安装盘启动

具体操作:在虚拟机—>设置—>存储—>控制器:IDE

选定光驱,给光驱指定一个Ubuntu的iso文件:

设置后如下:

启动虚拟机,按F12,出现启动界面后,按“C”,选择从CDROM中启动:

进入后选择试用Ubuntu:

4、用GParted配置硬盘

进入系统后,点击左下角的“Show applications”:

在搜索框中输入gpart:

启动GParted,此时已经能看到被扩展的区域了(unallocated)。

选中/dev/sda3分区,右键弹出菜单,直接选择Resize/Move即可(旧版程序还有Deactivate,新版没了,不需要此操作)

拖动滑块,指定要扩展的大小,一般是拉满:

搞定后,右键,点一下Check:

最后,一定要点一下apply,就是那个绿色的小按钮:

重启虚拟机,完成扩展。

很多情况下,我们希望能将一个虚拟机镜像拷贝到别的PC上,实现虚拟机迁移,这篇文章也提供了一个压缩磁盘的方法:

Distribution

If you want to distribute an export of your VM and want to have it as small as possible, the following might help:

In the Ubuntu guest OS install and run BleachBit to remove temporary files. You can compare this tool with CCleaner for Windows.

sudo telinit 1
mount -o remount,ro /dev/sda1
zerofree -v /dev/sda1

Update /etc/fstab and set the options of /dev/mapper/ubuntu–vg-root to ro but remember the previous options! In my case before the change it was errors=remount-ro.

Reboot

As root: telinit 1
zerofree /dev/mapper/ubuntu–vg-root
mount -o remount,rw /dev/mapper/ubuntu-vg-root

Update fstab with vi and restore the previously set options. Restart. Now the free space has been zeroed out. On your host OS (Windows in my case) do the following:

“c:\Program Files\Oracle\VirtualBox\VBoxManage.exe” modifymedium –compact kafkaworkshoposajava-disk1.vdi

This makes sure if you export the VDI, the exported disks will be as small as they can get with their contents. You can make the export even smaller by using 7-zip to compress the result. If you want good compression, set the dictionary size and block size in 7-zip to large values.

Have fun!

发表回复

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据