source: src-sh/pc-sysinstall/backend/functions-bsdlabel.sh

Last change on this file was db84e3d, checked in by Kris Moore <kris@…>, 4 months ago

Fix installing on raidX with GPT

  • Property mode set to 100755
File size: 22.3 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-bsdlabel.sh 247735 2013-03-03 23:07:27Z jpaetzel $
27
28# Functions related to disk operations using bsdlabel
29
30# Check if we are are provided a geli password on the nextline of the config
31check_for_enc_pass()
32{
33  CURLINE="${1}"
34 
35  get_next_cfg_line "${CFGF}" "${CURLINE}" 
36  echo ${VAL} | grep -q "^encpass=" 2>/dev/null
37  if [ $? -eq 0 ] ; then
38    # Found a password, return it
39    get_value_from_string "${VAL}"
40    return
41  fi
42
43  export VAL=""
44  return
45};
46
47# On check on the disk-label line if we have any extra vars for this device
48get_fs_line_xvars()
49{
50  ACTIVEDEV="${1}"
51  LINE="${2}"
52
53  echo $LINE | cut -d ' ' -f 4 | grep -q '(' 2>/dev/null
54  if [ $? -ne 0 ] ; then return ; fi
55
56  # See if we are looking for ZFS specific options
57  echo $LINE | grep -q '^ZFS' 2>/dev/null
58  if [ $? -eq 0 ] ; then
59    ZTYPE="NONE"
60    ZFSVARS="`echo $LINE | cut -d ' ' -f 4-20 |cut -d '(' -f 2- | cut -d ')' -f 1 | xargs`"
61
62    echo $ZFSVARS | grep -qE "^(disk|file|mirror|raidz(1|2|3)?|spare|log|cache):" 2>/dev/null
63    if [ $? -eq 0 ] ; then
64       ZTYPE=`echo $ZFSVARS | cut -f1 -d:`
65       tmpVars=`echo $ZFSVARS | sed "s|$ZTYPE: ||g" | sed "s|$ZTYPE:||g"`
66       ZFSVARS=""
67       # make sure we have a '/dev' in front of the extra devices
68       for i in $tmpVars
69       do
70          echo $i | grep -q '/dev/'
71          if [ $? -ne 0 ] ; then
72             ZFSVARS="$ZFSVARS /dev/${i}"
73          else
74             ZFSVARS="$ZFSVARS $i"
75          fi
76       done
77    fi
78
79    # Return the ZFS options
80    if [ "${ZTYPE}" = "NONE" ] ; then
81      VAR="${ACTIVEDEV} ${ZFSVARS}"
82    else
83      VAR="${ZTYPE} ${ACTIVEDEV} ${ZFSVARS}"
84    fi
85    export VAR
86    return
87  fi # End of ZFS block
88
89  # See if we are looking for UFS specific newfs options
90  echo $LINE | grep -q '^UFS' 2>/dev/null
91  if [ $? -eq 0 ] ; then
92    FSVARS="`echo $LINE | cut -d '(' -f 2- | cut -d ')' -f 1 | xargs`"
93    VAR="${FSVARS}"
94    export VAR
95    return
96  fi
97
98  # If we got here, set VAR to empty and export
99  export VAR=""
100  return
101};
102
103# Init each zfs mirror disk with a boot sector so we can failover
104setup_zfs_mirror_parts()
105{
106  _nZFS=""
107  SOUT="$4"
108
109  # Check if the target disk is using GRUB
110  grep -q "$3" ${TMPDIR}/.grub-install 2>/dev/null
111  if [ $? -eq 0 ] ; then
112     local _tBL="GRUB"
113  else
114     local _tBL="bsd"
115  fi
116
117  ZTYPE="`echo ${1} | awk '{print $1}'`"
118
119  # Using mirroring, setup boot partitions on each disk
120  _mirrline="`echo ${1} | sed 's|mirror ||g' | sed 's|raidz1 ||g' | sed 's|raidz2 ||g' | sed 's|raidz3 ||g' | sed 's|raidz ||g'`"
121  for _zvars in $_mirrline
122  do
123    echo "Looping through _zvars: $_zvars" >>${LOGOUT}
124    echo "$_zvars" | grep -q "${2}" 2>/dev/null
125    if [ $? -eq 0 ] ; then continue ; fi
126    if [ -z "$_zvars" ] ; then continue ; fi
127
128    is_disk "$_zvars" >/dev/null 2>/dev/null
129    if [ $? -eq 0 ] ; then
130
131      # Save this disk as one we want to clone the original disk setup to
132      ZFS_CLONE_DISKS="$ZFS_CLONE_DISKS ${_zvars}"
133      export ZFS_CLONE_DISKS
134
135      echo "Setting up ZFS disk $_zvars" >>${LOGOUT}
136      init_gpt_full_disk "$_zvars" "$_tBL" >/dev/null 2>/dev/null
137
138      # If we are not using GRUB we need to add pmbr / gptzfsboot
139      if [ "$_tBL" != "GRUB" ] ; then
140        rc_halt "gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ${_zvars}" >/dev/null 2>/dev/null
141      fi
142      _nZFS="$_nZFS ${_zvars}p2"       
143    else
144      _nZFS="$_nZFS ${_zvars}" 
145    fi 
146  done
147
148  # Export the ZXTRAOPTS
149  ZXTRAOPTS="$ZTYPE $2 `echo $_nZFS | tr -s ' '`"
150  export ZXTRAOPTS
151} ;
152
153# Function which creates a unique label name for the specified mount
154gen_glabel_name()
155{
156  MOUNT="$1"
157  TYPE="$2"
158  NUM="0"
159  MAXNUM="20"
160
161  if [ "$TYPE" = "ZFS" ] ; then
162    NAME="zpool"
163  elif [ "$MOUNT" = "/" ] ; then
164    NAME="rootfs"
165  else
166    # If doing a swap partition, also rename it
167    if [ "${TYPE}" = "SWAP" ]
168    then
169      NAME="swap"
170    else
171      NAME="`echo $MOUNT | sed 's|/||g' | sed 's| ||g'`"
172    fi
173  fi
174
175  # Loop through and break when we find our first available label
176  while
177  Z=1
178  do
179    glabel status | grep -q "${NAME}${NUM}" 2>/dev/null
180    if [ $? -ne 0 ]
181    then
182      break
183    else
184        NUM=$((NUM+1))
185    fi
186
187    if [ $NUM -gt $MAXNUM ]
188    then
189      exit_err "Cannot allocate additional glabel name for $NAME"
190      break
191    fi
192  done
193   
194
195  export VAL="${NAME}${NUM}" 
196};
197
198# Function to determine the size we can safely use when 0 is specified
199get_autosize()
200{
201  # Disk tag to look for
202  dTag="$1"
203
204  # Total MB Avail
205  get_disk_mediasize_mb "$2"
206  local _aSize=$VAL
207
208  while read aline
209  do
210    # Check for data on this slice
211    echo $aline | grep -q "^${_dTag}-part=" 2>/dev/null
212    if [ $? -ne 0 ] ; then continue ; fi
213
214    get_value_from_string "${aline}"
215    ASTRING="$VAL"
216
217    # Get the size of this partition
218    SIZE=`echo $ASTRING | tr -s '\t' ' ' | cut -d ' ' -f 2`
219    if [ $SIZE -eq 0 ] ; then continue ; fi
220    _aSize=`expr $_aSize - $SIZE`
221  done <${CFGF}
222
223  # Pad the size a bit
224  _aSize=`expr $_aSize - 2`
225
226  VAL="$_aSize"
227  export VAL
228};
229
230# Function to setup partitions using gpart
231setup_gpart_partitions()
232{
233  local _dTag="$1"
234  local _pDisk="$2"
235  local _wSlice="$3"
236  local _sNum="$4"
237  local _pType="$5"
238  FOUNDPARTS="1"
239  USEDAUTOSIZE=0
240
241  # Lets read in the config file now and setup our partitions
242  if [ "${_pType}" = "gpt" ] ; then
243    CURPART="2"
244  elif [ "${_pType}" = "apm" ] ; then
245    CURPART="3"
246  else
247    PARTLETTER="a"
248    CURPART="1"
249    if [ "${_pType}" = "mbr" ] ; then
250      rc_halt "gpart create -s BSD ${_wSlice}"
251    fi
252  fi
253
254  # Check if the target disk is using GRUB
255  grep -q "$3" ${TMPDIR}/.grub-install 2>/dev/null
256  if [ $? -eq 0 ] ; then
257     local _tBL="GRUB"
258  else
259     local _tBL="bsd"
260  fi
261
262  # Unset ZFS_CLONE_DISKS
263  #ZFS_CLONE_DISKS=""
264
265  while read line
266  do
267    # Check for data on this slice
268    echo $line | grep -q "^${_dTag}-part=" 2>/dev/null
269    if [ $? -eq 0 ]
270    then
271      FOUNDPARTS="0"
272      # Found a slice- entry, lets get the slice info
273      get_value_from_string "${line}"
274      STRING="$VAL"
275
276      # We need to split up the string now, and pick out the variables
277      FS=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 1` 
278      SIZE=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 2` 
279      MNT=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 3` 
280
281      # Check if we have a .eli extension on this FS
282      echo ${FS} | grep -q ".eli" 2>/dev/null
283      if [ $? -eq 0 ]
284      then
285        FS="`echo ${FS} | cut -d '.' -f 1`"
286        ENC="ON"
287        check_for_enc_pass "${line}"
288        if [ "${VAL}" != "" ] ; then
289          # We have a user supplied password, save it for later
290          ENCPASS="${VAL}" 
291        fi
292      else
293        ENC="OFF"
294      fi
295
296      # Check if the user tried to setup / as an encrypted partition
297      check_for_mount "${MNT}" "/"
298      if [ $? -eq 0 -a "${ENC}" = "ON" ]
299      then
300        export USINGENCROOT="0"
301      fi
302         
303      # Now check that these values are sane
304      case $FS in
305        UFS|UFS+S|UFS+J|UFS+SUJ|ZFS|SWAP) ;;
306       *) exit_err "ERROR: Invalid file system specified on $line" ;;
307      esac
308
309      # Check that we have a valid size number
310      expr $SIZE + 1 >/dev/null 2>/dev/null
311      if [ $? -ne 0 ]; then
312        exit_err "ERROR: The size specified on $line is invalid"
313      fi
314
315      # Check that the mount-point starts with /
316      echo "$MNT" | grep -qe "^/" -e "^none" 2>/dev/null
317      if [ $? -ne 0 ]; then
318        exit_err "ERROR: The mount-point specified on $line is invalid"
319      fi
320
321      if [ "$SIZE" = "0" ]
322      then
323        if [ $USEDAUTOSIZE -eq 1 ] ; then
324          exit_err "ERROR: You can not have two partitions with a size of 0 specified!"
325        fi
326        case ${_pType} in
327          gpt|apm) get_autosize "${_dTag}" "$_pDisk" ;;
328                *) get_autosize "${_dTag}" "$_wSlice" ;;
329        esac
330        SOUT="-s ${VAL}M"
331        USEDAUTOSIZE=1
332      else
333        SOUT="-s ${SIZE}M"
334      fi
335
336      # Check if we found a valid root partition
337      check_for_mount "${MNT}" "/"
338      if [ $? -eq 0 ] ; then
339        export FOUNDROOT="1"
340        if [ "${CURPART}" = "2" -a "$_pType" = "gpt" ] ; then
341          export FOUNDROOT="0"
342        fi
343        if [ "${CURPART}" = "3" -a "$_pType" = "apm" ] ; then
344          export FOUNDROOT="0"
345        fi
346        if [ "${CURPART}" = "1" -a "$_pType" = "mbr" ] ; then
347          export FOUNDROOT="0"
348        fi
349        if [ "${CURPART}" = "1" -a "$_pType" = "gptslice" ] ; then
350          export FOUNDROOT="0"
351        fi
352      fi
353
354      check_for_mount "${MNT}" "/boot"
355      if [ $? -eq 0 ] ; then
356        export USINGBOOTPART="0"
357        if [ "${CURPART}" != "2" -a "${_pType}" = "gpt" ] ; then
358            exit_err "/boot partition must be first partition"
359        fi
360        if [ "${CURPART}" != "3" -a "${_pType}" = "apm" ] ; then
361            exit_err "/boot partition must be first partition"
362        fi
363        if [ "${CURPART}" != "1" -a "${_pType}" = "mbr" ] ; then
364            exit_err "/boot partition must be first partition"
365        fi
366        if [ "${CURPART}" != "1" -a "${_pType}" = "gptslice" ] ; then
367            exit_err "/boot partition must be first partition"
368        fi
369
370        if [ "${FS}" != "UFS" -a "${FS}" != "UFS+S" -a "${FS}" != "UFS+J" -a "${FS}" != "UFS+SUJ" ] ; then
371          exit_err "/boot partition must be formatted with UFS"
372        fi
373      fi
374
375      # Generate a unique label name for this mount
376      gen_glabel_name "${MNT}" "${FS}"
377      PLABEL="${VAL}"
378
379      # Get any extra options for this fs / line
380      if [ "${_pType}" = "gpt" ] ; then
381        get_fs_line_xvars "${_pDisk}p${CURPART}" "${STRING}"
382      elif [ "${_pType}" = "apm" ] ; then
383        get_fs_line_xvars "${_pDisk}s${CURPART}" "${STRING}"
384      else
385        get_fs_line_xvars "${_wSlice}${PARTLETTER}" "${STRING}"
386      fi
387      XTRAOPTS="$VAR"
388
389      # Check if using zfs mirror
390      echo ${XTRAOPTS} | grep -q -e "mirror" -e "raidz"
391      if [ $? -eq 0 -a "$FS" = "ZFS" ] ; then
392        if [ "${_pType}" = "gpt" -o "${_pType}" = "gptslice" ] ; then
393          setup_zfs_mirror_parts "${XTRAOPTS}" "${_pDisk}p${CURPART}" "${_pDisk}" "${SOUT}"
394          XTRAOPTS="${ZXTRAOPTS}"
395        elif [ "${_pType}" = "apm" ] ; then
396          setup_zfs_mirror_parts "${XTRAOPTS}" "${_pDisk}s${CURPART}" "${_pDisk}" "${SOUT}"
397          XTRAOPTS="${ZXTRAOPTS}"
398        else
399          setup_zfs_mirror_parts "${XTRAOPTS}" "${_wSlice}${PARTLETTER}" "${_pDisk}" "${SOUT}"
400          XTRAOPTS="${ZXTRAOPTS}"
401        fi
402      fi
403
404      # Figure out the gpart type to use
405      case ${FS} in
406        ZFS) PARTYPE="freebsd-zfs" ;;
407        SWAP) PARTYPE="freebsd-swap" ;;
408        *) PARTYPE="freebsd-ufs" ;;
409      esac
410
411      # Create the partition
412      if [ "${_pType}" = "gpt" ] ; then
413        sleep 2
414        if [ "${INSTALLTYPE}" = "GhostBSD" ]
415        then
416          aCmd="gpart add ${SOUT} -t ${PARTYPE} ${_pDisk}"
417        else
418          aCmd="gpart add -a 4k ${SOUT} -t ${PARTYPE} ${_pDisk}"
419        fi
420      elif [ "${_pType}" = "gptslice" ]; then
421        sleep 2
422        aCmd="gpart add ${SOUT} -t ${PARTYPE} ${_wSlice}"
423      elif [ "${_pType}" = "apm" ]; then
424        sleep 2
425        aCmd="gpart add ${SOUT} -t ${PARTYPE} ${_pDisk}"
426      else
427        sleep 2
428        # MBR type
429        aCmd="gpart add ${SOUT} -t ${PARTYPE} -i ${CURPART} ${_wSlice}"
430      fi
431
432      # Run the gpart add command now
433      rc_halt "$aCmd"
434
435      # Check if we need to clone this layout to a ZFS mirror/raidz disk
436      if [ -n "$ZFS_CLONE_DISKS" ] ; then
437         for zC in $ZFS_CLONE_DISKS
438         do
439            echo_log "Cloning disk layout to ZFS disk ${zC}"
440            rc_halt "gpart add -a 4k ${SOUT} -t ${PARTYPE} ${zC}"
441            if [ "$PARTYPE" = "freebsd-swap" ] ; then
442               # If this is the first device, save the original swap dev
443               if [ -z "$ZFS_SWAP_DEVS" ] ; then
444                  ZFS_SWAP_DEVS="${_pDisk}p${CURPART}"
445               fi
446               # Save this swap device, we will gmirror it later
447               ZFS_SWAP_DEVS="${ZFS_SWAP_DEVS} ${zC}p${CURPART}"
448               export ZFS_SWAP_DEVS
449            fi
450         done
451      fi
452
453      # Check if this is a root / boot partition, and stamp the right loader
454      for TESTMNT in `echo ${MNT} | sed 's|,| |g'`
455      do
456        if [ "${TESTMNT}" = "/" -a -z "${BOOTTYPE}" ] ; then
457           BOOTTYPE="${PARTYPE}" 
458        fi
459        if [ "${TESTMNT}" = "/boot" ]  ; then
460           BOOTTYPE="${PARTYPE}" 
461        fi
462      done 
463
464      # Save this data to our partition config dir
465      if [ "${_pType}" = "gpt" ] ; then
466        _dFile="`echo $_pDisk | sed 's|/|-|g'`"
467        echo "${FS}#${MNT}#${ENC}#${PLABEL}#GPT#${XTRAOPTS}" >${PARTDIR}/${_dFile}p${CURPART}
468
469        # Clear out any headers
470        sleep 2
471        dd if=/dev/zero of=${_pDisk}p${CURPART} count=2048 2>/dev/null
472
473        # If we have a enc password, save it as well
474        if [ -n "${ENCPASS}" ] ; then
475          echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}p${CURPART}-encpass
476        fi
477      elif [ "${_pType}" = "apm" ] ; then
478        _dFile="`echo $_pDisk | sed 's|/|-|g'`"
479        echo "${FS}#${MNT}#${ENC}#${PLABEL}#GPT#${XTRAOPTS}" >${PARTDIR}/${_dFile}s${CURPART}
480
481        # Clear out any headers
482        sleep 2
483        dd if=/dev/zero of=${_pDisk}s${CURPART} count=2048 2>/dev/null
484
485        # If we have a enc password, save it as well
486        if [ -n "${ENCPASS}" ] ; then
487          echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}s${CURPART}-encpass
488        fi
489      else
490        # MBR Partition or GPT slice
491        _dFile="`echo $_wSlice | sed 's|/|-|g'`"
492        echo "${FS}#${MNT}#${ENC}#${PLABEL}#MBR#${XTRAOPTS}#${IMAGE}" >${PARTDIR}/${_dFile}${PARTLETTER}
493        # Clear out any headers
494        sleep 2
495        dd if=/dev/zero of=${_wSlice}${PARTLETTER} count=2048 2>/dev/null
496
497        # If we have a enc password, save it as well
498        if [ -n "${ENCPASS}" ] ; then
499          echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}${PARTLETTER}-encpass
500        fi
501      fi
502
503
504      # Increment our parts counter
505      if [ "$_pType" = "gpt" -o "$_pType" = "apm" ] ; then
506          CURPART=$((CURPART+1))
507        # If this is a gpt/apm partition,
508        # we can continue and skip the MBR part letter stuff
509        continue
510      else
511          CURPART=$((CURPART+1))
512        if [ "$CURPART" = "3" ] ; then CURPART="4" ; fi
513      fi
514
515
516      # This partition letter is used, get the next one
517      case ${PARTLETTER} in
518        a) PARTLETTER="b" ;;
519        b) PARTLETTER="d" ;;
520        d) PARTLETTER="e" ;;
521        e) PARTLETTER="f" ;;
522        f) PARTLETTER="g" ;;
523        g) PARTLETTER="h" ;;
524        h) PARTLETTER="ERR" ;;
525        *) exit_err "ERROR: bsdlabel only supports up to letter h for partitions." ;;
526      esac
527
528    fi # End of subsection locating a slice in config
529
530    echo $line | grep -q "^commitDiskLabel" 2>/dev/null
531    if [ $? -eq 0 -a "${FOUNDPARTS}" = "0" ]
532    then
533
534      # If this is the boot disk, stamp the right gptboot
535      if [ ! -z "${BOOTTYPE}" -a "$_pType" = "gpt" -a "$_tBL" != "GRUB" ] ; then
536        case ${BOOTTYPE} in
537          freebsd-ufs) rc_halt "gpart bootcode -p /boot/gptboot -i 1 ${_pDisk}" ;;
538          freebsd-zfs) rc_halt "gpart bootcode -p /boot/gptzfsboot -i 1 ${_pDisk}" ;;
539        esac
540      fi
541
542      # Make sure to stamp the MBR loader
543      if [ "$_pType" = "mbr" -a "$_tBL" != "GRUB" ] ; then
544        rc_halt "gpart bootcode -b /boot/boot ${_wSlice}"
545      fi
546
547      # Found our flag to commit this label setup, check that we found at least 1 partition
548      if [ "${CURPART}" = "1" ] ; then
549        exit_err "ERROR: commitDiskLabel was called without any partition entries for it!"
550      fi
551
552      break
553    fi
554  done <${CFGF}
555};
556
557# Reads through the config and sets up a BSDLabel for the given slice
558populate_disk_label()
559{
560  if [ -z "${1}" ]
561  then
562    exit_err "ERROR: populate_disk_label() called without argument!"
563  fi
564
565  # Set some vars from the given working slice
566  diskid="`echo $1 | cut -d ':' -f 1`" 
567  disk="`echo $1 | cut -d ':' -f 1 | sed 's|-|/|g'`" 
568  slicenum="`echo $1 | cut -d ':' -f 2`" 
569  type="`echo $1 | cut -d ':' -f 3`" 
570 
571  # Set WRKSLICE based upon format we are using
572  if [ "$type" = "mbr" ] ; then
573    wrkslice="${diskid}s${slicenum}"
574  fi
575  if [ "$type" = "apm" ] ; then
576    wrkslice="${diskid}s${slicenum}"
577  fi
578  if [ "$type" = "gpt" -o "$type" = "gptslice" ] ; then
579    wrkslice="${diskid}p${slicenum}"
580  fi
581
582  if [ ! -e "${SLICECFGDIR}/${wrkslice}" ] ; then
583    exit_err "ERROR: Missing SLICETAG data. This shouldn't happen - please let the developers know"
584  fi
585
586  disktag="`cat ${SLICECFGDIR}/${wrkslice}`"
587  slicedev="`echo $wrkslice | sed 's|-|/|g'`"
588 
589  # Setup the partitions with gpart
590  setup_gpart_partitions "${disktag}" "${disk}" "${slicedev}" "${slicenum}" "${type}"
591
592};
593
594# Function which reads in the disk slice config, and performs it
595setup_disk_label()
596{
597  # We are ready to start setting up the label, lets read the config and do the actions
598  # First confirm that we have a valid WORKINGSLICES
599  if [ -z "${WORKINGSLICES}" ]; then
600    exit_err "ERROR: No slices were setup! Please report this to the maintainers"
601  fi
602
603  # Check that the slices we have did indeed get setup and gpart worked
604  for i in $WORKINGSLICES
605  do
606    disk="`echo $i | cut -d '-' -f 1`" 
607    pnum="`echo $i | cut -d '-' -f 2`" 
608    type="`echo $i | cut -d '-' -f 3`" 
609    if [ "$type" = "mbr" -a ! -e "${disk}s${pnum}" ] ; then
610      exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
611    fi
612    if [ "$type" = "gpt" -a ! -e "${disk}p${pnum}" ] ; then
613      exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
614    fi
615    if [ "$type" = "apm" -a ! -e "${disk}s${pnum}" ] ; then
616      exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
617    fi
618    if [ "$type" = "gptslice" -a ! -e "${disk}p${pnum}" ] ; then
619      exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
620    fi
621  done
622
623  # Setup some files which we'll be referring to
624  export LABELLIST="${TMPDIR}/workingLabels"
625  rm $LABELLIST >/dev/null 2>/dev/null
626
627  # Set our flag to determine if we've got a valid root partition in this setup
628  export FOUNDROOT="-1"
629
630  # Check if we are using a /boot partition
631  export USINGBOOTPART="1"
632 
633  # Set encryption on root check
634  export USINGENCROOT="1"
635 
636  # Make the tmp directory where we'll store FS info & mount-points
637  rm -rf ${PARTDIR} >/dev/null 2>/dev/null
638  mkdir -p ${PARTDIR} >/dev/null 2>/dev/null
639  rm -rf ${PARTDIR}-enc >/dev/null 2>/dev/null
640  mkdir -p ${PARTDIR}-enc >/dev/null 2>/dev/null
641
642  for i in $WORKINGSLICES
643  do
644    populate_disk_label "${i}"
645  done
646
647  # Check if we made a root partition
648  if [ "$FOUNDROOT" = "-1" ]
649  then
650    exit_err "ERROR: No root (/) partition specified!!"
651  fi
652
653  # Check if we made a root partition
654  if [ "$FOUNDROOT" = "1" -a "${USINGBOOTPART}" != "0" ]
655  then
656    exit_err "ERROR: (/) partition isn't first partition on disk!"
657  fi
658
659};
660
661check_fstab_mbr()
662{
663  local SLICE
664  local FSTAB
665
666  if [ -z "$2" ]
667  then
668        return 1
669  fi
670
671  SLICE="$1"
672  FSTAB="$2/etc/fstab"
673
674  if [ -f "${FSTAB}" ]
675  then
676    PARTLETTER=`echo "$SLICE" | sed -E 's|^.+([a-h])$|\1|'`
677
678    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
679    if [ $? -eq 0 ]
680    then
681      if [ "${PARTLETTER}" = "a" ]
682      then
683        FOUNDROOT="0"
684      else
685        FOUNDROOT="1"
686      fi
687
688      ROOTIMAGE="1"
689
690      export FOUNDROOT
691      export ROOTIMAGE
692    fi
693
694    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
695    if [ $? -eq 0 ]
696    then
697      if [ "${PARTLETTER}" = "a" ]
698      then
699        USINGBOOTPART="0"
700      else
701        exit_err "/boot partition must be first partition"
702      fi
703      export USINGBOOTPART
704    fi
705
706    return 0
707  fi
708
709  return 1
710};
711
712check_fstab_gpt()
713{
714  local SLICE
715  local FSTAB
716
717  if [ -z "$2" ]
718  then
719        return 1
720  fi
721
722  SLICE="$1"
723  FSTAB="$2/etc/fstab"
724
725  if [ -f "${FSTAB}" ]
726  then
727    PARTNUMBER=`echo "${SLICE}" | sed -E 's|^.+p([0-9]*)$|\1|'`
728
729    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
730    if [ $? -eq 0 ]
731    then
732      if [ "${PARTNUMBER}" = "2" ]
733      then
734        FOUNDROOT="0"
735      else
736        FOUNDROOT="1"
737      fi
738
739      ROOTIMAGE="1"
740
741      export FOUNDROOT
742      export ROOTIMAGE
743    fi
744
745    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
746    if [ $? -eq 0 ]
747    then
748      if [ "${PARTNUMBER}" = "2" ]
749      then
750        USINGBOOTPART="0"
751      else
752        exit_err "/boot partition must be first partition"
753      fi
754      export USINGBOOTPART
755    fi
756
757    return 0
758  fi
759
760
761  return 1
762};
763
764check_disk_layout()
765{
766  local SLICES
767  local TYPE
768  local DISK
769  local RES
770  local F
771
772  DISK="$1"
773  TYPE="MBR"
774
775  if [ -z "${DISK}" ]
776  then
777        return 1
778  fi
779
780  SLICES_MBR=`ls /dev/${DISK}s[1-4]*[a-h]* 2>/dev/null`
781  SLICES_GPT=`ls /dev/${DISK}p[0-9]* 2>/dev/null`
782  SLICES_SLICE=`ls /dev/${DISK}[a-h]* 2>/dev/null`
783
784  if [ -n "${SLICES_MBR}" ]
785  then
786    SLICES="${SLICES_MBR}"
787    TYPE="MBR"
788    RES=0
789  fi
790  if [ -n "${SLICES_GPT}" ]
791  then
792    SLICES="${SLICES_GPT}"
793    TYPE="GPT"
794    RES=0
795  fi
796  if [ -n "${SLICES_SLICE}" ]
797  then
798    SLICES="${SLICES_SLICE}"
799    TYPE="MBR"
800    RES=0
801  fi
802 
803  for slice in ${SLICES}
804  do
805    F=1
806    mount ${slice} /mnt 2>/dev/null
807    if [ $? -ne 0 ]
808    then
809      continue
810    fi
811
812    if [ "${TYPE}" = "MBR" ]
813    then
814          check_fstab_mbr "${slice}" "/mnt"
815      F="$?"
816
817    elif [ "${TYPE}" = "GPT" ]
818    then
819          check_fstab_gpt "${slice}" "/mnt"
820      F="$?"
821    fi
822
823    if [ ${F} -eq 0 ]
824    then
825      umount /mnt
826      break
827    fi
828
829    umount /mnt
830  done
831
832  return ${RES}
833};
Note: See TracBrowser for help on using the repository browser.