]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
support/scripts: Allow building `UEFI` `ISO`'s
authorSergiu Moga <sergiu.moga@protonmail.com>
Sat, 20 May 2023 18:40:09 +0000 (21:40 +0300)
committerUnikraft <monkey@unikraft.io>
Fri, 11 Aug 2023 14:28:34 +0000 (14:28 +0000)
Give the script the ability to build `UEFI` bootable `ISO`'s.
The `FAT32` image contained within the `ISO` will have the
following layout:
```
└── EFI
    └── BOOT
        ├── BOOT${OPTARCH}.EFI
        ├── ${OPTINITRD}
        └── ${OPTCMDLINE}
```

Add two new options:
- b: to differentiate between possible bootloaders
- a: to differentiate between possible architectures

Furthermore, rename the script to highlight the fact that this is no
longer `GRUB` only.

Signed-off-by: Sergiu Moga <sergiu.moga@protonmail.com>
Reviewed-by: Stefan Jumarea <stefanjumarea02@gmail.com>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #910

support/scripts/mkgrubimg [deleted file]
support/scripts/mkukimg [new file with mode: 0755]

diff --git a/support/scripts/mkgrubimg b/support/scripts/mkgrubimg
deleted file mode 100755 (executable)
index bcf65e9..0000000
+++ /dev/null
@@ -1,129 +0,0 @@
-#!/usr/bin/env bash
-
-# help menu
-usage()
-{
-       echo "Usage: $0 [options] [output file]"
-       echo "Creates bootable GRUB images from Unikraft kernel images."
-       echo ""
-       echo "  -h                         Display help and exit"
-       echo "  -f                         Output format (supported: iso)"
-       echo "  -k                         Path to the Unikraft kernel image"
-       echo "  -c                         Kernel command-line parameters file (optional)"
-       echo "  -i                         Path to initrd cpio file (optional)"
-       exit 1
-}
-
-# default options
-OPTFORMAT="iso"
-OPTCMDLINE=""
-OPTOUTPUT=${1}
-
-# cleanup
-BUILDDIR=
-sighandler_exit()
-{
-       if [ -n "${BUILDDIR}" ] && [ -d "${BUILDDIR}" ]; then
-               rm -rf "${BUILDDIR}"
-       fi
-}
-
-mkgrubiso()
-{
-       # configure grub
-       mkdir -p "${BUILDDIR}/boot/grub"
-
-       if [ -n "${OPTINITRD}" ]; then
-               GRUB_CFG_INITRD="module /boot/${OPTINITRDNAME}"
-               cp "${OPTINITRD}" "${BUILDDIR}/boot/${OPTINITRDNAME}" || exit 1
-       fi
-       cp "${OPTKERNELIMG}" "${BUILDDIR}/boot/" || exit 1
-
-       MBCMDL=
-       if [ -n "${OPTCMDLINE}" ]; then
-               MBCMDL=$(tr '\n' ' ' < "${OPTCMDLINE}")
-       fi
-
-       cat >"${BUILDDIR}/boot/grub/grub.cfg" <<EOF
-set default=0
-set timeout=0
-
-menuentry "${OPTKERNELNAME}" {
-       multiboot /boot/${OPTKERNELNAME} /${OPTKERNELNAME} ${MBCMDL}
-       ${GRUB_CFG_INITRD}
-       boot
-}
-EOF
-
-       # build grub image
-       GRUB_INSTALL_MSGS=$( grub-mkrescue -o "${OPTOUTPUT}" --install-modules="multiboot" --fonts="" --themes="" --locales="" "${BUILDDIR}/" 2>&1 )
-       if [ ${?} != 0 ]; then
-               printf '%s\n' "${GRUB_INSTALL_MSGS}" 1>&2
-               exit 1
-       fi
-}
-
-# process options
-while getopts "hk:c:i:f:" OPT; do
-       case "${OPT}" in
-               h)
-                       usage
-                       ;;
-               f)
-                       OPTFORMAT="${OPTARG}"
-                       ;;
-               k)
-                       OPTKERNELIMG="${OPTARG}"
-                       ;;
-               c)
-                       OPTCMDLINE="${OPTARG}"
-                       ;;
-               i)
-                       OPTINITRD="${OPTARG}"
-                       ;;
-               *)
-                       usage
-                       ;;
-       esac
-done
-shift $((OPTIND-1))
-
-# validate arguments
-if [[ -z "${OPTKERNELIMG}" ]]; then
-       echo "Missing path to kernel image ('-k')" 1>&2
-       usage
-fi
-if [[ -z "${OPTOUTPUT}" ]]; then
-       echo "Missing path to output image" 1>&2
-       usage
-fi
-if [ ! -f "${OPTKERNELIMG}" ]; then
-       echo "${OPTKERNELIMG} does not exist or is not a file" 1>&2
-       exit 1
-fi
-if [ -n "${OPTINITRD}" ] && [ ! -f "${OPTINITRD}" ]; then
-       echo "${OPTINITRD} does not exist or is not a file" 1>&2
-       exit 1
-fi
-
-# Register exit handler and create BUILDDIR
-trap sighandler_exit exit
-BUILDDIR="$( mktemp -d )"
-if [ ${?} -ne 0 ] || [ -z "${BUILDDIR}" ] || [ ! -d "${BUILDDIR}" ]; then
-       echo "Failed to create temporary directory" 1>&2
-       exit 1
-fi
-
-OPTKERNELNAME="${OPTKERNELIMG##*/}"
-OPTINITRDNAME="${OPTINITRD##*/}"
-
-case "${OPTFORMAT}" in
-       # generate grub iso image
-       iso)
-               mkgrubiso
-               ;;
-       *)
-               echo -e "\"${OPTFORMAT}\" not a supported format." 1>&2
-               usage
-               ;;
-esac
diff --git a/support/scripts/mkukimg b/support/scripts/mkukimg
new file mode 100755 (executable)
index 0000000..6e14d72
--- /dev/null
@@ -0,0 +1,183 @@
+#!/usr/bin/env bash
+
+# help menu
+usage()
+{
+       echo "Usage: $0 [options] [output file]"
+       echo "Creates bootable images from Unikraft kernel images."
+       echo ""
+       echo "  -h                         Display help and exit"
+       echo "  -f                         Output format (supported: iso)"
+       echo "  -k                         Path to the Unikraft kernel image"
+       echo "  -c                         Kernel command-line parameters file (optional)"
+       echo "  -i                         Path to initrd cpio file (optional)"
+       echo "  -b                         Bootloader: grub (GRUB), ukefi (Unikraft EFI stub)"
+       echo "  -a                         Architecture: X64 (x86_64), AA64 (Aarch64)"
+       exit 1
+}
+
+# default options
+OPTFORMAT="iso"
+OPTCMDLINE=""
+OPTOUTPUT=${1}
+OPTARCH=
+
+# cleanup
+BUILDDIR=
+sighandler_exit()
+{
+       if [ -n "${BUILDDIR}" ] && [ -d "${BUILDDIR}" ]; then
+               rm -rf "${BUILDDIR}"
+       fi
+}
+
+mkgrubiso()
+{
+       # configure grub
+       mkdir -p "${BUILDDIR}/boot/grub"
+
+       if [ -n "${OPTINITRD}" ]; then
+               GRUB_CFG_INITRD="module /boot/${OPTINITRDNAME}"
+               cp "${OPTINITRD}" "${BUILDDIR}/boot/${OPTINITRDNAME}" || exit 1
+       fi
+       cp "${OPTKERNELIMG}" "${BUILDDIR}/boot/" || exit 1
+
+       MBCMDL=
+       if [ -n "${OPTCMDLINE}" ]; then
+               MBCMDL=$(tr '\n' ' ' < "${OPTCMDLINE}")
+       fi
+
+       cat >"${BUILDDIR}/boot/grub/grub.cfg" <<EOF
+set default=0
+set timeout=0
+
+menuentry "${OPTKERNELNAME}" {
+       multiboot /boot/${OPTKERNELNAME} /${OPTKERNELNAME} ${MBCMDL}
+       ${GRUB_CFG_INITRD}
+       boot
+}
+EOF
+
+       # build grub image
+       GRUB_INSTALL_MSGS=$( grub-mkrescue -o "${OPTOUTPUT}" --install-modules="multiboot" --fonts="" --themes="" --locales="" "${BUILDDIR}/" 2>&1 )
+       if [ ${?} != 0 ]; then
+               printf '%s\n' "${GRUB_INSTALL_MSGS}" 1>&2
+               exit 1
+       fi
+}
+
+mkukefiiso()
+{
+       # create initial empty image we will format as FAT32
+       # ESP must be at least 100MB
+       dd if=/dev/zero of=${BUILDDIR}/fs.img bs=1M count=100
+
+       mkfs.vfat -F 32 ${BUILDDIR}/fs.img
+
+       LOOPDEV=$(sudo losetup --find --show ${BUILDDIR}/fs.img)
+       mkdir ${BUILDDIR}/mnt
+       sudo mount ${LOOPDEV} ${BUILDDIR}/mnt
+       sudo mkdir -p ${BUILDDIR}/mnt/EFI/BOOT/
+
+       if [ ! -z ${OPTINITRD} ]; then
+               sudo cp ${OPTINITRD} ${BUILDDIR}/mnt/EFI/BOOT/
+       fi
+
+       if [ ! -z ${OPTCMDLINE} ]; then
+               sudo cp ${OPTCMDLINE} ${BUILDDIR}/mnt/EFI/BOOT/
+       fi
+
+       sudo cp ${OPTKERNELIMG} ${BUILDDIR}/mnt/EFI/BOOT/BOOT${OPTARCH}.EFI
+
+       sudo umount ${BUILDDIR}/mnt
+       sudo losetup -d ${LOOPDEV}
+
+       mkdir ${BUILDDIR}/iso_dir
+       cp ${BUILDDIR}/fs.img ${BUILDDIR}/iso_dir
+       xorriso -as mkisofs -R -f -e fs.img -no-emul-boot -o ${OPTOUTPUT} ${BUILDDIR}/iso_dir
+}
+
+# process options
+while getopts "hk:c:i:f:a:b:" OPT; do
+       case "${OPT}" in
+               h)
+                       usage
+                       ;;
+               f)
+                       OPTFORMAT="${OPTARG}"
+                       ;;
+               k)
+                       OPTKERNELIMG="${OPTARG}"
+                       ;;
+               c)
+                       OPTCMDLINE="${OPTARG}"
+                       ;;
+               i)
+                       OPTINITRD="${OPTARG}"
+                       ;;
+               b)
+                       OPTBOOTLOADER="${OPTARG}"
+                       ;;
+               a)
+                       OPTARCH="${OPTARG}"
+                       ;;
+               *)
+                       usage
+                       ;;
+       esac
+done
+shift $((OPTIND-1))
+
+# validate arguments
+if [[ -z "${OPTKERNELIMG}" ]]; then
+       echo "Missing path to kernel image ('-k')" 1>&2
+       usage
+fi
+if [[ -z "${OPTOUTPUT}" ]]; then
+       echo "Missing path to output image" 1>&2
+       usage
+fi
+if [ ! -f "${OPTKERNELIMG}" ]; then
+       echo "${OPTKERNELIMG} does not exist or is not a file" 1>&2
+       exit 1
+fi
+if [ -n "${OPTINITRD}" ] && [ ! -f "${OPTINITRD}" ]; then
+       echo "${OPTINITRD} does not exist or is not a file" 1>&2
+       exit 1
+fi
+if [ "${OPTARCH}" != "AA64" ] && [ "${OPTARCH}" != "X64" ]; then
+       echo "Invalid architecture" 1>&2
+       usage
+fi
+
+# Register exit handler and create BUILDDIR
+trap sighandler_exit exit
+BUILDDIR="$( mktemp -d )"
+if [ ${?} -ne 0 ] || [ -z "${BUILDDIR}" ] || [ ! -d "${BUILDDIR}" ]; then
+       echo "Failed to create temporary directory" 1>&2
+       exit 1
+fi
+
+OPTKERNELNAME="${OPTKERNELIMG##*/}"
+OPTINITRDNAME="${OPTINITRD##*/}"
+
+case "${OPTFORMAT}" in
+       iso)
+               case "${OPTBOOTLOADER}" in
+               grub)
+                       mkgrubiso
+                       ;;
+               ukefi)
+                       mkukefiiso
+                       ;;
+               *)
+                       echo -e "\"${OPTBOOTLOADER}\" not a supported bootloader." 1>&2
+                       usage
+                       ;;
+               esac
+               ;;
+       *)
+               echo -e "\"${OPTFORMAT}\" not a supported format." 1>&2
+               usage
+               ;;
+esac