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

releng/10.0.1releng/10.0.2
Last change on this file since e07f550 was e07f550, checked in by Kris Moore <kris@…>, 6 months ago

Fix a bug which was creating a empty directory on /

  • Property mode set to 100755
File size: 33.3 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
62ISOTMP="/usr/local/tmp/pcbsd-update.iso"
63
64######################################################################
65# Done with config values
66######################################################################
67
68# Files we always want to upgrade in /etc
69ETCCLOBBER="mail/sendmail.cf mail/freebsd.cf"
70
71show_usage() {
72        echo "$0 - Usage
73----
74  branches              - List available system branches
75  chbranch <tag>        - Change to new system branch
76  check                 - Check for system updates
77  install <tag>,<tag2>  - Install system updates
78  pkgcheck              - Check for updates to packages
79  pkgupdate             - Install packages updates
80"       
81
82        exit 1
83}
84
85get_update_file() {
86
87  # Now fetch the update file
88  get_file "${PATCHURL}" "${PATCHTMPFILE}" 1 >/dev/null 2>/dev/null
89  if [ $? -ne 0 ] ; then
90     rm ${PATCHTMPFILE} 2>/dev/null
91     is_net_up
92     if [ $? -eq 0 ] ; then
93       echo "No updates available for $SYSVER!" ; exit 0
94     else
95       exit_err "Could not contact update server!"
96     fi
97  fi
98
99  # Now fetch the update file signature
100  get_file "${PATCHURL}.sha1" "${PATCHTMPFILE}.sha1" 1 >/dev/null 2>/dev/null
101  if [ $? -ne 0 ] ; then
102     rm ${PATCHTMPFILE}.sha1 2>/dev/null
103     is_net_up
104     if [ $? -eq 0 ] ; then
105       echo "No updates available for $SYSVER!" ; exit 0
106     else
107       exit_err "Could not contact update server!"
108     fi
109  fi
110
111  # Lets verify the signature of the file
112  openssl dgst -sha1 -verify ${SKEY} \
113        -signature ${PATCHTMPFILE}.sha1 \
114        ${PATCHTMPFILE} >/dev/null 2>/dev/null
115  if [ $? -ne 0 ] ; then
116     rm ${PATCHTMPFILE} 2>/dev/null
117     rm ${PATCHTMPFILE}.sha1 2>/dev/null
118     exit_err "Update file failed verification..."
119  fi
120
121  # Done with the signature we can discard
122  rm ${PATCHTMPFILE}.sha1
123
124}
125
126do_branch() {
127  up="$1"
128
129  # Setup our variable to catch if we have a bad branch
130  BRANCHFOUND="0" 
131
132  while read line
133  do
134     echo $line | grep -q "^BRANCHES:"
135     if [ $? -eq 0 ] ; then
136        for i in `echo $line | cut -d ':' -f 2 | sed 's|,| |g'`
137        do
138          if [ "$i" != "$up" ] ; then continue ;fi
139          if [ "`uname -r | cut -d '-' -f 1-2`" = "$up" ] ; then
140             echo "You are already on this branch!"
141             BRANCHFOUND="1"
142             continue;
143          fi
144          start_major_fbsd_update "fbsd-$up"
145          BRANCHFOUND="1"
146          break
147        done
148     fi
149  done < ${PATCHTMPFILE}
150
151  # If no patches
152  if [ "${BRANCHFOUND}" = "0" ]; then 
153     # No available updates
154     echo "Unknown branch ${1}"
155  fi
156
157  rm ${PATCHTMPFILE}
158
159};
160
161list_branches() {
162  # Setup our variable, we have no branches so far
163  BRANCHFOUND="0" 
164
165  while read line
166  do
167     echo $line | grep -q "^BRANCHES:"
168     if [ $? -eq 0 ] ; then
169        echo "Available branches:"
170        echo "* = Current Branch"
171        echo "----------------------------------"
172        echo $line | cut -d ':' -f 2 | sed 's|,|\
173|g' | while read branch
174        do
175                if [ "`uname -r | cut -d '-' -f 1-2`" = "$branch" ] ; then
176                        echo "$branch *"
177                else
178                        echo "$branch"
179                fi
180        done
181        echo ""
182        echo "To change branches run: ${0} chbranch <branch>"
183        BRANCHFOUND="1"
184        continue
185     fi
186  done < ${PATCHTMPFILE}
187
188  # If no patches
189  if [ "${BRANCHFOUND}" = "0" ]; then 
190     # No available updates
191     echo "No branches currently available!"
192  fi
193
194  rm ${PATCHTMPFILE}
195
196};
197
198do_check() {
199  # Setup our variable, we have no patches so far
200  PATCHFOUND="0" 
201
202  while read line
203  do
204     echo $line | grep -q "^MU:"
205     if [ $? -eq 0 ] ; then
206        parse_mu_update_line "$line"
207        continue
208     fi
209     echo $line | grep -q "^SA:"
210     if [ $? -eq 0 ] ; then
211        parse_sa_update_line "$line"
212        continue
213     fi
214  done < ${PATCHTMPFILE}
215
216  # If no patches
217  if [ "${PATCHFOUND}" = "0" ]; then 
218     # No available updates
219     echo "Your system is up to date!"
220  fi
221
222  rm ${PATCHTMPFILE}
223
224};
225
226parse_mu_update_line()
227{
228   local line="$1"
229
230   # Check if this is a major update we can install
231   local sysUpOVer=`echo $line | cut -d ':' -f 2`
232   local sysUpNVer=`echo $line | cut -d ':' -f 3`
233
234   # Is this an update for our version?
235   if [ "$SYSVER" != "$sysUpOVer" ] ; then return; fi
236
237   if [ $PATCHFOUND -eq 0 ] ; then
238      echo ""
239      echo "The following updates are available:"
240      echo "------------------------------------"
241   fi
242   echo "NAME: System Update to ${sysUpNVer}" 
243   echo "TYPE: SYSUPDATE" 
244   echo "TAG: fbsd-${sysUpNVer}"
245   echo "VERSION: ${sysUpNVer}" 
246   echo ""
247   echo "To install: \"pc-updatemanager install fbsd-${sysUpNVer}\""
248   echo ""
249   echo ""
250
251   PATCHFOUND="`expr ${PATCHFOUND} + 1`" 
252}
253
254parse_sa_update_line()
255{
256   local line="$1"
257
258   # Check if this is an update we can install
259   local saVer=`echo $line | cut -d ':' -f 2`
260   local saArch=`echo $line | cut -d ':' -f 3`
261   local saType=`echo $line | cut -d ':' -f 4`
262   local saIdent="`echo $line | cut -d ':' -f 5`"
263
264   # For this version?
265   if [ "$SYSVER" != "$saVer" ] ; then return ; fi
266
267   # For this system arch?
268   if [ "$ARCH" != "$saArch" -a "$saArch" != "noarch" ] ; then return ; fi
269
270   # For this system type?
271   if [ "$SYSTYPE" != "$saType" -a "$saType" != "both" ] ; then return ; fi
272
273   # Already installed?
274   if [ -e "${INSDIR}/${SYSVER}/$saIdent" ] ; then return ; fi
275
276   if [ $PATCHFOUND -eq 0 ] ; then
277      echo ""
278      echo "The following updates are available:"
279      echo "------------------------------------"
280   fi
281   local saDesc="`echo $line | cut -d ':' -f 6`"
282   local saSize=`echo $line | cut -d ':' -f 8`
283   local saDetail="http://trac.pcbsd.org`echo $line | cut -d ':' -f 10`"
284   local saDate="`echo $line | cut -d ':' -f 11`"
285
286   echo "NAME: ${saDesc}" 
287   echo "TYPE: PATCH" 
288   echo "TAG: ${saIdent}"
289   echo "DETAILS: ${saDetail}"
290   echo "DATE: ${saDate}"
291   echo "SIZE: ${saSize}Mb" 
292   echo " "
293   echo "To install: \"pc-updatemanager install ${saIdent}\""
294   echo " "
295
296   PATCHFOUND="`expr ${PATCHFOUND} + 1`" 
297}
298
299start_pcbsd_patch() {
300  local up="$1"
301  local saIdent=""
302  local saVer=""
303  local saArch=""
304  local saPlat=""
305  local saUrl=""
306  local saDesc=""
307  local saCsum=""
308  local patchLine=""
309  if [ -e "${INSDIR}/${SYSVER}/${up}" ]; then
310     rm ${PATCHTMPFILE}
311     exit_err "Patch $up already installed!"
312  fi
313
314  while read line
315  do
316    echo $line | grep -q "^SA:"
317    if [ $? -ne 0 ] ; then continue; fi
318    saIdent="`echo $line | cut -d ':' -f 5`"
319    # Look for the right stand-alone update
320    if [ "$saIdent" != "$up" ] ; then continue ; fi
321    saVer=`echo $line | cut -d ':' -f 2`
322    saArch=`echo $line | cut -d ':' -f 3`
323    saType=`echo $line | cut -d ':' -f 4`
324    # Does this update apply to this system arch?
325    if [ "$saArch" != "$ARCH" -a "$saArch" != "noarch" ] ; then
326       rm ${PATCHTMPFILE}
327       exit_err "Patch $up is not for this system arch type!"
328    fi
329    # For this version?
330    if [ "$saVer" != "$SYSVER" ] ; then
331       rm ${PATCHTMPFILE}
332       exit_err "Patch $up is not for this system version!"
333    fi
334
335    # For this system type?
336    if [ "$SYSTYPE" != "$saType" -a "$saType" != "both" ] ; then
337       rm ${PATCHTMPFILE}
338       exit_err "Patch $up is not for this system type!"
339    fi
340
341    # Got here? We have a patch ready to go
342    patchLine="$line"
343    break
344  done < ${PATCHTMPFILE}
345
346  # Did we find the patch?
347  if [ -z "$patchLine" ] ; then
348     rm ${PATCHTMPFILE}
349     exit_err "Patch $up not found!"
350  fi
351
352  # Get the patch details
353  saVer=`echo $patchLine | cut -d ':' -f 2`
354  saArch=`echo $patchLine | cut -d ':' -f 3`
355  saPlat=`echo $patchLine | cut -d ':' -f 4`
356  saIdent="`echo $patchLine | cut -d ':' -f 5`"
357  saDesc="`echo $patchLine | cut -d ':' -f 6`"
358  saUrl="`echo $patchLine | cut -d ':' -f 7`"
359  saCsum="`echo $patchLine | cut -d ':' -f 9`"
360  local FILENAME="`basename $saUrl`"
361
362  # Start downloading the patch
363  touch ${TRIGGERFILE}
364  echo "DOWNLOADING: ${saIdent}"
365  echo "DOWNLOADING: ${saIdent}" >${TRIGGERFILE}
366
367  # Get the file
368  get_file_from_mirrors "/${saUrl}" "${DOWNLOADDIR}/${FILENAME}" "update"
369  if [ $? -ne 0 ] ; then
370     rm ${PATCHTMPFILE}
371     echo "FAILED: ${saIdent}" >${TRIGGERFILE}
372     exit_err "Failed to download: ${saIdent}"
373  fi
374
375  # Check the sha256 checksum
376  if [ "$saCsum" != "`sha256 -q ${DOWNLOADDIR}/${FILENAME} 2>/dev/null`" ]
377  then
378     # Download MD5 doesn't match! Delete the file
379     rm ${PATCHTMPFILE}
380     rm ${DOWNLOADDIR}/${FILENAME}
381     echo "FAILED: ${saIdent}" >${TRIGGERFILE}
382     exit_err "Failed to download: ${saIdent}"
383  else
384     echo "DOWNLOADFINISHED: ${saIdent}"
385     echo "DOWNLOADFINISHED: ${saIdent}" >${TRIGGERFILE}
386  fi
387
388  echo "INSTALLING: ${saIdent}" >${TRIGGERFILE}
389  sleep 1
390
391  PATCHTMPDIR="`mktemp -d ${DOWNLOADDIR}/patchInstallXXXXX`"
392  rc_halt "tar xvJf ${DOWNLOADDIR}/${FILENAME} -C ${PATCHTMPDIR}" 2>/dev/null
393  PATCHDIR="$PATCHTMPDIR" ; export PATCHDIR
394  cd ${PATCHTMPDIR}
395  sh update.sh
396  if [ $? -eq 0 ]; then
397     touch "${INSDIR}/${SYSVER}/${saIdent}"
398     echo "INSTALLFINISHED: ${saIdent}"
399     echo "INSTALLFINISHED: ${saIdent}" >${TRIGGERFILE}
400  else
401     rm ${PATCHTMPFILE}
402     rm -rf ${PATCHTMPDIR}
403     rm ${DOWNLOADDIR}/${FILENAME}
404     echo "INSTALLFAILED: ${saIdent}" >${TRIGGERFILE}
405     exit_err "INSTALLFAILED: ${saIdent}"
406  fi
407
408  rc_halt "rm -rf ${PATCHTMPDIR}"
409  rc_halt "rm ${DOWNLOADDIR}/${FILENAME}"
410}
411
412rollback_update()
413{
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  # Set some status variables
423  PKGSTATUS=0
424  USERPKGSTATUS=0
425  PBISTATUS=0
426  FREEBSDSTATUS=0
427  GRUBSTATUS=0
428  METASTATUS=0
429
430  # Clear any old summary files
431  rm /root/.failed-pkgs 2>/dev/null
432  rm /root/.failed-meta 2>/dev/null
433  rm /root/.failed-pbi 2>/dev/null
434  rm /root/.failed-freebsd 2>/dev/null
435  rm /root/.failed-grub 2>/dev/null
436
437  # Is this a desktop or server?
438  if [ -e "/usr/local/bin/pc-softwaremanager" ] ; then
439     SYSTYPE="desktop"
440  else
441     SYSTYPE="server"
442  fi
443
444  # Lets Mount the image image now
445  clear
446  MD=`mdconfig -a -t vnode -f ${ISOTMP}` ; export MD
447  if [ ! -d "/mnt/iso-update" ] ; then mkdir -p "/mnt/iso-update"; fi
448  mount -t cd9660 /dev/$MD /mnt/iso-update
449  if [ $? -ne 0 ] ; then
450     # This should never happen, but just in case the user removed the ISO
451     # file or something causes mdconfig / mount to fail, lets roll back
452     rm /var/.freebsd-update-finish
453     rm -rf /boot/kernel
454     mv /boot/kernel.preUpgrade /boot/kernel
455
456     echo "ERROR: Could not mount ${ISOTMP}... Did the file get removed?"
457     echo "The update will now be canceled. Please try again later."
458     echo "Press ENTER to continue"
459     read tmp
460     reboot
461     exit 1
462  fi
463
464  # Remove all PBIs
465  clear
466  echo "Removing PBIs..."
467  while read line
468  do
469    pbi=`echo $line | cut -d ' ' -f 1`
470    user=`echo $line | cut -d ' ' -f 2`
471
472    echo "Removing: ${pbi}"
473    if [ "$user" != "root" ] ; then
474      su $user -c "pbi_icon del-desktop $pbi" >/dev/null 2>/dev/null
475      su $user -c "pbi_icon del-menu $pbi" >/dev/null 2>/dev/null
476      su $user -c "pbi_icon del-mime $pbi" >/dev/null 2>/dev/null
477      su $user -c "pbi_icon del-pathlnk $pbi" >/dev/null 2>/dev/null
478      su $user -c "pbi_delete $pbi" >/dev/null 2>/dev/null
479      result=$?
480    else
481      pbi_icon del-desktop $pbi >/dev/null 2>/dev/null
482      pbi_icon del-menu $pbi >/dev/null 2>/dev/null
483      pbi_icon del-mime $pbi >/dev/null 2>/dev/null
484      pbi_icon del-pathlnk $pbi >/dev/null 2>/dev/null
485      pbi_delete $pbi >/dev/null 2>/dev/null
486      result=$?
487    fi
488    if [ $result -ne 0 ] ; then
489       echo "Warning: Failed to remove: $pbi"
490       sleep 5
491    fi
492  done < /root/pcbsd-pbi-before-update
493  echo "Done!"
494
495  # Start the package cleanup process
496  clear
497  echo "Removing packages...This may take a while..."
498  pkg delete -afy
499  if [ $? -ne 0 ] ; then
500     echo "Warning: Failed to remove all packages"
501     sleep 5
502  fi
503  echo "Done!"
504
505
506  # Setup temp logfile
507  if [ -e "/root/.failed-freebsd" ] ; then rm /root/.failed-freebsd; fi
508  clear
509
510  # We need to be sure and removal the immutable flags
511  echo "Preparing filesystem..."
512  chflags -R noschg /
513
514  distFiles="base doc games lib32"
515  for i in $distFiles
516  do
517    echo -e "Updating world: $i...\c"
518    tar -xvp -f /mnt/iso-update/dist/${i}.txz --exclude ./proc --exclude ./etc/ -C / >> /tmp/.extract.$$ 2>> /tmp/.extract.$$
519    if [ $? -ne 0 ] ; then
520       FREEBSDSTATUS=1
521       echo -e "Warning!"
522       cat /tmp/.extract.$$ >> /root/.failed-freebsd
523       sleep 5
524    else
525       echo -e "Done"
526    fi
527  done
528  rm /tmp/.extract.$$
529
530  # Now time to do all the etcmerge stuff
531  TMPETC="/root/etc-update"
532  if [ -d "$TMPETC" ] ; then rm -rf $TMPETC ; fi
533  mkdir -p $TMPETC
534
535  tar -xvp -f /mnt/iso-update/dist/base.txz -C ${TMPETC} ./etc >/dev/null 2>/dev/null
536  mv ${TMPETC}/etc ${TMPETC}/etc-new
537
538  # Can nuke these two, will be re-generated during etcmerge
539  rm ${TMPETC}/etc-new/spwd.db
540  rm ${TMPETC}/etc-new/pwd.db
541
542  echo "Merging /etc..."
543  /sbin/etcmerge -d ${TMPETC} init  >> /root/.failed-freebsd 2>> /root/.failed-freebsd
544  cd $TMPETC
545
546  # Lets copy the files we always want to clobber
547  for i in ${ETCCLOBBER}
548  do
549     # If this file existed on system, save copy of old version
550     if [ -e "/etc/$i" ] ; then
551        cp /etc/${i} etc-merged/${i}.preUpgrade
552     fi
553     # Overwrite the new version
554     if [ -e "etc-merged/${i}" ] ; then
555        cp etc-new/${i} etc-merged/${i}
556     fi
557  done
558
559  # Lets resolve the conflicts found, use the *new* strings by default
560  # Copy the old file to /etc/<file>.preUpgrade
561  for i in $(cat *.conflicts 2> /dev/null); do
562     if [ ! -e "etc-merged/${i}" ] ; then continue ; fi
563     if egrep -q '^(<<<<<<< |=======$|>>>>>>> )' etc-merged/$i; then
564        cp /etc/${i} etc-merged/${i}.preUpgrade
565
566        # Lets parse out the old, bring in new changes
567        rem=0
568        while read line
569        do
570          echo "$line" | egrep -q '^<<<<<<< '
571          if [ $? -eq 0 ] ; then rem=1 ; continue ; fi
572
573          echo "$line" | egrep -q '^=======$'
574          if [ $? -eq 0 ] ; then rem=0 ; continue ; fi
575
576          echo "$line" | egrep -q '^>>>>>>> '
577          if [ $? -eq 0 ] ; then rem=0 ; continue ; fi
578
579          if [ $rem -eq 0 ] ; then echo "$line" >> etc-merged/${i}.new ; fi
580        done < etc-merged/${i}
581
582        # Copy the fixed file
583        cp -p etc-merged/${i}.new etc-merged/${i}
584        rm etc-merged/${i}.new
585     fi
586  done
587
588  # Now install the merged /etc/ stuff
589  cd $TMPETC
590  /sbin/etcmerge -d ${TMPETC} install >> /root/.failed-freebsd 2>>/root/.failed-freebsd
591  if [ $? -ne 0 ] ; then
592     FREEBSDSTATUS=1
593     echo "Warning: Failed etcmerge install..."
594     sleep 5
595  fi
596
597  # Save the etc-new directory as the old one now
598  rm -rf /var/db/etc
599  cp -r etc-new /var/db/etc
600
601  # Cleanup
602  rm -rf ${TMPETC}
603  rm /sbin/etcmerge
604
605  clear
606
607  # Remove this, causes all kinds of havok during the update
608  if [ -e "/etc/pkg/FreeBSD.conf" ] ; then
609    mv /etc/pkg/FreeBSD.conf /etc/pkg/FreeBSD.conf.dist
610  fi
611
612  clear
613  echo "Starting networking..."
614  echo ""
615  # Enable networking
616  /etc/rc.d/devfs restart
617  /etc/rc.d/netif restart
618  /etc/rc.d/devd start
619  /etc/rc.d/wpa_supplicant start
620  /etc/rc.d/dhclient start
621  sleep 8
622  clear
623
624  # This is super-lame, we can't just remove all packages *except* pkgng, so we have to re-bootstrap
625  # Lets extract the new pkgng we fetched earlier
626  echo "Boot-Strapping PKGNG..."
627  if [ ! -d "/usr/local/etc/pkg/fingerprints/pcbsd/revoked" ] ; then
628     mkdir "/usr/local/etc/pkg/fingerprints/pcbsd/revoked"
629  fi
630  cd /mnt/iso-update/dist/packages/All
631  pkgFile=`ls pkg-[1-9]*`
632  tar xvpf $pkgFile -C / /usr/local/sbin/pkg-static >/dev/null 2>/dev/null
633  /usr/local/sbin/pkg-static add -f $pkgFile
634  if [ $? -ne 0 ] ; then
635     echo "Warning: Failed to re-install PKGNG..."
636     echo "Press ENTER to continue"
637     read tmp
638  fi
639
640
641  # Lets create our fake pcbsd.conf file
642  echo "pcbsd: {
643  url: \"file:///mnt/iso-update/dist/packages\",
644  enabled: true
645}" >  /usr/local/etc/pkg/repos/pcbsd.conf
646
647  # Now update pkgng
648  pkg-static update -f
649  if [ $? -ne 0 ] ; then
650     echo "Failed updating pkgng repo..."
651     sleep 5
652  fi
653
654  # Now lets begin re-installing packages
655  if [ "$SYSTYPE" = "desktop" ] ; then
656    echo "Installing desktop packages..."
657    pkg-static install -y pcbsd-base
658    if [ $? -ne 0 ] ; then
659       PKGSTATUS=1
660       echo "pcbsd-base" >> /root/.failed-pkgs
661       echo "Warning: Failed to install pcbsd-base!"
662       echo "You will need to manually install this package to re-enable the PC-BSD desktop"
663       echo "Press ENTER to continue"
664       read tmp
665    fi
666    pc-extractoverlay desktop
667  else
668    echo "Installing server packages..."
669    pkg-static install -y trueos-base
670    if [ $? -ne 0 ] ; then
671       PKGSTATUS=1
672       echo "trueos-base" >> /root/.failed-pkgs
673       echo "Warning: Failed to install trueos-base!"
674       echo "You will need to manually install this package for the TrueOS utils."
675       echo "Press ENTER to continue"
676       read tmp
677    fi
678    pc-extractoverlay server
679  fi
680
681  # Start pbid, so that by the time meta-packages are done, we have a full index
682  rm /var/db/pbi/index/*
683  /usr/local/etc/rc.d/pbid start >/dev/null 2>/dev/null
684
685  clear
686  echo "Re-Installing meta-packages"
687  while read newmeta
688  do
689    pkgDeps="`pc-metapkgmanager pkgdeps $newmeta`"
690    for i in $pkgDeps
691    do
692      pkg-static info -e ${i}
693      if [ $? -eq 0 ] ; then continue ; fi
694
695      pkg-static install -y ${i}
696      if [ $? -ne 0 ] ; then
697         METASTATUS=1
698         echo "$newmeta" >> /root/.failed-meta
699         echo "Warning: Failed to re-install meta-package: $newmeta (${i})"
700         echo "Press ENTER to continue"
701         read tmp
702      fi
703    done
704  done < /root/pcbsd-meta-before-update
705
706  # Now lets try re-installing any user-installed packages
707  clear
708  echo "Re-Installing user-installed packages"
709  while read userpkg
710  do
711    # Make sure package isn't already installed
712    pkg-static info -e ${userpkg}
713    if [ $? -eq 0 ] ; then continue ; fi
714
715    # Nope, lets try to load it now
716    pkg-static install -y ${userpkg}
717    if [ $? -ne 0 ] ; then
718       USERPKGSTATUS=1
719       echo "${userpkg}" >> /root/.failed-user-pkgs
720    fi
721  done < /root/pcbsd-userpkgs-before-update
722
723  cd /
724
725  # Now lets do the overlay extraction (This cleans up the fake pcbsd.conf)
726  pc-extractoverlay ports
727
728  # Now lets re-load any PBI files
729  clear
730  echo "Re-Installing PBIs"
731  while read line
732  do
733    newpbi=`echo $line | cut -d ' ' -f 1`
734    user=`echo $line | cut -d ' ' -f 2`
735    shortpbi="`echo $newpbi | rev | cut -d '-' -f 3- | rev`"
736
737    # Re-install the PBI as user or root depending on how it was installed
738    if [ "$user" != "root" ] ; then
739      su $user -c "pbi_add -r $shortpbi"
740      result=$?
741    else
742      pbi_add -r $shortpbi
743      result=$?
744    fi
745    if [ $result -ne 0 ] ; then
746       PBISTATUS=1
747       echo "$shortpbi" >> /root/.failed-pbi
748       echo "Warning: Failed to re-install PBI: $shortpbi"
749       echo "Press ENTER to continue"
750       read tmp
751    else
752       # Re-add menu / desktop stuff
753       if [ "$user" != "root" ] ; then
754         su $user -c "pbi_icon add-pathlnk $shortpbi" >/dev/null 2>/dev/null
755         su $user -c "pbi_icon add-desktop $shortpbi" >/dev/null 2>/dev/null
756         su $user -c "pbi_icon add-menu $shortpbi" >/dev/null 2>/dev/null
757         su $user -c "pbi_icon add-mime $shortpbi" >/dev/null 2>/dev/null
758       else
759         pbi_icon add-pathlnk $shortpbi >/dev/null 2>/dev/null
760         pbi_icon add-desktop $shortpbi >/dev/null 2>/dev/null
761         pbi_icon add-menu $shortpbi >/dev/null 2>/dev/null
762         pbi_icon add-mime $shortpbi >/dev/null 2>/dev/null
763       fi
764    fi
765  done < /root/pcbsd-pbi-before-update
766
767  # Last but not least, lets make sure to re-stamp GRUB in case any loader.conf.pcbsd modules changed
768  grub-mkconfig -o /boot/grub/grub.cfg | tee /root/.failed-grub
769  if [ $? -ne 0 ] ; then
770     GRUBSTATUS=1
771     echo "Warning: Failed running 'grub-mkconfig -o /boot/grub/grub.cfg'"
772     echo "Press ENTER to continue"
773     read tmp
774  fi
775
776
777  # Now lets show a summary
778  clear
779
780  echo "PC-BSD `uname -r` Upgrade Summary - (`date`)" > /root/pcbsd-update-summary
781  echo "-----------------------------------------------------" >> /root/pcbsd-update-summary
782
783  if [ $FREEBSDSTATUS -eq 0 ] ; then
784    echo "FreeBSD: The base-system updated successfully!" >> /root/pcbsd-update-summary
785  else
786    echo "FreeBSD: The base-system failed to install:" >> /root/pcbsd-update-summary
787    cat /root/.failed-freebsd >> /root/pcbsd-update-summary
788  fi
789
790  if [ $PKGSTATUS -eq 0 ] ; then
791    echo "    PKG: All packages updated successfully!" >> /root/pcbsd-update-summary
792  else
793    echo "    PKG: The following packages failed to install:" >> /root/pcbsd-update-summary
794    cat /root/.failed-pkgs >> /root/pcbsd-update-summary
795  fi
796
797  if [ $USERPKGSTATUS -eq 0 ] ; then
798    echo "USERPKG: All packages updated successfully!" >> /root/pcbsd-update-summary
799  else
800    echo "USERPKG: The following user-packages failed to install:" >> /root/pcbsd-update-summary
801    cat /root/.failed-user-pkgs >> /root/pcbsd-update-summary
802  fi
803
804  if [ $METASTATUS -eq 0 ] ; then
805    echo "   META: All meta-packages updated successfully!" >> /root/pcbsd-update-summary
806  else
807    echo "   META: The following meta-packages failed to install:" >> /root/pcbsd-update-summary
808    cat /root/.failed-meta >> /root/pcbsd-update-summary
809  fi
810
811  if [ $PBISTATUS -eq 0 ] ; then
812    echo "    PBI: All PBIs updated successfully!" >> /root/pcbsd-update-summary
813  else
814    echo "    PBI: The following PBIs failed to install:" >> /root/pcbsd-update-summary
815    cat /root/.failed-pbi >> /root/pcbsd-update-summary
816  fi
817
818  if [ $GRUBSTATUS -eq 0 ] ; then
819    echo "   GRUB: The GRUB boot-loader menus re-built successfully!" >> /root/pcbsd-update-summary
820  else
821    echo "   GRUB: The GRUB boot-loader menus failed to rebuild:" >> /root/pcbsd-update-summary
822    cat /root/.failed-grub >> /root/pcbsd-update-summary
823  fi
824
825  echo "" >> /root/pcbsd-update-summary
826  echo "Old system information: " >> /root/pcbsd-update-summary
827  echo "/root/pcbsd-pkg-before-update" >> /root/pcbsd-update-summary
828  echo "/root/pcbsd-pbi-before-update" >> /root/pcbsd-update-summary
829  echo "/root/pcbsd-meta-before-update" >> /root/pcbsd-update-summary
830
831  echo "" >> /root/pcbsd-update-summary
832
833  echo "This summary location: /root/pcbsd-update-summary" >> /root/pcbsd-update-summary
834
835  # Clear any old summary files
836  rm /root/.failed-pkgs 2>/dev/null
837  rm /root/.failed-user-pkgs 2>/dev/null
838  rm /root/.failed-meta 2>/dev/null
839  rm /root/.failed-pbi 2>/dev/null
840  rm /root/.failed-grub 2>/dev/null
841  rm /root/.failed-freebsd 2>/dev/null
842
843  more /root/pcbsd-update-summary
844
845  # Cleanup time
846  cd /
847  umount /mnt/iso-update 2>/dev/null
848  mdconfig -d -u $MD 2>/dev/null
849  rm ${ISOTMP} 2>/dev/null
850
851  echo "Press ENTER to reboot"
852  read tmp
853
854  exit 0
855}
856
857# Simple recursive loop to go through and get a list of all package deps for a top level package
858get_dep_origins()
859{
860  grep -q "^$1\$" "${2}.checked" 2>/dev/null
861  if [ $? -eq 0 ] ; then return; fi
862  echo "$1" >> ${2}.checked
863
864  local i
865  for i in `pkg query '%do' ${1}`
866  do
867     echo "$i" >> ${2}
868     get_dep_origins "$i" "$2"
869  done
870}
871
872start_major_fbsd_update() {
873  local rel=`echo $1 | sed 's|fbsd-||g'`
874
875  # Do the disk-space check
876  rZpool="`mount | grep 'on / ' | awk '{print $1}' | cut -d '/' -f 1`"
877  poolFree="`zpool list -H -o free $rZpool`"
878
879  # is space in GB?
880  echo "$poolFree" | grep -q "G"
881  if [ $? -eq 0 ] ; then
882     poolFree="`echo $poolFree | sed 's|G||g'`"
883     if [ $poolFree -lt 10 ] ; then
884        exit_err "Need at least 20GB of free space to continue"
885     fi
886  fi
887
888  # Is space in MB?
889  echo "$poolFree" | grep -q "M"
890  if [ $? -eq 0 ] ; then
891     poolFree="`echo $poolFree | sed 's|M||g'`"
892     if [ $poolFree -lt 10000 ] ; then
893        exit_err "Need at least 20GB of free space to continue"
894     fi
895  fi
896
897  # Grab the ISO file from the mirrors
898  get_file_from_mirrors "/$rel/$ARCH/latest.iso" "$ISOTMP" "iso"
899  get_file_from_mirrors "/$rel/$ARCH/latest.iso.md5" "${ISOTMP}.md5" "iso"
900
901  # Verify the ISO file is good for an update
902  verify_iso_data
903
904  # Lets auto-create a new boot-environment before updating
905  if [ -z "$NOBEADM" ] ; then
906    beadm create ${SYSVER}-beforeUpdate
907    if [ $? -ne 0 ] ; then
908       echo "WARNING: Unable to create a new boot-enviroment!"
909       sleep 10
910    fi
911  fi
912
913  # Now lets build some lists of what is present on this system now
914  pkg info > /root/pcbsd-pkg-before-update
915  pbi_info > /root/pcbsd-pbi-before-update.tmp
916  pc-metapkgmanager list-installed > /root/pcbsd-meta-before-update
917
918  # Figure out which user had installed which PBIs and save this list
919  while read line
920  do
921     insBy=`pbi_info -v $line | grep "InstalledBy:" | cut -d ' ' -f 2`
922     if [ -z "$insBy" ] ; then
923        echo "Unable to get UserName for installed PBI: $line"
924        continue
925     fi
926     echo "$line $insBy" >> /root/pcbsd-pbi-before-update
927  done < /root/pcbsd-pbi-before-update.tmp
928  touch /root/pcbsd-pbi-before-update
929  rm /root/pcbsd-pbi-before-update.tmp
930
931  # Figure out a list of user-installed packages to try and re-install on the updated system
932  echo "Building a list of user-installed packages... Please wait..."
933  local mPkgFile="/tmp/.mPkgDList.$$"
934  local mOriginFile="/tmp/.mPkgOList.$$"
935  rm $mPkgFile 2>/dev/null
936  echo "ports-mgmt/pkg" >> ${mPkgFile}
937  if [ -e "/usr/local/bin/pc-softwaremanager" ] ; then
938     echo "pcbsd-base" >> ${mPkgFile}
939  else
940     echo "trueos-base" >> ${mPkgFile}
941  fi
942  while read mpkg
943  do
944     pc-metapkgmanager pkgdeps $mpkg >/dev/null 2>/dev/null
945     if [ $? -ne 0 ] ; then continue ; fi
946     pc-metapkgmanager pkgdeps $mpkg >> $mPkgFile
947  done < /root/pcbsd-meta-before-update
948
949  # Now sort out the list of depends for our meta-pkgs
950  while read mpkg
951  do
952     #echo "Finding package dependencies of ${mpkg}.."
953     pkg query '%o' $mpkg >> ${mOriginFile}
954     get_dep_origins "$mpkg" "$mOriginFile"
955  done < ${mPkgFile}
956  rm ${mPkgFile}
957
958  echo "Building list of user-installed packages..."
959  # Sort the origins file
960  cat ${mOriginFile} | sort | uniq > ${mOriginFile}.new
961  mv ${mOriginFile}.new ${mOriginFile}
962
963  # Build a list of *all* package origins
964  pkg query -a '%o' | sort > ${mOriginFile}.system
965
966  # Save a list of packages that the user installed from elsewhere than our meta-packages
967  diff ${mOriginFile} ${mOriginFile}.system | grep '>' | awk '{print $2}' > /root/pcbsd-userpkgs-before-update
968  rm ${mOriginFile}.system
969  rm ${mOriginFile}
970
971  # Now install the updated kernel
972  if [ -d "/boot/kernel.preUpgrade" ] ; then
973     rm -rf /boot/kernel.preUpgrade
974  fi
975  mv /boot/kernel /boot/kernel.preUpgrade
976
977  echo "Updating kernel..."
978  tar xf /mnt/iso-update/dist/kernel.txz -C / 2>/dev/null >/dev/null
979  if [ $? -ne 0 ] ; then
980       rm -rf /boot/kernel
981       mv /boot/kernel.preUpgrade /boot/kernel
982       umount /mnt/iso-update
983       mdconfig -d -u $MD
984       rollback_update
985       exit_err "Kernel update failed! (Old kernel restored)"
986  fi
987
988  # Unmount / cleanup ISO
989  umount /mnt/iso-update
990  mdconfig -d -u $MD
991
992  # Touch our marker that we will need to reboot before doing other updates
993  touch /var/.freebsd-update-finish
994  touch /tmp/.fbsdup-reboot
995
996  echo "Major update staged!"
997  echo "Please reboot the system for the updater to continue."
998  exit 0
999}
1000
1001## Do a fetch of a pristine /etc directory for the three way etcmerge
1002fetch_clean_etc()
1003{
1004  echo "Fetching pristine /etc for merging..."
1005  baseTmp="/usr/local/tmp/base.txz.$$"
1006  get_file_from_mirrors "/$SYSVER/$ARCH/dist/base.txz" "$baseTmp" "iso"
1007  if [ $? -ne 0 ] ; then return 1 ; fi
1008
1009  rm -rf "/var/db/etc/"
1010  echo "Extracting pristine /etc for merging..."
1011  tar xvpf ${baseTmp} -C /var/db ./etc >/dev/null 2>/dev/null
1012  local err=$?
1013  rm ${baseTmp}
1014  return $err
1015}
1016
1017## Verify that ISO is suitable for upgrading with
1018verify_iso_data()
1019{
1020  if [ ! -e "${ISOTMP}" ] ; then
1021     rollback_update
1022     exit_err "ISO fails checksum..."
1023  fi
1024
1025  # Check that the MD5 matches
1026  echo "Checking ISO checksum..."
1027  if [ "`md5 -q $ISOTMP`" != "`cat ${ISOTMP}.md5`" ] ; then
1028     rollback_update
1029     exit_err "ISO fails checksum..."
1030  fi
1031
1032  # Now we need to "mount" the ISO, check its contents
1033  MD=`mdconfig -a -t vnode -f ${ISOTMP}` ; export MD
1034  if [ ! -d "/mnt/iso-update" ] ; then mkdir -p "/mnt/iso-update"; fi
1035  mount -t cd9660 /dev/$MD /mnt/iso-update
1036  if [ $? -ne 0 ] ; then
1037     mdconfig -d -u $MD
1038     rollback_update
1039     exit_err "ISO failed mount..."
1040  fi
1041
1042  # Now we are mounted, verify the dist files exist and can be read
1043  distFiles="base doc games kernel lib32"
1044  for i in $distFiles
1045  do
1046     if [ ! -e "/mnt/iso-update/dist/${i}.txz" ] ; then
1047       umount /mnt/iso-update
1048       mdconfig -d -u $MD
1049       rollback_update
1050       exit_err "ISO missing distfile: /dist/${i}.txz"
1051     fi
1052
1053     echo "Verifying distfile: ${i}.txz"
1054     tar tf "/mnt/iso-update/dist/${i}.txz" >/dev/null 2>/dev/null
1055     if [ $? -ne 0 ] ; then
1056       umount /mnt/iso-update
1057       mdconfig -d -u $MD
1058       rollback_update
1059       exit_err "ISO corrupt distfile: /dist/${i}.txz"
1060     fi
1061  done
1062
1063  # Lets fetch a pristine /etc for merging, don't trust an old one in /var/db
1064  fetch_clean_etc
1065  if [ $? -ne 0 ] ; then
1066     umount /mnt/iso-update
1067     mdconfig -d -u $MD
1068     rollback_update
1069     exit_err "Missing etcmerge directory: /var/db/etc"
1070  fi
1071
1072  # Make sure we have /sbin/etcmerge copied over
1073  cp ${PROGDIR}/etcmerge /sbin/etcmerge
1074  if [ $? -ne 0 ] ; then
1075       umount /mnt/iso-update
1076       mdconfig -d -u $MD
1077       rollback_update
1078       exit_err "Failed setting up etcmerge..."
1079  fi
1080  chmod 755 /sbin/etcmerge
1081}
1082
1083do_install() {
1084        # Make sure we are root and have specified updates to install
1085        if [ `id -u` != "0" ] ; then exit_err "Must be run as root!" ; fi
1086        if [ -z "$1" ] ; then exit_err "No updates specified to install!"; fi
1087
1088        # Begin to update the selected items
1089        for up in `echo $1 | sed 's|,| |g'`
1090        do
1091          # If this is a major update, start it up!
1092          echo $up | grep -q "^fbsd-" 
1093          if [ $? -eq 0 ] ; then
1094            start_major_fbsd_update "$up"
1095            exit 0
1096          elif [ "$up" == "pkg-updates" ] ; then
1097            # Check if this is a pkg-update request or regular patch
1098            start_pkg_updates
1099            continue
1100          else
1101            # Doing regular pcbsd patch
1102            start_pcbsd_patch "${up}"
1103            continue
1104          fi
1105
1106        done 
1107
1108        # All Finished!
1109        rm ${PATCHTMPFILE}
1110        exit 0
1111}
1112
1113is_net_up() {
1114        ping -c 1 www.pcbsd.org >/dev/null 2>/dev/null
1115        return $?
1116}
1117
1118checkup_pkgs() {
1119
1120  # Update the repo DB
1121  rc_halt "pkg-static update"
1122
1123  pkg-static upgrade -n >/tmp/.pkgData.$$
1124  if [ $? -ne 0 ] ; then
1125     echo "Error checking for package updates..."
1126     exit 1
1127  fi
1128
1129  # Check fr updates now
1130  grep -q "Upgrading" /tmp/.pkgData.$$
1131  if [ $? -ne 0 ] ; then
1132     echo "All packages are up to date!"
1133     rm /tmp/.pkgData.$$
1134     return 0
1135  fi
1136
1137  # Display the pkg update data
1138  cat /tmp/.pkgData.$$
1139  rm /tmp/.pkgData.$$
1140  echo ""
1141  echo "To start the upgrade run \"${0} pkgupdate\""
1142  return 0
1143}
1144
1145update_pkgs()
1146{
1147  if [ -n "$1" -a "$1" != "-f" ] ; then
1148     exit_err "Invalid option $1 passed"
1149  fi
1150
1151  # Update the DB first
1152  pkg-static update -f
1153
1154  # Look for conflicts
1155  check_pkg_conflicts "upgrade -FUy $1"
1156  if [ $? -ne 0 ] ; then
1157     exit 1
1158  fi
1159
1160  # Start the package update!
1161  pkg-static upgrade -U -y ${1}
1162  result=$?
1163
1164  # Lets now re-extract our overlay data to grab any adjusted port files
1165  pc-extractoverlay ports >/dev/null 2>/dev/null
1166
1167  # Now re-extract overlay files to ensure we do file fixing / massaging
1168  if [ "$SYSTYPE" = "PCBSD" ]; then
1169    pc-extractoverlay desktop
1170  fi
1171  if [ "$SYSTYPE" = "TRUEOS" ]; then
1172    pc-extractoverlay server
1173  fi
1174
1175  if [ $result -ne 0 ] ; then
1176     echo "Failed updating..."
1177  fi
1178  exit $result
1179}
1180
1181if [ "`id -u`" = "0" ] ; then
1182  # Make the installed directory for this version
1183  if [ ! -d "${INSDIR}/${SYSVER}" ] ; then mkdir -p ${INSDIR}/${SYSVER} ; fi
1184
1185  # Make the ignore directory for this version
1186  if [ ! -d "${IGNDIR}/${SYSVER}" ] ; then mkdir -p ${IGNDIR}/${SYSVER} ; fi
1187fi
1188
1189case $1 in
1190           check) get_update_file
1191                  do_check ;;
1192        branches) get_update_file
1193                  list_branches ;;
1194        chbranch) get_update_file
1195                  do_branch "${2}" ;;
1196        pkgcheck) checkup_pkgs ;;
1197       pkgupdate) update_pkgs "$2" ;;
1198finish-major-update) finish_major_fbsd_update ;;
1199         install) get_update_file
1200                  do_install "${2}" ;;
1201        *) show_usage ;;
1202esac
1203
1204exit 0
Note: See TracBrowser for help on using the repository browser.