Recently because I need to use virtualbox
Found some problems

If you install some packages in virtualbox

For example, install pytorch or some packages

Will need to download many files and generate temporary files

Maybe after deleting those temporary files

The total space used is 15G

But the vdi file outside may have 25G or even 30G

Because this file will basically only increase and not decrease.

But in this case

It is very troublesome to transfer the virtualbox system

Because it’s impossible to prepare such a big flash drive every time

Sometimes even the flash drive cannot be used

So it is necessary to slim down the vdi file

The way to find it online is

"For Linux, please use the Zerofree utility which supports ext2/ext3 file systems"

 

Please refer to the usage method

Install zerofree first

 

sudo apt-get install zerofree

 

Press ESC when virtualbox starts to enter Recovery Mode

and then execute

 

zerofree /dev/sda1

 

You may encounter many errors here

For example

 

zerofree failed to open filesystem sda1

or

filesystem dev/sda1 is mounted rw

The first thing to note is

sda1 is not absolute

You must use df to see which hard disk file is currently mounted with the directory.

For example, my virtualbox mounts the lv file

So the instruction I want to execute is

zerofree /dev/mapper/ubuntu–vg-ubuntu–lv

after completion

Exit virtualbox

Then go to the virtualbox installation folder and open the terminal

implement

VBoxManage modifyhd "D:\VirtualBox VMs\ubuntu22server\ubuntu22server.vdi" --compact

You should be able to see that the file has shrunk.

You can see from the picture

The following is the size of my original backup vdi file

There are about 20G + 10G

After scaling down it can reach 11G + 7G

Very different

So if you want to use virtualbox for system display or application

It's best to learn this trick

For your reference

~~