[OpenBSDsupport]

How to use ed to edit /etc/fstab in single user mode


The Problem: Adding or removing a disk suddenly changes the number(s) of the drive(s).

A system where everything was working just fine, suddenly fails to boot or tries to mount all the wrong partitions. The disk wd0 is now identified as wd1 or wd2, but the entries in /etc/fstab still reflect it as wd0! This can happen either when adding or when removing a disk. Unless you have made this change before, you cannot guess ahead of time.

The simplest way to avoid having to do a hard reboot with the power switch is to go ahead and type boot -s when the boot prompt comes up. As single user mode finishes starting, you will see which drive root is on. You can then type cat /etc/fstab or df and see if all is OK. If all is OK, continue booting. Otherwise, you will need to edit /etc/fstab.


Ed is your new best friend

You can, of course, do a lot of fsck'ing and mounting in order to get your fancy new editors like vi. But, why bother? The simple, but effective editor called ed can easily fix your fstab problems and you already have access to it in single user mode.

As always, you should start off reading the appropriate manual page: ed. I freely admit, becoming an "ed" master editor seems a little silly as there are now more powerful editors available. However, I cherish having learned to use ed. It saves my butt every time I have to use it. Actually, ed is very simple to use and there is little difficulty in making the simple changes to /etc/fstab.


The process:

You will need to operate in single user mode. You can get into that mode from the boot prompt:
boot> boot -s
This will boot into single user mode. At the end of the dmesg information that scrolls by, you will see:
root on wd?a swap on wd?b dump on wd?b
Enter pathname of shell or RETURN for sh:

Note that the ? will be a number such as 0,1,2,etc. For the rest of my examples, I will assume 2 came up.
This number tells you which number needs to be in /etc/fstab.

/ is now mounted as read only. In order to make changes to /etc/fstab, you will need write access. First you need to fsck and then mount /.
# fsck -fp /dev/wd2a
# mount /dev/wd2a /

/ is now mounted as read and write. (You cannot umount /.)

Lets see what needs to be changed.
# cat /etc/fstab
/dev/wd0a / ffs rw 1 1
/dev/wd0k /home ffs rw,nodev,nosuid 1 2
/dev/wd0d /tmp ffs rw,nodev,nosuid 1 2
/dev/wd0f /usr ffs rw,nodev 1 2
/dev/wd0e /var ffs rw,nodev,nosuid 1 2
/dev/wd1g /mycoolstuff ffs rw,nodev,nosuid 1 2

Clearly this drive has changed from being wd0 to now being wd2. Things cannot work properly without correcting /etc/fstab to have wd2 where wd0 is at now. Using ed can quickly fix this. Obviously, there are now three hard drives, wd0, wd1 and wd2. Since wd1 is also referenced in /etc/fstab, but we do not know (yet) which drive wd1 has now become (it could be wd0 or wd1 now), we are going to just comment that entry out and fix it later after a clean boot when all tools are available and everything is easier.

Ed has an option that I like to use ed -p allows a prompt to be selected instead of just a blank line. I will use ed -p^^ here so that you can see each line clearly.

# ed -p^^ /etc/fstab
^^1,$n
1       /dev/wd0a / ffs rw 1 1
2       /dev/wd0k /home ffs rw,nodev,nosuid 1 2
3       /dev/wd0d /tmp ffs rw,nodev,nosuid 1 2
4       /dev/wd0f /usr ffs rw,nodev 1 2
5       /dev/wd0e /var ffs rw,nodev,nosuid 1 2
6       /dev/wd1g /mycoolstuff ffs rw,nodev,nosuid 1 2
^^1,5s/wd0/wd2/
^^1,$n
1       /dev/wd2a / ffs rw 1 1
2       /dev/wd2k /home ffs rw,nodev,nosuid 1 2
3       /dev/wd2d /tmp ffs rw,nodev,nosuid 1 2
4       /dev/wd2f /usr ffs rw,nodev 1 2
5       /dev/wd2e /var ffs rw,nodev,nosuid 1 2
6       /dev/wd1g /mycoolstuff ffs rw,nodev,nosuid 1 2
^^6s/\/dev/#\/dev/
^^1,$p
/dev/wd2a / ffs rw 1 1
/dev/wd2k /home ffs rw,nodev,nosuid 1 2
/dev/wd2d /tmp ffs rw,nodev,nosuid 1 2
/dev/wd2f /usr ffs rw,nodev 1 2
/dev/wd2e /var ffs rw,nodev,nosuid 1 2
#/dev/wd1g /mycoolstuff ffs rw,nodev,nosuid 1 2
^^w
^^q
# cat /etc/fstab
/dev/wd2a / ffs rw 1 1
/dev/wd2k /home ffs rw,nodev,nosuid 1 2
/dev/wd2d /tmp ffs rw,nodev,nosuid 1 2
/dev/wd2f /usr ffs rw,nodev 1 2
/dev/wd2e /var ffs rw,nodev,nosuid 1 2
#/dev/wd1g /mycoolstuff ffs rw,nodev,nosuid 1 2
# exit (or reboot)

Ed has fairly simple commands: line number to start at , comma, line number to end at. 1,$ means line one to last line. 6 by itself means only line six. n means show lines with line numbers in front, p means show lines, s means to substitute, w means to write, q means to quit. You can hit Ctrl-c to cancel a command.

If you got cleanly to single user mode, you can just type exit and finish booting, however, if you got to single user mode trying to boot and it failed, use reboot.

Good luck. Remember, you will not have manual pages to look at if you cannot boot completely, so learn this before you do your disk swaps!