Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


Shrinking VirtualBox Linux images with ZeroFree

Stanislav KhromovStanislav Khromov

VirtualBox sparse VDI images always take up as much space as they have maximally had utilized at some point in time. To fix this, we need to fill the empty space on the vdi with zeros and resize it:

Download the SystemRescueCd Live Image.

Mount it as a CD on your virtual machine and boot into the live distro.

Open up a Root Terminal.

Find all available partition and LVM (volume groups) with:

fdisk -l 

After we find our partition run zerofree to zero out the free space. In our example we will run it on the LVM root partition.

zerofree -v /dev/mapper/vg_rheldev-lv_root

If you want to try a dry run without modifying the hard-drive, add the -n flag.
For LVM, there might also be a _home partition that needs a zerofree run as well.

Finally, boot down your virtual machine, and run VBoxManage on the host to resize the .vdi file.

VBoxManage.exe modifyhd --compact "C:\Users\k\VMs\CentOS Dev.vdi"

(Example is on a Windows host. Linux command should look the same, just omit .exe and add in the correct path.)

Done!

Source

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 11
  • David Roheim
    Posted on

    David Roheim David Roheim

    Reply Author

    Maybe a better / easier way is to take the VM down to runlevel 1, remount the partitions as read-only and then run zerofree && halt. At least that is what I do. :)


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      That does sound easier! Can you write down the commands required to perform this?


      • David Roheim
        Posted on

        David Roheim David Roheim

        Reply Author

        Sure. Quite easy and short. For me it is most of the time 6 lines or so.

        runlevel 1
        mount -n -o remount,ro -t ext4 /dev/mapper/lv_home
        mount -n -o remount,ro -t ext4 /dev/mapper/lv_root
        zerofree -v /dev/mapper/lv_home
        zerofree -v /dev/mapper/lv_root
        halt

        And then this:

        VBoxManage modifyhd my-large-file.vdi –compact

        You can find zerofree for CentOS 6 here in my repo: http://repos.zendbox.com/


  • tony
    Posted on

    tony tony

    Reply Author

    you said:
    “Mount it as a CD on your virtual machine.
    Open up a Root Terminal in Partition Magic.”
    how do you get from mount cd to partition magic? I couldn’t see any folder called partition magic in the mounted cd.
    btw I’m running a centos6 server which is command line only….


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      Sorry for the late reply Tony, when you reboot your machine, let live CD boot instead of your CentOS system, you’ll get a new shell inside the live CD where you can run the commands. There is also a way of doing it that does not need another live distro, check out David Roheims comment above.


  • Nick Robinson
    Posted on

    Nick Robinson Nick Robinson

    Reply Author

    Gparted no longer has zerofree installed…


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      Thanks for the info Nick,

      Quite annoying. I was earlier recommending PartedMagic, that went on to be a paid live CD, now GParted no longer has zerofree, grr.

      I checked out SystemRescueCd real quick and it seems to have all the utilities, so I have changed the link in the post. Let me know if that works out for you!

      You can also try the way David Roheim suggested which does not include third party distros.


  • Peter Ermke
    Posted on

    Peter Ermke Peter Ermke

    Reply Author

    I found a much simpler solution:
    Use: dd if=/dev/zero of=nullfile
    rm nullfile
    Shutdown the VM
    VBoxManage modifyhd –compact “filename.vdi”


    • Mark
      Posted on

      Mark Mark

      Reply Author

      The ‘dd’ approach does work, but fully realizes the dynamic disk before compaction. Your physical drive may not have enough free space left for that. If the physical drive is an SSD this growth also adds unnecessary wear and tear. The appeal of ‘zerofree’ is that it does not grow your virtual disk, and operates only on blocks already used.


      • Peter Ermke
        Posted on

        Peter Ermke Peter Ermke

        Reply Author

        I just tried it again: No, the virtual disk does NOT grow with the ‘dd’ command.