Software & AppsOperating SystemLinux

How To Run Zerofree on Ubuntu 18.04 in VirtualBox: Troubleshooting “Device is Mounted in RW” Error

Ubuntu 10

Running zerofree on Ubuntu 18.04 in a VirtualBox environment can sometimes be a challenging task, especially when you encounter the “Device is Mounted in RW” error. This article provides an in-depth guide on how to troubleshoot and resolve this error.

Quick Answer

To troubleshoot the "Device is Mounted in RW" error when running Zerofree on Ubuntu 18.04 in VirtualBox, you need to stop active processes, disable swap partitions, remount the partition as read-only, and then run Zerofree. Finally, you can reclaim space on the host OS by compacting the VDI file.

Understanding the “Device is Mounted in RW” Error

Before we dive into the solution, it’s important to understand what this error means. The “Device is Mounted in RW” error typically occurs when you attempt to run zerofree on a mounted partition. It indicates that the partition is currently mounted in Read-Write (RW) mode, which prevents zerofree from operating correctly. To run zerofree, the partition needs to be remounted in Read-Only (RO) mode.

Step-by-Step Guide to Troubleshooting

Step 1: Stop Active Processes

The first step in resolving this error is to stop any processes that are writing to the disk. The systemd-journald service and socket are common culprits. You can stop them using the following commands:

systemctl stop systemd-journald.socket
systemctl stop systemd-journald.service

The systemctl stop command stops a running service or socket. The systemd-journald.socket and systemd-journald.service parameters specify the services to stop.

Step 2: Disable Swap Partitions

Next, check if any swap partitions are enabled. Swap partitions are used to extend the available memory on your system by writing part of the RAM to a disk file. You can check for enabled swap partitions with the command:

swapon -s

The swapon -s command lists all enabled swap partitions. If any swap partitions are listed, disable them with the command:

sudo swapoff -a

The sudo swapoff -a command disables all active swap partitions.

Step 3: Remount the Partition

Now, you need to remount the partition as read-only. To do this, use the following command, replacing /dev/sda1 with the appropriate partition for your system:

mount -n -o remount,ro -t ext2 /dev/sda1 /

The mount command is used to mount filesystems. The -n option tells the system not to update the /etc/mtab file, which is necessary when remounting the root filesystem. The -o remount,ro option specifies that the filesystem should be remounted as read-only. The -t ext2 option specifies the filesystem type, which is ext2 in this case. Finally, /dev/sda1 / specifies the device and mount point.

Step 4: Run Zerofree

Finally, you can run zerofree on the partition:

zerofree -v /dev/sda1

The zerofree -v /dev/sda1 command runs zerofree on the specified partition. The -v option enables verbose output, which will display more information about what zerofree is doing.

After zerofree has finished running, you can shutdown Ubuntu with the command halt.

Reclaiming Space on the Host OS

To reclaim the space on the host OS (in this case, Windows 10), you can compact the VDI file using the following command:

VBoxManage.exe modifymedium disk "C:\path\to\disk.vdi" --compact

The VBoxManage.exe modifymedium disk command modifies the properties of a virtual disk. The --compact option tells VirtualBox to compact the disk, reducing its size.

Final Thoughts

If you encounter any issues, such as the mount point being busy, try stopping additional services and sockets that may be writing to the disk. You can use the commands systemctl --type=service and systemctl --type=socket to identify and stop these services and sockets.

If you are unable to remount the partition as read-only, you may need to boot from an alternate device, such as a bootable USB or ISO image, or use a live USB/CD to run zerofree on the partition.

By following these steps, you should be able to successfully run zerofree on Ubuntu 18.04 in VirtualBox and resolve the “Device is Mounted in RW” error.

What is `zerofree`?

zerofree is a utility that allows you to zero out unused blocks on a filesystem, resulting in the reduction of the virtual disk size. It is commonly used in virtualized environments to reclaim disk space.

How can I install `zerofree` on Ubuntu 18.04?

To install zerofree on Ubuntu 18.04, you can use the following command:

sudo apt-get install zerofree
Can `zerofree` be used on other Linux distributions?

Yes, zerofree can be used on other Linux distributions as well. You can install it using the package manager specific to your distribution. For example, on CentOS/RHEL, you can use yum or dnf to install zerofree.

Can I run `zerofree` on a partition that is currently in use?

No, you cannot run zerofree on a partition that is currently in use. The partition needs to be remounted in read-only mode before running zerofree. Please follow the troubleshooting steps provided in this guide to remount the partition correctly.

How long does it take to run `zerofree`?

The time it takes to run zerofree depends on the size of the partition and the amount of unused space. It can take anywhere from a few minutes to several hours. The verbose output from zerofree will give you a better idea of the progress and estimated time remaining.

Can I use `zerofree` on a physical machine?

Yes, zerofree can be used on a physical machine as well. However, it is important to note that zerofree is typically used in virtualized environments to reclaim disk space. If you are using zerofree on a physical machine, make sure to back up your data and proceed with caution.

Is it necessary to compact the VDI file after running `zerofree`?

Compacting the VDI file is not necessary after running zerofree, but it can help reclaim disk space on the host operating system. The --compact option in VBoxManage reduces the size of the VDI file by removing unused blocks. It is recommended to compact the VDI file after running zerofree to optimize disk usage.

Leave a Comment

Your email address will not be published. Required fields are marked *