4 # Copyright 2004-2007 Colin Percival
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted providing that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
30 #### Usage function -- called from command-line handling code.
32 # Usage instructions. Options not listed:
33 # --debug -- don't filter output from utilities
34 # --no-stats -- don't show progress statistics while fetching files
37 usage: `basename $0` [options] command ... [path]
40 -b basedir -- Operate on a system mounted at basedir
42 -d workdir -- Store working files in workdir
43 (default: /var/db/freebsd-update/)
44 -f conffile -- Read configuration options from conffile
45 (default: /etc/freebsd-update.conf)
46 -k KEY -- Trust an RSA key with SHA256 hash of KEY
47 -r release -- Target for upgrade (e.g., 6.2-RELEASE)
48 -s server -- Server from which to fetch updates
49 (default: update.FreeBSD.org)
50 -t address -- Mail output of cron command, if any, to address
53 fetch -- Fetch updates from server
54 cron -- Sleep rand(3600) seconds, fetch updates, and send an
55 email if updates were found
56 upgrade -- Fetch upgrades to FreeBSD version specified via -r option
57 install -- Install downloaded updates or upgrades
58 rollback -- Uninstall most recently installed updates
59 IDS -- Compare the system against an index of "known good" files.
64 #### Configuration processing functions
67 # Configuration options are set in the following order of priority:
68 # 1. Command line options
69 # 2. Configuration file options
71 # In addition, certain options (e.g., IgnorePaths) can be specified multiple
72 # times and (as long as these are all in the same place, e.g., inside the
73 # configuration file) they will accumulate. Finally, because the path to the
74 # configuration file can be specified at the command line, the entire command
75 # line must be processed before we start reading the configuration file.
77 # Sound like a mess? It is. Here's how we handle this:
78 # 1. Initialize CONFFILE and all the options to "".
79 # 2. Process the command line. Throw an error if a non-accumulating option
81 # 3. If CONFFILE is "", set CONFFILE to /etc/freebsd-update.conf .
82 # 4. For all the configuration options X, set X_saved to X.
83 # 5. Initialize all the options to "".
84 # 6. Read CONFFILE line by line, parsing options.
85 # 7. For each configuration option X, set X to X_saved iff X_saved is not "".
86 # 8. Repeat steps 4-7, except setting options to their default values at (6).
88 CONFIGOPTIONS="KEYPRINT WORKDIR SERVERNAME MAILTO ALLOWADD ALLOWDELETE
89 KEEPMODIFIEDMETADATA COMPONENTS IGNOREPATHS UPDATEIFUNMODIFIED
90 BASEDIR VERBOSELEVEL TARGETRELEASE STRICTCOMPONENTS MERGECHANGES
91 IDSIGNOREPATHS BACKUPKERNEL BACKUPKERNELDIR BACKUPKERNELSYMBOLFILES"
93 # Set all the configuration options to "".
95 for X in ${CONFIGOPTIONS}; do
100 # For each configuration option X, set X_saved to X.
102 for X in ${CONFIGOPTIONS}; do
103 eval ${X}_saved=\$${X}
107 # For each configuration option X, set X to X_saved if X_saved is not "".
109 for X in ${CONFIGOPTIONS}; do
111 if ! [ -z "${_}" ]; then
112 eval ${X}=\$${X}_saved
117 # Set the trusted keyprint.
119 if [ -z ${KEYPRINT} ]; then
126 # Set the working directory.
128 if [ -z ${WORKDIR} ]; then
135 # Set the name of the server (pool) from which to fetch updates
136 config_ServerName () {
137 if [ -z ${SERVERNAME} ]; then
144 # Set the address to which 'cron' output will be mailed.
146 if [ -z ${MAILTO} ]; then
153 # Set whether FreeBSD Update is allowed to add files (or directories, or
154 # symlinks) which did not previously exist.
156 if [ -z ${ALLOWADD} ]; then
173 # Set whether FreeBSD Update is allowed to remove files/directories/symlinks.
174 config_AllowDelete () {
175 if [ -z ${ALLOWDELETE} ]; then
192 # Set whether FreeBSD Update should keep existing inode ownership,
193 # permissions, and flags, in the event that they have been modified locally
195 config_KeepModifiedMetadata () {
196 if [ -z ${KEEPMODIFIEDMETADATA} ]; then
199 KEEPMODIFIEDMETADATA=yes
202 KEEPMODIFIEDMETADATA=no
213 # Add to the list of components which should be kept updated.
214 config_Components () {
216 COMPONENTS="${COMPONENTS} ${C}"
220 # Add to the list of paths under which updates will be ignored.
221 config_IgnorePaths () {
223 IGNOREPATHS="${IGNOREPATHS} ${C}"
227 # Add to the list of paths which IDS should ignore.
228 config_IDSIgnorePaths () {
230 IDSIGNOREPATHS="${IDSIGNOREPATHS} ${C}"
234 # Add to the list of paths within which updates will be performed only if the
235 # file on disk has not been modified locally.
236 config_UpdateIfUnmodified () {
238 UPDATEIFUNMODIFIED="${UPDATEIFUNMODIFIED} ${C}"
242 # Add to the list of paths within which updates to text files will be merged
243 # instead of overwritten.
244 config_MergeChanges () {
246 MERGECHANGES="${MERGECHANGES} ${C}"
250 # Work on a FreeBSD installation mounted under $1
252 if [ -z ${BASEDIR} ]; then
259 # When fetching upgrades, should we assume the user wants exactly the
260 # components listed in COMPONENTS, rather than trying to guess based on
261 # what's currently installed?
262 config_StrictComponents () {
263 if [ -z ${STRICTCOMPONENTS} ]; then
280 # Upgrade to FreeBSD $1
281 config_TargetRelease () {
282 if [ -z ${TARGETRELEASE} ]; then
289 # Define what happens to output of utilities
290 config_VerboseLevel () {
291 if [ -z ${VERBOSELEVEL} ]; then
293 [Dd][Ee][Bb][Uu][Gg])
296 [Nn][Oo][Ss][Tt][Aa][Tt][Ss])
299 [Ss][Tt][Aa][Tt][Ss])
311 config_BackupKernel () {
312 if [ -z ${BACKUPKERNEL} ]; then
329 config_BackupKernelDir () {
330 if [ -z ${BACKUPKERNELDIR} ]; then
332 echo "BackupKernelDir set to empty dir"
336 # We check for some paths which would be extremely odd
337 # to use, but which could cause a lot of problems if
340 /|/bin|/boot|/etc|/lib|/libexec|/sbin|/usr|/var)
341 echo "BackupKernelDir set to invalid path $1"
348 echo "BackupKernelDir ($1) is not an absolute path"
357 config_BackupKernelSymbolFiles () {
358 if [ -z ${BACKUPKERNELSYMBOLFILES} ]; then
361 BACKUPKERNELSYMBOLFILES=yes
364 BACKUPKERNELSYMBOLFILES=no
375 # Handle one line of configuration
377 if [ $# -eq 0 ]; then
386 #### Parameter handling functions.
388 # Initialize parameters to null, just in case they're
389 # set in the environment.
391 # Configration settings
394 # No configuration file set yet
397 # No commands specified yet
401 # Parse the command line
403 while [ $# -gt 0 ]; do
405 # Location of configuration file
407 if [ $# -eq 1 ]; then usage; fi
408 if [ ! -z "${CONFFILE}" ]; then usage; fi
412 # Configuration file equivalents
414 if [ $# -eq 1 ]; then usage; fi; shift
415 config_BaseDir $1 || usage
418 if [ $# -eq 1 ]; then usage; fi; shift
419 config_WorkDir $1 || usage
422 if [ $# -eq 1 ]; then usage; fi; shift
423 config_KeyPrint $1 || usage
426 if [ $# -eq 1 ]; then usage; fi; shift
427 config_ServerName $1 || usage
430 if [ $# -eq 1 ]; then usage; fi; shift
431 config_TargetRelease $1 || usage
434 if [ $# -eq 1 ]; then usage; fi; shift
435 config_MailTo $1 || usage
438 if [ $# -eq 1 ]; then usage; fi; shift
439 config_VerboseLevel $1 || usage
442 # Aliases for "-v debug" and "-v nostats"
444 config_VerboseLevel debug || usage
447 config_VerboseLevel nostats || usage
451 cron | fetch | upgrade | install | rollback | IDS)
452 COMMANDS="${COMMANDS} $1"
455 # Anything else is an error
463 # Make sure we have at least one command
464 if [ -z "${COMMANDS}" ]; then
469 # Parse the configuration file
471 # If a configuration file was specified on the command line, check
472 # that it exists and is readable.
473 if [ ! -z "${CONFFILE}" ] && [ ! -r "${CONFFILE}" ]; then
474 echo -n "File does not exist "
475 echo -n "or is not readable: "
480 # If a configuration file was not specified on the command line,
481 # use the default configuration file path. If that default does
482 # not exist, give up looking for any configuration.
483 if [ -z "${CONFFILE}" ]; then
484 CONFFILE="/etc/freebsd-update.conf"
485 if [ ! -r "${CONFFILE}" ]; then
490 # Save the configuration options specified on the command line, and
491 # clear all the options in preparation for reading the config file.
495 # Read the configuration file. Anything after the first '#' is
496 # ignored, and any blank lines are ignored.
500 LINEX=`echo "${LINE}" | cut -f 1 -d '#'`
501 if ! configline ${LINEX}; then
502 echo "Error processing configuration file, line $L:"
508 # Merge the settings read from the configuration file with those
509 # provided at the command line.
513 # Provide some default parameters
515 # Save any parameters already configured, and clear the slate
519 # Default configurations
520 config_WorkDir /var/db/freebsd-update
523 config_AllowDelete yes
524 config_KeepModifiedMetadata yes
526 config_VerboseLevel stats
527 config_StrictComponents no
528 config_BackupKernel yes
529 config_BackupKernelDir /boot/kernel.old
530 config_BackupKernelSymbolFiles no
532 # Merge these defaults into the earlier-configured settings
536 # Set utility output filtering options, based on ${VERBOSELEVEL}
537 fetch_setup_verboselevel () {
538 case ${VERBOSELEVEL} in
540 QUIETREDIR="/dev/stderr"
542 STATSREDIR="/dev/stderr"
550 STATSREDIR="/dev/null"
556 QUIETREDIR="/dev/null"
558 STATSREDIR="/dev/stdout"
566 # Perform sanity checks and set some final parameters
567 # in preparation for fetching files. Figure out which
568 # set of updates should be downloaded: If the user is
569 # running *-p[0-9]+, strip off the last part; if the
570 # user is running -SECURITY, call it -RELEASE. Chdir
571 # into the working directory.
572 fetch_check_params () {
573 export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)"
576 "SERVERNAME must be given via command line or configuration file."
577 _KEYPRINT_z="Key must be given via -k option or configuration file."
578 _KEYPRINT_bad="Invalid key fingerprint: "
579 _WORKDIR_bad="Directory does not exist or is not writable: "
581 if [ -z "${SERVERNAME}" ]; then
582 echo -n "`basename $0`: "
583 echo "${_SERVERNAME_z}"
586 if [ -z "${KEYPRINT}" ]; then
587 echo -n "`basename $0`: "
588 echo "${_KEYPRINT_z}"
591 if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
592 echo -n "`basename $0`: "
593 echo -n "${_KEYPRINT_bad}"
597 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
598 echo -n "`basename $0`: "
599 echo -n "${_WORKDIR_bad}"
603 cd ${WORKDIR} || exit 1
605 # Generate release number. The s/SECURITY/RELEASE/ bit exists
606 # to provide an upgrade path for FreeBSD Update 1.x users, since
607 # the kernels provided by FreeBSD Update 1.x are always labelled
610 sed -E 's,-p[0-9]+,,' |
611 sed -E 's,-SECURITY,-RELEASE,'`
613 FETCHDIR=${RELNUM}/${ARCH}
614 PATCHDIR=${RELNUM}/${ARCH}/bp
616 # Figure out what directory contains the running kernel
617 BOOTFILE=`sysctl -n kern.bootfile`
618 KERNELDIR=${BOOTFILE%/kernel}
619 if ! [ -d ${KERNELDIR} ]; then
620 echo "Cannot identify running kernel"
624 # Figure out what kernel configuration is running. We start with
625 # the output of `uname -i`, and then make the following adjustments:
626 # 1. Replace "SMP-GENERIC" with "SMP". Why the SMP kernel config
627 # file says "ident SMP-GENERIC", I don't know...
628 # 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64"
629 # _and_ `sysctl kern.version` contains a line which ends "/SMP", then
630 # we're running an SMP kernel. This mis-identification is a bug
631 # which was fixed in 6.2-STABLE.
633 if [ ${KERNCONF} = "SMP-GENERIC" ]; then
636 if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then
637 if sysctl kern.version | grep -qE '/SMP$'; then
643 BSPATCH=/usr/bin/bspatch
645 PHTTPGET=/usr/libexec/phttpget
647 # Set up variables relating to VERBOSELEVEL
648 fetch_setup_verboselevel
650 # Construct a unique name from ${BASEDIR}
651 BDHASH=`echo ${BASEDIR} | sha256 -q`
654 # Perform sanity checks etc. before fetching upgrades.
655 upgrade_check_params () {
658 # Unless set otherwise, we're upgrading to the same kernel config.
659 NKERNCONF=${KERNCONF}
661 # We need TARGETRELEASE set
662 _TARGETRELEASE_z="Release target must be specified via -r option."
663 if [ -z "${TARGETRELEASE}" ]; then
664 echo -n "`basename $0`: "
665 echo "${_TARGETRELEASE_z}"
669 # The target release should be != the current release.
670 if [ "${TARGETRELEASE}" = "${RELNUM}" ]; then
671 echo -n "`basename $0`: "
672 echo "Cannot upgrade from ${RELNUM} to itself"
676 # Turning off AllowAdd or AllowDelete is a bad idea for upgrades.
677 if [ "${ALLOWADD}" = "no" ]; then
678 echo -n "`basename $0`: "
679 echo -n "WARNING: \"AllowAdd no\" is a bad idea "
680 echo "when upgrading between releases."
683 if [ "${ALLOWDELETE}" = "no" ]; then
684 echo -n "`basename $0`: "
685 echo -n "WARNING: \"AllowDelete no\" is a bad idea "
686 echo "when upgrading between releases."
690 # Set EDITOR to /usr/bin/vi if it isn't already set
691 : ${EDITOR:='/usr/bin/vi'}
694 # Perform sanity checks and set some final parameters in
695 # preparation for installing updates.
696 install_check_params () {
697 # Check that we are root. All sorts of things won't work otherwise.
698 if [ `id -u` != 0 ]; then
699 echo "You must be root to run this."
703 # Check that securelevel <= 0. Otherwise we can't update schg files.
704 if [ `sysctl -n kern.securelevel` -gt 0 ]; then
705 echo "Updates cannot be installed when the system securelevel"
706 echo "is greater than zero."
710 # Check that we have a working directory
711 _WORKDIR_bad="Directory does not exist or is not writable: "
712 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
713 echo -n "`basename $0`: "
714 echo -n "${_WORKDIR_bad}"
718 cd ${WORKDIR} || exit 1
720 # Construct a unique name from ${BASEDIR}
721 BDHASH=`echo ${BASEDIR} | sha256 -q`
723 # Check that we have updates ready to install
724 if ! [ -L ${BDHASH}-install ]; then
725 echo "No updates are available to install."
726 echo "Run '$0 fetch' first."
729 if ! [ -f ${BDHASH}-install/INDEX-OLD ] ||
730 ! [ -f ${BDHASH}-install/INDEX-NEW ]; then
731 echo "Update manifest is corrupt -- this should never happen."
732 echo "Re-run '$0 fetch'."
736 # Figure out what directory contains the running kernel
737 BOOTFILE=`sysctl -n kern.bootfile`
738 KERNELDIR=${BOOTFILE%/kernel}
739 if ! [ -d ${KERNELDIR} ]; then
740 echo "Cannot identify running kernel"
745 # Perform sanity checks and set some final parameters in
746 # preparation for UNinstalling updates.
747 rollback_check_params () {
748 # Check that we are root. All sorts of things won't work otherwise.
749 if [ `id -u` != 0 ]; then
750 echo "You must be root to run this."
754 # Check that we have a working directory
755 _WORKDIR_bad="Directory does not exist or is not writable: "
756 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
757 echo -n "`basename $0`: "
758 echo -n "${_WORKDIR_bad}"
762 cd ${WORKDIR} || exit 1
764 # Construct a unique name from ${BASEDIR}
765 BDHASH=`echo ${BASEDIR} | sha256 -q`
767 # Check that we have updates ready to rollback
768 if ! [ -L ${BDHASH}-rollback ]; then
769 echo "No rollback directory found."
772 if ! [ -f ${BDHASH}-rollback/INDEX-OLD ] ||
773 ! [ -f ${BDHASH}-rollback/INDEX-NEW ]; then
774 echo "Update manifest is corrupt -- this should never happen."
779 # Perform sanity checks and set some final parameters
780 # in preparation for comparing the system against the
781 # published index. Figure out which index we should
782 # compare against: If the user is running *-p[0-9]+,
783 # strip off the last part; if the user is running
784 # -SECURITY, call it -RELEASE. Chdir into the working
786 IDS_check_params () {
787 export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)"
790 "SERVERNAME must be given via command line or configuration file."
791 _KEYPRINT_z="Key must be given via -k option or configuration file."
792 _KEYPRINT_bad="Invalid key fingerprint: "
793 _WORKDIR_bad="Directory does not exist or is not writable: "
795 if [ -z "${SERVERNAME}" ]; then
796 echo -n "`basename $0`: "
797 echo "${_SERVERNAME_z}"
800 if [ -z "${KEYPRINT}" ]; then
801 echo -n "`basename $0`: "
802 echo "${_KEYPRINT_z}"
805 if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
806 echo -n "`basename $0`: "
807 echo -n "${_KEYPRINT_bad}"
811 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
812 echo -n "`basename $0`: "
813 echo -n "${_WORKDIR_bad}"
817 cd ${WORKDIR} || exit 1
819 # Generate release number. The s/SECURITY/RELEASE/ bit exists
820 # to provide an upgrade path for FreeBSD Update 1.x users, since
821 # the kernels provided by FreeBSD Update 1.x are always labelled
824 sed -E 's,-p[0-9]+,,' |
825 sed -E 's,-SECURITY,-RELEASE,'`
827 FETCHDIR=${RELNUM}/${ARCH}
828 PATCHDIR=${RELNUM}/${ARCH}/bp
830 # Figure out what directory contains the running kernel
831 BOOTFILE=`sysctl -n kern.bootfile`
832 KERNELDIR=${BOOTFILE%/kernel}
833 if ! [ -d ${KERNELDIR} ]; then
834 echo "Cannot identify running kernel"
838 # Figure out what kernel configuration is running. We start with
839 # the output of `uname -i`, and then make the following adjustments:
840 # 1. Replace "SMP-GENERIC" with "SMP". Why the SMP kernel config
841 # file says "ident SMP-GENERIC", I don't know...
842 # 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64"
843 # _and_ `sysctl kern.version` contains a line which ends "/SMP", then
844 # we're running an SMP kernel. This mis-identification is a bug
845 # which was fixed in 6.2-STABLE.
847 if [ ${KERNCONF} = "SMP-GENERIC" ]; then
850 if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then
851 if sysctl kern.version | grep -qE '/SMP$'; then
858 PHTTPGET=/usr/libexec/phttpget
860 # Set up variables relating to VERBOSELEVEL
861 fetch_setup_verboselevel
864 #### Core functionality -- the actual work gets done here
866 # Use an SRV query to pick a server. If the SRV query doesn't provide
867 # a useful answer, use the server name specified by the user.
868 # Put another way... look up _http._tcp.${SERVERNAME} and pick a server
869 # from that; or if no servers are returned, use ${SERVERNAME}.
870 # This allows a user to specify "portsnap.freebsd.org" (in which case
871 # portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org"
872 # (in which case portsnap will use that particular server, since there
873 # won't be an SRV entry for that name).
875 # We ignore the Port field, since we are always going to use port 80.
877 # Fetch the mirror list, but do not pick a mirror yet. Returns 1 if
878 # no mirrors are available for any reason.
879 fetch_pick_server_init () {
882 # Check that host(1) exists (i.e., that the system wasn't built with the
883 # WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist.
884 if ! which -s host; then
889 echo -n "Looking up ${SERVERNAME} mirrors... "
891 # Issue the SRV query and pull out the Priority, Weight, and Target fields.
892 # BIND 9 prints "$name has SRV record ..." while BIND 8 prints
893 # "$name server selection ..."; we allow either format.
894 MLIST="_http._tcp.${SERVERNAME}"
895 host -t srv "${MLIST}" |
896 sed -nE "s/${MLIST} (has SRV record|server selection) //p" |
897 cut -f 1,2,4 -d ' ' |
899 sort > serverlist_full
901 # If no records, give up -- we'll just use the server name we were given.
902 if [ `wc -l < serverlist_full` -eq 0 ]; then
907 # Report how many mirrors we found.
908 echo `wc -l < serverlist_full` "mirrors found."
910 # Generate a random seed for use in picking mirrors. If HTTP_PROXY
911 # is set, this will be used to generate the seed; otherwise, the seed
913 if [ -n "${HTTP_PROXY}${http_proxy}" ]; then
914 RANDVALUE=`sha256 -qs "${HTTP_PROXY}${http_proxy}" |
918 RANDVALUE=`jot -r 1 0 999999999`
922 # Pick a mirror. Returns 1 if we have run out of mirrors to try.
923 fetch_pick_server () {
924 # Generate a list of not-yet-tried mirrors
925 sort serverlist_tried |
926 comm -23 serverlist_full - > serverlist
928 # Have we run out of mirrors?
929 if [ `wc -l < serverlist` -eq 0 ]; then
930 echo "No mirrors remaining, giving up."
934 # Find the highest priority level (lowest numeric value).
935 SRV_PRIORITY=`cut -f 1 -d ' ' serverlist | sort -n | head -1`
937 # Add up the weights of the response lines at that priority level.
942 SRV_W=`echo $X | cut -f 2 -d ' '`
943 SRV_WSUM=$(($SRV_WSUM + $SRV_W))
948 # If all the weights are 0, pretend that they are all 1 instead.
949 if [ ${SRV_WSUM} -eq 0 ]; then
950 SRV_WSUM=`grep -E "^${SRV_PRIORITY} " serverlist | wc -l`
956 # Pick a value between 0 and the sum of the weights - 1
957 SRV_RND=`expr ${RANDVALUE} % ${SRV_WSUM}`
959 # Read through the list of mirrors and set SERVERNAME. Write the line
960 # corresponding to the mirror we selected into serverlist_tried so that
961 # we won't try it again.
965 SRV_W=`echo $X | cut -f 2 -d ' '`
966 SRV_W=$(($SRV_W + $SRV_W_ADD))
967 if [ $SRV_RND -lt $SRV_W ]; then
968 SERVERNAME=`echo $X | cut -f 3 -d ' '`
969 echo "$X" >> serverlist_tried
972 SRV_RND=$(($SRV_RND - $SRV_W))
979 # Take a list of ${oldhash}|${newhash} and output a list of needed patches,
980 # i.e., those for which we have ${oldhash} and don't have ${newhash}.
981 fetch_make_patchlist () {
982 grep -vE "^([0-9a-f]{64})\|\1$" |
985 if [ -f "files/${Y}.gz" ] ||
986 [ ! -f "files/${X}.gz" ]; then
993 # Print user-friendly progress statistics
998 if [ $(($LNC % 10)) = 0 ]; then
1000 elif [ $(($LNC % 2)) = 0 ]; then
1007 # Function for asking the user if everything is ok
1009 while read -p "Does this look reasonable (y/n)? " CONTINUE; do
1010 case "${CONTINUE}" in
1021 # Initialize the working directory
1024 touch tINDEX.present
1027 # Check that we have a public key with an appropriate hash, or
1028 # fetch the key if it doesn't exist. Returns 1 if the key has
1029 # not yet been fetched.
1031 if [ -r pub.ssl ] && [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
1035 echo -n "Fetching public key from ${SERVERNAME}... "
1037 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/pub.ssl \
1038 2>${QUIETREDIR} || true
1039 if ! [ -r pub.ssl ]; then
1043 if ! [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
1044 echo "key has incorrect hash."
1051 # Fetch metadata signature, aka "tag".
1053 echo -n "Fetching metadata signature "
1054 echo ${NDEBUG} "for ${RELNUM} from ${SERVERNAME}... "
1056 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/latest.ssl \
1057 2>${QUIETREDIR} || true
1058 if ! [ -r latest.ssl ]; then
1063 openssl rsautl -pubin -inkey pub.ssl -verify \
1064 < latest.ssl > tag.new 2>${QUIETREDIR} || true
1067 if ! [ `wc -l < tag.new` = 1 ] ||
1069 "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \
1071 echo "invalid signature."
1077 RELPATCHNUM=`cut -f 4 -d '|' < tag.new`
1078 TINDEXHASH=`cut -f 5 -d '|' < tag.new`
1079 EOLTIME=`cut -f 6 -d '|' < tag.new`
1082 # Sanity-check the patch number in a tag, to make sure that we're not
1083 # going to "update" backwards and to prevent replay attacks.
1084 fetch_tagsanity () {
1085 # Check that we're not going to move from -pX to -pY with Y < X.
1086 RELPX=`uname -r | sed -E 's,.*-,,'`
1087 if echo ${RELPX} | grep -qE '^p[0-9]+$'; then
1088 RELPX=`echo ${RELPX} | cut -c 2-`
1092 if [ "${RELPATCHNUM}" -lt "${RELPX}" ]; then
1094 echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})"
1095 echo " appear older than what"
1096 echo "we are currently running (`uname -r`)!"
1097 echo "Cowardly refusing to proceed any further."
1101 # If "tag" exists and corresponds to ${RELNUM}, make sure that
1102 # it contains a patch number <= RELPATCHNUM, in order to protect
1103 # against rollback (replay) attacks.
1106 "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \
1108 LASTRELPATCHNUM=`cut -f 4 -d '|' < tag`
1110 if [ "${RELPATCHNUM}" -lt "${LASTRELPATCHNUM}" ]; then
1112 echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})"
1113 echo " are older than the"
1114 echo -n "most recently seen updates"
1115 echo " (${RELNUM}-p${LASTRELPATCHNUM})."
1116 echo "Cowardly refusing to proceed any further."
1122 # Fetch metadata index file
1123 fetch_metadata_index () {
1124 echo ${NDEBUG} "Fetching metadata index... "
1126 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/t/${TINDEXHASH}
1128 if ! [ -f ${TINDEXHASH} ]; then
1132 if [ `${SHA256} -q ${TINDEXHASH}` != ${TINDEXHASH} ]; then
1133 echo "update metadata index corrupt."
1139 # Print an error message about signed metadata being bogus.
1140 fetch_metadata_bogus () {
1142 echo "The update metadata$1 is correctly signed, but"
1143 echo "failed an integrity check."
1144 echo "Cowardly refusing to proceed any further."
1148 # Construct tINDEX.new by merging the lines named in $1 from ${TINDEXHASH}
1149 # with the lines not named in $@ from tINDEX.present (if that file exists).
1150 fetch_metadata_index_merge () {
1151 for METAFILE in $@; do
1152 if [ `grep -E "^${METAFILE}\|" ${TINDEXHASH} | wc -l` \
1154 fetch_metadata_bogus " index"
1158 grep -E "${METAFILE}\|" ${TINDEXHASH}
1160 sort > tINDEX.wanted
1162 if [ -f tINDEX.present ]; then
1163 join -t '|' -v 2 tINDEX.wanted tINDEX.present |
1164 sort -m - tINDEX.wanted > tINDEX.new
1167 mv tINDEX.wanted tINDEX.new
1171 # Sanity check all the lines of tINDEX.new. Even if more metadata lines
1172 # are added by future versions of the server, this won't cause problems,
1173 # since the only lines which appear in tINDEX.new are the ones which we
1174 # specifically grepped out of ${TINDEXHASH}.
1175 fetch_metadata_index_sanity () {
1176 if grep -qvE '^[0-9A-Z.-]+\|[0-9a-f]{64}$' tINDEX.new; then
1177 fetch_metadata_bogus " index"
1182 # Sanity check the metadata file $1.
1183 fetch_metadata_sanity () {
1184 # Some aliases to save space later: ${P} is a character which can
1185 # appear in a path; ${M} is the four numeric metadata fields; and
1186 # ${H} is a sha256 hash.
1187 P="[-+./:=_[[:alnum:]]"
1188 M="[0-9]+\|[0-9]+\|[0-9]+\|[0-9]+"
1191 # Check that the first four fields make sense.
1192 if gunzip -c < files/$1.gz |
1193 grep -qvE "^[a-z]+\|[0-9a-z]+\|${P}+\|[fdL-]\|"; then
1194 fetch_metadata_bogus ""
1198 # Remove the first three fields.
1199 gunzip -c < files/$1.gz |
1200 cut -f 4- -d '|' > sanitycheck.tmp
1202 # Sanity check entries with type 'f'
1203 if grep -E '^f' sanitycheck.tmp |
1204 grep -qvE "^f\|${M}\|${H}\|${P}*\$"; then
1205 fetch_metadata_bogus ""
1209 # Sanity check entries with type 'd'
1210 if grep -E '^d' sanitycheck.tmp |
1211 grep -qvE "^d\|${M}\|\|\$"; then
1212 fetch_metadata_bogus ""
1216 # Sanity check entries with type 'L'
1217 if grep -E '^L' sanitycheck.tmp |
1218 grep -qvE "^L\|${M}\|${P}*\|\$"; then
1219 fetch_metadata_bogus ""
1223 # Sanity check entries with type '-'
1224 if grep -E '^-' sanitycheck.tmp |
1225 grep -qvE "^-\|\|\|\|\|\|"; then
1226 fetch_metadata_bogus ""
1234 # Fetch the metadata index and metadata files listed in $@,
1235 # taking advantage of metadata patches where possible.
1237 fetch_metadata_index || return 1
1238 fetch_metadata_index_merge $@ || return 1
1239 fetch_metadata_index_sanity || return 1
1241 # Generate a list of wanted metadata patches
1242 join -t '|' -o 1.2,2.2 tINDEX.present tINDEX.new |
1243 fetch_make_patchlist > patchlist
1245 if [ -s patchlist ]; then
1246 # Attempt to fetch metadata patches
1247 echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
1248 echo ${NDEBUG} "metadata patches.${DDSTATS}"
1249 tr '|' '-' < patchlist |
1250 lam -s "${FETCHDIR}/tp/" - -s ".gz" |
1251 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1252 2>${STATSREDIR} | fetch_progress
1255 # Attempt to apply metadata patches
1256 echo -n "Applying metadata patches... "
1257 tr '|' ' ' < patchlist |
1259 if [ ! -f "${X}-${Y}.gz" ]; then continue; fi
1260 gunzip -c < ${X}-${Y}.gz > diff
1261 gunzip -c < files/${X}.gz > diff-OLD
1263 # Figure out which lines are being added and removed
1266 while read PREFIX; do
1267 look "${PREFIX}" diff-OLD
1270 grep -E '^\+' diff |
1271 cut -c 2- > diff-add
1273 # Generate the new file
1274 comm -23 diff-OLD diff-rm |
1275 sort - diff-add > diff-NEW
1277 if [ `${SHA256} -q diff-NEW` = ${Y} ]; then
1278 mv diff-NEW files/${Y}
1281 mv diff-NEW ${Y}.bad
1283 rm -f ${X}-${Y}.gz diff
1284 rm -f diff-OLD diff-NEW diff-add diff-rm
1285 done 2>${QUIETREDIR}
1289 # Update metadata without patches
1290 cut -f 2 -d '|' < tINDEX.new |
1292 if [ ! -f "files/${Y}.gz" ]; then
1298 if [ -s filelist ]; then
1299 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
1300 echo ${NDEBUG} "metadata files... "
1301 lam -s "${FETCHDIR}/m/" - -s ".gz" < filelist |
1302 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1306 if ! [ -f ${Y}.gz ]; then
1310 if [ `gunzip -c < ${Y}.gz |
1311 ${SHA256} -q` = ${Y} ]; then
1312 mv ${Y}.gz files/${Y}.gz
1314 echo "metadata is corrupt."
1321 # Sanity-check the metadata files.
1322 cut -f 2 -d '|' tINDEX.new > filelist
1324 fetch_metadata_sanity ${X} || return 1
1327 # Remove files which are no longer needed
1328 cut -f 2 -d '|' tINDEX.present |
1330 cut -f 2 -d '|' tINDEX.new |
1332 comm -13 - oldfiles |
1333 lam -s "files/" - -s ".gz" |
1335 rm patchlist filelist oldfiles
1339 mv tINDEX.new tINDEX.present
1345 # Extract a subset of a downloaded metadata file containing only the parts
1346 # which are listed in COMPONENTS.
1347 fetch_filter_metadata_components () {
1348 METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'`
1349 gunzip -c < files/${METAHASH}.gz > $1.all
1351 # Fish out the lines belonging to components we care about.
1352 for C in ${COMPONENTS}; do
1353 look "`echo ${C} | tr '/' '|'`|" $1.all
1356 # Remove temporary file.
1360 # Generate a filtered version of the metadata file $1 from the downloaded
1361 # file, by fishing out the lines corresponding to components we're trying
1362 # to keep updated, and then removing lines corresponding to paths we want
1364 fetch_filter_metadata () {
1365 # Fish out the lines belonging to components we care about.
1366 fetch_filter_metadata_components $1
1368 # Canonicalize directory names by removing any trailing / in
1369 # order to avoid listing directories multiple times if they
1370 # belong to multiple components. Turning "/" into "" doesn't
1371 # matter, since we add a leading "/" when we use paths later.
1372 cut -f 3- -d '|' $1 |
1373 sed -e 's,/|d|,|d|,' |
1376 # Figure out which lines to ignore and remove them.
1377 for X in ${IGNOREPATHS}; do
1378 grep -E "^${X}" $1.tmp
1381 comm -13 - $1.tmp > $1
1383 # Remove temporary files.
1387 # Filter the metadata file $1 by adding lines with "/boot/$2"
1388 # replaced by ${KERNELDIR} (which is `sysctl -n kern.bootfile` minus the
1389 # trailing "/kernel"); and if "/boot/$2" does not exist, remove
1390 # the original lines which start with that.
1391 # Put another way: Deal with the fact that the FOO kernel is sometimes
1392 # installed in /boot/FOO/ and is sometimes installed elsewhere.
1393 fetch_filter_kernel_names () {
1395 sed -e "s,/boot/$2,${KERNELDIR},g" |
1399 if ! [ -d /boot/$2 ]; then
1400 grep -v ^/boot/$2 $1 > $1.tmp
1405 # For all paths appearing in $1 or $3, inspect the system
1406 # and generate $2 describing what is currently installed.
1407 fetch_inspect_system () {
1411 # Tell the user why his disk is suddenly making lots of noise
1412 echo -n "Inspecting system... "
1414 # Generate list of files to inspect
1419 # Examine each file and output lines of the form
1420 # /path/to/file|type|device-inum|user|group|perm|flags|value
1421 # sorted by device and inode number.
1423 # If the symlink/file/directory does not exist, record this.
1424 if ! [ -e ${BASEDIR}/${F} ]; then
1428 if ! [ -r ${BASEDIR}/${F} ]; then
1429 echo "Cannot read file: ${BASEDIR}/${F}" \
1435 # Otherwise, output an index line.
1436 if [ -L ${BASEDIR}/${F} ]; then
1438 stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1439 readlink ${BASEDIR}/${F};
1440 elif [ -f ${BASEDIR}/${F} ]; then
1442 stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1443 sha256 -q ${BASEDIR}/${F};
1444 elif [ -d ${BASEDIR}/${F} ]; then
1446 stat -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1448 echo "Unknown file type: ${BASEDIR}/${F}" \
1454 sort -k 3,3 -t '|' > $2.tmp
1457 # Check if an error occured during system inspection
1458 if [ -f .err ]; then
1462 # Convert to the form
1463 # /path/to/file|type|user|group|perm|flags|value|hlink
1464 # by resolving identical device and inode numbers into hard links.
1465 cut -f 1,3 -d '|' $2.tmp |
1466 sort -k 1,1 -t '|' |
1467 sort -s -u -k 2,2 -t '|' |
1468 join -1 2 -2 3 -t '|' - $2.tmp |
1469 awk -F \| -v OFS=\| \
1471 if (($2 == $3) || ($4 == "-"))
1472 print $3,$4,$5,$6,$7,$8,$9,""
1474 print $3,$4,$5,$6,$7,$8,$9,$2
1479 # We're finished looking around
1483 # For any paths matching ${MERGECHANGES}, compare $1 and $2 and find any
1484 # files which differ; generate $3 containing these paths and the old hashes.
1485 fetch_filter_mergechanges () {
1486 # Pull out the paths and hashes of the files matching ${MERGECHANGES}.
1488 for X in ${MERGECHANGES}; do
1489 grep -E "^${X}" ${F}
1491 cut -f 1,2,7 -d '|' |
1495 # Any line in $2-values which doesn't appear in $1-values and is a
1496 # file means that we should list the path in $3.
1497 comm -13 $1-values $2-values |
1499 cut -f 1 -d '|' > $2-paths
1501 # For each path, pull out one (and only one!) entry from $1-values.
1502 # Note that we cannot distinguish which "old" version the user made
1503 # changes to; but hopefully any changes which occur due to security
1504 # updates will exist in both the "new" version and the version which
1505 # the user has installed, so the merging will still work.
1507 look "${X}|" $1-values |
1509 done < $2-paths > $3
1512 rm $1-values $2-values $2-paths
1515 # For any paths matching ${UPDATEIFUNMODIFIED}, remove lines from $[123]
1516 # which correspond to lines in $2 with hashes not matching $1 or $3, unless
1517 # the paths are listed in $4. For entries in $2 marked "not present"
1518 # (aka. type -), remove lines from $[123] unless there is a corresponding
1520 fetch_filter_unmodified_notpresent () {
1521 # Figure out which lines of $1 and $3 correspond to bits which
1522 # should only be updated if they haven't changed, and fish out
1523 # the (path, type, value) tuples.
1524 # NOTE: We don't consider a file to be "modified" if it matches
1526 for X in ${UPDATEIFUNMODIFIED}; do
1530 cut -f 1,2,7 -d '|' |
1533 # Do the same for $2.
1534 for X in ${UPDATEIFUNMODIFIED}; do
1537 cut -f 1,2,7 -d '|' |
1540 # Any entry in $2-values which is not in $1-values corresponds to
1541 # a path which we need to remove from $1, $2, and $3, unless it
1542 # that path appears in $4.
1543 comm -13 $1-values $2-values |
1544 sort -t '|' -k 1,1 > mlines.tmp
1545 cut -f 1 -d '|' $4 |
1547 join -v 2 -t '|' - mlines.tmp |
1549 rm $1-values $2-values mlines.tmp
1551 # Any lines in $2 which are not in $1 AND are "not present" lines
1552 # also belong in mlines.
1554 cut -f 1,2,7 -d '|' |
1555 fgrep '|-|' >> mlines
1557 # Remove lines from $1, $2, and $3
1558 for X in $1 $2 $3; do
1559 sort -t '|' -k 1,1 ${X} > ${X}.tmp
1560 cut -f 1 -d '|' < mlines |
1562 join -v 2 -t '|' - ${X}.tmp |
1567 # Store a list of the modified files, for future reference
1568 fgrep -v '|-|' mlines |
1569 cut -f 1 -d '|' > modifiedfiles
1573 # For each entry in $1 of type -, remove any corresponding
1574 # entry from $2 if ${ALLOWADD} != "yes". Remove all entries
1575 # of type - from $1.
1576 fetch_filter_allowadd () {
1577 cut -f 1,2 -d '|' < $1 |
1579 cut -f 1 -d '|' > filesnotpresent
1581 if [ ${ALLOWADD} != "yes" ]; then
1583 join -v 1 -t '|' - filesnotpresent |
1589 join -v 1 -t '|' - filesnotpresent |
1595 # If ${ALLOWDELETE} != "yes", then remove any entries from $1
1596 # which don't correspond to entries in $2.
1597 fetch_filter_allowdelete () {
1598 # Produce a lists ${PATH}|${TYPE}
1600 cut -f 1-2 -d '|' < ${X} |
1601 sort -u > ${X}.nodes
1604 # Figure out which lines need to be removed from $1.
1605 if [ ${ALLOWDELETE} != "yes" ]; then
1606 comm -23 $1.nodes $2.nodes > $1.badnodes
1611 # Remove the relevant lines from $1
1614 done < $1.badnodes |
1615 comm -13 - $1 > $1.tmp
1618 rm $1.badnodes $1.nodes $2.nodes
1621 # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in $2
1622 # with metadata not matching any entry in $1, replace the corresponding
1623 # line of $3 with one having the same metadata as the entry in $2.
1624 fetch_filter_modified_metadata () {
1625 # Fish out the metadata from $1 and $2
1627 cut -f 1-6 -d '|' < ${X} > ${X}.metadata
1630 # Find the metadata we need to keep
1631 if [ ${KEEPMODIFIEDMETADATA} = "yes" ]; then
1632 comm -13 $1.metadata $2.metadata > keepmeta
1637 # Extract the lines which we need to remove from $3, and
1638 # construct the lines which we need to add to $3.
1642 NODE=`echo "${LINE}" | cut -f 1-2 -d '|'`
1643 look "${NODE}|" $3 >> $3.remove
1644 look "${NODE}|" $3 |
1646 lam -s "${LINE}|" - >> $3.add
1649 # Remove the specified lines and add the new lines.
1652 sort -u - $3.add > $3.tmp
1655 rm keepmeta $1.metadata $2.metadata $3.add $3.remove
1658 # Remove lines from $1 and $2 which are identical;
1659 # no need to update a file if it isn't changing.
1660 fetch_filter_uptodate () {
1661 comm -23 $1 $2 > $1.tmp
1662 comm -13 $1 $2 > $2.tmp
1668 # Fetch any "clean" old versions of files we need for merging changes.
1669 fetch_files_premerge () {
1670 # We only need to do anything if $1 is non-empty.
1672 # Tell the user what we're doing
1673 echo -n "Fetching files from ${OLDRELNUM} for merging... "
1675 # List of files wanted
1678 sort -u > files.wanted
1680 # Only fetch the files we don't already have
1682 if [ ! -f "files/${Y}.gz" ]; then
1685 done < files.wanted > filelist
1687 # Actually fetch them
1688 lam -s "${OLDFETCHDIR}/f/" - -s ".gz" < filelist |
1689 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1692 # Make sure we got them all, and move them into /files/
1694 if ! [ -f ${Y}.gz ]; then
1698 if [ `gunzip -c < ${Y}.gz |
1699 ${SHA256} -q` = ${Y} ]; then
1700 mv ${Y}.gz files/${Y}.gz
1702 echo "${Y} has incorrect hash."
1709 rm filelist files.wanted
1713 # Prepare to fetch files: Generate a list of the files we need,
1714 # copy the unmodified files we have into /files/, and generate
1715 # a list of patches to download.
1716 fetch_files_prepare () {
1717 # Tell the user why his disk is suddenly making lots of noise
1718 echo -n "Preparing to download files... "
1720 # Reduce indices to ${PATH}|${HASH} pairs
1721 for X in $1 $2 $3; do
1722 cut -f 1,2,7 -d '|' < ${X} |
1728 # List of files wanted
1729 cut -f 2 -d '|' < $3.hashes |
1732 if ! [ -f files/${HASH}.gz ]; then
1737 # Generate a list of unmodified files
1738 comm -12 $1.hashes $2.hashes |
1739 sort -k 1,1 -t '|' > unmodified.files
1741 # Copy all files into /files/. We only need the unmodified files
1742 # for use in patching; but we'll want all of them if the user asks
1743 # to rollback the updates later.
1745 F=`echo "${LINE}" | cut -f 1 -d '|'`
1746 HASH=`echo "${LINE}" | cut -f 2 -d '|'`
1748 # Skip files we already have.
1749 if [ -f files/${HASH}.gz ]; then
1753 # Make sure the file hasn't changed.
1754 cp "${BASEDIR}/${F}" tmpfile
1755 if [ `sha256 -q tmpfile` != ${HASH} ]; then
1757 echo "File changed while FreeBSD Update running: ${F}"
1761 # Place the file into storage.
1762 gzip -c < tmpfile > files/${HASH}.gz
1766 # Produce a list of patches to download
1767 sort -k 1,1 -t '|' $3.hashes |
1768 join -t '|' -o 2.2,1.2 - unmodified.files |
1769 fetch_make_patchlist > patchlist
1772 rm unmodified.files $1.hashes $2.hashes $3.hashes
1774 # We don't need the list of possible old files any more.
1777 # We're finished making noise
1783 # Attempt to fetch patches
1784 if [ -s patchlist ]; then
1785 echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
1786 echo ${NDEBUG} "patches.${DDSTATS}"
1787 tr '|' '-' < patchlist |
1788 lam -s "${PATCHDIR}/" - |
1789 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1790 2>${STATSREDIR} | fetch_progress
1793 # Attempt to apply patches
1794 echo -n "Applying patches... "
1795 tr '|' ' ' < patchlist |
1797 if [ ! -f "${X}-${Y}" ]; then continue; fi
1798 gunzip -c < files/${X}.gz > OLD
1800 bspatch OLD NEW ${X}-${Y}
1802 if [ `${SHA256} -q NEW` = ${Y} ]; then
1806 rm -f diff OLD NEW ${X}-${Y}
1807 done 2>${QUIETREDIR}
1811 # Download files which couldn't be generate via patching
1813 if [ ! -f "files/${Y}.gz" ]; then
1816 done < files.wanted > filelist
1818 if [ -s filelist ]; then
1819 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
1820 echo ${NDEBUG} "files... "
1821 lam -s "${FETCHDIR}/f/" - -s ".gz" < filelist |
1822 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1826 if ! [ -f ${Y}.gz ]; then
1830 if [ `gunzip -c < ${Y}.gz |
1831 ${SHA256} -q` = ${Y} ]; then
1832 mv ${Y}.gz files/${Y}.gz
1834 echo "${Y} has incorrect hash."
1842 rm files.wanted filelist patchlist
1845 # Create and populate install manifest directory; and report what updates
1847 fetch_create_manifest () {
1848 # If we have an existing install manifest, nuke it.
1849 if [ -L "${BDHASH}-install" ]; then
1850 rm -r ${BDHASH}-install/
1851 rm ${BDHASH}-install
1854 # Report to the user if any updates were avoided due to local changes
1855 if [ -s modifiedfiles ]; then
1857 echo -n "The following files are affected by updates, "
1858 echo "but no changes have"
1859 echo -n "been downloaded because the files have been "
1860 echo "modified locally:"
1865 # If no files will be updated, tell the user and exit
1866 if ! [ -s INDEX-PRESENT ] &&
1867 ! [ -s INDEX-NEW ]; then
1868 rm INDEX-PRESENT INDEX-NEW
1870 echo -n "No updates needed to update system to "
1871 echo "${RELNUM}-p${RELPATCHNUM}."
1875 # Divide files into (a) removed files, (b) added files, and
1876 # (c) updated files.
1877 cut -f 1 -d '|' < INDEX-PRESENT |
1878 sort > INDEX-PRESENT.flist
1879 cut -f 1 -d '|' < INDEX-NEW |
1880 sort > INDEX-NEW.flist
1881 comm -23 INDEX-PRESENT.flist INDEX-NEW.flist > files.removed
1882 comm -13 INDEX-PRESENT.flist INDEX-NEW.flist > files.added
1883 comm -12 INDEX-PRESENT.flist INDEX-NEW.flist > files.updated
1884 rm INDEX-PRESENT.flist INDEX-NEW.flist
1886 # Report removed files, if any
1887 if [ -s files.removed ]; then
1889 echo -n "The following files will be removed "
1890 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1895 # Report added files, if any
1896 if [ -s files.added ]; then
1898 echo -n "The following files will be added "
1899 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1904 # Report updated files, if any
1905 if [ -s files.updated ]; then
1907 echo -n "The following files will be updated "
1908 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1914 # Create a directory for the install manifest.
1915 MDIR=`mktemp -d install.XXXXXX` || return 1
1918 mv INDEX-PRESENT ${MDIR}/INDEX-OLD
1919 mv INDEX-NEW ${MDIR}/INDEX-NEW
1921 # Link it into place
1922 ln -s ${MDIR} ${BDHASH}-install
1925 # Warn about any upcoming EoL
1927 # What's the current time?
1928 NOWTIME=`date "+%s"`
1930 # When did we last warn about the EoL date?
1931 if [ -f lasteolwarn ]; then
1932 LASTWARN=`cat lasteolwarn`
1934 LASTWARN=`expr ${NOWTIME} - 63072000`
1937 # If the EoL time is past, warn.
1938 if [ ${EOLTIME} -lt ${NOWTIME} ]; then
1941 WARNING: `uname -sr` HAS PASSED ITS END-OF-LIFE DATE.
1942 Any security issues discovered after `date -r ${EOLTIME}`
1943 will not have been corrected.
1948 # Figure out how long it has been since we last warned about the
1949 # upcoming EoL, and how much longer we have left.
1950 SINCEWARN=`expr ${NOWTIME} - ${LASTWARN}`
1951 TIMELEFT=`expr ${EOLTIME} - ${NOWTIME}`
1953 # Don't warn if the EoL is more than 3 months away
1954 if [ ${TIMELEFT} -gt 7884000 ]; then
1958 # Don't warn if the time remaining is more than 3 times the time
1959 # since the last warning.
1960 if [ ${TIMELEFT} -gt `expr ${SINCEWARN} \* 3` ]; then
1964 # Figure out what time units to use.
1965 if [ ${TIMELEFT} -lt 604800 ]; then
1968 elif [ ${TIMELEFT} -lt 2678400 ]; then
1976 # Compute the right number of units
1977 NUM=`expr ${TIMELEFT} / ${SIZE}`
1978 if [ ${NUM} != 1 ]; then
1985 WARNING: `uname -sr` is approaching its End-of-Life date.
1986 It is strongly recommended that you upgrade to a newer
1987 release within the next ${NUM} ${UNIT}.
1990 # Update the stored time of last warning
1991 echo ${NOWTIME} > lasteolwarn
1994 # Do the actual work involved in "fetch" / "cron".
1996 workdir_init || return 1
1998 # Prepare the mirror list.
1999 fetch_pick_server_init && fetch_pick_server
2001 # Try to fetch the public key until we run out of servers.
2002 while ! fetch_key; do
2003 fetch_pick_server || return 1
2006 # Try to fetch the metadata index signature ("tag") until we run
2007 # out of available servers; and sanity check the downloaded tag.
2008 while ! fetch_tag; do
2009 fetch_pick_server || return 1
2011 fetch_tagsanity || return 1
2013 # Fetch the latest INDEX-NEW and INDEX-OLD files.
2014 fetch_metadata INDEX-NEW INDEX-OLD || return 1
2016 # Generate filtered INDEX-NEW and INDEX-OLD files containing only
2017 # the lines which (a) belong to components we care about, and (b)
2018 # don't correspond to paths we're explicitly ignoring.
2019 fetch_filter_metadata INDEX-NEW || return 1
2020 fetch_filter_metadata INDEX-OLD || return 1
2022 # Translate /boot/${KERNCONF} into ${KERNELDIR}
2023 fetch_filter_kernel_names INDEX-NEW ${KERNCONF}
2024 fetch_filter_kernel_names INDEX-OLD ${KERNCONF}
2026 # For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the
2027 # system and generate an INDEX-PRESENT file.
2028 fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2030 # Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which
2031 # correspond to lines in INDEX-PRESENT with hashes not appearing
2032 # in INDEX-OLD or INDEX-NEW. Also remove lines where the entry in
2033 # INDEX-PRESENT has type - and there isn't a corresponding entry in
2034 # INDEX-OLD with type -.
2035 fetch_filter_unmodified_notpresent \
2036 INDEX-OLD INDEX-PRESENT INDEX-NEW /dev/null
2038 # For each entry in INDEX-PRESENT of type -, remove any corresponding
2039 # entry from INDEX-NEW if ${ALLOWADD} != "yes". Remove all entries
2040 # of type - from INDEX-PRESENT.
2041 fetch_filter_allowadd INDEX-PRESENT INDEX-NEW
2043 # If ${ALLOWDELETE} != "yes", then remove any entries from
2044 # INDEX-PRESENT which don't correspond to entries in INDEX-NEW.
2045 fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW
2047 # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in
2048 # INDEX-PRESENT with metadata not matching any entry in INDEX-OLD,
2049 # replace the corresponding line of INDEX-NEW with one having the
2050 # same metadata as the entry in INDEX-PRESENT.
2051 fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW
2053 # Remove lines from INDEX-PRESENT and INDEX-NEW which are identical;
2054 # no need to update a file if it isn't changing.
2055 fetch_filter_uptodate INDEX-PRESENT INDEX-NEW
2057 # Prepare to fetch files: Generate a list of the files we need,
2058 # copy the unmodified files we have into /files/, and generate
2059 # a list of patches to download.
2060 fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2063 fetch_files || return 1
2065 # Create and populate install manifest directory; and report what
2066 # updates are available.
2067 fetch_create_manifest || return 1
2069 # Warn about any upcoming EoL
2070 fetch_warn_eol || return 1
2073 # If StrictComponents is not "yes", generate a new components list
2074 # with only the components which appear to be installed.
2075 upgrade_guess_components () {
2076 if [ "${STRICTCOMPONENTS}" = "no" ]; then
2077 # Generate filtered INDEX-ALL with only the components listed
2079 fetch_filter_metadata_components $1 || return 1
2081 # Tell the user why his disk is suddenly making lots of noise
2082 echo -n "Inspecting system... "
2084 # Look at the files on disk, and assume that a component is
2085 # supposed to be present if it is more than half-present.
2086 cut -f 1-3 -d '|' < INDEX-ALL |
2088 while read C S F; do
2089 if [ -e ${BASEDIR}/${F} ]; then
2096 sed -E 's,^ +,,' > compfreq
2097 grep ' = ' compfreq |
2099 sort -k 2,2 -t ' ' > compfreq.total
2100 grep ' + ' compfreq |
2102 sort -k 2,2 -t ' ' > compfreq.present
2103 join -t ' ' -1 2 -2 2 compfreq.present compfreq.total |
2104 while read S P T; do
2105 if [ ${P} -gt `expr ${T} / 2` ]; then
2109 cut -f 2 -d ' ' < compfreq.total > comp.total
2110 rm INDEX-ALL compfreq compfreq.total compfreq.present
2112 # We're done making noise.
2115 # Sometimes the kernel isn't installed where INDEX-ALL
2116 # thinks that it should be: In particular, it is often in
2117 # /boot/kernel instead of /boot/GENERIC or /boot/SMP. To
2118 # deal with this, if "kernel|X" is listed in comp.total
2119 # (i.e., is a component which would be upgraded if it is
2120 # found to be present) we will add it to comp.present.
2121 # If "kernel|<anything>" is in comp.total but "kernel|X" is
2122 # not, we print a warning -- the user is running a kernel
2123 # which isn't part of the release.
2124 KCOMP=`echo ${KERNCONF} | tr 'A-Z' 'a-z'`
2125 grep -E "^kernel\|${KCOMP}\$" comp.total >> comp.present
2127 if grep -qE "^kernel\|" comp.total &&
2128 ! grep -qE "^kernel\|${KCOMP}\$" comp.total; then
2131 WARNING: This system is running a "${KCOMP}" kernel, which is not a
2132 kernel configuration distributed as part of FreeBSD ${RELNUM}.
2133 This kernel will not be updated: you MUST update the kernel manually
2134 before running "$0 install".
2138 # Re-sort the list of installed components and generate
2139 # the list of non-installed components.
2140 sort -u < comp.present > comp.present.tmp
2141 mv comp.present.tmp comp.present
2142 comm -13 comp.present comp.total > comp.absent
2144 # Ask the user to confirm that what we have is correct. To
2145 # reduce user confusion, translate "X|Y" back to "X/Y" (as
2146 # subcomponents must be listed in the configuration file).
2148 echo -n "The following components of FreeBSD "
2149 echo "seem to be installed:"
2150 tr '|' '/' < comp.present |
2153 echo -n "The following components of FreeBSD "
2154 echo "do not seem to be installed:"
2155 tr '|' '/' < comp.absent |
2158 continuep || return 1
2161 # Suck the generated list of components into ${COMPONENTS}.
2162 # Note that comp.present.tmp is used due to issues with
2163 # pipelines and setting variables.
2165 tr '|' '/' < comp.present > comp.present.tmp
2167 COMPONENTS="${COMPONENTS} ${C}"
2168 done < comp.present.tmp
2170 # Delete temporary files
2171 rm comp.present comp.present.tmp comp.absent comp.total
2175 # If StrictComponents is not "yes", COMPONENTS contains an entry
2176 # corresponding to the currently running kernel, and said kernel
2177 # does not exist in the new release, add "kernel/generic" to the
2178 # list of components.
2179 upgrade_guess_new_kernel () {
2180 if [ "${STRICTCOMPONENTS}" = "no" ]; then
2181 # Grab the unfiltered metadata file.
2182 METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'`
2183 gunzip -c < files/${METAHASH}.gz > $1.all
2185 # If "kernel/${KCOMP}" is in ${COMPONENTS} and that component
2186 # isn't in $1.all, we need to add kernel/generic.
2187 for C in ${COMPONENTS}; do
2188 if [ ${C} = "kernel/${KCOMP}" ] &&
2189 ! grep -qE "^kernel\|${KCOMP}\|" $1.all; then
2190 COMPONENTS="${COMPONENTS} kernel/generic"
2194 WARNING: This system is running a "${KCOMP}" kernel, which is not a
2195 kernel configuration distributed as part of FreeBSD ${RELNUM}.
2196 As part of upgrading to FreeBSD ${RELNUM}, this kernel will be
2197 replaced with a "generic" kernel.
2199 continuep || return 1
2203 # Don't need this any more...
2208 # Convert INDEX-OLD (last release) and INDEX-ALL (new release) into
2209 # INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades).
2210 upgrade_oldall_to_oldnew () {
2211 # For each ${F}|... which appears in INDEX-ALL but does not appear
2212 # in INDEX-OLD, add ${F}|-|||||| to INDEX-OLD.
2213 cut -f 1 -d '|' < $1 |
2215 cut -f 1 -d '|' < $2 |
2217 comm -13 $1.paths - |
2218 lam - -s "|-||||||" |
2222 # Remove lines from INDEX-OLD which also appear in INDEX-ALL
2223 comm -23 $1 $2 > $1.tmp
2226 # Remove lines from INDEX-ALL which have a file name not appearing
2227 # anywhere in INDEX-OLD (since these must be files which haven't
2228 # changed -- if they were new, there would be an entry of type "-").
2229 cut -f 1 -d '|' < $1 |
2231 sort -k 1,1 -t '|' < $2 |
2232 join -t '|' - $1.paths |
2237 # Rename INDEX-ALL to INDEX-NEW.
2241 # From the list of "old" files in $1, merge changes in $2 with those in $3,
2242 # and update $3 to reflect the hashes of merged files.
2244 # We only need to do anything if $1 is non-empty.
2246 cut -f 1 -d '|' $1 |
2249 # Create staging area for merging files
2253 mkdir -p merge/old/${D}
2254 mkdir -p merge/${OLDRELNUM}/${D}
2255 mkdir -p merge/${RELNUM}/${D}
2256 mkdir -p merge/new/${D}
2261 # Currently installed file
2262 V=`look "${F}|" $2 | cut -f 7 -d '|'`
2263 gunzip < files/${V}.gz > merge/old/${F}
2266 if look "${F}|" $1 | fgrep -q "|f|"; then
2267 V=`look "${F}|" $1 | cut -f 3 -d '|'`
2268 gunzip < files/${V}.gz \
2269 > merge/${OLDRELNUM}/${F}
2273 if look "${F}|" $3 | cut -f 1,2,7 -d '|' |
2274 fgrep -q "|f|"; then
2275 V=`look "${F}|" $3 | cut -f 7 -d '|'`
2276 gunzip < files/${V}.gz \
2277 > merge/${RELNUM}/${F}
2281 # Attempt to automatically merge changes
2282 echo -n "Attempting to automatically merge "
2283 echo -n "changes in files..."
2286 # If the file doesn't exist in the new release,
2287 # the result of "merging changes" is having the file
2289 if ! [ -f merge/${RELNUM}/${F} ]; then
2293 # If the file didn't exist in the old release, we're
2294 # going to throw away the existing file and hope that
2295 # the version from the new release is what we want.
2296 if ! [ -f merge/${OLDRELNUM}/${F} ]; then
2297 cp merge/${RELNUM}/${F} merge/new/${F}
2301 # Some files need special treatment.
2303 /etc/spwd.db | /etc/pwd.db | /etc/login.conf.db)
2304 # Don't merge these -- we're rebuild them
2305 # after updates are installed.
2306 cp merge/old/${F} merge/new/${F}
2309 if ! merge -p -L "current version" \
2310 -L "${OLDRELNUM}" -L "${RELNUM}" \
2312 merge/${OLDRELNUM}/${F} \
2313 merge/${RELNUM}/${F} \
2314 > merge/new/${F} 2>/dev/null; then
2315 echo ${F} >> failed.merges
2322 # Ask the user to handle any files which didn't merge.
2326 The following file could not be merged automatically: ${F}
2327 Press Enter to edit this file in ${EDITOR} and resolve the conflicts
2330 read dummy </dev/tty
2331 ${EDITOR} `pwd`/merge/new/${F} < /dev/tty
2332 done < failed.merges
2335 # Ask the user to confirm that he likes how the result
2338 # Skip files which haven't changed.
2339 if [ -f merge/new/${F} ] &&
2340 cmp -s merge/old/${F} merge/new/${F}; then
2344 # Warn about files which are ceasing to exist.
2345 if ! [ -f merge/new/${F} ]; then
2348 The following file will be removed, as it no longer exists in
2349 FreeBSD ${RELNUM}: ${F}
2351 continuep < /dev/tty || return 1
2355 # Print changes for the user's approval.
2358 The following changes, which occurred between FreeBSD ${OLDRELNUM} and
2359 FreeBSD ${RELNUM} have been merged into ${F}:
2361 diff -U 5 -L "current version" -L "new version" \
2362 merge/old/${F} merge/new/${F} || true
2363 continuep < /dev/tty || return 1
2366 # Store merged files.
2368 if [ -f merge/new/${F} ]; then
2369 V=`${SHA256} -q merge/new/${F}`
2371 gzip -c < merge/new/${F} > files/${V}.gz
2374 done < $1-paths > newhashes
2376 # Pull lines out from $3 which need to be updated to
2377 # reflect merged files.
2380 done < $1-paths > $3-oldlines
2382 # Update lines to reflect merged files
2383 join -t '|' -o 1.1,1.2,1.3,1.4,1.5,1.6,2.2,1.8 \
2384 $3-oldlines newhashes > $3-newlines
2386 # Remove old lines from $3 and add new lines.
2389 sort - $3-newlines > $3.tmp
2393 rm $1-paths newhashes $3-oldlines $3-newlines
2397 # We're done with merging files.
2401 # Do the work involved in fetching upgrades to a new release
2403 workdir_init || return 1
2405 # Prepare the mirror list.
2406 fetch_pick_server_init && fetch_pick_server
2408 # Try to fetch the public key until we run out of servers.
2409 while ! fetch_key; do
2410 fetch_pick_server || return 1
2413 # Try to fetch the metadata index signature ("tag") until we run
2414 # out of available servers; and sanity check the downloaded tag.
2415 while ! fetch_tag; do
2416 fetch_pick_server || return 1
2418 fetch_tagsanity || return 1
2420 # Fetch the INDEX-OLD and INDEX-ALL.
2421 fetch_metadata INDEX-OLD INDEX-ALL || return 1
2423 # If StrictComponents is not "yes", generate a new components list
2424 # with only the components which appear to be installed.
2425 upgrade_guess_components INDEX-ALL || return 1
2427 # Generate filtered INDEX-OLD and INDEX-ALL files containing only
2428 # the components we want and without anything marked as "Ignore".
2429 fetch_filter_metadata INDEX-OLD || return 1
2430 fetch_filter_metadata INDEX-ALL || return 1
2432 # Merge the INDEX-OLD and INDEX-ALL files into INDEX-OLD.
2433 sort INDEX-OLD INDEX-ALL > INDEX-OLD.tmp
2434 mv INDEX-OLD.tmp INDEX-OLD
2437 # Adjust variables for fetching files from the new release.
2439 RELNUM=${TARGETRELEASE}
2440 OLDFETCHDIR=${FETCHDIR}
2441 FETCHDIR=${RELNUM}/${ARCH}
2443 # Try to fetch the NEW metadata index signature ("tag") until we run
2444 # out of available servers; and sanity check the downloaded tag.
2445 while ! fetch_tag; do
2446 fetch_pick_server || return 1
2449 # Fetch the new INDEX-ALL.
2450 fetch_metadata INDEX-ALL || return 1
2452 # If StrictComponents is not "yes", COMPONENTS contains an entry
2453 # corresponding to the currently running kernel, and said kernel
2454 # does not exist in the new release, add "kernel/generic" to the
2455 # list of components.
2456 upgrade_guess_new_kernel INDEX-ALL || return 1
2458 # Filter INDEX-ALL to contain only the components we want and without
2459 # anything marked as "Ignore".
2460 fetch_filter_metadata INDEX-ALL || return 1
2462 # Convert INDEX-OLD (last release) and INDEX-ALL (new release) into
2463 # INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades).
2464 upgrade_oldall_to_oldnew INDEX-OLD INDEX-ALL INDEX-NEW
2466 # Translate /boot/${KERNCONF} or /boot/${NKERNCONF} into ${KERNELDIR}
2467 fetch_filter_kernel_names INDEX-NEW ${NKERNCONF}
2468 fetch_filter_kernel_names INDEX-OLD ${KERNCONF}
2470 # For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the
2471 # system and generate an INDEX-PRESENT file.
2472 fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2474 # Based on ${MERGECHANGES}, generate a file tomerge-old with the
2475 # paths and hashes of old versions of files to merge.
2476 fetch_filter_mergechanges INDEX-OLD INDEX-PRESENT tomerge-old
2478 # Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which
2479 # correspond to lines in INDEX-PRESENT with hashes not appearing
2480 # in INDEX-OLD or INDEX-NEW. Also remove lines where the entry in
2481 # INDEX-PRESENT has type - and there isn't a corresponding entry in
2482 # INDEX-OLD with type -.
2483 fetch_filter_unmodified_notpresent \
2484 INDEX-OLD INDEX-PRESENT INDEX-NEW tomerge-old
2486 # For each entry in INDEX-PRESENT of type -, remove any corresponding
2487 # entry from INDEX-NEW if ${ALLOWADD} != "yes". Remove all entries
2488 # of type - from INDEX-PRESENT.
2489 fetch_filter_allowadd INDEX-PRESENT INDEX-NEW
2491 # If ${ALLOWDELETE} != "yes", then remove any entries from
2492 # INDEX-PRESENT which don't correspond to entries in INDEX-NEW.
2493 fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW
2495 # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in
2496 # INDEX-PRESENT with metadata not matching any entry in INDEX-OLD,
2497 # replace the corresponding line of INDEX-NEW with one having the
2498 # same metadata as the entry in INDEX-PRESENT.
2499 fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW
2501 # Remove lines from INDEX-PRESENT and INDEX-NEW which are identical;
2502 # no need to update a file if it isn't changing.
2503 fetch_filter_uptodate INDEX-PRESENT INDEX-NEW
2505 # Fetch "clean" files from the old release for merging changes.
2506 fetch_files_premerge tomerge-old
2508 # Prepare to fetch files: Generate a list of the files we need,
2509 # copy the unmodified files we have into /files/, and generate
2510 # a list of patches to download.
2511 fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2513 # Fetch patches from to-${RELNUM}/${ARCH}/bp/
2514 PATCHDIR=to-${RELNUM}/${ARCH}/bp
2515 fetch_files || return 1
2517 # Merge configuration file changes.
2518 upgrade_merge tomerge-old INDEX-PRESENT INDEX-NEW || return 1
2520 # Create and populate install manifest directory; and report what
2521 # updates are available.
2522 fetch_create_manifest || return 1
2524 # Leave a note behind to tell the "install" command that the kernel
2525 # needs to be installed before the world.
2526 touch ${BDHASH}-install/kernelfirst
2529 # Make sure that all the file hashes mentioned in $@ have corresponding
2530 # gzipped files stored in /files/.
2532 # Generate a list of hashes
2539 # Make sure all the hashes exist
2541 if ! [ -f files/${HASH}.gz ]; then
2542 echo -n "Update files missing -- "
2543 echo "this should never happen."
2544 echo "Re-run '$0 fetch'."
2553 # Remove the system immutable flag from files
2555 # Generate file list
2557 cut -f 1 -d '|' > filelist
2561 if ! [ -e ${BASEDIR}/${F} ]; then
2565 chflags noschg ${BASEDIR}/${F} || return 1
2572 # Decide which directory name to use for kernel backups.
2573 backup_kernel_finddir () {
2576 # Pathname does not exist, so it is OK use that name
2577 # for backup directory.
2578 if [ ! -e $BACKUPKERNELDIR ]; then
2582 # If directory do exist, we only use if it has our
2584 if [ -d $BACKUPKERNELDIR -a \
2585 -e $BACKUPKERNELDIR/.freebsd-update ]; then
2589 # We could not use current directory name, so add counter to
2590 # the end and try again.
2592 if [ $CNT -gt 9 ]; then
2593 echo "Could not find valid backup dir ($BACKUPKERNELDIR)"
2596 BACKUPKERNELDIR="`echo $BACKUPKERNELDIR | sed -Ee 's/[0-9]\$//'`"
2597 BACKUPKERNELDIR="${BACKUPKERNELDIR}${CNT}"
2601 # Backup the current kernel using hardlinks, if not disabled by user.
2602 # Since we delete all files in the directory used for previous backups
2603 # we create a marker file called ".freebsd-update" in the directory so
2604 # we can determine on the next run that the directory was created by
2605 # freebsd-update and we then do not accidentally remove user files in
2606 # the unlikely case that the user has created a directory with a
2609 # Only make kernel backup is so configured.
2610 if [ $BACKUPKERNEL != yes ]; then
2614 # Decide which directory name to use for kernel backups.
2615 backup_kernel_finddir
2617 # Remove old kernel backup files. If $BACKUPKERNELDIR was
2618 # "not ours", backup_kernel_finddir would have exited, so
2619 # deleting the directory content is as safe as we can make it.
2620 if [ -d $BACKUPKERNELDIR ]; then
2621 rm -f $BACKUPKERNELDIR/*
2624 # Create directory for backup if it doesn't exist.
2625 mkdir -p $BACKUPKERNELDIR
2627 # Mark the directory as having been created by freebsd-update.
2628 touch $BACKUPKERNELDIR/.freebsd-update
2629 if [ $? -ne 0 ]; then
2630 echo "Could not create kernel backup directory"
2634 # Disable pathname expansion to be sure *.symbols is not
2638 # Use find to ignore symbol files, unless disabled by user.
2639 if [ $BACKUPKERNELSYMBOLFILES = yes ]; then
2642 FINDFILTER=-"a ! -name *.symbols"
2645 # Backup all the kernel files using hardlinks.
2646 find $KERNELDIR -type f $FINDFILTER | \
2647 sed -Ee "s,($KERNELDIR)/?(.*),\1/\2 ${BACKUPKERNELDIR}/\2," | \
2650 # Re-enable patchname expansion.
2655 install_from_index () {
2656 # First pass: Do everything apart from setting file flags. We
2657 # can't set flags yet, because schg inhibits hard linking.
2658 sort -k 1,1 -t '|' $1 |
2660 while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do
2663 # Create a directory
2664 install -d -o ${OWNER} -g ${GROUP} \
2665 -m ${PERM} ${BASEDIR}/${FPATH}
2668 if [ -z "${LINK}" ]; then
2669 # Create a file, without setting flags.
2670 gunzip < files/${HASH}.gz > ${HASH}
2671 install -S -o ${OWNER} -g ${GROUP} \
2672 -m ${PERM} ${HASH} ${BASEDIR}/${FPATH}
2675 # Create a hard link.
2676 ln -f ${BASEDIR}/${LINK} ${BASEDIR}/${FPATH}
2681 ln -sfh ${HASH} ${BASEDIR}/${FPATH}
2686 # Perform a second pass, adding file flags.
2688 while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do
2689 if [ ${TYPE} = "f" ] &&
2690 ! [ ${FLAGS} = "0" ]; then
2691 chflags ${FLAGS} ${BASEDIR}/${FPATH}
2696 # Remove files which we want to delete
2698 # Generate list of new files
2699 cut -f 1 -d '|' < $2 |
2702 # Generate subindex of old files we want to nuke
2703 sort -k 1,1 -t '|' $1 |
2704 join -t '|' -v 1 - newfiles |
2705 sort -r -k 1,1 -t '|' |
2707 tr '|' ' ' > killfiles
2709 # Remove the offending bits
2710 while read FPATH TYPE; do
2713 rmdir ${BASEDIR}/${FPATH}
2716 rm ${BASEDIR}/${FPATH}
2719 rm ${BASEDIR}/${FPATH}
2725 rm newfiles killfiles
2728 # Install new files, delete old files, and update linker.hints
2730 # If we haven't already dealt with the kernel, deal with it.
2731 if ! [ -f $1/kerneldone ]; then
2732 grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD
2733 grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW
2735 # Backup current kernel before installing a new one
2736 backup_kernel || return 1
2739 install_from_index INDEX-NEW || return 1
2741 # Remove files which need to be deleted
2742 install_delete INDEX-OLD INDEX-NEW || return 1
2744 # Update linker.hints if necessary
2745 if [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2746 kldxref -R /boot/ 2>/dev/null
2749 # We've finished updating the kernel.
2752 # Do we need to ask for a reboot now?
2753 if [ -f $1/kernelfirst ] &&
2754 [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2757 Kernel updates have been installed. Please reboot and run
2758 "$0 install" again to finish installing updates.
2764 # If we haven't already dealt with the world, deal with it.
2765 if ! [ -f $1/worlddone ]; then
2766 # Install new shared libraries next
2767 grep -vE '^/boot/' $1/INDEX-NEW |
2768 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2769 install_from_index INDEX-NEW || return 1
2771 # Deal with everything else
2772 grep -vE '^/boot/' $1/INDEX-OLD |
2773 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2774 grep -vE '^/boot/' $1/INDEX-NEW |
2775 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2776 install_from_index INDEX-NEW || return 1
2777 install_delete INDEX-OLD INDEX-NEW || return 1
2779 # Rebuild /etc/spwd.db and /etc/pwd.db if necessary.
2780 if [ /etc/master.passwd -nt /etc/spwd.db ] ||
2781 [ /etc/master.passwd -nt /etc/pwd.db ]; then
2782 pwd_mkdb /etc/master.passwd
2785 # Rebuild /etc/login.conf.db if necessary.
2786 if [ /etc/login.conf -nt /etc/login.conf.db ]; then
2787 cap_mkdb /etc/login.conf
2790 # We've finished installing the world and deleting old files
2791 # which are not shared libraries.
2794 # Do we need to ask the user to portupgrade now?
2795 grep -vE '^/boot/' $1/INDEX-NEW |
2796 grep -E '/lib/.*\.so\.[0-9]+\|' |
2799 if grep -vE '^/boot/' $1/INDEX-OLD |
2800 grep -E '/lib/.*\.so\.[0-9]+\|' |
2803 join -v 1 - newfiles |
2807 Completing this upgrade requires removing old shared object files.
2808 Please rebuild all installed 3rd party software (e.g., programs
2809 installed from the ports tree) and then run "$0 install"
2810 again to finish installing updates.
2818 # Remove old shared libraries
2819 grep -vE '^/boot/' $1/INDEX-NEW |
2820 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2821 grep -vE '^/boot/' $1/INDEX-OLD |
2822 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2823 install_delete INDEX-OLD INDEX-NEW || return 1
2825 # Remove temporary files
2826 rm INDEX-OLD INDEX-NEW
2829 # Rearrange bits to allow the installed updates to be rolled back
2830 install_setup_rollback () {
2831 # Remove the "reboot after installing kernel", "kernel updated", and
2832 # "finished installing the world" flags if present -- they are
2833 # irrelevant when rolling back updates.
2834 if [ -f ${BDHASH}-install/kernelfirst ]; then
2835 rm ${BDHASH}-install/kernelfirst
2836 rm ${BDHASH}-install/kerneldone
2838 if [ -f ${BDHASH}-install/worlddone ]; then
2839 rm ${BDHASH}-install/worlddone
2842 if [ -L ${BDHASH}-rollback ]; then
2843 mv ${BDHASH}-rollback ${BDHASH}-install/rollback
2846 mv ${BDHASH}-install ${BDHASH}-rollback
2849 # Actually install updates
2851 echo -n "Installing updates..."
2853 # Make sure we have all the files we should have
2854 install_verify ${BDHASH}-install/INDEX-OLD \
2855 ${BDHASH}-install/INDEX-NEW || return 1
2857 # Remove system immutable flag from files
2858 install_unschg ${BDHASH}-install/INDEX-OLD \
2859 ${BDHASH}-install/INDEX-NEW || return 1
2861 # Install new files, delete old files, and update linker.hints
2862 install_files ${BDHASH}-install || return 1
2864 # Rearrange bits to allow the installed updates to be rolled back
2865 install_setup_rollback
2870 # Rearrange bits to allow the previous set of updates to be rolled back next.
2871 rollback_setup_rollback () {
2872 if [ -L ${BDHASH}-rollback/rollback ]; then
2873 mv ${BDHASH}-rollback/rollback rollback-tmp
2874 rm -r ${BDHASH}-rollback/
2875 rm ${BDHASH}-rollback
2876 mv rollback-tmp ${BDHASH}-rollback
2878 rm -r ${BDHASH}-rollback/
2879 rm ${BDHASH}-rollback
2883 # Install old files, delete new files, and update linker.hints
2885 # Install old shared library files which don't have the same path as
2886 # a new shared library file.
2887 grep -vE '^/boot/' $1/INDEX-NEW |
2888 grep -E '/lib/.*\.so\.[0-9]+\|' |
2890 sort > INDEX-NEW.libs.flist
2891 grep -vE '^/boot/' $1/INDEX-OLD |
2892 grep -E '/lib/.*\.so\.[0-9]+\|' |
2893 sort -k 1,1 -t '|' - |
2894 join -t '|' -v 1 - INDEX-NEW.libs.flist > INDEX-OLD
2895 install_from_index INDEX-OLD || return 1
2897 # Deal with files which are neither kernel nor shared library
2898 grep -vE '^/boot/' $1/INDEX-OLD |
2899 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2900 grep -vE '^/boot/' $1/INDEX-NEW |
2901 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2902 install_from_index INDEX-OLD || return 1
2903 install_delete INDEX-NEW INDEX-OLD || return 1
2905 # Install any old shared library files which we didn't install above.
2906 grep -vE '^/boot/' $1/INDEX-OLD |
2907 grep -E '/lib/.*\.so\.[0-9]+\|' |
2908 sort -k 1,1 -t '|' - |
2909 join -t '|' - INDEX-NEW.libs.flist > INDEX-OLD
2910 install_from_index INDEX-OLD || return 1
2912 # Delete unneeded shared library files
2913 grep -vE '^/boot/' $1/INDEX-OLD |
2914 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2915 grep -vE '^/boot/' $1/INDEX-NEW |
2916 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2917 install_delete INDEX-NEW INDEX-OLD || return 1
2919 # Deal with kernel files
2920 grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD
2921 grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW
2922 install_from_index INDEX-OLD || return 1
2923 install_delete INDEX-NEW INDEX-OLD || return 1
2924 if [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2925 kldxref -R /boot/ 2>/dev/null
2928 # Remove temporary files
2929 rm INDEX-OLD INDEX-NEW INDEX-NEW.libs.flist
2932 # Actually rollback updates
2934 echo -n "Uninstalling updates..."
2936 # If there are updates waiting to be installed, remove them; we
2937 # want the user to re-run 'fetch' after rolling back updates.
2938 if [ -L ${BDHASH}-install ]; then
2939 rm -r ${BDHASH}-install/
2940 rm ${BDHASH}-install
2943 # Make sure we have all the files we should have
2944 install_verify ${BDHASH}-rollback/INDEX-NEW \
2945 ${BDHASH}-rollback/INDEX-OLD || return 1
2947 # Remove system immutable flag from files
2948 install_unschg ${BDHASH}-rollback/INDEX-NEW \
2949 ${BDHASH}-rollback/INDEX-OLD || return 1
2951 # Install old files, delete new files, and update linker.hints
2952 rollback_files ${BDHASH}-rollback || return 1
2954 # Remove the rollback directory and the symlink pointing to it; and
2955 # rearrange bits to allow the previous set of updates to be rolled
2957 rollback_setup_rollback
2962 # Compare INDEX-ALL and INDEX-PRESENT and print warnings about differences.
2964 # Get all the lines which mismatch in something other than file
2965 # flags. We ignore file flags because sysinstall doesn't seem to
2966 # set them when it installs FreeBSD; warning about these adds a
2967 # very large amount of noise.
2968 cut -f 1-5,7-8 -d '|' $1 > $1.noflags
2969 sort -k 1,1 -t '|' $1.noflags > $1.sorted
2970 cut -f 1-5,7-8 -d '|' $2 |
2971 comm -13 $1.noflags - |
2972 fgrep -v '|-|||||' |
2973 sort -k 1,1 -t '|' |
2974 join -t '|' $1.sorted - > INDEX-NOTMATCHING
2976 # Ignore files which match IDSIGNOREPATHS.
2977 for X in ${IDSIGNOREPATHS}; do
2978 grep -E "^${X}" INDEX-NOTMATCHING
2981 comm -13 - INDEX-NOTMATCHING > INDEX-NOTMATCHING.tmp
2982 mv INDEX-NOTMATCHING.tmp INDEX-NOTMATCHING
2984 # Go through the lines and print warnings.
2986 FPATH=`echo "${LINE}" | cut -f 1 -d '|'`
2987 TYPE=`echo "${LINE}" | cut -f 2 -d '|'`
2988 OWNER=`echo "${LINE}" | cut -f 3 -d '|'`
2989 GROUP=`echo "${LINE}" | cut -f 4 -d '|'`
2990 PERM=`echo "${LINE}" | cut -f 5 -d '|'`
2991 HASH=`echo "${LINE}" | cut -f 6 -d '|'`
2992 LINK=`echo "${LINE}" | cut -f 7 -d '|'`
2993 P_TYPE=`echo "${LINE}" | cut -f 8 -d '|'`
2994 P_OWNER=`echo "${LINE}" | cut -f 9 -d '|'`
2995 P_GROUP=`echo "${LINE}" | cut -f 10 -d '|'`
2996 P_PERM=`echo "${LINE}" | cut -f 11 -d '|'`
2997 P_HASH=`echo "${LINE}" | cut -f 12 -d '|'`
2998 P_LINK=`echo "${LINE}" | cut -f 13 -d '|'`
3000 # Warn about different object types.
3001 if ! [ "${TYPE}" = "${P_TYPE}" ]; then
3002 echo -n "${FPATH} is a "
3004 f) echo -n "regular file, "
3006 d) echo -n "directory, "
3008 L) echo -n "symlink, "
3011 echo -n "but should be a "
3013 f) echo -n "regular file."
3015 d) echo -n "directory."
3017 L) echo -n "symlink."
3022 # Skip other tests, since they don't make sense if
3023 # we're comparing different object types.
3027 # Warn about different owners.
3028 if ! [ "${OWNER}" = "${P_OWNER}" ]; then
3029 echo -n "${FPATH} is owned by user id ${P_OWNER}, "
3030 echo "but should be owned by user id ${OWNER}."
3033 # Warn about different groups.
3034 if ! [ "${GROUP}" = "${P_GROUP}" ]; then
3035 echo -n "${FPATH} is owned by group id ${P_GROUP}, "
3036 echo "but should be owned by group id ${GROUP}."
3039 # Warn about different permissions. We do not warn about
3040 # different permissions on symlinks, since some archivers
3041 # don't extract symlink permissions correctly and they are
3043 if ! [ "${PERM}" = "${P_PERM}" ] &&
3044 ! [ "${TYPE}" = "L" ]; then
3045 echo -n "${FPATH} has ${P_PERM} permissions, "
3046 echo "but should have ${PERM} permissions."
3049 # Warn about different file hashes / symlink destinations.
3050 if ! [ "${HASH}" = "${P_HASH}" ]; then
3051 if [ "${TYPE}" = "L" ]; then
3052 echo -n "${FPATH} is a symlink to ${P_HASH}, "
3053 echo "but should be a symlink to ${HASH}."
3055 if [ "${TYPE}" = "f" ]; then
3056 echo -n "${FPATH} has SHA256 hash ${P_HASH}, "
3057 echo "but should have SHA256 hash ${HASH}."
3061 # We don't warn about different hard links, since some
3062 # some archivers break hard links, and as long as the
3063 # underlying data is correct they really don't matter.
3064 done < INDEX-NOTMATCHING
3067 rm $1 $1.noflags $1.sorted $2 INDEX-NOTMATCHING
3070 # Do the work involved in comparing the system to a "known good" index
3072 workdir_init || return 1
3074 # Prepare the mirror list.
3075 fetch_pick_server_init && fetch_pick_server
3077 # Try to fetch the public key until we run out of servers.
3078 while ! fetch_key; do
3079 fetch_pick_server || return 1
3082 # Try to fetch the metadata index signature ("tag") until we run
3083 # out of available servers; and sanity check the downloaded tag.
3084 while ! fetch_tag; do
3085 fetch_pick_server || return 1
3087 fetch_tagsanity || return 1
3089 # Fetch INDEX-OLD and INDEX-ALL.
3090 fetch_metadata INDEX-OLD INDEX-ALL || return 1
3092 # Generate filtered INDEX-OLD and INDEX-ALL files containing only
3093 # the components we want and without anything marked as "Ignore".
3094 fetch_filter_metadata INDEX-OLD || return 1
3095 fetch_filter_metadata INDEX-ALL || return 1
3097 # Merge the INDEX-OLD and INDEX-ALL files into INDEX-ALL.
3098 sort INDEX-OLD INDEX-ALL > INDEX-ALL.tmp
3099 mv INDEX-ALL.tmp INDEX-ALL
3102 # Translate /boot/${KERNCONF} to ${KERNELDIR}
3103 fetch_filter_kernel_names INDEX-ALL ${KERNCONF}
3105 # Inspect the system and generate an INDEX-PRESENT file.
3106 fetch_inspect_system INDEX-ALL INDEX-PRESENT /dev/null || return 1
3108 # Compare INDEX-ALL and INDEX-PRESENT and print warnings about any
3110 IDS_compare INDEX-ALL INDEX-PRESENT
3113 #### Main functions -- call parameter-handling and core functions
3115 # Using the command line, configuration file, and defaults,
3116 # set all the parameters which are needed later.
3124 # Fetch command. Make sure that we're being called
3125 # interactively, then run fetch_check_params and fetch_run
3128 echo -n "`basename $0` fetch should not "
3129 echo "be run non-interactively."
3130 echo "Run `basename $0` cron instead."
3137 # Cron command. Make sure the parameters are sensible; wait
3138 # rand(3600) seconds; then fetch updates. While fetching updates,
3139 # send output to a temporary file; only print that file if the
3143 sleep `jot -r 1 0 3600`
3145 TMPFILE=`mktemp /tmp/freebsd-update.XXXXXX` || exit 1
3146 if ! fetch_run >> ${TMPFILE} ||
3147 ! grep -q "No updates needed" ${TMPFILE} ||
3148 [ ${VERBOSELEVEL} = "debug" ]; then
3149 mail -s "`hostname` security updates" ${MAILTO} < ${TMPFILE}
3155 # Fetch files for upgrading to a new release.
3157 upgrade_check_params
3158 upgrade_run || exit 1
3161 # Install downloaded updates.
3163 install_check_params
3164 install_run || exit 1
3167 # Rollback most recently installed updates.
3169 rollback_check_params
3170 rollback_run || exit 1
3173 # Compare system against a "known good" index.
3181 # Make sure we find utilities from the base system
3182 export PATH=/sbin:/bin:/usr/sbin:/usr/bin:${PATH}
3184 # Set LC_ALL in order to avoid problems with character ranges like [A-Z].
3188 for COMMAND in ${COMMANDS}; do