forked from rwbaumg/admin-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount-image.sh
executable file
·74 lines (60 loc) · 1.8 KB
/
mount-image.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# Mounts an image to the specified mountpoint
hash mountpoint 2>/dev/null || { echo >&2 "'mountpoint' command not found; you need to install initscripts package. Aborting."; exit 1; }
hash mount 2>/dev/null || { echo >&2 "You need to install 'mount'. Aborting."; exit 1; }
if [ $# -lt 1 ]; then
echo "Usage: $0 <image> [mountpoint]"
echo "Note: Mountpoint defaults to /mnt if not specified."
exit 1
fi
IMAGE="$1"
MOUNT_POINT="/mnt"
MOUNT_RW="false"
MOUNT_OPTS="loop"
# check if image exists
if [ ! -e "$IMAGE" ]; then
echo >&2 "'$IMAGE' does not exist; aborting."
exit 1
fi
# verify image is a regular file
if [ ! -f "$IMAGE" ]; then
echo >&2 "'$IMAGE' is not a regular file; aborting."
exit 1
fi
# test image to ensure it can be mounted
# todo: support additional image types
MOUNT_TYPE="iso9660"
IMAGE_TYPE=$(file -i "$IMAGE" | awk -F" " '{ printf "%s\n", $2 }' | sed 's/application\///g' | sed 's/;//g')
if [ "$IMAGE_TYPE" != "x-iso9660-image" ]; then
echo >&2 "'$IMAGE' is not a valid ISO9660 image file; aborting."
exit 1
fi
if [ -n "$2" ]; then
# user-specified mountpoint
MOUNT_POINT="$2"
fi
# check if mountpoint exists
if [ ! -d "$MOUNT_POINT" ]; then
echo >&2 "'$MOUNT_POINT' does not exist; aborting."
exit 1
fi
# check if mountpoint is in use
if mountpoint -q "$MOUNT_POINT" > /dev/null 2>&1; then
echo >&2 "'$MOUNT_POINT' is already in use; aborting."
exit 1
fi
# check if mountpoint is empty
if [ -z "$MOUNT_POINT" ]; then
echo >&2 "'$MOUNT_POINT' is not empty; aborting."
exit 1
fi
# decide whether or not image should be mounted r/o
if [ "$MOUNT_RW" == "true" ]; then
MOUNT_OPTS=$MOUNT_OPTS",rw"
else
MOUNT_OPTS=$MOUNT_OPTS",ro"
fi
# mount the image
echo "Mounting $IMAGE to $MOUNT_POINT ..."
mount -t $MOUNT_TYPE -o $MOUNT_OPTS "$IMAGE" "$MOUNT_POINT"
exit $?