RPi Emergency Mode: Fixing bad fstab
Recently, while working on a project to set up my HDD as a network drive using my Raspberry Pi 400, I encountered a bad fstab error. I spent several hours going through various web pages and trying many things to edit the fstab file but to no avail. The solution I found in the end, took only 5 mins to execute, using solutions from a couple of online forums.
In this blog, I outline the reason for the fstab error and the simple and easy method to fix it.
Error in /etc/fstab
On Linux operating systems, the fstab
(file system table) is a configuration file that defines how disk partitions and file systems should be automatically mounted at boot. It specifies mount points, file system types, and options to control storage behavior.
In my case, I had configured my HDD (/dev/sdb3/) to mount to a shared folder on boot using the following command:
$ sudo mount /dev/sdb3 /user/home/share/USBHDD auto nofail defaults 0 3
Ideally, if the load should have failed for any reason, the ‘nofail’ argument should have handled it, but for reasons unknown to me, that did not work. The result was that on boot, my RPi tried to load the HDD, but it failed and then the Pi entered ‘emergency mode’ and locked up the terminal.
From the log history, I know that perhaps I have configured my mount incorrectly and ideally I should be able to fix it by editing the fstab file. Unfortunately, since my terminal is locked, I have no way to access it.
And this was the cause of all problems. I could not just plug the SD card into my laptop and edit it. I tried using ext2fsb to mount the Linux partition as a Windows drive and edit it, but it was write-protected and no matter what I tried, I could not gain write permission to the file.
I also tried to mount the SD Card to a WSL instance but again, I could not get edit permissions to any file in the SD Card.
Solution
Reading the SD card on Windows allows me to edit the bootfs partition (not rootfs which has the Linux folder). In the bootfs partition, we can find a file called cmdline.txt
We need to add a command in this file that would skip the fstab section and directly open into the bash-shell of the Pi.
.... init=/bin/sh
NOTE: This needs to be added to the end of the command that is already present. Adding the command in a new line will not work!
Save the file, place the SD Card in the Pi, and connect the power. The Pi should have opened into a basic terminal. At this point, we still do not have write permission for the files. To do that, we need to remount the disk with I/O permissions.
$ mount -n -o remount,rw /
With the write permission now acquired, I could edit the fstab file.
$ sudo nano /etc/fstab
I removed the commands that were causing the errors and restarted the Pi from the shell terminal. Viola! It worked perfectly, booting into the regular shell terminal.
One final step would be to remove the init=/bin/sh command from the cmdline.txt otherwise it would open to the basic shell on each reboot.
$ sudo nano /boot/firmware/cmdline.txt
Delete the added lines, save, and exit, and I finally have a fully functional Pi back.
Other Solutions
One other solution could have been to read the SD card as USB from a different Pi image and then edit it, but I did not have any success in trying that way using WSL and I did not have a spare SD card at hand to experiment either.
Summary
The method I used only required an extra SD card reader and no other software. In fact, this method can be used for fixing any issues caused due to incorrect configurations on an RPi.
Following a concise step-by-step guide:
1. Place the SD card on card-reader and open cmdline.txt
2. Add "init=/bin/sh" to the end of the existing command
3. Place SD card in RPi and turn on power to boot into basic shell mode
4. Remount the boot drive as writeable mode
$ mount -n -o remount,rw /
5. Edit the broken file using an editor
$ sudo nano path/to/file
6. Save and restart the RPi
$ sudo reboot -h now
7. Once loaded into terminal view, remove the "init=/bin/sh" command
$ sudo nano /boot/firmware/cmdline.txt
8. Save and Exit