From: Sergiu Moga Date: Sat, 20 May 2023 14:59:30 +0000 (+0300) Subject: support/scripts/mkgrubimg: Move `GRUB ISO` image making into method X-Git-Tag: RELEASE-0.14.0~67 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=393027f8af0a7bc9ddf4079e028e1e3539dbcbb6;p=unikraft%2Funikraft.git support/scripts/mkgrubimg: Move `GRUB ISO` image making into method 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 Reviewed-by: Stefan Jumarea Approved-by: Razvan Deaconescu Tested-by: Unikraft CI GitHub-Closes: #910 --- diff --git a/support/scripts/mkgrubimg b/support/scripts/mkgrubimg index 87aed7410..9ec4aca00 100755 --- a/support/scripts/mkgrubimg +++ b/support/scripts/mkgrubimg @@ -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" <&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" <&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