source: src-sh/pc-sysinstall/backend/functions-disk.sh @ dfee0c1

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since dfee0c1 was dfee0c1, checked in by Kris Moore <kris@…>, 11 months ago

Add option for bootManager=GRUB-slice

This will load GRUB on the target installation slice, leaving alone the MBR

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