Reinstall a running Linux system via SSH without rebooting, with takeover.ssh
It can sound like science fiction, but it is possible! (Running in RAM!)
A script to completely take over a running Linux system remotely, allowing you to log into an in-memory rescue environment, unmount the original root filesystem, and do anything you want, all without rebooting. Replace one distro with another without touching a physical console.
…this script will not (itself) make any permanent changes to your existing root filesystem (assuming you run it from a tmpfs), so as long as you can remotely reboot your box using an out-of-band mechanism, you should be OK.
How it works?
Seven simple steps:
- Create a directory
/takeoveron your target system and mount a tmpfs on it
- Extract your rescue environment there. Make sure it works by chrooting into it and running a few commands. Make sure you do not bork filesystem permissions. Exit the chroot.
- Grab a recent copy of
busybox(statically linked) and put it in
/takeover/busybox. You can find binaries here. Make sure it works by trying something like
- Copy the contents of this repository into
fakeinit.c. It must be compiled such that it works inside the takeover environment. If your rescue environment has
gcc, you can just compile it inside the chroot:
chroot /takeover gcc /fakeinit.c -o /fakeinit. Otherwise, you might want to statically link it.
- Shut down as many services as you can on your host.
takeover.shwill by default set up an SSHd listening on port 80, though you may edit this in the script.
sh /takeover/takeover.shand follow the prompts.
If everything worked, congratulations! You may now use your new SSH session to kill any remaining old daemons (
kill -9is recommended to make sure they don’t try to do anything silly during shutdown), and then unmount all filesystems under
/old_rootitself. You may want to first copy
/old_root/lib/modulesinto your new tmpfs in case you need any old kernel modules.
takeover.shcould be extended to support re-execing a new init once you’re done. This could be used to switch to a newdistro entirely without rebooting, as long as you’re happy using the old kernel. If you’re interested, pull requests welcome :-).