source: src-sh/libsh/functions.sh @ cdfb605

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

Fix some issues with conflicts detection during package upgrades, as well as
using EVENT_PIPE only during the download phase. This will need to be backported
to the current PRODUCTION set before issuing new updates.

  • Property mode set to 100755
File size: 15.2 KB
Line 
1#!/bin/sh
2# Functions we can source for pc-bsd scripts
3# Author: Kris Moore
4# Copyright: 2012
5# License: BSD
6##############################################################
7
8PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
9
10get_mirror() {
11
12  # Check if we already looked up a mirror we can keep using
13  if [ -n "$CACHED_PCBSD_MIRROR" ] ; then
14     VAL="$CACHED_PCBSD_MIRROR"
15     export VAL
16     return
17  fi
18
19  # Set the mirror URL
20  VAL="`cat ${PCBSD_ETCCONF} 2>/dev/null | grep 'PCBSD_MIRROR: ' | sed 's|PCBSD_MIRROR: ||g'`"
21  if [ -n "$VAL" ] ; then
22     echo "Using mirror: $VAL"
23     CACHED_PCBSD_MIRROR="$VAL"
24     export VAL CACHED_PCBSD_MIRROR
25     return
26  fi
27
28  echo "Getting regional mirror..."
29  . /etc/profile
30
31  # No URL? Lets get one from the master server
32  local mFile="${HOME}/.mirrorUrl.$$"
33  touch $mFile
34  fetch -o $mFile http://getmirror.pcbsd.org >/dev/null 2>/dev/null
35  VAL="`cat $mFile | grep 'URL: ' | sed 's|URL: ||g'`"
36  rm $mFile
37  if [ -n "$VAL" ] ; then
38     echo "Using mirror: $VAL"
39     CACHED_PCBSD_MIRROR="$VAL"
40     export VAL CACHED_PCBSD_MIRROR
41     return
42  fi
43
44  # Still no mirror? Lets try the PC-BSD FTP server...
45  VAL="ftp://ftp.pcbsd.org/pub/mirror"
46  CACHED_PCBSD_MIRROR="$VAL"
47  export VAL CACHED_PCBSD_MIRROR
48  echo "Using mirror: $VAL"
49  return 
50}
51
52# Function which returns the installed list of PC-BSD mirrors for use
53# with the fetch command
54# Will return just a single mirror, if the user has manually specified one
55# in /usr/local/etc/pcbsd.conf
56get_mirror_loc()
57{
58  if [ -z $1 ] ; then
59     exit_err "Need to supply file to grab from mirrors..."
60  fi
61  if [ -z $2 ] ; then
62     exit_err "Need to supply which mirror to fetch from..."
63  fi
64
65  case $2 in
66    pkg) mirrorTag="PKG_MIRROR" 
67         mirrorFile="/usr/local/share/pcbsd/conf/pkg-mirror"
68         ;;
69    pbi) mirrorTag="PBI_MIRROR" 
70         mirrorFile="/usr/local/share/pcbsd/conf/pbi-mirror"
71         ;;
72    iso) mirrorTag="ISO_MIRROR" 
73         mirrorFile="/usr/local/share/pcbsd/conf/iso-mirror"
74         ;;
75  update) mirrorTag="UPDATE_MIRROR" 
76         mirrorFile="/usr/local/share/pcbsd/conf/update-mirror"
77         ;;
78    *) exit_err "Bad mirror type!" ;;
79  esac
80
81  # Set the mirror URL
82  local VAL=`cat ${PCBSD_ETCCONF} 2>/dev/null | grep "^${mirrorTag}:" | sed "s|^${mirrorTag}: ||g"`
83  if [ -n "$VAL" ] ; then
84     echo "${VAL}${1}"
85     return
86  fi
87
88  if [ ! -e "${mirrorFile}" ] ; then
89     exit_err "Missing mirror list: ${mirrorFile}"
90  fi
91
92  # Build the mirror list
93  while read line
94  do
95    VAL="${line}${1}"
96    break
97  done < ${mirrorFile}
98  echo ${VAL}
99}
100
101# Function to download a file from the pcbsd mirrors
102# Arg1 = Remote File URL
103# Arg2 = Where to save file
104get_file_from_mirrors()
105{
106   _rf="${1}"
107   _lf="${2}"
108   _mtype="${3}"
109
110   case $_mtype in
111      iso|pbi|pkg|update) ;;
112      *) exit_err "Fixme! Missing mirror type in get_file_from_mirrors" ;;
113   esac
114
115   # Get any proxy information
116   . /etc/profile
117
118   # Get mirror list
119   local mirrorLoc="$(get_mirror_loc ${_rf} ${_mtype})"
120   mirrorLoc="`echo $mirrorLoc | awk '{print $1}'`"
121
122   # Running from a non GUI?
123   if [ "$GUI_FETCH_PARSING" != "YES" -a "$PBI_FETCH_PARSING" != "YES" -a -z "$PCFETCHGUI" ] ; then
124      fetch -o "${_lf}" ${mirrorLoc}
125      return $?
126   fi
127
128   echo "FETCH: ${_rf}"
129
130   # Doing a front-end download, parse the output of fetch
131   _eFile="/tmp/.fetch-exit.$$"
132   fetch -s ${mirrorLoc} > /tmp/.fetch-size.$$ 2>/dev/null
133   _fSize=`cat /tmp/.fetch-size.$$ 2>/dev/null`
134   _fSize="`expr ${_fSize} / 1024 2>/dev/null`"
135   rm "/tmp/.fetch-size.$$" 2>/dev/null
136   _time=1
137   if [ -z "$_fSize" ] ; then _fSize=0; fi
138
139   ( fetch -o ${_lf} ${mirrorLoc} >/dev/null 2>/dev/null ; echo "$?" > ${_eFile} ) &
140   FETCH_PID=$!
141   while :
142   do
143      if [ -e "${_lf}" ] ; then
144         sync
145         _dSize=`du -k ${_lf} | tr -d '\t' | cut -d '/' -f 1`
146         if [ $(is_num "$_dSize") ] ; then
147            if [ ${_fSize} -lt ${_dSize} ] ; then _dSize="$_fSize" ; fi
148            _kbs=`expr ${_dSize} \/ $_time`
149            echo "SIZE: ${_fSize} DOWNLOADED: ${_dSize} SPEED: ${_kbs} KB/s"
150         fi
151      fi
152
153      # Make sure download isn't finished
154      jobs -l >/tmp/.jobProcess.$$
155      cat /tmp/.jobProcess.$$ | awk '{print $3}' | grep -q ${FETCH_PID}
156      if [ "$?" != "0" ] ; then rm /tmp/.jobProcess.$$ ; break ; fi
157      sleep 1
158      _time=`expr $_time + 1`
159   done
160
161   _err="`cat ${_eFile} 2>/dev/null`"
162   if [ -z "$_err" ] ; then _err="0"; fi
163   rm ${_eFile} 2>/dev/null
164   if [ "$_err" = "0" ]; then echo "FETCHDONE" ; fi
165   unset FETCH_PID
166   return $_err
167
168}
169
170# Function to download a file from remote using fetch
171# Arg1 = Remote File URL
172# Arg2 = Where to save file
173# Arg3 = Number of attempts to make before failing
174get_file() {
175
176        _rf="${1}"
177        _lf="${2}"
178        _ftries=${3}
179        if [ -z "$_ftries" ] ; then _ftries=3; fi
180
181        # Get any proxy information
182        . /etc/profile
183
184        if [ -e "${_lf}" ] ; then
185                echo "Resuming download of: ${_lf}"
186        fi
187
188        if [ "$GUI_FETCH_PARSING" != "YES" -a -z "$PCFETCHGUI" ] ; then
189                fetch -r -o "${_lf}" "${_rf}"
190                _err=$?
191        else
192                echo "FETCH: ${_rf}"
193
194                # Doing a front-end download, parse the output of fetch
195                _eFile="/tmp/.fetch-exit.$$"
196                fetch -s "${_rf}" > /tmp/.fetch-size.$$ 2>/dev/null
197                _fSize=`cat /tmp/.fetch-size.$$ 2>/dev/null`
198                _fSize="`expr ${_fSize} / 1024 2>/dev/null`"
199                rm "/tmp/.fetch-size.$$" 2>/dev/null
200                _time=1
201                if [ -z "$_fSize" ] ; then _fSize=0; fi
202
203                ( fetch -r -o "${_lf}" "${_rf}" >/dev/null 2>/dev/null ; echo "$?" > ${_eFile} ) &
204                FETCH_PID=`ps -auwwwx | grep -v grep | grep "fetch -r -o ${_lf}" | awk '{print $2}'`
205                while :
206                do
207                        if [ -e "${_lf}" ] ; then
208                                _dSize=`du -k ${_lf} | tr -d '\t' | cut -d '/' -f 1`
209                                if [ $(is_num "$_dSize") ] ; then
210                                        if [ ${_fSize} -lt ${_dSize} ] ; then _dSize="$_fSize" ; fi
211                                        _kbs=`expr ${_dSize} \/ $_time`
212                                        echo "SIZE: ${_fSize} DOWNLOADED: ${_dSize} SPEED: ${_kbs} KB/s"
213                                fi
214                        fi
215
216                        # Make sure download isn't finished
217                        ps -p $FETCH_PID >/dev/null 2>/dev/null
218                        if [ "$?" != "0" ] ; then break ; fi
219                        sleep 2
220                        _time=`expr $_time + 2`
221                done
222
223                _err="`cat ${_eFile} 2>/dev/null`"
224                if [ -z "$_err" ] ; then _err="0"; fi
225                rm ${_eFile} 2>/dev/null
226                if [ "$_err" = "0" ]; then echo "FETCHDONE" ; fi
227                unset FETCH_PID
228        fi
229
230        echo ""
231        if [ $_err -ne 0 -a $_ftries -gt 0 ] ; then
232                sleep 30
233                _ftries=`expr $_ftries - 1`
234
235                # Remove the local file if we failed
236                if [ -e "${_lf}" ]; then rm "${_lf}"; fi
237
238                get_file "${_rf}" "${_lf}" $_ftries     
239                _err=$?
240        fi
241        return $_err
242}
243
244# Check if a value is a number
245is_num()
246{
247        expr $1 + 1 2>/dev/null
248        return $?
249}
250
251# Exit with a error message
252exit_err() {
253        if [ -n "${LOGFILE}" ] ; then
254           echo "ERROR: $*" >> ${LOGFILE}
255        fi
256        echo >&2 "ERROR: $*"
257        exit 1
258}
259
260
261### Print an error on STDERR and bail out
262printerror() {
263  exit_err $*
264}
265
266
267# Check if the target directory is on ZFS
268# Arg1 = The dir to check
269# Arg2 = If set to 1, don't dig down to lower level directory
270isDirZFS() {
271  local _chkDir="$1"
272  while :
273  do
274     # Is this dir a ZFS mount
275     mount | grep -w "on $_chkDir " | grep -qw "(zfs," && return 0
276
277     # If this directory is mounted, but NOT ZFS
278     if [ "$2" != "1" ] ; then
279       mount | grep -qw "on $_chkDir " && return 1
280     fi
281     
282     # Quit if not walking down
283     if [ "$2" = "1" ] ; then return 1 ; fi
284 
285     if [ "$_chkDir" = "/" ] ; then break ; fi
286     _chkDir=`dirname $_chkDir`
287  done
288 
289  return 1
290}
291
292# Gets the mount-point of a particular zpool / dataset
293# Arg1 = zpool to check
294getZFSMount() {
295  local zpool="$1"
296  local mnt=`mount | grep "^${zpool} on" | grep "(zfs," | awk '{print $3}'`
297  if [ -n "$mnt" ] ; then
298     echo "$mnt"
299     return 0
300  fi
301  return 1
302}
303
304# Get the ZFS dataset of a particular directory
305getZFSDataset() {
306  local _chkDir="$1"
307  while :
308  do
309    local zData=`mount | grep " on ${_chkDir} " | grep "(zfs," | awk '{print $1}'`
310    if [ -n "$zData" ] ; then
311       echo "$zData"
312       return 0
313    fi
314    if [ "$2" != "rec" ] ; then return 1 ; fi
315    if [ "$_chkDir" = "/" ] ; then return 1 ; fi
316    _chkDir=`dirname $_chkDir`
317  done
318  return 1
319}
320
321# Get the ZFS tank name for a directory
322# Arg1 = Directory to check
323getZFSTank() {
324  local _chkDir="$1"
325
326  _chkdir=${_chkDir%/}
327  while :
328  do
329     zpath=`zfs list | awk -v path="${_chkDir}" '$5 == path { print $1 }'`
330     if [ -n "${zpath}" ] ; then
331        echo $zpath | cut -f1 -d '/'
332        return 0
333     fi
334
335     if [ "$_chkDir" = "/" ] ; then return 1 ; fi
336     _chkDir=`dirname $_chkDir`
337  done
338
339  return 1
340}
341
342# Get the mountpoint for a ZFS name
343# Arg1 = name
344getZFSMountpoint() {
345   local _chkName="${1}"
346   if [ -z "${_chkName}" ]; then return 1 ; fi
347
348   zfs list "${_chkName}" | tail -1 | awk '{ print $5 }'
349}
350
351# Get the ZFS relative path for a path
352# Arg1 = Path
353getZFSRelativePath() {
354   local _chkDir="${1}"
355   local _tank=`getZFSTank "$_chkDir"`
356   local _mp=`getZFSMountpoint "${_tank}"`
357
358   if [ -z "${_tank}" ] ; then return 1 ; fi
359
360   local _name="${_chkDir#${_mp}}"
361
362   # Make sure we have a '/' at the start of dataset
363   if [ "`echo ${_name} | cut -c 1`" != "/" ] ; then _name="/${_name}"; fi
364
365   echo "${_name}"
366   return 0
367}
368
369# Check if an address is IPv6
370isV6() {
371  echo ${1} | grep -q ":"
372  return $?
373}
374   
375# Is a mount point, or any of its parent directories, a symlink?
376is_symlinked_mountpoint()
377{
378        local _dir
379        _dir=$1
380        [ -L "$_dir" ] && return 0
381        [ "$_dir" = "/" ] && return 1
382        is_symlinked_mountpoint `dirname $_dir`
383        return $?
384}
385
386# Function to ask the user to press Return to continue
387rtn()
388{
389  echo -e "Press ENTER to continue\c";
390  read garbage
391};
392
393# Function to check if an IP address passes a basic sanity test
394check_ip()
395{
396  ip="$1"
397 
398  # If this is a V6 address, skip validation for now
399  isV6 "${ip}"
400  if [ $? -eq 0 ] ; then return ; fi
401
402  # Check if we can cut this IP into the right segments
403  SEG="`echo $ip | cut -d '.' -f 1 2>/dev/null`"
404  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
405  if [ "$?" != "0" ]
406  then
407     return 1
408  fi
409  if [ $SEG -gt 255 -o $SEG -lt 0 ]
410  then
411     return 1
412  fi
413 
414  # Second segment
415  SEG="`echo $ip | cut -d '.' -f 2 2>/dev/null`"
416  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
417  if [ "$?" != "0" ]
418  then
419     return 1
420  fi
421  if [ $SEG -gt 255 -o $SEG -lt 0 ]
422  then
423     return 1
424  fi
425
426  # Third segment
427  SEG="`echo $ip | cut -d '.' -f 3 2>/dev/null`"
428  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
429  if [ "$?" != "0" ]
430  then
431     return 1
432  fi
433  if [ $SEG -gt 255 -o $SEG -lt 0 ]
434  then
435     return 1
436  fi
437 
438  # Fourth segment
439  SEG="`echo $ip | cut -d '.' -f 4 2>/dev/null`"
440  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
441  if [ "$?" != "0" ]
442  then
443     return 1
444  fi
445  if [ $SEG -gt 255 -o $SEG -lt 0 ]
446  then
447     return 1
448  fi
449
450  return 0
451};
452
453check_pkg_conflicts()
454{
455
456  # Lets test if we have any conflicts
457  pkg-static ${1} 2>&1| tee /tmp/.pkgConflicts.$$
458
459  cat /tmp/.pkgConflicts.$$ | grep -q "WARNING: locally installed"
460  if [ $? -ne 0 ] ; then rm /tmp/.pkgConflicts.$$ ; return ; fi
461
462 
463  # Found conflicts, suprise suprise, yet another reason I hate packages
464  # Lets start building a list of the old packages we can prompt to remove
465
466  # Nice ugly sed line, sure this can be neater
467  cat /tmp/.pkgConflicts.$$ | grep 'WARNING: locally installed' \
468        | sed 's|.*installed ||g' | sed 's| conflicts.*||g' | sort | uniq \
469        > /tmp/.pkgConflicts.$$.2
470
471  # Check how many conflicts we found
472  found=`wc -l /tmp/.pkgConflicts.$$.2 | awk '{print $1}'`
473  if [ "$found" = "0" ] ; then
474     rm /tmp/.pkgConflicts.$$
475     rm /tmp/.pkgConflicts.$$.2
476     return 0
477  fi
478
479  while read line
480  do
481    cList="$line $cList"
482  done < /tmp/.pkgConflicts.$$.2
483  rm /tmp/.pkgConflicts.$$.2
484  rm /tmp/.pkgConflicts.$$
485
486  if [ "$GUI_FETCH_PARSING" != "YES" -a "$PBI_FETCH_PARSING" != "YES" -a -z "$PCFETCHGUI" ] ; then
487        echo "The following packages will conflict with your pkg command:"
488        echo "-------------------------------------"
489        echo "$cList" | more
490        echo "Do you wish to remove them automatically?"
491        echo -e "Default yes: (y/n)\c"
492        read tmp
493        if [ "$tmp" != "y" -a "$tmp" != "Y" -a -n "$tmp" ] ; then return 1 ; fi
494  else
495        echo "PKGCONFLICTS: $cList"
496        echo "PKGREPLY: /tmp/pkgans.$$"
497        while :
498        do
499          if [ -e "/tmp/pkgans.$$" ] ; then
500            ans=`cat /tmp/pkgans.$$`
501            if [ "$ans" = "yes" ] ; then
502               break
503            else
504               return 1
505            fi
506          fi
507          sleep 3
508        done
509  fi
510
511  # Lets auto-resolve these bad-boys
512  # Right now the logic is pretty simple, you conflict, you die
513  for bPkg in $cList
514  do
515     # Nuked!
516     echo "Removing conflicting package: $bPkg"
517
518     # If EVENT_PIPE is set, unset it, seems to cause some weird crash in pkgng 1.2.3
519     if [ -n "$EVENT_PIPE" ] ; then
520        oEP="$EVENT_PIPE"
521        unset EVENT_PIPE
522     fi
523
524     # Delete the package now
525     pkg delete -q -y -f ${bPkg}
526
527     # Reset EVENT_PIPE if we need to
528     if [ -n "$oEP" ] ; then
529        EVENT_PIPE="$oEP"; export EVENT_PIPE
530        unset oEP
531     fi
532  done
533
534  # Lets test if we still have any conflicts
535  pkg-static ${1} 2>/dev/null >/dev/null
536  if [ $? -eq 0 ] ; then return 0; fi
537
538  # Crapola, we still have conflicts, lets warn and bail
539  echo "ERROR: pkg ${1} is still reporting conflicts... Resolve these manually and try again"
540  return 1
541}
542
543# Run the first boot wizard
544# Should be called from a .xinitrc script, after fluxbox is already running
545run_firstboot()
546{
547  # Is the trigger file set?
548  if [ ! -e "/var/.pcbsd-firstgui" ] ; then return; fi
549
550  # Set all our path variables
551  PATH="/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/bin:/usr/local/sbin"
552  HOME="/root"
553  export PATH HOME
554
555  # Unset the PROGDIR variable
556  PROGDIR=""
557  export PROGDIR
558
559  if [ -e "/root/.xprofile" ] ; then . /root/.xprofile ; fi
560
561  # Figure out which intro video to play
562  res=`xdpyinfo | grep dimensions: | awk "{print $2}"`
563  h=`echo $res | cut -d "x" -f 1`
564  w=`echo $res | cut -d "x" -f 2`
565  h=`expr 100 \* $h`
566  ratio=`expr $h \/ $w | cut -c 1-2`
567  case $ratio in
568    13) mov="PCBSD9_4-3_UXGA.flv";;
569    16) mov="PCBSD9_16-10_WUXGA.flv";;
570    17) mov="PCBSD9_16-9_1080p.flv";;
571     *) mov="PCBSD9_4-3_UXGA.flv";;
572  esac
573
574  # Play the video now
575  # NO Movie for 10, if we end up with one, replace this
576  #mplayer -fs -nomouseinput -zoom /usr/local/share/pcbsd/movies/$mov
577
578  # Setting a language
579  if [ -e "/etc/pcbsd-lang" ] ; then
580    LANG=`cat /etc/pcbsd-lang`
581    export LANG
582  fi
583
584  # Start first-boot wizard
585  /usr/local/bin/pc-firstboot >/var/log/pc-firstbootwiz 2>/var/log/pc-firstbootwiz
586  if [ $? -eq 0 ] ; then
587    rm /var/.pcbsd-firstgui
588  fi
589}
590
591# Run-command, don't halt if command exits with non-0
592rc_nohalt()
593{
594  CMD="$1"
595
596  if [ -z "${CMD}" ] ; then
597    exit_err "Error: missing argument in rc_nohalt()"
598  fi
599
600  ${CMD}
601}
602
603# Run-command, halt if command exits with non-0
604rc_halt()
605{
606  CMD="$@"
607
608  if [ -z "${CMD}" ] ; then
609    exit_err "Error: missing argument in rc_halt()"
610  fi
611
612  ${CMD}
613  STATUS=$?
614  if [ ${STATUS} -ne 0 ] ; then
615    exit_err "Error ${STATUS}: ${CMD}"
616  fi
617}
618
619# Run-command silently, only display / halt if command exits with non-0
620rc_halt_s()
621{
622  CMD="$@"
623
624  if [ -z "${CMD}" ] ; then
625    exit_err "Error: missing argument in rc_halt()"
626  fi
627
628  TMPRCLOG=`mktemp /tmp/.rc_halt.XXXXXX`
629  ${CMD} >${TMPRCLOG} 2>${TMPRCLOG}
630  STATUS=$?
631  if [ ${STATUS} -ne 0 ] ; then
632    cat ${TMPRCLOG}
633    rm ${TMPRCLOG}
634    exit_err "Error ${STATUS}: ${CMD}"
635  fi
636  rm ${TMPRCLOG}
637}
Note: See TracBrowser for help on using the repository browser.