-
Notifications
You must be signed in to change notification settings - Fork 61
/
image-check
82 lines (79 loc) · 1.5 KB
/
image-check
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
#!/bin/bash
errexit()
{
echo ""
echo "$1"
echo ""
exit 1
}
usage()
{
errexit "Usage: $0 imagefile [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"
PART="$2"
if [ "${IMGFILE}" = "" ]; then
usage
fi
if [ ! -f "${IMGFILE}" ]; then
errexit "${IMGFILE} not found"
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 "Check ${IMGFILE} (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
FS_TYPE=$(blkid "${LOOP}p${PART}" | sed -n 's|^.*TYPE="\(\S\+\)".*|\1|p')
if [ "${PART}" = "1" ]; then
dosfsck "${LOOP}p${PART}"
else
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
if [ "${FS_TYPE}" = "ext4" ]; then
echo ""
INFO="$(tune2fs -l "${LOOP}p${PART}" 2>/dev/null)"
echo "${INFO}"
fi
losetup -d "${LOOP}"
echo ""