source: src-sh/libsh/functions.sh

Last change on this file was 7d56521, checked in by JoshDW19 <joshms@…>, 7 weeks ago

Changed structure to match our code styling. (functions.sh)

  • Property mode set to 100755
File size: 19.0 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# Modified 7/14/2015 by Joshua Smith
7##############################################################
8
9PCBSD_ETCCONF="/usr/local/etc/pcbsd.conf"
10
11get_mirror() {
12
13  # Check if we already looked up a mirror we can keep using
14  if [ -n "$CACHED_PCBSD_MIRROR" ] ; then
15     VAL="$CACHED_PCBSD_MIRROR"
16     export VAL
17     return
18  fi
19
20  # Set the mirror URL
21  VAL="`cat ${PCBSD_ETCCONF} 2>/dev/null | grep 'PCBSD_MIRROR: ' | sed 's|PCBSD_MIRROR: ||g'`"
22  if [ -n "$VAL" ] ; then
23     echo "Using mirror: $VAL"
24     CACHED_PCBSD_MIRROR="$VAL"
25     export VAL CACHED_PCBSD_MIRROR
26     return
27  fi
28
29  echo "Getting regional mirror..."
30  . /etc/profile
31
32  # No URL? Lets get one from the master server
33  local mFile="${HOME}/.mirrorUrl.$$"
34  touch $mFile
35  fetch -o $mFile http://getmirror.pcbsd.org >/dev/null 2>/dev/null
36  VAL="`cat $mFile | grep 'URL: ' | sed 's|URL: ||g'`"
37  rm $mFile
38  if [ -n "$VAL" ] ; then
39     echo "Using mirror: $VAL"
40     CACHED_PCBSD_MIRROR="$VAL"
41     export VAL CACHED_PCBSD_MIRROR
42     return
43  fi
44
45  # Still no mirror? Lets try the PC-BSD FTP server...
46  VAL="ftp://ftp.pcbsd.org/pub/mirror"
47  CACHED_PCBSD_MIRROR="$VAL"
48  export VAL CACHED_PCBSD_MIRROR
49  echo "Using mirror: $VAL"
50  return 
51}
52
53# Function which returns the installed list of PC-BSD mirrors for use
54# with the fetch command
55# Will return just a single mirror, if the user has manually specified one
56# in /usr/local/etc/pcbsd.conf
57get_mirror_loc()
58{
59  if [ -z $1 ] ; then
60     exit_err "Need to supply file to grab from mirrors..."
61  fi
62  if [ -z $2 ] ; then
63     exit_err "Need to supply which mirror to fetch from..."
64  fi
65
66  case $2 in
67    pkg) mirrorTag="PKG_MIRROR" 
68         mirrorFile="/usr/local/share/pcbsd/conf/pkg-mirror"
69         ;;
70    pbi) mirrorTag="PBI_MIRROR" 
71         mirrorFile="/usr/local/share/pcbsd/conf/pbi-mirror"
72         ;;
73    iso) mirrorTag="ISO_MIRROR" 
74         mirrorFile="/usr/local/share/pcbsd/conf/iso-mirror"
75         ;;
76  update) mirrorTag="UPDATE_MIRROR" 
77         mirrorFile="/usr/local/share/pcbsd/conf/update-mirror"
78         ;;
79    *) exit_err "Bad mirror type!" ;;
80  esac
81
82  # Set the mirror URL
83  local VAL=`cat ${PCBSD_ETCCONF} 2>/dev/null | grep "^${mirrorTag}:" | sed "s|^${mirrorTag}: ||g"`
84  if [ -n "$VAL" ] ; then
85     echo "${VAL}${1}"
86     return
87  fi
88
89  if [ ! -e "${mirrorFile}" ] ; then
90     exit_err "Missing mirror list: ${mirrorFile}"
91  fi
92
93  # Build the mirror list
94  while read line
95  do
96    VAL="${line}${1}"
97    break
98  done < ${mirrorFile}
99  echo ${VAL}
100}
101
102# Function to download a file from the pcbsd mirrors
103# Arg1 = Remote File URL
104# Arg2 = Where to save file
105get_file_from_mirrors()
106{
107   _rf="${1}"
108   _lf="${2}"
109   _mtype="${3}"
110
111   case $_mtype in
112      iso|pbi|pkg|update) ;;
113      *) exit_err "Fixme! Missing mirror type in get_file_from_mirrors" ;;
114   esac
115
116   # Get any proxy information
117   . /etc/profile
118
119   # Get mirror list
120   local mirrorLoc="$(get_mirror_loc ${_rf} ${_mtype})"
121   mirrorLoc="`echo $mirrorLoc | awk '{print $1}'`"
122
123   # Running from a non GUI?
124   if [ "$GUI_FETCH_PARSING" != "YES" -a "$PBI_FETCH_PARSING" != "YES" -a -z "$PCFETCHGUI" ] ; then
125      fetch -o "${_lf}" ${mirrorLoc}
126      return $?
127   fi
128
129   echo "FETCH: ${_rf}"
130
131   # Doing a front-end download, parse the output of fetch
132   _eFile="/tmp/.fetch-exit.$$"
133   fetch -s ${mirrorLoc} > /tmp/.fetch-size.$$ 2>/dev/null
134   _fSize=`cat /tmp/.fetch-size.$$ 2>/dev/null`
135   _fSize="`expr ${_fSize} / 1024 2>/dev/null`"
136   rm "/tmp/.fetch-size.$$" 2>/dev/null
137   _time=1
138   if [ -z "$_fSize" ] ; then _fSize=0; fi
139
140   ( fetch -o ${_lf} ${mirrorLoc} >/dev/null 2>/dev/null ; echo "$?" > ${_eFile} ) &
141   FETCH_PID=$!
142   while :
143   do
144      if [ -e "${_lf}" ] ; then
145         sync
146         _dSize=`du -k ${_lf} | tr -d '\t' | cut -d '/' -f 1`
147         if [ $(is_num "$_dSize") ] ; then
148            if [ ${_fSize} -lt ${_dSize} ] ; then _dSize="$_fSize" ; fi
149                   _kbs=`expr ${_dSize} \/ $_time`
150                   echo "SIZE: ${_fSize} DOWNLOADED: ${_dSize} SPEED: ${_kbs} KB/s"
151             fi
152      fi
153
154      # Make sure download isn't finished
155      jobs -l >/tmp/.jobProcess.$$
156      cat /tmp/.jobProcess.$$ | awk '{print $3}' | grep -q ${FETCH_PID}
157      if [ "$?" != "0" ] ; then
158         rm /tmp/.jobProcess.$$ ; break
159         fi
160      sleep 1
161      _time=`expr $_time + 1`
162   done
163
164   _err="`cat ${_eFile} 2>/dev/null`"
165   if [ -z "$_err" ] ; then
166      _err="0";
167   fi
168   rm ${_eFile} 2>/dev/null
169
170   if [ "$_err" = "0" ]; then
171      echo "FETCHDONE" ; fi
172   unset FETCH_PID
173   return $_err
174
175}
176
177# Function to download a file from remote using fetch
178# Arg1 = Remote File URL
179# Arg2 = Where to save file
180# Arg3 = Number of attempts to make before failing
181get_file() {
182
183        _rf="${1}"
184        _lf="${2}"
185        _ftries=${3}
186        if [ -z "$_ftries" ] ; then _ftries=3; fi
187
188        # Get any proxy information
189        . /etc/profile
190
191        if [ -e "${_lf}" ] ; then
192                echo "Resuming download of: ${_lf}"
193        fi
194
195        if [ "$GUI_FETCH_PARSING" != "YES" -a -z "$PCFETCHGUI" ] ; then
196                fetch -r -o "${_lf}" "${_rf}"
197                _err=$?
198        else
199                echo "FETCH: ${_rf}"
200
201                # Doing a front-end download, parse the output of fetch
202                _eFile="/tmp/.fetch-exit.$$"
203                fetch -s "${_rf}" > /tmp/.fetch-size.$$ 2>/dev/null
204                _fSize=`cat /tmp/.fetch-size.$$ 2>/dev/null`
205                _fSize="`expr ${_fSize} / 1024 2>/dev/null`"
206                rm "/tmp/.fetch-size.$$" 2>/dev/null
207                _time=1
208                if [ -z "$_fSize" ] ; then _fSize=0; fi
209
210                ( fetch -r -o "${_lf}" "${_rf}" >/dev/null 2>/dev/null ; echo "$?" > ${_eFile} ) &
211                FETCH_PID=`ps -auwwwx | grep -v grep | grep "fetch -r -o ${_lf}" | awk '{print $2}'`
212                while :
213                do
214                        if [ -e "${_lf}" ] ; then
215                                _dSize=`du -k ${_lf} | tr -d '\t' | cut -d '/' -f 1`
216                                if [ $(is_num "$_dSize") ] ; then
217                                        if [ ${_fSize} -lt ${_dSize} ] ; then _dSize="$_fSize" ; fi
218                                        _kbs=`expr ${_dSize} \/ $_time`
219                                        echo "SIZE: ${_fSize} DOWNLOADED: ${_dSize} SPEED: ${_kbs} KB/s"
220                                fi
221                        fi
222
223                        # Make sure download isn't finished
224                        ps -p $FETCH_PID >/dev/null 2>/dev/null
225                        if [ "$?" != "0" ] ; then break ; fi
226                        sleep 2
227                        _time=`expr $_time + 2`
228                done
229
230                _err="`cat ${_eFile} 2>/dev/null`"
231                if [ -z "$_err" ] ; then _err="0"; fi
232                rm ${_eFile} 2>/dev/null
233                if [ "$_err" = "0" ]; then echo "FETCHDONE" ; fi
234                unset FETCH_PID
235        fi
236
237        echo ""
238        if [ $_err -ne 0 -a $_ftries -gt 0 ] ; then
239                sleep 30
240                _ftries=`expr $_ftries - 1`
241
242                # Remove the local file if we failed
243                if [ -e "${_lf}" ]; then rm "${_lf}"; fi
244
245                get_file "${_rf}" "${_lf}" $_ftries     
246                _err=$?
247        fi
248        return $_err
249}
250
251# Check if a value is a number
252is_num()
253{
254        expr $1 + 1 2>/dev/null
255        return $?
256}
257
258# Exit with a error message
259exit_err() {
260        if [ -n "${LOGFILE}" ] ; then
261           echo "ERROR: $*" >> ${LOGFILE}
262        fi
263        echo >&2 "ERROR: $*"
264        exit 1
265}
266
267
268### Print an error on STDERR and bail out
269printerror() {
270  exit_err $*
271}
272
273
274# Check if the target directory is on ZFS
275# Arg1 = The dir to check
276# Arg2 = If set to 1, don't dig down to lower level directory
277isDirZFS() {
278  local _chkDir="$1"
279  while :
280  do
281     # Is this dir a ZFS mount
282     mount | grep -w "on $_chkDir " | grep -qw "(zfs," && return 0
283
284     # If this directory is mounted, but NOT ZFS
285     if [ "$2" != "1" ] ; then
286       mount | grep -qw "on $_chkDir " && return 1
287     fi
288     
289     # Quit if not walking down
290     if [ "$2" = "1" ] ; then return 1 ; fi
291 
292     if [ "$_chkDir" = "/" ] ; then break ; fi
293     _chkDir=`dirname $_chkDir`
294  done
295 
296  return 1
297}
298
299# Gets the mount-point of a particular zpool / dataset
300# Arg1 = zpool to check
301getZFSMount() {
302  local zpool="$1"
303  local mnt=`mount | grep "^${zpool} on" | grep "(zfs," | awk '{print $3}'`
304  if [ -n "$mnt" ] ; then
305     echo "$mnt"
306     return 0
307  fi
308  return 1
309}
310
311# Get the ZFS dataset of a particular directory
312getZFSDataset() {
313  local _chkDir="$1"
314  while :
315  do
316    local zData=`mount | grep " on ${_chkDir} " | grep "(zfs," | awk '{print $1}'`
317    if [ -n "$zData" ] ; then
318       echo "$zData"
319       return 0
320    fi
321    if [ "$2" != "rec" ] ; then return 1 ; fi
322    if [ "$_chkDir" = "/" ] ; then return 1 ; fi
323    _chkDir=`dirname $_chkDir`
324  done
325  return 1
326}
327
328# Get the ZFS tank name for a directory
329# Arg1 = Directory to check
330getZFSTank() {
331  local _chkDir="$1"
332
333  _chkdir=${_chkDir%/}
334  while :
335  do
336     zpath=`zfs list | awk -v path="${_chkDir}" '$5 == path { print $1 }'`
337     if [ -n "${zpath}" ] ; then
338        echo $zpath | cut -f1 -d '/'
339        return 0
340     fi
341
342     if [ "$_chkDir" = "/" ] ; then return 1 ; fi
343     _chkDir=`dirname $_chkDir`
344  done
345
346  return 1
347}
348
349# Get the mountpoint for a ZFS name
350# Arg1 = name
351getZFSMountpoint() {
352   local _chkName="${1}"
353   if [ -z "${_chkName}" ]; then return 1 ; fi
354
355   zfs list "${_chkName}" | tail -1 | awk '{ print $5 }'
356}
357
358# Get the ZFS relative path for a path
359# Arg1 = Path
360getZFSRelativePath() {
361   local _chkDir="${1}"
362   local _tank=`getZFSTank "$_chkDir"`
363   local _mp=`getZFSMountpoint "${_tank}"`
364
365   if [ -z "${_tank}" ] ; then return 1 ; fi
366
367   local _name="${_chkDir#${_mp}}"
368
369   # Make sure we have a '/' at the start of dataset
370   if [ "`echo ${_name} | cut -c 1`" != "/" ] ; then _name="/${_name}"; fi
371
372   echo "${_name}"
373   return 0
374}
375
376# Check if an address is IPv6
377isV6() {
378  echo ${1} | grep -q ":"
379  return $?
380}
381   
382# Is a mount point, or any of its parent directories, a symlink?
383is_symlinked_mountpoint()
384{
385        local _dir
386        _dir=$1
387        [ -L "$_dir" ] && return 0
388        [ "$_dir" = "/" ] && return 1
389        is_symlinked_mountpoint `dirname $_dir`
390        return $?
391}
392
393# Function to ask the user to press Return to continue
394rtn()
395{
396  echo -e "Press ENTER to continue\c";
397  read garbage
398};
399
400# Function to check if an IP address passes a basic sanity test
401check_ip()
402{
403  ip="$1"
404 
405  # If this is a V6 address, skip validation for now
406  isV6 "${ip}"
407  if [ $? -eq 0 ] ; then return ; fi
408
409  # Check if we can cut this IP into the right segments
410  SEG="`echo $ip | cut -d '.' -f 1 2>/dev/null`"
411  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
412  if [ "$?" != "0" ]
413  then
414     return 1
415  fi
416  if [ $SEG -gt 255 -o $SEG -lt 0 ]
417  then
418     return 1
419  fi
420 
421  # Second segment
422  SEG="`echo $ip | cut -d '.' -f 2 2>/dev/null`"
423  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
424  if [ "$?" != "0" ]
425  then
426     return 1
427  fi
428  if [ $SEG -gt 255 -o $SEG -lt 0 ]
429  then
430     return 1
431  fi
432
433  # Third segment
434  SEG="`echo $ip | cut -d '.' -f 3 2>/dev/null`"
435  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
436  if [ "$?" != "0" ]
437  then
438     return 1
439  fi
440  if [ $SEG -gt 255 -o $SEG -lt 0 ]
441  then
442     return 1
443  fi
444 
445  # Fourth segment
446  SEG="`echo $ip | cut -d '.' -f 4 2>/dev/null`"
447  echo $SEG | grep -E "^[0-9]+$" >/dev/null 2>/dev/null
448  if [ "$?" != "0" ]
449  then
450     return 1
451  fi
452  if [ $SEG -gt 255 -o $SEG -lt 0 ]
453  then
454     return 1
455  fi
456
457  return 0
458};
459
460# Run the first boot wizard
461# Should be called from a .xinitrc script, after fluxbox is already running
462run_firstboot()
463{
464  # Is the trigger file set?
465  if [ ! -e "/var/.pcbsd-firstgui" ] ; then return; fi
466
467  # Set all our path variables
468  PATH="/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/bin:/usr/local/sbin"
469  HOME="/root"
470  export PATH HOME
471
472  # Unset the PROGDIR variable
473  PROGDIR=""
474  export PROGDIR
475
476  if [ -e "/root/.xprofile" ] ; then . /root/.xprofile ; fi
477
478  # Figure out which intro video to play
479  res=`xdpyinfo | grep dimensions: | awk "{print $2}"`
480  h=`echo $res | cut -d "x" -f 1`
481  w=`echo $res | cut -d "x" -f 2`
482  h=`expr 100 \* $h`
483  ratio=`expr $h \/ $w | cut -c 1-2`
484  case $ratio in
485    13) mov="PCBSD9_4-3_UXGA.flv";;
486    16) mov="PCBSD9_16-10_WUXGA.flv";;
487    17) mov="PCBSD9_16-9_1080p.flv";;
488     *) mov="PCBSD9_4-3_UXGA.flv";;
489  esac
490
491  # Play the video now
492  # NO Movie for 10, if we end up with one, replace this
493  #mplayer -fs -nomouseinput -zoom /usr/local/share/pcbsd/movies/$mov
494
495  # Setting a language
496  if [ -e "/etc/pcbsd-lang" ] ; then
497    LANG=`cat /etc/pcbsd-lang`
498    export LANG
499  fi
500
501  # Start first-boot wizard
502  /usr/local/bin/pc-firstboot >/var/log/pc-firstbootwiz 2>/var/log/pc-firstbootwiz
503  if [ $? -eq 0 ] ; then
504    rm /var/.pcbsd-firstgui
505  fi
506}
507
508# Run-command, don't halt if command exits with non-0
509rc_nohalt()
510{
511  CMD="$1"
512
513  if [ -z "${CMD}" ] ; then
514    exit_err "Error: missing argument in rc_nohalt()"
515  fi
516
517  ${CMD}
518}
519
520# Run-command, halt if command exits with non-0
521rc_halt()
522{
523  CMD="$@"
524
525  if [ -z "${CMD}" ] ; then
526    exit_err "Error: missing argument in rc_halt()"
527  fi
528
529  ${CMD}
530  STATUS=$?
531  if [ ${STATUS} -ne 0 ] ; then
532    exit_err "Error ${STATUS}: ${CMD}"
533  fi
534}
535
536# Run-command silently, only display / halt if command exits with non-0
537rc_halt_s()
538{
539  CMD="$@"
540
541  if [ -z "${CMD}" ] ; then
542    exit_err "Error: missing argument in rc_halt()"
543  fi
544
545  TMPRCLOG=`mktemp /tmp/.rc_halt.XXXXXX`
546  ${CMD} >${TMPRCLOG} 2>${TMPRCLOG}
547  STATUS=$?
548  if [ ${STATUS} -ne 0 ] ; then
549    cat ${TMPRCLOG}
550    rm ${TMPRCLOG}
551    exit_err "Error ${STATUS}: ${CMD}"
552  fi
553  rm ${TMPRCLOG}
554}
555
556create_auto_beadm()
557{
558  if [ -n "$NOBEADM" ] ; then return; fi
559
560  echo "Creating new boot-environment..."
561  beadm create beforeUpdate-`date "+%Y-%m-%d_%H-%M-%S"`
562  if [ $? -ne 0 ] ; then
563     echo "WARNING: Unable to create a new boot-enviroment!"
564     sleep 10
565     return
566  fi
567
568}
569
570do_prune_be()
571{
572  # Check for number of BE's to keep
573  MAXBE="3"
574  VAL="`cat ${PCBSD_ETCCONF} 2>/dev/null | grep 'MAXBE: ' | sed 's|MAXBE: ||g'`"
575  if [ -n "$VAL" ] ; then
576     if [ $(is_num "$VAL") ] ; then
577       MAXBE="$VAL"
578     fi
579  fi
580
581  # Shrink MAXBE by one, so that when we add a new one, it matches the real MAXBE
582  MAXBE=`expr $MAXBE - 1`
583  if [ $MAXBE -lt 1 ] ; then
584    MAXBE=1
585  fi
586
587  # Check if we need to prune any BEs
588  echo "Pruning old boot-environments..."
589  bList="`mktemp /tmp/.belist.XXXXXX`"
590  beadm list > $bList 2>$bList
591  snapList=`cat $bList | grep -e "^beforeUpdate" -e "default" -e "-up-" | awk '{print $1}'`
592  snapCount=`cat $bList | grep -e "^beforeUpdate" -e "default" -e "-up-" | awk '{print $1}' | wc -l | awk '{print $1}'`
593
594  if [ -z "$snapCount" ] ; then return ; fi
595
596  # Check if this is forced removal of an old BE
597  if [ "$1" = "force" ] ; then
598     # If we only have 3 BE's, return, don't want to prune less than that
599     if [ $snapCount -lt 3 ] ; then
600        return;
601     fi
602     MAXBE=`expr $snapCount - 1`
603  fi
604
605  # If we have less BE than MAX, return
606  if [ $snapCount -lt $MAXBE ] ; then return ; fi
607
608  # Reverse the list
609  for tmp in $snapList
610  do
611     rSnaps="$tmp $rSnaps"
612  done
613
614  # Do any pruning
615  KEEP="$MAXBE"
616  num=0
617  for snap in $rSnaps
618  do
619     num=`expr $num + 1`
620     # Make sure this BE isn't mounted or running
621     cat $bList | grep "^$snap " | grep -q -e " N " -e " NR "  -e " /"
622     if [ $? -eq 0 ] ; then continue ; fi
623
624     if [ $num -gt $KEEP ] ; then
625        # Remove this old BE
626        echo "Removing Boot Environment: $snap"
627        beadm destroy -F $snap >/dev/null 2>/dev/null
628     fi
629  done
630
631  rm $bList
632}
633
634# Function to take a gptid/<foo> string, and map it to the real device name
635map_gptid_to_dev()
636{
637  # First check glabel
638  local needle="`echo $1 | sed 's|.eli||g'`"
639  local realName="`glabel status | grep -w -e $needle | awk '{print $3}'`"
640  if [ -n "$realName" ] ; then
641     echo "$realName"
642     return 0
643  fi
644
645  # Do it the hard way
646  gpart list > /tmp/.gptList.$$
647  # Strip off the gptid/
648  needle="`echo $1 | sed 's|gptid/||g'`"
649  local realName=""
650
651  while read uline
652  do
653    echo "$uline" | grep -q " Name: "
654    if [ $? -eq 0 ]; then
655      realName="`echo $uline | awk '{print $3}'`"
656      continue
657    fi
658
659    echo "$uline" | grep -q "rawuuid: $needle"
660    if [ $? -eq 0 ]; then
661       echo "$realName"
662       rm /tmp/.gptList.$$
663       return 0
664       break
665    fi
666  done < /tmp/.gptList.$$
667  rm /tmp/.gptList.$$
668  return 1
669}
670
671# Find FreeBSD partitions geom name
672map_diskid_to_dev()
673{
674  # Remove the .eli / s1 / p2 or whatever from end of label
675  diskID=`echo $1 | sed 's|.eli||g' | rev | cut -c 3- | rev`
676
677  devName="`glabel status | grep -w -e $diskID | awk '{print $3}'`"
678  if [ -n "$devName" ] ; then
679     echo "${devName}"
680  fi
681}
682
683# Restamp grub-install onto the ZFS root disks
684update_grub_boot()
685{
686  # Bail out if GRUB isn't setup as the default BL on this system
687  if [ ! -e "/boot/grub/grub.cfg" ] ; then return 0; fi
688
689  ROOTFS=`mount | awk '/ \/ / {print $1}'`
690  BEDS="$( echo ${ROOTFS} | awk -F '/' '{print $2}' )"
691  if [ "$BEDS" = "dev" ] ; then BEDS="ROOT"; fi
692
693  # Check if we can re-stamp the boot-loader on any of this pools disks
694  TANK=`echo $ROOTFS | cut -d '/' -f 1`
695  zpool status $TANK > /tmp/.zpStatus.$$
696
697  restampDisks=""
698
699  while read zline
700  do
701     # If we have reached cache / log devices, we can break now
702     echo $zline | grep -qw "cache"
703     if [ $? -eq 0 ] ; then break ; fi
704     echo $zline | grep -qw "log"
705     if [ $? -eq 0 ] ; then break ; fi
706
707     # Only try to stamp disks marked as online
708     echo $zline | grep -q "state: "
709     if [ $? -eq 0 ] ; then continue ; fi
710     echo $zline | grep -q "ONLINE"
711     if [ $? -ne 0 ] ; then continue ; fi
712
713     # Get the disk name
714     disk="`echo $zline | awk '{print $1}'`"
715
716     # Is this a legit disk?
717     if [ ! -e "/dev/${disk}" ] ; then continue; fi
718
719     restampDisks="$restampDisks $disk"
720  done < /tmp/.zpStatus.$$
721  rm /tmp/.zpStatus.$$
722
723  for i in $restampDisks
724  do
725     disk="$i"
726
727     # If this is a GPTID / rawuuid, find out
728     echo "$disk" | grep -q "gptid"
729     if [ $? -eq 0 ] ; then
730        # Just a GPTID, resolve it down to real device
731        disk="$(map_gptid_to_dev ${i})"
732        if [ -z "$disk" ] ; then
733           echo "Warning: Unable to map ${i} to real device name"
734           continue
735        fi
736     fi
737
738     # Check for diskid glabel
739     echo "$disk" | grep -q "diskid"
740     if [ $? -eq 0 ] ; then
741        # Try to determine disk ID -> adaX mapping
742        disk=$(map_diskid_to_dev $disk)
743        if [ -z "$disk" ] ; then
744           echo "Warning: Unable to map ${i} to real device name"
745           continue
746        fi
747     fi
748
749     # If we are doing a EFI boot
750     if [ "`kenv grub.platform 2>/dev/null`" = "efi" ] ; then
751        GRUBFLAGS="$GRUBFLAGS --efi-directory=/boot/efi --removable --target=x86_64-efi"
752     fi
753
754     # Check encryption
755     zpool status | grep -q '\.eli '
756     if [ $? -eq 0 ] ; then
757       # Check for crypto disk
758       cat /usr/local/etc/default/grub | grep -q "GRUB_ENABLE_CRYPTODISK"
759       if [ $? -ne 0 ] ; then
760         echo "GRUB_ENABLE_CRYPTODISK=y" >> /usr/local/etc/default/grub
761       fi
762     fi
763
764     # Remove the .eli, if it exists
765     disk=`echo $disk | sed 's|.eli||g'`
766
767     # Now get the root of the disk
768     disk=`echo $disk | sed 's|p[1-9]$||g' | sed 's|p[1-9][0-9]$||g' | sed "s|s[1-9][a-z]$||g" | sed "s|s[1-9]$||g"`
769     if [ ! -e "/dev/${disk}" ] ; then continue; fi
770
771     # Re-install GRUB on this disk
772     echo "Installing GRUB to $disk" >&2
773     grub-install $GRUBFLAGS /dev/${disk} 2>/dev/null
774     if [ $? -ne 0 ] ; then
775       grub-install /dev/${disk}
776     fi
777  done
778
779  return 0
780}
781
782get_root_pool_free_space()
783{
784  local ROOTPOOL=`mount | grep 'on / ' | cut -d '/' -f 1`
785  if [ -z "$ROOTPOOL" ] ; then return ; fi
786  local freeSpace=`zpool list -Hp ${ROOTPOOL} | awk '{print $4}'`
787  if [ ! $(is_num "$freeSpace") ] ; then return ; fi
788  # Convert freespace to Kb
789  freeSpace=`expr $freeSpace / 1024 2>/dev/null`
790  echo "$freeSpace"
791}
Note: See TracBrowser for help on using the repository browser.