-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmount-img
executable file
·202 lines (175 loc) · 4.33 KB
/
mount-img
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
#
# SPDX-License-Identifier: Apache-2.0
#
# Name:
# Mount image utility
#
# Authors:
# 2017 Zack YL Shih <[email protected]>
# 2018 Fero JD Zhou <[email protected]>
# 2019 Ken CJ Chou <[email protected]>
#
set -e
VERSION=1.3.0
usage() {
echo -e "Usage:"
echo -e " Mount image"
echo -e " # ${0} m|mount <image_file> <mount_dir>"
echo -e " Unmount image"
echo -e " # ${0} u|umount <mount_dir>"
echo -e ""
echo -e "Parameters:"
echo -e " image_file"
echo -e " A image format file, usually with \".img\" file extension"
echo -e ""
echo -e " mount_dir"
echo -e " The path of directory for the image to be mounted"
echo -e ""
echo -e "Examples:"
echo -e " Mount an image file named \"example.img\" at \"/tmp/mnt\""
echo -e " # ./mount-img mount example.img /tmp/mnt"
echo -e ""
echo -e " Unmount the image which mounted at \"/tmp/mnt\""
echo -e " # ./mount-img umount /tmp/mnt"
echo -e ""
return 0
}
get_value() {
local tmp
tmp="$(echo "${1}" | grep "^${2}=")"
echo "${tmp##*=}"
return 0
}
get_image_info() {
local imgfile=${1}
local fdisk_l_output sec_size num_part part_info
local line count tmp
echo "IMAGE_FILE=${imgfile}"
echo "IMAGE_FILENAME=${imgfile##*/}"
fdisk_l_output="$(fdisk -l ${imgfile})"
# get sector size
sec_size="$(echo "${fdisk_l_output}" | grep "^Units:")"
sec_size="${sec_size##*= }"
sec_size="${sec_size%% bytes}"
echo "IMAGE_SECTOR_SIZE=${sec_size}"
# get partition information
part_info="$(echo "${fdisk_l_output}" | grep "^${imgfile}[0-9]")"
num_part=$(echo "${part_info}" | wc -l)
echo "IMAGE_NUM_OF_PARTITIONS=${num_part}"
count=0
while read -r line; do
count=$((count + 1))
# bootflag is set if the line contains '*'
if echo "${line}" | grep -q "*"; then
line="$(echo "${line}" | sed 's/*//')"
echo "IMAGE_P${count}_BOOTFLAG=y"
else
echo "IMAGE_P${count}_BOOTFLAG=n"
fi
tmp="$(echo "${line}" | awk '{print $1}')"
echo "IMAGE_P${count}_NUMBER=${tmp##*${imgfile}}"
tmp="$(echo "${line}" | awk '{print $2}')"
echo "IMAGE_P${count}_OFFSET=$((tmp * sec_size))"
tmp="$(echo "${line}" | awk '{print $4}')"
echo "IMAGE_P${count}_SIZE=$((tmp * sec_size))"
tmp="$(echo "${line}" | awk '{print $6}')"
echo "IMAGE_P${count}_FSTYPE=${tmp}"
done <<< "${part_info}"
return 0
}
get_device_for_mountpoint() {
local mnt_point=${1}
local src tgt
while read -r src tgt; do
if [ "${tgt}" == "${mnt_point}" ]; then
echo "${src}"
return 0
fi
done <<< "$(df --output=source,target)"
echo "Error: \"${mnt_point}\" is not a mount point"
return 1
}
mount_img() {
local img=${1}
local mnt_dir=${2}
local img_info i n_part
if ! [ -f "${img}" ]; then
echo "image file not found: ${img}" >&2
exit 2
fi
img_info="$(get_image_info "${img}")"
mkdir -p ${mnt_dir}
if [ -n "$(ls -A ${mnt_dir})" ]; then
echo "Error: target directory is not empty" >&2
exit 2
fi
n_part=$(get_value "${img_info}" "IMAGE_NUM_OF_PARTITIONS")
for i in $(seq 1 ${n_part}); do
local p_no=$(get_value "${img_info}" "IMAGE_P${i}_NUMBER")
local p_offset=$(get_value "${img_info}" "IMAGE_P${i}_OFFSET")
local p_size=$(get_value "${img_info}" "IMAGE_P${i}_SIZE")
local mnt_point=${mnt_dir}/p${p_no}
local loopdev
loopdev="$(losetup -f)"
losetup -o ${p_offset} --sizelimit ${p_size} ${loopdev} ${img}
# do nothing for Empty disk type
if [ "${part_type}" != "0" ]; then
mkdir -p "${mnt_point}"
mount "${loopdev}" "${mnt_point}" || true
fi
done
return 0
}
unmount_img() {
local mnt_dir=${1}
local part_mnt loopdev
if ! [ -d "${mnt_dir}" ]; then
echo "directory not exist: ${mnt_dir}" >&2
exit 2
fi
mnt_dir="$(realpath ${mnt_dir})"
for part_mnt in ${mnt_dir}/*; do
if mountpoint -q ${part_mnt}; then
loopdev="$(get_device_for_mountpoint "${part_mnt}")"
umount "${part_mnt}" || true
losetup -d ${loopdev}
else
echo "Warning: a non-mountpoint found \"${part_mnt}\"" >&2
fi
done
rm -rf ${mnt_dir}
return 0
}
main() {
case "${1}" in
"-v"|"--version")
echo ${VERSION}
;;
"m"|"mount")
if [ $# -ne 3 ]; then
usage >&2
return 1
fi
mount_img "${2}" "${3}"
;;
"u"|"umount")
if [ $# -ne 2 ]; then
usage >&2
return 1
fi
unmount_img "${2}"
;;
*)
echo "Unknown action: \"${1}\"" >&2
usage >&2
return 1
;;
esac
}
if [ "${EUID}" -ne 0 ]; then
echo "Please run as root" >&2
exit 1
fi
main "$@"
exit 0