Skip to content

Instantly share code, notes, and snippets.

@LunNova
Created May 10, 2019 12:03
Show Gist options
  • Save LunNova/682c7aa880b1ec500d174d3bd63bb54b to your computer and use it in GitHub Desktop.
Save LunNova/682c7aa880b1ec500d174d3bd63bb54b to your computer and use it in GitHub Desktop.
worst bootloader ever
#!/usr/bin/env bash
# This script boots the first linux it finds in a grub config on any available mount point
# using kexec
DEBIAN_FRONTEND=noninteractive sudo apt-get -q -y install kexec-tools
set -euvo pipefail
parts=$(sudo blkid | grep -v /dev/loop | sort | cut -d: -f 1)
grub_paths="/boot/grub/grub.cfg /grub/grub.cfg /boot/grub.cfg"
echo $parts
for device in $parts; do
mntpnt="/bootmount/$device"
sudo mkdir -p "$mntpnt"
sudo mount -o ro "$device" "$mntpnt" || true
for grub_path in $grub_paths; do
path="${mntpnt}${grub_path}"
if [ -f "$path" ]; then
echo "Found grub cfg at $path"
menu=$(grep '^\s*menuentry\s\s*["'\'']' < "$path" | head -n 1 | sed 's/^\s*//g' || true)
linux=$(grep '^\s*linux' < "$path" | head -n 1 | sed 's/^\s*linux\s*//' | sed 's/\s*$//' || true)
initrd=$(grep '^\s*initrd' < "$path" | head -n 1 | sed 's/^\s*initrd\s*//' | sed 's/\s*$//' || true)
kernel=$(echo "$linux" | awk -F '[\t ]' '{print $1}')
command=$(echo "$linux" | awk -F '[\t ]' '{s = ""; for (i = 2; i <= NF; i++) s = s $i " "; print s}')
echo "config: $menu $linux"
echo "kernel: $kernel"
echo "kernel command: $command"
echo "initrd: $initrd"
sudo kexec -l "$mntpnt/$kernel" --initrd="$mntpnt/$initrd" --command-line="$command"
sudo kexec -e
exit 1
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment