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
47 done < <(format < /dev/null | awk '/^ *[0-9]*\. /{print $2; print;}')
48
49 for want in $*
50 do
51 for disk in "${!disksize[@]}" ; do
52 case "$want" in
53 \>*)
54 if [[ -n ${disksize[$disk]} && "${disksize[$disk]}" -ge "${want:1}" ]]; then
55 echo $disk
56 fi
57 ;;
58 \<*)
59 if [[ -n ${disksize[$disk]} && "${disksize[$disk]}" -le "${want:1}" ]]; then
60 echo $disk
61 fi
62 ;;
63 *)
64 if [[ "$disk" == "$want" ]]; then
65 echo $disk
66 fi
67 ;;
68 esac
69 done
70
71 for disk in "${!diskname[@]}" ; do
72 case "$want" in
73 ~*)
74 PAT=${want:1}
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 SMIboot() {
95 DISK=$1
96 RDSK=/dev/rdsk/${DISK}p0
97 S2=/dev/rdsk/${DISK}s2
98 fdisk -B ${RDSK}
99 disks -C
100 prtvtoc -h ${RDSK} | awk '/./{p=0;} {if($1=="2"){size=$5;p=1;} if($1=="8"){start=$5;p=1;} if(p==1){print $1" "$2" "$3" "$4" "$5;}} END{size=size-start; print "0 2 00 "start" "size;}' | sort -n | fmthard -s /dev/stdin $S2
101 disks -C
102 }
103
104 BuildRpool() {
105 ztype=""
106 ztgt=""
107 disks=`ListDisksUnique $*`
108 log "Disks being used for rpool: $disks"
109 if [[ -z "$disks" ]]; then
110 bomb "No matching disks found to build rpool"
111 fi
112 for i in $disks
113 do
114 SMIboot $i
115 if [[ -n "$ztgt" ]]; then
116 ztype="mirror"
117 fi
118 ztgt="$ztgt ${i}s0"
119 INSTALL_GRUB_TGT="$INSTALL_GRUB_TGT /dev/rsdk/${i}s2"
120 done
121 log "zpool destroy rpool (just in case we've been run twice)"
122 zpool destroy rpool 2> /dev/null
123 log "Creating rpool with: zpool create -f rpool $ztype $ztgt"
124 zpool create -f rpool $ztype $ztgt || bomb "Failed to create rpool"
125 BuildBE
126 }
127 GetTargetVolSize() {
128 # Aim for 25% of physical memory (minimum 1G)
129 # prtconf always reports in megabytes
130 local mem=`/usr/sbin/prtconf | /bin/awk '/^Memory size/ { print $3 }'`
131 if [[ $mem -lt 4096 ]]; then
132 local vsize=1
133 else
134 local quart=`echo "scale=1;$mem/4096" | /bin/bc`
135 local vsize=`printf %0.f $quart`
136 fi
137 echo $vsize
138 }
139 GetRpoolFree() {
140 local zfsavail=`/sbin/zfs list -H -o avail rpool`
141 if [[ ${zfsavail:(-1)} = "G" ]]; then
142 local avail=`printf %0.f ${zfsavail::-1}`
143 elif [[ ${zfsavail:(-1)} = "T" ]]; then
144 local gigs=`echo "scale=1;${zfsavail::-1}*1024" | /bin/bc`
145 avail=`printf %0.f $gigs`
146 else
147 # If we get here, there's too little space left to be usable
148 avail=0
149 fi
150 echo $avail
151 }
152 MakeSwapDump() {
153 local size=`GetTargetVolSize`
154 local totalvols=""
155 local usable=""
156 local finalsize=""
157 local savecore=""
158
159 # We're creating both swap and dump volumes of the same size
160 let totalvols=${size}*2
161
162 # We want at least 10GB left free after swap/dump
163 # If we can't make swap/dump at least 1G each, don't bother
164 let usable=`GetRpoolFree`-10
165 if [[ $usable -lt 2 ]]; then
166 log "Not enough free space for reasonably-sized swap and dump; not creating either."
167 return 0
168 fi
169
170 # If the total of swap and dump is greater than the usable free space,
171 # make swap and dump each take half but don't enable savecore
172 if [[ $totalvols -ge $usable ]]; then
173 let finalsize=${usable}/2
174 savecore="-n"
175 else
176 finalsize=$size
177 savecore="-y"
178 fi
179
180 for volname in swap dump; do
181 /sbin/zfs create -V ${finalsize}G rpool/$volname || \
182 bomb "Failed to create rpool/$volname"
183 done
184 printf "/dev/zvol/dsk/rpool/swap\t-\t-\tswap\t-\tno\t-\n" >> $ALTROOT/etc/vfstab
185 Postboot /usr/sbin/dumpadm $savecore -d /dev/zvol/dsk/rpool/dump
186 return 0
187 }