diff options
author | Sven Gothel <[email protected]> | 2021-07-02 06:22:40 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-07-02 06:22:40 +0200 |
commit | 3ddd80ce13e5c57b7b3c87ea970905b9f94f40b6 (patch) | |
tree | 1d62c0f921f45e0f30dc37368f0d381451a7e92c /imagetool.sh | |
parent | f4940a8b73e8a3ec50354c62a00460dc74690d9a (diff) |
imagetool.sh: Easier handling for mount and umount, use implicit image and mountpoint arguments; find NBD_DEV for umount
Diffstat (limited to 'imagetool.sh')
-rwxr-xr-x | imagetool.sh | 106 |
1 files changed, 87 insertions, 19 deletions
diff --git a/imagetool.sh b/imagetool.sh index 7ec0f03..9247ea0 100755 --- a/imagetool.sh +++ b/imagetool.sh @@ -12,8 +12,9 @@ function usage() cat << HEREDOC Usage: - Mount Image : $progname [--mount] [-i <path to qcow2 image>] [-p <mount point>] - Umount Image: $progname [--umount] [-n <nbd-device>] [-p <mount point>] + Mount Image : $progname [-m|--mount] <path to qcow2 image> <mount point> + Mount Image : $progname [-r|--mount-raw] <path to raw image> <mount point> + Umount Image: $progname [-u|--umount] [-n <nbd-device>] <mount point> Cleanup NBD : $progname [--cleanup] arguments: @@ -23,8 +24,6 @@ Usage: -m, --mount mount image -r, --mount-raw mount raw image -u, --umount umount image - -i, --image-name path to qcow2 image - -p, --mount-point mount point for image -n, --nbd-device nbd device ./$progname --mount --image <your image> --mount-point <your path> @@ -33,8 +32,8 @@ HEREDOC } MOUNT=0 -UMOUNT=0 RAW_IMAGE=0 +UMOUNT=0 IMAGE="" MOUNTPOINT="" NBD_DEV= @@ -69,19 +68,45 @@ force_nbd_cleanup() { fi } +find_nbd() { + mntpoint="${1}" + + MAIN_DEVS="$(lsblk | grep nbd | grep disk | cut -d" " -f1)" + if [ ! -z "${MAIN_DEVS}" ]; then + for md in $MAIN_DEVS; do + if [ ! -z "${md}" ]; then + #SUB_DEVS="$(lsblk -l "/dev/${md}" | grep "${md}p" | cut -d" " -f1)" + if lsblk -l "/dev/${md}" | grep "${mntpoint}" >& /dev/null ; then + echo "/dev/${md}" + return 0; + fi + fi + done + fi + return 1; +} + +error_exit() { + echo "$@" + usage + exit +} + # As long as there is at least one more argument, keep looping while [[ $# -gt 0 ]]; do key="$1" case "$key" in -h|--help) usage - exit + exit ;; -c|--cleanup) nbd_cleanup + exit ;; -C|--force-cleanup) force_nbd_cleanup + exit ;; -r|--mount-raw) MOUNT=1 @@ -93,22 +118,28 @@ while [[ $# -gt 0 ]]; do -u|--umount) UMOUNT=1 ;; - -i|--image-name) - shift - IMAGE="$1" - ;; - -p|--mount-point) - shift - MOUNTPOINT="$1" - ;; -n|--nbd-device) shift NBD_DEV="$1" ;; *) - echo "Unknown option '$key'" - usage - exit + if [ $MOUNT -eq 1 ]; then + if [ -z "$IMAGE" ]; then + IMAGE=$1 + elif [ -z "$MOUNTPOINT" ]; then + MOUNTPOINT=$1 + else + error_exit "Unknown mount option '$key'" + fi + elif [ $UMOUNT -eq 1 ]; then + if [ -z "$MOUNTPOINT" ]; then + MOUNTPOINT=$1 + else + error_exit "Unknown unmount option '$key'" + fi + else + error_exit "Unknown option '$key'" + fi ;; esac # Shift after checking all the cases to get the next option @@ -133,6 +164,40 @@ if [ "${UMOUNT}" = "1" ] && [ -z "${MOUNTPOINT}" ]; then exit fi +if [ -n "${MOUNTPOINT}" -a ! -d "${MOUNTPOINT}" ]; then + echo "Mountpoint ${MOUNTPOINT} not existing." + exit 1 +fi + +if [ -n "${IMAGE}" -a ! -f "${IMAGE}" ]; then + echo "Image ${IMAGE} not existing." + exit 1 +fi + +source scripts/qcow2_handling + +if [ "${MOUNT}" = "1" ]; then + if [ "${RAW_IMAGE}" = "1" ]; then + mount_rawimage "${IMAGE}" "${MOUNTPOINT}" + else + mount_qimage "${IMAGE}" "${MOUNTPOINT}" + fi + echo Using NBD_DEV $NBD_DEV +elif [ "${UMOUNT}" = "1" ]; then + if [ -z "$NBD_DEV" ] ; then + NBD_DEV=$(find_nbd ${MOUNTPOINT}) + if [ -z "${NBD_DEV}" ] ; then + echo "umount: NBD_DEV not set and not found for ${MOUNTPOINT}. Exit." + exit 1; + fi + fi + umount_image "${MOUNTPOINT}" +fi +if [ ! -d "${MOUNTPOINT}" ]; then + echo "Mountpoint ${MOUNTPOINT} not existing." + exit 1 +fi + source scripts/qcow2_handling if [ "${MOUNT}" = "1" ]; then @@ -144,8 +209,11 @@ if [ "${MOUNT}" = "1" ]; then echo Using NBD_DEV $NBD_DEV elif [ "${UMOUNT}" = "1" ]; then if [ -z "$NBD_DEV" ] ; then - echo "umount: NBD_DEV not set. Exit." - exit 1; + NBD_DEV=$(find_nbd ${MOUNTPOINT}) + if [ -z "${NBD_DEV}" ] ; then + echo "umount: NBD_DEV not set and not found for ${MOUNTPOINT}. Exit." + exit 1; + fi fi umount_image "${MOUNTPOINT}" fi |