source: src-sh/pc-updatemanager/pc-updatemanager @ 876ed39

9.2-release
Last change on this file since 876ed39 was 876ed39, checked in by Kris Moore <kris@…>, 9 months ago

Backport the fixes for changing branch / major upgrade to 9.2

  • Property mode set to 100755
File size: 16.6 KB
Line 
1#!/bin/sh
2# Script which performs updating services
3############################################################################
4
5# Source our functions
6. /usr/local/share/pcbsd/scripts/functions.sh
7
8# Set the program location
9PROGDIR="/usr/local/share/pcbsd/pc-updatemanager" ; export PROGDIR
10
11# Start by sourcing /etc/profile
12# This grabs any HTTP_ / FTP_ PROXY variables
13. /etc/profile
14
15PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
16export PCBSD_ETCCONF
17
18# Set the pub openssl key
19SKEY="${PROGDIR}/conf/security.key" ; export SKEY
20
21# Directory to store downloaded updates
22DOWNLOADDIR="/usr/local/tmp"
23
24# Get the system version we are checking for updates to
25SYSVER="`uname -r | cut -d '-' -f 1-2`" ; export SYSVER
26
27# Get the system type
28SYSTYPE="`/usr/local/bin/pbreg get /PC-BSD/SysType`" ; export SYSTYPE
29
30# Set the config location
31UPDATECONF="${PROGDIR}/conf/sysupdate.conf"
32
33# Set the system arch type
34ARCH=`uname -m`
35
36# Default pcbsd.conf file
37PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
38
39# Patchset Data
40PATCHSERVER="`cat ${UPDATECONF} | grep '^UPDATESERVER:' | cut -d ' ' -f 2`"
41PATCHSET="`cat ${UPDATECONF} | grep '^PATCHSET:' | cut -d ' ' -f 2`"
42PATCHFILE="${PATCHSET}.upd"
43PATCHURL="${PATCHSERVER}/${PATCHFILE}"
44PATCHTMPDIR=`mktemp -d /tmp/.sysupdateXXXXXX`
45PATCHTMPFILE="${PATCHTMPDIR}/sysupdate-${SYSVER}.upd"
46
47MUSTAGEDIR="${DOWNLOADDIR}/update-stagedir"
48
49# Enable ftp passive mode for file transfers
50FTP_PASSIVE_MODE="YES" ; export FTP_PASSIVE_MODE
51
52if [ ! -d "${DOWNLOADDIR}" ]; then mkdir -p ${DOWNLOADDIR}; fi
53
54
55# Trigger File for Tray Application
56TRIGGERFILE="/tmp/.sysupdatetraytrigger"
57
58DBDIR="/var/db/pc-updatemanager"
59INSDIR="${DBDIR}/installed"
60IGNDIR="${DBDIR}/ignored"
61
62
63######################################################################
64# Done with config values
65######################################################################
66
67show_usage() {
68        echo "$0 - Usage
69----
70  branches              - List available system branches
71  chbranch <tag>        - Change to new system branch
72  check                 - Check for system updates
73  install <tag>,<tag2>  - Install system updates
74  pkgcheck              - Check for updates to packages
75  pkgupdate             - Install packages updates
76"       
77
78        exit 1
79}
80
81get_update_file() {
82
83  # Now fetch the update file
84  get_file "${PATCHURL}" "${PATCHTMPFILE}" 1 >/dev/null 2>/dev/null
85  if [ $? -ne 0 ] ; then
86     rm ${PATCHTMPFILE} 2>/dev/null
87     is_net_up
88     if [ $? -eq 0 ] ; then
89       echo "No updates available for $SYSVER!" ; exit 0
90     else
91       exit_err "Could not contact update server!"
92     fi
93  fi
94
95  # Now fetch the update file signature
96  get_file "${PATCHURL}.sha1" "${PATCHTMPFILE}.sha1" 1 >/dev/null 2>/dev/null
97  if [ $? -ne 0 ] ; then
98     rm ${PATCHTMPFILE}.sha1 2>/dev/null
99     is_net_up
100     if [ $? -eq 0 ] ; then
101       echo "No updates available for $SYSVER!" ; exit 0
102     else
103       exit_err "Could not contact update server!"
104     fi
105  fi
106
107  # Lets verify the signature of the file
108  openssl dgst -sha1 -verify ${SKEY} \
109        -signature ${PATCHTMPFILE}.sha1 \
110        ${PATCHTMPFILE} >/dev/null 2>/dev/null
111  if [ $? -ne 0 ] ; then
112     rm ${PATCHTMPFILE} 2>/dev/null
113     rm ${PATCHTMPFILE}.sha1 2>/dev/null
114     exit_err "Update file failed verification..."
115  fi
116
117  # Done with the signature we can discard
118  rm ${PATCHTMPFILE}.sha1
119
120}
121
122do_branch() {
123  up="$1"
124
125  # Setup our variable to catch if we have a bad branch
126  BRANCHFOUND="0" 
127
128  while read line
129  do
130     echo $line | grep -q "^BRANCHES:"
131     if [ $? -eq 0 ] ; then
132        for i in `echo $line | cut -d ':' -f 2 | sed 's|,| |g'`
133        do
134          if [ "$i" != "$up" ] ; then continue ;fi
135          if [ "`uname -r | cut -d '-' -f 1-2`" = "$up" ] ; then
136             echo "You are already on this branch!"
137             BRANCHFOUND="1"
138             continue;
139          fi
140          start_major_fbsd_update "fbsd-$up"
141          BRANCHFOUND="1"
142          break
143        done
144     fi
145  done < ${PATCHTMPFILE}
146
147  # If no patches
148  if [ "${BRANCHFOUND}" = "0" ]; then 
149     # No available updates
150     echo "Unknown branch ${1}"
151  fi
152
153  rm ${PATCHTMPFILE}
154
155};
156
157list_branches() {
158  # Setup our variable, we have no branches so far
159  BRANCHFOUND="0" 
160
161  while read line
162  do
163     echo $line | grep -q "^BRANCHES:"
164     if [ $? -eq 0 ] ; then
165        echo "Available branches:"
166        echo "* = Current Branch"
167        echo "----------------------------------"
168        echo $line | cut -d ':' -f 2 | sed 's|,|\
169|g' | while read branch
170        do
171                if [ "`uname -r | cut -d '-' -f 1-2`" = "$branch" ] ; then
172                        echo "$branch *"
173                else
174                        echo "$branch"
175                fi
176        done
177        echo ""
178        echo "To change branches run: ${0} chbranch <branch>"
179        BRANCHFOUND="1"
180        continue
181     fi
182  done < ${PATCHTMPFILE}
183
184  # If no patches
185  if [ "${BRANCHFOUND}" = "0" ]; then 
186     # No available updates
187     echo "No branches currently available!"
188  fi
189
190  rm ${PATCHTMPFILE}
191
192};
193
194do_check() {
195  # Setup our variable, we have no patches so far
196  PATCHFOUND="0" 
197
198  while read line
199  do
200     echo $line | grep -q "^MU:"
201     if [ $? -eq 0 ] ; then
202        parse_mu_update_line "$line"
203        continue
204     fi
205     echo $line | grep -q "^SA:"
206     if [ $? -eq 0 ] ; then
207        parse_sa_update_line "$line"
208        continue
209     fi
210  done < ${PATCHTMPFILE}
211
212  # If no patches
213  if [ "${PATCHFOUND}" = "0" ]; then 
214     # No available updates
215     echo "Your system is up to date!"
216  fi
217
218  rm ${PATCHTMPFILE}
219
220};
221
222parse_mu_update_line()
223{
224   local line="$1"
225
226   # Check if this is a major update we can install
227   local sysUpOVer=`echo $line | cut -d ':' -f 2`
228   local sysUpNVer=`echo $line | cut -d ':' -f 3`
229
230   # Is this an update for our version?
231   if [ "$SYSVER" != "$sysUpOVer" ] ; then return; fi
232
233   if [ $PATCHFOUND -eq 0 ] ; then
234      echo ""
235      echo "The following updates are available:"
236      echo "------------------------------------"
237   fi
238   echo "NAME: System Update to ${sysUpNVer}" 
239   echo "TYPE: SYSUPDATE" 
240   echo "TAG: fbsd-${sysUpNVer}"
241   echo "VERSION: ${sysUpNVer}" 
242   echo ""
243   echo "To install: \"pc-updatemanager install fbsd-${sysUpNVer}\""
244   echo ""
245   echo ""
246
247   PATCHFOUND="`expr ${PATCHFOUND} + 1`" 
248}
249
250parse_sa_update_line()
251{
252   local line="$1"
253
254   # Check if this is an update we can install
255   local saVer=`echo $line | cut -d ':' -f 2`
256   local saArch=`echo $line | cut -d ':' -f 3`
257   local saType=`echo $line | cut -d ':' -f 4`
258   local saIdent="`echo $line | cut -d ':' -f 5`"
259
260   # For this version?
261   if [ "$SYSVER" != "$saVer" ] ; then return ; fi
262
263   # For this system arch?
264   if [ "$ARCH" != "$saArch" -a "$saArch" != "noarch" ] ; then return ; fi
265
266   # For this system type?
267   if [ "$SYSTYPE" != "$saType" -a "$saType" != "both" ] ; then return ; fi
268
269   # Already installed?
270   if [ -e "${INSDIR}/${SYSVER}/$saIdent" ] ; then return ; fi
271
272   if [ $PATCHFOUND -eq 0 ] ; then
273      echo ""
274      echo "The following updates are available:"
275      echo "------------------------------------"
276   fi
277   local saDesc="`echo $line | cut -d ':' -f 6`"
278   local saSize=`echo $line | cut -d ':' -f 8`
279   local saDetail="http://trac.pcbsd.org`echo $line | cut -d ':' -f 10`"
280   local saDate="`echo $line | cut -d ':' -f 11`"
281
282   echo "NAME: ${saDesc}" 
283   echo "TYPE: PATCH" 
284   echo "TAG: ${saIdent}"
285   echo "DETAILS: ${saDetail}"
286   echo "DATE: ${saDate}"
287   echo "SIZE: ${saSize}Mb" 
288   echo " "
289   echo "To install: \"pc-updatemanager install ${saIdent}\""
290   echo " "
291
292   PATCHFOUND="`expr ${PATCHFOUND} + 1`" 
293}
294
295start_pcbsd_patch() {
296  local up="$1"
297  local saIdent=""
298  local saVer=""
299  local saArch=""
300  local saPlat=""
301  local saUrl=""
302  local saDesc=""
303  local saCsum=""
304  local patchLine=""
305  if [ -e "${INSDIR}/${SYSVER}/${up}" ]; then
306     rm ${PATCHTMPFILE}
307     exit_err "Patch $up already installed!"
308  fi
309
310  while read line
311  do
312    echo $line | grep -q "^SA:"
313    if [ $? -ne 0 ] ; then continue; fi
314    saIdent="`echo $line | cut -d ':' -f 5`"
315    # Look for the right stand-alone update
316    if [ "$saIdent" != "$up" ] ; then continue ; fi
317    saVer=`echo $line | cut -d ':' -f 2`
318    saArch=`echo $line | cut -d ':' -f 3`
319    saType=`echo $line | cut -d ':' -f 4`
320    # Does this update apply to this system arch?
321    if [ "$saArch" != "$ARCH" -a "$saArch" != "noarch" ] ; then
322       rm ${PATCHTMPFILE}
323       exit_err "Patch $up is not for this system arch type!"
324    fi
325    # For this version?
326    if [ "$saVer" != "$SYSVER" ] ; then
327       rm ${PATCHTMPFILE}
328       exit_err "Patch $up is not for this system version!"
329    fi
330
331    # For this system type?
332    if [ "$SYSTYPE" != "$saType" -a "$saType" != "both" ] ; then
333       rm ${PATCHTMPFILE}
334       exit_err "Patch $up is not for this system type!"
335    fi
336
337    # Got here? We have a patch ready to go
338    patchLine="$line"
339    break
340  done < ${PATCHTMPFILE}
341
342  # Did we find the patch?
343  if [ -z "$patchLine" ] ; then
344     rm ${PATCHTMPFILE}
345     exit_err "Patch $up not found!"
346  fi
347
348  # Get the patch details
349  saVer=`echo $patchLine | cut -d ':' -f 2`
350  saArch=`echo $patchLine | cut -d ':' -f 3`
351  saPlat=`echo $patchLine | cut -d ':' -f 4`
352  saIdent="`echo $patchLine | cut -d ':' -f 5`"
353  saDesc="`echo $patchLine | cut -d ':' -f 6`"
354  saUrl="`echo $patchLine | cut -d ':' -f 7`"
355  saCsum="`echo $patchLine | cut -d ':' -f 9`"
356  local FILENAME="`basename $saUrl`"
357
358  # Start downloading the patch
359  touch ${TRIGGERFILE}
360  echo "DOWNLOADING: ${saIdent}"
361  echo "DOWNLOADING: ${saIdent}" >${TRIGGERFILE}
362
363  # Get the file
364  get_file_from_mirrors "/${saUrl}" "${DOWNLOADDIR}/${FILENAME}" "update"
365  if [ $? -ne 0 ] ; then
366     rm ${PATCHTMPFILE}
367     echo "FAILED: ${saIdent}" >${TRIGGERFILE}
368     exit_err "Failed to download: ${saIdent}"
369  fi
370
371  # Check the sha256 checksum
372  if [ "$saCsum" != "`sha256 -q ${DOWNLOADDIR}/${FILENAME} 2>/dev/null`" ]
373  then
374     # Download MD5 doesn't match! Delete the file
375     rm ${PATCHTMPFILE}
376     rm ${DOWNLOADDIR}/${FILENAME}
377     echo "FAILED: ${saIdent}" >${TRIGGERFILE}
378     exit_err "Failed to download: ${saIdent}"
379  else
380     echo "DOWNLOADFINISHED: ${saIdent}"
381     echo "DOWNLOADFINISHED: ${saIdent}" >${TRIGGERFILE}
382  fi
383
384  echo "INSTALLING: ${saIdent}" >${TRIGGERFILE}
385  sleep 1
386
387  PATCHTMPDIR="`mktemp -d ${DOWNLOADDIR}/patchInstallXXXXX`"
388  rc_halt "tar xvJf ${DOWNLOADDIR}/${FILENAME} -C ${PATCHTMPDIR}" 2>/dev/null
389  PATCHDIR="$PATCHTMPDIR" ; export PATCHDIR
390  cd ${PATCHTMPDIR}
391  sh update.sh
392  if [ $? -eq 0 ]; then
393     touch "${INSDIR}/${SYSVER}/${saIdent}"
394     echo "INSTALLFINISHED: ${saIdent}"
395     echo "INSTALLFINISHED: ${saIdent}" >${TRIGGERFILE}
396  else
397     rm ${PATCHTMPFILE}
398     rm -rf ${PATCHTMPDIR}
399     rm ${DOWNLOADDIR}/${FILENAME}
400     echo "INSTALLFAILED: ${saIdent}" >${TRIGGERFILE}
401     exit_err "INSTALLFAILED: ${saIdent}"
402  fi
403
404  rc_halt "rm -rf ${PATCHTMPDIR}"
405  rc_halt "rm ${DOWNLOADDIR}/${FILENAME}"
406}
407
408rollback_update()
409{
410  cp /tmp/pcbsd.conf.preUpdate.$$ /usr/local/etc/pkg/repos/pcbsd.conf
411  if [ "$PKGROLLBACK" = "YES" ] ; then
412
413  fi
414}
415
416# After installing the kernel and rebooting, this gets run
417finish_major_fbsd_update() {
418
419  PATH="${PATH}:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
420  export PATH
421
422  echo "Installing FreeBSD updates... Please wait..."
423  freebsd-update --non-interactive install
424  if [ $? -ne 0 ] ; then
425     echo "Warning: Failed running freebsd-update install!"
426     echo "Press ENTER to continue"
427     read tmp
428  fi
429
430  # Remove this, causes all kinds of havok during the update
431  if [ -e "/etc/pkg/FreeBSD.conf" ] ; then
432    mv /etc/pkg/FreeBSD.conf /etc/pkg/FreeBSD.conf.dist
433  fi
434
435  # Now its time to force a pkg-update of all packages to their new FreeBSD compiled versions
436  pc-updatemanager pkgupdate -f
437  if [ $? -ne 0 ] ; then
438     echo "Warning: Failed running 'pc-updatemanager pkgupdate -f'"
439     echo "Press ENTER to continue"
440     read tmp
441  fi
442
443  # Last but not least, lets make sure to re-stamp GRUB in case any loader.conf.pcbsd modules changed
444  grub-mkconfig -o /boot/grub/grub.cfg
445  if [ $? -ne 0 ] ; then
446     echo "Warning: Failed running 'pc-updatemanager pkgupdate -f'"
447     echo "Press ENTER to continue"
448     read tmp
449  fi
450
451  echo "Updating overlay files..."
452  if [ -e "/usr/local/bin/pc-softwaremanager" ] ; then
453    pc-extractoverlay desktop
454  else
455    pc-extractoverlay server
456  fi
457
458  exit 0
459}
460
461start_major_fbsd_update() {
462  local rel=`echo $1 | sed 's|fbsd-||g'`
463  PKGROLLBACK="NO"
464
465  # Save the repo file
466  cp /usr/local/etc/pkg/repos/pcbsd.conf /tmp/pcbsd.conf.preUpdate.$$
467
468  # Set the new pcbsd.conf file to the new repo
469  local arch=`uname -m`
470  echo "pcbsd: {
471        url: "http://pkg.cdn.pcbsd.org/$rel/$arch",
472        signature_type: "fingerprints",
473        fingerprints: "/usr/local/etc/pkg/fingerprints/pcbsd",
474        enabled: true
475       }" > /usr/local/etc/pkg/repos/pcbsd.conf
476
477  # Lets backup old pkgng
478  oPkg=`pkg info pkg | awk '{print $1}'`
479  pkg create -o /usr/local/tmp/ ${oPkg}
480  if [ -e "/usr/local/tmp/oldPkg.txz" ] ; then rm /usr/local/tmp/oldPkg.txz; fi
481  mv /usr/local/tmp/${oPkg}.txz /usr/local/tmp/oldPkg.txz
482
483  # Lets download the new pkgng for this release
484  if [ -e "/usr/local/tmp/pkg.txz" ] ; then rm /usr/local/tmp/pkg.txz; fi
485  get_file_from_mirrors "/packages/$rel/$arch/Latest/pkg.txz" "/usr/local/tmp/pkg.txz" "pkg"
486  if [ $? -ne 0 ] ; then
487     rollback_update
488     exit_err "Failed downloading pkgng update..."
489  fi
490
491  # Lets install pkgng update
492  PKGROLLBACK="YES"
493
494  # Download update files from freebsd-update
495  freebsd-update --non-interactive -r "$rel" upgrade
496  if [ $? -ne 0 ] ; then
497     rollback_update
498     exit_err "Failed running \"freebsd-update -r $rel upgrade\"..."
499  fi
500
501  # Touch our marker that we will need to reboot before doing other updates
502  touch /tmp/.fbsdup-reboot
503
504  # Now install freebsd-update files
505  freebsd-update --non-interactive install
506  if [ $? -ne 0 ] ; then
507     rollback_update
508     exit_err "Failed running \"freebsd-update install\"..."
509  fi
510
511  # Remove backup repo file
512  rm /tmp/pcbsd.conf.preUpdate.$$
513
514  echo "Major update finished!" 
515  echo "Please reboot the system for changes to take effect." 
516  exit 0
517}
518
519do_install() {
520        # Make sure we are root and have specified updates to install
521        if [ `id -u` != "0" ] ; then exit_err "Must be run as root!" ; fi
522        if [ -z "$1" ] ; then exit_err "No updates specified to install!"; fi
523
524        # Begin to update the selected items
525        for up in `echo $1 | sed 's|,| |g'`
526        do
527          # If this is a major update, start it up!
528          echo $up | grep -q "^fbsd-" 
529          if [ $? -eq 0 ] ; then
530            start_major_fbsd_update "$up"
531            exit 0
532          elif [ "$up" == "pkg-updates" ] ; then
533            # Check if this is a pkg-update request or regular patch
534            start_pkg_updates
535            continue
536          else
537            # Doing regular pcbsd patch
538            start_pcbsd_patch "${up}"
539            continue
540          fi
541
542        done 
543
544        # All Finished!
545        rm ${PATCHTMPFILE}
546        exit 0
547}
548
549is_net_up() {
550        ping -c 1 www.pcbsd.org >/dev/null 2>/dev/null
551        return $?
552}
553
554checkup_pkgs() {
555
556  # Update the repo DB
557  rc_halt "pkg-static update"
558
559  pkg-static upgrade -n >/tmp/.pkgData.$$
560  if [ $? -ne 0 ] ; then
561     echo "Error checking for package updates..."
562     exit 1
563  fi
564
565  # Check fr updates now
566  grep -q "Upgrading" /tmp/.pkgData.$$
567  if [ $? -ne 0 ] ; then
568     echo "All packages are up to date!"
569     rm /tmp/.pkgData.$$
570     return 0
571  fi
572
573  # Display the pkg update data
574  cat /tmp/.pkgData.$$
575  rm /tmp/.pkgData.$$
576  echo ""
577  echo "To start the upgrade run \"${0} pkgupdate\""
578  return 0
579}
580
581update_pkgs()
582{
583  if [ -n "$1" -a "$1" != "-f" ] ; then
584     exit_err "Invalid option $1 passed"
585  fi
586
587  # Update the DB first
588  pkg-static update -f
589
590  # Look for conflicts
591  check_pkg_conflicts "upgrade -FUy $1"
592  if [ $? -ne 0 ] ; then
593     exit 1
594  fi
595
596  # Start the package update!
597  pkg-static upgrade -U -y ${1}
598  result=$?
599
600  # Lets now re-extract our overlay data to grab any adjusted port files
601  pc-extractoverlay ports >/dev/null 2>/dev/null
602
603  # Now re-extract overlay files to ensure we do file fixing / massaging
604  if [ "$SYSTYPE" = "PCBSD" ]; then
605    pc-extractoverlay desktop
606  fi
607  if [ "$SYSTYPE" = "TRUEOS" ]; then
608    pc-extractoverlay server
609  fi
610
611  if [ $result -ne 0 ] ; then
612     echo "Failed updating..."
613  fi
614  exit $result
615}
616
617# Check if we have an update folder for the version we are on
618if [ ! -d "${AVAILDIR}/${SYSVER}" ] ; then mkdir -p ${AVAILDIR}/${SYSVER} ; fi
619
620if [ "`id -u`" = "0" ] ; then
621  # Make the installed directory for this version
622  if [ ! -d "${INSDIR}/${SYSVER}" ] ; then mkdir -p ${INSDIR}/${SYSVER} ; fi
623
624  # Make the ignore directory for this version
625  if [ ! -d "${IGNDIR}/${SYSVER}" ] ; then mkdir -p ${IGNDIR}/${SYSVER} ; fi
626fi
627
628case $1 in
629           check) get_update_file
630                  do_check ;;
631        branches) get_update_file
632                  list_branches ;;
633        chbranch) get_update_file
634                  do_branch "${2}" ;;
635        pkgcheck) checkup_pkgs ;;
636       pkgupdate) update_pkgs "$2" ;;
637finish-major-update) finish_major_fbsd_update ;;
638         install) get_update_file
639                  do_install "${2}" ;;
640        *) show_usage ;;
641esac
642
643exit 0
Note: See TracBrowser for help on using the repository browser.