Deleted Added
full compact
functions-disk.sh (217164) functions-disk.sh (217229)
1#!/bin/sh
2#-
3# Copyright (c) 2010 iXsystems, Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11# notice, this list of conditions and the following disclaimer in the
12# documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
1#!/bin/sh
2#-
3# Copyright (c) 2010 iXsystems, Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11# notice, this list of conditions and the following disclaimer in the
12# documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD: head/usr.sbin/pc-sysinstall/backend/functions-disk.sh 217164 2011-01-08 20:25:00Z jpaetzel $
26# $FreeBSD: head/usr.sbin/pc-sysinstall/backend/functions-disk.sh 217229 2011-01-10 19:11:25Z jpaetzel $
27
28# Functions related to disk operations using gpart
29
30# See if device is a full disk or partition/slice
31is_disk()
32{
33 for _dsk in `sysctl -n kern.disks`
34 do
35 if [ "$_dsk" = "${1}" ] ; then return 0 ; fi
36 done
37
38 return 1
39}
40
41# Get a MBR partitions sysid
42get_partition_sysid_mbr()
43{
44 INPART="0"
45 DISK="$1"
46 PARTNUM=`echo ${2} | sed "s|${DISK}s||g"`
47 fdisk ${DISK} >${TMPDIR}/disk-${DISK} 2>/dev/null
48 while read i
49 do
50 echo "$i" | grep "The data for partition" >/dev/null 2>/dev/null
51 if [ "$?" = "0" ] ; then
52 INPART="0"
53 PART="`echo ${i} | cut -d ' ' -f 5`"
54 if [ "$PART" = "$PARTNUM" ] ; then
55 INPART="1"
56 fi
57 fi
58
59 # In the partition section
60 if [ "$INPART" = "1" ] ; then
61 echo "$i" | grep "^sysid" >/dev/null 2>/dev/null
62 if [ "$?" = "0" ] ; then
63 SYSID="`echo ${i} | tr -s '\t' ' ' | cut -d ' ' -f 2`"
64 break
65 fi
66
67 fi
68
69 done < ${TMPDIR}/disk-${DISK}
70 rm ${TMPDIR}/disk-${DISK}
71
72 VAL="${SYSID}"
73 export VAL
74};
75
76# Get the partitions MBR label
77get_partition_label_mbr()
78{
79 INPART="0"
80 DISK="$1"
81 PARTNUM=`echo ${2} | sed "s|${DISK}s||g"`
82 fdisk ${DISK} >${TMPDIR}/disk-${DISK} 2>/dev/null
83 while read i
84 do
85 echo "$i" | grep "The data for partition" >/dev/null 2>/dev/null
86 if [ "$?" = "0" ] ; then
87 INPART="0"
88 PART="`echo ${i} | cut -d ' ' -f 5`"
89 if [ "$PART" = "$PARTNUM" ] ; then
90 INPART="1"
91 fi
92 fi
93
94 # In the partition section
95 if [ "$INPART" = "1" ] ; then
96 echo "$i" | grep "^sysid" >/dev/null 2>/dev/null
97 if [ "$?" = "0" ] ; then
98 LABEL="`echo ${i} | tr -s '\t' ' ' | cut -d ',' -f 2-10`"
99 break
100 fi
101
102 fi
103
104 done < ${TMPDIR}/disk-${DISK}
105 rm ${TMPDIR}/disk-${DISK}
106
107 VAL="${LABEL}"
108 export VAL
109};
110
111# Get a GPT partitions label
112get_partition_label_gpt()
113{
114 DISK="${1}"
115 PARTNUM=`echo ${2} | sed "s|${DISK}p||g"`
116
117 gpart show ${DISK} >${TMPDIR}/disk-${DISK}
118 while read i
119 do
120 SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
121 if [ "${SLICE}" = "${PARTNUM}" ] ; then
122 LABEL="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 4`"
123 break
124 fi
125 done <${TMPDIR}/disk-${DISK}
126 rm ${TMPDIR}/disk-${DISK}
127
128 VAL="${LABEL}"
129 export VAL
130};
131
132# Get a partitions startblock
133get_partition_startblock()
134{
135 DISK="${1}"
136 PARTNUM=`echo ${2} | sed "s|${DISK}p||g" | sed "s|${DISK}s||g"`
137
138 gpart show ${DISK} >${TMPDIR}/disk-${DISK}
139 while read i
140 do
141 SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
142 if [ "$SLICE" = "${PARTNUM}" ] ; then
143 SB="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 1`"
144 break
145 fi
146 done <${TMPDIR}/disk-${DISK}
147 rm ${TMPDIR}/disk-${DISK}
148
149 VAL="${SB}"
150 export VAL
151};
152
153# Get a partitions blocksize
154get_partition_blocksize()
155{
156 DISK="${1}"
157 PARTNUM=`echo ${2} | sed "s|${DISK}p||g" | sed "s|${DISK}s||g"`
158
159 gpart show ${DISK} >${TMPDIR}/disk-${DISK}
160 while read i
161 do
162 SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
163 if [ "$SLICE" = "${PARTNUM}" ] ; then
164 BS="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 2`"
165 break
166 fi
167 done <${TMPDIR}/disk-${DISK}
168 rm ${TMPDIR}/disk-${DISK}
169
170 VAL="${BS}"
171 export VAL
172};
173
174# Function which returns the partitions on a target disk
175get_disk_partitions()
176{
177 gpart show ${1} >/dev/null 2>/dev/null
178 if [ "$?" != "0" ] ; then
179 VAL="" ; export VAL
180 return
181 fi
182
183 type=`gpart show ${1} | awk '/^=>/ { printf("%s",$5); }'`
184
185 SLICES="`gpart show ${1} | grep -v ${1} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 4 | sed '/^$/d'`"
186 for i in ${SLICES}
187 do
188 case $type in
189 MBR) name="${1}s${i}" ;;
190 GPT) name="${1}p${i}";;
191 *) name="${1}s${i}";;
192 esac
193 if [ -z "${RSLICES}" ]
194 then
195 RSLICES="${name}"
196 else
197 RSLICES="${RSLICES} ${name}"
198 fi
199 done
200
201 VAL="${RSLICES}" ; export VAL
202};
203
204# Function which returns a target disks cylinders
205get_disk_cyl()
206{
207 cyl=`diskinfo -v ${1} | grep "# Cylinders" | tr -s ' ' | cut -f 2`
208 VAL="${cyl}" ; export VAL
209};
210
211# Function which returns a target disks sectors
212get_disk_sectors()
213{
214 sec=`diskinfo -v ${1} | grep "# Sectors" | tr -s ' ' | cut -f 2`
215 VAL="${sec}" ; export VAL
216};
217
218# Function which returns a target disks heads
219get_disk_heads()
220{
221 head=`diskinfo -v ${1} | grep "# Heads" | tr -s ' ' | cut -f 2`
222 VAL="${head}" ; export VAL
223};
224
225# Function which returns a target disks mediasize in sectors
226get_disk_mediasize()
227{
228 mediasize=`diskinfo -v ${1} | grep "# mediasize in sectors" | tr -s ' ' | cut -f 2`
229 VAL="${mediasize}" ; export VAL
230};
231
232# Function which exports all zpools, making them safe to overwrite potentially
233export_all_zpools()
234{
235 # Export any zpools
236 for i in `zpool list -H -o name`
237 do
238 zpool export -f ${i}
239 done
240};
241
242# Function to delete all gparts before starting an install
243delete_all_gpart()
244{
245 echo_log "Deleting all gparts"
27
28# Functions related to disk operations using gpart
29
30# See if device is a full disk or partition/slice
31is_disk()
32{
33 for _dsk in `sysctl -n kern.disks`
34 do
35 if [ "$_dsk" = "${1}" ] ; then return 0 ; fi
36 done
37
38 return 1
39}
40
41# Get a MBR partitions sysid
42get_partition_sysid_mbr()
43{
44 INPART="0"
45 DISK="$1"
46 PARTNUM=`echo ${2} | sed "s|${DISK}s||g"`
47 fdisk ${DISK} >${TMPDIR}/disk-${DISK} 2>/dev/null
48 while read i
49 do
50 echo "$i" | grep "The data for partition" >/dev/null 2>/dev/null
51 if [ "$?" = "0" ] ; then
52 INPART="0"
53 PART="`echo ${i} | cut -d ' ' -f 5`"
54 if [ "$PART" = "$PARTNUM" ] ; then
55 INPART="1"
56 fi
57 fi
58
59 # In the partition section
60 if [ "$INPART" = "1" ] ; then
61 echo "$i" | grep "^sysid" >/dev/null 2>/dev/null
62 if [ "$?" = "0" ] ; then
63 SYSID="`echo ${i} | tr -s '\t' ' ' | cut -d ' ' -f 2`"
64 break
65 fi
66
67 fi
68
69 done < ${TMPDIR}/disk-${DISK}
70 rm ${TMPDIR}/disk-${DISK}
71
72 VAL="${SYSID}"
73 export VAL
74};
75
76# Get the partitions MBR label
77get_partition_label_mbr()
78{
79 INPART="0"
80 DISK="$1"
81 PARTNUM=`echo ${2} | sed "s|${DISK}s||g"`
82 fdisk ${DISK} >${TMPDIR}/disk-${DISK} 2>/dev/null
83 while read i
84 do
85 echo "$i" | grep "The data for partition" >/dev/null 2>/dev/null
86 if [ "$?" = "0" ] ; then
87 INPART="0"
88 PART="`echo ${i} | cut -d ' ' -f 5`"
89 if [ "$PART" = "$PARTNUM" ] ; then
90 INPART="1"
91 fi
92 fi
93
94 # In the partition section
95 if [ "$INPART" = "1" ] ; then
96 echo "$i" | grep "^sysid" >/dev/null 2>/dev/null
97 if [ "$?" = "0" ] ; then
98 LABEL="`echo ${i} | tr -s '\t' ' ' | cut -d ',' -f 2-10`"
99 break
100 fi
101
102 fi
103
104 done < ${TMPDIR}/disk-${DISK}
105 rm ${TMPDIR}/disk-${DISK}
106
107 VAL="${LABEL}"
108 export VAL
109};
110
111# Get a GPT partitions label
112get_partition_label_gpt()
113{
114 DISK="${1}"
115 PARTNUM=`echo ${2} | sed "s|${DISK}p||g"`
116
117 gpart show ${DISK} >${TMPDIR}/disk-${DISK}
118 while read i
119 do
120 SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
121 if [ "${SLICE}" = "${PARTNUM}" ] ; then
122 LABEL="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 4`"
123 break
124 fi
125 done <${TMPDIR}/disk-${DISK}
126 rm ${TMPDIR}/disk-${DISK}
127
128 VAL="${LABEL}"
129 export VAL
130};
131
132# Get a partitions startblock
133get_partition_startblock()
134{
135 DISK="${1}"
136 PARTNUM=`echo ${2} | sed "s|${DISK}p||g" | sed "s|${DISK}s||g"`
137
138 gpart show ${DISK} >${TMPDIR}/disk-${DISK}
139 while read i
140 do
141 SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
142 if [ "$SLICE" = "${PARTNUM}" ] ; then
143 SB="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 1`"
144 break
145 fi
146 done <${TMPDIR}/disk-${DISK}
147 rm ${TMPDIR}/disk-${DISK}
148
149 VAL="${SB}"
150 export VAL
151};
152
153# Get a partitions blocksize
154get_partition_blocksize()
155{
156 DISK="${1}"
157 PARTNUM=`echo ${2} | sed "s|${DISK}p||g" | sed "s|${DISK}s||g"`
158
159 gpart show ${DISK} >${TMPDIR}/disk-${DISK}
160 while read i
161 do
162 SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
163 if [ "$SLICE" = "${PARTNUM}" ] ; then
164 BS="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 2`"
165 break
166 fi
167 done <${TMPDIR}/disk-${DISK}
168 rm ${TMPDIR}/disk-${DISK}
169
170 VAL="${BS}"
171 export VAL
172};
173
174# Function which returns the partitions on a target disk
175get_disk_partitions()
176{
177 gpart show ${1} >/dev/null 2>/dev/null
178 if [ "$?" != "0" ] ; then
179 VAL="" ; export VAL
180 return
181 fi
182
183 type=`gpart show ${1} | awk '/^=>/ { printf("%s",$5); }'`
184
185 SLICES="`gpart show ${1} | grep -v ${1} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 4 | sed '/^$/d'`"
186 for i in ${SLICES}
187 do
188 case $type in
189 MBR) name="${1}s${i}" ;;
190 GPT) name="${1}p${i}";;
191 *) name="${1}s${i}";;
192 esac
193 if [ -z "${RSLICES}" ]
194 then
195 RSLICES="${name}"
196 else
197 RSLICES="${RSLICES} ${name}"
198 fi
199 done
200
201 VAL="${RSLICES}" ; export VAL
202};
203
204# Function which returns a target disks cylinders
205get_disk_cyl()
206{
207 cyl=`diskinfo -v ${1} | grep "# Cylinders" | tr -s ' ' | cut -f 2`
208 VAL="${cyl}" ; export VAL
209};
210
211# Function which returns a target disks sectors
212get_disk_sectors()
213{
214 sec=`diskinfo -v ${1} | grep "# Sectors" | tr -s ' ' | cut -f 2`
215 VAL="${sec}" ; export VAL
216};
217
218# Function which returns a target disks heads
219get_disk_heads()
220{
221 head=`diskinfo -v ${1} | grep "# Heads" | tr -s ' ' | cut -f 2`
222 VAL="${head}" ; export VAL
223};
224
225# Function which returns a target disks mediasize in sectors
226get_disk_mediasize()
227{
228 mediasize=`diskinfo -v ${1} | grep "# mediasize in sectors" | tr -s ' ' | cut -f 2`
229 VAL="${mediasize}" ; export VAL
230};
231
232# Function which exports all zpools, making them safe to overwrite potentially
233export_all_zpools()
234{
235 # Export any zpools
236 for i in `zpool list -H -o name`
237 do
238 zpool export -f ${i}
239 done
240};
241
242# Function to delete all gparts before starting an install
243delete_all_gpart()
244{
245 echo_log "Deleting all gparts"
246 DISK="$1"
246 local DISK="$1"
247
248 # Check for any swaps to stop
249 for i in `gpart show ${DISK} 2>/dev/null | grep 'freebsd-swap' | tr -s ' ' | cut -d ' ' -f 4`
250 do
251 swapoff /dev/${DISK}s${i}b >/dev/null 2>/dev/null
252 swapoff /dev/${DISK}p${i} >/dev/null 2>/dev/null
253 done
254
255 # Delete the gparts now
256 for i in `gpart show ${DISK} 2>/dev/null | tr -s ' ' | cut -d ' ' -f 4`
257 do
258 if [ "${i}" != "${DISK}" -a "${i}" != "-" ] ; then
259 rc_nohalt "gpart delete -i ${i} ${DISK}"
260 fi
261 done
262
247
248 # Check for any swaps to stop
249 for i in `gpart show ${DISK} 2>/dev/null | grep 'freebsd-swap' | tr -s ' ' | cut -d ' ' -f 4`
250 do
251 swapoff /dev/${DISK}s${i}b >/dev/null 2>/dev/null
252 swapoff /dev/${DISK}p${i} >/dev/null 2>/dev/null
253 done
254
255 # Delete the gparts now
256 for i in `gpart show ${DISK} 2>/dev/null | tr -s ' ' | cut -d ' ' -f 4`
257 do
258 if [ "${i}" != "${DISK}" -a "${i}" != "-" ] ; then
259 rc_nohalt "gpart delete -i ${i} ${DISK}"
260 fi
261 done
262
263 # Destroy the disk geom
264 rc_nohalt "gpart destroy ${DISK}"
265
266 # Make sure we clear any hidden gpt tables
267 clear_backup_gpt_table "${DISK}"
268
269 # Wipe out front of disk
263 rc_nohalt "dd if=/dev/zero of=/dev/${DISK} count=3000"
264
265};
266
267# Function to export all zpools before starting an install
268stop_all_zfs()
269{
270 # Export all zpools again, so that we can overwrite these partitions potentially
271 for i in `zpool list -H -o name`
272 do
273 zpool export -f ${i}
274 done
275};
276
277# Function which stops all gmirrors before doing any disk manipulation
278stop_all_gmirror()
279{
280 DISK="${1}"
281 GPROV="`gmirror list | grep ". Name: mirror/" | cut -d '/' -f 2`"
282 for gprov in $GPROV
283 do
284 gmirror list | grep "Name: ${DISK}" >/dev/null 2>/dev/null
285 if [ "$?" = "0" ]
286 then
287 echo_log "Stopping mirror $gprov $DISK"
288 rc_nohalt "gmirror remove $gprov $DISK"
289 rc_nohalt "dd if=/dev/zero of=/dev/${DISK} count=4096"
290 fi
291 done
292};
293
294# Make sure we don't have any geli providers active on this disk
295stop_all_geli()
296{
297 _geld="${1}"
298 cd /dev
299
300 for i in `ls ${_geld}*`
301 do
302 echo $i | grep '.eli' >/dev/null 2>/dev/null
303 if [ "$?" = "0" ]
304 then
305 echo_log "Detaching GELI on ${i}"
306 rc_halt "geli detach ${i}"
307 fi
308 done
309
310};
311
312# Function which reads in the disk slice config, and performs it
313setup_disk_slice()
314{
315
316 # Cleanup any slice / mirror dirs
317 rm -rf ${SLICECFGDIR} >/dev/null 2>/dev/null
318 mkdir ${SLICECFGDIR}
319 rm -rf ${MIRRORCFGDIR} >/dev/null 2>/dev/null
320 mkdir ${MIRRORCFGDIR}
321
322 # Start with disk0
323 disknum="0"
324
325 # Make sure all zpools are exported
326 export_all_zpools
327
328 # We are ready to start setting up the disks, lets read the config and do the actions
329 while read line
330 do
331 echo $line | grep "^disk${disknum}=" >/dev/null 2>/dev/null
332 if [ "$?" = "0" ]
333 then
334
335 # Found a disk= entry, lets get the disk we are working on
336 get_value_from_string "${line}"
337 strip_white_space "$VAL"
338 DISK="$VAL"
339
340 # Before we go further, lets confirm this disk really exists
341 if [ ! -e "/dev/${DISK}" ]
342 then
343 exit_err "ERROR: The disk ${DISK} does not exist!"
344 fi
345
346 # Make sure we stop any gmirrors on this disk
347 stop_all_gmirror ${DISK}
348
349 # Make sure we stop any geli stuff on this disk
350 stop_all_geli ${DISK}
351
352 # Make sure we don't have any zpools loaded
353 stop_all_zfs
354
355 fi
356
357 # Lets look if this device will be mirrored on another disk
358 echo $line | grep "^mirror=" >/dev/null 2>/dev/null
359 if [ "$?" = "0" ]
360 then
361
362 # Found a disk= entry, lets get the disk we are working on
363 get_value_from_string "${line}"
364 strip_white_space "$VAL"
365 MIRRORDISK="$VAL"
366
367 # Before we go further, lets confirm this disk really exists
368 if [ ! -e "/dev/${MIRRORDISK}" ]
369 then
370 exit_err "ERROR: The mirror disk ${MIRRORDISK} does not exist!"
371 fi
372 fi
373
374 # Lets see if we have been given a mirror balance choice
375 echo $line | grep "^mirrorbal=" >/dev/null 2>/dev/null
376 if [ "$?" = "0" ]
377 then
378
379 # Found a disk= entry, lets get the disk we are working on
380 get_value_from_string "${line}"
381 strip_white_space "$VAL"
382 MIRRORBAL="$VAL"
383 fi
384
385 echo $line | grep "^partition=" >/dev/null 2>/dev/null
386 if [ "$?" = "0" ]
387 then
388 # Found a partition= entry, lets read / set it
389 get_value_from_string "${line}"
390 strip_white_space "$VAL"
391 PTYPE=`echo $VAL|tr A-Z a-z`
392
393 # We are using free space, figure out the slice number
394 if [ "${PTYPE}" = "free" ]
395 then
396 # Lets figure out what number this slice will be
397 LASTSLICE="`gpart show ${DISK} \
398 | grep -v ${DISK} \
399 | grep -v ' free' \
400 | tr -s '\t' ' ' \
401 | cut -d ' ' -f 4 \
402 | sed '/^$/d' \
403 | tail -n 1`"
404
405 if [ -z "${LASTSLICE}" ]
406 then
407 LASTSLICE="1"
408 else
409 LASTSLICE="`expr $LASTSLICE + 1`"
410 fi
411
412 if [ $LASTSLICE -gt 4 ]
413 then
414 exit_err "ERROR: BSD only supports primary partitions, and there are none availble on $DISK"
415 fi
416
417 fi
418 fi
419
420 # Check if we have an image file defined
421 echo $line | grep "^image=" >/dev/null 2>/dev/null
422 if [ "$?" = "0" ] ; then
423 # Found an image= entry, lets read / set it
424 get_value_from_string "${line}"
425 strip_white_space "$VAL"
426 IMAGE="$VAL"
427 if [ ! -f "$IMAGE" ] ; then
428 exit_err "$IMAGE file does not exist"
429 fi
430 fi
431
432 # Check if we have a partscheme specified
433 echo $line | grep "^partscheme=" >/dev/null 2>/dev/null
434 if [ "$?" = "0" ] ; then
435 # Found a partscheme= entry, lets read / set it
436 get_value_from_string "${line}"
437 strip_white_space "$VAL"
438 PSCHEME="$VAL"
439 if [ "$PSCHEME" != "GPT" -a "$PSCHEME" != "MBR" ] ; then
440 exit_err "Unknown partition scheme: $PSCHEME"
441 fi
442 fi
443
444 echo $line | grep "^bootManager=" >/dev/null 2>/dev/null
445 if [ "$?" = "0" ]
446 then
447 # Found a bootManager= entry, lets read /set it
448 get_value_from_string "${line}"
449 strip_white_space "$VAL"
450 BMANAGER="$VAL"
451 fi
452
453 echo $line | grep "^commitDiskPart" >/dev/null 2>/dev/null
454 if [ "$?" = "0" ]
455 then
456 # Found our flag to commit this disk setup / lets do sanity check and do it
457 if [ ! -z "${DISK}" -a ! -z "${PTYPE}" ]
458 then
459 case ${PTYPE} in
460 all)
461 if [ "$PSCHEME" = "MBR" -o -z "$PSCHEME" ] ; then
462 PSCHEME="MBR"
463 tmpSLICE="${DISK}s1"
464 else
465 tmpSLICE="${DISK}p1"
466 fi
467
468 run_gpart_full "${DISK}" "${BMANAGER}" "${PSCHEME}"
469 ;;
470
471 s1|s2|s3|s4)
472 tmpSLICE="${DISK}${PTYPE}"
473 # Get the number of the slice we are working on
474 s="`echo ${PTYPE} | awk '{print substr($0,length,1)}'`"
475 run_gpart_slice "${DISK}" "${BMANAGER}" "${s}"
476 ;;
477
478 free)
479 tmpSLICE="${DISK}s${LASTSLICE}"
480 run_gpart_free "${DISK}" "${LASTSLICE}" "${BMANAGER}"
481 ;;
482
483 image)
484 if [ -z "${IMAGE}" ]
485 then
486 exit_err "ERROR: partition type image specified with no image!"
487 fi
488 ;;
489
490 *) exit_err "ERROR: Unknown PTYPE: $PTYPE" ;;
491 esac
492
493
494 if [ -n "${IMAGE}" ]
495 then
496 local DEST
497
498 if [ -n "${tmpSLICE}" ]
499 then
500 DEST="${tmpSLICE}"
501 else
502 DEST="${DISK}"
503 fi
504
505 write_image "${IMAGE}" "${DEST}"
506 check_disk_layout "${DEST}"
507 fi
508
509 # Now save which disk<num> this is, so we can parse it later during slice partition setup
510 if [ -z "${IMAGE}" ]
511 then
512 echo "disk${disknum}" >${SLICECFGDIR}/$tmpSLICE
513 fi
514
515 # Save any mirror config
516 if [ ! -z "$MIRRORDISK" ]
517 then
518 # Default to round-robin if the user didn't specify
519 if [ -z "$MIRRORBAL" ]
520 then
521 MIRRORBAL="round-robin"
522 fi
523 echo "$MIRRORDISK:$MIRRORBAL" >${MIRRORCFGDIR}/$DISK
524 fi
525
526 # Increment our disk counter to look for next disk and unset
527 unset BMANAGER PTYPE DISK MIRRORDISK MIRRORBAL PSCHEME IMAGE
528 disknum="`expr $disknum + 1`"
529 else
530 exit_err "ERROR: commitDiskPart was called without procceding disk<num>= and partition= entries!!!"
531 fi
532 fi
533
534 done <${CFGF}
535
536};
537
538# Stop all gjournals on disk / slice
539stop_gjournal()
540{
541 _gdsk="$1"
542 # Check if we need to shutdown any journals on this drive
543 ls /dev/${_gdsk}*.journal >/dev/null 2>/dev/null
544 if [ "$?" = "0" ]
545 then
546 cd /dev
547 for i in `ls ${_gdsk}*.journal`
548 do
549 rawjournal="`echo ${i} | cut -d '.' -f 1`"
550 gjournal stop -f ${rawjournal} >>${LOGOUT} 2>>${LOGOUT}
551 gjournal clear ${rawjournal} >>${LOGOUT} 2>>${LOGOUT}
552 done
553 fi
554} ;
555
270 rc_nohalt "dd if=/dev/zero of=/dev/${DISK} count=3000"
271
272};
273
274# Function to export all zpools before starting an install
275stop_all_zfs()
276{
277 # Export all zpools again, so that we can overwrite these partitions potentially
278 for i in `zpool list -H -o name`
279 do
280 zpool export -f ${i}
281 done
282};
283
284# Function which stops all gmirrors before doing any disk manipulation
285stop_all_gmirror()
286{
287 DISK="${1}"
288 GPROV="`gmirror list | grep ". Name: mirror/" | cut -d '/' -f 2`"
289 for gprov in $GPROV
290 do
291 gmirror list | grep "Name: ${DISK}" >/dev/null 2>/dev/null
292 if [ "$?" = "0" ]
293 then
294 echo_log "Stopping mirror $gprov $DISK"
295 rc_nohalt "gmirror remove $gprov $DISK"
296 rc_nohalt "dd if=/dev/zero of=/dev/${DISK} count=4096"
297 fi
298 done
299};
300
301# Make sure we don't have any geli providers active on this disk
302stop_all_geli()
303{
304 _geld="${1}"
305 cd /dev
306
307 for i in `ls ${_geld}*`
308 do
309 echo $i | grep '.eli' >/dev/null 2>/dev/null
310 if [ "$?" = "0" ]
311 then
312 echo_log "Detaching GELI on ${i}"
313 rc_halt "geli detach ${i}"
314 fi
315 done
316
317};
318
319# Function which reads in the disk slice config, and performs it
320setup_disk_slice()
321{
322
323 # Cleanup any slice / mirror dirs
324 rm -rf ${SLICECFGDIR} >/dev/null 2>/dev/null
325 mkdir ${SLICECFGDIR}
326 rm -rf ${MIRRORCFGDIR} >/dev/null 2>/dev/null
327 mkdir ${MIRRORCFGDIR}
328
329 # Start with disk0
330 disknum="0"
331
332 # Make sure all zpools are exported
333 export_all_zpools
334
335 # We are ready to start setting up the disks, lets read the config and do the actions
336 while read line
337 do
338 echo $line | grep "^disk${disknum}=" >/dev/null 2>/dev/null
339 if [ "$?" = "0" ]
340 then
341
342 # Found a disk= entry, lets get the disk we are working on
343 get_value_from_string "${line}"
344 strip_white_space "$VAL"
345 DISK="$VAL"
346
347 # Before we go further, lets confirm this disk really exists
348 if [ ! -e "/dev/${DISK}" ]
349 then
350 exit_err "ERROR: The disk ${DISK} does not exist!"
351 fi
352
353 # Make sure we stop any gmirrors on this disk
354 stop_all_gmirror ${DISK}
355
356 # Make sure we stop any geli stuff on this disk
357 stop_all_geli ${DISK}
358
359 # Make sure we don't have any zpools loaded
360 stop_all_zfs
361
362 fi
363
364 # Lets look if this device will be mirrored on another disk
365 echo $line | grep "^mirror=" >/dev/null 2>/dev/null
366 if [ "$?" = "0" ]
367 then
368
369 # Found a disk= entry, lets get the disk we are working on
370 get_value_from_string "${line}"
371 strip_white_space "$VAL"
372 MIRRORDISK="$VAL"
373
374 # Before we go further, lets confirm this disk really exists
375 if [ ! -e "/dev/${MIRRORDISK}" ]
376 then
377 exit_err "ERROR: The mirror disk ${MIRRORDISK} does not exist!"
378 fi
379 fi
380
381 # Lets see if we have been given a mirror balance choice
382 echo $line | grep "^mirrorbal=" >/dev/null 2>/dev/null
383 if [ "$?" = "0" ]
384 then
385
386 # Found a disk= entry, lets get the disk we are working on
387 get_value_from_string "${line}"
388 strip_white_space "$VAL"
389 MIRRORBAL="$VAL"
390 fi
391
392 echo $line | grep "^partition=" >/dev/null 2>/dev/null
393 if [ "$?" = "0" ]
394 then
395 # Found a partition= entry, lets read / set it
396 get_value_from_string "${line}"
397 strip_white_space "$VAL"
398 PTYPE=`echo $VAL|tr A-Z a-z`
399
400 # We are using free space, figure out the slice number
401 if [ "${PTYPE}" = "free" ]
402 then
403 # Lets figure out what number this slice will be
404 LASTSLICE="`gpart show ${DISK} \
405 | grep -v ${DISK} \
406 | grep -v ' free' \
407 | tr -s '\t' ' ' \
408 | cut -d ' ' -f 4 \
409 | sed '/^$/d' \
410 | tail -n 1`"
411
412 if [ -z "${LASTSLICE}" ]
413 then
414 LASTSLICE="1"
415 else
416 LASTSLICE="`expr $LASTSLICE + 1`"
417 fi
418
419 if [ $LASTSLICE -gt 4 ]
420 then
421 exit_err "ERROR: BSD only supports primary partitions, and there are none availble on $DISK"
422 fi
423
424 fi
425 fi
426
427 # Check if we have an image file defined
428 echo $line | grep "^image=" >/dev/null 2>/dev/null
429 if [ "$?" = "0" ] ; then
430 # Found an image= entry, lets read / set it
431 get_value_from_string "${line}"
432 strip_white_space "$VAL"
433 IMAGE="$VAL"
434 if [ ! -f "$IMAGE" ] ; then
435 exit_err "$IMAGE file does not exist"
436 fi
437 fi
438
439 # Check if we have a partscheme specified
440 echo $line | grep "^partscheme=" >/dev/null 2>/dev/null
441 if [ "$?" = "0" ] ; then
442 # Found a partscheme= entry, lets read / set it
443 get_value_from_string "${line}"
444 strip_white_space "$VAL"
445 PSCHEME="$VAL"
446 if [ "$PSCHEME" != "GPT" -a "$PSCHEME" != "MBR" ] ; then
447 exit_err "Unknown partition scheme: $PSCHEME"
448 fi
449 fi
450
451 echo $line | grep "^bootManager=" >/dev/null 2>/dev/null
452 if [ "$?" = "0" ]
453 then
454 # Found a bootManager= entry, lets read /set it
455 get_value_from_string "${line}"
456 strip_white_space "$VAL"
457 BMANAGER="$VAL"
458 fi
459
460 echo $line | grep "^commitDiskPart" >/dev/null 2>/dev/null
461 if [ "$?" = "0" ]
462 then
463 # Found our flag to commit this disk setup / lets do sanity check and do it
464 if [ ! -z "${DISK}" -a ! -z "${PTYPE}" ]
465 then
466 case ${PTYPE} in
467 all)
468 if [ "$PSCHEME" = "MBR" -o -z "$PSCHEME" ] ; then
469 PSCHEME="MBR"
470 tmpSLICE="${DISK}s1"
471 else
472 tmpSLICE="${DISK}p1"
473 fi
474
475 run_gpart_full "${DISK}" "${BMANAGER}" "${PSCHEME}"
476 ;;
477
478 s1|s2|s3|s4)
479 tmpSLICE="${DISK}${PTYPE}"
480 # Get the number of the slice we are working on
481 s="`echo ${PTYPE} | awk '{print substr($0,length,1)}'`"
482 run_gpart_slice "${DISK}" "${BMANAGER}" "${s}"
483 ;;
484
485 free)
486 tmpSLICE="${DISK}s${LASTSLICE}"
487 run_gpart_free "${DISK}" "${LASTSLICE}" "${BMANAGER}"
488 ;;
489
490 image)
491 if [ -z "${IMAGE}" ]
492 then
493 exit_err "ERROR: partition type image specified with no image!"
494 fi
495 ;;
496
497 *) exit_err "ERROR: Unknown PTYPE: $PTYPE" ;;
498 esac
499
500
501 if [ -n "${IMAGE}" ]
502 then
503 local DEST
504
505 if [ -n "${tmpSLICE}" ]
506 then
507 DEST="${tmpSLICE}"
508 else
509 DEST="${DISK}"
510 fi
511
512 write_image "${IMAGE}" "${DEST}"
513 check_disk_layout "${DEST}"
514 fi
515
516 # Now save which disk<num> this is, so we can parse it later during slice partition setup
517 if [ -z "${IMAGE}" ]
518 then
519 echo "disk${disknum}" >${SLICECFGDIR}/$tmpSLICE
520 fi
521
522 # Save any mirror config
523 if [ ! -z "$MIRRORDISK" ]
524 then
525 # Default to round-robin if the user didn't specify
526 if [ -z "$MIRRORBAL" ]
527 then
528 MIRRORBAL="round-robin"
529 fi
530 echo "$MIRRORDISK:$MIRRORBAL" >${MIRRORCFGDIR}/$DISK
531 fi
532
533 # Increment our disk counter to look for next disk and unset
534 unset BMANAGER PTYPE DISK MIRRORDISK MIRRORBAL PSCHEME IMAGE
535 disknum="`expr $disknum + 1`"
536 else
537 exit_err "ERROR: commitDiskPart was called without procceding disk<num>= and partition= entries!!!"
538 fi
539 fi
540
541 done <${CFGF}
542
543};
544
545# Stop all gjournals on disk / slice
546stop_gjournal()
547{
548 _gdsk="$1"
549 # Check if we need to shutdown any journals on this drive
550 ls /dev/${_gdsk}*.journal >/dev/null 2>/dev/null
551 if [ "$?" = "0" ]
552 then
553 cd /dev
554 for i in `ls ${_gdsk}*.journal`
555 do
556 rawjournal="`echo ${i} | cut -d '.' -f 1`"
557 gjournal stop -f ${rawjournal} >>${LOGOUT} 2>>${LOGOUT}
558 gjournal clear ${rawjournal} >>${LOGOUT} 2>>${LOGOUT}
559 done
560 fi
561} ;
562
563
564# Function to wipe the potential backup gpt table from a disk
565clear_backup_gpt_table()
566{
567 # Get the disk block size
568 local dSize="`gpart show $1 | grep $1 | tr -s ' ' | cut -d ' ' -f 3`"
569
570 # Make sure this is a valid number
571 is_num "${dSize}" >/dev/null 2>/dev/null
572 if [ "$?" != "0" ] ; then return ; fi
573
574 # Die backup label, DIE
575 echo_log "Clearing gpt backup table location on disk"
576 rc_nohalt "dd if=/dev/zero of=${1} bs=512 seek=${dSize}"
577
578} ;
579
580
556# Function which runs gpart and creates a single large GPT partition scheme
557init_gpt_full_disk()
558{
559 _intDISK=$1
560
561 # Set our sysctl so we can overwrite any geom using drives
562 sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
563
564 # Stop any journaling
565 stop_gjournal "${_intDISK}"
566
567 # Remove any existing partitions
568 delete_all_gpart "${_intDISK}"
569
581# Function which runs gpart and creates a single large GPT partition scheme
582init_gpt_full_disk()
583{
584 _intDISK=$1
585
586 # Set our sysctl so we can overwrite any geom using drives
587 sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
588
589 # Stop any journaling
590 stop_gjournal "${_intDISK}"
591
592 # Remove any existing partitions
593 delete_all_gpart "${_intDISK}"
594
570 #Erase any existing bootloader
571 echo_log "Cleaning up ${_intDISK}"
572 rc_halt "dd if=/dev/zero of=/dev/${_intDISK} count=2048"
573
574 sleep 2
575
576 echo_log "Running gpart on ${_intDISK}"
577 rc_halt "gpart create -s GPT ${_intDISK}"
595 sleep 2
596
597 echo_log "Running gpart on ${_intDISK}"
598 rc_halt "gpart create -s GPT ${_intDISK}"
578 rc_halt "gpart add -b 34 -s 128 -t freebsd-boot ${_intDISK}"
599 rc_halt "gpart add -b 34 -s 64 -t freebsd-boot ${_intDISK}"
579
580 echo_log "Stamping boot sector on ${_intDISK}"
581 rc_halt "gpart bootcode -b /boot/pmbr ${_intDISK}"
582
583}
584
585# Function which runs gpart and creates a single large MBR partition scheme
586init_mbr_full_disk()
587{
588 _intDISK=$1
589 _intBOOT=$2
590
600
601 echo_log "Stamping boot sector on ${_intDISK}"
602 rc_halt "gpart bootcode -b /boot/pmbr ${_intDISK}"
603
604}
605
606# Function which runs gpart and creates a single large MBR partition scheme
607init_mbr_full_disk()
608{
609 _intDISK=$1
610 _intBOOT=$2
611
591 startblock="63"
612 startblock="2016"
592
593 # Set our sysctl so we can overwrite any geom using drives
594 sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
595
596 # Stop any journaling
597 stop_gjournal "${_intDISK}"
598
599 # Remove any existing partitions
600 delete_all_gpart "${_intDISK}"
601
613
614 # Set our sysctl so we can overwrite any geom using drives
615 sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
616
617 # Stop any journaling
618 stop_gjournal "${_intDISK}"
619
620 # Remove any existing partitions
621 delete_all_gpart "${_intDISK}"
622
602 #Erase any existing bootloader
603 echo_log "Cleaning up ${_intDISK}"
604 rc_halt "dd if=/dev/zero of=/dev/${_intDISK} count=2048"
605
606 sleep 2
607
608 echo_log "Running gpart on ${_intDISK}"
609 rc_halt "gpart create -s mbr ${_intDISK}"
610
611 # Lets figure out disk size in blocks
612 # Get the cyl of this disk
613 get_disk_cyl "${_intDISK}"
614 cyl="${VAL}"
615
616 # Get the heads of this disk
617 get_disk_heads "${_intDISK}"
618 head="${VAL}"
619
620 # Get the tracks/sectors of this disk
621 get_disk_sectors "${_intDISK}"
622 sec="${VAL}"
623
624 # Multiply them all together to get our total blocks
625 totalblocks="`expr ${cyl} \* ${head}`"
626 totalblocks="`expr ${totalblocks} \* ${sec}`"
627 if [ -z "${totalblocks}" ]
628 then
629 totalblocks=`gpart show "${_intDISK}"|tail -2|head -1|awk '{ print $2 }'`
630 fi
631
632 # Now set the ending block to the total disk block size
633 sizeblock="`expr ${totalblocks} - ${startblock}`"
634
635 # Install new partition setup
636 echo_log "Running gpart add on ${_intDISK}"
637 rc_halt "gpart add -b ${startblock} -s ${sizeblock} -t freebsd -i 1 ${_intDISK}"
638 sleep 2
639
640 echo_log "Cleaning up ${_intDISK}s1"
641 rc_halt "dd if=/dev/zero of=/dev/${_intDISK}s1 count=1024"
642
643 if [ "$_intBOOT" = "bsd" ] ; then
644 echo_log "Stamping boot0 on ${_intDISK}"
645 rc_halt "gpart bootcode -b /boot/boot0 ${_intDISK}"
646 else
647 echo_log "Stamping boot1 on ${_intDISK}"
648 rc_halt "gpart bootcode -b /boot/boot1 ${_intDISK}"
649 fi
650
651}
652
653# Function which runs gpart and creates a single large slice
654run_gpart_full()
655{
656 DISK=$1
657 BOOT=$2
658 SCHEME=$3
659
660 if [ "$SCHEME" = "MBR" ] ; then
661 init_mbr_full_disk "$DISK" "$BOOT"
662 slice="${DISK}-1-mbr"
663 else
664 init_gpt_full_disk "$DISK"
665 slice="${DISK}-1-gpt"
666 fi
667
668 # Lets save our slice, so we know what to look for in the config file later on
669 if [ -z "$WORKINGSLICES" ]
670 then
671 WORKINGSLICES="${slice}"
672 export WORKINGSLICES
673 else
674 WORKINGSLICES="${WORKINGSLICES} ${slice}"
675 export WORKINGSLICES
676 fi
677};
678
679# Function which runs gpart on a specified s1-4 slice
680run_gpart_slice()
681{
682 DISK=$1
683 if [ ! -z "$2" ]
684 then
685 BMANAGER="$2"
686 fi
687
688 # Set the slice we will use later
689 slice="${1}s${3}"
690
691 # Set our sysctl so we can overwrite any geom using drives
692 sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
693
694 # Get the number of the slice we are working on
695 slicenum="$3"
696
697 # Stop any journaling
698 stop_gjournal "${slice}"
699
700 # Make sure we have disabled swap on this drive
701 if [ -e "${slice}b" ]
702 then
703 swapoff ${slice}b >/dev/null 2>/dev/null
704 swapoff ${slice}b.eli >/dev/null 2>/dev/null
705 fi
706
707 # Modify partition type
708 echo_log "Running gpart modify on ${DISK}"
709 rc_halt "gpart modify -t freebsd -i ${slicenum} ${DISK}"
710 sleep 2
711
712 # Clean up old partition
713 echo_log "Cleaning up $slice"
714 rc_halt "dd if=/dev/zero of=/dev/${DISK}s${slicenum} count=1024"
715
716 sleep 1
717
718 if [ "${BMANAGER}" = "bsd" ]
719 then
720 echo_log "Stamping boot sector on ${DISK}"
721 rc_halt "gpart bootcode -b /boot/boot0 ${DISK}"
722 fi
723
724 # Set the slice to the format we'll be using for gpart later
725 slice="${1}-${3}-mbr"
726
727 # Lets save our slice, so we know what to look for in the config file later on
728 if [ -z "$WORKINGSLICES" ]
729 then
730 WORKINGSLICES="${slice}"
731 export WORKINGSLICES
732 else
733 WORKINGSLICES="${WORKINGSLICES} ${slice}"
734 export WORKINGSLICES
735 fi
736};
737
738# Function which runs gpart and creates a new slice from free disk space
739run_gpart_free()
740{
741 DISK=$1
742 SLICENUM=$2
743 if [ ! -z "$3" ]
744 then
745 BMANAGER="$3"
746 fi
747
748 # Set our sysctl so we can overwrite any geom using drives
749 sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
750
751 slice="${DISK}s${SLICENUM}"
752 slicenum="${SLICENUM}"
753
754 # Working on the first slice, make sure we have MBR setup
755 gpart show ${DISK} >/dev/null 2>/dev/null
756 if [ "$?" != "0" -a "$SLICENUM" = "1" ] ; then
757 echo_log "Initializing disk, no existing MBR setup"
758 rc_halt "gpart create -s mbr ${DISK}"
759 fi
760
761 # Lets get the starting block first
762 if [ "${slicenum}" = "1" ]
763 then
764 startblock="63"
765 else
766 # Lets figure out where the prior slice ends
767 checkslice="`expr ${slicenum} - 1`"
768
769 # Get starting block of this slice
770 sblk=`gpart show ${DISK} | grep -v ${DISK} | tr -s '\t' ' ' | sed '/^$/d' | grep " ${checkslice} " | cut -d ' ' -f 2`
771 blksize=`gpart show ${DISK} | grep -v ${DISK} | tr -s '\t' ' ' | sed '/^$/d' | grep " ${checkslice} " | cut -d ' ' -f 3`
772 startblock="`expr ${sblk} + ${blksize}`"
773 fi
774
775 # No slice after the new slice, lets figure out the free space remaining and use it
776 # Get the cyl of this disk
777 get_disk_cyl "${DISK}"
778 cyl="${VAL}"
779
780 # Get the heads of this disk
781 get_disk_heads "${DISK}"
782 head="${VAL}"
783
784 # Get the tracks/sectors of this disk
785 get_disk_sectors "${DISK}"
786 sec="${VAL}"
787
788 # Multiply them all together to get our total blocks
789 totalblocks="`expr ${cyl} \* ${head}`"
790 totalblocks="`expr ${totalblocks} \* ${sec}`"
791
792
793 # Now set the ending block to the total disk block size
794 sizeblock="`expr ${totalblocks} - ${startblock}`"
795
796 # Install new partition setup
797 echo_log "Running gpart on ${DISK}"
798 rc_halt "gpart add -b ${startblock} -s ${sizeblock} -t freebsd -i ${slicenum} ${DISK}"
799 sleep 2
800
801 echo_log "Cleaning up $slice"
802 rc_halt "dd if=/dev/zero of=/dev/${slice} count=1024"
803
804 sleep 1
805
806 if [ "${BMANAGER}" = "bsd" ]
807 then
808 echo_log "Stamping boot sector on ${DISK}"
809 rc_halt "gpart bootcode -b /boot/boot0 ${DISK}"
810 fi
811
812 slice="${DISK}-${SLICENUM}-mbr"
813 # Lets save our slice, so we know what to look for in the config file later on
814 if [ -z "$WORKINGSLICES" ]
815 then
816 WORKINGSLICES="${slice}"
817 export WORKINGSLICES
818 else
819 WORKINGSLICES="${WORKINGSLICES} ${slice}"
820 export WORKINGSLICES
821 fi
822};
623 sleep 2
624
625 echo_log "Running gpart on ${_intDISK}"
626 rc_halt "gpart create -s mbr ${_intDISK}"
627
628 # Lets figure out disk size in blocks
629 # Get the cyl of this disk
630 get_disk_cyl "${_intDISK}"
631 cyl="${VAL}"
632
633 # Get the heads of this disk
634 get_disk_heads "${_intDISK}"
635 head="${VAL}"
636
637 # Get the tracks/sectors of this disk
638 get_disk_sectors "${_intDISK}"
639 sec="${VAL}"
640
641 # Multiply them all together to get our total blocks
642 totalblocks="`expr ${cyl} \* ${head}`"
643 totalblocks="`expr ${totalblocks} \* ${sec}`"
644 if [ -z "${totalblocks}" ]
645 then
646 totalblocks=`gpart show "${_intDISK}"|tail -2|head -1|awk '{ print $2 }'`
647 fi
648
649 # Now set the ending block to the total disk block size
650 sizeblock="`expr ${totalblocks} - ${startblock}`"
651
652 # Install new partition setup
653 echo_log "Running gpart add on ${_intDISK}"
654 rc_halt "gpart add -b ${startblock} -s ${sizeblock} -t freebsd -i 1 ${_intDISK}"
655 sleep 2
656
657 echo_log "Cleaning up ${_intDISK}s1"
658 rc_halt "dd if=/dev/zero of=/dev/${_intDISK}s1 count=1024"
659
660 if [ "$_intBOOT" = "bsd" ] ; then
661 echo_log "Stamping boot0 on ${_intDISK}"
662 rc_halt "gpart bootcode -b /boot/boot0 ${_intDISK}"
663 else
664 echo_log "Stamping boot1 on ${_intDISK}"
665 rc_halt "gpart bootcode -b /boot/boot1 ${_intDISK}"
666 fi
667
668}
669
670# Function which runs gpart and creates a single large slice
671run_gpart_full()
672{
673 DISK=$1
674 BOOT=$2
675 SCHEME=$3
676
677 if [ "$SCHEME" = "MBR" ] ; then
678 init_mbr_full_disk "$DISK" "$BOOT"
679 slice="${DISK}-1-mbr"
680 else
681 init_gpt_full_disk "$DISK"
682 slice="${DISK}-1-gpt"
683 fi
684
685 # Lets save our slice, so we know what to look for in the config file later on
686 if [ -z "$WORKINGSLICES" ]
687 then
688 WORKINGSLICES="${slice}"
689 export WORKINGSLICES
690 else
691 WORKINGSLICES="${WORKINGSLICES} ${slice}"
692 export WORKINGSLICES
693 fi
694};
695
696# Function which runs gpart on a specified s1-4 slice
697run_gpart_slice()
698{
699 DISK=$1
700 if [ ! -z "$2" ]
701 then
702 BMANAGER="$2"
703 fi
704
705 # Set the slice we will use later
706 slice="${1}s${3}"
707
708 # Set our sysctl so we can overwrite any geom using drives
709 sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
710
711 # Get the number of the slice we are working on
712 slicenum="$3"
713
714 # Stop any journaling
715 stop_gjournal "${slice}"
716
717 # Make sure we have disabled swap on this drive
718 if [ -e "${slice}b" ]
719 then
720 swapoff ${slice}b >/dev/null 2>/dev/null
721 swapoff ${slice}b.eli >/dev/null 2>/dev/null
722 fi
723
724 # Modify partition type
725 echo_log "Running gpart modify on ${DISK}"
726 rc_halt "gpart modify -t freebsd -i ${slicenum} ${DISK}"
727 sleep 2
728
729 # Clean up old partition
730 echo_log "Cleaning up $slice"
731 rc_halt "dd if=/dev/zero of=/dev/${DISK}s${slicenum} count=1024"
732
733 sleep 1
734
735 if [ "${BMANAGER}" = "bsd" ]
736 then
737 echo_log "Stamping boot sector on ${DISK}"
738 rc_halt "gpart bootcode -b /boot/boot0 ${DISK}"
739 fi
740
741 # Set the slice to the format we'll be using for gpart later
742 slice="${1}-${3}-mbr"
743
744 # Lets save our slice, so we know what to look for in the config file later on
745 if [ -z "$WORKINGSLICES" ]
746 then
747 WORKINGSLICES="${slice}"
748 export WORKINGSLICES
749 else
750 WORKINGSLICES="${WORKINGSLICES} ${slice}"
751 export WORKINGSLICES
752 fi
753};
754
755# Function which runs gpart and creates a new slice from free disk space
756run_gpart_free()
757{
758 DISK=$1
759 SLICENUM=$2
760 if [ ! -z "$3" ]
761 then
762 BMANAGER="$3"
763 fi
764
765 # Set our sysctl so we can overwrite any geom using drives
766 sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
767
768 slice="${DISK}s${SLICENUM}"
769 slicenum="${SLICENUM}"
770
771 # Working on the first slice, make sure we have MBR setup
772 gpart show ${DISK} >/dev/null 2>/dev/null
773 if [ "$?" != "0" -a "$SLICENUM" = "1" ] ; then
774 echo_log "Initializing disk, no existing MBR setup"
775 rc_halt "gpart create -s mbr ${DISK}"
776 fi
777
778 # Lets get the starting block first
779 if [ "${slicenum}" = "1" ]
780 then
781 startblock="63"
782 else
783 # Lets figure out where the prior slice ends
784 checkslice="`expr ${slicenum} - 1`"
785
786 # Get starting block of this slice
787 sblk=`gpart show ${DISK} | grep -v ${DISK} | tr -s '\t' ' ' | sed '/^$/d' | grep " ${checkslice} " | cut -d ' ' -f 2`
788 blksize=`gpart show ${DISK} | grep -v ${DISK} | tr -s '\t' ' ' | sed '/^$/d' | grep " ${checkslice} " | cut -d ' ' -f 3`
789 startblock="`expr ${sblk} + ${blksize}`"
790 fi
791
792 # No slice after the new slice, lets figure out the free space remaining and use it
793 # Get the cyl of this disk
794 get_disk_cyl "${DISK}"
795 cyl="${VAL}"
796
797 # Get the heads of this disk
798 get_disk_heads "${DISK}"
799 head="${VAL}"
800
801 # Get the tracks/sectors of this disk
802 get_disk_sectors "${DISK}"
803 sec="${VAL}"
804
805 # Multiply them all together to get our total blocks
806 totalblocks="`expr ${cyl} \* ${head}`"
807 totalblocks="`expr ${totalblocks} \* ${sec}`"
808
809
810 # Now set the ending block to the total disk block size
811 sizeblock="`expr ${totalblocks} - ${startblock}`"
812
813 # Install new partition setup
814 echo_log "Running gpart on ${DISK}"
815 rc_halt "gpart add -b ${startblock} -s ${sizeblock} -t freebsd -i ${slicenum} ${DISK}"
816 sleep 2
817
818 echo_log "Cleaning up $slice"
819 rc_halt "dd if=/dev/zero of=/dev/${slice} count=1024"
820
821 sleep 1
822
823 if [ "${BMANAGER}" = "bsd" ]
824 then
825 echo_log "Stamping boot sector on ${DISK}"
826 rc_halt "gpart bootcode -b /boot/boot0 ${DISK}"
827 fi
828
829 slice="${DISK}-${SLICENUM}-mbr"
830 # Lets save our slice, so we know what to look for in the config file later on
831 if [ -z "$WORKINGSLICES" ]
832 then
833 WORKINGSLICES="${slice}"
834 export WORKINGSLICES
835 else
836 WORKINGSLICES="${WORKINGSLICES} ${slice}"
837 export WORKINGSLICES
838 fi
839};