Skip to content

Instantly share code, notes, and snippets.

@LittleFox94
Created June 11, 2021 22:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LittleFox94/b3796794aa90f79828a2febed005d469 to your computer and use it in GitHub Desktop.
Save LittleFox94/b3796794aa90f79828a2febed005d469 to your computer and use it in GitHub Desktop.
Create grub2 floppy images with ease - opinionated, paths for debian-installed grub hard-coded
#!/bin/sh -eu
keeptemporary=""
outfile="floppy.img"
modules=""
modules_default="biosdisk part_msdos fat multiboot configfile ls cat help"
help() {
cat <<EOF
Usage: $0 -o outfile.img -m vbe -m multiboot
OPTIONS
-h Show help (this help you are reading right now)
-o Set output file
-m Add a module, if given, only the given modules (and their dependencies
are added, otherwise a default set of modules is installed)
-k Keep temporary files
DEFAULT MODULES
$modules_default
Be gay, do crime
EOF
}
while getopts ko:m:h arg; do
case $arg in
o)
outfile=$OPTARG
;;
m)
modules="$modules $OPTARG"
;;
k)
keeptemporary=blacklivesmatter # we just need any string
;;
*|h)
help
exit 0
;;
esac
done
if [ -z "$modules" ]; then
modules="$modules_default"
fi
outdir="$(dirname "$outfile")"
grub_img="$outdir/$(basename "$outfile" .img)-grub.img"
grub-mkimage -p /grub -C auto -O i386-pc -o "$grub_img" $modules
size=$(ls --block-size=512 -s "$grub_img" | sed 's/\s.*$//')
dd if=/dev/zero of="$outfile" bs=512 count=2880
dd if=/usr/lib/grub/i386-pc/boot.img of="$outfile" conv=notrunc
dd if="$grub_img" of="$outfile" conv=notrunc seek=1
mformat -i "$outfile" -kR $(($size + 2))
mmd -i "$outfile" grub
foxfile=$(mktemp -p "$outdir" README.foxXXXX)
cat >"$foxfile" <<EOF
Image built with grub2-mkfloppy. Be gay, do crimes, be you, be happy
(not all required, your choice)
Modules included: $modules
For help query LittleFox on libera/#osdev
For comments about this being political, go talk to a tree.
EOF
mcopy -i "$outfile" "$foxfile" ::/README.fox
if [ -z "$keeptemporary" ]; then
rm "$grub_img" "$foxfile"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment