Recover from a linux boot issue - no free disk space
Tags: linux, tooling
Published: 2022-11-09 13:30:33

The other day, I ended up in an unenviable position. I was running low on disk space on my Linux laptop, I got a warning as expected. I deleted some unwanted files, and emptied the trash. That bought me some free space. 

I ran [BleachBit](https://github.com/bleachbit/bleachbit) to do a system-wide clean to reclaim more. While Bleachbit was running, I was doing quite a bit in parallel. I was processing some raw images, playing a video in the background, downloading a torrent, I had a sprawling browsing session in Firefox, and writing code. Way too much! 

Long story short, I had a suspicion that BleachBit did not finish the cleaning task, but did not know for sure when I had closed windows! I did not notice anything unusual. So, I shutdown my laptop.

Next time, I turned on the laptop, the splash screen for Ubuntu came up, and then it was just a black screen. ⬛

<img src="https://www.roboleary.net/assets/img/blog/2022-11-09-fixing-linux-boot-error-no-free-disk-space/fuck.jpg" alt="ffffuuuucckkkk" width="1000" height="1500" loading="lazy">

I could see and move the cursor, but it would not boot the OS completely.

My guess was that Bleachbit hoarded the free disk space and did not release it. I just needed to troubleshoot to see how I could reverse this scenario. 

Luckily, I had a bootable pen drive with Manjaro on it, so I could boot into another OS quickly and look at the disk to see what state it was in. 🦸

**Spoiler alert**: I fixed the issue. Maybe, this might save your bacon some day, but I hope you never get into this situation!🤞

## Troubleshooting

Once Manjaro was booted, I opened the file explorer to see if the hard disk was mounted.

It was.

Next, I opened a terminal to explore. The `df` command can report on system disk usage. Using it with the `-h` flag gives you a human-readable summary of your disk usage. Here is an abbreviated view of my system (recreated):

```shell
df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            7.5G     0  7.5G   0% /dev
tmpfs           1.6G  2.1M  1.6G   1% /run
/dev/sda5       480G  456G    0M  95% /
...
```

And there you have it, my harddrive - `/dev/sda5` - has no available space!

How the hell do I fix that?

I deleted some files and ran the command again. There still was no available space.

I looked up how to empty the trash bin of a disk on the command-line. Since I am doing this external to the installed operating system, it is not straightforward. I couldn't get it to work.

The issue is likely to be that the space allocated to the files that have been deleted has not been released. How do I trigger that release process?

The most common root cause suggested is that the space is being held by open files, files with a process still attached. In my case, a BleachBit process was killed improperly. Maybe that is the issue.

I looked at this troubleshooting guide - [Space is not released after deleting files in Linux?](https://kerneltalks.com/troubleshooting/space-is-not-released-after-deleting-files-in-linux/) - to see if I could replicate what they did. The first suggestion they made is use the `lsof` command. This requires that you are logged into the operating system, so that was out.

The second suggestion requires flushing the proc filesystem. This did not work for me.

I was stuck.

I called it a day. After a while away from the screen, I remembered that the *Use%* field showed 95% of the disk was being used. What about that 5%? 🤔

## The backdoor 🚪

By default, ext3 and ext4 filesystems on Linux reserve 5% of their capacity for special processes that can continue to run when diskspace is running low. We can change this allocation and reclaim that space!

We can use the `tune2fs` command to adjust filesystem parameters on ext3 and ext4 filesystems. We can use this command to free the reserved space. The `-m` flag allows you to set the reservered blocks percentage. To set it to zero, run the following command:

```shell
tune2fs -m 0 /dev/sda5
```

And bingo, we have a few gigabytes of disk space available!

I restart the laptop and Ubuntu boots. Great! 🌟

However, we still have gigabytes of dead space.

I tried the `lsof` command to see about the open files from deleted processes. We can run the following command to get that list:

```shell
lsof | grep -i deleted
```

I did not see anything that looked like it belonged to BleachBit. Not a clear way to free that space.

Maybe, I can just run BleachBit?

<img src="https://www.roboleary.net/assets/img/blog/2022-11-09-fixing-linux-boot-error-no-free-disk-space/bleachbit-round2.jpg" width="1200" height="675" loading="lazy">

I did a data backup and gave it a go.

And it finished. I checked the disk usage, andddddd I got the reported 30.55G of free space back! Halleujah! 🙌

We are done yet though! Don't forget to restore that reserved system space. I decided to reduce the reserved space to 3% as below:

```shell
tune2fs -m 3 /dev/sda5
```

Now, we are fully recovered. Business as usual! 👔

## Post mortem

My solution worked, but it was slightly unorthodox. After the fact, I found some more answers to fill the gaps in my knowledge.

John P Rouillard [left a comment](https://dev.to/rouilj/comment/22nep) on a cross-posted version of this article:

> It's possible that your system booted just fine but couldn't bring X [X Window System] up. IIRC [If I recall correctly] parts of X (like xdm) do not run as root would not be able to get the required disk space. Did you try changing to an alternate virtual console and log in as the root user?
>
> If I understand what you were seeing, logging in on a virtual console as root should have worked. Then you could have looked for files to clean up. With all your processing you may have filled up /tmp or /var/tmp with files. You could have deleted these files freeing enough disk space to get a graphic console back so you could run beachblit again.

The route I was unable to take due to lack of knowledge was to log into the system via a console to troubleshoot. This can done through [Single User Mode](https://teacherscollegesj.org/what-is-linux-single-user-mode/), or through a [virtual terminal](https://www.makeuseof.com/what-are-linux-virtual-consoles/).

Single User Mode is preferable as you are likely to be able to login as the `root` user (or as a ["superuser"](https://www.redhat.com/sysadmin/linux-superuser-access)) to issue commands without restrictions.

### Log in via single user mode

[Single User Mode](https://teacherscollegesj.org/what-is-linux-single-user-mode/) that is a restricted mode for maintenance purposes. It is a mode in Unix-like operating systems such as Linux operate, where a handful of services are started at system boot for basic functionality to enable a single superuser to perform certain critical tasks. The single user mode does not have any graphical interface or any networking services running.

You can read [this forum post on Ask Ubuntu](https://askubuntu.com/questions/132965/how-do-i-boot-into-single-user-mode-from-grub) to understand how to log into single user mode from Linux's default boot loader, [GRUB (GRand Unified Bootloader)](https://www.gnu.org/software/grub/).

### Log in via a virtual terminal

Linux is a multi-user system, which allows many users to work on it simultaneously via virtual terminals. A virtual terminal gives you a way to interact with the system through a text interface. You can log into a virtual terminal as the system boots. This is useful if the graphical windows system, [X (X Window System)](https://en.wikipedia.org/wiki/X_Window_System), locks up or something else goes wrong, and you need a way to try to fix the system.

A virtual terminal is the same as a regular terminal, only that you cannot use the mouse. These are some of the shortcuts that you should be aware of while working on virtual terminals.

| Shortcut         | Function                                            |
| ---------------- | --------------------------------------------------- |
| Home or Ctrl + a | Move the cursor to the start of the current line    |
| End or Ctrl + e  | Move the cursor to the end of the current line      |
| Tab              | Autocomplete commands                               |
| Ctrl + u         | Erase the current line                              |
| Ctrl + w         | Delete the word before the cursor                   |
| Ctrl + k         | Delete the line from the cursor position to the end |
| reset            | Reset the terminal                                  |
| history          | List of commands executed by the user               |
| Arrow up         | Scroll up in history and enter to execute           |
| Arrow down       | Scroll down in history and enter to execute         |
| Ctrl + d         | Logout from the terminal                            |
| Ctrl + Alt + Del | Reboot the system

There are six virtual terminals which can be launched using the shortcut keys <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>F1</kbd> through to <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>F6</kbd>.

To login into a virtual terminal:
- Start the computer and press <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>F1</kbd>. That should move you to the first virtual terminal, which is also where the boot output is redirected.
- Enter your User ID and Password.
- The virtual terminal should be ready to interact with the system.