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
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 }