Support systemd and potentially other inits with a telinit wrapper

This commit is contained in:
James Le Cuirot 2017-02-22 13:49:27 +00:00
parent b69d42fb27
commit 5cd2f683aa
No known key found for this signature in database
GPG key ID: 21C632129C6D7DE4
2 changed files with 9 additions and 6 deletions

View file

@ -28,9 +28,10 @@ handholding, this script is not for you.
## Compatibility
This script is designed for systems using sysvinit that support the `telinit u`
command to reload `/sbin/init`. If your system uses something else, you will
have to adapt it, or this might not work at all. You're on your own here.
This script is designed for init systems that support the `telinit u` command to
reload the init binary. This includes sysvinit and systemd. If your init system
works a different way, you will have to adapt it, or this might not work at
all. You're on your own here.
You should always test this in a VM first. You can grab a tarball of your live
root filesystem, extract it into a VM image, get your VM up and running (boot

View file

@ -2,6 +2,7 @@
set -e
TO=/takeover
OLD_INIT=$(readlink /proc/1/exe)
PORT=80
cd "$TO"
@ -46,7 +47,7 @@ if [ "$a" != "OK" ] ; then
fi
./busybox echo "Preparing init..."
./busybox cat >tmp/init <<EOF
./busybox cat >tmp/${OLD_INIT##*/} <<EOF
#!${TO}/busybox sh
exec <"${TO}/${TTY}" >"${TO}/${TTY}" 2>"${TO}/${TTY}"
@ -54,11 +55,12 @@ cd "${TO}"
./busybox echo "Init takeover successful"
./busybox echo "Pivoting root..."
./busybox mount --make-rprivate /
./busybox pivot_root . old_root
./busybox echo "Chrooting and running init..."
exec ./busybox chroot . /fakeinit
EOF
./busybox chmod +x tmp/init
./busybox chmod +x tmp/${OLD_INIT##*/}
./busybox echo "Starting secondary sshd"
@ -78,7 +80,7 @@ fi
./busybox echo "You may then kill the remnants of this session and any remaining"
./busybox echo "processes from your new SSH session, and umount the old root filesystem."
./busybox mount --bind tmp/init /sbin/init
./busybox mount --bind tmp/${OLD_INIT##*/} ${OLD_INIT}
telinit u