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

releng/10.0.1releng/10.0.2releng/10.0.3
Last change on this file since 07e446b was 07e446b, checked in by Eric Turgeon <ericturgeon.bsd@…>, 8 months ago

Replacing ${_wSlice} by ${_pSlice} for GPT.

GPT can not be us like MBR. We can not create label in the GPT partition like MBR.

  • Property mode set to 100755
File size: 22.1 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     _tBL="GRUB"
113  else
114     _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  # Unset ZFS_CLONE_DISKS
255  #ZFS_CLONE_DISKS=""
256
257  while read line
258  do
259    # Check for data on this slice
260    echo $line | grep -q "^${_dTag}-part=" 2>/dev/null
261    if [ $? -eq 0 ]
262    then
263      FOUNDPARTS="0"
264      # Found a slice- entry, lets get the slice info
265      get_value_from_string "${line}"
266      STRING="$VAL"
267
268      # We need to split up the string now, and pick out the variables
269      FS=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 1` 
270      SIZE=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 2` 
271      MNT=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 3` 
272
273      # Check if we have a .eli extension on this FS
274      echo ${FS} | grep -q ".eli" 2>/dev/null
275      if [ $? -eq 0 ]
276      then
277        FS="`echo ${FS} | cut -d '.' -f 1`"
278        ENC="ON"
279        check_for_enc_pass "${line}"
280        if [ "${VAL}" != "" ] ; then
281          # We have a user supplied password, save it for later
282          ENCPASS="${VAL}" 
283        fi
284      else
285        ENC="OFF"
286      fi
287
288      # Check if the user tried to setup / as an encrypted partition
289      check_for_mount "${MNT}" "/"
290      if [ $? -eq 0 -a "${ENC}" = "ON" ]
291      then
292        export USINGENCROOT="0"
293      fi
294         
295      # Now check that these values are sane
296      case $FS in
297        UFS|UFS+S|UFS+J|UFS+SUJ|ZFS|SWAP) ;;
298       *) exit_err "ERROR: Invalid file system specified on $line" ;;
299      esac
300
301      # Check that we have a valid size number
302      expr $SIZE + 1 >/dev/null 2>/dev/null
303      if [ $? -ne 0 ]; then
304        exit_err "ERROR: The size specified on $line is invalid"
305      fi
306
307      # Check that the mount-point starts with /
308      echo "$MNT" | grep -qe "^/" -e "^none" 2>/dev/null
309      if [ $? -ne 0 ]; then
310        exit_err "ERROR: The mount-point specified on $line is invalid"
311      fi
312
313      if [ "$SIZE" = "0" ]
314      then
315        if [ $USEDAUTOSIZE -eq 1 ] ; then
316          exit_err "ERROR: You can not have two partitions with a size of 0 specified!"
317        fi
318        case ${_pType} in
319          gpt|apm) get_autosize "${_dTag}" "$_pDisk" ;;
320                *) get_autosize "${_dTag}" "$_wSlice" ;;
321        esac
322        SOUT="-s ${VAL}M"
323        USEDAUTOSIZE=1
324      else
325        SOUT="-s ${SIZE}M"
326      fi
327
328      # Check if we found a valid root partition
329      check_for_mount "${MNT}" "/"
330      if [ $? -eq 0 ] ; then
331        export FOUNDROOT="1"
332        if [ "${CURPART}" = "2" -a "$_pType" = "gpt" ] ; then
333          export FOUNDROOT="0"
334        fi
335        if [ "${CURPART}" = "3" -a "$_pType" = "apm" ] ; then
336          export FOUNDROOT="0"
337        fi
338        if [ "${CURPART}" = "1" -a "$_pType" = "mbr" ] ; then
339          export FOUNDROOT="0"
340        fi
341        if [ "${CURPART}" = "1" -a "$_pType" = "gptslice" ] ; then
342          export FOUNDROOT="0"
343        fi
344      fi
345
346      check_for_mount "${MNT}" "/boot"
347      if [ $? -eq 0 ] ; then
348        export USINGBOOTPART="0"
349        if [ "${CURPART}" != "2" -a "${_pType}" = "gpt" ] ; then
350            exit_err "/boot partition must be first partition"
351        fi
352        if [ "${CURPART}" != "3" -a "${_pType}" = "apm" ] ; then
353            exit_err "/boot partition must be first partition"
354        fi
355        if [ "${CURPART}" != "1" -a "${_pType}" = "mbr" ] ; then
356            exit_err "/boot partition must be first partition"
357        fi
358        if [ "${CURPART}" != "1" -a "${_pType}" = "gptslice" ] ; then
359            exit_err "/boot partition must be first partition"
360        fi
361
362        if [ "${FS}" != "UFS" -a "${FS}" != "UFS+S" -a "${FS}" != "UFS+J" -a "${FS}" != "UFS+SUJ" ] ; then
363          exit_err "/boot partition must be formatted with UFS"
364        fi
365      fi
366
367      # Generate a unique label name for this mount
368      gen_glabel_name "${MNT}" "${FS}"
369      PLABEL="${VAL}"
370
371      # Get any extra options for this fs / line
372      if [ "${_pType}" = "gpt" ] ; then
373        get_fs_line_xvars "${_pDisk}p${CURPART}" "${STRING}"
374      elif [ "${_pType}" = "apm" ] ; then
375        get_fs_line_xvars "${_pDisk}s${CURPART}" "${STRING}"
376      else
377        get_fs_line_xvars "${_wSlice}${PARTLETTER}" "${STRING}"
378      fi
379      XTRAOPTS="$VAR"
380
381      # Check if using zfs mirror
382      echo ${XTRAOPTS} | grep -q -e "mirror" -e "raidz"
383      if [ $? -eq 0 -a "$FS" = "ZFS" ] ; then
384        if [ "${_pType}" = "gpt" -o "${_pType}" = "gptslice" ] ; then
385          setup_zfs_mirror_parts "${XTRAOPTS}" "${_pDisk}p${CURPART}" "${_pDisk}" "${SOUT}"
386          XTRAOPTS="${ZXTRAOPTS}"
387        elif [ "${_pType}" = "apm" ] ; then
388          setup_zfs_mirror_parts "${XTRAOPTS}" "${_pDisk}s${CURPART}" "${_pDisk}" "${SOUT}"
389          XTRAOPTS="${ZXTRAOPTS}"
390        else
391          setup_zfs_mirror_parts "${XTRAOPTS}" "${_wSlice}${PARTLETTER}" "${_pDisk}" "${SOUT}"
392          XTRAOPTS="${ZXTRAOPTS}"
393        fi
394      fi
395
396      # Figure out the gpart type to use
397      case ${FS} in
398        ZFS) PARTYPE="freebsd-zfs" ;;
399        SWAP) PARTYPE="freebsd-swap" ;;
400        *) PARTYPE="freebsd-ufs" ;;
401      esac
402
403      # Create the partition
404      if [ "${_pType}" = "gpt" ] ; then
405        sleep 2
406        aCmd="gpart add -a 4k ${SOUT} -t ${PARTYPE} ${_pDisk}"
407      elif [ "${_pType}" = "gptslice" ]; then
408        sleep 2
409        aCmd="gpart add ${SOUT} -t ${PARTYPE} ${_pDisk}"
410      elif [ "${_pType}" = "apm" ]; then
411        sleep 2
412        aCmd="gpart add ${SOUT} -t ${PARTYPE} ${_pDisk}"
413      else
414        sleep 2
415        # MBR type
416        aCmd="gpart add ${SOUT} -t ${PARTYPE} -i ${CURPART} ${_wSlice}"
417      fi
418
419      # Run the gpart add command now
420      rc_halt "$aCmd"
421
422      # Check if we need to clone this layout to a ZFS mirror/raidz disk
423      if [ -n "$ZFS_CLONE_DISKS" ] ; then
424         for zC in $ZFS_CLONE_DISKS
425         do
426            echo_log "Cloning disk layout to ZFS disk ${zC}"
427            rc_halt "gpart add -a 4k ${SOUT} -t ${PARTYPE} ${zC}"
428            if [ "$PARTYPE" = "freebsd-swap" ] ; then
429               # If this is the first device, save the original swap dev
430               if [ -z "$ZFS_SWAP_DEVS" ] ; then
431                  ZFS_SWAP_DEVS="${_pDisk}p${CURPART}"
432               fi
433               # Save this swap device, we will gmirror it later
434               ZFS_SWAP_DEVS="${ZFS_SWAP_DEVS} ${zC}p${CURPART}"
435               export ZFS_SWAP_DEVS
436            fi
437         done
438      fi
439
440      # Check if this is a root / boot partition, and stamp the right loader
441      for TESTMNT in `echo ${MNT} | sed 's|,| |g'`
442      do
443        if [ "${TESTMNT}" = "/" -a -z "${BOOTTYPE}" ] ; then
444           BOOTTYPE="${PARTYPE}" 
445        fi
446        if [ "${TESTMNT}" = "/boot" ]  ; then
447           BOOTTYPE="${PARTYPE}" 
448        fi
449      done 
450
451      # Save this data to our partition config dir
452      if [ "${_pType}" = "gpt" ] ; then
453        _dFile="`echo $_pDisk | sed 's|/|-|g'`"
454        echo "${FS}#${MNT}#${ENC}#${PLABEL}#GPT#${XTRAOPTS}" >${PARTDIR}/${_dFile}p${CURPART}
455
456        # Clear out any headers
457        sleep 2
458        dd if=/dev/zero of=${_pDisk}p${CURPART} count=2048 2>/dev/null
459
460        # If we have a enc password, save it as well
461        if [ -n "${ENCPASS}" ] ; then
462          echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}p${CURPART}-encpass
463        fi
464      elif [ "${_pType}" = "apm" ] ; then
465        _dFile="`echo $_pDisk | sed 's|/|-|g'`"
466        echo "${FS}#${MNT}#${ENC}#${PLABEL}#GPT#${XTRAOPTS}" >${PARTDIR}/${_dFile}s${CURPART}
467
468        # Clear out any headers
469        sleep 2
470        dd if=/dev/zero of=${_pDisk}s${CURPART} count=2048 2>/dev/null
471
472        # If we have a enc password, save it as well
473        if [ -n "${ENCPASS}" ] ; then
474          echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}s${CURPART}-encpass
475        fi
476      else
477        # MBR Partition or GPT slice
478        _dFile="`echo $_wSlice | sed 's|/|-|g'`"
479        echo "${FS}#${MNT}#${ENC}#${PLABEL}#MBR#${XTRAOPTS}#${IMAGE}" >${PARTDIR}/${_dFile}${PARTLETTER}
480        # Clear out any headers
481        sleep 2
482        dd if=/dev/zero of=${_wSlice}${PARTLETTER} count=2048 2>/dev/null
483
484        # If we have a enc password, save it as well
485        if [ -n "${ENCPASS}" ] ; then
486          echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}${PARTLETTER}-encpass
487        fi
488      fi
489
490
491      # Increment our parts counter
492      if [ "$_pType" = "gpt" -o "$_pType" = "apm" ] ; then
493          CURPART=$((CURPART+1))
494        # If this is a gpt/apm partition,
495        # we can continue and skip the MBR part letter stuff
496        continue
497      else
498          CURPART=$((CURPART+1))
499        if [ "$CURPART" = "3" ] ; then CURPART="4" ; fi
500      fi
501
502
503      # This partition letter is used, get the next one
504      case ${PARTLETTER} in
505        a) PARTLETTER="b" ;;
506        b) PARTLETTER="d" ;;
507        d) PARTLETTER="e" ;;
508        e) PARTLETTER="f" ;;
509        f) PARTLETTER="g" ;;
510        g) PARTLETTER="h" ;;
511        h) PARTLETTER="ERR" ;;
512        *) exit_err "ERROR: bsdlabel only supports up to letter h for partitions." ;;
513      esac
514
515    fi # End of subsection locating a slice in config
516
517    echo $line | grep -q "^commitDiskLabel" 2>/dev/null
518    if [ $? -eq 0 -a "${FOUNDPARTS}" = "0" ]
519    then
520
521      # If this is the boot disk, stamp the right gptboot
522      if [ ! -z "${BOOTTYPE}" -a "$_pType" = "gpt" ] ; then
523        case ${BOOTTYPE} in
524          freebsd-ufs) rc_halt "gpart bootcode -p /boot/gptboot -i 1 ${_pDisk}" ;;
525          freebsd-zfs) rc_halt "gpart bootcode -p /boot/gptzfsboot -i 1 ${_pDisk}" ;;
526        esac
527      fi
528
529      # Make sure to stamp the MBR loader
530      if [ "$_pType" = "mbr" ] ; then
531        rc_halt "gpart bootcode -b /boot/boot ${_wSlice}"
532      fi
533
534      # Found our flag to commit this label setup, check that we found at least 1 partition
535      if [ "${CURPART}" = "1" ] ; then
536        exit_err "ERROR: commitDiskLabel was called without any partition entries for it!"
537      fi
538
539      break
540    fi
541  done <${CFGF}
542};
543
544# Reads through the config and sets up a BSDLabel for the given slice
545populate_disk_label()
546{
547  if [ -z "${1}" ]
548  then
549    exit_err "ERROR: populate_disk_label() called without argument!"
550  fi
551
552  # Set some vars from the given working slice
553  diskid="`echo $1 | cut -d ':' -f 1`" 
554  disk="`echo $1 | cut -d ':' -f 1 | sed 's|-|/|g'`" 
555  slicenum="`echo $1 | cut -d ':' -f 2`" 
556  type="`echo $1 | cut -d ':' -f 3`" 
557 
558  # Set WRKSLICE based upon format we are using
559  if [ "$type" = "mbr" ] ; then
560    wrkslice="${diskid}s${slicenum}"
561  fi
562  if [ "$type" = "apm" ] ; then
563    wrkslice="${diskid}s${slicenum}"
564  fi
565  if [ "$type" = "gpt" -o "$type" = "gptslice" ] ; then
566    wrkslice="${diskid}p${slicenum}"
567  fi
568
569  if [ ! -e "${SLICECFGDIR}/${wrkslice}" ] ; then
570    exit_err "ERROR: Missing SLICETAG data. This shouldn't happen - please let the developers know"
571  fi
572
573  disktag="`cat ${SLICECFGDIR}/${wrkslice}`"
574  slicedev="`echo $wrkslice | sed 's|-|/|g'`"
575 
576  # Setup the partitions with gpart
577  setup_gpart_partitions "${disktag}" "${disk}" "${slicedev}" "${slicenum}" "${type}"
578
579};
580
581# Function which reads in the disk slice config, and performs it
582setup_disk_label()
583{
584  # We are ready to start setting up the label, lets read the config and do the actions
585  # First confirm that we have a valid WORKINGSLICES
586  if [ -z "${WORKINGSLICES}" ]; then
587    exit_err "ERROR: No slices were setup! Please report this to the maintainers"
588  fi
589
590  # Check that the slices we have did indeed get setup and gpart worked
591  for i in $WORKINGSLICES
592  do
593    disk="`echo $i | cut -d '-' -f 1`" 
594    pnum="`echo $i | cut -d '-' -f 2`" 
595    type="`echo $i | cut -d '-' -f 3`" 
596    if [ "$type" = "mbr" -a ! -e "${disk}s${pnum}" ] ; then
597      exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
598    fi
599    if [ "$type" = "gpt" -a ! -e "${disk}p${pnum}" ] ; then
600      exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
601    fi
602    if [ "$type" = "apm" -a ! -e "${disk}s${pnum}" ] ; then
603      exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
604    fi
605    if [ "$type" = "gptslice" -a ! -e "${disk}p${pnum}" ] ; then
606      exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
607    fi
608  done
609
610  # Setup some files which we'll be referring to
611  export LABELLIST="${TMPDIR}/workingLabels"
612  rm $LABELLIST >/dev/null 2>/dev/null
613
614  # Set our flag to determine if we've got a valid root partition in this setup
615  export FOUNDROOT="-1"
616
617  # Check if we are using a /boot partition
618  export USINGBOOTPART="1"
619 
620  # Set encryption on root check
621  export USINGENCROOT="1"
622 
623  # Make the tmp directory where we'll store FS info & mount-points
624  rm -rf ${PARTDIR} >/dev/null 2>/dev/null
625  mkdir -p ${PARTDIR} >/dev/null 2>/dev/null
626  rm -rf ${PARTDIR}-enc >/dev/null 2>/dev/null
627  mkdir -p ${PARTDIR}-enc >/dev/null 2>/dev/null
628
629  for i in $WORKINGSLICES
630  do
631    populate_disk_label "${i}"
632  done
633
634  # Check if we made a root partition
635  if [ "$FOUNDROOT" = "-1" ]
636  then
637    exit_err "ERROR: No root (/) partition specified!!"
638  fi
639
640  # Check if we made a root partition
641  if [ "$FOUNDROOT" = "1" -a "${USINGBOOTPART}" != "0" ]
642  then
643    exit_err "ERROR: (/) partition isn't first partition on disk!"
644  fi
645
646  if [ "${USINGENCROOT}" = "0" -a "${USINGBOOTPART}" != "0" ]
647  then
648    exit_err "ERROR: Can't encrypt (/) with no (/boot) partition!"
649  fi
650};
651
652check_fstab_mbr()
653{
654  local SLICE
655  local FSTAB
656
657  if [ -z "$2" ]
658  then
659        return 1
660  fi
661
662  SLICE="$1"
663  FSTAB="$2/etc/fstab"
664
665  if [ -f "${FSTAB}" ]
666  then
667    PARTLETTER=`echo "$SLICE" | sed -E 's|^.+([a-h])$|\1|'`
668
669    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
670    if [ $? -eq 0 ]
671    then
672      if [ "${PARTLETTER}" = "a" ]
673      then
674        FOUNDROOT="0"
675      else
676        FOUNDROOT="1"
677      fi
678
679      ROOTIMAGE="1"
680
681      export FOUNDROOT
682      export ROOTIMAGE
683    fi
684
685    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
686    if [ $? -eq 0 ]
687    then
688      if [ "${PARTLETTER}" = "a" ]
689      then
690        USINGBOOTPART="0"
691      else
692        exit_err "/boot partition must be first partition"
693      fi
694      export USINGBOOTPART
695    fi
696
697    return 0
698  fi
699
700  return 1
701};
702
703check_fstab_gpt()
704{
705  local SLICE
706  local FSTAB
707
708  if [ -z "$2" ]
709  then
710        return 1
711  fi
712
713  SLICE="$1"
714  FSTAB="$2/etc/fstab"
715
716  if [ -f "${FSTAB}" ]
717  then
718    PARTNUMBER=`echo "${SLICE}" | sed -E 's|^.+p([0-9]*)$|\1|'`
719
720    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
721    if [ $? -eq 0 ]
722    then
723      if [ "${PARTNUMBER}" = "2" ]
724      then
725        FOUNDROOT="0"
726      else
727        FOUNDROOT="1"
728      fi
729
730      ROOTIMAGE="1"
731
732      export FOUNDROOT
733      export ROOTIMAGE
734    fi
735
736    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
737    if [ $? -eq 0 ]
738    then
739      if [ "${PARTNUMBER}" = "2" ]
740      then
741        USINGBOOTPART="0"
742      else
743        exit_err "/boot partition must be first partition"
744      fi
745      export USINGBOOTPART
746    fi
747
748    return 0
749  fi
750
751
752  return 1
753};
754
755check_disk_layout()
756{
757  local SLICES
758  local TYPE
759  local DISK
760  local RES
761  local F
762
763  DISK="$1"
764  TYPE="MBR"
765
766  if [ -z "${DISK}" ]
767  then
768        return 1
769  fi
770
771  SLICES_MBR=`ls /dev/${DISK}s[1-4]*[a-h]* 2>/dev/null`
772  SLICES_GPT=`ls /dev/${DISK}p[0-9]* 2>/dev/null`
773  SLICES_SLICE=`ls /dev/${DISK}[a-h]* 2>/dev/null`
774
775  if [ -n "${SLICES_MBR}" ]
776  then
777    SLICES="${SLICES_MBR}"
778    TYPE="MBR"
779    RES=0
780  fi
781  if [ -n "${SLICES_GPT}" ]
782  then
783    SLICES="${SLICES_GPT}"
784    TYPE="GPT"
785    RES=0
786  fi
787  if [ -n "${SLICES_SLICE}" ]
788  then
789    SLICES="${SLICES_SLICE}"
790    TYPE="MBR"
791    RES=0
792  fi
793 
794  for slice in ${SLICES}
795  do
796    F=1
797    mount ${slice} /mnt 2>/dev/null
798    if [ $? -ne 0 ]
799    then
800      continue
801    fi
802
803    if [ "${TYPE}" = "MBR" ]
804    then
805          check_fstab_mbr "${slice}" "/mnt"
806      F="$?"
807
808    elif [ "${TYPE}" = "GPT" ]
809    then
810          check_fstab_gpt "${slice}" "/mnt"
811      F="$?"
812    fi
813
814    if [ ${F} -eq 0 ]
815    then
816      umount /mnt
817      break
818    fi
819
820    umount /mnt
821  done
822
823  return ${RES}
824};
Note: See TracBrowser for help on using the repository browser.