fix sed -i invocations
[unleashed-kayak.git] / disk_help.sh
blob85829ca4bf30c51448c3a6eca6a7fc9d7908abcc
1 #!/usr/bin/bash
3 # CDDL HEADER START
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.
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.
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]
21 # CDDL HEADER END
24 # Copyright 2012 OmniTI Computer Consulting, Inc. All rights reserved.
25 # Use is subject to license terms.
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;}')
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
37 disk=""
38 while builtin read diskline
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
47 done < <(format < /dev/null | awk '/^ *[0-9]*\. /{print $2; print;}')
49 for want in $*
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
58 \<*)
59 if [[ -n ${disksize[$disk]} && "${disksize[$disk]}" -le "${want:1}" ]]; then
60 echo $disk
64 if [[ "$disk" == "$want" ]]; then
65 echo $disk
68 esac
69 done
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
79 esac
80 done
81 done
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;}}'
89 ListDisksUnique(){
90 for term in $*; do
91 ListDisksAnd $term
92 done | sort | uniq | xargs
95 BuildRpoolOnly() {
96 ztype=""
97 ztgt=""
98 disks=`ListDisksUnique $*`
99 log "Disks being used for root pool $RPOOL: $disks"
100 if [[ -z "$disks" ]]; then
101 bomb "No matching disks found to build root pool $RPOOL"
103 rm -f /tmp/kayak-disk-list
104 for i in $disks
106 if [[ -n "$ztgt" ]]; then
107 ztype="mirror"
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 root pool 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 root pool $RPOOL"
119 BuildRpool() {
120 BuildRpoolOnly $*
121 BuildBE
123 GetTargetVolSize() {
124 # Aim for 25% of physical memory (minimum 1G)
125 # prtconf always reports in megabytes
126 local mem=`/usr/sbin/prtconf | /bin/awk '/^Memory size/ { print $3 }'`
127 if [[ $mem -lt 4096 ]]; then
128 local vsize=1
129 else
130 local quart=`echo "scale=1;$mem/4096" | /bin/bc`
131 local vsize=`printf %0.f $quart`
133 echo $vsize
135 GetRpoolFree() {
136 local zfsavail=`/sbin/zfs list -H -o avail $RPOOL`
137 if [[ ${zfsavail:(-1)} = "G" ]]; then
138 local avail=`printf %0.f ${zfsavail::-1}`
139 elif [[ ${zfsavail:(-1)} = "T" ]]; then
140 local gigs=`echo "scale=1;${zfsavail::-1}*1024" | /bin/bc`
141 avail=`printf %0.f $gigs`
142 else
143 # If we get here, there's too little space left to be usable
144 avail=0
146 echo $avail
148 MakeSwapDump() {
149 local size=`GetTargetVolSize`
150 local totalvols=""
151 local usable=""
152 local finalsize=""
153 local savecore=""
155 # We're creating both swap and dump volumes of the same size
156 let totalvols=${size}*2
158 # We want at least 10GB left free after swap/dump
159 # If we can't make swap/dump at least 1G each, don't bother
160 let usable=`GetRpoolFree`-10
161 if [[ $usable -lt 2 ]]; then
162 log "Not enough free space for reasonably-sized swap and dump; not creating either."
163 return 0
166 # If the total of swap and dump is greater than the usable free space,
167 # make swap and dump each take half but don't enable savecore
168 if [[ $totalvols -ge $usable ]]; then
169 let finalsize=${usable}/2
170 savecore="-n"
171 else
172 finalsize=$size
173 savecore="-y"
176 for volname in swap dump; do
177 /sbin/zfs create -V ${finalsize}G $RPOOL/$volname || \
178 bomb "Failed to create $RPOOL/$volname"
179 done
180 printf "/dev/zvol/dsk/$RPOOL/swap\t-\t-\tswap\t-\tno\t-\n" >> $ALTROOT/etc/vfstab
181 Postboot /usr/sbin/dumpadm $savecore -d /dev/zvol/dsk/$RPOOL/dump
182 return 0