]> CyberLeo.Net >> Repos - FreeBSD/releng/9.1.git/blob - usr.sbin/pc-sysinstall/backend/functions-disk.sh
Copy stable/9 to releng/9.1 as part of the 9.1-RELEASE release process.
[FreeBSD/releng/9.1.git] / usr.sbin / pc-sysinstall / backend / functions-disk.sh
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$
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 {
33   for _dsk in `sysctl -n kern.disks`
34   do
35     [ "$_dsk" = "${1}" ] && return 0
36   done
37
38   return 1
39 }
40
41 # Get a MBR partitions sysid
42 get_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 -q "The data for partition" 2>/dev/null
51     if [ $? -eq 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 -q "^sysid" 2>/dev/null
62        if [ $? -eq 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   export VAL="${SYSID}"
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 -q "The data for partition" 2>/dev/null
85     if [ $? -eq 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 -q "^sysid" 2>/dev/null
96        if [ $? -eq 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   export VAL="${LABEL}"
107 };
108
109 # Get a GPT partitions label
110 get_partition_label_gpt()
111 {
112   DISK="${1}"
113   PARTNUM=`echo ${2} | sed "s|${DISK}p||g"`
114
115   gpart show ${DISK} >${TMPDIR}/disk-${DISK}
116   while read i
117   do
118      SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
119      if [ "${SLICE}" = "${PARTNUM}" ] ; then
120        LABEL="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 4`"
121        break
122      fi
123   done <${TMPDIR}/disk-${DISK}
124   rm ${TMPDIR}/disk-${DISK}
125
126   export VAL="${LABEL}"
127 };
128
129 # Get a partitions startblock
130 get_partition_startblock()
131 {
132   DISK="${1}"
133   PARTNUM=`echo ${2} | sed "s|${DISK}p||g" | sed "s|${DISK}s||g"`
134
135   gpart show ${DISK} >${TMPDIR}/disk-${DISK}
136   while read i
137   do
138      SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
139      if [ "$SLICE" = "${PARTNUM}" ] ; then
140        SB="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 1`"
141        break
142      fi
143   done <${TMPDIR}/disk-${DISK}
144   rm ${TMPDIR}/disk-${DISK}
145
146   export VAL="${SB}"
147 };
148
149 # Get a partitions blocksize
150 get_partition_blocksize()
151 {
152   DISK="${1}"
153   PARTNUM=`echo ${2} | sed "s|${DISK}p||g" | sed "s|${DISK}s||g"`
154
155   gpart show ${DISK} >${TMPDIR}/disk-${DISK}
156   while read i
157   do
158      SLICE="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 3`"
159      if [ "$SLICE" = "${PARTNUM}" ] ; then
160        BS="`echo ${i} | grep -v ${DISK} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 2`"
161        break
162      fi
163   done <${TMPDIR}/disk-${DISK}
164   rm ${TMPDIR}/disk-${DISK}
165
166   export VAL="${BS}"
167 };
168
169 # Function which returns the partitions on a target disk
170 get_disk_partitions()
171 {
172   gpart show ${1} >/dev/null 2>/dev/null
173   if [ $? -ne 0 ] ; then
174     export VAL=""
175     return
176   fi
177
178   type=`gpart show ${1} | awk '/^=>/ { printf("%s",$5); }'`
179
180   SLICES="`gpart show ${1} | grep -v ${1} | grep -v ' free ' |tr -s '\t' ' ' | cut -d ' ' -f 4 | sed '/^$/d'`"
181   for i in ${SLICES}
182   do
183     case $type in
184       MBR) name="${1}s${i}" ;;
185       GPT) name="${1}p${i}";;
186       *) name="${1}s${i}";;
187     esac
188     if [ -z "${RSLICES}" ]
189     then
190       RSLICES="${name}"
191     else
192       RSLICES="${RSLICES} ${name}"
193     fi
194   done
195
196   export VAL="${RSLICES}"
197 };
198
199 # Function which returns a target disks cylinders
200 get_disk_cyl()
201 {
202   cyl=`diskinfo -v ${1} | grep "# Cylinders" | tr -s ' ' | cut -f 2`
203   export VAL="${cyl}"
204 };
205
206 # Function which returns a target disks sectors
207 get_disk_sectors()
208 {
209   sec=`diskinfo -v ${1} | grep "# Sectors" | tr -s ' ' | cut -f 2`
210   export VAL="${sec}"
211 };
212
213 # Function which returns a target disks heads
214 get_disk_heads()
215 {
216   head=`diskinfo -v ${1} | grep "# Heads" | tr -s ' ' | cut -f 2`
217   export VAL="${head}"
218 };
219
220 # Function which returns a target disks mediasize in sectors
221 get_disk_mediasize()
222 {
223   mediasize=`diskinfo -v ${1} | grep "# mediasize in sectors" | tr -s ' ' | cut -f 2`
224   export VAL="${mediasize}"
225 };
226
227 # Function to delete all gparts before starting an install
228 delete_all_gpart()
229 {
230   echo_log "Deleting all gparts"
231   local DISK="$1"
232
233   # Check for any swaps to stop
234   for i in `swapctl -l | grep "$DISK" | awk '{print $1}'`
235   do
236     swapoff ${i} >/dev/null 2>/dev/null
237   done
238
239   # Delete the gparts now
240   for i in `gpart show ${DISK} 2>/dev/null | tr -s ' ' | cut -d ' ' -f 4`
241   do
242    if [ "/dev/${i}" != "${DISK}" -a "${i}" != "-" ] ; then
243      rc_nohalt "gpart delete -i ${i} ${DISK}"
244    fi
245   done
246
247   # Destroy the disk geom
248   rc_nohalt "gpart destroy ${DISK}"
249
250   # Make sure we clear any hidden gpt tables
251   clear_backup_gpt_table "${DISK}"
252
253   # Wipe out front of disk
254   rc_nohalt "dd if=/dev/zero of=${DISK} count=3000"
255
256 };
257
258 # Function to export all zpools before starting an install
259 stop_all_zfs()
260 {
261   local DISK="`echo ${1} | sed 's|/dev/||g'`"
262
263   # Export any zpools using this device so we can overwrite
264   for i in `zpool list -H -o name`
265   do
266     ztst=`zpool status ${i} | grep "ONLINE" | awk '{print $1}' | grep -q ${DISK}`
267     if [ "$ztst" = "$DISK" ] ; then
268       zpool export -f ${i}
269     fi
270   done
271 };
272
273 # Function which stops all gmirrors before doing any disk manipulation
274 stop_all_gmirror()
275 {
276   local DISK="`echo ${1} | sed 's|/dev/||g'`"
277   GPROV="`gmirror list | grep ". Name: mirror/" | cut -d '/' -f 2`"
278   for gprov in $GPROV 
279   do
280     gmirror list | grep -q "Name: ${DISK}" 2>/dev/null
281     if [ $? -eq 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   local _geld="`echo ${1} | sed 's|/dev/||g'`"
294   cd /dev
295
296   for i in `ls ${_geld}*`
297   do
298     echo $i | grep -q '.eli' 2>/dev/null
299     if [ $? -eq 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 and gm0
319   disknum="0"
320   gmnum="0"
321
322   # We are ready to start setting up the disks, lets read the config and do the actions
323   while read line
324   do
325     echo $line | grep -q "^disk${disknum}=" 2>/dev/null
326     if [ $? -eq 0 ]
327     then
328
329       # Found a disk= entry, lets get the disk we are working on
330       get_value_from_string "${line}"
331       strip_white_space "$VAL"
332       DISK="$VAL"
333
334       echo "${DISK}" | grep -q '^/dev/'
335       if [ $? -ne 0 ] ; then DISK="/dev/$DISK" ; fi
336      
337       # Before we go further, lets confirm this disk really exists
338       if [ ! -e "${DISK}" ] ; 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 ${DISK}
350
351      fi
352
353     # Lets look if this device will be mirrored on another disk
354     echo $line | grep -q "^mirror=" 2>/dev/null
355     if [ $? -eq 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       echo "${MIRRORDISK}" | grep -q '^/dev/'
363       if [ $? -ne 0 ] ; then MIRRORDISK="/dev/$MIRRORDISK" ; fi
364      
365       # Before we go further, lets confirm this disk really exists
366       if [ ! -e "${MIRRORDISK}" ]
367       then
368         exit_err "ERROR: The mirror disk ${MIRRORDISK} does not exist!"
369       fi
370
371       # Make sure we stop any gmirrors on this mirror disk
372       stop_all_gmirror ${MIRRORDISK}
373
374       # Make sure we stop any geli stuff on this mirror disk
375       stop_all_geli ${MIRRORDISK}
376
377       # Make sure we don't have any zpools mirror loaded
378       stop_all_zfs ${MIRRORDISK}
379
380     fi
381
382     # Lets see if we have been given a mirror balance choice
383     echo $line | grep -q "^mirrorbal=" 2>/dev/null
384     if [ $? -eq 0 ]
385     then
386
387       # Found a disk= entry, lets get the disk we are working on
388       get_value_from_string "${line}"
389       strip_white_space "$VAL"
390       MIRRORBAL="$VAL"
391     fi
392
393     echo $line | grep -q "^partition=" 2>/dev/null
394     if [ $? -eq 0 ]
395     then
396       # Found a partition= entry, lets read / set it 
397       get_value_from_string "${line}"
398       strip_white_space "$VAL"
399       PTYPE=`echo $VAL|tr A-Z a-z`
400
401       # We are using free space, figure out the slice number
402       if [ "${PTYPE}" = "free" ]
403       then
404         # Lets figure out what number this slice will be
405         LASTSLICE="`gpart show ${DISK} \
406           | grep -v ${DISK} \
407           | grep -v ' free' \
408           | tr -s '\t' ' ' \
409           | cut -d ' ' -f 4 \
410           | sed '/^$/d' \
411           | tail -n 1`"
412
413         if [ -z "${LASTSLICE}" ]
414         then
415           LASTSLICE="1"
416         else
417             LASTSLICE=$((LASTSLICE+1))
418         fi
419
420         if [ $LASTSLICE -gt 4 ]
421         then
422           exit_err "ERROR: BSD only supports primary partitions, and there are none availble on $DISK"
423         fi
424
425       fi
426     fi
427
428     # Check if we have an image file defined
429     echo $line | grep -q "^image=" 2>/dev/null
430     if [ $? -eq 0 ] ; then
431       # Found an image= entry, lets read / set it
432       get_value_from_string "${line}"
433       strip_white_space "$VAL"
434       IMAGE="$VAL"
435       if [ ! -f "$IMAGE" ] ; then
436         exit_err "$IMAGE file does not exist"
437       fi
438     fi
439
440     # Check if we have a partscheme specified
441     echo $line | grep -q "^partscheme=" 2>/dev/null
442     if [ $? -eq 0 ] ; then
443       # Found a partscheme= entry, lets read / set it 
444       get_value_from_string "${line}"
445       strip_white_space "$VAL"
446       PSCHEME="$VAL"
447       if [ "$PSCHEME" != "GPT" -a "$PSCHEME" != "MBR" ] ; then
448         exit_err "Unknown partition scheme: $PSCHEME" 
449       fi
450     fi
451
452     echo $line | grep -q "^bootManager=" 2>/dev/null
453     if [ $? -eq 0 ]
454     then
455       # Found a bootManager= entry, lets read /set it
456       get_value_from_string "${line}"
457       strip_white_space "$VAL"
458       BMANAGER="$VAL"
459     fi
460
461     echo $line | grep -q "^commitDiskPart" 2>/dev/null
462     if [ $? -eq 0 ]
463     then
464       # Found our flag to commit this disk setup / lets do sanity check and do it
465       if [ ! -z "${DISK}" -a ! -z "${PTYPE}" ]
466       then
467         # Make sure we are only installing ppc to full disk
468         if [ `uname -m` = "powerpc" -o `uname -m` = "powerpc64" ]; then
469           if [ "$PTYPE" != "all" ] ; then
470             exit_err "powerpc can only be installed to a full disk"
471           fi
472         fi
473
474         case ${PTYPE} in
475           all)
476             # If we have a gmirror, lets set it up
477             if [ -n "$MIRRORDISK" ]; then
478               # Default to round-robin if the user didn't specify
479               if [ -z "$MIRRORBAL" ]; then MIRRORBAL="round-robin" ; fi
480
481               _mFile=`echo $DISK | sed 's|/|%|g'`
482               echo "$MIRRORDISK:$MIRRORBAL:gm${gmnum}" >${MIRRORCFGDIR}/$_mFile
483               init_gmirror "$gmnum" "$MIRRORBAL" "$DISK" "$MIRRORDISK"
484
485               # Reset DISK to the gmirror device
486               DISK="/dev/mirror/gm${gmnum}"
487               gmnum=$((gmknum+1))
488             fi
489
490             if [ "$PSCHEME" = "MBR" -o -z "$PSCHEME" ] ; then
491               PSCHEME="MBR"
492               tmpSLICE="${DISK}s1"  
493             else
494               tmpSLICE="${DISK}p1"  
495             fi
496
497             if [ `uname -m` = "powerpc" -o `uname -m` = "powerpc64" ]
498             then
499               PSCHEME="APM"
500               tmpSLICE="${DISK}s1"  
501             fi
502
503             run_gpart_full "${DISK}" "${BMANAGER}" "${PSCHEME}"
504             ;;
505
506           s1|s2|s3|s4)
507             tmpSLICE="${DISK}${PTYPE}" 
508             # Get the number of the slice we are working on
509             s="`echo ${PTYPE} | awk '{print substr($0,length,1)}'`" 
510             run_gpart_slice "${DISK}" "${BMANAGER}" "${s}"
511             ;;
512
513           p1|p2|p3|p4|p5|p6|p7|p8|p9|p10|p11|p12|p13|p14|p15|p16|p17|p18|p19|p20)
514             tmpSLICE="${DISK}${PTYPE}" 
515             # Get the number of the gpt partition we are working on
516             s="`echo ${PTYPE} | awk '{print substr($0,length,1)}'`" 
517             run_gpart_gpt_part "${DISK}" "${BMANAGER}" "${s}"
518             ;;
519
520           free)
521             tmpSLICE="${DISK}s${LASTSLICE}"
522             run_gpart_free "${DISK}" "${LASTSLICE}" "${BMANAGER}"
523             ;;
524
525           image)
526             if [ -z "${IMAGE}" ]
527             then
528               exit_err "ERROR: partition type image specified with no image!"
529             fi 
530             ;;
531
532           *) exit_err "ERROR: Unknown PTYPE: $PTYPE" ;;
533         esac
534         
535
536                 if [ -n "${IMAGE}" ]
537                 then 
538           local DEST
539           
540                   if [ -n "${tmpSLICE}" ]
541           then
542                         DEST="${tmpSLICE}"
543           else 
544                         DEST="${DISK}"
545           fi 
546
547           write_image "${IMAGE}" "${DEST}"
548           check_disk_layout "${DEST}"
549                 fi
550
551         # Now save which disk<num> this is, so we can parse it later during slice partition setup
552         if [ -z "${IMAGE}" ]
553         then
554           _sFile=`echo $tmpSLICE | sed 's|/|-|g'`
555           echo "disk${disknum}" >${SLICECFGDIR}/$_sFile
556         fi
557
558         # Increment our disk counter to look for next disk and unset
559         unset BMANAGER PTYPE DISK MIRRORDISK MIRRORBAL PSCHEME IMAGE
560         disknum=$((disknum+1))
561       else
562         exit_err "ERROR: commitDiskPart was called without procceding disk<num>= and partition= entries!!!" 
563       fi
564     fi
565
566   done <${CFGF}
567
568 };
569
570
571 # Init the gmirror device
572 init_gmirror()
573 {
574     local _mNum=$1
575     local _mBal=$2
576     local _mDisk=$3
577
578     # Create this mirror device
579     rc_halt "gmirror label -vb ${_mBal} gm${_mNum} ${_mDisk}"
580
581     sleep 3
582
583 }
584
585 # Stop all gjournals on disk / slice
586 stop_gjournal()
587 {
588   _gdsk="`echo $1 | sed 's|/dev/||g'`"
589   # Check if we need to shutdown any journals on this drive
590   ls /dev/${_gdsk}*.journal >/dev/null 2>/dev/null
591   if [ $? -eq 0 ]
592   then
593     cd /dev
594     for i in `ls ${_gdsk}*.journal`
595     do
596       rawjournal="`echo ${i} | cut -d '.' -f 1`"
597       gjournal stop -f ${rawjournal} >>${LOGOUT} 2>>${LOGOUT}
598       gjournal clear ${rawjournal} >>${LOGOUT} 2>>${LOGOUT}
599     done
600   fi
601 } ;
602
603
604 # Function to wipe the potential backup gpt table from a disk
605 clear_backup_gpt_table()
606 {
607   echo_log "Clearing gpt backup table location on disk"
608   rc_nohalt "dd if=/dev/zero of=${1} bs=1m count=1"
609   rc_nohalt "dd if=/dev/zero of=${1} bs=1m oseek=`diskinfo ${1} | awk '{print int($3 / (1024*1024)) - 4;}'`"
610 } ;
611
612 # Function which runs gpart and creates a single large APM partition scheme
613 init_apm_full_disk()
614 {
615   _intDISK=$1
616  
617   # Set our sysctl so we can overwrite any geom using drives
618   sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
619
620   # Stop any journaling
621   stop_gjournal "${_intDISK}"
622
623   # Remove any existing partitions
624   delete_all_gpart "${_intDISK}"
625
626   sleep 2
627
628   echo_log "Running gpart on ${_intDISK}"
629   rc_halt "gpart create -s APM ${_intDISK}"
630   rc_halt "gpart add -s 800k -t freebsd-boot ${_intDISK}"
631   
632   echo_log "Stamping boot sector on ${_intDISK}"
633   rc_halt "gpart bootcode -p /boot/boot1.hfs -i 1 ${_intDISK}"
634
635 }
636
637 # Function which runs gpart and creates a single large GPT partition scheme
638 init_gpt_full_disk()
639 {
640   _intDISK=$1
641  
642   # Set our sysctl so we can overwrite any geom using drives
643   sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
644
645   # Stop any journaling
646   stop_gjournal "${_intDISK}"
647
648   # Remove any existing partitions
649   delete_all_gpart "${_intDISK}"
650
651   sleep 2
652
653   echo_log "Running gpart on ${_intDISK}"
654   rc_halt "gpart create -s GPT ${_intDISK}"
655   rc_halt "gpart add -b 34 -s 128 -t freebsd-boot ${_intDISK}"
656   
657   echo_log "Stamping boot sector on ${_intDISK}"
658   rc_halt "gpart bootcode -b /boot/pmbr ${_intDISK}"
659
660 }
661
662 # Function which runs gpart and creates a single large MBR partition scheme
663 init_mbr_full_disk()
664 {
665   _intDISK=$1
666   _intBOOT=$2
667  
668   startblock="2016"
669
670   # Set our sysctl so we can overwrite any geom using drives
671   sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
672
673   # Stop any journaling
674   stop_gjournal "${_intDISK}"
675
676   # Remove any existing partitions
677   delete_all_gpart "${_intDISK}"
678
679   sleep 2
680
681   echo_log "Running gpart on ${_intDISK}"
682   rc_halt "gpart create -s mbr -f active ${_intDISK}"
683
684   # Install new partition setup
685   echo_log "Running gpart add on ${_intDISK}"
686   rc_halt "gpart add -a 4k -t freebsd -i 1 ${_intDISK}"
687   sleep 2
688   
689   echo_log "Cleaning up ${_intDISK}s1"
690   rc_halt "dd if=/dev/zero of=${_intDISK}s1 count=1024"
691   
692   # Make the partition active
693   rc_halt "gpart set -a active -i 1 ${_intDISK}"
694
695   if [ "$_intBOOT" = "bsd" ] ; then
696     echo_log "Stamping boot0 on ${_intDISK}"
697     rc_halt "gpart bootcode -b /boot/boot0 ${_intDISK}"
698   else
699     echo_log "Stamping boot1 on ${_intDISK}"
700     rc_halt "gpart bootcode -b /boot/boot1 ${_intDISK}"
701   fi
702
703 }
704
705 # Function which runs gpart and creates a single large slice
706 run_gpart_full()
707 {
708   DISK=$1
709   BOOT=$2
710   SCHEME=$3
711
712   if [ "$SCHEME" = "APM" ] ; then
713     init_apm_full_disk "$DISK"
714     slice=`echo "${DISK}:1:apm" | sed 's|/|-|g'`
715   elif [ "$SCHEME" = "MBR" ] ; then
716     init_mbr_full_disk "$DISK" "$BOOT"
717     slice=`echo "${DISK}:1:mbr" | sed 's|/|-|g'`
718   else
719     init_gpt_full_disk "$DISK"
720     slice=`echo "${DISK}:1:gpt" | sed 's|/|-|g'`
721   fi
722
723   # Lets save our slice, so we know what to look for in the config file later on
724   if [ -z "$WORKINGSLICES" ]
725   then
726     WORKINGSLICES="${slice}"
727     export WORKINGSLICES
728   else
729     WORKINGSLICES="${WORKINGSLICES} ${slice}"
730     export WORKINGSLICES
731   fi
732 };
733
734 # Function which runs gpart on a specified gpt partition
735 run_gpart_gpt_part()
736 {
737   DISK=$1
738
739   # Set the slice we will use later
740   slice="${1}p${3}"
741  
742   # Set our sysctl so we can overwrite any geom using drives
743   sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
744
745   # Get the number of the slice we are working on
746   slicenum="$3"
747
748   # Stop any journaling
749   stop_gjournal "${slice}"
750
751   # Make sure we have disabled swap on this drive
752   if [ -e "${slice}b" ]
753   then
754    swapoff ${slice}b >/dev/null 2>/dev/null
755    swapoff ${slice}b.eli >/dev/null 2>/dev/null
756   fi
757
758   # Modify partition type
759   echo_log "Running gpart modify on ${DISK}"
760   rc_halt "gpart modify -t freebsd -i ${slicenum} ${DISK}"
761   sleep 2
762
763   # Clean up old partition
764   echo_log "Cleaning up $slice"
765   rc_halt "dd if=/dev/zero of=${DISK}p${slicenum} count=1024"
766
767   sleep 4
768
769   # Init the MBR partition
770   rc_halt "gpart create -s BSD ${DISK}p${slicenum}"
771
772   # Stamp the bootloader
773   sleep 4
774   rc_halt "gpart bootcode -b /boot/boot ${DISK}p${slicenum}"
775
776   # Set the slice to the format we'll be using for gpart later
777   slice=`echo "${1}:${3}:gptslice" | sed 's|/|-|g'`
778
779   # Lets save our slice, so we know what to look for in the config file later on
780   if [ -z "$WORKINGSLICES" ]
781   then
782     WORKINGSLICES="${slice}"
783     export WORKINGSLICES
784   else
785     WORKINGSLICES="${WORKINGSLICES} ${slice}"
786     export WORKINGSLICES
787   fi
788 };
789
790 # Function which runs gpart on a specified s1-4 slice
791 run_gpart_slice()
792 {
793   DISK=$1
794   if [ -n "$2" ]
795   then
796     BMANAGER="$2"
797   fi
798
799   # Set the slice we will use later
800   slice="${1}s${3}"
801  
802   # Set our sysctl so we can overwrite any geom using drives
803   sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
804
805   # Get the number of the slice we are working on
806   slicenum="$3"
807
808   # Stop any journaling
809   stop_gjournal "${slice}"
810
811   # Make sure we have disabled swap on this drive
812   if [ -e "${slice}b" ]
813   then
814    swapoff ${slice}b >/dev/null 2>/dev/null
815    swapoff ${slice}b.eli >/dev/null 2>/dev/null
816   fi
817
818   # Modify partition type
819   echo_log "Running gpart modify on ${DISK}"
820   rc_halt "gpart modify -t freebsd -i ${slicenum} ${DISK}"
821   sleep 2
822
823   # Clean up old partition
824   echo_log "Cleaning up $slice"
825   rc_halt "dd if=/dev/zero of=${DISK}s${slicenum} count=1024"
826
827   sleep 1
828
829   if [ "${BMANAGER}" = "bsd" ]
830   then
831     echo_log "Stamping boot sector on ${DISK}"
832     rc_halt "gpart bootcode -b /boot/boot0 ${DISK}"
833   fi
834
835   # Set the slice to the format we'll be using for gpart later
836   slice=`echo "${1}:${3}:mbr" | sed 's|/|-|g'`
837
838   # Lets save our slice, so we know what to look for in the config file later on
839   if [ -z "$WORKINGSLICES" ]
840   then
841     WORKINGSLICES="${slice}"
842     export WORKINGSLICES
843   else
844     WORKINGSLICES="${WORKINGSLICES} ${slice}"
845     export WORKINGSLICES
846   fi
847 };
848
849 # Function which runs gpart and creates a new slice from free disk space
850 run_gpart_free()
851 {
852   DISK=$1
853   SLICENUM=$2
854   if [ -n "$3" ]
855   then
856     BMANAGER="$3"
857   fi
858
859   # Set our sysctl so we can overwrite any geom using drives
860   sysctl kern.geom.debugflags=16 >>${LOGOUT} 2>>${LOGOUT}
861
862   slice="${DISK}s${SLICENUM}"
863   slicenum="${SLICENUM}" 
864
865   # Working on the first slice, make sure we have MBR setup
866   gpart show ${DISK} >/dev/null 2>/dev/null
867   if [ $? -ne 0 -a "$SLICENUM" = "1" ] ; then
868     echo_log "Initializing disk, no existing MBR setup"
869     rc_halt "gpart create -s mbr ${DISK}"
870   fi
871
872   # Install new partition setup
873   echo_log "Running gpart on ${DISK}"
874   rc_halt "gpart add -a 4k -t freebsd -i ${slicenum} ${DISK}"
875   sleep 2
876   
877   echo_log "Cleaning up $slice"
878   rc_halt "dd if=/dev/zero of=${slice} count=1024"
879
880   sleep 1
881
882   if [ "${BMANAGER}" = "bsd" ]
883   then
884     echo_log "Stamping boot sector on ${DISK}"
885     rc_halt "gpart bootcode -b /boot/boot0 ${DISK}"
886   fi
887
888   slice=`echo "${DISK}:${SLICENUM}:mbr" | sed 's|/|-|g'`
889   # Lets save our slice, so we know what to look for in the config file later on
890   if [ -z "$WORKINGSLICES" ]
891   then
892     WORKINGSLICES="${slice}"
893     export WORKINGSLICES
894   else
895     WORKINGSLICES="${WORKINGSLICES} ${slice}"
896     export WORKINGSLICES
897   fi
898 };