Skip to content

Instantly share code, notes, and snippets.

@WOnder93
Last active December 17, 2015 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WOnder93/5676693 to your computer and use it in GitHub Desktop.
Save WOnder93/5676693 to your computer and use it in GitHub Desktop.
This script allows you to boot some ISO images from GRUB.
#!/bin/sh
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
. ${libdir}/grub/grub-mkconfig_lib
ROOTDIR=""
ISODIR="home/shared/iso"
if [ "x${GRUB_DEVICE_UUID}" = "x" ] || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
|| ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
|| uses_abstraction "${GRUB_DEVICE}" lvm; then
ROOT_DEVICE=${GRUB_DEVICE}
else
ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
fi
require_tmpdir() {
if [ -z "$TMPDIR" ]; then
if type mktemp >/dev/null 2>&1; then
export TMPDIR=`mktemp -d /tmp/grub-iso.XXXXXX`
else
mkdir /tmp/grub-iso
export TMPDIR=/tmp/grub-iso
fi
fi
}
add_entry_from_iso() {
ISO_MOUNT="$1"
ISO_NAME="$2"
ISO_PATH=`make_system_path_relative_to_its_root "/$ISODIR/$ISO_NAME"`
if [ -e "$ISO_MOUNT/.disk/info" ]; then
TITLE="ISO image: "`cat "$ISO_MOUNT/.disk/info"`" ($ISO_NAME)"
else
TITLE="ISO image: "$ISO_NAME
fi
if [ -e "$ISO_MOUNT/boot/grub/loopback.cfg" ]; then
echo "submenu '$TITLE' {"
sed "s@menuentry.*{@&\nsearch --set -f \"$ISO_PATH\"\nloopback loop \"$ISO_PATH\"@;s@\${iso_path}@\"$ISO_PATH\"@;s@\(linux\|initrd\)\w*\s*@&(loop)@" "$ISO_MOUNT/boot/grub/loopback.cfg"
echo "}"
elif [ -d "$ISO_MOUNT/live" ]; then
echo "submenu '$TITLE' {"
echo " menuentry 'Default settings' {"
echo " search --set -f \"$ISO_PATH\""
echo " loopback loop \"$ISO_PATH\""
echo " linux (loop)/live/vmlinuz"
echo " initrd (loop)/live/initrd.img boot=live config noswap noprompt nosplash"
echo " }"
grep "([[:blank:]]*(menu label|kernel|append))" "$ISO_MOUNT/isolinux/live.cfg"
# | sed "s@menu label\s*\(.*\)\s*\n@menuentry \"\1\" {\n@;s@kernel\s*\(.*\)\s*\n@linux (loop)\1\n@;s@append\s*initrd=\(.*\)\s*\(.*\)\n@initrd (loop)\1 \2@"
echo "}"
else
return 1
fi
return 0
}
for f in "$ROOTDIR/$ISODIR"/*; do
if [ ! -e "$f" ]; then
continue
fi
name=${f##*/}
require_tmpdir
mount -o loop "$f" "$TMPDIR" > /dev/null 2>&1
if add_entry_from_iso "$TMPDIR" "$name"; then
echo "Found ISO image: $name" >&2
fi
umount -l "$TMPDIR" > /dev/null 2>&1
done

Usage

  1. Put file grub.iso in /etc/grub.d and rename it according to the desired position in the boot menu (usually you want it to run somewhere at the end, so you should name it e. g. 90_grub-iso)
  2. Change the ROOTDIR variable to the path to the root filesysytem, without the trailing slash (normally you should leave this empty) and ISODIR to the path to the directory in which you have the ISO files, without the leading and trailing slash (can be on any partition).
  3. Run # update-grub. You should see lines like Found ISO image: ... if the ISO files were successfully added to the boot menu.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment