]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
support/scripts/mkgrubimg: Move `GRUB ISO` image making into method
authorSergiu Moga <sergiu.moga@protonmail.com>
Sat, 20 May 2023 14:59:30 +0000 (17:59 +0300)
committerUnikraft <monkey@unikraft.io>
Fri, 11 Aug 2023 14:28:34 +0000 (14:28 +0000)
Move the making of `GRUB ISO` images into its own method to make the
script more modular and make adding future functionalities easier.

Furthermore, while at it, fix some minor existing `shellcheck` warnings.

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

index 87aed741097799f2184f8070b2efba03b1c21e35..9ec4aca0016be51d6b91cb1b28f4273d3225c196 100755 (executable)
@@ -17,16 +17,47 @@ usage()
 # default options
 OPTFORMAT="iso"
 OPTCMDLINE=""
+OPTOUTPUT=${1}
 
 # cleanup
 BUILDDIR=
 sighandler_exit()
 {
-       if [ ! -z "${BUILDDIR}" -a -d "${BUILDDIR}" ]; then
+       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
+
+       cat >"${BUILDDIR}/boot/grub/grub.cfg" <<EOF
+set default=0
+set timeout=0
+
+menuentry "${OPTKERNELNAME}" {
+       multiboot /boot/${OPTKERNELNAME} /${OPTKERNELNAME} ${OPTCMDLINE}
+       ${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:i:f:a:" OPT; do
        case "${OPT}" in
@@ -57,7 +88,7 @@ if [[ -z "${OPTKERNELIMG}" ]]; then
        echo "Missing path to kernel image ('-k')" 1>&2
        usage
 fi
-if [[ -z "${1}" ]]; then
+if [[ -z "${OPTOUTPUT}" ]]; then
        echo "Missing path to output image" 1>&2
        usage
 fi
@@ -65,7 +96,7 @@ if [ ! -f "${OPTKERNELIMG}" ]; then
        echo "${OPTKERNELIMG} does not exist or is not a file" 1>&2
        exit 1
 fi
-if [ ! -z "${OPTINITRD}" -a ! -f "${OPTINITRD}" ]; then
+if [ -n "${OPTINITRD}" ] && [ ! -f "${OPTINITRD}" ]; then
        echo "${OPTINITRD} does not exist or is not a file" 1>&2
        exit 1
 fi
@@ -73,7 +104,7 @@ fi
 # Register exit handler and create BUILDDIR
 trap sighandler_exit exit
 BUILDDIR="$( mktemp -d )"
-if [ $? -ne 0 -o -z "${BUILDDIR}" -o ! -d "${BUILDDIR}" ]; then
+if [ ${?} -ne 0 ] || [ -z "${BUILDDIR}" ] || [ ! -d "${BUILDDIR}" ]; then
        echo "Failed to create temporary directory" 1>&2
        exit 1
 fi
@@ -84,32 +115,7 @@ OPTINITRDNAME="${OPTINITRD##*/}"
 case "${OPTFORMAT}" in
        # generate grub iso image
        iso)
-               # configure grub
-               mkdir -p "${BUILDDIR}/boot/grub"
-
-               if [ ! -z "${OPTINITRD}" ]; then
-                       GRUB_CFG_INITRD="module /boot/${OPTINITRDNAME}"
-                       cp "${OPTINITRD}" "${BUILDDIR}/boot/${OPTINITRDNAME}" || exit 1
-               fi
-               cp "${OPTKERNELIMG}" "${BUILDDIR}/boot/" || exit 1
-
-               cat >"${BUILDDIR}/boot/grub/grub.cfg" <<EOF
-set default=0
-set timeout=0
-
-menuentry "${OPTKERNELNAME}" {
-       multiboot /boot/${OPTKERNELNAME} /${OPTKERNELNAME} ${OPTCMDLINE}
-       ${GRUB_CFG_INITRD}
-       boot
-}
-EOF
-
-               # build grub image
-               GRUB_INSTALL_MSGS=$( grub-mkrescue -o "$1" --install-modules="multiboot" --fonts="" --themes="" --locales="" "${BUILDDIR}/" 2>&1 )
-               if [ $? != 0 ]; then
-                       printf '%s\n' "$GRUB_INSTALL_MSGS" 1>&2
-                       exit 1
-               fi
+               mkgrubiso
                ;;
        *)
                echo -e "\"${OPTFORMAT}\" not a supported format." 1>&2