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