/
freebsd-update.sh
3400 lines (2960 loc) · 91.3 KB
/
freebsd-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
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#-
# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
#
# Copyright 2004-2007 Colin Percival
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# $FreeBSD$
#### Usage function -- called from command-line handling code.
# Usage instructions. Options not listed:
# --debug -- don't filter output from utilities
# --no-stats -- don't show progress statistics while fetching files
usage () {
cat <<EOF
usage: `basename $0` [options] command ... [path]
Options:
-b basedir -- Operate on a system mounted at basedir
(default: /)
-d workdir -- Store working files in workdir
(default: /var/db/freebsd-update/)
-f conffile -- Read configuration options from conffile
(default: /etc/freebsd-update.conf)
-F -- Force a fetch operation to proceed in the
case of an unfinished upgrade
-k KEY -- Trust an RSA key with SHA256 hash of KEY
-r release -- Target for upgrade (e.g., 11.1-RELEASE)
-s server -- Server from which to fetch updates
(default: update.FreeBSD.org)
-t address -- Mail output of cron command, if any, to address
(default: root)
--not-running-from-cron
-- Run without a tty, for use by automated tools
--currently-running release
-- Update as if currently running this release
Commands:
fetch -- Fetch updates from server
cron -- Sleep rand(3600) seconds, fetch updates, and send an
email if updates were found
upgrade -- Fetch upgrades to FreeBSD version specified via -r option
updatesready -- Check if there are fetched updates ready to install
install -- Install downloaded updates or upgrades
rollback -- Uninstall most recently installed updates
IDS -- Compare the system against an index of "known good" files
showconfig -- Show configuration
EOF
exit 0
}
#### Configuration processing functions
#-
# Configuration options are set in the following order of priority:
# 1. Command line options
# 2. Configuration file options
# 3. Default options
# In addition, certain options (e.g., IgnorePaths) can be specified multiple
# times and (as long as these are all in the same place, e.g., inside the
# configuration file) they will accumulate. Finally, because the path to the
# configuration file can be specified at the command line, the entire command
# line must be processed before we start reading the configuration file.
#
# Sound like a mess? It is. Here's how we handle this:
# 1. Initialize CONFFILE and all the options to "".
# 2. Process the command line. Throw an error if a non-accumulating option
# is specified twice.
# 3. If CONFFILE is "", set CONFFILE to /etc/freebsd-update.conf .
# 4. For all the configuration options X, set X_saved to X.
# 5. Initialize all the options to "".
# 6. Read CONFFILE line by line, parsing options.
# 7. For each configuration option X, set X to X_saved iff X_saved is not "".
# 8. Repeat steps 4-7, except setting options to their default values at (6).
CONFIGOPTIONS="KEYPRINT WORKDIR SERVERNAME MAILTO ALLOWADD ALLOWDELETE
KEEPMODIFIEDMETADATA COMPONENTS IGNOREPATHS UPDATEIFUNMODIFIED
BASEDIR VERBOSELEVEL TARGETRELEASE STRICTCOMPONENTS MERGECHANGES
IDSIGNOREPATHS BACKUPKERNEL BACKUPKERNELDIR BACKUPKERNELSYMBOLFILES"
# Set all the configuration options to "".
nullconfig () {
for X in ${CONFIGOPTIONS}; do
eval ${X}=""
done
}
# For each configuration option X, set X_saved to X.
saveconfig () {
for X in ${CONFIGOPTIONS}; do
eval ${X}_saved=\$${X}
done
}
# For each configuration option X, set X to X_saved if X_saved is not "".
mergeconfig () {
for X in ${CONFIGOPTIONS}; do
eval _=\$${X}_saved
if ! [ -z "${_}" ]; then
eval ${X}=\$${X}_saved
fi
done
}
# Set the trusted keyprint.
config_KeyPrint () {
if [ -z ${KEYPRINT} ]; then
KEYPRINT=$1
else
return 1
fi
}
# Set the working directory.
config_WorkDir () {
if [ -z ${WORKDIR} ]; then
WORKDIR=$1
else
return 1
fi
}
# Set the name of the server (pool) from which to fetch updates
config_ServerName () {
if [ -z ${SERVERNAME} ]; then
SERVERNAME=$1
else
return 1
fi
}
# Set the address to which 'cron' output will be mailed.
config_MailTo () {
if [ -z ${MAILTO} ]; then
MAILTO=$1
else
return 1
fi
}
# Set whether FreeBSD Update is allowed to add files (or directories, or
# symlinks) which did not previously exist.
config_AllowAdd () {
if [ -z ${ALLOWADD} ]; then
case $1 in
[Yy][Ee][Ss])
ALLOWADD=yes
;;
[Nn][Oo])
ALLOWADD=no
;;
*)
return 1
;;
esac
else
return 1
fi
}
# Set whether FreeBSD Update is allowed to remove files/directories/symlinks.
config_AllowDelete () {
if [ -z ${ALLOWDELETE} ]; then
case $1 in
[Yy][Ee][Ss])
ALLOWDELETE=yes
;;
[Nn][Oo])
ALLOWDELETE=no
;;
*)
return 1
;;
esac
else
return 1
fi
}
# Set whether FreeBSD Update should keep existing inode ownership,
# permissions, and flags, in the event that they have been modified locally
# after the release.
config_KeepModifiedMetadata () {
if [ -z ${KEEPMODIFIEDMETADATA} ]; then
case $1 in
[Yy][Ee][Ss])
KEEPMODIFIEDMETADATA=yes
;;
[Nn][Oo])
KEEPMODIFIEDMETADATA=no
;;
*)
return 1
;;
esac
else
return 1
fi
}
# Add to the list of components which should be kept updated.
config_Components () {
for C in $@; do
COMPONENTS="${COMPONENTS} ${C}"
done
}
# Remove src component from list if it isn't installed
finalize_components_config () {
COMPONENTS=""
for C in $@; do
if [ "$C" = "src" ]; then
if [ -e "${BASEDIR}/usr/src/COPYRIGHT" ]; then
COMPONENTS="${COMPONENTS} ${C}"
else
echo "src component not installed, skipped"
fi
else
COMPONENTS="${COMPONENTS} ${C}"
fi
done
}
# Add to the list of paths under which updates will be ignored.
config_IgnorePaths () {
for C in $@; do
IGNOREPATHS="${IGNOREPATHS} ${C}"
done
}
# Add to the list of paths which IDS should ignore.
config_IDSIgnorePaths () {
for C in $@; do
IDSIGNOREPATHS="${IDSIGNOREPATHS} ${C}"
done
}
# Add to the list of paths within which updates will be performed only if the
# file on disk has not been modified locally.
config_UpdateIfUnmodified () {
for C in $@; do
UPDATEIFUNMODIFIED="${UPDATEIFUNMODIFIED} ${C}"
done
}
# Add to the list of paths within which updates to text files will be merged
# instead of overwritten.
config_MergeChanges () {
for C in $@; do
MERGECHANGES="${MERGECHANGES} ${C}"
done
}
# Work on a FreeBSD installation mounted under $1
config_BaseDir () {
if [ -z ${BASEDIR} ]; then
BASEDIR=$1
else
return 1
fi
}
# When fetching upgrades, should we assume the user wants exactly the
# components listed in COMPONENTS, rather than trying to guess based on
# what's currently installed?
config_StrictComponents () {
if [ -z ${STRICTCOMPONENTS} ]; then
case $1 in
[Yy][Ee][Ss])
STRICTCOMPONENTS=yes
;;
[Nn][Oo])
STRICTCOMPONENTS=no
;;
*)
return 1
;;
esac
else
return 1
fi
}
# Upgrade to FreeBSD $1
config_TargetRelease () {
if [ -z ${TARGETRELEASE} ]; then
TARGETRELEASE=$1
else
return 1
fi
if echo ${TARGETRELEASE} | grep -qE '^[0-9.]+$'; then
TARGETRELEASE="${TARGETRELEASE}-RELEASE"
fi
}
# Pretend current release is FreeBSD $1
config_SourceRelease () {
UNAME_r=$1
if echo ${UNAME_r} | grep -qE '^[0-9.]+$'; then
UNAME_r="${UNAME_r}-RELEASE"
fi
export UNAME_r
}
# Define what happens to output of utilities
config_VerboseLevel () {
if [ -z ${VERBOSELEVEL} ]; then
case $1 in
[Dd][Ee][Bb][Uu][Gg])
VERBOSELEVEL=debug
;;
[Nn][Oo][Ss][Tt][Aa][Tt][Ss])
VERBOSELEVEL=nostats
;;
[Ss][Tt][Aa][Tt][Ss])
VERBOSELEVEL=stats
;;
*)
return 1
;;
esac
else
return 1
fi
}
config_BackupKernel () {
if [ -z ${BACKUPKERNEL} ]; then
case $1 in
[Yy][Ee][Ss])
BACKUPKERNEL=yes
;;
[Nn][Oo])
BACKUPKERNEL=no
;;
*)
return 1
;;
esac
else
return 1
fi
}
config_BackupKernelDir () {
if [ -z ${BACKUPKERNELDIR} ]; then
if [ -z "$1" ]; then
echo "BackupKernelDir set to empty dir"
return 1
fi
# We check for some paths which would be extremely odd
# to use, but which could cause a lot of problems if
# used.
case $1 in
/|/bin|/boot|/etc|/lib|/libexec|/sbin|/usr|/var)
echo "BackupKernelDir set to invalid path $1"
return 1
;;
/*)
BACKUPKERNELDIR=$1
;;
*)
echo "BackupKernelDir ($1) is not an absolute path"
return 1
;;
esac
else
return 1
fi
}
config_BackupKernelSymbolFiles () {
if [ -z ${BACKUPKERNELSYMBOLFILES} ]; then
case $1 in
[Yy][Ee][Ss])
BACKUPKERNELSYMBOLFILES=yes
;;
[Nn][Oo])
BACKUPKERNELSYMBOLFILES=no
;;
*)
return 1
;;
esac
else
return 1
fi
}
# Handle one line of configuration
configline () {
if [ $# -eq 0 ]; then
return
fi
OPT=$1
shift
config_${OPT} $@
}
#### Parameter handling functions.
# Initialize parameters to null, just in case they're
# set in the environment.
init_params () {
# Configration settings
nullconfig
# No configuration file set yet
CONFFILE=""
# No commands specified yet
COMMANDS=""
# Force fetch to proceed
FORCEFETCH=0
# Run without a TTY
NOTTYOK=0
# Fetched first in a chain of commands
ISFETCHED=0
}
# Parse the command line
parse_cmdline () {
while [ $# -gt 0 ]; do
case "$1" in
# Location of configuration file
-f)
if [ $# -eq 1 ]; then usage; fi
if [ ! -z "${CONFFILE}" ]; then usage; fi
shift; CONFFILE="$1"
;;
-F)
FORCEFETCH=1
;;
--not-running-from-cron)
NOTTYOK=1
;;
--currently-running)
shift
config_SourceRelease $1 || usage
;;
# Configuration file equivalents
-b)
if [ $# -eq 1 ]; then usage; fi; shift
config_BaseDir $1 || usage
;;
-d)
if [ $# -eq 1 ]; then usage; fi; shift
config_WorkDir $1 || usage
;;
-k)
if [ $# -eq 1 ]; then usage; fi; shift
config_KeyPrint $1 || usage
;;
-s)
if [ $# -eq 1 ]; then usage; fi; shift
config_ServerName $1 || usage
;;
-r)
if [ $# -eq 1 ]; then usage; fi; shift
config_TargetRelease $1 || usage
;;
-t)
if [ $# -eq 1 ]; then usage; fi; shift
config_MailTo $1 || usage
;;
-v)
if [ $# -eq 1 ]; then usage; fi; shift
config_VerboseLevel $1 || usage
;;
# Aliases for "-v debug" and "-v nostats"
--debug)
config_VerboseLevel debug || usage
;;
--no-stats)
config_VerboseLevel nostats || usage
;;
# Commands
cron | fetch | upgrade | updatesready | install | rollback |\
IDS | showconfig)
COMMANDS="${COMMANDS} $1"
;;
# Anything else is an error
*)
usage
;;
esac
shift
done
# Make sure we have at least one command
if [ -z "${COMMANDS}" ]; then
usage
fi
}
# Parse the configuration file
parse_conffile () {
# If a configuration file was specified on the command line, check
# that it exists and is readable.
if [ ! -z "${CONFFILE}" ] && [ ! -r "${CONFFILE}" ]; then
echo -n "File does not exist "
echo -n "or is not readable: "
echo ${CONFFILE}
exit 1
fi
# If a configuration file was not specified on the command line,
# use the default configuration file path. If that default does
# not exist, give up looking for any configuration.
if [ -z "${CONFFILE}" ]; then
CONFFILE="/etc/freebsd-update.conf"
if [ ! -r "${CONFFILE}" ]; then
return
fi
fi
# Save the configuration options specified on the command line, and
# clear all the options in preparation for reading the config file.
saveconfig
nullconfig
# Read the configuration file. Anything after the first '#' is
# ignored, and any blank lines are ignored.
L=0
while read LINE; do
L=$(($L + 1))
LINEX=`echo "${LINE}" | cut -f 1 -d '#'`
if ! configline ${LINEX}; then
echo "Error processing configuration file, line $L:"
echo "==> ${LINE}"
exit 1
fi
done < ${CONFFILE}
# Merge the settings read from the configuration file with those
# provided at the command line.
mergeconfig
}
# Provide some default parameters
default_params () {
# Save any parameters already configured, and clear the slate
saveconfig
nullconfig
# Default configurations
config_WorkDir /var/db/freebsd-update
config_MailTo root
config_AllowAdd yes
config_AllowDelete yes
config_KeepModifiedMetadata yes
config_BaseDir /
config_VerboseLevel stats
config_StrictComponents no
config_BackupKernel yes
config_BackupKernelDir /boot/kernel.old
config_BackupKernelSymbolFiles no
# Merge these defaults into the earlier-configured settings
mergeconfig
}
# Set utility output filtering options, based on ${VERBOSELEVEL}
fetch_setup_verboselevel () {
case ${VERBOSELEVEL} in
debug)
QUIETREDIR="/dev/stderr"
QUIETFLAG=" "
STATSREDIR="/dev/stderr"
DDSTATS=".."
XARGST="-t"
NDEBUG=" "
;;
nostats)
QUIETREDIR=""
QUIETFLAG=""
STATSREDIR="/dev/null"
DDSTATS=".."
XARGST=""
NDEBUG=""
;;
stats)
QUIETREDIR="/dev/null"
QUIETFLAG="-q"
STATSREDIR="/dev/stdout"
DDSTATS=""
XARGST=""
NDEBUG="-n"
;;
esac
}
# Perform sanity checks and set some final parameters
# in preparation for fetching files. Figure out which
# set of updates should be downloaded: If the user is
# running *-p[0-9]+, strip off the last part; if the
# user is running -SECURITY, call it -RELEASE. Chdir
# into the working directory.
fetchupgrade_check_params () {
export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)"
_SERVERNAME_z=\
"SERVERNAME must be given via command line or configuration file."
_KEYPRINT_z="Key must be given via -k option or configuration file."
_KEYPRINT_bad="Invalid key fingerprint: "
_WORKDIR_bad="Directory does not exist or is not writable: "
_WORKDIR_bad2="Directory is not on a persistent filesystem: "
if [ -z "${SERVERNAME}" ]; then
echo -n "`basename $0`: "
echo "${_SERVERNAME_z}"
exit 1
fi
if [ -z "${KEYPRINT}" ]; then
echo -n "`basename $0`: "
echo "${_KEYPRINT_z}"
exit 1
fi
if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
echo -n "`basename $0`: "
echo -n "${_KEYPRINT_bad}"
echo ${KEYPRINT}
exit 1
fi
if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
echo -n "`basename $0`: "
echo -n "${_WORKDIR_bad}"
echo ${WORKDIR}
exit 1
fi
case `df -T ${WORKDIR}` in */dev/md[0-9]* | *tmpfs*)
echo -n "`basename $0`: "
echo -n "${_WORKDIR_bad2}"
echo ${WORKDIR}
exit 1
;;
esac
chmod 700 ${WORKDIR}
cd ${WORKDIR} || exit 1
# Generate release number. The s/SECURITY/RELEASE/ bit exists
# to provide an upgrade path for FreeBSD Update 1.x users, since
# the kernels provided by FreeBSD Update 1.x are always labelled
# as X.Y-SECURITY.
RELNUM=`uname -r |
sed -E 's,-p[0-9]+,,' |
sed -E 's,-SECURITY,-RELEASE,'`
ARCH=`uname -m`
FETCHDIR=${RELNUM}/${ARCH}
PATCHDIR=${RELNUM}/${ARCH}/bp
# Disallow upgrade from a version that is not a release
case ${RELNUM} in
*-RELEASE | *-ALPHA* | *-BETA* | *-RC*)
;;
*)
echo -n "`basename $0`: "
cat <<- EOF
Cannot upgrade from a version that is not a release
(including alpha, beta and release candidates)
using `basename $0`. Instead, FreeBSD can be directly
upgraded by source or upgraded to a RELEASE/RELENG version
prior to running `basename $0`.
Currently running: ${RELNUM}
EOF
exit 1
;;
esac
# Figure out what directory contains the running kernel
BOOTFILE=`sysctl -n kern.bootfile`
KERNELDIR=${BOOTFILE%/kernel}
if ! [ -d ${KERNELDIR} ]; then
echo "Cannot identify running kernel"
exit 1
fi
# Figure out what kernel configuration is running. We start with
# the output of `uname -i`, and then make the following adjustments:
# 1. Replace "SMP-GENERIC" with "SMP". Why the SMP kernel config
# file says "ident SMP-GENERIC", I don't know...
# 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64"
# _and_ `sysctl kern.version` contains a line which ends "/SMP", then
# we're running an SMP kernel. This mis-identification is a bug
# which was fixed in 6.2-STABLE.
KERNCONF=`uname -i`
if [ ${KERNCONF} = "SMP-GENERIC" ]; then
KERNCONF=SMP
fi
if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then
if sysctl kern.version | grep -qE '/SMP$'; then
KERNCONF=SMP
fi
fi
# Define some paths
BSPATCH=/usr/bin/bspatch
SHA256=/sbin/sha256
PHTTPGET=/usr/libexec/phttpget
# Set up variables relating to VERBOSELEVEL
fetch_setup_verboselevel
# Construct a unique name from ${BASEDIR}
BDHASH=`echo ${BASEDIR} | sha256 -q`
}
# Perform sanity checks etc. before fetching updates.
fetch_check_params () {
fetchupgrade_check_params
if ! [ -z "${TARGETRELEASE}" ]; then
echo -n "`basename $0`: "
echo -n "-r option is meaningless with 'fetch' command. "
echo "(Did you mean 'upgrade' instead?)"
exit 1
fi
# Check that we have updates ready to install
if [ -f ${BDHASH}-install/kerneldone -a $FORCEFETCH -eq 0 ]; then
echo "You have a partially completed upgrade pending"
echo "Run '$0 install' first."
echo "Run '$0 fetch -F' to proceed anyway."
exit 1
fi
}
# Perform sanity checks etc. before fetching upgrades.
upgrade_check_params () {
fetchupgrade_check_params
# Unless set otherwise, we're upgrading to the same kernel config.
NKERNCONF=${KERNCONF}
# We need TARGETRELEASE set
_TARGETRELEASE_z="Release target must be specified via -r option."
if [ -z "${TARGETRELEASE}" ]; then
echo -n "`basename $0`: "
echo "${_TARGETRELEASE_z}"
exit 1
fi
# The target release should be != the current release.
if [ "${TARGETRELEASE}" = "${RELNUM}" ]; then
echo -n "`basename $0`: "
echo "Cannot upgrade from ${RELNUM} to itself"
exit 1
fi
# Turning off AllowAdd or AllowDelete is a bad idea for upgrades.
if [ "${ALLOWADD}" = "no" ]; then
echo -n "`basename $0`: "
echo -n "WARNING: \"AllowAdd no\" is a bad idea "
echo "when upgrading between releases."
echo
fi
if [ "${ALLOWDELETE}" = "no" ]; then
echo -n "`basename $0`: "
echo -n "WARNING: \"AllowDelete no\" is a bad idea "
echo "when upgrading between releases."
echo
fi
# Set EDITOR to /usr/bin/vi if it isn't already set
: ${EDITOR:='/usr/bin/vi'}
}
# Perform sanity checks and set some final parameters in
# preparation for installing updates.
install_check_params () {
# Check that we are root. All sorts of things won't work otherwise.
if [ `id -u` != 0 ]; then
echo "You must be root to run this."
exit 1
fi
# Check that securelevel <= 0. Otherwise we can't update schg files.
if [ `sysctl -n kern.securelevel` -gt 0 ]; then
echo "Updates cannot be installed when the system securelevel"
echo "is greater than zero."
exit 1
fi
# Check that we have a working directory
_WORKDIR_bad="Directory does not exist or is not writable: "
if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
echo -n "`basename $0`: "
echo -n "${_WORKDIR_bad}"
echo ${WORKDIR}
exit 1
fi
cd ${WORKDIR} || exit 1
# Construct a unique name from ${BASEDIR}
BDHASH=`echo ${BASEDIR} | sha256 -q`
# Check that we have updates ready to install
if ! [ -L ${BDHASH}-install ]; then
echo "No updates are available to install."
if [ $ISFETCHED -eq 0 ]; then
echo "Run '$0 fetch' first."
exit 2
fi
exit 0
fi
if ! [ -f ${BDHASH}-install/INDEX-OLD ] ||
! [ -f ${BDHASH}-install/INDEX-NEW ]; then
echo "Update manifest is corrupt -- this should never happen."
echo "Re-run '$0 fetch'."
exit 1
fi
# Figure out what directory contains the running kernel
BOOTFILE=`sysctl -n kern.bootfile`
KERNELDIR=${BOOTFILE%/kernel}
if ! [ -d ${KERNELDIR} ]; then
echo "Cannot identify running kernel"
exit 1
fi
}
# Perform sanity checks and set some final parameters in
# preparation for UNinstalling updates.
rollback_check_params () {
# Check that we are root. All sorts of things won't work otherwise.
if [ `id -u` != 0 ]; then
echo "You must be root to run this."
exit 1
fi
# Check that we have a working directory
_WORKDIR_bad="Directory does not exist or is not writable: "
if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
echo -n "`basename $0`: "
echo -n "${_WORKDIR_bad}"
echo ${WORKDIR}
exit 1
fi
cd ${WORKDIR} || exit 1
# Construct a unique name from ${BASEDIR}
BDHASH=`echo ${BASEDIR} | sha256 -q`
# Check that we have updates ready to rollback
if ! [ -L ${BDHASH}-rollback ]; then
echo "No rollback directory found."
exit 1
fi
if ! [ -f ${BDHASH}-rollback/INDEX-OLD ] ||
! [ -f ${BDHASH}-rollback/INDEX-NEW ]; then
echo "Update manifest is corrupt -- this should never happen."
exit 1
fi
}
# Perform sanity checks and set some final parameters
# in preparation for comparing the system against the
# published index. Figure out which index we should
# compare against: If the user is running *-p[0-9]+,
# strip off the last part; if the user is running
# -SECURITY, call it -RELEASE. Chdir into the working
# directory.
IDS_check_params () {
export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)"
_SERVERNAME_z=\
"SERVERNAME must be given via command line or configuration file."
_KEYPRINT_z="Key must be given via -k option or configuration file."
_KEYPRINT_bad="Invalid key fingerprint: "
_WORKDIR_bad="Directory does not exist or is not writable: "
if [ -z "${SERVERNAME}" ]; then
echo -n "`basename $0`: "
echo "${_SERVERNAME_z}"
exit 1
fi
if [ -z "${KEYPRINT}" ]; then
echo -n "`basename $0`: "
echo "${_KEYPRINT_z}"
exit 1
fi
if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
echo -n "`basename $0`: "
echo -n "${_KEYPRINT_bad}"
echo ${KEYPRINT}
exit 1
fi
if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
echo -n "`basename $0`: "
echo -n "${_WORKDIR_bad}"
echo ${WORKDIR}
exit 1
fi
cd ${WORKDIR} || exit 1
# Generate release number. The s/SECURITY/RELEASE/ bit exists
# to provide an upgrade path for FreeBSD Update 1.x users, since
# the kernels provided by FreeBSD Update 1.x are always labelled
# as X.Y-SECURITY.
RELNUM=`uname -r |
sed -E 's,-p[0-9]+,,' |
sed -E 's,-SECURITY,-RELEASE,'`
ARCH=`uname -m`
FETCHDIR=${RELNUM}/${ARCH}
PATCHDIR=${RELNUM}/${ARCH}/bp
# Figure out what directory contains the running kernel
BOOTFILE=`sysctl -n kern.bootfile`
KERNELDIR=${BOOTFILE%/kernel}
if ! [ -d ${KERNELDIR} ]; then
echo "Cannot identify running kernel"
exit 1
fi
# Figure out what kernel configuration is running. We start with
# the output of `uname -i`, and then make the following adjustments:
# 1. Replace "SMP-GENERIC" with "SMP". Why the SMP kernel config
# file says "ident SMP-GENERIC", I don't know...
# 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64"
# _and_ `sysctl kern.version` contains a line which ends "/SMP", then
# we're running an SMP kernel. This mis-identification is a bug
# which was fixed in 6.2-STABLE.
KERNCONF=`uname -i`
if [ ${KERNCONF} = "SMP-GENERIC" ]; then
KERNCONF=SMP
fi
if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then
if sysctl kern.version | grep -qE '/SMP$'; then
KERNCONF=SMP
fi
fi
# Define some paths
SHA256=/sbin/sha256
PHTTPGET=/usr/libexec/phttpget
# Set up variables relating to VERBOSELEVEL
fetch_setup_verboselevel
}
#### Core functionality -- the actual work gets done here
# Use an SRV query to pick a server. If the SRV query doesn't provide
# a useful answer, use the server name specified by the user.
# Put another way... look up _http._tcp.${SERVERNAME} and pick a server
# from that; or if no servers are returned, use ${SERVERNAME}.
# This allows a user to specify "portsnap.freebsd.org" (in which case
# portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org"
# (in which case portsnap will use that particular server, since there
# won't be an SRV entry for that name).
#
# We ignore the Port field, since we are always going to use port 80.
# Fetch the mirror list, but do not pick a mirror yet. Returns 1 if
# no mirrors are available for any reason.
fetch_pick_server_init () {
: > serverlist_tried
# Check that host(1) exists (i.e., that the system wasn't built with the
# WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist.
if ! which -s host; then
: > serverlist_full
return 1
fi
echo -n "Looking up ${SERVERNAME} mirrors... "
# Issue the SRV query and pull out the Priority, Weight, and Target fields.
# BIND 9 prints "$name has SRV record ..." while BIND 8 prints