Factory Reset

In various scenarios it is important to be able to reset operating systems back into a “factory state”, i.e. where all state, user data and configuration is reset so that it resembles the system state when it was originally shipped.

systemd natively supports a concept of factory reset, that can both act as a specific implementation for UEFI based systems, as well as a series of hook points and a template for implementations on other systems.

Factory reset always takes place during early boot, i.e. from a well-defined “clean” state. Factory reset operations may be requested from one boot to be executed on the next.

Specifically, the following concepts are available:

Exposure in the UI

If a graphical UI shall expose a factory reset operation it should first check if requesting a factory reset is supported at all via the Varlink service mentioned above. Once a factory reset shall be executed it shall ask for activation of the factory-reset.target unit.

Alternatively, systemd-logind.service’s hotkey support may be used, for example to request factory reset if the reboot button is pressed for a long time.

Support for non-UEFI Systems

The above is a relatively bespoke solution for EFI systems. It uses EFI variables as stateful memory to request the factory reset on the next boot.

On non-EFI systems, a different mechanism should be devised. A service requesting the factory request can then be plugged into factory-reset.target. At boot the request should then be fed back to the booted kernel via the systemd.factory_reset=1 kernel command line option, in order to execute the reset operation.

Support for Resetting other Resources than Partitions + TPM

By default a factory reset implemented with systemd’s tools can reset/erase partitions (via systemd-repart, see above) and reset the TPM (via systemd-tpm2-clear.service, see above).

In some cases other resources shall be reset/erased too. To support that, define your own service and plug it into factory-reset-now.target, ensuring it is ordered before that.

Factory Reset via Boot Menu

Factory reset can also be requested via the boot menu. A simple factory reset (that does not touch the TPM) at boot can be requested via a boot menu item containing the systemd.factory_reset=1 kernel command line option. A more comprehensive factory reset operation (that also erases the TPM) can be requested by booting with rd.systemd.unit=factory-reset.target. Note that the latter will require one reboot (required since that’s how TPM resets work), while the former will reset state and continue running without an additional reboot.