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 ## Compatibility
This script is designed for systems using sysvinit that support the `telinit u` This script is designed for init systems that support the `telinit u` command to
command to reload `/sbin/init`. If your system uses something else, you will reload the init binary. This includes sysvinit and systemd. If your init system
have to adapt it, or this might not work at all. You're on your own here. 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 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 root filesystem, extract it into a VM image, get your VM up and running (boot

View file

@ -2,6 +2,7 @@
set -e set -e
TO=/takeover TO=/takeover
OLD_INIT=$(readlink /proc/1/exe)
PORT=80 PORT=80
cd "$TO" cd "$TO"
@ -46,7 +47,7 @@ if [ "$a" != "OK" ] ; then
fi fi
./busybox echo "Preparing init..." ./busybox echo "Preparing init..."
./busybox cat >tmp/init <<EOF ./busybox cat >tmp/${OLD_INIT##*/} <<EOF
#!${TO}/busybox sh #!${TO}/busybox sh
exec <"${TO}/${TTY}" >"${TO}/${TTY}" 2>"${TO}/${TTY}" exec <"${TO}/${TTY}" >"${TO}/${TTY}" 2>"${TO}/${TTY}"
@ -54,11 +55,12 @@ cd "${TO}"
./busybox echo "Init takeover successful" ./busybox echo "Init takeover successful"
./busybox echo "Pivoting root..." ./busybox echo "Pivoting root..."
./busybox mount --make-rprivate /
./busybox pivot_root . old_root ./busybox pivot_root . old_root
./busybox echo "Chrooting and running init..." ./busybox echo "Chrooting and running init..."
exec ./busybox chroot . /fakeinit exec ./busybox chroot . /fakeinit
EOF EOF
./busybox chmod +x tmp/init ./busybox chmod +x tmp/${OLD_INIT##*/}
./busybox echo "Starting secondary sshd" ./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 "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 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 telinit u