source: src-sh/pc-updatemanager/pc-updatemanager @ b246b06

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since b246b06 was b246b06, checked in by Kris Moore <kris@…>, 12 months ago

After doing a major update, we need to re-stamp GRUB to ensure any new
modules get loaded at boot-time

  • Property mode set to 100755
File size: 15.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="`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 "----------------------------------"
167        echo $line | cut -d ':' -f 2 | sed 's|,|\
168|g'
169        echo ""
170        echo "To change branches run: ${0} chbranch <branch>"
171        BRANCHFOUND="1"
172        continue
173     fi
174  done < ${PATCHTMPFILE}
175
176  # If no patches
177  if [ "${BRANCHFOUND}" = "0" ]; then 
178     # No available updates
179     echo "No branches currently available!"
180  fi
181
182  rm ${PATCHTMPFILE}
183
184};
185
186do_check() {
187  # Setup our variable, we have no patches so far
188  PATCHFOUND="0" 
189
190  while read line
191  do
192     echo $line | grep -q "^MU:"
193     if [ $? -eq 0 ] ; then
194        parse_mu_update_line "$line"
195        continue
196     fi
197     echo $line | grep -q "^SA:"
198     if [ $? -eq 0 ] ; then
199        parse_sa_update_line "$line"
200        continue
201     fi
202  done < ${PATCHTMPFILE}
203
204  # If no patches
205  if [ "${PATCHFOUND}" = "0" ]; then 
206     # No available updates
207     echo "Your system is up to date!"
208  fi
209
210  rm ${PATCHTMPFILE}
211
212};
213
214parse_mu_update_line()
215{
216   local line="$1"
217
218   # Check if this is a major update we can install
219   local sysUpOVer=`echo $line | cut -d ':' -f 2`
220   local sysUpNVer=`echo $line | cut -d ':' -f 3`
221
222   # Is this an update for our version?
223   if [ "$SYSVER" != "$sysUpOVer" ] ; then return; fi
224
225   if [ $PATCHFOUND -eq 0 ] ; then
226      echo ""
227      echo "The following updates are available:"
228      echo "------------------------------------"
229   fi
230   echo "NAME: System Update to ${sysUpNVer}" 
231   echo "TYPE: SYSUPDATE" 
232   echo "TAG: fbsd-${sysUpNVer}"
233   echo "VERSION: ${sysUpNVer}" 
234   echo ""
235   echo "To install: \"pc-updatemanager install fbsd-${sysUpNVer}\""
236   echo ""
237   echo ""
238
239   PATCHFOUND="`expr ${PATCHFOUND} + 1`" 
240}
241
242parse_sa_update_line()
243{
244   local line="$1"
245
246   # Check if this is an update we can install
247   local saVer=`echo $line | cut -d ':' -f 2`
248   local saArch=`echo $line | cut -d ':' -f 3`
249   local saType=`echo $line | cut -d ':' -f 4`
250   local saIdent="`echo $line | cut -d ':' -f 5`"
251
252   # For this version?
253   if [ "$SYSVER" != "$saVer" ] ; then return ; fi
254
255   # For this system arch?
256   if [ "$ARCH" != "$saArch" -a "$saArch" != "noarch" ] ; then return ; fi
257
258   # For this system type?
259   if [ "$SYSTYPE" != "$saType" -a "$saType" != "both" ] ; then return ; fi
260
261   # Already installed?
262   if [ -e "${INSDIR}/${SYSVER}/$saIdent" ] ; then return ; fi
263
264   if [ $PATCHFOUND -eq 0 ] ; then
265      echo ""
266      echo "The following updates are available:"
267      echo "------------------------------------"
268   fi
269   local saDesc="`echo $line | cut -d ':' -f 6`"
270   local saSize=`echo $line | cut -d ':' -f 8`
271   local saDetail="http://trac.pcbsd.org`echo $line | cut -d ':' -f 10`"
272   local saDate="`echo $line | cut -d ':' -f 11`"
273
274   echo "NAME: ${saDesc}" 
275   echo "TYPE: PATCH" 
276   echo "TAG: ${saIdent}"
277   echo "DETAILS: ${saDetail}"
278   echo "DATE: ${saDate}"
279   echo "SIZE: ${saSize}Mb" 
280   echo " "
281   echo "To install: \"pc-updatemanager install ${saIdent}\""
282   echo " "
283
284   PATCHFOUND="`expr ${PATCHFOUND} + 1`" 
285}
286
287start_pcbsd_patch() {
288  local up="$1"
289  local saIdent=""
290  local saVer=""
291  local saArch=""
292  local saPlat=""
293  local saUrl=""
294  local saDesc=""
295  local saCsum=""
296  local patchLine=""
297  if [ -e "${INSDIR}/${SYSVER}/${up}" ]; then
298     rm ${PATCHTMPFILE}
299     exit_err "Patch $up already installed!"
300  fi
301
302  while read line
303  do
304    echo $line | grep -q "^SA:"
305    if [ $? -ne 0 ] ; then continue; fi
306    saIdent="`echo $line | cut -d ':' -f 5`"
307    # Look for the right stand-alone update
308    if [ "$saIdent" != "$up" ] ; then continue ; fi
309    saVer=`echo $line | cut -d ':' -f 2`
310    saArch=`echo $line | cut -d ':' -f 3`
311    saType=`echo $line | cut -d ':' -f 4`
312    # Does this update apply to this system arch?
313    if [ "$saArch" != "$ARCH" -a "$saArch" != "noarch" ] ; then
314       rm ${PATCHTMPFILE}
315       exit_err "Patch $up is not for this system arch type!"
316    fi
317    # For this version?
318    if [ "$saVer" != "$SYSVER" ] ; then
319       rm ${PATCHTMPFILE}
320       exit_err "Patch $up is not for this system version!"
321    fi
322
323    # For this system type?
324    if [ "$SYSTYPE" != "$saType" -a "$saType" != "both" ] ; then
325       rm ${PATCHTMPFILE}
326       exit_err "Patch $up is not for this system type!"
327    fi
328
329    # Got here? We have a patch ready to go
330    patchLine="$line"
331    break
332  done < ${PATCHTMPFILE}
333
334  # Did we find the patch?
335  if [ -z "$patchLine" ] ; then
336     rm ${PATCHTMPFILE}
337     exit_err "Patch $up not found!"
338  fi
339
340  # Get the patch details
341  saVer=`echo $patchLine | cut -d ':' -f 2`
342  saArch=`echo $patchLine | cut -d ':' -f 3`
343  saPlat=`echo $patchLine | cut -d ':' -f 4`
344  saIdent="`echo $patchLine | cut -d ':' -f 5`"
345  saDesc="`echo $patchLine | cut -d ':' -f 6`"
346  saUrl="`echo $patchLine | cut -d ':' -f 7`"
347  saCsum="`echo $patchLine | cut -d ':' -f 9`"
348  local FILENAME="`basename $saUrl`"
349
350  # Start downloading the patch
351  touch ${TRIGGERFILE}
352  echo "DOWNLOADING: ${saIdent}"
353  echo "DOWNLOADING: ${saIdent}" >${TRIGGERFILE}
354
355  # Get the file
356  get_file_from_mirrors "/${saUrl}" "${DOWNLOADDIR}/${FILENAME}" "update"
357  if [ $? -ne 0 ] ; then
358     rm ${PATCHTMPFILE}
359     echo "FAILED: ${saIdent}" >${TRIGGERFILE}
360     exit_err "Failed to download: ${saIdent}"
361  fi
362
363  # Check the sha256 checksum
364  if [ "$saCsum" != "`sha256 -q ${DOWNLOADDIR}/${FILENAME} 2>/dev/null`" ]
365  then
366     # Download MD5 doesn't match! Delete the file
367     rm ${PATCHTMPFILE}
368     rm ${DOWNLOADDIR}/${FILENAME}
369     echo "FAILED: ${saIdent}" >${TRIGGERFILE}
370     exit_err "Failed to download: ${saIdent}"
371  else
372     echo "DOWNLOADFINISHED: ${saIdent}"
373     echo "DOWNLOADFINISHED: ${saIdent}" >${TRIGGERFILE}
374  fi
375
376  echo "INSTALLING: ${saIdent}" >${TRIGGERFILE}
377  sleep 1
378
379  PATCHTMPDIR="`mktemp -d ${DOWNLOADDIR}/patchInstallXXXXX`"
380  rc_halt "tar xvJf ${DOWNLOADDIR}/${FILENAME} -C ${PATCHTMPDIR}" 2>/dev/null
381  PATCHDIR="$PATCHTMPDIR" ; export PATCHDIR
382  cd ${PATCHTMPDIR}
383  sh update.sh
384  if [ $? -eq 0 ]; then
385     touch "${INSDIR}/${SYSVER}/${saIdent}"
386     echo "INSTALLFINISHED: ${saIdent}"
387     echo "INSTALLFINISHED: ${saIdent}" >${TRIGGERFILE}
388  else
389     rm ${PATCHTMPFILE}
390     rm -rf ${PATCHTMPDIR}
391     rm ${DOWNLOADDIR}/${FILENAME}
392     echo "INSTALLFAILED: ${saIdent}" >${TRIGGERFILE}
393     exit_err "INSTALLFAILED: ${saIdent}"
394  fi
395
396  rc_halt "rm -rf ${PATCHTMPDIR}"
397  rc_halt "rm ${DOWNLOADDIR}/${FILENAME}"
398}
399
400rollback_update()
401{
402  cp /usr/local/etc/pkg.conf.preUpgrade /usr/local/etc/pkg.conf
403  if [ "$PKGROLLBACK" = "YES" ] ; then
404
405  fi
406}
407
408start_major_fbsd_update() {
409  local rel=`echo $1 | sed 's|fbsd-||g'`
410  PKGROLLBACK="NO"
411
412  # Lets backup the old pkg.conf file
413  cp /usr/local/etc/pkg.conf /usr/local/etc/pkg.conf.preUpgrade
414
415  # Set the new pkg.conf file to the new repo
416  local arch=`uname -m`
417  echo "packagesite: http://pkg.cdn.pcbsd.org/$rel/$arch" >/usr/local/etc/pkg.conf
418  echo "PUBKEY: /usr/local/etc/pkg-pubkey.cert" >>/usr/local/etc/pkg.conf
419  echo "PKG_CACHEDIR: /usr/local/tmp" >>/usr/local/etc/pkg.conf
420
421  # Lets backup old pkgng
422  oPkg=`pkg info pkg | awk '{print $1}'`
423  pkg create -o /usr/local/tmp/ ${oPkg}
424  if [ -e "/usr/local/tmp/oldPkg.txz" ] ; then rm /usr/local/tmp/oldPkg.txz; fi
425  mv /usr/local/tmp/${oPkg}.txz /usr/local/tmp/oldPkg.txz
426
427  # Lets download the new pkgng for this release
428  if [ -e "/usr/local/tmp/pkg.txz" ] ; then rm /usr/local/tmp/pkg.txz; fi
429  get_file_from_mirrors "/packages/$rel/$arch/Latest/pkg.txz" "/usr/local/tmp/pkg.txz" "pkg"
430  if [ $? -ne 0 ] ; then
431     rollback_update
432     exit_err "Failed downloading pkgng update..."
433  fi
434
435  # Lets install pkgng update
436  PKGROLLBACK="YES"
437  echo "Installing pkgng update..."
438  pkg-static add /usr/local/tmp/pkg.txz
439
440  # Lets pre-download all packages
441  download_cache_packages "pkg-static upgrade -fn"
442  if [ $? -ne 0 ] ; then
443     rollback_update
444     exit_err "Failed downloading packages for update..."
445  fi
446
447  # Download update files from freebsd-update
448  freebsd-update --non-interactive -r "$rel" upgrade
449  if [ $? -ne 0 ] ; then
450     rollback_update
451     exit_err "Failed running \"freebsd-update -r $rel upgrade\"..."
452  fi
453
454  # Now install freebsd-update files
455  freebsd-update --non-interactive install
456  if [ $? -ne 0 ] ; then
457     rollback_update
458     exit_err "Failed running \"freebsd-update install\"..."
459  fi
460
461  # Now its time to force a pkg-update of all packages to their new FreeBSD compiled versions
462  SKIP_CACHE_PKGS="YES" ; export SKIP_CACHE_PKGS
463  pc-updatemanager pkgupdate -f
464
465  # Last but not least, lets make sure to re-stamp GRUB in case any loader.conf.pcbsd modules changed
466  grub-mkconfig -o /boot/grub/grub.cfg
467
468  echo "Major update finished!" 
469  echo "Please reboot the system for changes to take effect." 
470  exit 0
471}
472
473do_install() {
474        # Make sure we are root and have specified updates to install
475        if [ `id -u` != "0" ] ; then exit_err "Must be run as root!" ; fi
476        if [ -z "$1" ] ; then exit_err "No updates specified to install!"; fi
477
478        # Begin to update the selected items
479        for up in `echo $1 | sed 's|,| |g'`
480        do
481          # If this is a major update, start it up!
482          echo $up | grep -q "^fbsd-" 
483          if [ $? -eq 0 ] ; then
484            start_major_fbsd_update "$up"
485            exit 0
486          elif [ "$up" == "pkg-updates" ] ; then
487            # Check if this is a pkg-update request or regular patch
488            start_pkg_updates
489            continue
490          else
491            # Doing regular pcbsd patch
492            start_pcbsd_patch "${up}"
493            continue
494          fi
495
496        done 
497
498        # All Finished!
499        rm ${PATCHTMPFILE}
500        exit 0
501}
502
503is_net_up() {
504        ping -c 1 www.pcbsd.org >/dev/null 2>/dev/null
505        return $?
506}
507
508checkup_pkgs() {
509
510  # Checkout the installed pkgs and compare to master list
511  rc_halt "pkg-static update"
512  pkg-static upgrade -n >/tmp/.pkgData.$$
513  grep -q "Upgrading" /tmp/.pkgData.$$
514  if [ $? -ne 0 ] ; then
515     echo "All packages are up to date!"
516     rm /tmp/.pkgData.$$
517     return 0
518  fi
519
520  # Display the pkg update data
521  cat /tmp/.pkgData.$$
522  rm /tmp/.pkgData.$$
523  echo ""
524  echo "To start the upgrade run \"${0} pkgupdate\""
525  return 0
526}
527
528update_pkgs()
529{
530  if [ -n "$1" -a "$1" != "-f" ] ; then
531     exit_err "Invalid option $1 passed"
532  fi
533
534  # If we aren't instructed to skip the package cache process
535  if [ -z "$SKIP_CACHE_PKGS" ] ; then
536    # Lets pre-download all the packages
537    download_cache_packages "pkg-static upgrade -U -n $1"
538  fi
539
540  check_pkg_conflicts "upgrade -U -F -y $1"
541  if [ $? -ne 0 ] ; then
542     exit 1
543  fi
544
545  # Start the package update!
546  pkg-static upgrade -U -y ${1}
547  result=$?
548
549  # Lets now re-extract our overlay data to grab any adjusted port files
550  pc-extractoverlay ports >/dev/null 2>/dev/null
551
552  # Now re-extract overlay files to ensure we do file fixing / massaging
553  if [ "$SYSTYPE" = "PCBSD" ]; then
554    pc-extractoverlay desktop
555  fi
556  if [ "$SYSTYPE" = "TRUEOS" ]; then
557    pc-extractoverlay server
558  fi
559
560  if [ $result -ne 0 ] ; then
561     echo "Failed updating..."
562  fi
563  exit $result
564}
565
566# Check if we have an update folder for the version we are on
567if [ ! -d "${AVAILDIR}/${SYSVER}" ] ; then mkdir -p ${AVAILDIR}/${SYSVER} ; fi
568
569if [ "`id -u`" = "0" ] ; then
570  # Make the installed directory for this version
571  if [ ! -d "${INSDIR}/${SYSVER}" ] ; then mkdir -p ${INSDIR}/${SYSVER} ; fi
572
573  # Make the ignore directory for this version
574  if [ ! -d "${IGNDIR}/${SYSVER}" ] ; then mkdir -p ${IGNDIR}/${SYSVER} ; fi
575fi
576
577case $1 in
578           check) get_update_file
579                  do_check ;;
580        branches) get_update_file
581                  list_branches ;;
582        chbranch) get_update_file
583                  do_branch "${2}" ;;
584        pkgcheck) checkup_pkgs ;;
585       pkgupdate) update_pkgs "$2" ;;
586         install) get_update_file
587                  do_install "${2}" ;;
588        *) show_usage ;;
589esac
590
591exit 0
Note: See TracBrowser for help on using the repository browser.