source: src-sh/pc-updatemanager/pc-updatemanager @ 8af4a68

9.2-release
Last change on this file since 8af4a68 was 8af4a68, checked in by Kris Moore <kris@…>, 10 months ago

Backport some fixes to 9.2 for pkgng 1.2.x updating

  • Property mode set to 100755
File size: 16.4 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  # Now its time to force a pkg-update of all packages to their new FreeBSD compiled versions
431  pc-updatemanager pkgupdate -f
432  if [ $? -ne 0 ] ; then
433     echo "Warning: Failed running 'pc-updatemanager pkgupdate -f'"
434     echo "Press ENTER to continue"
435     read tmp
436  fi
437
438  # Last but not least, lets make sure to re-stamp GRUB in case any loader.conf.pcbsd modules changed
439  grub-mkconfig -o /boot/grub/grub.cfg
440  if [ $? -ne 0 ] ; then
441     echo "Warning: Failed running 'pc-updatemanager pkgupdate -f'"
442     echo "Press ENTER to continue"
443     read tmp
444  fi
445
446  echo "Updating overlay files..."
447  if [ -e "/usr/local/bin/pc-softwaremanager" ] ; then
448    pc-extractoverlay desktop
449  else
450    pc-extractoverlay server
451  fi
452
453  exit 0
454}
455
456start_major_fbsd_update() {
457  local rel=`echo $1 | sed 's|fbsd-||g'`
458  PKGROLLBACK="NO"
459
460  # Save the repo file
461  cp /usr/local/etc/pkg/repos/pcbsd.conf /tmp/pcbsd.conf.preUpdate.$$
462
463  # Set the new pcbsd.conf file to the new repo
464  local arch=`uname -m`
465  echo "pcbsd: {
466        url: "http://pkg.cdn.pcbsd.org/$rel/$arch",
467        signature_type: "fingerprints",
468        fingerprints: "/usr/local/etc/pkg/fingerprints/pcbsd",
469        enabled: true
470       }" > /usr/local/etc/pkg/repos/pcbsd.conf
471
472  # Lets backup old pkgng
473  oPkg=`pkg info pkg | awk '{print $1}'`
474  pkg create -o /usr/local/tmp/ ${oPkg}
475  if [ -e "/usr/local/tmp/oldPkg.txz" ] ; then rm /usr/local/tmp/oldPkg.txz; fi
476  mv /usr/local/tmp/${oPkg}.txz /usr/local/tmp/oldPkg.txz
477
478  # Lets download the new pkgng for this release
479  if [ -e "/usr/local/tmp/pkg.txz" ] ; then rm /usr/local/tmp/pkg.txz; fi
480  get_file_from_mirrors "/packages/$rel/$arch/Latest/pkg.txz" "/usr/local/tmp/pkg.txz" "pkg"
481  if [ $? -ne 0 ] ; then
482     rollback_update
483     exit_err "Failed downloading pkgng update..."
484  fi
485
486  # Lets install pkgng update
487  PKGROLLBACK="YES"
488
489  # Download update files from freebsd-update
490  freebsd-update --non-interactive -r "$rel" upgrade
491  if [ $? -ne 0 ] ; then
492     rollback_update
493     exit_err "Failed running \"freebsd-update -r $rel upgrade\"..."
494  fi
495
496  # Touch our marker that we will need to reboot before doing other updates
497  touch /tmp/.fbsdup-reboot
498
499  # Now install freebsd-update files
500  freebsd-update --non-interactive install
501  if [ $? -ne 0 ] ; then
502     rollback_update
503     exit_err "Failed running \"freebsd-update install\"..."
504  fi
505
506  # Remove backup repo file
507  rm /tmp/pcbsd.conf.preUpdate.$$
508
509  echo "Major update finished!" 
510  echo "Please reboot the system for changes to take effect." 
511  exit 0
512}
513
514do_install() {
515        # Make sure we are root and have specified updates to install
516        if [ `id -u` != "0" ] ; then exit_err "Must be run as root!" ; fi
517        if [ -z "$1" ] ; then exit_err "No updates specified to install!"; fi
518
519        # Begin to update the selected items
520        for up in `echo $1 | sed 's|,| |g'`
521        do
522          # If this is a major update, start it up!
523          echo $up | grep -q "^fbsd-" 
524          if [ $? -eq 0 ] ; then
525            start_major_fbsd_update "$up"
526            exit 0
527          elif [ "$up" == "pkg-updates" ] ; then
528            # Check if this is a pkg-update request or regular patch
529            start_pkg_updates
530            continue
531          else
532            # Doing regular pcbsd patch
533            start_pcbsd_patch "${up}"
534            continue
535          fi
536
537        done 
538
539        # All Finished!
540        rm ${PATCHTMPFILE}
541        exit 0
542}
543
544is_net_up() {
545        ping -c 1 www.pcbsd.org >/dev/null 2>/dev/null
546        return $?
547}
548
549checkup_pkgs() {
550
551  # Update the repo DB
552  rc_halt "pkg-static update"
553
554  pkg-static upgrade -n >/tmp/.pkgData.$$
555  if [ $? -ne 0 ] ; then
556     echo "Error checking for package updates..."
557     exit 1
558  fi
559
560  # Check fr updates now
561  grep -q "Upgrading" /tmp/.pkgData.$$
562  if [ $? -ne 0 ] ; then
563     echo "All packages are up to date!"
564     rm /tmp/.pkgData.$$
565     return 0
566  fi
567
568  # Display the pkg update data
569  cat /tmp/.pkgData.$$
570  rm /tmp/.pkgData.$$
571  echo ""
572  echo "To start the upgrade run \"${0} pkgupdate\""
573  return 0
574}
575
576update_pkgs()
577{
578  if [ -n "$1" -a "$1" != "-f" ] ; then
579     exit_err "Invalid option $1 passed"
580  fi
581
582  export EVENT_PIPE
583  check_pkg_conflicts "upgrade -FUy $1"
584  if [ $? -ne 0 ] ; then
585     exit 1
586  fi
587
588  # Start the package update!
589  pkg-static upgrade -U -y ${1}
590  result=$?
591
592  # Lets now re-extract our overlay data to grab any adjusted port files
593  pc-extractoverlay ports >/dev/null 2>/dev/null
594
595  # Now re-extract overlay files to ensure we do file fixing / massaging
596  if [ "$SYSTYPE" = "PCBSD" ]; then
597    pc-extractoverlay desktop
598  fi
599  if [ "$SYSTYPE" = "TRUEOS" ]; then
600    pc-extractoverlay server
601  fi
602
603  if [ $result -ne 0 ] ; then
604     echo "Failed updating..."
605  fi
606  exit $result
607}
608
609# Check if we have an update folder for the version we are on
610if [ ! -d "${AVAILDIR}/${SYSVER}" ] ; then mkdir -p ${AVAILDIR}/${SYSVER} ; fi
611
612if [ "`id -u`" = "0" ] ; then
613  # Make the installed directory for this version
614  if [ ! -d "${INSDIR}/${SYSVER}" ] ; then mkdir -p ${INSDIR}/${SYSVER} ; fi
615
616  # Make the ignore directory for this version
617  if [ ! -d "${IGNDIR}/${SYSVER}" ] ; then mkdir -p ${IGNDIR}/${SYSVER} ; fi
618fi
619
620case $1 in
621           check) get_update_file
622                  do_check ;;
623        branches) get_update_file
624                  list_branches ;;
625        chbranch) get_update_file
626                  do_branch "${2}" ;;
627        pkgcheck) checkup_pkgs ;;
628       pkgupdate) update_pkgs "$2" ;;
629finish-major-update) finish_major_fbsd_update ;;
630         install) get_update_file
631                  do_install "${2}" ;;
632        *) show_usage ;;
633esac
634
635exit 0
Note: See TracBrowser for help on using the repository browser.