github.com/rancher/elemental/tests@v0.0.0-20240517125144-ae048c615b3f/scripts/get-boot-files-for-pxe (about)

     1  #!/bin/bash
     2  
     3  # This script extracts the kernel and initrd files from an ISO image
     4  
     5  set -e -x
     6  
     7  # Cleaning function
     8  function clean_and_exit() {
     9    typeset ERR_MSG="$@"
    10  
    11    sudo umount ${TMP_DIR} >/dev/null 2>&1 \
    12      || error "Cannot unmount ${TMP_DIR}!"
    13    rmdir ${TMP_DIR} \
    14      || error "Cannot delete ${TMP_DIR}!"
    15    [[ -n "${ERR_MSG}" ]] \
    16      && error "${ERR_MSG}"
    17  
    18    exit 0
    19  }
    20  
    21  # Error function
    22  function error() {
    23    echo -e "$@" >&2
    24    exit 1
    25  }
    26  
    27  # Variables
    28  typeset ISO_FILE=$1
    29  typeset FILENAME=${ISO_FILE%.iso}
    30  
    31  # ISO must be provided!
    32  [[ -z "${ISO_FILE}" ]] && error "ISO filename must be provided!"
    33  
    34  # Loop mount the ISO to get the file
    35  TMP_DIR=$(mktemp -d ${0##*/}.XXXXXXXXXX)
    36  sudo mount -o loop ${ISO_FILE} ${TMP_DIR} >/dev/null 2>&1 \
    37    || error "Cannot mount ISO file ${ISO_FILE}"
    38  
    39  # Extract initrd, kernel and squashfs
    40  for I in initrd linux squashfs; do
    41    # Squashfs is a little bit different
    42    [[ "${I}" == "squashfs" ]] && NAME="rootfs.squashfs" || NAME=${I}
    43  
    44    # Extract file
    45    find -L ${TMP_DIR} -name ${NAME} -exec sudo cp {} ${FILENAME}-${I} \;
    46  
    47    # Just check that each needed files are here and not empty
    48    if [[ ! -s "${FILENAME}-${I}" ]]; then
    49      clean_and_exit "File ${FILENAME}-${I} does not exist or is empty!"
    50    fi
    51  
    52    # Fix owner/permissions
    53    sudo chown $(id -u):$(id -g) ${FILENAME}-${I} \
    54      || error "Cannot change owner for ${FILENAME}-${I}!"
    55    chmod 644 ${FILENAME}-${I} \
    56      || error "Cannot change permissions for ${FILENAME}-${I}!"
    57  done
    58  
    59  # Clean all
    60  clean_and_exit