diff --git a/scripts/tripleo-mount-image b/scripts/tripleo-mount-image index 1790fac85..d388312b8 100755 --- a/scripts/tripleo-mount-image +++ b/scripts/tripleo-mount-image @@ -21,10 +21,10 @@ set -eu set -o pipefail SCRIPT_NAME=$(basename $0) -NBD_DEVICE=/dev/nbd0 +NBD_DEVICE="" IMAGE_FILE="" MOUNT_DIR="" -if [ ! -a "${NBD_DEVICE}" ]; then +if [ ! -a "/dev/nbd0" ]; then modprobe nbd fi @@ -47,8 +47,8 @@ mount_show_options() { echo " -h, --help -- print this help." echo " -a -- Image file to mount." echo " -m -- Directory to mount image to." - echo " -n -- NBD device to use." - echo " Defaults to /dev/nbd0" + echo " -n -- NBD device to use (example, /dev/nbd0)." + echo " Defaults to first available" echo echo "Mount an overcloud image to a directory" echo @@ -61,8 +61,8 @@ unmount_show_options() { echo "Options:" echo " -h, --help -- print this help." echo " -m -- Directory to unmount." - echo " -n -- NBD device to disconnect." - echo " Defaults to /dev/nbd0" + echo " -n -- NBD device to disconnect (example, /dev/nbd0)." + echo " Defaults to detected device from mounted directory" echo echo "Unmount a mounted overcloud image" echo @@ -117,7 +117,9 @@ mount_image() { # wait for any sub-devices to appear timeout 5 sh -c "while ! ls ${NBD_DEVICE}p* ; do sleep 1; done" || true - devices=$(ls -1 ${NBD_DEVICE}p*) + set +e + devices=$(ls ${NBD_DEVICE}p*) + set -e device_count=$(echo $devices | wc -w) if [ $device_count == "0" ]; then # if there are no partition devices, assume one root device @@ -239,6 +241,22 @@ if [ $SCRIPT_NAME == "tripleo-unmount-image" ]; then if [ -z "${MOUNT_DIR}" ]; then unmount_show_options 1 fi + MOUNT_DIR=$(realpath ${MOUNT_DIR}) + if [ -z "${NBD_DEVICE}" ]; then + for i in {0..15} ; do + device="/dev/nbd${i}" + mountpoints=$(lsblk --noheadings --output MOUNTPOINT $device) + if [[ $mountpoints =~ "$MOUNT_DIR" ]]; then + NBD_DEVICE="$device" + break + fi + done + fi + if [ -z "${NBD_DEVICE}" ]; then + echo "NBD device could not be detected from existing mounts." + echo "Specify a device with -n if an unmount is really required" + exit 1 + fi unmount_image else TEMP=`getopt -o ha:m:n: -l help -n $SCRIPT_NAME -- "$@"` @@ -262,5 +280,24 @@ else if [ -z "${MOUNT_DIR}" ] || [ -z "${IMAGE_FILE}" ]; then mount_show_options 1 fi + MOUNT_DIR=$(realpath ${MOUNT_DIR}) + if mountpoint "${MOUNT_DIR}"; then + echo "${MOUNT_DIR} is already a mountpoint, unmount it or specify a different path" + mount_show_options 1 + fi + if [ -z "${NBD_DEVICE}" ]; then + for i in {0..15} ; do + device="/dev/nbd${i}" + part_type=$(lsblk --nodeps --noheadings --output PTTYPE $device) + if [ -z "${part_type}" ]; then + NBD_DEVICE="$device" + break + fi + done + if [ -z "${NBD_DEVICE}" ]; then + echo "No NBD device is available" + exit 1 + fi + fi mount_image fi \ No newline at end of file