Skip to content

Instantly share code, notes, and snippets.

@disaac
Last active November 16, 2021 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save disaac/b64184b7b64de5f7b36545672b16bc60 to your computer and use it in GitHub Desktop.
Save disaac/b64184b7b64de5f7b36545672b16bc60 to your computer and use it in GitHub Desktop.
isomnt - mount iso files easily on macos 10.13 and greater
#!/usr/bin/env bash
function initVars () {
ENVFILE=${ISOMNT_ENVFILE:-"${HOME}/bin/.isomnt_env"}
if [[ -s "${ENVFILE}" ]]; then
# Import all the environment variables for envfile
source "${ENVFILE}"
fi
MNT_BASE_DIR=${MNT_BASE_DIR:-/var/tmp}
MNT_SUB_DIR=${MNT_SUB_DIR:-iso_mnt}
MNT_DIR_SUFFIX=${MNT_DIR_SUFFIX:-$(date +"%Y%m%H%M%S")}
MNT_DIR_PATH="${MNT_BASE_DIR}/${MNT_SUB_DIR}_${MNT_DIR_SUFFIX}"
KEXT_CD9660=${KEXT_CD9660:-"/System/Library/Extensions/cd9660.kext"}
ISO_FILE="${ISO_FILE:-""}"
}
function checkDeps () {
local binDeps
binDeps=(
date
hdiutil
mount
find
grep
awk
)
for bin in "${binDeps[@]}"; do
command -v "${bin}" &>/dev/null || { echo >&2 "Required binary ${bin} not found exiting."; exit 1; }
done
}
function hdAttach () {
DEV_DISK="$(hdiutil attach -nomount "${ISO_FILE}" | awk '{print $1}')"
}
function mntIso () {
mkdir -p "${MNT_DIR_PATH}"
if mount -t cd9660 "${DEV_DISK}" "${MNT_DIR_PATH}"; then
echo "Mounted ISO: ${ISO_FILE} DISK: ${DEV_DISK} PATH: ${MNT_DIR_PATH}"
else
echo "Failed to Mount ISO: ${ISO_FILE} DISK: ${DEV_DISK} PATH: ${MNT_DIR_PATH}"
fi
}
function manageKext () {
local cmd="${1}"
sudo kmutil ${cmd} -p ${KEXT_CD9660}
}
function umntIso () {
OG_MNT_DIR_PATH=$(mount | grep "${DEV_DISK}" | awk '{ print $3}')
if umount "${DEV_DISK}" && sleep 3 && hdiutil detach "${DEV_DISK}"; then
echo "Unmounted ISO: ${ISO_FILE} DISK: ${DEV_DISK} PATH: ${OG_MNT_DIR_PATH}"
echo "removing empty directory: ${OG_MNT_DIR_PATH}"
find "${OG_MNT_DIR_PATH}" -maxdepth 0 -empty -exec rm -rf {} \;
echo "Unloading Kext: ${KEXT_CD9660}"
manageKext "unload"
else
echo "Failed to Unmount ISO: ${ISO_FILE} DISK: ${DEV_DISK} PATH: ${OG_MNT_DIR_PATH}"
fi
}
function main () {
local cmd
if [[ $# -eq 2 ]] && [[ -f "${2}" ]]; then
initVars
checkDeps
cmd="$1"
ISO_FILE="${2}"
else
echo "Usage:"
echo "${0} mount|umount /path/to/isofile.iso"
echo "arg1=${1} arg2=${2}"
exit 1
fi
if [[ "${cmd}" == "mount" ]] || [[ "${cmd}" == "umount" ]]; then
manageKext "load"
hdAttach
[[ ! -b "${DEV_DISK}" ]] && { echo "${DEV_DISK} not a block device"; exit 1; }
[[ "${cmd}" == "mount" ]] && mntIso
[[ "${cmd}" == "umount" ]] && umntIso
fi
}
main "$@"
@disaac
Copy link
Author

disaac commented Nov 16, 2021

# Mount 
isomnt mount ~/Downloads/dd-sata_sil-2.4-2.el8_1.elrepo.iso
# Unmunt
isomnt umount ~/Downloads/dd-sata_sil-2.4-2.el8_1.elrepo.iso

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment