From f77041a12a5484769ef982c324e2230ab9c2e377 Mon Sep 17 00:00:00 2001 From: James Le Cuirot Date: Wed, 22 Feb 2017 13:29:20 +0000 Subject: [PATCH 1/3] Ignore a fakeinit binary --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..489fda8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/fakeinit From b69d42fb27472f39b5a01104827b4072dd7cfbc6 Mon Sep 17 00:00:00 2001 From: James Le Cuirot Date: Wed, 22 Feb 2017 13:46:28 +0000 Subject: [PATCH 2/3] Bind mount just the init file This is cleaner and saves you from having to copy telinit beforehand. --- takeover.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/takeover.sh b/takeover.sh index 7c04fb2..fa887d4 100755 --- a/takeover.sh +++ b/takeover.sh @@ -2,7 +2,6 @@ set -e TO=/takeover -OLD_TELINIT=/sbin/telinit PORT=80 cd "$TO" @@ -47,7 +46,6 @@ if [ "$a" != "OK" ] ; then fi ./busybox echo "Preparing init..." -./busybox cp $OLD_TELINIT tmp/telinit ./busybox cat >tmp/init < Date: Wed, 22 Feb 2017 13:49:27 +0000 Subject: [PATCH 3/3] Support systemd and potentially other inits with a telinit wrapper --- README.md | 7 ++++--- takeover.sh | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a7e7eec..50d6b5a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/takeover.sh b/takeover.sh index fa887d4..784b142 100755 --- a/takeover.sh +++ b/takeover.sh @@ -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 <tmp/${OLD_INIT##*/} <"${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