forked from mafintosh/mount-img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.sh
executable file
·73 lines (61 loc) · 1.59 KB
/
index.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
#!/usr/bin/env bash
PARTITION=""
FORCE=false
READ_ONLY=""
set_image_or_mnt () {
if [ "$IMAGE" == "" ]; then
IMAGE="$1"
else
MNT="$1"
fi
}
silent () {
"$@" 2>/dev/null >/dev/null
}
while [ "$1" != "" ]; do
case "$1" in
--dry-run) DRY_RUN=echo; shift ;;
--partition) PARTITION="$2"; shift; shift ;;
-p) PARTITION="$2"; shift; shift ;;
--force) FORCE=true; shift;;
-f) FORCE=true; shift;;
--read-only) READ_ONLY="--read-only"; shift;;
-r) READ_ONLY="--read-only"; shift;;
*) set_image_or_mnt "$1"; shift ;;
esac
done
if [ "$IMAGE" == "" ] || [ "$MNT" == "" ]; then
echo "Usage: mount-img <image> <mnt> [options]"
echo
echo " --partition, -p [partition-number]"
echo " --force, -f (force mount)"
echo " --read-only, -r (read-only mount)"
echo
exit 1
fi
if ! [ -f "$IMAGE" ]; then
echo "$IMAGE" is not a file
exit 2
fi
if ! [ -d "$MNT" ]; then
echo "$MNT" is not a directory
exit 2
fi
if $FORCE; then
$DRY_RUN silent sudo umount "$MNT"
fi
IFS=$'\n'
for line in $(fdisk -l "$IMAGE" | grep "$IMAGE$PARTITION" | grep -v "Disk $IMAGE"); do
OFFSET=$(echo $line | sed 's|\*||' | awk '{print $2}')
SECTORS=$(echo $line | sed 's|\*||' | awk '{print $4}')
break
done
if [ "$OFFSET" == "" ] && [ "$SECTORS" == "" ] && [ "$PARTITION" != "" ]; then
echo Could not find partition
exit 3
fi
if [ "$OFFSET" == "" ]; then
$DRY_RUN sudo mount $READ_ONLY -o loop "$IMAGE" "$MNT"
else
$DRY_RUN sudo mount $READ_ONLY -o loop,offset=$(($OFFSET * 512)),sizelimit=$(($SECTORS * 512)) "$IMAGE" "$MNT"
fi