Accidentally Deleting fstab —

Accidentally Deleting fstab

by Joshua Branson — April 05, 2023

The end of my previous blog post was a bit of a cliff hanger:

I did have a great time the next day. I was hoping to automount my usb stick on boot. So I added this beauty to my /etc/fstab.

sd1i /mnt/usb msdos rw 1 2

The next time I booted it threw me into a rescue shell with only / mounted.

This is the story of how I fixed a broken fstab on OpenBSD. The day began like any other. It was cold and dark as I arose to play with my new OpenBSD computer. It was time to reboot! That magical moment when OpenBSD relinks the kernel at boot. How cool is that?

My admiration for this world class operating continues to rise as it boots, and OpenBSD proudly slaps me in the face (I did not write down the OpenBSD error message, but this is essentially what it said):

mount cannot find device sd1i. You are now in a resque shell.


Hmmm. Well that’s a bummer. It looks like I had told OpenBSD that that usb stick was necesary for boot. I also did not have the usb stick plugged in, and OpenBSD needed me to write /dev/sd1i not sd1i. It might be a good project idea to add opendev support to OpenBSD’s mount. Any takers?

Anyway, to fix this, I attempted to delete that last line from /etc/fstab. I tried to type out cd /etc/. Instead I got gibberish. Testing the keyboard a bit, I noticed that the keyboard was in the qwerty layout. I use dvorak. And my laptop keyboard physically shows a dvorak keyboard layout. It’s really hard to type correctly on a keyboard when pressing “‘,.pyf” is “qwerty”. Well let’s change my keyboard layout (notice that all commands below are run as the root user. The # means you are running as a root user).

I painstakingly typed out the following command.

# wsconctl keyboard.encoding=us.dvorak

Awesome, that is an improvement. Let’s change /etc/fstab. The following commands did not work, because the shell could not find the binary:

  • nano /etc/fstab
  • vim /etc/fstab
  • vi /etc/fstab

Well that’s weird. Is /usr/bin not mounted?

# mount

/dev/sd1a on / type ffs (ro)

Oh great! I only have / mounted! So my commands are limited, and / is mounted read only. So even if I find a text editor that I can use, I cannot modify /etc/fstab. How do I mount / read-write? According to the irc people who helped me out, this is how: you update the mount information based on what /etc/fstab says.

# mount -uvw /
# mount

/dev/sd1a on / type ffs (rw)

Awesome, fstab is editable! I tried to edit /etc/fstab but vi was not available, probably because vi is located in /usr, which is not yet mounted.

What I should have done was mount -U. This just mounts all mount points listed in /etc/fstab. I did not read that option in the manpage yet. So I decided to manually try to guess which filesytem was which.

Well let’s print out human read-able file sizes of my filesystem partitions and that will give me some clue which filesystem is which. (please note that I am intentionally removing the offsets).

# disklabel -h sd1

16 partitions:
#                size           offset  fstype [fsize bsize   cpg]
  a:             1.0G                   4.2BSD   2048 16384 12960
  b:             8.0G                     swap
  c:           931.5G                   unused
  d:             4.0G                   4.2BSD   2048 16384 12960
  e:            19.5G                   4.2BSD   2048 16384 12960
  f:            30.0G                   4.2BSD   2048 16384 12960
  g:             1.0G                   4.2BSD   2048 16384 12960
  h:            20.0G                   4.2BSD   2048 16384 12960
  i:             3.0G                   4.2BSD   2048 16384 12960
  j:             6.0G                   4.2BSD   2048 16384 12960
  k:           300.0G                   4.2BSD   4096 32768 26062

Ok. Clearly the 300G is my /home. I can mount that now.

# mount /dev/sd1k /home

Ok. How do I mount /usr so that I have text editors? I don’t really know which partition is which. I guess I will just guess. Eventually I did correctly mount /usr, so now I should have access to some text editors! So now I could edit fstab.

# vim /etc/fstab

vim: unknown command

Ok. vim is not installed. Let’s try vi.

# vi /etc/fstab

vi: unknown terminal type

So vi doesn’t run on the console? That’s odd. Ok. Lets see what head shows me:

# head /etc/fstab

5583d235b610c8a2.a /          ffs rw,softdep 1 1
5583d235b610c8a2.k /home      ffs rw,softdep,nodev,nosuid 1 2
5583d235b610c8a2.d /tmp       ffs rw,softdep,nodev,nosuid 1 2
5583d235b610c8a2.f /usr       ffs rw,softdep,nodev 1 2
5583d235b610c8a2.g /usr/X11R6 ffs rw,softdep,nodev 1 2
5583d235b610c8a2.h /usr/local ffs rw,wxallowed,softdep,nodev 1 2
5583d235b610c8a2.j /usr/obj   ffs rw,softdep,nodev,nosuid 1 2
5583d235b610c8a2.i /usr/src   ffs rw,softdep,nodev,nosuid 1 2
5583d235b610c8a2.e /var       ffs rw,softdep,nodev,nosuid 1 2
5583d235b610c8a2.b none swap sw

Hey would you look at that! There is no line with /mnt/usb. I should be able to just overwrite fstab with the output of head. Please do NOT copy or execute the following command:

# head /etc/fstab > /etc/fstab

I wish I had read the irc chat before I had executed the above command. In the words of a wise sage, “the redirection happens before head runs, so you’ll just get a blank file.” And that is exactly what happened. /etc/fstab was empty. Now how do I mount filesystem partitions in the correct locations? “Out of the frying pan and into the fire.”

Some of the people on irc mentioned that /var/backups should have a copy of my fstab. Unfortunately, that backup command had not run yet. This was a fresh OpenBSD install afterall.

One of the people on the OpenBSD chat showed me this link, which was super helpful, because it shows you the default size of the various partitions that the auto installer sets up. With that information, I was able to re-mount all my various partitions. Then someone on irc chat gave me this beauty of a command:

# mount | awk '{ print $1 " " $3 " " $5 " rw 0 0"}' > /etc/fstab

That created my fstab for me! Let’s check it out!

# cat /etc/fstab

sd1.a /          ffs rw 1 1
sd1.k /home      ffs rw 1 2
sd1.d /tmp       ffs rw 1 2
sd1.f /usr       ffs rw 1 2
sd1.g /usr/X11R6 ffs rw 1 2
sd1.h /usr/local ffs rw 1 2
sd1.j /usr/obj   ffs rw 1 2
sd1.i /usr/src   ffs rw 1 2
sd1.e /var       ffs rw 1 2
sd1.b none swap sw

Hmm, well the mount options are absent, and it is NOT using UIDs…But this should be enough to boot into a complete system. So I rebooted. Upon reboot, I was able to change the mount points to UIDs and copy the proper mount options from my desktop OpenBSD. I then very quickly set up hotplugd, which I will explain next time. Until then!