I'm using Ubuntu 18.04 for development on VM VirtualBox on windows 10 pro host I've compacted the virtual hard drive previously by following the steps from here:
apart from the usual updates - I have not messed around with it, (recently just had installed the update for virtualbox gues additions)
previously, I only had one /dev/sda1 showing up - today I had three and when I attempt to run zerofree on any of those, I get that the device is mounted in rw
I have followed some instructions here as well to stop some services and try to remount- but it didnt work
it still says mount point is busy
I'm unsure if this happened because of a recent update ? I also tested by uninstalling the vmware guest additions - but it didn't work
Suggestions ?
210 Answers
What worked for me (combining answers from several related questions and forums):
- VirtualBox 5.2.10
- Windows 10 as Host OS
- Ubuntu 18.04 as Guest OS
First install zerofree:
sudo apt-get install zerofreeCreate 0s to get space to be reclaimed(delete all files you don't need as well as empty the trash)
Reboot to GRUB menu (Press Esc as Ubuntu is booting up)
Advanced options for Ubuntu > Recovery Mode (choose highest version number)From the Recovery Menu select Drop to root shell prompt
df to get your partitions(to see the partition you want to shrink e.g. sda1, sda 4 etc. usually its /dev/sda1)
dfTry to run zerofree
zerofree /dev/sda1Partition is read-write, needs to be mounted as read-only for zerofree to run Error:
If you get an error from zerofree like /dev/sda1 is mounted as rw, then you need to mount it as read-only to continue.
To mount the /dev/sda1 or your desired partition as read-only to use zerofree on it:
Note 1: you should perform the following steps relatively quick to be able to mount the drive as ro before services and sockets go back into use and start writing to the disk again. Otherwise you may need to perform the systemctl step again to make sure writing to the disk has stopped.
Note 2: (Skip reading this until the steps following this fail).Depending on your system, you may have other services and sockets still writing to disk other than systemd-journald. If the below steps are not working try to see whats still writing to disk after you run the steps and then stop them too in the same way.
systemctl --type=service
systemctl --type=socketSteps:
Update: User @Kevin gave an excellent suggestion to execute the steps ( see below) in a one liner command in case the services and sockets go back into use too fast:
systemctl stop systemd-journal* && sudo swapoff -a && mount -n -o remount,ro -t ext2 /dev/sda1 / && zerofree -v /dev/sda1First, stop all processes writing to the disk so you don't get mount /: mount busy error
systemctl stop systemd-journald.socket
systemctl stop systemd-journald.serviceCheck if any swap partitions are enabled:
swapon -sIf enabled, then disable them
sudo swapoff -aThen finally you should be able to mount dev/sda1 as read-only. (Your filesystem type might be different from ext2. To find the filesystem, run df -T)
mount -n -o remount,ro -t ext2 /dev/sda1 /Then finally run zerofree
zerofree -v /dev/sda1Whens zerofree is done, shutdown Ubuntu
haltThen finally, on the Host OS (in my case Windows 10), compact the vdi to reclaim space:
VBoxManage.exe modifymedium disk "C:\path\to\disk.vdi" --compact 8 I used Emmanuels' solution and it only worked after stopping two more sockets:
systemctl stop systemd-journald.socket new lines:
systemctl stop systemd-journald-dev-log.socket
systemctl stop systemd-journald-audit.socketsystemctl stop systemd-journald.service
sudo swapoff -a
mount -n -o remount,ro -t ext4 /dev/sda1 /
zerofree /dev/sda1Note: I use Ubuntu 18.04.3 and EXT4
Shrunk my disk from 40 GB to 7 GB. Thanks. :)
3install zerofree
sudo apt install zerofree
reboot into ubuntu recovery console, hold down [RIGHT SHIFT]
advanced options -> recovery mode -> root console
find the root directory
mount | grep "sda"
run zerofree
echo "u" > /proc/sysrq-trigger
mount /dev/mapper / -o remount,ro
zerofree -v /dev/sda1reboot
shutdown -r now
compress the drive
VBoxManage modifyhd /path/to/VDI/VM.vdi --compact
source: cyplex from ubuntuforums.org
3As mentioned in one of the comments, you have three partitions mounted (sda1, sda2 and sda3) and several temporary filesystems used for other purposes.
The manual for zerofree indicates that if you want to run this on a filesystem, then that filesystem must be either mounted readonly, or unmounted. When you are running Ubuntu, it is certain that the root partition /, which is /dev/sda1 on your computer, is mounted thus the command will give a failure.
If you want to run zerofree on /dev/sda1 you will need to either boot from an alternate device, uch as a bootable USB or ISO image, or perform a chroot to an alternate linux image. The link that you reference has specific instructions for you, on how to run this program on your root partition. Please re-read their instructions on how to use this program in Linux:
- Boot and access Grub before the OS loads
- Choose 'Advanced Options' from the grub menu
- Enter a recovery mode session
- use the 'root' login
- Identify the disk
- Run
zerofree - halt the machine
- powerdown and restart the VM
2019-01-22
The current version Ubuntu makes running zerofree difficult from the recovery session - in this case, it is easier to run this program from a live USB:
- Download a current Ubuntu iso file
- Create a live USB / CD with the Ubuntu iso
- Boot your device from the live media (this can also be done in VM's)
- Install the program
zerofree, if it is not already installed, with the commandsudo apt install zerofree - Run the command
- Reboot the machine
I'd add a comment to Charles Green answer but I don't have enough reputation.
When running zerofree command, you may get an error saying that the /dev/sda1 partition is mounted as read-write. In this case you have to remount such partiton as read-only:
mount -n -o remount,ro -t ext2 /dev/sda1 /and re-run the zerofree command, as described here
I used Emmanuel's solution and it worked. But for me the service and socket go back too quickly as he mentioned, so after failing a few times I decided to use a chained command to do all the steps in 1 line:
systemctl stop systemd-journald.socket && systemctl stop systemd-journald.service && sudo swapoff -a && mount -n -o remount,ro -t ext2 /dev/sda1 / && zerofree /dev/sda1And it worked finally.
Here's how I did it on Ubuntu 18.04 Server guest
- Put Ubuntu 18.04 Desktop Live CD in VM's optical drive (make sure boot sequence loads this first)
- Boot VM, when CD asks: click
Try Ubuntu - Once GUI is up, run terminal
sudo apt install zerofreeand let it installsudo fdisk -land note system HD e.g./dev/sda2sudo zerofree -v /dev/sda2and let it finishhalt- ACPI shutdown VM
- Remove Ubuntu 18.04 Desktop Live CD from VM's optical drive before restarting
I succeeded in shrinking VBox VM Image size by using dd instead of zerofree.
- Normal log into Ubuntu (my VM is elementary OS 5.1)
- Run
sudo dd if=/dev/zero of=/EMPTY bs=1M - Run
sudo rm -f /EMPTY - Power off:
sudo poweroff - In host OS (windows 10) run
VBoxManage.exe modifyhd vbox.vdi --compact.
My VM file is shrinked form 8GB to 6GB. NOTE: It is okay to ignore some warning outputs.
The following is full example of my input/output:
In VM:
erich@vbox:~$ sudo dd if=/dev/zero of=/EMPTY bs=1M status=progress
[sudo] password for erich:
97889812480 bytes (98 GB, 91 GiB) copied, 415 s, 236 MB/s
dd: error writing '/EMPTY': No space left on device
93399+0 records in
93398+0 records out
97934905344 bytes (98 GB, 91 GiB) copied, 415.592 s, 236 MB/s
erich@vbox:~$ sudo rm -f /EMPTY
bash: cannot create temp file for here-document: No space left on deviceIn host OS:
cd <C:/path/to/folder/of/vbox.vdi>
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd eOS.vdi --compact
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% 1 I am using Ubuntu 18.04 server VMs accessed remotely via phpvirtualbox. There seemed to be a lot of services running.
I tried the various tips mentioned above, but the method quoted by Teoman was the only way I could get the drive mounted ro, and so was able to run zerofree.
(As an aside, there's no point doing this if your VM is encrypted, as compacting the VM cannot work when its encrypted. Which mine are.)
This command worked for me. It's better if you add -v on zerofree because the terminal will otherwise look like it's not responding when executing the command. Add -v will display the percentage of the status. The OS is Ubuntu 18.04.4.
$ systemctl stop systemd-journald.socket && systemctl stop systemd-journald.service && sudo swapoff -a && mount -n -o remount,ro -t ext4 /dev/sda1 / && zerofree -v /dev/sda1And after I finished the command below, I free the VDI image size from 160G to 25G.
VBoxManage.exe modifymedium disk "C:\path\to\disk.vdi" --compact