-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmount_image.sh
More file actions
executable file
·152 lines (128 loc) · 3.32 KB
/
mount_image.sh
File metadata and controls
executable file
·152 lines (128 loc) · 3.32 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env bash
#
# Mount a disk img(.img) See help below
#
# error codes 0:Success 1:Operations Fail 2:Abort 4:Invalid User Input
PART_N=1
MOUNT_POINT="${HOME}/mnt"
ROOT_METHOD="sudo"
help_and_exit(){
cat 1>&2 << EOF
mount_image.sh:
Mount and dismount raw disk images. assumes a single parition in the
file. n is the number of the loop device, as shown in list. An image
needs to be a paritioned disk image.
Default mountpoint is $HOME/mnt. see -m option below
USAGE: mount_image.sh <-switches> [command] [file.img|n]
COMMANDS: mount umount list
mount: mount_image.sh mount <filename.img>
umount: mount_image.sh umount <n>
list: list mounted images. gives Numbers <n> to use with umount
SWITCHES:
-m, --mount-point Specificy directory to use as mountpoint,
default is $HOME/mnt
EOF
exit 4
}
message(){
echo "mount_image.sh: ${@}"
}
exit_with_error(){
echo 1>&2 "mount_image: ERROR: ${2}"
exit ${1}
}
as_root() {
# execute a command as root.
case $ROOT_METHOD in
sudo)
sudo ${@}
;;
pkexec)
pkexec ${@}
;;
uid)
${@}
;;
esac
}
_mount-img() {
local -i local_exit=0
local filename="${1}"
local loop_dev=$(as_root losetup -f)
message "Mounting ${filename} on ${loop_dev} on ${MOUNT_POINT}"
[ -z "${MOUNT_POINT}" ] && exit_with_error 4 "No mountpoint specified with -m see --help"
[ -d "${MOUNT_POINT}" ] || exit_with_error 4 "${MOUNT_POINT} is not a directory, exiting!"
as_root losetup -P ${loop_dev} "${filename}" || local_exit+=1
as_root mount ${loop_dev}p${PART_N} ${MOUNT_POINT} || local_exit+=1
return ${local_exit}
}
_umount-img() {
local -i local_exit=0
index=${1}
local loop_dev=/dev/loop${index}
local loop_mount="$(cat /proc/self/mounts | grep ${loop_dev}p${PART_N} | cut -d ' ' -f 2 )"
message "UnMounting ${MOUNT_POINT} from ${loop_dev}"
#as_root umount -Rf ${loop_dev}p${PART_N} || local_exit+=1
as_root umount -Rf ${loop_mount} || local_exit+=1
as_root losetup -d ${loop_dev} || local_exit+=1
return ${local_exit}
}
_list_mounts() {
local -i local_exit=0
local loop_names=( $(losetup -a |cut -d ":" -f 1) )
local loop_imgs=( $(losetup -a |cut -d ":" -f 3) )
local -i item=0
local n=${#loop_names[@]}
# reduce loop device names to numbers
for ((i=0; $i < $n ; i+=1));do
j=$(( ${#loop_names[$i]} - 1))
loop_names[${i}]=${loop_names[${i}]:${j}:1}
done
message "Mounted Images:"
local -i item=0
local n=${#loop_imgs[@]}
for ((i=0; $i < $n ; i+=1));do
echo "${loop_names[$i]} : ${loop_imgs[$i]}"
done
}
switch_checker() {
PARMS=""
while [ ! -z "${1}" ];do
case "$1" in
-\?|--help)
help_and_exit
;;
-m|--mount-point)
MOUNT_POINT="${2}"
shift
;;
*)
PARMS+="${1} "
;;
esac
shift
done
}
main() {
local command="${1}"
case ${command} in
mount)
local filename="${2}"
[ -z ${filename} ] && help_and_exit
_mount-img "${filename}" || exit_with_error 1 "Could not mount ${filename}"
;;
umount)
local -i loop_index=${2}
[ -z ${loop_index} ] && help_and_exit
_umount-img ${loop_index} || exit_with_error 1 "Could Not unmount ${MOUNT_POINT}"
;;
list)
_list_mounts || exit_with_error 1 "Couldn't list mounts???"
;;
*)
help_and_exit
;;
esac
}
switch_checker "${@}"
main ${PARMS}