This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
/
pc-updatemanager
executable file
·594 lines (489 loc) · 15.7 KB
/
pc-updatemanager
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/bin/sh
# Script which performs updating services
############################################################################
# Source our functions
. /usr/local/share/pcbsd/scripts/functions.sh
# Set the program location
PROGDIR="/usr/local/share/pcbsd/pc-updatemanager" ; export PROGDIR
# Start by sourcing /etc/profile
# This grabs any HTTP_ / FTP_ PROXY variables
. /etc/profile
PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
export PCBSD_ETCCONF
# Set the pub openssl key
SKEY="${PROGDIR}/conf/security.key" ; export SKEY
# Directory to store downloaded updates
DOWNLOADDIR="/usr/local/tmp"
# Get the system version we are checking for updates to
SYSVER="`uname -r | cut -d '-' -f 1-2`" ; export SYSVER
# Get the system type
SYSTYPE="`pbreg get /PC-BSD/SysType`" ; export SYSTYPE
# Set the config location
UPDATECONF="${PROGDIR}/conf/sysupdate.conf"
# Set the system arch type
ARCH=`uname -m`
# Default pcbsd.conf file
PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
# Patchset Data
PATCHSERVER="`cat ${UPDATECONF} | grep '^UPDATESERVER:' | cut -d ' ' -f 2`"
PATCHSET="`cat ${UPDATECONF} | grep '^PATCHSET:' | cut -d ' ' -f 2`"
PATCHFILE="${PATCHSET}.upd"
PATCHURL="${PATCHSERVER}/${PATCHFILE}"
PATCHTMPDIR=`mktemp -d /tmp/.sysupdateXXXXXX`
PATCHTMPFILE="${PATCHTMPDIR}/sysupdate-${SYSVER}.upd"
MUSTAGEDIR="${DOWNLOADDIR}/update-stagedir"
# Enable ftp passive mode for file transfers
FTP_PASSIVE_MODE="YES" ; export FTP_PASSIVE_MODE
if [ ! -d "${DOWNLOADDIR}" ]; then mkdir -p ${DOWNLOADDIR}; fi
# Trigger File for Tray Application
TRIGGERFILE="/tmp/.sysupdatetraytrigger"
DBDIR="/var/db/pc-updatemanager"
INSDIR="${DBDIR}/installed"
IGNDIR="${DBDIR}/ignored"
######################################################################
# Done with config values
######################################################################
show_usage() {
echo "$0 - Usage
----
branches - List available system branches
chbranch <tag> - Change to new system branch
check - Check for system updates
install <tag>,<tag2> - Install system updates
pkgcheck - Check for updates to packages
pkgupdate - Install packages updates
"
exit 1
}
get_update_file() {
# Now fetch the update file
get_file "${PATCHURL}" "${PATCHTMPFILE}" 1 >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
rm ${PATCHTMPFILE} 2>/dev/null
is_net_up
if [ $? -eq 0 ] ; then
echo "No updates available for $SYSVER!" ; exit 0
else
exit_err "Could not contact update server!"
fi
fi
# Now fetch the update file signature
get_file "${PATCHURL}.sha1" "${PATCHTMPFILE}.sha1" 1 >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
rm ${PATCHTMPFILE}.sha1 2>/dev/null
is_net_up
if [ $? -eq 0 ] ; then
echo "No updates available for $SYSVER!" ; exit 0
else
exit_err "Could not contact update server!"
fi
fi
# Lets verify the signature of the file
openssl dgst -sha1 -verify ${SKEY} \
-signature ${PATCHTMPFILE}.sha1 \
${PATCHTMPFILE} >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
rm ${PATCHTMPFILE} 2>/dev/null
rm ${PATCHTMPFILE}.sha1 2>/dev/null
exit_err "Update file failed verification..."
fi
# Done with the signature we can discard
rm ${PATCHTMPFILE}.sha1
}
do_branch() {
up="$1"
# Setup our variable to catch if we have a bad branch
BRANCHFOUND="0"
while read line
do
echo $line | grep -q "^BRANCHES:"
if [ $? -eq 0 ] ; then
for i in `echo $line | cut -d ':' -f 2 | sed 's|,| |g'`
do
if [ "$i" != "$up" ] ; then continue ;fi
if [ "`uname -r | cut -d '-' -f 1-2`" = "$up" ] ; then
echo "You are already on this branch!"
BRANCHFOUND="1"
continue;
fi
start_major_fbsd_update "fbsd-$up"
BRANCHFOUND="1"
break
done
fi
done < ${PATCHTMPFILE}
# If no patches
if [ "${BRANCHFOUND}" = "0" ]; then
# No available updates
echo "Unknown branch ${1}"
fi
rm ${PATCHTMPFILE}
};
list_branches() {
# Setup our variable, we have no branches so far
BRANCHFOUND="0"
while read line
do
echo $line | grep -q "^BRANCHES:"
if [ $? -eq 0 ] ; then
echo "Available branches:"
echo "----------------------------------"
echo $line | cut -d ':' -f 2 | sed 's|,|\
|g'
echo ""
echo "To change branches run: ${0} chbranch <branch>"
BRANCHFOUND="1"
continue
fi
done < ${PATCHTMPFILE}
# If no patches
if [ "${BRANCHFOUND}" = "0" ]; then
# No available updates
echo "No branches currently available!"
fi
rm ${PATCHTMPFILE}
};
do_check() {
# Setup our variable, we have no patches so far
PATCHFOUND="0"
while read line
do
echo $line | grep -q "^MU:"
if [ $? -eq 0 ] ; then
parse_mu_update_line "$line"
continue
fi
echo $line | grep -q "^SA:"
if [ $? -eq 0 ] ; then
parse_sa_update_line "$line"
continue
fi
done < ${PATCHTMPFILE}
# If no patches
if [ "${PATCHFOUND}" = "0" ]; then
# No available updates
echo "Your system is up to date!"
fi
rm ${PATCHTMPFILE}
};
parse_mu_update_line()
{
local line="$1"
# Check if this is a major update we can install
local sysUpOVer=`echo $line | cut -d ':' -f 2`
local sysUpNVer=`echo $line | cut -d ':' -f 3`
# Is this an update for our version?
if [ "$SYSVER" != "$sysUpOVer" ] ; then return; fi
if [ $PATCHFOUND -eq 0 ] ; then
echo ""
echo "The following updates are available:"
echo "------------------------------------"
fi
echo "NAME: System Update to ${sysUpNVer}"
echo "TYPE: SYSUPDATE"
echo "TAG: fbsd-${sysUpNVer}"
echo "VERSION: ${sysUpNVer}"
echo ""
echo "To install: \"pc-updatemanager install fbsd-${sysUpNVer}\""
echo ""
echo ""
PATCHFOUND="`expr ${PATCHFOUND} + 1`"
}
parse_sa_update_line()
{
local line="$1"
# Check if this is an update we can install
local saVer=`echo $line | cut -d ':' -f 2`
local saArch=`echo $line | cut -d ':' -f 3`
local saType=`echo $line | cut -d ':' -f 4`
local saIdent="`echo $line | cut -d ':' -f 5`"
# For this version?
if [ "$SYSVER" != "$saVer" ] ; then return ; fi
# For this system arch?
if [ "$ARCH" != "$saArch" -a "$saArch" != "noarch" ] ; then return ; fi
# For this system type?
if [ "$SYSTYPE" != "$saType" -a "$saType" != "both" ] ; then return ; fi
# Already installed?
if [ -e "${INSDIR}/${SYSVER}/$saIdent" ] ; then return ; fi
if [ $PATCHFOUND -eq 0 ] ; then
echo ""
echo "The following updates are available:"
echo "------------------------------------"
fi
local saDesc="`echo $line | cut -d ':' -f 6`"
local saSize=`echo $line | cut -d ':' -f 8`
local saDetail="http://trac.pcbsd.org`echo $line | cut -d ':' -f 10`"
local saDate="`echo $line | cut -d ':' -f 11`"
echo "NAME: ${saDesc}"
echo "TYPE: PATCH"
echo "TAG: ${saIdent}"
echo "DETAILS: ${saDetail}"
echo "DATE: ${saDate}"
echo "SIZE: ${saSize}Mb"
echo " "
echo "To install: \"pc-updatemanager install ${saIdent}\""
echo " "
PATCHFOUND="`expr ${PATCHFOUND} + 1`"
}
start_pcbsd_patch() {
local up="$1"
local saIdent=""
local saVer=""
local saArch=""
local saPlat=""
local saUrl=""
local saDesc=""
local saCsum=""
local patchLine=""
if [ -e "${INSDIR}/${SYSVER}/${up}" ]; then
rm ${PATCHTMPFILE}
exit_err "Patch $up already installed!"
fi
while read line
do
echo $line | grep -q "^SA:"
if [ $? -ne 0 ] ; then continue; fi
saIdent="`echo $line | cut -d ':' -f 5`"
# Look for the right stand-alone update
if [ "$saIdent" != "$up" ] ; then continue ; fi
saVer=`echo $line | cut -d ':' -f 2`
saArch=`echo $line | cut -d ':' -f 3`
saType=`echo $line | cut -d ':' -f 4`
# Does this update apply to this system arch?
if [ "$saArch" != "$ARCH" -a "$saArch" != "noarch" ] ; then
rm ${PATCHTMPFILE}
exit_err "Patch $up is not for this system arch type!"
fi
# For this version?
if [ "$saVer" != "$SYSVER" ] ; then
rm ${PATCHTMPFILE}
exit_err "Patch $up is not for this system version!"
fi
# For this system type?
if [ "$SYSTYPE" != "$saType" -a "$saType" != "both" ] ; then
rm ${PATCHTMPFILE}
exit_err "Patch $up is not for this system type!"
fi
# Got here? We have a patch ready to go
patchLine="$line"
break
done < ${PATCHTMPFILE}
# Did we find the patch?
if [ -z "$patchLine" ] ; then
rm ${PATCHTMPFILE}
exit_err "Patch $up not found!"
fi
# Get the patch details
saVer=`echo $patchLine | cut -d ':' -f 2`
saArch=`echo $patchLine | cut -d ':' -f 3`
saPlat=`echo $patchLine | cut -d ':' -f 4`
saIdent="`echo $patchLine | cut -d ':' -f 5`"
saDesc="`echo $patchLine | cut -d ':' -f 6`"
saUrl="`echo $patchLine | cut -d ':' -f 7`"
saCsum="`echo $patchLine | cut -d ':' -f 9`"
local FILENAME="`basename $saUrl`"
# Start downloading the patch
touch ${TRIGGERFILE}
echo "DOWNLOADING: ${saIdent}"
echo "DOWNLOADING: ${saIdent}" >${TRIGGERFILE}
# Get the file
get_file_from_mirrors "/${saUrl}" "${DOWNLOADDIR}/${FILENAME}" "update"
if [ $? -ne 0 ] ; then
rm ${PATCHTMPFILE}
echo "FAILED: ${saIdent}" >${TRIGGERFILE}
exit_err "Failed to download: ${saIdent}"
fi
# Check the sha256 checksum
if [ "$saCsum" != "`sha256 -q ${DOWNLOADDIR}/${FILENAME} 2>/dev/null`" ]
then
# Download MD5 doesn't match! Delete the file
rm ${PATCHTMPFILE}
rm ${DOWNLOADDIR}/${FILENAME}
echo "FAILED: ${saIdent}" >${TRIGGERFILE}
exit_err "Failed to download: ${saIdent}"
else
echo "DOWNLOADFINISHED: ${saIdent}"
echo "DOWNLOADFINISHED: ${saIdent}" >${TRIGGERFILE}
fi
echo "INSTALLING: ${saIdent}" >${TRIGGERFILE}
sleep 1
PATCHTMPDIR="`mktemp -d ${DOWNLOADDIR}/patchInstallXXXXX`"
rc_halt "tar xvJf ${DOWNLOADDIR}/${FILENAME} -C ${PATCHTMPDIR}" 2>/dev/null
PATCHDIR="$PATCHTMPDIR" ; export PATCHDIR
cd ${PATCHTMPDIR}
sh update.sh
if [ $? -eq 0 ]; then
touch "${INSDIR}/${SYSVER}/${saIdent}"
echo "INSTALLFINISHED: ${saIdent}"
echo "INSTALLFINISHED: ${saIdent}" >${TRIGGERFILE}
else
rm ${PATCHTMPFILE}
rm -rf ${PATCHTMPDIR}
rm ${DOWNLOADDIR}/${FILENAME}
echo "INSTALLFAILED: ${saIdent}" >${TRIGGERFILE}
exit_err "INSTALLFAILED: ${saIdent}"
fi
rc_halt "rm -rf ${PATCHTMPDIR}"
rc_halt "rm ${DOWNLOADDIR}/${FILENAME}"
}
rollback_update()
{
cp /usr/local/etc/pkg.conf.preUpgrade /usr/local/etc/pkg.conf
if [ "$PKGROLLBACK" = "YES" ] ; then
fi
}
start_major_fbsd_update() {
local rel=`echo $1 | sed 's|fbsd-||g'`
PKGROLLBACK="NO"
# Lets backup the old pkg.conf file
cp /usr/local/etc/pkg.conf /usr/local/etc/pkg.conf.preUpgrade
# Set the new pkg.conf file to the new repo
local arch=`uname -m`
echo "packagesite: http://pkg.cdn.pcbsd.org/$rel/$arch" >/usr/local/etc/pkg.conf
echo "PUBKEY: /usr/local/etc/pkg-pubkey.cert" >>/usr/local/etc/pkg.conf
echo "PKG_CACHEDIR: /usr/local/tmp" >>/usr/local/etc/pkg.conf
# Lets backup old pkgng
oPkg=`pkg info pkg | awk '{print $1}'`
pkg create -o /usr/local/tmp/ ${oPkg}
if [ -e "/usr/local/tmp/oldPkg.txz" ] ; then rm /usr/local/tmp/oldPkg.txz; fi
mv /usr/local/tmp/${oPkg}.txz /usr/local/tmp/oldPkg.txz
# Lets download the new pkgng for this release
if [ -e "/usr/local/tmp/pkg.txz" ] ; then rm /usr/local/tmp/pkg.txz; fi
get_file_from_mirrors "/packages/$rel/$arch/Latest/pkg.txz" "/usr/local/tmp/pkg.txz" "pkg"
if [ $? -ne 0 ] ; then
rollback_update
exit_err "Failed downloading pkgng update..."
fi
# Lets install pkgng update
PKGROLLBACK="YES"
echo "Installing pkgng update..."
pkg-static add /usr/local/tmp/pkg.txz
# Lets pre-download all packages
download_cache_packages "pkg-static upgrade -fn"
if [ $? -ne 0 ] ; then
rollback_update
exit_err "Failed downloading packages for update..."
fi
# Download update files from freebsd-update
freebsd-update --non-interactive -r "$rel" upgrade
if [ $? -ne 0 ] ; then
rollback_update
exit_err "Failed running \"freebsd-update -r $rel upgrade\"..."
fi
# Touch our marker that we will need to reboot before doing other updates
touch /tmp/.fbsdup-reboot
# Now install freebsd-update files
freebsd-update --non-interactive install
if [ $? -ne 0 ] ; then
rollback_update
exit_err "Failed running \"freebsd-update install\"..."
fi
# Now its time to force a pkg-update of all packages to their new FreeBSD compiled versions
SKIP_CACHE_PKGS="YES" ; export SKIP_CACHE_PKGS
pc-updatemanager pkgupdate -f
# Last but not least, lets make sure to re-stamp GRUB in case any loader.conf.pcbsd modules changed
grub-mkconfig -o /boot/grub/grub.cfg
echo "Major update finished!"
echo "Please reboot the system for changes to take effect."
exit 0
}
do_install() {
# Make sure we are root and have specified updates to install
if [ `id -u` != "0" ] ; then exit_err "Must be run as root!" ; fi
if [ -z "$1" ] ; then exit_err "No updates specified to install!"; fi
# Begin to update the selected items
for up in `echo $1 | sed 's|,| |g'`
do
# If this is a major update, start it up!
echo $up | grep -q "^fbsd-"
if [ $? -eq 0 ] ; then
start_major_fbsd_update "$up"
exit 0
elif [ "$up" == "pkg-updates" ] ; then
# Check if this is a pkg-update request or regular patch
start_pkg_updates
continue
else
# Doing regular pcbsd patch
start_pcbsd_patch "${up}"
continue
fi
done
# All Finished!
rm ${PATCHTMPFILE}
exit 0
}
is_net_up() {
ping -c 1 www.pcbsd.org >/dev/null 2>/dev/null
return $?
}
checkup_pkgs() {
# Checkout the installed pkgs and compare to master list
rc_halt "pkg-static update"
pkg-static upgrade -n >/tmp/.pkgData.$$
grep -q "Upgrading" /tmp/.pkgData.$$
if [ $? -ne 0 ] ; then
echo "All packages are up to date!"
rm /tmp/.pkgData.$$
return 0
fi
# Display the pkg update data
cat /tmp/.pkgData.$$
rm /tmp/.pkgData.$$
echo ""
echo "To start the upgrade run \"${0} pkgupdate\""
return 0
}
update_pkgs()
{
if [ -n "$1" -a "$1" != "-f" ] ; then
exit_err "Invalid option $1 passed"
fi
# If we aren't instructed to skip the package cache process
if [ -z "$SKIP_CACHE_PKGS" ] ; then
# Lets pre-download all the packages
download_cache_packages "pkg-static upgrade -U -n $1"
fi
check_pkg_conflicts "upgrade -U -F -y $1"
if [ $? -ne 0 ] ; then
exit 1
fi
# Start the package update!
pkg-static upgrade -U -y ${1}
result=$?
# Lets now re-extract our overlay data to grab any adjusted port files
pc-extractoverlay ports >/dev/null 2>/dev/null
# Now re-extract overlay files to ensure we do file fixing / massaging
if [ "$SYSTYPE" = "PCBSD" ]; then
pc-extractoverlay desktop
fi
if [ "$SYSTYPE" = "TRUEOS" ]; then
pc-extractoverlay server
fi
if [ $result -ne 0 ] ; then
echo "Failed updating..."
fi
exit $result
}
# Check if we have an update folder for the version we are on
if [ ! -d "${AVAILDIR}/${SYSVER}" ] ; then mkdir -p ${AVAILDIR}/${SYSVER} ; fi
if [ "`id -u`" = "0" ] ; then
# Make the installed directory for this version
if [ ! -d "${INSDIR}/${SYSVER}" ] ; then mkdir -p ${INSDIR}/${SYSVER} ; fi
# Make the ignore directory for this version
if [ ! -d "${IGNDIR}/${SYSVER}" ] ; then mkdir -p ${IGNDIR}/${SYSVER} ; fi
fi
case $1 in
check) get_update_file
do_check ;;
branches) get_update_file
list_branches ;;
chbranch) get_update_file
do_branch "${2}" ;;
pkgcheck) checkup_pkgs ;;
pkgupdate) update_pkgs "$2" ;;
install) get_update_file
do_install "${2}" ;;
*) show_usage ;;
esac
exit 0