Project

General

Profile

Download (17.8 KB) Statistics
| Branch: | Tag: | Revision:
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: src/usr.sbin/pc-sysinstall/backend/functions-disk.sh,v 1.2 2010/06/27 16:46:11 imp Exp $
27

    
28
# Functions related to disk operations using gpart
29

    
30
# See if device is a full disk or partition/slice
31
is_disk() {
32
	for _dsk in `sysctl -n kern.disks`
33
	do
34
		if [ "$_dsk" = "${1}" ] ; then return 0 ; fi
35
	done
36

    
37
	return 1
38
}
39

    
40
# Get a MBR partitions sysid
41
get_partition_sysid_mbr()
42
{
43
  INPART="0"
44
  DISK="$1"
45
  PARTNUM=`echo ${2} | sed "s|${DISK}s||g"`
46
  fdisk ${DISK} >${TMPDIR}/disk-${DISK} 2>/dev/null
47
  while read i
48
  do
49
    echo "$i" | grep "The data for partition"  >/dev/null 2>/dev/null
50
    if [ "$?" = "0" ] ; then
51
       INPART="0"
52
       PART="`echo ${i} | cut -d ' ' -f 5`"
53
       if [ "$PART" = "$PARTNUM" ] ; then
54
          INPART="1"
55
       fi
56
    fi
57

    
58
    # In the partition section
59
    if [ "$INPART" = "1" ] ; then
60
       echo "$i" | grep "^sysid" >/dev/null 2>/dev/null
61
       if [ "$?" = "0" ] ; then
62
         SYSID="`echo ${i} | tr -s '\t' ' ' | cut -d ' ' -f 2`"
63
         break
64
       fi
65

    
66
    fi
67

    
68
  done < ${TMPDIR}/disk-${DISK}
69
  rm ${TMPDIR}/disk-${DISK}
70

    
71
  VAL="${SYSID}"
72
  export VAL
73
};
74

    
75
# Get the partitions MBR label
76
get_partition_label_mbr()
77
{
78
  INPART="0"
79
  DISK="$1"
80
  PARTNUM=`echo ${2} | sed "s|${DISK}s||g"`
81
  fdisk ${DISK} >${TMPDIR}/disk-${DISK} 2>/dev/null
82
  while read i
83
  do
84
    echo "$i" | grep "The data for partition"  >/dev/null 2>/dev/null
85
    if [ "$?" = "0" ] ; then
86
       INPART="0"
87
       PART="`echo ${i} | cut -d ' ' -f 5`"
88
       if [ "$PART" = "$PARTNUM" ] ; then
89
          INPART="1"
90
       fi
91
    fi
92

    
93
    # In the partition section
94
    if [ "$INPART" = "1" ] ; then
95
       echo "$i" | grep "^sysid" >/dev/null 2>/dev/null
96
       if [ "$?" = "0" ] ; then
97
         LABEL="`echo ${i} | tr -s '\t' ' ' | cut -d ',' -f 2-10`"
98
         break
99
       fi
100

    
101
    fi
102

    
103
  done < ${TMPDIR}/disk-${DISK}
104
  rm ${TMPDIR}/disk-${DISK}
105

    
106
  VAL="${LABEL}"
107
  export VAL
108
};
109

    
110
# Get a GPT partitions label
111
get_partition_label_gpt()
112
{
113
  DISK="${1}"
114
  PARTNUM=`echo ${2} | sed "s|${DISK}p||g"`
115

    
116
  gpart show ${DISK} >${TMPDIR}/disk-${DISK}
117
  while read i
118
  do
119
     SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
120
     if [ "${SLICE}" = "${PARTNUM}" ] ; then
121
       LABEL="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 4`"
122
       break
123
     fi
124
  done <${TMPDIR}/disk-${DISK}
125
  rm ${TMPDIR}/disk-${DISK}
126

    
127
  VAL="${LABEL}"
128
  export VAL
129
};
130

    
131
# Get a partitions startblock
132
get_partition_startblock()
133
{
134
  DISK="${1}"
135
  PARTNUM=`echo ${2} | sed "s|${DISK}p||g" | sed "s|${DISK}s||g"`
136

    
137
  gpart show ${DISK} >${TMPDIR}/disk-${DISK}
138
  while read i
139
  do
140
     SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
141
     if [ "$SLICE" = "${PARTNUM}" ] ; then
142
       SB="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 1`"
143
       break
144
     fi
145
  done <${TMPDIR}/disk-${DISK}
146
  rm ${TMPDIR}/disk-${DISK}
147

    
148
  VAL="${SB}"
149
  export VAL
150
};
151

    
152
# Get a partitions blocksize
153
get_partition_blocksize()
154
{
155
  DISK="${1}"
156
  PARTNUM=`echo ${2} | sed "s|${DISK}p||g" | sed "s|${DISK}s||g"`
157

    
158
  gpart show ${DISK} >${TMPDIR}/disk-${DISK}
159
  while read i
160
  do
161
     SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
162
     if [ "$SLICE" = "${PARTNUM}" ] ; then
163
       BS="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 2`"
164
       break
165
     fi
166
  done <${TMPDIR}/disk-${DISK}
167
  rm ${TMPDIR}/disk-${DISK}
168

    
169
  VAL="${BS}"
170
  export VAL
171
};
172

    
173
# Function which returns the partitions on a target disk
174
get_disk_partitions()
175
{
176
  gpart show ${1} >/dev/null 2>/dev/null
177
  if [ "$?" != "0" ] ; then
178
    VAL="" ; export VAL
179
    return
180
  fi
181

    
182
  gpart show ${1} | grep "MBR" >/dev/null 2>/dev/null
183
  if [ "$?" = "0" ] ; then
184
    type="MBR"
185
  else
186
    type="GPT"
187
  fi
188

    
189
  SLICES="`gpart show ${1} | grep -v ${1} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 4 | sed '/^$/d'`"
190
  for i in ${SLICES}
191
  do
192
    case $type in
193
       MBR) name="${1}s${i}" ;;
194
       GPT) name="${1}p${i}";;
195
       *) name="${1}s${i}";;
196
    esac
197
    if [ -z "${RSLICES}" ]
198
    then
199
      RSLICES="${name}"
200
    else
201
      RSLICES="${RSLICES} ${name}"
202
    fi
203
  done
204

    
205
  VAL="${RSLICES}" ; export VAL
206
};
207

    
208
# Function which returns a target disks cylinders
209
get_disk_cyl()
210
{
211
  cyl=`diskinfo -v ${1} | grep "# Cylinders" | tr -s ' ' | cut -f 2`
212
  VAL="${cyl}" ; export VAL
213
};
214

    
215
# Function which returns a target disks sectors
216
get_disk_sectors()
217
{
218
  sec=`diskinfo -v ${1} | grep "# Sectors" | tr -s ' ' | cut -f 2`
219
  VAL="${sec}" ; export VAL
220
};
221

    
222
# Function which returns a target disks heads
223
get_disk_heads()
224
{
225
  head=`diskinfo -v ${1} | grep "# Heads" | tr -s ' ' | cut -f 2`
226
  VAL="${head}" ; export VAL
227
};
228

    
229
# Function which exports all zpools, making them safe to overwrite potentially
230
export_all_zpools() {
231
  # Export any zpools
232
  for i in `zpool list -H -o name`
233
  do
234
    zpool export -f ${i}
235
  done
236
};
237

    
238
# Function to delete all gparts before starting an install
239
delete_all_gpart()
240
{
241
  echo_log "Deleting all gparts"
242
  DISK="$1"
243

    
244
  # Check for any swaps to stop
245
  for i in `gpart show ${DISK} 2>/dev/null | grep 'freebsd-swap' | tr -s ' ' | cut -d ' ' -f 4`
246
  do
247
    swapoff /dev/${DISK}s${i}b >/dev/null 2>/dev/null
248
    swapoff /dev/${DISK}p${i} >/dev/null 2>/dev/null
249
  done
250

    
251
  # Delete the gparts now
252
  for i in `gpart show ${DISK} 2>/dev/null | tr -s ' ' | cut -d ' ' -f 4`
253
  do
254
   if [ "${i}" != "${DISK}" -a "${i}" != "-" ] ; then
255
     rc_nohalt "gpart delete -i ${i} ${DISK}"
256
   fi
257
  done
258

    
259
  rc_nohalt "dd if=/dev/zero of=/dev/${DISK} count=3000"
260

    
261
};
262

    
263
# Function to export all zpools before starting an install
264
stop_all_zfs()
265
{
266
  # Export all zpools again, so that we can overwrite these partitions potentially
267
  for i in `zpool list -H -o name`
268
  do
269
    zpool export -f ${i}
270
  done
271
};
272

    
273
# Function which stops all gmirrors before doing any disk manipulation
274
stop_all_gmirror()
275
{
276
  DISK="${1}"
277
  GPROV="`gmirror list | grep ". Name: mirror/" | cut -d '/' -f 2`"
278
  for gprov in $GPROV 
279
  do
280
    gmirror list | grep "Name: ${DISK}" >/dev/null 2>/dev/null
281
    if [ "$?" = "0" ]
282
    then
283
      echo_log "Stopping mirror $gprov $DISK"
284
      rc_nohalt "gmirror remove $gprov $DISK"
285
      rc_nohalt "dd if=/dev/zero of=/dev/${DISK} count=4096"
286
    fi
287
  done
288
};
289

    
290
# Make sure we don't have any geli providers active on this disk
291
stop_all_geli()
292
{
293
  _geld="${1}"
294
  cd /dev
295

    
296
  for i in `ls ${_geld}*`
297
  do
298
    echo $i | grep '.eli' >/dev/null 2>/dev/null
299
    if [ "$?" = "0" ]
300
    then
301
      echo_log "Detaching GELI on ${i}"
302
      rc_halt "geli detach ${i}"
303
    fi
304
  done
305

    
306
};
307

    
308
# Function which reads in the disk slice config, and performs it
309
setup_disk_slice()
310
{
311

    
312
  # Cleanup any slice / mirror dirs
313
  rm -rf ${SLICECFGDIR} >/dev/null 2>/dev/null
314
  mkdir ${SLICECFGDIR}
315
  rm -rf ${MIRRORCFGDIR} >/dev/null 2>/dev/null
316
  mkdir ${MIRRORCFGDIR}
317

    
318
  # Start with disk0
319
  disknum="0"
320

    
321
  # Make sure all zpools are exported
322
  export_all_zpools
323

    
324
  # We are ready to start setting up the disks, lets read the config and do the actions
325
  while read line
326
  do
327
     echo $line | grep "^disk${disknum}=" >/dev/null 2>/dev/null
328
     if [ "$?" = "0" ]
329
     then
330

    
331
       # Found a disk= entry, lets get the disk we are working on
332
       get_value_from_string "${line}"
333
       strip_white_space "$VAL"
334
       DISK="$VAL"
335
     
336
       # Before we go further, lets confirm this disk really exists
337
       if [ ! -e "/dev/${DISK}" ]
338
       then
339
         exit_err "ERROR: The disk ${DISK} does not exist!"
340
       fi
341

    
342
       # Make sure we stop any gmirrors on this disk
343
       stop_all_gmirror ${DISK}
344

    
345
       # Make sure we stop any geli stuff on this disk
346
       stop_all_geli ${DISK}
347

    
348
       # Make sure we don't have any zpools loaded
349
       stop_all_zfs
350

    
351
     fi
352

    
353
     # Lets look if this device will be mirrored on another disk
354
     echo $line | grep "^mirror=" >/dev/null 2>/dev/null
355
     if [ "$?" = "0" ]
356
     then
357

    
358
       # Found a disk= entry, lets get the disk we are working on
359
       get_value_from_string "${line}"
360
       strip_white_space "$VAL"
361
       MIRRORDISK="$VAL"
362
     
363
       # Before we go further, lets confirm this disk really exists
364
       if [ ! -e "/dev/${MIRRORDISK}" ]
365
       then
366
         exit_err "ERROR: The mirror disk ${MIRRORDISK} does not exist!"
367
       fi
368
     fi
369

    
370
     # Lets see if we have been given a mirror balance choice
371
     echo $line | grep "^mirrorbal=" >/dev/null 2>/dev/null
372
     if [ "$?" = "0" ]
373
     then
374

    
375
       # Found a disk= entry, lets get the disk we are working on
376
       get_value_from_string "${line}"
377
       strip_white_space "$VAL"
378
       MIRRORBAL="$VAL"
379
     fi
380

    
381
     echo $line | grep "^partition=" >/dev/null 2>/dev/null
382
     if [ "$?" = "0" ]
383
     then
384
       # Found a partition= entry, lets read / set it 
385
       get_value_from_string "${line}"
386
       strip_white_space "$VAL"
387
       PTYPE="$VAL"
388

    
389
       # We are using free space, figure out the slice number
390
       if [ "${PTYPE}" = "free" -o "${PTYPE}" = "FREE" ]
391
       then
392
         # Lets figure out what number this slice will be
393
         LASTSLICE="`gpart show ${DISK} | grep -v ${DISK} | grep -v ' free' |tr -s '\t' ' ' | cut -d ' ' -f 4 | sed '/^$/d' | tail -n 1`"
394
         if [ -z "${LASTSLICE}" ]
395
         then
396
           LASTSLICE="1"
397
         else
398
           LASTSLICE="`expr $LASTSLICE + 1`"
399
         fi
400

    
401
         if [ $LASTSLICE -gt 4 ]
402
         then
403
           exit_err "ERROR: BSD only supports primary partitions, and there are none availble on $DISK"
404
         fi
405

    
406
       fi
407
     fi
408

    
409
     echo $line | grep "^bootManager=" >/dev/null 2>/dev/null
410
     if [ "$?" = "0" ]
411
     then
412
       # Found a bootManager= entry, lets read /set it
413
       get_value_from_string "${line}"
414
       strip_white_space "$VAL"
415
       BMANAGER="$VAL"
416
     fi
417

    
418
     echo $line | grep "^commitDiskPart" >/dev/null 2>/dev/null
419
     if [ "$?" = "0" ]
420
     then
421
       # Found our flag to commit this disk setup / lets do sanity check and do it
422
       if [ ! -z "${DISK}" -a ! -z "${PTYPE}" ]
423
       then
424
         case ${PTYPE} in
425
                   all|ALL) tmpSLICE="${DISK}p1"  
426
                        run_gpart_full "${DISK}" "${BMANAGER}" ;;
427
           s1|s2|s3|s4) tmpSLICE="${DISK}${PTYPE}" 
428
                        # Get the number of the slice we are working on
429
                        s="`echo ${PTYPE} | awk '{print substr($0,length,1)}'`" 
430
                        run_gpart_slice "${DISK}" "${BMANAGER}" "${s}" ;;
431
                 free|FREE) tmpSLICE="${DISK}s${LASTSLICE}"
432
                        run_gpart_free "${DISK}" "${LASTSLICE}" "${BMANAGER}" ;;
433
                     *) exit_err "ERROR: Unknown PTYPE: $PTYPE" ;;
434
         esac
435

    
436
         # Now save which disk<num> this is, so we can parse it later during slice partition setup
437
         echo "disk${disknum}" >${SLICECFGDIR}/$tmpSLICE
438

    
439
         # Save any mirror config
440
         if [ ! -z "$MIRRORDISK" ]
441
         then
442
           # Default to round-robin if the user didn't specify
443
           if [ -z "$MIRRORBAL" ]
444
           then
445
             MIRRORBAL="round-robin"
446
           fi
447
           echo "$MIRRORDISK:$MIRRORBAL" >${MIRRORCFGDIR}/$DISK
448
         fi
449

    
450

    
451
         # Increment our disk counter to look for next disk and unset
452
         unset BMANAGER PTYPE DISK MIRRORDISK MIRRORBAL
453
         disknum="`expr $disknum + 1`"
454
       else
455
         exit_err "ERROR: commitDiskPart was called without procceding disk<num>= and partition= entries!!!" 
456
       fi
457
     fi
458

    
459
  done <${CFGF}
460

    
461
};
462

    
463
# Stop all gjournals on disk / slice
464
stop_gjournal() {
465
  _gdsk="$1"
466
  # Check if we need to shutdown any journals on this drive
467
  ls /dev/${_gdsk}*.journal >/dev/null 2>/dev/null
468
  if [ "$?" = "0" ]
469
  then
470
    cd /dev
471
    for i in `ls ${_gdsk}*.journal`
472
    do
473
      rawjournal="`echo ${i} | cut -d '.' -f 1`"
474
      gjournal stop -f ${rawjournal} >>${LOGOUT} 2>>${LOGOUT}
475
      gjournal clear ${rawjournal} >>${LOGOUT} 2>>${LOGOUT}
476
    done
477
  fi
478
} ;
479

    
480
# Function which runs gpart and creates a single large slice
481
init_gpt_full_disk()
482
{
483
  _intDISK=$1
484
 
485
  # Set our sysctl so we can overwrite any geom using drives
486
  sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
487

    
488
  # Stop any journaling
489
  stop_gjournal "${_intDISK}"
490

    
491
  # Remove any existing partitions
492
  delete_all_gpart "${_intDISK}"
493

    
494
  #Erase any existing bootloader
495
  echo_log "Cleaning up ${_intDISK}"
496
  rc_halt "dd if=/dev/zero of=/dev/${_intDISK} count=2048"
497

    
498
  sleep 2
499

    
500
  echo_log "Running gpart on ${_intDISK}"
501
  rc_halt "gpart create -s GPT ${_intDISK}"
502
  rc_halt "gpart add -b 34 -s 128 -t freebsd-boot ${_intDISK}"
503
  
504
  echo_log "Stamping boot sector on ${_intDISK}"
505
  rc_halt "gpart bootcode -b /boot/pmbr ${_intDISK}"
506

    
507
}
508

    
509
# Function which runs gpart and creates a single large slice
510
run_gpart_full()
511
{
512
  DISK=$1
513

    
514
  init_gpt_full_disk "$DISK"
515

    
516
  slice="${DISK}-1-gpt"
517

    
518
  # Lets save our slice, so we know what to look for in the config file later on
519
  if [ -z "$WORKINGSLICES" ]
520
  then
521
    WORKINGSLICES="${slice}"
522
    export WORKINGSLICES
523
  else
524
    WORKINGSLICES="${WORKINGSLICES} ${slice}"
525
    export WORKINGSLICES
526
  fi
527
};
528

    
529
# Function which runs gpart on a specified s1-4 slice
530
run_gpart_slice()
531
{
532
  DISK=$1
533
  if [ ! -z "$2" ]
534
  then
535
    BMANAGER="$2"
536
  fi
537

    
538
  # Set the slice we will use later
539
  slice="${1}s${3}"
540
 
541
  # Set our sysctl so we can overwrite any geom using drives
542
  sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
543

    
544
  # Get the number of the slice we are working on
545
  slicenum="$3"
546

    
547
  # Stop any journaling
548
  stop_gjournal "${slice}"
549

    
550
  # Make sure we have disabled swap on this drive
551
  if [ -e "${slice}b" ]
552
  then
553
   swapoff ${slice}b >/dev/null 2>/dev/null
554
   swapoff ${slice}b.eli >/dev/null 2>/dev/null
555
  fi
556

    
557
  # Modify partition type
558
  echo_log "Running gpart modify on ${DISK}"
559
  rc_halt "gpart modify -t freebsd -i ${slicenum} ${DISK}"
560
  sleep 2
561

    
562
  # Clean up old partition
563
  echo_log "Cleaning up $slice"
564
  rc_halt "dd if=/dev/zero of=/dev/${DISK}s${slicenum} count=1024"
565

    
566
  sleep 1
567

    
568
  if [ "${BMANAGER}" = "bsd" ]
569
  then
570
    echo_log "Stamping boot sector on ${DISK}"
571
    rc_halt "gpart bootcode -b /boot/boot0 ${DISK}"
572
  fi
573

    
574
  # Set the slice to the format we'll be using for gpart later
575
  slice="${1}-${3}-mbr"
576

    
577
  # Lets save our slice, so we know what to look for in the config file later on
578
  if [ -z "$WORKINGSLICES" ]
579
  then
580
    WORKINGSLICES="${slice}"
581
    export WORKINGSLICES
582
  else
583
    WORKINGSLICES="${WORKINGSLICES} ${slice}"
584
    export WORKINGSLICES
585
  fi
586
};
587

    
588
# Function which runs gpart and creates a new slice from free disk space
589
run_gpart_free()
590
{
591
  DISK=$1
592
  SLICENUM=$2
593
  if [ ! -z "$3" ]
594
  then
595
    BMANAGER="$3"
596
  fi
597

    
598
  # Set our sysctl so we can overwrite any geom using drives
599
  sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
600

    
601
  slice="${DISK}s${SLICENUM}"
602
  slicenum="${SLICENUM}" 
603

    
604
  # Working on the first slice, make sure we have MBR setup
605
  gpart show ${DISK} >/dev/null 2>/dev/null
606
  if [ "$?" != "0" -a "$SLICENUM" = "1" ] ; then
607
    echo_log "Initializing disk, no existing MBR setup"
608
    rc_halt "gpart create -s mbr ${DISK}"
609
  fi
610

    
611
  # Lets get the starting block first
612
  if [ "${slicenum}" = "1" ]
613
  then
614
     startblock="63"
615
  else
616
     # Lets figure out where the prior slice ends
617
     checkslice="`expr ${slicenum} - 1`"
618

    
619
     # Get starting block of this slice
620
     sblk=`gpart show ${DISK} | grep -v ${DISK} | tr -s '\t' ' ' | sed '/^$/d' | grep " ${checkslice} " | cut -d ' ' -f 2`
621
     blksize=`gpart show ${DISK} | grep -v ${DISK} | tr -s '\t' ' ' | sed '/^$/d' | grep " ${checkslice} " | cut -d ' ' -f 3`
622
     startblock="`expr ${sblk} + ${blksize}`"
623
  fi
624

    
625
  # No slice after the new slice, lets figure out the free space remaining and use it
626
  # Get the cyl of this disk
627
  get_disk_cyl "${DISK}"
628
  cyl="${VAL}"
629

    
630
  # Get the heads of this disk
631
  get_disk_heads "${DISK}"
632
  head="${VAL}"
633

    
634
  # Get the tracks/sectors of this disk
635
  get_disk_sectors "${DISK}"
636
  sec="${VAL}"
637

    
638
  # Multiply them all together to get our total blocks
639
  totalblocks="`expr ${cyl} \* ${head}`"
640
  totalblocks="`expr ${totalblocks} \* ${sec}`"
641

    
642

    
643
  # Now set the ending block to the total disk block size
644
  sizeblock="`expr ${totalblocks} - ${startblock}`"
645

    
646
  # Install new partition setup
647
  echo_log "Running gpart on ${DISK}"
648
  rc_halt "gpart add -b ${startblock} -s ${sizeblock} -t freebsd -i ${slicenum} ${DISK}"
649
  sleep 2
650
  
651
  echo_log "Cleaning up $slice"
652
  rc_halt "dd if=/dev/zero of=/dev/${slice} count=1024"
653

    
654
  sleep 1
655

    
656
  if [ "${BMANAGER}" = "bsd" ]
657
  then
658
    echo_log "Stamping boot sector on ${DISK}"
659
    rc_halt "gpart bootcode -b /boot/boot0 ${DISK}"
660
  fi
661

    
662
  slice="${DISK}-${SLICENUM}-mbr"
663
  # Lets save our slice, so we know what to look for in the config file later on
664
  if [ -z "$WORKINGSLICES" ]
665
  then
666
    WORKINGSLICES="${slice}"
667
    export WORKINGSLICES
668
  else
669
    WORKINGSLICES="${WORKINGSLICES} ${slice}"
670
    export WORKINGSLICES
671
  fi
672
};
(4-4/21)