# Emergency and rescue mode startup

The "emergency" and "rescue" startup modes (which superseded "single-user" mode in the 1990s) are two of the three system startup modes, the third being "normal" mode. They are usually signalled by flags such as `-b` or `-s` being passed as command-line arguments to the first user process, running `system-manager`, which in turn passes them along to the `init` subcommand of `system-control`. These flags originate in an "Initial RAM disc" system, the kernel, or often in the operating system's boot loader; passing through this chain to reach the system manager as process 1.

As mentioned in the `shutdown` manual, these are startup modes, not shutdown modes. One can only start up into them, not shut down to them.

## Emergency mode

The `emergency` target wants a service named `emergency-login@console`, an emergency login service instatiated against the system console device. (See `freebsd-console` and `linux-console`.)

The main command that this runs is `emergency-login`, to check the password of the superuser, and do a minimal login dialogue with "glass TTY" semantics and fixed to the "root" superuser account in the system account database. It inherits the minimal environment set up by `system-manager`, as modified by `vc-get-terminal`. Elsewhere in this service, `setsid` makes the process a session leader, `open-controlling-terminal` sets up the system console as the controlling terminal for the session, and `setlogin` sets the login name for the session.

"emergency" mode brings up no other services, and thus mounts no (non-"API") filesystems, and does not even necessarily make the root filesystem available read-write.

As these are multi-call binaries with the subordinate commands that they require built-in, accidentally deleting the subordinate commands from the default `PATH` will not render a system unbootable. Moreover, there are a limited number of files that need to be repaired on a very broken system in order to make it at least bootable into emergency startup mode. They are (the copies on the root volume of) the system manager, the `emergency-login` service bundle, the `nosh` script interpreter for the `run` script that invokes emergency login, the `emergency-login` command, and the program for whatever shell `emergency-login` in its turn invokes.

### Emergency mode tips

In shells such as the Korn, Bourne Again, and Watanabe shells, POSIX-conformant mode can be turned off once the shell is running with `set +o posix`. In shells such as Debian Almquist and Almquist shells, there is effectively no non-POSIX-conformant mode.

It is possible to use the `shell` setting in the `login.conf` subsystem to differentiate the emergency-mode shell from the rescue and normal mode shells. This can permit, for example, the emergency mode shell to be statically linked whilst the rescue and normal mode shells are dynamically linked.

Note: On FreeBSD until 2021, this required setting up `root` and `toor` with distinct login classes in `login.conf` . Until 2021, FreeBSD stuck with the long-standing convention of the `root` account's login shell being the C shell, and this is still assumed for a lot of FreeBSD documentation. After 2021 on FreeBSD, this merely requires a modification to the `root` entry in the global `login.conf`.

Note: NetBSD uses its Almquist `sh` for the login shell of both the `root` and `toor` accounts. As of 2023 it already configures, out of the box, its `toor` account to specify a statically linked build of its Almquist `sh` as the account's login shell to run.

## Rescue mode

The `rescue` target is a more conventional TUI login service, like the `ttylogin@device` services used for virtual terminal login but instantiated against the system console. It expects the system account database to be functional, and permits logging in to accounts other than the superuser. The login dialogue that this presents is displayed by `login-envuidgid`, and it expects the console to be a "modern" terminal capable of 1980s cursor addressibility, colours, and character attributes; and of 1990s Unicode.

Note: If the `freebsd-console` or `linux-console` is a serial or parallel device, the DTE at the other end of the wire is expected to be capable of speaking UTF-8. This is the 21st century, of course; even Microsoft HyperTerminal was capable of handling UTF-8 on Windows XP.

"rescue" mode brings up local mounted filesystems, runs checks, and does basic system intializations such as services that set the dynamic hostname and machine ID.