Print this page
Modernize CDDL license text and copyrights for OmniTI-original code.
Checkpoint for find-and-install
Generalize rpool to variable RPOOL in disk_help.sh
   1 #!/usr/bin/bash

   2 #
   3 # CDDL HEADER START



   4 #
   5 # The contents of this file are subject to the terms of the
   6 # Common Development and Distribution License, Version 1.0 only
   7 # (the "License").  You may not use this file except in compliance
   8 # with the License.
   9 #
  10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  11 # or http://www.opensolaris.org/os/licensing.
  12 # See the License for the specific language governing permissions
  13 # and limitations under the License.
  14 #
  15 # When distributing Covered Code, include this CDDL HEADER in each
  16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  17 # If applicable, add the following below this CDDL HEADER, with the
  18 # fields enclosed by brackets "[]" replaced with your own identifying
  19 # information: Portions Copyright [yyyy] [name of copyright owner]
  20 #
  21 # CDDL HEADER END
  22 #
  23 #
  24 # Copyright 2012 OmniTI Computer Consulting, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 #
  27 ListDisks() {
  28   declare -A disksize
  29   declare -A diskname
  30   for rdsk in $(prtconf -v | grep dev_link | awk -F= '/\/dev\/rdsk\/c.*p0/{print $2;}')
  31   do
  32     disk=`echo $rdsk | sed -e 's/.*\///g; s/p0//;'`
  33     size=`prtvtoc $rdsk 2>/dev/null | awk '/bytes\/sector/{bps=$2} /sectors\/cylinder/{bpc=bps*$2} /accessible sectors/{print ($2*bps)/1048576;} /accessible cylinders/{print int(($2*bpc)/1048576);}'`
  34     disksize+=([$disk]=$size)
  35   done
  36 
  37   disk=""
  38   while builtin read diskline
  39   do
  40     if [[ -n "$disk" ]]; then
  41       desc=`echo $diskline | sed -e 's/^[^\<]*//; s/[\<\>]//g;'`
  42       diskname+=([$disk]=$desc)
  43       disk=""
  44     else
  45       disk=$diskline
  46     fi


  75           if [[ -n $(echo ${diskname[$disk]} | egrep -e "$PAT") ]]; then
  76             echo $disk
  77           fi
  78           ;;
  79       esac
  80     done
  81   done
  82 }
  83 ListDisksAnd() {
  84   EXPECT=$(( $(echo "$1" | sed -e 's/[^,]//g;' | wc -c) + 0))
  85   for part in $(echo "$1" | sed -e 's/,/ /g;'); do
  86     ListDisks $part
  87   done | sort | uniq -c | awk '{if($1=='$EXPECT'){print $2;}}'
  88 }
  89 ListDisksUnique(){
  90   for term in $*; do
  91     ListDisksAnd $term
  92   done | sort | uniq | xargs
  93 }
  94 
  95 BuildRpool() {
  96   ztype=""
  97   ztgt=""
  98   disks=`ListDisksUnique $*`
  99   log "Disks being used for rpool: $disks"
 100   if [[ -z "$disks" ]]; then
 101     bomb "No matching disks found to build rpool"
 102   fi
 103   rm -f /tmp/kayak-disk-list
 104   for i in $disks
 105   do
 106     if [[ -n "$ztgt" ]]; then
 107       ztype="mirror"
 108     fi
 109     ztgt="$ztgt ${i}"
 110     # Keep track of disks for later...
 111     echo ${i} >> /tmp/kayak-disk-list
 112   done
 113   log "zpool destroy rpool (just in case we've been run twice)"
 114   zpool destroy rpool 2> /dev/null
 115   log "Creating rpool with: zpool create -f rpool $ztype $ztgt"
 116   # Just let "zpool create" do its thing. We want GPT disks now.
 117   zpool create -f rpool $ztype $ztgt || bomb "Failed to create rpool"



 118   BuildBE
 119 }
 120 GetTargetVolSize() {
 121     # Aim for 25% of physical memory (minimum 1G)
 122     # prtconf always reports in megabytes
 123     local mem=`/usr/sbin/prtconf | /bin/awk '/^Memory size/ { print $3 }'`
 124     if [[ $mem -lt 4096 ]]; then
 125         local vsize=1
 126     else
 127         local quart=`echo "scale=1;$mem/4096" | /bin/bc`
 128         local vsize=`printf %0.f $quart`
 129     fi
 130     echo $vsize
 131 }    
 132 GetRpoolFree() {
 133     local zfsavail=`/sbin/zfs list -H -o avail rpool`
 134     if [[ ${zfsavail:(-1)} = "G" ]]; then
 135         local avail=`printf %0.f ${zfsavail::-1}`
 136     elif [[ ${zfsavail:(-1)} = "T" ]]; then
 137         local gigs=`echo "scale=1;${zfsavail::-1}*1024" | /bin/bc`
 138         avail=`printf %0.f $gigs`
 139     else
 140         # If we get here, there's too little space left to be usable
 141         avail=0
 142     fi
 143     echo $avail
 144 }
 145 MakeSwapDump() {
 146     local size=`GetTargetVolSize`
 147     local totalvols=""
 148     local usable=""
 149     local finalsize=""
 150     local savecore=""
 151 
 152     # We're creating both swap and dump volumes of the same size
 153     let totalvols=${size}*2
 154 
 155     # We want at least 10GB left free after swap/dump
 156     # If we can't make swap/dump at least 1G each, don't bother
 157     let usable=`GetRpoolFree`-10
 158     if [[ $usable -lt 2 ]]; then
 159         log "Not enough free space for reasonably-sized swap and dump; not creating either."
 160         return 0
 161     fi
 162 
 163     # If the total of swap and dump is greater than the usable free space,
 164     # make swap and dump each take half but don't enable savecore
 165     if [[ $totalvols -ge $usable ]]; then
 166         let finalsize=${usable}/2
 167         savecore="-n"
 168     else
 169         finalsize=$size
 170         savecore="-y"
 171     fi
 172 
 173     for volname in swap dump; do
 174         /sbin/zfs create -V ${finalsize}G rpool/$volname || \
 175             bomb "Failed to create rpool/$volname"
 176     done
 177     printf "/dev/zvol/dsk/rpool/swap\t-\t-\tswap\t-\tno\t-\n" >> $ALTROOT/etc/vfstab
 178     Postboot /usr/sbin/dumpadm $savecore -d /dev/zvol/dsk/rpool/dump
 179     return 0
 180 }
   1 #!/usr/bin/bash
   2 
   3 #
   4 # This file and its contents are supplied under the terms of the
   5 # Common Development and Distribution License ("CDDL"), version 1.0.
   6 # You may only use this file in accordance with the terms of version
   7 # 1.0 of the CDDL.
   8 #
   9 # A full copy of the text of the CDDL should have accompanied this
  10 # source.  A copy of the CDDL is also available via the Internet at
  11 # http://www.illumos.org/license/CDDL.

  12 #
  13 



  14 #
  15 # Copyright 2017 OmniTI Computer Consulting, Inc.  All rights reserved.




  16 #
  17 





  18 ListDisks() {
  19   declare -A disksize
  20   declare -A diskname
  21   for rdsk in $(prtconf -v | grep dev_link | awk -F= '/\/dev\/rdsk\/c.*p0/{print $2;}')
  22   do
  23     disk=`echo $rdsk | sed -e 's/.*\///g; s/p0//;'`
  24     size=`prtvtoc $rdsk 2>/dev/null | awk '/bytes\/sector/{bps=$2} /sectors\/cylinder/{bpc=bps*$2} /accessible sectors/{print ($2*bps)/1048576;} /accessible cylinders/{print int(($2*bpc)/1048576);}'`
  25     disksize+=([$disk]=$size)
  26   done
  27 
  28   disk=""
  29   while builtin read diskline
  30   do
  31     if [[ -n "$disk" ]]; then
  32       desc=`echo $diskline | sed -e 's/^[^\<]*//; s/[\<\>]//g;'`
  33       diskname+=([$disk]=$desc)
  34       disk=""
  35     else
  36       disk=$diskline
  37     fi


  66           if [[ -n $(echo ${diskname[$disk]} | egrep -e "$PAT") ]]; then
  67             echo $disk
  68           fi
  69           ;;
  70       esac
  71     done
  72   done
  73 }
  74 ListDisksAnd() {
  75   EXPECT=$(( $(echo "$1" | sed -e 's/[^,]//g;' | wc -c) + 0))
  76   for part in $(echo "$1" | sed -e 's/,/ /g;'); do
  77     ListDisks $part
  78   done | sort | uniq -c | awk '{if($1=='$EXPECT'){print $2;}}'
  79 }
  80 ListDisksUnique(){
  81   for term in $*; do
  82     ListDisksAnd $term
  83   done | sort | uniq | xargs
  84 }
  85 
  86 BuildRpoolOnly() {
  87   ztype=""
  88   ztgt=""
  89   disks=`ListDisksUnique $*`
  90   log "Disks being used for root pool $RPOOL: $disks"
  91   if [[ -z "$disks" ]]; then
  92     bomb "No matching disks found to build root pool $RPOOL"
  93   fi
  94   rm -f /tmp/kayak-disk-list
  95   for i in $disks
  96   do
  97     if [[ -n "$ztgt" ]]; then
  98       ztype="mirror"
  99     fi
 100     ztgt="$ztgt ${i}"
 101     # Keep track of disks for later...
 102     echo ${i} >> /tmp/kayak-disk-list
 103   done
 104   log "zpool destroy $RPOOL (just in case we've been run twice)"
 105   zpool destroy $RPOOL 2> /dev/null
 106   log "Creating root pool with: zpool create -f $RPOOL $ztype $ztgt"
 107   # Just let "zpool create" do its thing. We want GPT disks now.
 108   zpool create -f $RPOOL $ztype $ztgt || bomb "Failed to create root pool $RPOOL"
 109 }
 110 BuildRpool() {
 111   BuildRpoolOnly $*
 112   BuildBE
 113 }
 114 GetTargetVolSize() {
 115     # Aim for 25% of physical memory (minimum 1G)
 116     # prtconf always reports in megabytes
 117     local mem=`/usr/sbin/prtconf | /bin/awk '/^Memory size/ { print $3 }'`
 118     if [[ $mem -lt 4096 ]]; then
 119         local vsize=1
 120     else
 121         local quart=`echo "scale=1;$mem/4096" | /bin/bc`
 122         local vsize=`printf %0.f $quart`
 123     fi
 124     echo $vsize
 125 }    
 126 GetRpoolFree() {
 127     local zfsavail=`/sbin/zfs list -H -o avail $RPOOL` 
 128     if [[ ${zfsavail:(-1)} = "G" ]]; then
 129         local avail=`printf %0.f ${zfsavail::-1}`
 130     elif [[ ${zfsavail:(-1)} = "T" ]]; then
 131         local gigs=`echo "scale=1;${zfsavail::-1}*1024" | /bin/bc`
 132         avail=`printf %0.f $gigs`
 133     else
 134         # If we get here, there's too little space left to be usable
 135         avail=0
 136     fi
 137     echo $avail
 138 }
 139 MakeSwapDump() {
 140     local size=`GetTargetVolSize`
 141     local totalvols=""
 142     local usable=""
 143     local finalsize=""
 144     local savecore=""
 145 
 146     # We're creating both swap and dump volumes of the same size
 147     let totalvols=${size}*2
 148 
 149     # We want at least 10GB left free after swap/dump
 150     # If we can't make swap/dump at least 1G each, don't bother
 151     let usable=`GetRpoolFree`-10
 152     if [[ $usable -lt 2 ]]; then
 153         log "Not enough free space for reasonably-sized swap and dump; not creating either."
 154         return 0
 155     fi
 156 
 157     # If the total of swap and dump is greater than the usable free space,
 158     # make swap and dump each take half but don't enable savecore
 159     if [[ $totalvols -ge $usable ]]; then
 160         let finalsize=${usable}/2
 161         savecore="-n"
 162     else
 163         finalsize=$size
 164         savecore="-y"
 165     fi
 166 
 167     for volname in swap dump; do
 168         /sbin/zfs create -V ${finalsize}G $RPOOL/$volname || \
 169             bomb "Failed to create $RPOOL/$volname"
 170     done
 171     printf "/dev/zvol/dsk/$RPOOL/swap\t-\t-\tswap\t-\tno\t-\n" >> $ALTROOT/etc/vfstab
 172     Postboot /usr/sbin/dumpadm $savecore -d /dev/zvol/dsk/$RPOOL/dump
 173     return 0
 174 }