diff options
author | Sven Gothel <[email protected]> | 2021-07-01 06:22:45 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-07-01 06:22:45 +0200 |
commit | f1e61a9052cb96a9833f7e54d2b501e75a474a19 (patch) | |
tree | b56b0cf4aa714a6bf10a126463ce641871492b75 | |
parent | 81dc51e171a542f2534e9a3cb8c9bfbe6245a83b (diff) |
qcow2_handling: Add more robustness, make imagetool.sh more usable (add raw-image) and don't set NBD_DEV (bugfix)
-rwxr-xr-x | imagetool.sh | 31 | ||||
-rw-r--r-- | scripts/qcow2_handling | 60 |
2 files changed, 81 insertions, 10 deletions
diff --git a/imagetool.sh b/imagetool.sh index fafbade..7ec0f03 100755 --- a/imagetool.sh +++ b/imagetool.sh @@ -12,8 +12,8 @@ function usage() cat << HEREDOC Usage: - Mount Image : $progname [--mount] [--image-name <path to qcow2 image>] [--mount-point <mount point>] - Umount Image: $progname [--umount] [--mount-point <mount point>] + Mount Image : $progname [--mount] [-i <path to qcow2 image>] [-p <mount point>] + Umount Image: $progname [--umount] [-n <nbd-device>] [-p <mount point>] Cleanup NBD : $progname [--cleanup] arguments: @@ -21,20 +21,23 @@ Usage: -c, --cleanup cleanup orphaned device mappings -C, --force-cleanup forcefully cleanup orphaned or still connected device mappings -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 - This tool will use /dev/nbd1 as default for mounting an image. If you want to use another device, execute like this: - NBD_DEV=/dev/nbd2 ./$progname --mount --image <your image> --mount-point <your path> + ./$progname --mount --image <your image> --mount-point <your path> HEREDOC } MOUNT=0 UMOUNT=0 +RAW_IMAGE=0 IMAGE="" MOUNTPOINT="" +NBD_DEV= nbd_cleanup() { DEVS="$(lsblk | grep nbd | grep disk | cut -d" " -f1)" @@ -80,6 +83,10 @@ while [[ $# -gt 0 ]]; do -C|--force-cleanup) force_nbd_cleanup ;; + -r|--mount-raw) + MOUNT=1 + RAW_IMAGE=1 + ;; -m|--mount) MOUNT=1 ;; @@ -94,6 +101,10 @@ while [[ $# -gt 0 ]]; do shift MOUNTPOINT="$1" ;; + -n|--nbd-device) + shift + NBD_DEV="$1" + ;; *) echo "Unknown option '$key'" usage @@ -122,11 +133,19 @@ if [ "${UMOUNT}" = "1" ] && [ -z "${MOUNTPOINT}" ]; then exit fi -export NBD_DEV="${NBD_DEV:-/dev/nbd1}" source scripts/qcow2_handling if [ "${MOUNT}" = "1" ]; then - mount_qimage "${MOUNTPOINT}" "${IMAGE}" + 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 + echo "umount: NBD_DEV not set. Exit." + exit 1; + fi umount_image "${MOUNTPOINT}" fi diff --git a/scripts/qcow2_handling b/scripts/qcow2_handling index 4252a16..d210c3b 100644 --- a/scripts/qcow2_handling +++ b/scripts/qcow2_handling @@ -39,6 +39,10 @@ export -f init_nbd # connect image with known format to block device connect_blkdev() { init_nbd + if [ -z "$NBD_DEV" ] ; then + echo "connect_blkdev: NBD_DEV not determined. Exit." + exit 1; + fi qemu-nbd --discard=unmap -c $NBD_DEV "$1" sync kpartx -a $NBD_DEV @@ -50,6 +54,10 @@ export -f connect_blkdev # connect raw image to block device connect_raw_blkdev() { init_nbd + if [ -z "$NBD_DEV" ] ; then + echo "connect_raw_blkdev: NBD_DEV not determined. Exit." + exit 1; + fi qemu-nbd --discard=unmap -f raw -c $NBD_DEV "$1" sync kpartx -a $NBD_DEV @@ -60,6 +68,10 @@ export -f connect_raw_blkdev # disconnect image from block device disconnect_blkdev() { + if [ -z "$NBD_DEV" ] ; then + echo "disconnect_blkdev: NBD_DEV not set. Exit." + exit 1; + fi kpartx -d $NBD_DEV qemu-nbd -d $NBD_DEV NBD_DEV= @@ -73,11 +85,29 @@ export -f disconnect_blkdev # mount qcow2 image: mount_image <image file> <mountpoint> mount_qimage() { + if [ -z "$1" ] ; then + echo "mount_qimage: image-file not given. Exit." + exit 1; + fi + if [ -z "$2" ] ; then + echo "mount_qimage: mountpoint not given. Exit." + exit 1; + fi + connect_blkdev "$1" + + if [ -z "$NBD_DEV" ] ; then + echo "mount_qimage: NBD_DEV not determined. Exit." + exit 1; + fi + if [ -z "$MAP_ROOT_DEV" -o -z "$MAP_BOOT_DEV" -o -z "$MAP_DATA_DEV" ] ; then + echo "mount_qimage: map devices not determined. Exit." + exit 1; + fi mount -v -t ext4 $MAP_ROOT_DEV "$2" - mkdir -p "${ROOTFS_DIR}/boot" + mkdir -p "$2/boot" mount -v -t vfat $MAP_BOOT_DEV "$2/boot" - mkdir -p "${ROOTFS_DIR}/data" + mkdir -p "$2/data" mount -v -t ext4 $MAP_DATA_DEV "$2/data" CURRENT_MOUNTPOINT="$2" } @@ -85,11 +115,29 @@ export -f mount_qimage # mount raw image: mount_image <image file> <mountpoint> mount_rawimage() { + if [ -z "$1" ] ; then + echo "mount_rawimage: image-file not given. Exit." + exit 1; + fi + if [ -z "$2" ] ; then + echo "mount_rawimage: mountpoint not given. Exit." + exit 1; + fi + connect_raw_blkdev "$1" + + if [ -z "$NBD_DEV" ] ; then + echo "mount_rawimage: NBD_DEV not determined. Exit." + exit 1; + fi + if [ -z "$MAP_ROOT_DEV" -o -z "$MAP_BOOT_DEV" -o -z "$MAP_DATA_DEV" ] ; then + echo "mount_rawimage: map devices not determined. Exit." + exit 1; + fi mount -v -t ext4 $MAP_ROOT_DEV "$2" - mkdir -p "${ROOTFS_DIR}/boot" + mkdir -p "$2/boot" mount -v -t vfat $MAP_BOOT_DEV "$2/boot" - mkdir -p "${ROOTFS_DIR}/data" + mkdir -p "$2/data" mount -v -t ext4 $MAP_DATA_DEV "$2/data" CURRENT_MOUNTPOINT="$2" } @@ -98,6 +146,10 @@ export -f mount_rawimage # umount qcow2 or raw image: umount_image <current mountpoint> umount_image() { sync + if [ -z "$1" ] ; then + echo "umount_image: mountpoint not given. Exit." + exit 1; + fi #umount "$1/boot" #umount "$1/data" while mount | grep -q "$1"; do |