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

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

Fix a bug installing GRUB to additional GPT / ZFS raidz partitions

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