forked from seamusdemora/RonR-RPi-image-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-mount
87 lines (84 loc) · 1.63 KB
/
image-mount
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
#!/bin/bash
errexit()
{
echo ""
echo "$1"
echo ""
exit 1
}
usage()
{
errexit "Usage: $0 imagefile mountpoint [W95|Linux]"
}
if [ $(id -u) -ne 0 ]; then
errexit "$0 must be run as root user"
fi
PGMNAME="$(basename $0)"
for PID in $(pidof -x -o %PPID "${PGMNAME}"); do
if [ ${PID} -ne $$ ]; then
errexit "${PGMNAME} is already running"
fi
done
IMGFILE="$1"
MNT="$2"
PART="$3"
if [[ "${IMGFILE}" = "" || "${MNT}" = "" ]]; then
usage
fi
if [ ! -f "${IMGFILE}" ]; then
errexit "${IMGFILE} not found"
fi
if [ ! -d "${MNT}" ]; then
errexit "${MNT} is not a directory"
fi
if [ "${PART}" = "" ]; then
PART="2"
else
PART="$(tr [A-Z] [a-z] <<< "${PART}")"
if [ "${PART}" = "w95" ];then
PART="1"
elif [ "${PART}" = "linux" ];then
PART="2"
else
usage
fi
fi
echo ""
echo -n "Mount ${IMGFILE} on ${MNT} (y/n)? "
while read -r -n 1 -s answer; do
if [[ "${answer}" = [yYnN] ]]; then
echo "${answer}"
if [[ "${answer}" = [yY] ]]; then
break
else
errexit "Aborted"
fi
fi
done
echo ""
LOOP="$(losetup -f --show -P "${IMGFILE}")"
if [ $? -ne 0 ]; then
errexit "Unable to create loop device"
fi
if [ "${PART}" = "1" ]; then
dosfsck "${LOOP}p${PART}"
else
FS_TYPE=$(blkid "${LOOP}p${PART}" | sed -n 's|^.*TYPE="\(\S\+\)".*|\1|p')
if [ "${FS_TYPE}" = "f2fs" ]; then
fsck.f2fs "${LOOP}p${PART}"
else
fsck.ext4 -f "${LOOP}p${PART}"
fi
fi
if [ $? -ne 0 ]; then
losetup -d "${LOOP}"
errexit "Filesystem appears corrupted"
fi
mount "${LOOP}p${PART}" ${MNT}
echo ""
echo "${IMGFILE} mounted on ${MNT}"
echo ""
echo "When done, run:"
echo "umount ${MNT}"
echo "losetup -d ${LOOP}"
echo ""