-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashwiniso
116 lines (100 loc) · 3.09 KB
/
hashwiniso
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
#!/bin/bash
# you can get lastest Windows ISO here (use Firefox or Chrome, not Edge) :
# https://www.microsoft.com/en-us/software-download/windows10ISO
# you can retrieve old Windows ISO here (not a Microsoft site) :
# https://tb.rg-adguard.net/
# you can also have a look here :
# https://github.com/pbatard/Fido
# dependencies : except wimlib (for WIM files), all others binaries should
# be in your Linux
# as user, ISO image can be mounted for example with
# sudo mount file.iso /mount/point/
# or
# udisksctl loop-setup -f file.iso
set -eu
# binary hashing
# hash{space}{space}]filename is expected
hash_bin=sha1sum
#hash_bin=sha256sum
#hash_bin=sha512sum
# checks mount point argument
( [ "${1:-x}" == "x" ] || [ "${2:-x}" != "x" ] ) &&
echo "usage: $( basename $0 ) mount_point" > /dev/stderr &&
exit -1
# real path (eg. symlink)
mount_point=$( readlink -f "$1" )
# checks mount point
[ -f "$mount_point" ] &&
echo "'$mount_point' is not a directory" > /dev/stderr &&
exit -2
[ ! -d "$mount_point" ] &&
echo "'$mount_point' does not exist" > /dev/stderr &&
exit -2
[ ! -r "$mount_point" ] &&
echo "'$mount_point' is unreadable" > /dev/stderr &&
exit -2
# warning if no stdout redirection (eg. hashwiniso win.iso | tee win.iso.sha1)
[ -t 1 ] &&
echo -n "stdout is not redirected : continue ?" > /dev/stderr &&
read -s &&
echo
# "sed like" but with fixed strings
psed(){
python -u -c "
import sys
for line in sys.stdin:
print(line.strip().replace('$1', '$2'))
"
}
# temporary directory and garbage collector
wim_point=$( mktemp -d )
trap "fusermount -u -z $wim_point &> /dev/null || true; rmdir $wim_point" exit
# firstly, the ISO image
# only if the mount point refers to a file, not a block device
disk_image=$( mount | grep "$mount_point" | sed 's/ on .*//' )
[ "$disk_image" ] &&
[ -f "$disk_image" ] &&
echo "# hashing '$disk_image'..." &&
$hash_bin "$disk_image" |
# $disk_image can contain "reserved" characters (regex)
psed " $disk_image" "\t$( basename "$disk_image")"
# then, the files on the ISO image
echo "# hashing files in '$mount_point'..."
find "$mount_point" -type f -exec $hash_bin '{}' \; |
psed " $mount_point" "\t" |
# NTFS is not case sensitive
tr '[:upper:]' '[:lower:]'
# find all WIM files
wim_list=$(
find "$mount_point" -type f -name '*.wim' |
psed "$mount_point" ""
)
# and finally, all the files present in the different images embedded
# into the WIM files found
for wim_file in $wim_list
do
echo "# hashing files in '$wim_file'..."
count=$(
wimlib-imagex info "$mount_point/$wim_file" |
egrep '^Image Count: +[0-9]+$' |
egrep -o '[0-9]+$'
)
for index in $( seq $count )
do
name=$(
wimlib-imagex info "$mount_point/$wim_file" $index |
grep '^Name: ' |
sed "s;^Name: \+;# $index/$count ;"
)
echo $name
wimlib-imagex mount "$mount_point/$wim_file" $index $wim_point
# comment next line if you want to hash Windows Pro files
[[ ! "${name,,}" =~ "pro" ]] &&
find $wim_point/ -type f -exec $hash_bin '{}' \; |
psed " $wim_point" "\t" |
tr '[:upper:]' '[:lower:]'
fusermount -u $wim_point
done
done
# end of process marker
echo "# done"