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
287 if echo ${TARGETRELEASE} | grep -qE '^[0-9.]+$'; then
288 TARGETRELEASE="${TARGETRELEASE}-RELEASE"
292 # Define what happens to output of utilities
293 config_VerboseLevel () {
294 if [ -z ${VERBOSELEVEL} ]; then
296 [Dd][Ee][Bb][Uu][Gg])
299 [Nn][Oo][Ss][Tt][Aa][Tt][Ss])
302 [Ss][Tt][Aa][Tt][Ss])
314 config_BackupKernel () {
315 if [ -z ${BACKUPKERNEL} ]; then
332 config_BackupKernelDir () {
333 if [ -z ${BACKUPKERNELDIR} ]; then
335 echo "BackupKernelDir set to empty dir"
339 # We check for some paths which would be extremely odd
340 # to use, but which could cause a lot of problems if
343 /|/bin|/boot|/etc|/lib|/libexec|/sbin|/usr|/var)
344 echo "BackupKernelDir set to invalid path $1"
351 echo "BackupKernelDir ($1) is not an absolute path"
360 config_BackupKernelSymbolFiles () {
361 if [ -z ${BACKUPKERNELSYMBOLFILES} ]; then
364 BACKUPKERNELSYMBOLFILES=yes
367 BACKUPKERNELSYMBOLFILES=no
378 # Handle one line of configuration
380 if [ $# -eq 0 ]; then
389 #### Parameter handling functions.
391 # Initialize parameters to null, just in case they're
392 # set in the environment.
394 # Configration settings
397 # No configuration file set yet
400 # No commands specified yet
404 # Parse the command line
406 while [ $# -gt 0 ]; do
408 # Location of configuration file
410 if [ $# -eq 1 ]; then usage; fi
411 if [ ! -z "${CONFFILE}" ]; then usage; fi
415 # Configuration file equivalents
417 if [ $# -eq 1 ]; then usage; fi; shift
418 config_BaseDir $1 || usage
421 if [ $# -eq 1 ]; then usage; fi; shift
422 config_WorkDir $1 || usage
425 if [ $# -eq 1 ]; then usage; fi; shift
426 config_KeyPrint $1 || usage
429 if [ $# -eq 1 ]; then usage; fi; shift
430 config_ServerName $1 || usage
433 if [ $# -eq 1 ]; then usage; fi; shift
434 config_TargetRelease $1 || usage
437 if [ $# -eq 1 ]; then usage; fi; shift
438 config_MailTo $1 || usage
441 if [ $# -eq 1 ]; then usage; fi; shift
442 config_VerboseLevel $1 || usage
445 # Aliases for "-v debug" and "-v nostats"
447 config_VerboseLevel debug || usage
450 config_VerboseLevel nostats || usage
454 cron | fetch | upgrade | install | rollback | IDS)
455 COMMANDS="${COMMANDS} $1"
458 # Anything else is an error
466 # Make sure we have at least one command
467 if [ -z "${COMMANDS}" ]; then
472 # Parse the configuration file
474 # If a configuration file was specified on the command line, check
475 # that it exists and is readable.
476 if [ ! -z "${CONFFILE}" ] && [ ! -r "${CONFFILE}" ]; then
477 echo -n "File does not exist "
478 echo -n "or is not readable: "
483 # If a configuration file was not specified on the command line,
484 # use the default configuration file path. If that default does
485 # not exist, give up looking for any configuration.
486 if [ -z "${CONFFILE}" ]; then
487 CONFFILE="/etc/freebsd-update.conf"
488 if [ ! -r "${CONFFILE}" ]; then
493 # Save the configuration options specified on the command line, and
494 # clear all the options in preparation for reading the config file.
498 # Read the configuration file. Anything after the first '#' is
499 # ignored, and any blank lines are ignored.
503 LINEX=`echo "${LINE}" | cut -f 1 -d '#'`
504 if ! configline ${LINEX}; then
505 echo "Error processing configuration file, line $L:"
511 # Merge the settings read from the configuration file with those
512 # provided at the command line.
516 # Provide some default parameters
518 # Save any parameters already configured, and clear the slate
522 # Default configurations
523 config_WorkDir /var/db/freebsd-update
526 config_AllowDelete yes
527 config_KeepModifiedMetadata yes
529 config_VerboseLevel stats
530 config_StrictComponents no
531 config_BackupKernel yes
532 config_BackupKernelDir /boot/kernel.old
533 config_BackupKernelSymbolFiles no
535 # Merge these defaults into the earlier-configured settings
539 # Set utility output filtering options, based on ${VERBOSELEVEL}
540 fetch_setup_verboselevel () {
541 case ${VERBOSELEVEL} in
543 QUIETREDIR="/dev/stderr"
545 STATSREDIR="/dev/stderr"
553 STATSREDIR="/dev/null"
559 QUIETREDIR="/dev/null"
561 STATSREDIR="/dev/stdout"
569 # Perform sanity checks and set some final parameters
570 # in preparation for fetching files. Figure out which
571 # set of updates should be downloaded: If the user is
572 # running *-p[0-9]+, strip off the last part; if the
573 # user is running -SECURITY, call it -RELEASE. Chdir
574 # into the working directory.
575 fetchupgrade_check_params () {
576 export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)"
579 "SERVERNAME must be given via command line or configuration file."
580 _KEYPRINT_z="Key must be given via -k option or configuration file."
581 _KEYPRINT_bad="Invalid key fingerprint: "
582 _WORKDIR_bad="Directory does not exist or is not writable: "
584 if [ -z "${SERVERNAME}" ]; then
585 echo -n "`basename $0`: "
586 echo "${_SERVERNAME_z}"
589 if [ -z "${KEYPRINT}" ]; then
590 echo -n "`basename $0`: "
591 echo "${_KEYPRINT_z}"
594 if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
595 echo -n "`basename $0`: "
596 echo -n "${_KEYPRINT_bad}"
600 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
601 echo -n "`basename $0`: "
602 echo -n "${_WORKDIR_bad}"
607 cd ${WORKDIR} || exit 1
609 # Generate release number. The s/SECURITY/RELEASE/ bit exists
610 # to provide an upgrade path for FreeBSD Update 1.x users, since
611 # the kernels provided by FreeBSD Update 1.x are always labelled
614 sed -E 's,-p[0-9]+,,' |
615 sed -E 's,-SECURITY,-RELEASE,'`
617 FETCHDIR=${RELNUM}/${ARCH}
618 PATCHDIR=${RELNUM}/${ARCH}/bp
620 # Figure out what directory contains the running kernel
621 BOOTFILE=`sysctl -n kern.bootfile`
622 KERNELDIR=${BOOTFILE%/kernel}
623 if ! [ -d ${KERNELDIR} ]; then
624 echo "Cannot identify running kernel"
628 # Figure out what kernel configuration is running. We start with
629 # the output of `uname -i`, and then make the following adjustments:
630 # 1. Replace "SMP-GENERIC" with "SMP". Why the SMP kernel config
631 # file says "ident SMP-GENERIC", I don't know...
632 # 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64"
633 # _and_ `sysctl kern.version` contains a line which ends "/SMP", then
634 # we're running an SMP kernel. This mis-identification is a bug
635 # which was fixed in 6.2-STABLE.
637 if [ ${KERNCONF} = "SMP-GENERIC" ]; then
640 if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then
641 if sysctl kern.version | grep -qE '/SMP$'; then
647 BSPATCH=/usr/bin/bspatch
649 PHTTPGET=/usr/libexec/phttpget
651 # Set up variables relating to VERBOSELEVEL
652 fetch_setup_verboselevel
654 # Construct a unique name from ${BASEDIR}
655 BDHASH=`echo ${BASEDIR} | sha256 -q`
658 # Perform sanity checks etc. before fetching updates.
659 fetch_check_params () {
660 fetchupgrade_check_params
662 if ! [ -z "${TARGETRELEASE}" ]; then
663 echo -n "`basename $0`: "
664 echo -n "-r option is meaningless with 'fetch' command. "
665 echo "(Did you mean 'upgrade' instead?)"
670 # Perform sanity checks etc. before fetching upgrades.
671 upgrade_check_params () {
672 fetchupgrade_check_params
674 # Unless set otherwise, we're upgrading to the same kernel config.
675 NKERNCONF=${KERNCONF}
677 # We need TARGETRELEASE set
678 _TARGETRELEASE_z="Release target must be specified via -r option."
679 if [ -z "${TARGETRELEASE}" ]; then
680 echo -n "`basename $0`: "
681 echo "${_TARGETRELEASE_z}"
685 # The target release should be != the current release.
686 if [ "${TARGETRELEASE}" = "${RELNUM}" ]; then
687 echo -n "`basename $0`: "
688 echo "Cannot upgrade from ${RELNUM} to itself"
692 # Turning off AllowAdd or AllowDelete is a bad idea for upgrades.
693 if [ "${ALLOWADD}" = "no" ]; then
694 echo -n "`basename $0`: "
695 echo -n "WARNING: \"AllowAdd no\" is a bad idea "
696 echo "when upgrading between releases."
699 if [ "${ALLOWDELETE}" = "no" ]; then
700 echo -n "`basename $0`: "
701 echo -n "WARNING: \"AllowDelete no\" is a bad idea "
702 echo "when upgrading between releases."
706 # Set EDITOR to /usr/bin/vi if it isn't already set
707 : ${EDITOR:='/usr/bin/vi'}
710 # Perform sanity checks and set some final parameters in
711 # preparation for installing updates.
712 install_check_params () {
713 # Check that we are root. All sorts of things won't work otherwise.
714 if [ `id -u` != 0 ]; then
715 echo "You must be root to run this."
719 # Check that securelevel <= 0. Otherwise we can't update schg files.
720 if [ `sysctl -n kern.securelevel` -gt 0 ]; then
721 echo "Updates cannot be installed when the system securelevel"
722 echo "is greater than zero."
726 # Check that we have a working directory
727 _WORKDIR_bad="Directory does not exist or is not writable: "
728 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
729 echo -n "`basename $0`: "
730 echo -n "${_WORKDIR_bad}"
734 cd ${WORKDIR} || exit 1
736 # Construct a unique name from ${BASEDIR}
737 BDHASH=`echo ${BASEDIR} | sha256 -q`
739 # Check that we have updates ready to install
740 if ! [ -L ${BDHASH}-install ]; then
741 echo "No updates are available to install."
742 echo "Run '$0 fetch' first."
745 if ! [ -f ${BDHASH}-install/INDEX-OLD ] ||
746 ! [ -f ${BDHASH}-install/INDEX-NEW ]; then
747 echo "Update manifest is corrupt -- this should never happen."
748 echo "Re-run '$0 fetch'."
752 # Figure out what directory contains the running kernel
753 BOOTFILE=`sysctl -n kern.bootfile`
754 KERNELDIR=${BOOTFILE%/kernel}
755 if ! [ -d ${KERNELDIR} ]; then
756 echo "Cannot identify running kernel"
761 # Perform sanity checks and set some final parameters in
762 # preparation for UNinstalling updates.
763 rollback_check_params () {
764 # Check that we are root. All sorts of things won't work otherwise.
765 if [ `id -u` != 0 ]; then
766 echo "You must be root to run this."
770 # Check that we have a working directory
771 _WORKDIR_bad="Directory does not exist or is not writable: "
772 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
773 echo -n "`basename $0`: "
774 echo -n "${_WORKDIR_bad}"
778 cd ${WORKDIR} || exit 1
780 # Construct a unique name from ${BASEDIR}
781 BDHASH=`echo ${BASEDIR} | sha256 -q`
783 # Check that we have updates ready to rollback
784 if ! [ -L ${BDHASH}-rollback ]; then
785 echo "No rollback directory found."
788 if ! [ -f ${BDHASH}-rollback/INDEX-OLD ] ||
789 ! [ -f ${BDHASH}-rollback/INDEX-NEW ]; then
790 echo "Update manifest is corrupt -- this should never happen."
795 # Perform sanity checks and set some final parameters
796 # in preparation for comparing the system against the
797 # published index. Figure out which index we should
798 # compare against: If the user is running *-p[0-9]+,
799 # strip off the last part; if the user is running
800 # -SECURITY, call it -RELEASE. Chdir into the working
802 IDS_check_params () {
803 export HTTP_USER_AGENT="freebsd-update (${COMMAND}, `uname -r`)"
806 "SERVERNAME must be given via command line or configuration file."
807 _KEYPRINT_z="Key must be given via -k option or configuration file."
808 _KEYPRINT_bad="Invalid key fingerprint: "
809 _WORKDIR_bad="Directory does not exist or is not writable: "
811 if [ -z "${SERVERNAME}" ]; then
812 echo -n "`basename $0`: "
813 echo "${_SERVERNAME_z}"
816 if [ -z "${KEYPRINT}" ]; then
817 echo -n "`basename $0`: "
818 echo "${_KEYPRINT_z}"
821 if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
822 echo -n "`basename $0`: "
823 echo -n "${_KEYPRINT_bad}"
827 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
828 echo -n "`basename $0`: "
829 echo -n "${_WORKDIR_bad}"
833 cd ${WORKDIR} || exit 1
835 # Generate release number. The s/SECURITY/RELEASE/ bit exists
836 # to provide an upgrade path for FreeBSD Update 1.x users, since
837 # the kernels provided by FreeBSD Update 1.x are always labelled
840 sed -E 's,-p[0-9]+,,' |
841 sed -E 's,-SECURITY,-RELEASE,'`
843 FETCHDIR=${RELNUM}/${ARCH}
844 PATCHDIR=${RELNUM}/${ARCH}/bp
846 # Figure out what directory contains the running kernel
847 BOOTFILE=`sysctl -n kern.bootfile`
848 KERNELDIR=${BOOTFILE%/kernel}
849 if ! [ -d ${KERNELDIR} ]; then
850 echo "Cannot identify running kernel"
854 # Figure out what kernel configuration is running. We start with
855 # the output of `uname -i`, and then make the following adjustments:
856 # 1. Replace "SMP-GENERIC" with "SMP". Why the SMP kernel config
857 # file says "ident SMP-GENERIC", I don't know...
858 # 2. If the kernel claims to be GENERIC _and_ ${ARCH} is "amd64"
859 # _and_ `sysctl kern.version` contains a line which ends "/SMP", then
860 # we're running an SMP kernel. This mis-identification is a bug
861 # which was fixed in 6.2-STABLE.
863 if [ ${KERNCONF} = "SMP-GENERIC" ]; then
866 if [ ${KERNCONF} = "GENERIC" ] && [ ${ARCH} = "amd64" ]; then
867 if sysctl kern.version | grep -qE '/SMP$'; then
874 PHTTPGET=/usr/libexec/phttpget
876 # Set up variables relating to VERBOSELEVEL
877 fetch_setup_verboselevel
880 #### Core functionality -- the actual work gets done here
882 # Use an SRV query to pick a server. If the SRV query doesn't provide
883 # a useful answer, use the server name specified by the user.
884 # Put another way... look up _http._tcp.${SERVERNAME} and pick a server
885 # from that; or if no servers are returned, use ${SERVERNAME}.
886 # This allows a user to specify "portsnap.freebsd.org" (in which case
887 # portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org"
888 # (in which case portsnap will use that particular server, since there
889 # won't be an SRV entry for that name).
891 # We ignore the Port field, since we are always going to use port 80.
893 # Fetch the mirror list, but do not pick a mirror yet. Returns 1 if
894 # no mirrors are available for any reason.
895 fetch_pick_server_init () {
898 # Check that host(1) exists (i.e., that the system wasn't built with the
899 # WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist.
900 if ! which -s host; then
905 echo -n "Looking up ${SERVERNAME} mirrors... "
907 # Issue the SRV query and pull out the Priority, Weight, and Target fields.
908 # BIND 9 prints "$name has SRV record ..." while BIND 8 prints
909 # "$name server selection ..."; we allow either format.
910 MLIST="_http._tcp.${SERVERNAME}"
911 host -t srv "${MLIST}" |
912 sed -nE "s/${MLIST} (has SRV record|server selection) //p" |
913 cut -f 1,2,4 -d ' ' |
915 sort > serverlist_full
917 # If no records, give up -- we'll just use the server name we were given.
918 if [ `wc -l < serverlist_full` -eq 0 ]; then
923 # Report how many mirrors we found.
924 echo `wc -l < serverlist_full` "mirrors found."
926 # Generate a random seed for use in picking mirrors. If HTTP_PROXY
927 # is set, this will be used to generate the seed; otherwise, the seed
929 if [ -n "${HTTP_PROXY}${http_proxy}" ]; then
930 RANDVALUE=`sha256 -qs "${HTTP_PROXY}${http_proxy}" |
934 RANDVALUE=`jot -r 1 0 999999999`
938 # Pick a mirror. Returns 1 if we have run out of mirrors to try.
939 fetch_pick_server () {
940 # Generate a list of not-yet-tried mirrors
941 sort serverlist_tried |
942 comm -23 serverlist_full - > serverlist
944 # Have we run out of mirrors?
945 if [ `wc -l < serverlist` -eq 0 ]; then
946 echo "No mirrors remaining, giving up."
950 # Find the highest priority level (lowest numeric value).
951 SRV_PRIORITY=`cut -f 1 -d ' ' serverlist | sort -n | head -1`
953 # Add up the weights of the response lines at that priority level.
958 SRV_W=`echo $X | cut -f 2 -d ' '`
959 SRV_WSUM=$(($SRV_WSUM + $SRV_W))
964 # If all the weights are 0, pretend that they are all 1 instead.
965 if [ ${SRV_WSUM} -eq 0 ]; then
966 SRV_WSUM=`grep -E "^${SRV_PRIORITY} " serverlist | wc -l`
972 # Pick a value between 0 and the sum of the weights - 1
973 SRV_RND=`expr ${RANDVALUE} % ${SRV_WSUM}`
975 # Read through the list of mirrors and set SERVERNAME. Write the line
976 # corresponding to the mirror we selected into serverlist_tried so that
977 # we won't try it again.
981 SRV_W=`echo $X | cut -f 2 -d ' '`
982 SRV_W=$(($SRV_W + $SRV_W_ADD))
983 if [ $SRV_RND -lt $SRV_W ]; then
984 SERVERNAME=`echo $X | cut -f 3 -d ' '`
985 echo "$X" >> serverlist_tried
988 SRV_RND=$(($SRV_RND - $SRV_W))
995 # Take a list of ${oldhash}|${newhash} and output a list of needed patches,
996 # i.e., those for which we have ${oldhash} and don't have ${newhash}.
997 fetch_make_patchlist () {
998 grep -vE "^([0-9a-f]{64})\|\1$" |
1001 if [ -f "files/${Y}.gz" ] ||
1002 [ ! -f "files/${X}.gz" ]; then
1009 # Print user-friendly progress statistics
1014 if [ $(($LNC % 10)) = 0 ]; then
1016 elif [ $(($LNC % 2)) = 0 ]; then
1023 # Function for asking the user if everything is ok
1025 while read -p "Does this look reasonable (y/n)? " CONTINUE; do
1026 case "${CONTINUE}" in
1037 # Initialize the working directory
1040 touch tINDEX.present
1043 # Check that we have a public key with an appropriate hash, or
1044 # fetch the key if it doesn't exist. Returns 1 if the key has
1045 # not yet been fetched.
1047 if [ -r pub.ssl ] && [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
1051 echo -n "Fetching public key from ${SERVERNAME}... "
1053 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/pub.ssl \
1054 2>${QUIETREDIR} || true
1055 if ! [ -r pub.ssl ]; then
1059 if ! [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
1060 echo "key has incorrect hash."
1067 # Fetch metadata signature, aka "tag".
1069 echo -n "Fetching metadata signature "
1070 echo ${NDEBUG} "for ${RELNUM} from ${SERVERNAME}... "
1072 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/latest.ssl \
1073 2>${QUIETREDIR} || true
1074 if ! [ -r latest.ssl ]; then
1079 openssl rsautl -pubin -inkey pub.ssl -verify \
1080 < latest.ssl > tag.new 2>${QUIETREDIR} || true
1083 if ! [ `wc -l < tag.new` = 1 ] ||
1085 "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \
1087 echo "invalid signature."
1093 RELPATCHNUM=`cut -f 4 -d '|' < tag.new`
1094 TINDEXHASH=`cut -f 5 -d '|' < tag.new`
1095 EOLTIME=`cut -f 6 -d '|' < tag.new`
1098 # Sanity-check the patch number in a tag, to make sure that we're not
1099 # going to "update" backwards and to prevent replay attacks.
1100 fetch_tagsanity () {
1101 # Check that we're not going to move from -pX to -pY with Y < X.
1102 RELPX=`uname -r | sed -E 's,.*-,,'`
1103 if echo ${RELPX} | grep -qE '^p[0-9]+$'; then
1104 RELPX=`echo ${RELPX} | cut -c 2-`
1108 if [ "${RELPATCHNUM}" -lt "${RELPX}" ]; then
1110 echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})"
1111 echo " appear older than what"
1112 echo "we are currently running (`uname -r`)!"
1113 echo "Cowardly refusing to proceed any further."
1117 # If "tag" exists and corresponds to ${RELNUM}, make sure that
1118 # it contains a patch number <= RELPATCHNUM, in order to protect
1119 # against rollback (replay) attacks.
1122 "^freebsd-update\|${ARCH}\|${RELNUM}\|[0-9]+\|[0-9a-f]{64}\|[0-9]{10}" \
1124 LASTRELPATCHNUM=`cut -f 4 -d '|' < tag`
1126 if [ "${RELPATCHNUM}" -lt "${LASTRELPATCHNUM}" ]; then
1128 echo -n "Files on mirror (${RELNUM}-p${RELPATCHNUM})"
1129 echo " are older than the"
1130 echo -n "most recently seen updates"
1131 echo " (${RELNUM}-p${LASTRELPATCHNUM})."
1132 echo "Cowardly refusing to proceed any further."
1138 # Fetch metadata index file
1139 fetch_metadata_index () {
1140 echo ${NDEBUG} "Fetching metadata index... "
1142 fetch ${QUIETFLAG} http://${SERVERNAME}/${FETCHDIR}/t/${TINDEXHASH}
1144 if ! [ -f ${TINDEXHASH} ]; then
1148 if [ `${SHA256} -q ${TINDEXHASH}` != ${TINDEXHASH} ]; then
1149 echo "update metadata index corrupt."
1155 # Print an error message about signed metadata being bogus.
1156 fetch_metadata_bogus () {
1158 echo "The update metadata$1 is correctly signed, but"
1159 echo "failed an integrity check."
1160 echo "Cowardly refusing to proceed any further."
1164 # Construct tINDEX.new by merging the lines named in $1 from ${TINDEXHASH}
1165 # with the lines not named in $@ from tINDEX.present (if that file exists).
1166 fetch_metadata_index_merge () {
1167 for METAFILE in $@; do
1168 if [ `grep -E "^${METAFILE}\|" ${TINDEXHASH} | wc -l` \
1170 fetch_metadata_bogus " index"
1174 grep -E "${METAFILE}\|" ${TINDEXHASH}
1176 sort > tINDEX.wanted
1178 if [ -f tINDEX.present ]; then
1179 join -t '|' -v 2 tINDEX.wanted tINDEX.present |
1180 sort -m - tINDEX.wanted > tINDEX.new
1183 mv tINDEX.wanted tINDEX.new
1187 # Sanity check all the lines of tINDEX.new. Even if more metadata lines
1188 # are added by future versions of the server, this won't cause problems,
1189 # since the only lines which appear in tINDEX.new are the ones which we
1190 # specifically grepped out of ${TINDEXHASH}.
1191 fetch_metadata_index_sanity () {
1192 if grep -qvE '^[0-9A-Z.-]+\|[0-9a-f]{64}$' tINDEX.new; then
1193 fetch_metadata_bogus " index"
1198 # Sanity check the metadata file $1.
1199 fetch_metadata_sanity () {
1200 # Some aliases to save space later: ${P} is a character which can
1201 # appear in a path; ${M} is the four numeric metadata fields; and
1202 # ${H} is a sha256 hash.
1203 P="[-+./:=_[[:alnum:]]"
1204 M="[0-9]+\|[0-9]+\|[0-9]+\|[0-9]+"
1207 # Check that the first four fields make sense.
1208 if gunzip -c < files/$1.gz |
1209 grep -qvE "^[a-z]+\|[0-9a-z]+\|${P}+\|[fdL-]\|"; then
1210 fetch_metadata_bogus ""
1214 # Remove the first three fields.
1215 gunzip -c < files/$1.gz |
1216 cut -f 4- -d '|' > sanitycheck.tmp
1218 # Sanity check entries with type 'f'
1219 if grep -E '^f' sanitycheck.tmp |
1220 grep -qvE "^f\|${M}\|${H}\|${P}*\$"; then
1221 fetch_metadata_bogus ""
1225 # Sanity check entries with type 'd'
1226 if grep -E '^d' sanitycheck.tmp |
1227 grep -qvE "^d\|${M}\|\|\$"; then
1228 fetch_metadata_bogus ""
1232 # Sanity check entries with type 'L'
1233 if grep -E '^L' sanitycheck.tmp |
1234 grep -qvE "^L\|${M}\|${P}*\|\$"; then
1235 fetch_metadata_bogus ""
1239 # Sanity check entries with type '-'
1240 if grep -E '^-' sanitycheck.tmp |
1241 grep -qvE "^-\|\|\|\|\|\|"; then
1242 fetch_metadata_bogus ""
1250 # Fetch the metadata index and metadata files listed in $@,
1251 # taking advantage of metadata patches where possible.
1253 fetch_metadata_index || return 1
1254 fetch_metadata_index_merge $@ || return 1
1255 fetch_metadata_index_sanity || return 1
1257 # Generate a list of wanted metadata patches
1258 join -t '|' -o 1.2,2.2 tINDEX.present tINDEX.new |
1259 fetch_make_patchlist > patchlist
1261 if [ -s patchlist ]; then
1262 # Attempt to fetch metadata patches
1263 echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
1264 echo ${NDEBUG} "metadata patches.${DDSTATS}"
1265 tr '|' '-' < patchlist |
1266 lam -s "${FETCHDIR}/tp/" - -s ".gz" |
1267 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1268 2>${STATSREDIR} | fetch_progress
1271 # Attempt to apply metadata patches
1272 echo -n "Applying metadata patches... "
1273 tr '|' ' ' < patchlist |
1275 if [ ! -f "${X}-${Y}.gz" ]; then continue; fi
1276 gunzip -c < ${X}-${Y}.gz > diff
1277 gunzip -c < files/${X}.gz > diff-OLD
1279 # Figure out which lines are being added and removed
1282 while read PREFIX; do
1283 look "${PREFIX}" diff-OLD
1286 grep -E '^\+' diff |
1287 cut -c 2- > diff-add
1289 # Generate the new file
1290 comm -23 diff-OLD diff-rm |
1291 sort - diff-add > diff-NEW
1293 if [ `${SHA256} -q diff-NEW` = ${Y} ]; then
1294 mv diff-NEW files/${Y}
1297 mv diff-NEW ${Y}.bad
1299 rm -f ${X}-${Y}.gz diff
1300 rm -f diff-OLD diff-NEW diff-add diff-rm
1301 done 2>${QUIETREDIR}
1305 # Update metadata without patches
1306 cut -f 2 -d '|' < tINDEX.new |
1308 if [ ! -f "files/${Y}.gz" ]; then
1314 if [ -s filelist ]; then
1315 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
1316 echo ${NDEBUG} "metadata files... "
1317 lam -s "${FETCHDIR}/m/" - -s ".gz" < filelist |
1318 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1322 if ! [ -f ${Y}.gz ]; then
1326 if [ `gunzip -c < ${Y}.gz |
1327 ${SHA256} -q` = ${Y} ]; then
1328 mv ${Y}.gz files/${Y}.gz
1330 echo "metadata is corrupt."
1337 # Sanity-check the metadata files.
1338 cut -f 2 -d '|' tINDEX.new > filelist
1340 fetch_metadata_sanity ${X} || return 1
1343 # Remove files which are no longer needed
1344 cut -f 2 -d '|' tINDEX.present |
1346 cut -f 2 -d '|' tINDEX.new |
1348 comm -13 - oldfiles |
1349 lam -s "files/" - -s ".gz" |
1351 rm patchlist filelist oldfiles
1355 mv tINDEX.new tINDEX.present
1361 # Extract a subset of a downloaded metadata file containing only the parts
1362 # which are listed in COMPONENTS.
1363 fetch_filter_metadata_components () {
1364 METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'`
1365 gunzip -c < files/${METAHASH}.gz > $1.all
1367 # Fish out the lines belonging to components we care about.
1368 for C in ${COMPONENTS}; do
1369 look "`echo ${C} | tr '/' '|'`|" $1.all
1372 # Remove temporary file.
1376 # Generate a filtered version of the metadata file $1 from the downloaded
1377 # file, by fishing out the lines corresponding to components we're trying
1378 # to keep updated, and then removing lines corresponding to paths we want
1380 fetch_filter_metadata () {
1381 # Fish out the lines belonging to components we care about.
1382 fetch_filter_metadata_components $1
1384 # Canonicalize directory names by removing any trailing / in
1385 # order to avoid listing directories multiple times if they
1386 # belong to multiple components. Turning "/" into "" doesn't
1387 # matter, since we add a leading "/" when we use paths later.
1388 cut -f 3- -d '|' $1 |
1389 sed -e 's,/|d|,|d|,' |
1392 # Figure out which lines to ignore and remove them.
1393 for X in ${IGNOREPATHS}; do
1394 grep -E "^${X}" $1.tmp
1397 comm -13 - $1.tmp > $1
1399 # Remove temporary files.
1403 # Filter the metadata file $1 by adding lines with "/boot/$2"
1404 # replaced by ${KERNELDIR} (which is `sysctl -n kern.bootfile` minus the
1405 # trailing "/kernel"); and if "/boot/$2" does not exist, remove
1406 # the original lines which start with that.
1407 # Put another way: Deal with the fact that the FOO kernel is sometimes
1408 # installed in /boot/FOO/ and is sometimes installed elsewhere.
1409 fetch_filter_kernel_names () {
1411 sed -e "s,/boot/$2,${KERNELDIR},g" |
1415 if ! [ -d /boot/$2 ]; then
1416 grep -v ^/boot/$2 $1 > $1.tmp
1421 # For all paths appearing in $1 or $3, inspect the system
1422 # and generate $2 describing what is currently installed.
1423 fetch_inspect_system () {
1427 # Tell the user why his disk is suddenly making lots of noise
1428 echo -n "Inspecting system... "
1430 # Generate list of files to inspect
1435 # Examine each file and output lines of the form
1436 # /path/to/file|type|device-inum|user|group|perm|flags|value
1437 # sorted by device and inode number.
1439 # If the symlink/file/directory does not exist, record this.
1440 if ! [ -e ${BASEDIR}/${F} ]; then
1444 if ! [ -r ${BASEDIR}/${F} ]; then
1445 echo "Cannot read file: ${BASEDIR}/${F}" \
1451 # Otherwise, output an index line.
1452 if [ -L ${BASEDIR}/${F} ]; then
1454 stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1455 readlink ${BASEDIR}/${F};
1456 elif [ -f ${BASEDIR}/${F} ]; then
1458 stat -n -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1459 sha256 -q ${BASEDIR}/${F};
1460 elif [ -d ${BASEDIR}/${F} ]; then
1462 stat -f '%d-%i|%u|%g|%Mp%Lp|%Of|' ${BASEDIR}/${F};
1464 echo "Unknown file type: ${BASEDIR}/${F}" \
1470 sort -k 3,3 -t '|' > $2.tmp
1473 # Check if an error occurred during system inspection
1474 if [ -f .err ]; then
1478 # Convert to the form
1479 # /path/to/file|type|user|group|perm|flags|value|hlink
1480 # by resolving identical device and inode numbers into hard links.
1481 cut -f 1,3 -d '|' $2.tmp |
1482 sort -k 1,1 -t '|' |
1483 sort -s -u -k 2,2 -t '|' |
1484 join -1 2 -2 3 -t '|' - $2.tmp |
1485 awk -F \| -v OFS=\| \
1487 if (($2 == $3) || ($4 == "-"))
1488 print $3,$4,$5,$6,$7,$8,$9,""
1490 print $3,$4,$5,$6,$7,$8,$9,$2
1495 # We're finished looking around
1499 # For any paths matching ${MERGECHANGES}, compare $1 and $2 and find any
1500 # files which differ; generate $3 containing these paths and the old hashes.
1501 fetch_filter_mergechanges () {
1502 # Pull out the paths and hashes of the files matching ${MERGECHANGES}.
1504 for X in ${MERGECHANGES}; do
1505 grep -E "^${X}" ${F}
1507 cut -f 1,2,7 -d '|' |
1511 # Any line in $2-values which doesn't appear in $1-values and is a
1512 # file means that we should list the path in $3.
1513 comm -13 $1-values $2-values |
1515 cut -f 1 -d '|' > $2-paths
1517 # For each path, pull out one (and only one!) entry from $1-values.
1518 # Note that we cannot distinguish which "old" version the user made
1519 # changes to; but hopefully any changes which occur due to security
1520 # updates will exist in both the "new" version and the version which
1521 # the user has installed, so the merging will still work.
1523 look "${X}|" $1-values |
1525 done < $2-paths > $3
1528 rm $1-values $2-values $2-paths
1531 # For any paths matching ${UPDATEIFUNMODIFIED}, remove lines from $[123]
1532 # which correspond to lines in $2 with hashes not matching $1 or $3, unless
1533 # the paths are listed in $4. For entries in $2 marked "not present"
1534 # (aka. type -), remove lines from $[123] unless there is a corresponding
1536 fetch_filter_unmodified_notpresent () {
1537 # Figure out which lines of $1 and $3 correspond to bits which
1538 # should only be updated if they haven't changed, and fish out
1539 # the (path, type, value) tuples.
1540 # NOTE: We don't consider a file to be "modified" if it matches
1542 for X in ${UPDATEIFUNMODIFIED}; do
1546 cut -f 1,2,7 -d '|' |
1549 # Do the same for $2.
1550 for X in ${UPDATEIFUNMODIFIED}; do
1553 cut -f 1,2,7 -d '|' |
1556 # Any entry in $2-values which is not in $1-values corresponds to
1557 # a path which we need to remove from $1, $2, and $3, unless it
1558 # that path appears in $4.
1559 comm -13 $1-values $2-values |
1560 sort -t '|' -k 1,1 > mlines.tmp
1561 cut -f 1 -d '|' $4 |
1563 join -v 2 -t '|' - mlines.tmp |
1565 rm $1-values $2-values mlines.tmp
1567 # Any lines in $2 which are not in $1 AND are "not present" lines
1568 # also belong in mlines.
1570 cut -f 1,2,7 -d '|' |
1571 fgrep '|-|' >> mlines
1573 # Remove lines from $1, $2, and $3
1574 for X in $1 $2 $3; do
1575 sort -t '|' -k 1,1 ${X} > ${X}.tmp
1576 cut -f 1 -d '|' < mlines |
1578 join -v 2 -t '|' - ${X}.tmp |
1583 # Store a list of the modified files, for future reference
1584 fgrep -v '|-|' mlines |
1585 cut -f 1 -d '|' > modifiedfiles
1589 # For each entry in $1 of type -, remove any corresponding
1590 # entry from $2 if ${ALLOWADD} != "yes". Remove all entries
1591 # of type - from $1.
1592 fetch_filter_allowadd () {
1593 cut -f 1,2 -d '|' < $1 |
1595 cut -f 1 -d '|' > filesnotpresent
1597 if [ ${ALLOWADD} != "yes" ]; then
1599 join -v 1 -t '|' - filesnotpresent |
1605 join -v 1 -t '|' - filesnotpresent |
1611 # If ${ALLOWDELETE} != "yes", then remove any entries from $1
1612 # which don't correspond to entries in $2.
1613 fetch_filter_allowdelete () {
1614 # Produce a lists ${PATH}|${TYPE}
1616 cut -f 1-2 -d '|' < ${X} |
1617 sort -u > ${X}.nodes
1620 # Figure out which lines need to be removed from $1.
1621 if [ ${ALLOWDELETE} != "yes" ]; then
1622 comm -23 $1.nodes $2.nodes > $1.badnodes
1627 # Remove the relevant lines from $1
1630 done < $1.badnodes |
1631 comm -13 - $1 > $1.tmp
1634 rm $1.badnodes $1.nodes $2.nodes
1637 # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in $2
1638 # with metadata not matching any entry in $1, replace the corresponding
1639 # line of $3 with one having the same metadata as the entry in $2.
1640 fetch_filter_modified_metadata () {
1641 # Fish out the metadata from $1 and $2
1643 cut -f 1-6 -d '|' < ${X} > ${X}.metadata
1646 # Find the metadata we need to keep
1647 if [ ${KEEPMODIFIEDMETADATA} = "yes" ]; then
1648 comm -13 $1.metadata $2.metadata > keepmeta
1653 # Extract the lines which we need to remove from $3, and
1654 # construct the lines which we need to add to $3.
1658 NODE=`echo "${LINE}" | cut -f 1-2 -d '|'`
1659 look "${NODE}|" $3 >> $3.remove
1660 look "${NODE}|" $3 |
1662 lam -s "${LINE}|" - >> $3.add
1665 # Remove the specified lines and add the new lines.
1668 sort -u - $3.add > $3.tmp
1671 rm keepmeta $1.metadata $2.metadata $3.add $3.remove
1674 # Remove lines from $1 and $2 which are identical;
1675 # no need to update a file if it isn't changing.
1676 fetch_filter_uptodate () {
1677 comm -23 $1 $2 > $1.tmp
1678 comm -13 $1 $2 > $2.tmp
1684 # Fetch any "clean" old versions of files we need for merging changes.
1685 fetch_files_premerge () {
1686 # We only need to do anything if $1 is non-empty.
1688 # Tell the user what we're doing
1689 echo -n "Fetching files from ${OLDRELNUM} for merging... "
1691 # List of files wanted
1694 sort -u > files.wanted
1696 # Only fetch the files we don't already have
1698 if [ ! -f "files/${Y}.gz" ]; then
1701 done < files.wanted > filelist
1703 # Actually fetch them
1704 lam -s "${OLDFETCHDIR}/f/" - -s ".gz" < filelist |
1705 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1708 # Make sure we got them all, and move them into /files/
1710 if ! [ -f ${Y}.gz ]; then
1714 if [ `gunzip -c < ${Y}.gz |
1715 ${SHA256} -q` = ${Y} ]; then
1716 mv ${Y}.gz files/${Y}.gz
1718 echo "${Y} has incorrect hash."
1725 rm filelist files.wanted
1729 # Prepare to fetch files: Generate a list of the files we need,
1730 # copy the unmodified files we have into /files/, and generate
1731 # a list of patches to download.
1732 fetch_files_prepare () {
1733 # Tell the user why his disk is suddenly making lots of noise
1734 echo -n "Preparing to download files... "
1736 # Reduce indices to ${PATH}|${HASH} pairs
1737 for X in $1 $2 $3; do
1738 cut -f 1,2,7 -d '|' < ${X} |
1744 # List of files wanted
1745 cut -f 2 -d '|' < $3.hashes |
1748 if ! [ -f files/${HASH}.gz ]; then
1753 # Generate a list of unmodified files
1754 comm -12 $1.hashes $2.hashes |
1755 sort -k 1,1 -t '|' > unmodified.files
1757 # Copy all files into /files/. We only need the unmodified files
1758 # for use in patching; but we'll want all of them if the user asks
1759 # to rollback the updates later.
1761 F=`echo "${LINE}" | cut -f 1 -d '|'`
1762 HASH=`echo "${LINE}" | cut -f 2 -d '|'`
1764 # Skip files we already have.
1765 if [ -f files/${HASH}.gz ]; then
1769 # Make sure the file hasn't changed.
1770 cp "${BASEDIR}/${F}" tmpfile
1771 if [ `sha256 -q tmpfile` != ${HASH} ]; then
1773 echo "File changed while FreeBSD Update running: ${F}"
1777 # Place the file into storage.
1778 gzip -c < tmpfile > files/${HASH}.gz
1782 # Produce a list of patches to download
1783 sort -k 1,1 -t '|' $3.hashes |
1784 join -t '|' -o 2.2,1.2 - unmodified.files |
1785 fetch_make_patchlist > patchlist
1788 rm unmodified.files $1.hashes $2.hashes $3.hashes
1790 # We don't need the list of possible old files any more.
1793 # We're finished making noise
1799 # Attempt to fetch patches
1800 if [ -s patchlist ]; then
1801 echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
1802 echo ${NDEBUG} "patches.${DDSTATS}"
1803 tr '|' '-' < patchlist |
1804 lam -s "${PATCHDIR}/" - |
1805 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1806 2>${STATSREDIR} | fetch_progress
1809 # Attempt to apply patches
1810 echo -n "Applying patches... "
1811 tr '|' ' ' < patchlist |
1813 if [ ! -f "${X}-${Y}" ]; then continue; fi
1814 gunzip -c < files/${X}.gz > OLD
1816 bspatch OLD NEW ${X}-${Y}
1818 if [ `${SHA256} -q NEW` = ${Y} ]; then
1822 rm -f diff OLD NEW ${X}-${Y}
1823 done 2>${QUIETREDIR}
1827 # Download files which couldn't be generate via patching
1829 if [ ! -f "files/${Y}.gz" ]; then
1832 done < files.wanted > filelist
1834 if [ -s filelist ]; then
1835 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
1836 echo ${NDEBUG} "files... "
1837 lam -s "${FETCHDIR}/f/" - -s ".gz" < filelist |
1838 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
1842 if ! [ -f ${Y}.gz ]; then
1846 if [ `gunzip -c < ${Y}.gz |
1847 ${SHA256} -q` = ${Y} ]; then
1848 mv ${Y}.gz files/${Y}.gz
1850 echo "${Y} has incorrect hash."
1858 rm files.wanted filelist patchlist
1861 # Create and populate install manifest directory; and report what updates
1863 fetch_create_manifest () {
1864 # If we have an existing install manifest, nuke it.
1865 if [ -L "${BDHASH}-install" ]; then
1866 rm -r ${BDHASH}-install/
1867 rm ${BDHASH}-install
1870 # Report to the user if any updates were avoided due to local changes
1871 if [ -s modifiedfiles ]; then
1873 echo -n "The following files are affected by updates, "
1874 echo "but no changes have"
1875 echo -n "been downloaded because the files have been "
1876 echo "modified locally:"
1881 # If no files will be updated, tell the user and exit
1882 if ! [ -s INDEX-PRESENT ] &&
1883 ! [ -s INDEX-NEW ]; then
1884 rm INDEX-PRESENT INDEX-NEW
1886 echo -n "No updates needed to update system to "
1887 echo "${RELNUM}-p${RELPATCHNUM}."
1891 # Divide files into (a) removed files, (b) added files, and
1892 # (c) updated files.
1893 cut -f 1 -d '|' < INDEX-PRESENT |
1894 sort > INDEX-PRESENT.flist
1895 cut -f 1 -d '|' < INDEX-NEW |
1896 sort > INDEX-NEW.flist
1897 comm -23 INDEX-PRESENT.flist INDEX-NEW.flist > files.removed
1898 comm -13 INDEX-PRESENT.flist INDEX-NEW.flist > files.added
1899 comm -12 INDEX-PRESENT.flist INDEX-NEW.flist > files.updated
1900 rm INDEX-PRESENT.flist INDEX-NEW.flist
1902 # Report removed files, if any
1903 if [ -s files.removed ]; then
1905 echo -n "The following files will be removed "
1906 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1911 # Report added files, if any
1912 if [ -s files.added ]; then
1914 echo -n "The following files will be added "
1915 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1920 # Report updated files, if any
1921 if [ -s files.updated ]; then
1923 echo -n "The following files will be updated "
1924 echo "as part of updating to ${RELNUM}-p${RELPATCHNUM}:"
1930 # Create a directory for the install manifest.
1931 MDIR=`mktemp -d install.XXXXXX` || return 1
1934 mv INDEX-PRESENT ${MDIR}/INDEX-OLD
1935 mv INDEX-NEW ${MDIR}/INDEX-NEW
1937 # Link it into place
1938 ln -s ${MDIR} ${BDHASH}-install
1941 # Warn about any upcoming EoL
1943 # What's the current time?
1944 NOWTIME=`date "+%s"`
1946 # When did we last warn about the EoL date?
1947 if [ -f lasteolwarn ]; then
1948 LASTWARN=`cat lasteolwarn`
1950 LASTWARN=`expr ${NOWTIME} - 63072000`
1953 # If the EoL time is past, warn.
1954 if [ ${EOLTIME} -lt ${NOWTIME} ]; then
1957 WARNING: `uname -sr` HAS PASSED ITS END-OF-LIFE DATE.
1958 Any security issues discovered after `date -r ${EOLTIME}`
1959 will not have been corrected.
1964 # Figure out how long it has been since we last warned about the
1965 # upcoming EoL, and how much longer we have left.
1966 SINCEWARN=`expr ${NOWTIME} - ${LASTWARN}`
1967 TIMELEFT=`expr ${EOLTIME} - ${NOWTIME}`
1969 # Don't warn if the EoL is more than 3 months away
1970 if [ ${TIMELEFT} -gt 7884000 ]; then
1974 # Don't warn if the time remaining is more than 3 times the time
1975 # since the last warning.
1976 if [ ${TIMELEFT} -gt `expr ${SINCEWARN} \* 3` ]; then
1980 # Figure out what time units to use.
1981 if [ ${TIMELEFT} -lt 604800 ]; then
1984 elif [ ${TIMELEFT} -lt 2678400 ]; then
1992 # Compute the right number of units
1993 NUM=`expr ${TIMELEFT} / ${SIZE}`
1994 if [ ${NUM} != 1 ]; then
2001 WARNING: `uname -sr` is approaching its End-of-Life date.
2002 It is strongly recommended that you upgrade to a newer
2003 release within the next ${NUM} ${UNIT}.
2006 # Update the stored time of last warning
2007 echo ${NOWTIME} > lasteolwarn
2010 # Do the actual work involved in "fetch" / "cron".
2012 workdir_init || return 1
2014 # Prepare the mirror list.
2015 fetch_pick_server_init && fetch_pick_server
2017 # Try to fetch the public key until we run out of servers.
2018 while ! fetch_key; do
2019 fetch_pick_server || return 1
2022 # Try to fetch the metadata index signature ("tag") until we run
2023 # out of available servers; and sanity check the downloaded tag.
2024 while ! fetch_tag; do
2025 fetch_pick_server || return 1
2027 fetch_tagsanity || return 1
2029 # Fetch the latest INDEX-NEW and INDEX-OLD files.
2030 fetch_metadata INDEX-NEW INDEX-OLD || return 1
2032 # Generate filtered INDEX-NEW and INDEX-OLD files containing only
2033 # the lines which (a) belong to components we care about, and (b)
2034 # don't correspond to paths we're explicitly ignoring.
2035 fetch_filter_metadata INDEX-NEW || return 1
2036 fetch_filter_metadata INDEX-OLD || return 1
2038 # Translate /boot/${KERNCONF} into ${KERNELDIR}
2039 fetch_filter_kernel_names INDEX-NEW ${KERNCONF}
2040 fetch_filter_kernel_names INDEX-OLD ${KERNCONF}
2042 # For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the
2043 # system and generate an INDEX-PRESENT file.
2044 fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2046 # Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which
2047 # correspond to lines in INDEX-PRESENT with hashes not appearing
2048 # in INDEX-OLD or INDEX-NEW. Also remove lines where the entry in
2049 # INDEX-PRESENT has type - and there isn't a corresponding entry in
2050 # INDEX-OLD with type -.
2051 fetch_filter_unmodified_notpresent \
2052 INDEX-OLD INDEX-PRESENT INDEX-NEW /dev/null
2054 # For each entry in INDEX-PRESENT of type -, remove any corresponding
2055 # entry from INDEX-NEW if ${ALLOWADD} != "yes". Remove all entries
2056 # of type - from INDEX-PRESENT.
2057 fetch_filter_allowadd INDEX-PRESENT INDEX-NEW
2059 # If ${ALLOWDELETE} != "yes", then remove any entries from
2060 # INDEX-PRESENT which don't correspond to entries in INDEX-NEW.
2061 fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW
2063 # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in
2064 # INDEX-PRESENT with metadata not matching any entry in INDEX-OLD,
2065 # replace the corresponding line of INDEX-NEW with one having the
2066 # same metadata as the entry in INDEX-PRESENT.
2067 fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW
2069 # Remove lines from INDEX-PRESENT and INDEX-NEW which are identical;
2070 # no need to update a file if it isn't changing.
2071 fetch_filter_uptodate INDEX-PRESENT INDEX-NEW
2073 # Prepare to fetch files: Generate a list of the files we need,
2074 # copy the unmodified files we have into /files/, and generate
2075 # a list of patches to download.
2076 fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2079 fetch_files || return 1
2081 # Create and populate install manifest directory; and report what
2082 # updates are available.
2083 fetch_create_manifest || return 1
2085 # Warn about any upcoming EoL
2086 fetch_warn_eol || return 1
2089 # If StrictComponents is not "yes", generate a new components list
2090 # with only the components which appear to be installed.
2091 upgrade_guess_components () {
2092 if [ "${STRICTCOMPONENTS}" = "no" ]; then
2093 # Generate filtered INDEX-ALL with only the components listed
2095 fetch_filter_metadata_components $1 || return 1
2097 # Tell the user why his disk is suddenly making lots of noise
2098 echo -n "Inspecting system... "
2100 # Look at the files on disk, and assume that a component is
2101 # supposed to be present if it is more than half-present.
2102 cut -f 1-3 -d '|' < INDEX-ALL |
2104 while read C S F; do
2105 if [ -e ${BASEDIR}/${F} ]; then
2112 sed -E 's,^ +,,' > compfreq
2113 grep ' = ' compfreq |
2115 sort -k 2,2 -t ' ' > compfreq.total
2116 grep ' + ' compfreq |
2118 sort -k 2,2 -t ' ' > compfreq.present
2119 join -t ' ' -1 2 -2 2 compfreq.present compfreq.total |
2120 while read S P T; do
2121 if [ ${P} -gt `expr ${T} / 2` ]; then
2125 cut -f 2 -d ' ' < compfreq.total > comp.total
2126 rm INDEX-ALL compfreq compfreq.total compfreq.present
2128 # We're done making noise.
2131 # Sometimes the kernel isn't installed where INDEX-ALL
2132 # thinks that it should be: In particular, it is often in
2133 # /boot/kernel instead of /boot/GENERIC or /boot/SMP. To
2134 # deal with this, if "kernel|X" is listed in comp.total
2135 # (i.e., is a component which would be upgraded if it is
2136 # found to be present) we will add it to comp.present.
2137 # If "kernel|<anything>" is in comp.total but "kernel|X" is
2138 # not, we print a warning -- the user is running a kernel
2139 # which isn't part of the release.
2140 KCOMP=`echo ${KERNCONF} | tr 'A-Z' 'a-z'`
2141 grep -E "^kernel\|${KCOMP}\$" comp.total >> comp.present
2143 if grep -qE "^kernel\|" comp.total &&
2144 ! grep -qE "^kernel\|${KCOMP}\$" comp.total; then
2147 WARNING: This system is running a "${KCOMP}" kernel, which is not a
2148 kernel configuration distributed as part of FreeBSD ${RELNUM}.
2149 This kernel will not be updated: you MUST update the kernel manually
2150 before running "$0 install".
2154 # Re-sort the list of installed components and generate
2155 # the list of non-installed components.
2156 sort -u < comp.present > comp.present.tmp
2157 mv comp.present.tmp comp.present
2158 comm -13 comp.present comp.total > comp.absent
2160 # Ask the user to confirm that what we have is correct. To
2161 # reduce user confusion, translate "X|Y" back to "X/Y" (as
2162 # subcomponents must be listed in the configuration file).
2164 echo -n "The following components of FreeBSD "
2165 echo "seem to be installed:"
2166 tr '|' '/' < comp.present |
2169 echo -n "The following components of FreeBSD "
2170 echo "do not seem to be installed:"
2171 tr '|' '/' < comp.absent |
2174 continuep || return 1
2177 # Suck the generated list of components into ${COMPONENTS}.
2178 # Note that comp.present.tmp is used due to issues with
2179 # pipelines and setting variables.
2181 tr '|' '/' < comp.present > comp.present.tmp
2183 COMPONENTS="${COMPONENTS} ${C}"
2184 done < comp.present.tmp
2186 # Delete temporary files
2187 rm comp.present comp.present.tmp comp.absent comp.total
2191 # If StrictComponents is not "yes", COMPONENTS contains an entry
2192 # corresponding to the currently running kernel, and said kernel
2193 # does not exist in the new release, add "kernel/generic" to the
2194 # list of components.
2195 upgrade_guess_new_kernel () {
2196 if [ "${STRICTCOMPONENTS}" = "no" ]; then
2197 # Grab the unfiltered metadata file.
2198 METAHASH=`look "$1|" tINDEX.present | cut -f 2 -d '|'`
2199 gunzip -c < files/${METAHASH}.gz > $1.all
2201 # If "kernel/${KCOMP}" is in ${COMPONENTS} and that component
2202 # isn't in $1.all, we need to add kernel/generic.
2203 for C in ${COMPONENTS}; do
2204 if [ ${C} = "kernel/${KCOMP}" ] &&
2205 ! grep -qE "^kernel\|${KCOMP}\|" $1.all; then
2206 COMPONENTS="${COMPONENTS} kernel/generic"
2210 WARNING: This system is running a "${KCOMP}" kernel, which is not a
2211 kernel configuration distributed as part of FreeBSD ${RELNUM}.
2212 As part of upgrading to FreeBSD ${RELNUM}, this kernel will be
2213 replaced with a "generic" kernel.
2215 continuep || return 1
2219 # Don't need this any more...
2224 # Convert INDEX-OLD (last release) and INDEX-ALL (new release) into
2225 # INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades).
2226 upgrade_oldall_to_oldnew () {
2227 # For each ${F}|... which appears in INDEX-ALL but does not appear
2228 # in INDEX-OLD, add ${F}|-|||||| to INDEX-OLD.
2229 cut -f 1 -d '|' < $1 |
2231 cut -f 1 -d '|' < $2 |
2233 comm -13 $1.paths - |
2234 lam - -s "|-||||||" |
2238 # Remove lines from INDEX-OLD which also appear in INDEX-ALL
2239 comm -23 $1 $2 > $1.tmp
2242 # Remove lines from INDEX-ALL which have a file name not appearing
2243 # anywhere in INDEX-OLD (since these must be files which haven't
2244 # changed -- if they were new, there would be an entry of type "-").
2245 cut -f 1 -d '|' < $1 |
2247 sort -k 1,1 -t '|' < $2 |
2248 join -t '|' - $1.paths |
2253 # Rename INDEX-ALL to INDEX-NEW.
2257 # From the list of "old" files in $1, merge changes in $2 with those in $3,
2258 # and update $3 to reflect the hashes of merged files.
2260 # We only need to do anything if $1 is non-empty.
2262 cut -f 1 -d '|' $1 |
2265 # Create staging area for merging files
2269 mkdir -p merge/old/${D}
2270 mkdir -p merge/${OLDRELNUM}/${D}
2271 mkdir -p merge/${RELNUM}/${D}
2272 mkdir -p merge/new/${D}
2277 # Currently installed file
2278 V=`look "${F}|" $2 | cut -f 7 -d '|'`
2279 gunzip < files/${V}.gz > merge/old/${F}
2282 if look "${F}|" $1 | fgrep -q "|f|"; then
2283 V=`look "${F}|" $1 | cut -f 3 -d '|'`
2284 gunzip < files/${V}.gz \
2285 > merge/${OLDRELNUM}/${F}
2289 if look "${F}|" $3 | cut -f 1,2,7 -d '|' |
2290 fgrep -q "|f|"; then
2291 V=`look "${F}|" $3 | cut -f 7 -d '|'`
2292 gunzip < files/${V}.gz \
2293 > merge/${RELNUM}/${F}
2297 # Attempt to automatically merge changes
2298 echo -n "Attempting to automatically merge "
2299 echo -n "changes in files..."
2302 # If the file doesn't exist in the new release,
2303 # the result of "merging changes" is having the file
2305 if ! [ -f merge/${RELNUM}/${F} ]; then
2309 # If the file didn't exist in the old release, we're
2310 # going to throw away the existing file and hope that
2311 # the version from the new release is what we want.
2312 if ! [ -f merge/${OLDRELNUM}/${F} ]; then
2313 cp merge/${RELNUM}/${F} merge/new/${F}
2317 # Some files need special treatment.
2319 /etc/spwd.db | /etc/pwd.db | /etc/login.conf.db)
2320 # Don't merge these -- we're rebuild them
2321 # after updates are installed.
2322 cp merge/old/${F} merge/new/${F}
2325 if ! merge -p -L "current version" \
2326 -L "${OLDRELNUM}" -L "${RELNUM}" \
2328 merge/${OLDRELNUM}/${F} \
2329 merge/${RELNUM}/${F} \
2330 > merge/new/${F} 2>/dev/null; then
2331 echo ${F} >> failed.merges
2338 # Ask the user to handle any files which didn't merge.
2342 The following file could not be merged automatically: ${F}
2343 Press Enter to edit this file in ${EDITOR} and resolve the conflicts
2346 read dummy </dev/tty
2347 ${EDITOR} `pwd`/merge/new/${F} < /dev/tty
2348 done < failed.merges
2351 # Ask the user to confirm that he likes how the result
2354 # Skip files which haven't changed.
2355 if [ -f merge/new/${F} ] &&
2356 cmp -s merge/old/${F} merge/new/${F}; then
2360 # Warn about files which are ceasing to exist.
2361 if ! [ -f merge/new/${F} ]; then
2364 The following file will be removed, as it no longer exists in
2365 FreeBSD ${RELNUM}: ${F}
2367 continuep < /dev/tty || return 1
2371 # Print changes for the user's approval.
2374 The following changes, which occurred between FreeBSD ${OLDRELNUM} and
2375 FreeBSD ${RELNUM} have been merged into ${F}:
2377 diff -U 5 -L "current version" -L "new version" \
2378 merge/old/${F} merge/new/${F} || true
2379 continuep < /dev/tty || return 1
2382 # Store merged files.
2384 if [ -f merge/new/${F} ]; then
2385 V=`${SHA256} -q merge/new/${F}`
2387 gzip -c < merge/new/${F} > files/${V}.gz
2390 done < $1-paths > newhashes
2392 # Pull lines out from $3 which need to be updated to
2393 # reflect merged files.
2396 done < $1-paths > $3-oldlines
2398 # Update lines to reflect merged files
2399 join -t '|' -o 1.1,1.2,1.3,1.4,1.5,1.6,2.2,1.8 \
2400 $3-oldlines newhashes > $3-newlines
2402 # Remove old lines from $3 and add new lines.
2405 sort - $3-newlines > $3.tmp
2409 rm $1-paths newhashes $3-oldlines $3-newlines
2413 # We're done with merging files.
2417 # Do the work involved in fetching upgrades to a new release
2419 workdir_init || return 1
2421 # Prepare the mirror list.
2422 fetch_pick_server_init && fetch_pick_server
2424 # Try to fetch the public key until we run out of servers.
2425 while ! fetch_key; do
2426 fetch_pick_server || return 1
2429 # Try to fetch the metadata index signature ("tag") until we run
2430 # out of available servers; and sanity check the downloaded tag.
2431 while ! fetch_tag; do
2432 fetch_pick_server || return 1
2434 fetch_tagsanity || return 1
2436 # Fetch the INDEX-OLD and INDEX-ALL.
2437 fetch_metadata INDEX-OLD INDEX-ALL || return 1
2439 # If StrictComponents is not "yes", generate a new components list
2440 # with only the components which appear to be installed.
2441 upgrade_guess_components INDEX-ALL || return 1
2443 # Generate filtered INDEX-OLD and INDEX-ALL files containing only
2444 # the components we want and without anything marked as "Ignore".
2445 fetch_filter_metadata INDEX-OLD || return 1
2446 fetch_filter_metadata INDEX-ALL || return 1
2448 # Merge the INDEX-OLD and INDEX-ALL files into INDEX-OLD.
2449 sort INDEX-OLD INDEX-ALL > INDEX-OLD.tmp
2450 mv INDEX-OLD.tmp INDEX-OLD
2453 # Adjust variables for fetching files from the new release.
2455 RELNUM=${TARGETRELEASE}
2456 OLDFETCHDIR=${FETCHDIR}
2457 FETCHDIR=${RELNUM}/${ARCH}
2459 # Try to fetch the NEW metadata index signature ("tag") until we run
2460 # out of available servers; and sanity check the downloaded tag.
2461 while ! fetch_tag; do
2462 fetch_pick_server || return 1
2465 # Fetch the new INDEX-ALL.
2466 fetch_metadata INDEX-ALL || return 1
2468 # If StrictComponents is not "yes", COMPONENTS contains an entry
2469 # corresponding to the currently running kernel, and said kernel
2470 # does not exist in the new release, add "kernel/generic" to the
2471 # list of components.
2472 upgrade_guess_new_kernel INDEX-ALL || return 1
2474 # Filter INDEX-ALL to contain only the components we want and without
2475 # anything marked as "Ignore".
2476 fetch_filter_metadata INDEX-ALL || return 1
2478 # Convert INDEX-OLD (last release) and INDEX-ALL (new release) into
2479 # INDEX-OLD and INDEX-NEW files (in the sense of normal upgrades).
2480 upgrade_oldall_to_oldnew INDEX-OLD INDEX-ALL INDEX-NEW
2482 # Translate /boot/${KERNCONF} or /boot/${NKERNCONF} into ${KERNELDIR}
2483 fetch_filter_kernel_names INDEX-NEW ${NKERNCONF}
2484 fetch_filter_kernel_names INDEX-OLD ${KERNCONF}
2486 # For all paths appearing in INDEX-OLD or INDEX-NEW, inspect the
2487 # system and generate an INDEX-PRESENT file.
2488 fetch_inspect_system INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2490 # Based on ${MERGECHANGES}, generate a file tomerge-old with the
2491 # paths and hashes of old versions of files to merge.
2492 fetch_filter_mergechanges INDEX-OLD INDEX-PRESENT tomerge-old
2494 # Based on ${UPDATEIFUNMODIFIED}, remove lines from INDEX-* which
2495 # correspond to lines in INDEX-PRESENT with hashes not appearing
2496 # in INDEX-OLD or INDEX-NEW. Also remove lines where the entry in
2497 # INDEX-PRESENT has type - and there isn't a corresponding entry in
2498 # INDEX-OLD with type -.
2499 fetch_filter_unmodified_notpresent \
2500 INDEX-OLD INDEX-PRESENT INDEX-NEW tomerge-old
2502 # For each entry in INDEX-PRESENT of type -, remove any corresponding
2503 # entry from INDEX-NEW if ${ALLOWADD} != "yes". Remove all entries
2504 # of type - from INDEX-PRESENT.
2505 fetch_filter_allowadd INDEX-PRESENT INDEX-NEW
2507 # If ${ALLOWDELETE} != "yes", then remove any entries from
2508 # INDEX-PRESENT which don't correspond to entries in INDEX-NEW.
2509 fetch_filter_allowdelete INDEX-PRESENT INDEX-NEW
2511 # If ${KEEPMODIFIEDMETADATA} == "yes", then for each entry in
2512 # INDEX-PRESENT with metadata not matching any entry in INDEX-OLD,
2513 # replace the corresponding line of INDEX-NEW with one having the
2514 # same metadata as the entry in INDEX-PRESENT.
2515 fetch_filter_modified_metadata INDEX-OLD INDEX-PRESENT INDEX-NEW
2517 # Remove lines from INDEX-PRESENT and INDEX-NEW which are identical;
2518 # no need to update a file if it isn't changing.
2519 fetch_filter_uptodate INDEX-PRESENT INDEX-NEW
2521 # Fetch "clean" files from the old release for merging changes.
2522 fetch_files_premerge tomerge-old
2524 # Prepare to fetch files: Generate a list of the files we need,
2525 # copy the unmodified files we have into /files/, and generate
2526 # a list of patches to download.
2527 fetch_files_prepare INDEX-OLD INDEX-PRESENT INDEX-NEW || return 1
2529 # Fetch patches from to-${RELNUM}/${ARCH}/bp/
2530 PATCHDIR=to-${RELNUM}/${ARCH}/bp
2531 fetch_files || return 1
2533 # Merge configuration file changes.
2534 upgrade_merge tomerge-old INDEX-PRESENT INDEX-NEW || return 1
2536 # Create and populate install manifest directory; and report what
2537 # updates are available.
2538 fetch_create_manifest || return 1
2540 # Leave a note behind to tell the "install" command that the kernel
2541 # needs to be installed before the world.
2542 touch ${BDHASH}-install/kernelfirst
2544 # Remind the user that they need to run "freebsd-update install"
2545 # to install the downloaded bits, in case they didn't RTFM.
2546 echo "To install the downloaded upgrades, run \"$0 install\"."
2549 # Make sure that all the file hashes mentioned in $@ have corresponding
2550 # gzipped files stored in /files/.
2552 # Generate a list of hashes
2559 # Make sure all the hashes exist
2561 if ! [ -f files/${HASH}.gz ]; then
2562 echo -n "Update files missing -- "
2563 echo "this should never happen."
2564 echo "Re-run '$0 fetch'."
2573 # Remove the system immutable flag from files
2575 # Generate file list
2577 cut -f 1 -d '|' > filelist
2581 if ! [ -e ${BASEDIR}/${F} ]; then
2585 chflags noschg ${BASEDIR}/${F} || return 1
2592 # Decide which directory name to use for kernel backups.
2593 backup_kernel_finddir () {
2596 # Pathname does not exist, so it is OK use that name
2597 # for backup directory.
2598 if [ ! -e $BACKUPKERNELDIR ]; then
2602 # If directory do exist, we only use if it has our
2604 if [ -d $BACKUPKERNELDIR -a \
2605 -e $BACKUPKERNELDIR/.freebsd-update ]; then
2609 # We could not use current directory name, so add counter to
2610 # the end and try again.
2612 if [ $CNT -gt 9 ]; then
2613 echo "Could not find valid backup dir ($BACKUPKERNELDIR)"
2616 BACKUPKERNELDIR="`echo $BACKUPKERNELDIR | sed -Ee 's/[0-9]\$//'`"
2617 BACKUPKERNELDIR="${BACKUPKERNELDIR}${CNT}"
2621 # Backup the current kernel using hardlinks, if not disabled by user.
2622 # Since we delete all files in the directory used for previous backups
2623 # we create a marker file called ".freebsd-update" in the directory so
2624 # we can determine on the next run that the directory was created by
2625 # freebsd-update and we then do not accidentally remove user files in
2626 # the unlikely case that the user has created a directory with a
2629 # Only make kernel backup is so configured.
2630 if [ $BACKUPKERNEL != yes ]; then
2634 # Decide which directory name to use for kernel backups.
2635 backup_kernel_finddir
2637 # Remove old kernel backup files. If $BACKUPKERNELDIR was
2638 # "not ours", backup_kernel_finddir would have exited, so
2639 # deleting the directory content is as safe as we can make it.
2640 if [ -d $BACKUPKERNELDIR ]; then
2641 rm -fr $BACKUPKERNELDIR
2644 # Create directories for backup.
2645 mkdir -p $BACKUPKERNELDIR
2646 mtree -cdn -p "${KERNELDIR}" | \
2647 mtree -Ue -p "${BACKUPKERNELDIR}" > /dev/null
2649 # Mark the directory as having been created by freebsd-update.
2650 touch $BACKUPKERNELDIR/.freebsd-update
2651 if [ $? -ne 0 ]; then
2652 echo "Could not create kernel backup directory"
2656 # Disable pathname expansion to be sure *.symbols is not
2660 # Use find to ignore symbol files, unless disabled by user.
2661 if [ $BACKUPKERNELSYMBOLFILES = yes ]; then
2664 FINDFILTER=-"a ! -name *.symbols"
2667 # Backup all the kernel files using hardlinks.
2668 (cd $KERNELDIR && find . -type f $FINDFILTER -exec \
2669 cp -pl '{}' ${BACKUPKERNELDIR}/'{}' \;)
2671 # Re-enable patchname expansion.
2676 install_from_index () {
2677 # First pass: Do everything apart from setting file flags. We
2678 # can't set flags yet, because schg inhibits hard linking.
2679 sort -k 1,1 -t '|' $1 |
2681 while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do
2684 # Create a directory
2685 install -d -o ${OWNER} -g ${GROUP} \
2686 -m ${PERM} ${BASEDIR}/${FPATH}
2689 if [ -z "${LINK}" ]; then
2690 # Create a file, without setting flags.
2691 gunzip < files/${HASH}.gz > ${HASH}
2692 install -S -o ${OWNER} -g ${GROUP} \
2693 -m ${PERM} ${HASH} ${BASEDIR}/${FPATH}
2696 # Create a hard link.
2697 ln -f ${BASEDIR}/${LINK} ${BASEDIR}/${FPATH}
2702 ln -sfh ${HASH} ${BASEDIR}/${FPATH}
2707 # Perform a second pass, adding file flags.
2709 while read FPATH TYPE OWNER GROUP PERM FLAGS HASH LINK; do
2710 if [ ${TYPE} = "f" ] &&
2711 ! [ ${FLAGS} = "0" ]; then
2712 chflags ${FLAGS} ${BASEDIR}/${FPATH}
2717 # Remove files which we want to delete
2719 # Generate list of new files
2720 cut -f 1 -d '|' < $2 |
2723 # Generate subindex of old files we want to nuke
2724 sort -k 1,1 -t '|' $1 |
2725 join -t '|' -v 1 - newfiles |
2726 sort -r -k 1,1 -t '|' |
2728 tr '|' ' ' > killfiles
2730 # Remove the offending bits
2731 while read FPATH TYPE; do
2734 rmdir ${BASEDIR}/${FPATH}
2737 rm ${BASEDIR}/${FPATH}
2740 rm ${BASEDIR}/${FPATH}
2746 rm newfiles killfiles
2749 # Install new files, delete old files, and update linker.hints
2751 # If we haven't already dealt with the kernel, deal with it.
2752 if ! [ -f $1/kerneldone ]; then
2753 grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD
2754 grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW
2756 # Backup current kernel before installing a new one
2757 backup_kernel || return 1
2760 install_from_index INDEX-NEW || return 1
2762 # Remove files which need to be deleted
2763 install_delete INDEX-OLD INDEX-NEW || return 1
2765 # Update linker.hints if necessary
2766 if [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2767 kldxref -R /boot/ 2>/dev/null
2770 # We've finished updating the kernel.
2773 # Do we need to ask for a reboot now?
2774 if [ -f $1/kernelfirst ] &&
2775 [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2778 Kernel updates have been installed. Please reboot and run
2779 "$0 install" again to finish installing updates.
2785 # If we haven't already dealt with the world, deal with it.
2786 if ! [ -f $1/worlddone ]; then
2787 # Install new shared libraries next
2788 grep -vE '^/boot/' $1/INDEX-NEW |
2789 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2790 install_from_index INDEX-NEW || return 1
2792 # Deal with everything else
2793 grep -vE '^/boot/' $1/INDEX-OLD |
2794 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2795 grep -vE '^/boot/' $1/INDEX-NEW |
2796 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2797 install_from_index INDEX-NEW || return 1
2798 install_delete INDEX-OLD INDEX-NEW || return 1
2800 # Rebuild /etc/spwd.db and /etc/pwd.db if necessary.
2801 if [ /etc/master.passwd -nt /etc/spwd.db ] ||
2802 [ /etc/master.passwd -nt /etc/pwd.db ]; then
2803 pwd_mkdb /etc/master.passwd
2806 # Rebuild /etc/login.conf.db if necessary.
2807 if [ /etc/login.conf -nt /etc/login.conf.db ]; then
2808 cap_mkdb /etc/login.conf
2811 # We've finished installing the world and deleting old files
2812 # which are not shared libraries.
2815 # Do we need to ask the user to portupgrade now?
2816 grep -vE '^/boot/' $1/INDEX-NEW |
2817 grep -E '/lib/.*\.so\.[0-9]+\|' |
2820 if grep -vE '^/boot/' $1/INDEX-OLD |
2821 grep -E '/lib/.*\.so\.[0-9]+\|' |
2824 join -v 1 - newfiles |
2828 Completing this upgrade requires removing old shared object files.
2829 Please rebuild all installed 3rd party software (e.g., programs
2830 installed from the ports tree) and then run "$0 install"
2831 again to finish installing updates.
2839 # Remove old shared libraries
2840 grep -vE '^/boot/' $1/INDEX-NEW |
2841 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2842 grep -vE '^/boot/' $1/INDEX-OLD |
2843 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2844 install_delete INDEX-OLD INDEX-NEW || return 1
2846 # Remove temporary files
2847 rm INDEX-OLD INDEX-NEW
2850 # Rearrange bits to allow the installed updates to be rolled back
2851 install_setup_rollback () {
2852 # Remove the "reboot after installing kernel", "kernel updated", and
2853 # "finished installing the world" flags if present -- they are
2854 # irrelevant when rolling back updates.
2855 if [ -f ${BDHASH}-install/kernelfirst ]; then
2856 rm ${BDHASH}-install/kernelfirst
2857 rm ${BDHASH}-install/kerneldone
2859 if [ -f ${BDHASH}-install/worlddone ]; then
2860 rm ${BDHASH}-install/worlddone
2863 if [ -L ${BDHASH}-rollback ]; then
2864 mv ${BDHASH}-rollback ${BDHASH}-install/rollback
2867 mv ${BDHASH}-install ${BDHASH}-rollback
2870 # Actually install updates
2872 echo -n "Installing updates..."
2874 # Make sure we have all the files we should have
2875 install_verify ${BDHASH}-install/INDEX-OLD \
2876 ${BDHASH}-install/INDEX-NEW || return 1
2878 # Remove system immutable flag from files
2879 install_unschg ${BDHASH}-install/INDEX-OLD \
2880 ${BDHASH}-install/INDEX-NEW || return 1
2882 # Install new files, delete old files, and update linker.hints
2883 install_files ${BDHASH}-install || return 1
2885 # Rearrange bits to allow the installed updates to be rolled back
2886 install_setup_rollback
2891 # Rearrange bits to allow the previous set of updates to be rolled back next.
2892 rollback_setup_rollback () {
2893 if [ -L ${BDHASH}-rollback/rollback ]; then
2894 mv ${BDHASH}-rollback/rollback rollback-tmp
2895 rm -r ${BDHASH}-rollback/
2896 rm ${BDHASH}-rollback
2897 mv rollback-tmp ${BDHASH}-rollback
2899 rm -r ${BDHASH}-rollback/
2900 rm ${BDHASH}-rollback
2904 # Install old files, delete new files, and update linker.hints
2906 # Install old shared library files which don't have the same path as
2907 # a new shared library file.
2908 grep -vE '^/boot/' $1/INDEX-NEW |
2909 grep -E '/lib/.*\.so\.[0-9]+\|' |
2911 sort > INDEX-NEW.libs.flist
2912 grep -vE '^/boot/' $1/INDEX-OLD |
2913 grep -E '/lib/.*\.so\.[0-9]+\|' |
2914 sort -k 1,1 -t '|' - |
2915 join -t '|' -v 1 - INDEX-NEW.libs.flist > INDEX-OLD
2916 install_from_index INDEX-OLD || return 1
2918 # Deal with files which are neither kernel nor shared library
2919 grep -vE '^/boot/' $1/INDEX-OLD |
2920 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2921 grep -vE '^/boot/' $1/INDEX-NEW |
2922 grep -vE '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2923 install_from_index INDEX-OLD || return 1
2924 install_delete INDEX-NEW INDEX-OLD || return 1
2926 # Install any old shared library files which we didn't install above.
2927 grep -vE '^/boot/' $1/INDEX-OLD |
2928 grep -E '/lib/.*\.so\.[0-9]+\|' |
2929 sort -k 1,1 -t '|' - |
2930 join -t '|' - INDEX-NEW.libs.flist > INDEX-OLD
2931 install_from_index INDEX-OLD || return 1
2933 # Delete unneeded shared library files
2934 grep -vE '^/boot/' $1/INDEX-OLD |
2935 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-OLD
2936 grep -vE '^/boot/' $1/INDEX-NEW |
2937 grep -E '/lib/.*\.so\.[0-9]+\|' > INDEX-NEW
2938 install_delete INDEX-NEW INDEX-OLD || return 1
2940 # Deal with kernel files
2941 grep -E '^/boot/' $1/INDEX-OLD > INDEX-OLD
2942 grep -E '^/boot/' $1/INDEX-NEW > INDEX-NEW
2943 install_from_index INDEX-OLD || return 1
2944 install_delete INDEX-NEW INDEX-OLD || return 1
2945 if [ -s INDEX-OLD -o -s INDEX-NEW ]; then
2946 kldxref -R /boot/ 2>/dev/null
2949 # Remove temporary files
2950 rm INDEX-OLD INDEX-NEW INDEX-NEW.libs.flist
2953 # Actually rollback updates
2955 echo -n "Uninstalling updates..."
2957 # If there are updates waiting to be installed, remove them; we
2958 # want the user to re-run 'fetch' after rolling back updates.
2959 if [ -L ${BDHASH}-install ]; then
2960 rm -r ${BDHASH}-install/
2961 rm ${BDHASH}-install
2964 # Make sure we have all the files we should have
2965 install_verify ${BDHASH}-rollback/INDEX-NEW \
2966 ${BDHASH}-rollback/INDEX-OLD || return 1
2968 # Remove system immutable flag from files
2969 install_unschg ${BDHASH}-rollback/INDEX-NEW \
2970 ${BDHASH}-rollback/INDEX-OLD || return 1
2972 # Install old files, delete new files, and update linker.hints
2973 rollback_files ${BDHASH}-rollback || return 1
2975 # Remove the rollback directory and the symlink pointing to it; and
2976 # rearrange bits to allow the previous set of updates to be rolled
2978 rollback_setup_rollback
2983 # Compare INDEX-ALL and INDEX-PRESENT and print warnings about differences.
2985 # Get all the lines which mismatch in something other than file
2986 # flags. We ignore file flags because sysinstall doesn't seem to
2987 # set them when it installs FreeBSD; warning about these adds a
2988 # very large amount of noise.
2989 cut -f 1-5,7-8 -d '|' $1 > $1.noflags
2990 sort -k 1,1 -t '|' $1.noflags > $1.sorted
2991 cut -f 1-5,7-8 -d '|' $2 |
2992 comm -13 $1.noflags - |
2993 fgrep -v '|-|||||' |
2994 sort -k 1,1 -t '|' |
2995 join -t '|' $1.sorted - > INDEX-NOTMATCHING
2997 # Ignore files which match IDSIGNOREPATHS.
2998 for X in ${IDSIGNOREPATHS}; do
2999 grep -E "^${X}" INDEX-NOTMATCHING
3002 comm -13 - INDEX-NOTMATCHING > INDEX-NOTMATCHING.tmp
3003 mv INDEX-NOTMATCHING.tmp INDEX-NOTMATCHING
3005 # Go through the lines and print warnings.
3007 FPATH=`echo "${LINE}" | cut -f 1 -d '|'`
3008 TYPE=`echo "${LINE}" | cut -f 2 -d '|'`
3009 OWNER=`echo "${LINE}" | cut -f 3 -d '|'`
3010 GROUP=`echo "${LINE}" | cut -f 4 -d '|'`
3011 PERM=`echo "${LINE}" | cut -f 5 -d '|'`
3012 HASH=`echo "${LINE}" | cut -f 6 -d '|'`
3013 LINK=`echo "${LINE}" | cut -f 7 -d '|'`
3014 P_TYPE=`echo "${LINE}" | cut -f 8 -d '|'`
3015 P_OWNER=`echo "${LINE}" | cut -f 9 -d '|'`
3016 P_GROUP=`echo "${LINE}" | cut -f 10 -d '|'`
3017 P_PERM=`echo "${LINE}" | cut -f 11 -d '|'`
3018 P_HASH=`echo "${LINE}" | cut -f 12 -d '|'`
3019 P_LINK=`echo "${LINE}" | cut -f 13 -d '|'`
3021 # Warn about different object types.
3022 if ! [ "${TYPE}" = "${P_TYPE}" ]; then
3023 echo -n "${FPATH} is a "
3025 f) echo -n "regular file, "
3027 d) echo -n "directory, "
3029 L) echo -n "symlink, "
3032 echo -n "but should be a "
3034 f) echo -n "regular file."
3036 d) echo -n "directory."
3038 L) echo -n "symlink."
3043 # Skip other tests, since they don't make sense if
3044 # we're comparing different object types.
3048 # Warn about different owners.
3049 if ! [ "${OWNER}" = "${P_OWNER}" ]; then
3050 echo -n "${FPATH} is owned by user id ${P_OWNER}, "
3051 echo "but should be owned by user id ${OWNER}."
3054 # Warn about different groups.
3055 if ! [ "${GROUP}" = "${P_GROUP}" ]; then
3056 echo -n "${FPATH} is owned by group id ${P_GROUP}, "
3057 echo "but should be owned by group id ${GROUP}."
3060 # Warn about different permissions. We do not warn about
3061 # different permissions on symlinks, since some archivers
3062 # don't extract symlink permissions correctly and they are
3064 if ! [ "${PERM}" = "${P_PERM}" ] &&
3065 ! [ "${TYPE}" = "L" ]; then
3066 echo -n "${FPATH} has ${P_PERM} permissions, "
3067 echo "but should have ${PERM} permissions."
3070 # Warn about different file hashes / symlink destinations.
3071 if ! [ "${HASH}" = "${P_HASH}" ]; then
3072 if [ "${TYPE}" = "L" ]; then
3073 echo -n "${FPATH} is a symlink to ${P_HASH}, "
3074 echo "but should be a symlink to ${HASH}."
3076 if [ "${TYPE}" = "f" ]; then
3077 echo -n "${FPATH} has SHA256 hash ${P_HASH}, "
3078 echo "but should have SHA256 hash ${HASH}."
3082 # We don't warn about different hard links, since some
3083 # some archivers break hard links, and as long as the
3084 # underlying data is correct they really don't matter.
3085 done < INDEX-NOTMATCHING
3088 rm $1 $1.noflags $1.sorted $2 INDEX-NOTMATCHING
3091 # Do the work involved in comparing the system to a "known good" index
3093 workdir_init || return 1
3095 # Prepare the mirror list.
3096 fetch_pick_server_init && fetch_pick_server
3098 # Try to fetch the public key until we run out of servers.
3099 while ! fetch_key; do
3100 fetch_pick_server || return 1
3103 # Try to fetch the metadata index signature ("tag") until we run
3104 # out of available servers; and sanity check the downloaded tag.
3105 while ! fetch_tag; do
3106 fetch_pick_server || return 1
3108 fetch_tagsanity || return 1
3110 # Fetch INDEX-OLD and INDEX-ALL.
3111 fetch_metadata INDEX-OLD INDEX-ALL || return 1
3113 # Generate filtered INDEX-OLD and INDEX-ALL files containing only
3114 # the components we want and without anything marked as "Ignore".
3115 fetch_filter_metadata INDEX-OLD || return 1
3116 fetch_filter_metadata INDEX-ALL || return 1
3118 # Merge the INDEX-OLD and INDEX-ALL files into INDEX-ALL.
3119 sort INDEX-OLD INDEX-ALL > INDEX-ALL.tmp
3120 mv INDEX-ALL.tmp INDEX-ALL
3123 # Translate /boot/${KERNCONF} to ${KERNELDIR}
3124 fetch_filter_kernel_names INDEX-ALL ${KERNCONF}
3126 # Inspect the system and generate an INDEX-PRESENT file.
3127 fetch_inspect_system INDEX-ALL INDEX-PRESENT /dev/null || return 1
3129 # Compare INDEX-ALL and INDEX-PRESENT and print warnings about any
3131 IDS_compare INDEX-ALL INDEX-PRESENT
3134 #### Main functions -- call parameter-handling and core functions
3136 # Using the command line, configuration file, and defaults,
3137 # set all the parameters which are needed later.
3145 # Fetch command. Make sure that we're being called
3146 # interactively, then run fetch_check_params and fetch_run
3149 echo -n "`basename $0` fetch should not "
3150 echo "be run non-interactively."
3151 echo "Run `basename $0` cron instead."
3158 # Cron command. Make sure the parameters are sensible; wait
3159 # rand(3600) seconds; then fetch updates. While fetching updates,
3160 # send output to a temporary file; only print that file if the
3164 sleep `jot -r 1 0 3600`
3166 TMPFILE=`mktemp /tmp/freebsd-update.XXXXXX` || exit 1
3167 if ! fetch_run >> ${TMPFILE} ||
3168 ! grep -q "No updates needed" ${TMPFILE} ||
3169 [ ${VERBOSELEVEL} = "debug" ]; then
3170 mail -s "`hostname` security updates" ${MAILTO} < ${TMPFILE}
3176 # Fetch files for upgrading to a new release.
3178 upgrade_check_params
3179 upgrade_run || exit 1
3182 # Install downloaded updates.
3184 install_check_params
3185 install_run || exit 1
3188 # Rollback most recently installed updates.
3190 rollback_check_params
3191 rollback_run || exit 1
3194 # Compare system against a "known good" index.
3202 # Make sure we find utilities from the base system
3203 export PATH=/sbin:/bin:/usr/sbin:/usr/bin:${PATH}
3205 # Set a pager if the user doesn't
3206 if [ -z "$PAGER" ]; then
3210 # Set LC_ALL in order to avoid problems with character ranges like [A-Z].
3214 for COMMAND in ${COMMANDS}; do