source: src-sh/libsh/functions.sh

Last change on this file was b969f40, checked in by Kris Moore <kris@…>, 7 weeks ago

Remove the old pkgng conflict detection scripts we used to run
on pkgng prior to 1.3.x. The newer pkgng does a much better job
of figuring out and fixing conflicts on the fly during upgrades,
so we can retire this bit of code

  • Property mode set to 100755
File size: 17.3 KB
RevLine 
[1620346]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
[7d497d1]53# with the fetch command
[1620346]54# Will return just a single mirror, if the user has manually specified one
55# in /usr/local/etc/pcbsd.conf
[7d497d1]56get_mirror_loc()
[1620346]57{
58  if [ -z $1 ] ; then
59     exit_err "Need to supply file to grab from mirrors..."
60  fi
[9788f38]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
[1620346]80
81  # Set the mirror URL
[9788f38]82  local VAL=`cat ${PCBSD_ETCCONF} 2>/dev/null | grep "^${mirrorTag}:" | sed "s|^${mirrorTag}: ||g"`
[1620346]83  if [ -n "$VAL" ] ; then
84     echo "${VAL}${1}"
85     return
86  fi
87
[9788f38]88  if [ ! -e "${mirrorFile}" ] ; then
89     exit_err "Missing mirror list: ${mirrorFile}"
[1620346]90  fi
91
92  # Build the mirror list
93  while read line
94  do
[7d497d1]95    VAL="${line}${1}"
96    break
[9788f38]97  done < ${mirrorFile}
[1620346]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}"
[9788f38]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
[1620346]114
115   # Get any proxy information
116   . /etc/profile
117
118   # Get mirror list
[7d497d1]119   local mirrorLoc="$(get_mirror_loc ${_rf} ${_mtype})"
120   mirrorLoc="`echo $mirrorLoc | awk '{print $1}'`"
121
[1620346]122   # Running from a non GUI?
123   if [ "$GUI_FETCH_PARSING" != "YES" -a "$PBI_FETCH_PARSING" != "YES" -a -z "$PCFETCHGUI" ] ; then
[7d497d1]124      fetch -o "${_lf}" ${mirrorLoc}
[1620346]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.$$"
[7d497d1]132   fetch -s ${mirrorLoc} > /tmp/.fetch-size.$$ 2>/dev/null
[1620346]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
[e013d8d2]137   if [ -z "$_fSize" ] ; then _fSize=0; fi
[1620346]138
[7d497d1]139   ( fetch -o ${_lf} ${mirrorLoc} >/dev/null 2>/dev/null ; echo "$?" > ${_eFile} ) &
[6f2048f]140   FETCH_PID=$!
[1620346]141   while :
142   do
143      if [ -e "${_lf}" ] ; then
[7f1df64]144         sync
[1620346]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
[6f2048f]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`
[1620346]159   done
160
[3b0aba7]161   _err="`cat ${_eFile} 2>/dev/null`"
162   if [ -z "$_err" ] ; then _err="0"; fi
[1620346]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
[f967699]201                if [ -z "$_fSize" ] ; then _fSize=0; fi
[1620346]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
[3b0aba7]223                _err="`cat ${_eFile} 2>/dev/null`"
224                if [ -z "$_err" ] ; then _err="0"; fi
[1620346]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}}"
[8afc51a]361
362   # Make sure we have a '/' at the start of dataset
363   if [ "`echo ${_name} | cut -c 1`" != "/" ] ; then _name="/${_name}"; fi
364
[1620346]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};
[4c06449]452
[4950767]453# Run the first boot wizard
454# Should be called from a .xinitrc script, after fluxbox is already running
455run_firstboot()
456{
457  # Is the trigger file set?
458  if [ ! -e "/var/.pcbsd-firstgui" ] ; then return; fi
459
460  # Set all our path variables
461  PATH="/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/bin:/usr/local/sbin"
462  HOME="/root"
463  export PATH HOME
464
465  # Unset the PROGDIR variable
466  PROGDIR=""
467  export PROGDIR
468
469  if [ -e "/root/.xprofile" ] ; then . /root/.xprofile ; fi
470
471  # Figure out which intro video to play
472  res=`xdpyinfo | grep dimensions: | awk "{print $2}"`
473  h=`echo $res | cut -d "x" -f 1`
474  w=`echo $res | cut -d "x" -f 2`
475  h=`expr 100 \* $h`
476  ratio=`expr $h \/ $w | cut -c 1-2`
477  case $ratio in
478    13) mov="PCBSD9_4-3_UXGA.flv";;
479    16) mov="PCBSD9_16-10_WUXGA.flv";;
480    17) mov="PCBSD9_16-9_1080p.flv";;
481     *) mov="PCBSD9_4-3_UXGA.flv";;
482  esac
483
484  # Play the video now
[bd99c30]485  # NO Movie for 10, if we end up with one, replace this
486  #mplayer -fs -nomouseinput -zoom /usr/local/share/pcbsd/movies/$mov
[4950767]487
488  # Setting a language
489  if [ -e "/etc/pcbsd-lang" ] ; then
490    LANG=`cat /etc/pcbsd-lang`
491    export LANG
492  fi
493
494  # Start first-boot wizard
[f967699]495  /usr/local/bin/pc-firstboot >/var/log/pc-firstbootwiz 2>/var/log/pc-firstbootwiz
[4950767]496  if [ $? -eq 0 ] ; then
497    rm /var/.pcbsd-firstgui
498  fi
499}
[65fd04b2]500
501# Run-command, don't halt if command exits with non-0
502rc_nohalt()
503{
504  CMD="$1"
505
506  if [ -z "${CMD}" ] ; then
507    exit_err "Error: missing argument in rc_nohalt()"
508  fi
509
510  ${CMD}
511}
512
513# Run-command, halt if command exits with non-0
514rc_halt()
515{
516  CMD="$@"
517
518  if [ -z "${CMD}" ] ; then
519    exit_err "Error: missing argument in rc_halt()"
520  fi
521
522  ${CMD}
523  STATUS=$?
524  if [ ${STATUS} -ne 0 ] ; then
525    exit_err "Error ${STATUS}: ${CMD}"
526  fi
527}
[26599e2]528
529# Run-command silently, only display / halt if command exits with non-0
530rc_halt_s()
531{
532  CMD="$@"
533
534  if [ -z "${CMD}" ] ; then
535    exit_err "Error: missing argument in rc_halt()"
536  fi
537
538  TMPRCLOG=`mktemp /tmp/.rc_halt.XXXXXX`
539  ${CMD} >${TMPRCLOG} 2>${TMPRCLOG}
540  STATUS=$?
541  if [ ${STATUS} -ne 0 ] ; then
542    cat ${TMPRCLOG}
543    rm ${TMPRCLOG}
544    exit_err "Error ${STATUS}: ${CMD}"
545  fi
546  rm ${TMPRCLOG}
547}
[eee006a]548
549create_auto_beadm()
550{
551  if [ -n "$NOBEADM" ] ; then return; fi
[c5e3699]552
553  echo "Creating new boot-environment..."
[eee006a]554  beadm create beforeUpdate-`date "+%Y-%m-%d_%H-%M-%S"`
555  if [ $? -ne 0 ] ; then
556     echo "WARNING: Unable to create a new boot-enviroment!"
557     sleep 10
558     return
559  fi
560
[404a46c]561  # Check for number of BE's to keep
562  MAXBE="5"
563  VAL="`cat ${PCBSD_ETCCONF} 2>/dev/null | grep 'MAXBE: ' | sed 's|MAXBE: ||g'`"
564  if [ -n "$VAL" ] ; then
565     if [ $(is_num "$VAL") ] ; then
566       MAXBE="$VAL"
567     fi
568  fi
569
[eee006a]570  # Check if we need to prune any BEs
571  # TODO
[c5e3699]572  echo "Pruning old boot-environments..."
573  bList="`mktemp /tmp/.belist.XXXXXX`"
574  beadm list > $bList 2>$bList
575  snapList=`cat $bList | grep ^beforeUpdate | awk '{print $1}'`
576  snapCount=`cat $bList | grep ^beforeUpdate | awk '{print $1}' | wc -l | awk '{print $1}'`
[eee006a]577
[8482df7]578  if [ -z "$snapCount" ] ; then return ; fi
[404a46c]579  if [ $snapCount -lt $MAXBE ] ; then return ; fi
[8482df7]580
581
582  # Reverse the list
583  for tmp in $snapList
584  do
585     rSnaps="$tmp $rSnaps"
586  done
587
588  # Do any pruning
[404a46c]589  KEEP="$MAXBE"
[8482df7]590  num=0
591  for snap in $rSnaps
592  do
593     num=`expr $num + 1`
594     # Make sure this BE isn't mounted or running
[c5e3699]595     cat $bList | grep "^$snap " | grep -q -e " N " -e " NR "  -e " /"
[8482df7]596     if [ $? -eq 0 ] ; then continue ; fi
597
598     if [ $num -gt $KEEP ] ; then
599        # Remove this old BE
[cd05890]600        echo "Removing Boot Environment: $snap"
601        beadm destroy -F $snap >/dev/null 2>/dev/null
[8482df7]602     fi
603  done
[c5e3699]604
605  rm $bList
[eee006a]606}
[22917b9]607
[a217080]608# Function to take a gptid/<foo> string, and map it to the real device name
609map_gptid_to_dev()
610{
611  gpart list > /tmp/.gptList.$$
612
613  # Strip off the gptid/
614  local needle="`echo $1 | sed 's|gptid/||g'`"
615  local realName=""
616
[234414e]617  while read uline
[a217080]618  do
[234414e]619    echo "$uline" | grep -q " Name: "
[a217080]620    if [ $? -eq 0 ]; then
[234414e]621      realName="`echo $uline | awk '{print $3}'`"
[a217080]622      continue
623    fi
624
[234414e]625    echo "$uline" | grep -q "rawuuid: $needle"
[a217080]626    if [ $? -eq 0 ]; then
627       echo "$realName"
628       rm /tmp/.gptList.$$
629       return 0
630       break
631    fi
632  done < /tmp/.gptList.$$
633  rm /tmp/.gptList.$$
634  return 1
635}
[22917b9]636
637# Restamp grub-install onto the ZFS root disks
638update_grub_boot()
639{
640  ROOTFS=`mount | awk '/ \/ / {print $1}'`
641  BEDS="$( echo ${ROOTFS} | awk -F '/' '{print $2}' )"
642  if [ "$BEDS" = "dev" ] ; then BEDS="ROOT"; fi
643
644  for i in `beadm list -a 2>/dev/null | grep "/${BEDS}/" | awk '{print $1}'`
645  do
646    if ! mount | grep -q "$dTank on / ("; then
647       echo -e "Copying grub.cfg to $dTank...\c" >&2
648       fMnt="/mnt.$$"
649       mkdir $fMnt
650       if ! mount -t zfs ${dTank} $fMnt ; then
651          echo "WARNING: Failed to update grub.cfg on: ${dTank}" >&2
652          continue
653       else
654         # Copy grub config and modules over to old dataset
655         # This is done so that newer grub on boot-sector has
656         # matching modules to load from all BE's
657         cp /boot/grub/grub.cfg ${fMnt}/boot/grub/grub.cfg
658         rm -rf ${fMnt}/boot/grub/i386-*
659         cp -r /boot/grub/i386-* ${fMnt}/boot/grub/
660         echo -e "done" >&2
661         umount ${fMnt} >/dev/null
662         rmdir ${fMnt} >/dev/null
663       fi
664    fi
665  done
666
667  # Check if we can re-stamp the boot-loader on any of this pools disks
668  TANK=`echo $ROOTFS | cut -d '/' -f 1`
[f671ad3]669  zpool status $TANK > /tmp/.zpStatus.$$
[a217080]670
[234414e]671  restampDisks=""
672
673  while read zline
[f671ad3]674  do
675     # If we have reached cache / log devices, we can break now
[234414e]676     echo $zline | grep -q " cache "
[f671ad3]677     if [ $? -eq 0 ] ; then break ; fi
[234414e]678     echo $zline | grep -q " log "
[f671ad3]679     if [ $? -eq 0 ] ; then break ; fi
680
681     # Only try to stamp disks marked as online
[234414e]682     echo $zline | grep -q "state: "
683     if [ $? -eq 0 ] ; then continue ; fi
684     echo $zline | grep -q "ONLINE"
[f671ad3]685     if [ $? -ne 0 ] ; then continue ; fi
686
687     # Get the disk name
[234414e]688     disk="`echo $zline | awk '{print $1}'`"
[f671ad3]689
690     # Is this a legit disk?
[234414e]691     if [ ! -e "/dev/${disk}" ] ; then continue; fi
692
693     restampDisks="$restampDisks $disk"
694  done < /tmp/.zpStatus.$$
695  rm /tmp/.zpStatus.$$
696
697  for i in $restampDisks
698  do
699     disk="$i"
[a217080]700
701     # If this is a GPTID / rawuuid, find out
702     echo "$disk" | grep -q "gptid"
703     if [ $? -eq 0 ] ; then
[54d2124]704        GRUBFLAGS="--modules='zfs part_gpt part_bsd geli'"
[a217080]705        # Just a GPTID, resolve it down to real device
706        disk="$(map_gptid_to_dev ${i})"
707        if [ -z "$disk" ] ; then
708           echo "Warning: Unable to map ${i} to real device name"
709           continue
710        fi
711     fi
712
713     # Remove the .eli, if it exists
714     disk=`echo $disk | sed 's|.eli||g'`
[22917b9]715
716     # Now get the root of the disk
717     disk=`echo $disk | sed 's|p[1-9]$||g' | sed "s|s[1-9][a-z]||g"`
718     if [ ! -e "/dev/${disk}" ] ; then continue; fi
719
720     # Re-install GRUB on this disk
721     echo "Installing GRUB to $disk" >&2
[54d2124]722     grub-install $GRUBFLAGS /dev/${disk}
[234414e]723  done
[22917b9]724  return 0
725}
Note: See TracBrowser for help on using the repository browser.