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!)


And you can do with this script developed by Héctor Martín Cantero:

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:

  1. Create a directory /takeover on your target system and mount a tmpfs on it
  2. 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.
  3. 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 /takeover/busybox sh.
  4. Copy the contents of this repository into /takeover.
  5. Compile 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.
  6. Shut down as many services as you can on your host. takeover.sh will by default set up an SSHd listening on port 80, though you may edit this in the script.
  7. Run sh /takeover/takeover.sh and follow the prompts.

If everything worked, congratulations! You may now use your new SSH session to kill any remaining old daemons (kill -9 is recommended to make sure they don’t try to do anything silly during shutdown), and then unmount all filesystems under /old_root, including /old_root itself. You may want to first copy /old_root/lib/modules into your new tmpfs in case you need any old kernel modules.

takeover.sh could 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 :-).

More info and download

https://github.com/marcan/takeover.sh

Comments