#!/bin/sh

# make a bootable rescue iso image using the currently run kernel.
#
# grub-disk (>= 0.95) and mkisofs packages should be installed.
#

TMPDIR=/tmp/mkrescue-$$
RELEASE=`uname -r`
KERNEL=vmlinuz-$RELEASE
INITRD=initrd.img-$RELEASE
ROOT=`awk '$2 == "/" {print $1}' /etc/fstab`

mkdir $TMPDIR
mkdir $TMPDIR/iso.orig
mkdir $TMPDIR/iso
gunzip -c /usr/share/grub-disk/grub-*.iso.gz > $TMPDIR/grub.iso
if ! mount -o loop $TMPDIR/grub.iso $TMPDIR/iso.orig; then
    echo failed to mount iso image. Exiting ...
    exit 1
fi
cp -a $TMPDIR/iso.orig/* $TMPDIR/iso
if ! umount $TMPDIR/iso.orig; then
    echo failed to umount iso image.
fi
cp /boot/$KERNEL $TMPDIR/iso
cp /boot/$INITRD $TMPDIR/iso
cat <<EOF > $TMPDIR/iso/boot/grub/menu.lst
#
# Rescue disk boot menu configuration file
#

# Boot automatically after 30 secs.
timeout 30

# By default, boot the first entry.
default 0

# For booting GNU/Linux
title  Rescue boot
kernel /$KERNEL root=$ROOT
initrd /$INITRD

EOF
mkisofs \
    -b boot/grub/stage2_eltorito \
    -no-emul-boot -boot-load-size 4 -boot-info-table \
    -o rescue-$RELEASE.iso \
    -r $TMPDIR/iso
rm -Rf $TMPDIR
