Okay, your system is completely broken, and you need to umount / or something like that.
What do you do?
One of our servers had a broken root filesystem (btrfs, don't judge me).
Online recovery was not possible, so the filesystem needed to be unmounted which is not possible for the root fs.
Additionally, as errors were detected, the kernel decided to mount it read only and didn't let me remount it as rw.
IPMI? Yes, I had the password in my password store but not the username.
So the only logical solution was to kexec into an emergency system.
This code is what I used.
It recovers all IP addresses as well as SSH host and user keys from the old system and kexecs into a new one - entirely in-memory.
The emergency script (found in the repository root) will SSH over and execute the following things:
- Build the recovery image (a
.tar.xzwith a small nix store and akexecscript) from the files in this repository locally on the machine you're executing this code on- The system configuration is found in
configuration.nix - Some
kexec-related features are imported fromkexec.nix - The scripts will be included to be used in the
kexecscript (see below)
- The system configuration is found in
- Try to
mkdir/nixand/tmp. If the don't already exist and your root fs is read-only, you have a problem this project can't fix - Mount a fresh
tmpfson/tmpbecause there might not be one already scpthe emergency image over and extract it- Mount the nix store from the emergency image over
/nixusingoverlayfs - Run the kexec script
The kexec script (found in kexec.nix) will do the following:
- Prepare a second initrd
- Put your SSH host keys into the initrd
- Put all of your SSH user keys into the initrd
- Fetch all your IP addresses and routes and put them into the initrd
- Pack the second initrd and append it to the default NixOS initrd from the emergency image
kexecinto the kernel from the emergency image while using the new initrd- In case you didn't already notice: This will crash your currently running system, so maybe it's a good idea to gracefully shut down remaining daemons if that's still possible
The script that is packed into the initrd of the new system will do the following:
- Place the SSH host key
- Place the SSH user keys
- Place a script for the IP addresses which will be executed using
networking.localCommandsso the interfaces are available
If you set the environment variable EMERGENCY_DUMP_NETWORK to 1, all IPs, routes, and nameservers will be placed in the emergency_ips, emergency_routes, and emergency_nameservers files, respectively.
$ ./emergency root@somehost
# or
$ ./emergency somebody@somehost
If it doesn't work for you, I'm sorry. I can probably not help you, but if you're able to fix something, feel free to create a PR.
The code is based on clever's kexec nix-test (found here).
The code is licensed under the LGPL3.