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

9.1-release9.2-releasereleng/10.0releng/10.0.1
Last change on this file since 1620346 was 1620346, checked in by Kris Moore <kris@…>, 12 months ago

Initial import of PC-BSD /current/ SVN repo

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