forked from debian-pi/raspbian-ua-netinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.sh
executable file
·277 lines (231 loc) · 8.02 KB
/
update.sh
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env bash
KERNEL_VERSION_RPI1=4.4.0-1-rpi
KERNEL_VERSION_RPI2=4.4.0-1-rpi2
RASPBIAN_ARCHIVE_KEY_DIRECTORY="https://archive.raspbian.org"
RASPBIAN_ARCHIVE_KEY_FILE_NAME="raspbian.public.key"
RASPBIAN_ARCHIVE_KEY_URL="${RASPBIAN_ARCHIVE_KEY_DIRECTORY}/${RASPBIAN_ARCHIVE_KEY_FILE_NAME}"
RASPBIAN_ARCHIVE_KEY_FINGERPRINT="A0DA38D0D76E8B5D638872819165938D90FDDD2E"
RASPBERRYPI_ARCHIVE_KEY_DIRECTORY="https://archive.raspberrypi.org/debian"
RASPBERRYPI_ARCHIVE_KEY_FILE_NAME="raspberrypi.gpg.key"
RASPBERRYPI_ARCHIVE_KEY_URL="${RASPBERRYPI_ARCHIVE_KEY_DIRECTORY}/${RASPBERRYPI_ARCHIVE_KEY_FILE_NAME}"
RASPBERRYPI_ARCHIVE_KEY_FINGERPRINT="CF8A1AF502A2AA2D763BAE7E82B129927FA3303E"
mirror=http://archive.raspbian.org/raspbian/
release=jessie
packages=()
# programs
packages+=("raspberrypi-bootloader-nokernel")
packages+=("linux-image-${KERNEL_VERSION_RPI1}")
packages+=("linux-image-${KERNEL_VERSION_RPI2}")
packages+=("btrfs-tools")
packages+=("busybox-static")
packages+=("cdebootstrap-static")
packages+=("dosfstools")
packages+=("e2fslibs")
packages+=("e2fsprogs")
packages+=("f2fs-tools")
packages+=("gpgv")
packages+=("lsb-base")
packages+=("netbase")
packages+=("ntpdate")
packages+=("raspbian-archive-keyring")
# libraries
packages+=("libblkid1")
packages+=("libbz2-1.0")
packages+=("libc6")
packages+=("libcomerr2")
packages+=("libgcc1")
packages+=("liblzo2-2")
packages+=("libssl1.0.0")
packages+=("libuuid1")
packages+=("zlib1g")
check_key() {
# param 1 = keyfile
# param 2 = key fingerprint
# check input parameters
if [ -z $1 ] || [ ! -f $1 ] ; then
echo "Parameter 1 of check_key() is not a file!"
return 1
fi
if [ -z $2 ] ; then
echo "Parameter 2 of check_key() is not a key fingerprint!"
return 1
fi
KEY_FILE="$1"
KEY_FINGERPRINT="$2"
echo -n "Checking key file '${KEY_FILE}'... "
# check that there is only 1 public key in the key file
if [ ! $(gpg --homedir gnupg --keyid-format long --with-fingerprint --with-colons ${KEY_FILE} | grep ^pub: | wc -l) -eq 1 ] ; then
echo "FAILED!"
echo "There are zero or more than one keys in the ${KEY_FILE} key file!"
return 1
fi
# check that the key file's fingerprint is correct
if [ "$(gpg --homedir gnupg --keyid-format long --with-fingerprint --with-colons ${KEY_FILE} | grep ^fpr: | awk -F: '{print $10}')" != "${KEY_FINGERPRINT}" ] ; then
echo "FAILED!"
echo "Bad GPG key fingerprint for ${KEY_FILE}!"
return 1
fi
echo "OK"
return 0
}
setup_archive_keys() {
mkdir -m 0700 -p gnupg
# Let gpg set itself up already in the 'gnupg' dir before we actually use it
echo "Setting up gpg... "
gpg --homedir gnupg --list-secret-keys
echo ""
echo "Downloading ${RASPBIAN_ARCHIVE_KEY_FILE_NAME}."
curl -# -O ${RASPBIAN_ARCHIVE_KEY_URL}
if check_key "${RASPBIAN_ARCHIVE_KEY_FILE_NAME}" "${RASPBIAN_ARCHIVE_KEY_FINGERPRINT}" ; then
# GPG key checks out, thus import it into our own keyring
echo -n "Importing '${RASPBIAN_ARCHIVE_KEY_FILE_NAME}' into keyring... "
if gpg -q --homedir gnupg --import "${RASPBIAN_ARCHIVE_KEY_FILE_NAME}" ; then
echo "OK"
else
echo "FAILED!"
return 1
fi
else
return 1
fi
echo ""
echo "Downloading ${RASPBERRYPI_ARCHIVE_KEY_FILE_NAME}."
curl -# -O ${RASPBERRYPI_ARCHIVE_KEY_URL}
if check_key "${RASPBERRYPI_ARCHIVE_KEY_FILE_NAME}" "${RASPBERRYPI_ARCHIVE_KEY_FINGERPRINT}" ; then
# GPG key checks out, thus import it into our own keyring
echo -n "Importing '${RASPBERRYPI_ARCHIVE_KEY_FILE_NAME}' into keyring..."
if gpg -q --homedir gnupg --import "${RASPBERRYPI_ARCHIVE_KEY_FILE_NAME}" ; then
echo "OK"
else
echo "FAILED!"
return 1
fi
else
return 1
fi
return 0
}
required() {
for i in ${packages[@]}; do
[[ $i = $1 ]] && return 0
done
return 1
}
unset_required() {
for i in ${!packages[@]}; do
[[ ${packages[$i]} = $1 ]] && unset packages[$i] && return 0
done
return 1
}
allfound() {
[[ ${#packages[@]} -eq 0 ]] && return 0
return 1
}
filter_package_list() {
grep -E 'Package:|Filename:|SHA256:|^$'
}
download_package_list() {
# Download and verify package list for $package_section, then add to Packages file
# Assume that the repository's base Release file is present
extensions=( '.xz' '.bz2' '.gz' '' )
for extension in "${extensions[@]}" ; do
# Check that this extension is available
if grep -q ${package_section}/binary-armhf/Packages${extension} Release ; then
# Download Packages file
echo -e "\nDownloading ${package_section} package list..."
curl -# -o tmp${extension} $mirror/dists/$release/$package_section/binary-armhf/Packages${extension}
# Verify the checksum of the Packages file, assuming that the last checksums in the Release file are SHA256 sums
echo -n "Verifying ${package_section} package list... "
if [ $(grep ${package_section}/binary-armhf/Packages${extension} Release | tail -n1 | awk '{print $1}') = \
$(sha256sum tmp${extension} | awk '{print $1}') ]; then
echo "OK"
else
echo -e "ERROR\nThe checksum of the ${package_section}/binary-armhf/Packages${extension} file doesn't match!"
cd ..
exit 1
fi
# Decompress the Packages file
if [ $extension = ".bz2" ] ; then
decompressor="bunzip2 -c "
elif [ $extension = ".xz" ] ; then
decompressor="xzcat "
elif [ $extension = ".gz" ] ; then
decompressor="gunzip -c "
elif [ $extension = "" ] ; then
decompressor="cat "
fi
${decompressor} tmp${extension} >> Packages
rm tmp${extension}
break
fi
done
}
download_package_lists() {
setup_archive_keys
if [ $? != 0 ] ; then
echo -e "ERROR\nSetting up the archives failed! Exiting."
cd ..
exit 1
fi
echo -e "\nDownloading Release file and its signature..."
curl -# -O $mirror/dists/$release/Release -O $mirror/dists/$release/Release.gpg
echo -n "Verifying Release file... "
if gpg --homedir gnupg --verify Release.gpg Release &> /dev/null ; then
echo "OK"
else
echo -e "ERROR\nBroken GPG signature on Release file!"
cd ..
exit 1
fi
echo -n > Packages
package_section=firmware
download_package_list
package_section=main
download_package_list
}
rm -rf packages/
mkdir packages
cd packages
download_package_lists
packages_debs=()
packages_sha256=()
echo -e "\nSearching for required packages..."
while read k v
do
if [ "$k" = "Package:" ]; then
current_package=$v
elif [ "$k" = "Filename:" ]; then
current_filename=$v
elif [ "$k" = "SHA256:" ]; then
current_sha256=$v
elif [ "$k" = "" ]; then
if required $current_package; then
printf " %-32s %s\n" $current_package $(basename $current_filename)
unset_required $current_package
packages_debs+=("${mirror}${current_filename}")
packages_sha256+=("${current_sha256} $(basename ${current_filename})")
allfound && break
fi
current_package=
current_filename=
current_sha256=
fi
done < <(filter_package_list <Packages)
if ! allfound ; then
echo "ERROR: Unable to find all required packages in package list!"
echo "Missing packages: ${packages[@]}"
cd ..
exit 1
fi
echo -e "\nDownloading packages..."
curl -# --remote-name-all ${packages_debs[@]}
echo -n "Verifying downloaded packages... "
printf "%s\n" "${packages_sha256[@]}" > SHA256SUMS
if sha256sum --quiet -c SHA256SUMS ; then
echo "OK"
else
echo -e "ERROR\nThe checksums of the downloaded packages don't match the package lists!"
cd ..
exit 1
fi
cd ..