marcan / takeover.sh

Wipe and reinstall a running Linux system via SSH, without rebooting. You know you want to.
Other
7.18k stars 356 forks source link

Tried this on debian 10 #12

Open fivethreeo opened 3 months ago

fivethreeo commented 3 months ago

rm -rf takeover
rm systemrescue.iso

wget https://downloads.sourceforge.net/project/systemrescuecd/sysresccd-x86/11.01/systemrescue-11.01-amd64.iso -O systemrescue.iso 
mount systemrescue.iso /mnt/
unsquashfs -f -d takeover/ /mnt/sysresccd/x86_64/airootfs.sfs
umount /mnt
rm systemrescue.iso
wget https://raw.githubusercontent.com/fivethreeo/takeover.sh/master/fakeinit.c -O takeover/fakeinit.c
wget https://raw.githubusercontent.com/fivethreeo/takeover.sh/master/takeover.sh -O takeover/takeover.sh
wget https://www.busybox.net/downloads/binaries/1.35.0-x86_64-linux-musl/busybox -O takeover/busybox
chmod u+x takeover/busybox
cp /etc/resolv.conf takeover/etc/
gcc takeover/fakeinit.c -o takeover/fakeinit
systemctl isolate rescue-ssh.target
sh takeover/takeover.sh

But I get

Init takeover successful
Pivoting root...
pivot_root: (null): Resource busy
Chrooting and running init...
fivethreeo commented 3 months ago

Changed

./busybox echo "Pivoting root..."
./busybox mount --make-rprivate /
./busybox pivot_root . old_root
./busybox echo "Chrooting and running init..."

to

./busybox echo "Pivoting root..."
./busybox mount --make-rprivate /
./busybox unshare -m
./busybox pivot_root . old_root
./busybox echo "Chrooting and running init..."
fivethreeo commented 3 months ago

cd /
sudo bash
rm -rf takeover
rm systemrescue.iso

wget https://downloads.sourceforge.net/project/systemrescuecd/sysresccd-x86/11.01/systemrescue-11.01-amd64.iso -O systemrescue.iso 
mount systemrescue.iso /mnt/
unsquashfs -f -d takeover/ /mnt/sysresccd/x86_64/airootfs.sfs
umount /mnt
rm systemrescue.iso
wget https://raw.githubusercontent.com/fivethreeo/takeover.sh/master/fakeinit.c -O takeover/fakeinit.c
wget https://raw.githubusercontent.com/fivethreeo/takeover.sh/master/takeover.sh -O takeover/takeover.sh
wget https://www.busybox.net/downloads/binaries/1.35.0-x86_64-linux-musl/busybox -O takeover/busybox
chmod u+x takeover/busybox
cp /etc/resolv.conf takeover/etc/
gcc takeover/fakeinit.c -o takeover/fakeinit
systemctl isolate rescue-ssh.target
sh takeover/takeover.sh

# Login on new sshd root@host

cd /
mkdir -p installer/tmp
cd installer/tmp
wget https://deb.debian.org/debian/dists/bookworm/main/installer-amd64/current/images/netboot/netboot.tar.gz
tar xzfv netboot.tar.gz
cd ..
zcat tmp/debian-installer/amd64/initrd | cpio -idmv
mount -t proc /proc proc/
mount --rbind /sys sys/
mount --rbind /dev dev/
killall -s SIGKILL agetty
killall -s SIGKILL dhclient
kill -9 `pgrep sshd | head -1` # other sshd
cp /etc/resolv.conf etc/

cat <<- EOF > preseed.cfg
d-i netcfg/enable boolean false
EOF

busybox chroot . /sbin/debian-installer-startup
busybox chroot . /sbin/debian-installer

Partitioning fails because of lvm modules

fivethreeo commented 3 months ago

How far I got


cd /
sudo bash
rm -rf takeover
rm systemrescue.iso

wget https://downloads.sourceforge.net/project/systemrescuecd/sysresccd-x86/11.01/systemrescue-11.01-amd64.iso -O systemrescue.iso 
mount systemrescue.iso /mnt/
unsquashfs -f -d takeover/ /mnt/sysresccd/x86_64/airootfs.sfs
umount /mnt
rm systemrescue.iso
cp -R /lib/modules/`uname -r` takeover/lib/modules/`uname -r`
wget https://raw.githubusercontent.com/fivethreeo/takeover.sh/master/fakeinit.c -O takeover/fakeinit.c
wget https://raw.githubusercontent.com/fivethreeo/takeover.sh/master/takeover.sh -O takeover/takeover.sh
wget https://www.busybox.net/downloads/binaries/1.35.0-x86_64-linux-musl/busybox -O takeover/busybox
chmod u+x takeover/busybox
cp /etc/resolv.conf takeover/etc/
gcc takeover/fakeinit.c -o takeover/fakeinit
systemctl isolate rescue-ssh.target
sh takeover/takeover.sh

# Login on new sshd root@host

cd /
mkdir -p installer/tmp
cd installer/tmp
wget https://deb.debian.org/debian/dists/bookworm/main/installer-amd64/current/images/netboot/netboot.tar.gz
tar xzfv netboot.tar.gz
cd ..
zcat tmp/debian-installer/amd64/initrd | cpio -idmv

busybox rm -f etc/mtab
busybox ln -s /etc/mtab etc/mtab

busybox mount -t tmpfs tmp tmp
busybox mount -t proc proc proc
busybox mount -t sysfs sys sys

if ! busybox mount -t devtmpfs dev dev; then
    busybox mount -t tmpfs dev dev
    busybox cp -a /dev/* dev/
    busybox rm -rf dev/pts
    busybox mkdir dev/pts
fi
busybox mount --bind /dev/pts dev/pts

find /lib/modules/`uname -r`/kernel/drivers/block -type f -print0 | xargs -0 -I {} echo insmod {} >> blockmodules.sh

cat <<- EOF > preseed.cfg
d-i netcfg/enable boolean false
EOF

cp -R /lib/modules/`uname -r` lib/modules/`uname -r`

busybox chroot . /bin/sh
sh blockmodules.sh
/sbin/debian-installer-startup
/sbin/debian-installer

# open another ssh
cp /etc/resolv.conf /installer/etc/