functions-bsdlabel.sh revision 232901
1209513Simp#!/bin/sh
2209513Simp#-
3209552Simp# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4209513Simp#
5209513Simp# Redistribution and use in source and binary forms, with or without
6209513Simp# modification, are permitted provided that the following conditions
7209513Simp# are met:
8209513Simp# 1. Redistributions of source code must retain the above copyright
9209513Simp#    notice, this list of conditions and the following disclaimer.
10209513Simp# 2. Redistributions in binary form must reproduce the above copyright
11209513Simp#    notice, this list of conditions and the following disclaimer in the
12209513Simp#    documentation and/or other materials provided with the distribution.
13209513Simp#
14209513Simp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15209513Simp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16209513Simp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17209513Simp# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18209513Simp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19209513Simp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20209513Simp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21209513Simp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22209513Simp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23209513Simp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24209513Simp# SUCH DAMAGE.
25209513Simp#
26209513Simp# $FreeBSD: head/usr.sbin/pc-sysinstall/backend/functions-bsdlabel.sh 232901 2012-03-12 21:41:29Z jpaetzel $
27209513Simp
28209513Simp# Functions related to disk operations using bsdlabel
29209513Simp
30209513Simp# Check if we are are provided a geli password on the nextline of the config
31209513Simpcheck_for_enc_pass()
32209513Simp{
33209513Simp  CURLINE="${1}"
34209513Simp 
35209513Simp  get_next_cfg_line "${CFGF}" "${CURLINE}" 
36220059Sjpaetzel  echo ${VAL} | grep -q "^encpass=" 2>/dev/null
37220059Sjpaetzel  if [ $? -eq 0 ] ; then
38209513Simp    # Found a password, return it
39209513Simp    get_value_from_string "${VAL}"
40209513Simp    return
41209513Simp  fi
42209513Simp
43220059Sjpaetzel  export VAL=""
44209513Simp  return
45209513Simp};
46209513Simp
47209513Simp# On check on the disk-label line if we have any extra vars for this device
48209513Simpget_fs_line_xvars()
49209513Simp{
50209513Simp  ACTIVEDEV="${1}"
51209513Simp  LINE="${2}"
52209513Simp
53232899Sjpaetzel  echo $LINE | cut -d ' ' -f 4 | grep -q ' (' 2>/dev/null
54220059Sjpaetzel  if [ $? -eq 0 ] ; then
55209513Simp
56209513Simp    # See if we are looking for ZFS specific options
57220059Sjpaetzel    echo $LINE | grep -q '^ZFS' 2>/dev/null
58220059Sjpaetzel    if [ $? -eq 0 ] ; then
59209513Simp      ZTYPE="NONE"
60232899Sjpaetzel      ZFSVARS="`echo $LINE | cut -d ' ' -f 4 |cut -d '(' -f 2- | cut -d ')' -f 1 | xargs`"
61209513Simp
62232510Seadler      echo $ZFSVARS | grep -qE "^(disk|file|mirror|raidz(1|2|3)?|spare|log|cache):" 2>/dev/null
63220059Sjpaetzel	  if [ $? -eq 0 ] ; then
64211488Simp       ZTYPE=`echo $ZFSVARS | cut -f1 -d:`
65211488Simp       ZFSVARS=`echo $ZFSVARS | sed "s|$ZTYPE: ||g" | sed "s|$ZTYPE:||g"`
66211488Simp	  fi
67209513Simp
68209513Simp      # Return the ZFS options
69209513Simp      if [ "${ZTYPE}" = "NONE" ] ; then
70209513Simp        VAR="${ACTIVEDEV} ${ZFSVARS}"
71209513Simp      else
72209513Simp        VAR="${ZTYPE} ${ACTIVEDEV} ${ZFSVARS}"
73209513Simp      fi
74209513Simp      export VAR
75209513Simp      return
76209513Simp    fi # End of ZFS block
77209513Simp
78225657Sjpaetzel    # See if we are looking for UFS specific newfs options
79225657Sjpaetzel    echo $LINE | grep -q '^UFS' 2>/dev/null
80225657Sjpaetzel    if [ $? -eq 0 ] ; then
81225657Sjpaetzel      FSVARS="`echo $LINE | cut -d '(' -f 2- | cut -d ')' -f 1 | xargs`"
82225657Sjpaetzel      VAR="${FSVARS}"
83225657Sjpaetzel      export VAR
84225657Sjpaetzel      return
85225657Sjpaetzel    fi
86225657Sjpaetzel
87209513Simp  fi # End of xtra-options block
88209513Simp
89209513Simp  # If we got here, set VAR to empty and export
90220059Sjpaetzel  export VAR=""
91209513Simp  return
92209513Simp};
93209513Simp
94209513Simp# Init each zfs mirror disk with a boot sector so we can failover
95211730Simpsetup_zfs_mirror_parts()
96211730Simp{
97211730Simp  _nZFS=""
98209513Simp
99211730Simp  # Using mirroring, setup boot partitions on each disk
100211730Simp  _mirrline="`echo ${1} | sed 's|mirror ||g'`"
101211730Simp  for _zvars in $_mirrline
102211730Simp  do
103211730Simp    echo "Looping through _zvars: $_zvars" >>${LOGOUT}
104220059Sjpaetzel    echo "$_zvars" | grep -q "${2}" 2>/dev/null
105220059Sjpaetzel    if [ $? -eq 0 ] ; then continue ; fi
106211730Simp    if [ -z "$_zvars" ] ; then continue ; fi
107211730Simp
108211730Simp    is_disk "$_zvars" >/dev/null 2>/dev/null
109220059Sjpaetzel    if [ $? -eq 0 ] ; then
110211730Simp      echo "Setting up ZFS mirror disk $_zvars" >>${LOGOUT}
111211730Simp      init_gpt_full_disk "$_zvars" >/dev/null 2>/dev/null
112211730Simp      rc_halt "gpart add -t freebsd-zfs ${_zvars}" >/dev/null 2>/dev/null
113211730Simp      _nZFS="$_nZFS ${_zvars}p2"	
114211730Simp    else
115211730Simp      _nZFS="$_nZFS ${_zvars}"	
116211730Simp    fi	
117211730Simp  done
118211730Simp  echo "mirror $2 `echo $_nZFS | tr -s ' '`"
119209513Simp} ;
120209513Simp
121209513Simp# Function which creates a unique label name for the specified mount
122209513Simpgen_glabel_name()
123209513Simp{
124209513Simp  MOUNT="$1"
125209513Simp  TYPE="$2"
126209513Simp  NUM="0"
127209513Simp  MAXNUM="20"
128209513Simp
129232899Sjpaetzel  if [ "$TYPE" = "ZFS" ] ; then
130232899Sjpaetzel    NAME="zpool"
131232899Sjpaetzel  elif [ "$MOUNT" = "/" ] ; then
132209513Simp    NAME="rootfs"
133209513Simp  else
134209513Simp    # If doing a swap partition, also rename it
135209513Simp    if [ "${TYPE}" = "SWAP" ]
136209513Simp    then
137209513Simp      NAME="swap"
138209513Simp    else
139209513Simp      NAME="`echo $MOUNT | sed 's|/||g' | sed 's| ||g'`"
140209513Simp    fi
141209513Simp  fi
142209513Simp
143209513Simp  # Loop through and break when we find our first available label
144209513Simp  while
145209513Simp  Z=1
146209513Simp  do
147220059Sjpaetzel    glabel status | grep -q "${NAME}${NUM}" 2>/dev/null
148220059Sjpaetzel    if [ $? -ne 0 ]
149209513Simp    then
150209513Simp      break
151209513Simp    else
152220059Sjpaetzel        NUM=$((NUM+1))
153209513Simp    fi
154209513Simp
155209513Simp    if [ $NUM -gt $MAXNUM ]
156209513Simp    then
157209513Simp      exit_err "Cannot allocate additional glabel name for $NAME"
158209513Simp      break
159209513Simp    fi
160209513Simp  done 
161209513Simp   
162209513Simp
163220059Sjpaetzel  export VAL="${NAME}${NUM}" 
164209513Simp};
165209513Simp
166217229Sjpaetzel# Function to setup partitions using gpart
167217229Sjpaetzelsetup_gpart_partitions()
168209513Simp{
169217229Sjpaetzel  local _dTag="$1"
170217229Sjpaetzel  local _pDisk="$2"
171217229Sjpaetzel  local _wSlice="$3"
172217229Sjpaetzel  local _sNum="$4"
173217229Sjpaetzel  local _pType="$5"
174209513Simp  FOUNDPARTS="1"
175209513Simp
176217229Sjpaetzel  # Lets read in the config file now and setup our partitions
177217229Sjpaetzel  if [ "${_pType}" = "gpt" ] ; then
178217229Sjpaetzel    CURPART="2"
179217229Sjpaetzel  else
180217229Sjpaetzel    PARTLETTER="a"
181217229Sjpaetzel    CURPART="1"
182227368Sjpaetzel    if [ "${_pType}" = "mbr" ] ; then
183227368Sjpaetzel      rc_halt "gpart create -s BSD ${_wSlice}"
184227368Sjpaetzel    fi
185217229Sjpaetzel  fi
186209513Simp
187209513Simp  while read line
188209513Simp  do
189209513Simp    # Check for data on this slice
190220059Sjpaetzel    echo $line | grep -q "^${_dTag}-part=" 2>/dev/null
191220059Sjpaetzel    if [ $? -eq 0 ]
192209513Simp    then
193209513Simp      FOUNDPARTS="0"
194209513Simp      # Found a slice- entry, lets get the slice info
195209513Simp      get_value_from_string "${line}"
196209513Simp      STRING="$VAL"
197209513Simp
198209513Simp      # We need to split up the string now, and pick out the variables
199209513Simp      FS=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 1` 
200209513Simp      SIZE=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 2` 
201209513Simp      MNT=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 3` 
202209513Simp
203209513Simp      # Check if we have a .eli extension on this FS
204220059Sjpaetzel      echo ${FS} | grep -q ".eli" 2>/dev/null
205220059Sjpaetzel      if [ $? -eq 0 ]
206209513Simp      then
207209513Simp        FS="`echo ${FS} | cut -d '.' -f 1`"
208209513Simp        ENC="ON"
209209513Simp        check_for_enc_pass "${line}"
210209513Simp        if [ "${VAL}" != "" ] ; then
211209513Simp          # We have a user supplied password, save it for later
212209513Simp          ENCPASS="${VAL}" 
213209513Simp        fi
214209513Simp      else
215209513Simp        ENC="OFF"
216209513Simp      fi
217209513Simp
218209513Simp      # Check if the user tried to setup / as an encrypted partition
219209513Simp      check_for_mount "${MNT}" "/"
220220059Sjpaetzel      if [ $? -eq 0 -a "${ENC}" = "ON" ]
221209513Simp      then
222220059Sjpaetzel        export USINGENCROOT="0"
223209513Simp      fi
224209513Simp          
225209513Simp      # Now check that these values are sane
226209513Simp      case $FS in
227213647Simp        UFS|UFS+S|UFS+J|UFS+SUJ|ZFS|SWAP) ;;
228209513Simp       *) exit_err "ERROR: Invalid file system specified on $line" ;;
229209513Simp      esac
230209513Simp
231209513Simp      # Check that we have a valid size number
232209513Simp      expr $SIZE + 1 >/dev/null 2>/dev/null
233220059Sjpaetzel      if [ $? -ne 0 ]; then
234209513Simp        exit_err "ERROR: The size specified on $line is invalid"
235209513Simp      fi
236209513Simp
237209513Simp      # Check that the mount-point starts with /
238220059Sjpaetzel      echo "$MNT" | grep -qe "^/" -e "^none" 2>/dev/null
239220059Sjpaetzel      if [ $? -ne 0 ]; then
240209513Simp        exit_err "ERROR: The mount-point specified on $line is invalid"
241209513Simp      fi
242209513Simp
243209513Simp      if [ "$SIZE" = "0" ]
244209513Simp      then
245209513Simp        SOUT=""
246209513Simp      else
247209513Simp        SOUT="-s ${SIZE}M"
248209513Simp      fi
249209513Simp
250209513Simp      # Check if we found a valid root partition
251209513Simp      check_for_mount "${MNT}" "/"
252220059Sjpaetzel      if [ $? -eq 0 ] ; then
253220059Sjpaetzel        export FOUNDROOT="1"
254217229Sjpaetzel        if [ "${CURPART}" = "2" -a "$_pType" = "gpt" ] ; then
255220059Sjpaetzel          export FOUNDROOT="0"
256209513Simp        fi
257217229Sjpaetzel        if [ "${CURPART}" = "1" -a "$_pType" = "mbr" ] ; then
258220059Sjpaetzel          export FOUNDROOT="0"
259217229Sjpaetzel        fi
260227368Sjpaetzel        if [ "${CURPART}" = "1" -a "$_pType" = "gptslice" ] ; then
261227368Sjpaetzel          export FOUNDROOT="0"
262227368Sjpaetzel        fi
263209513Simp      fi
264209513Simp
265209513Simp      check_for_mount "${MNT}" "/boot"
266220059Sjpaetzel      if [ $? -eq 0 ] ; then
267220059Sjpaetzel        export USINGBOOTPART="0"
268217229Sjpaetzel        if [ "${CURPART}" != "2" -a "${_pType}" = "gpt" ] ; then
269209513Simp            exit_err "/boot partition must be first partition"
270209513Simp        fi
271217229Sjpaetzel        if [ "${CURPART}" != "1" -a "${_pType}" = "mbr" ] ; then
272217229Sjpaetzel            exit_err "/boot partition must be first partition"
273217229Sjpaetzel        fi
274227368Sjpaetzel        if [ "${CURPART}" != "1" -a "${_pType}" = "gptslice" ] ; then
275227368Sjpaetzel            exit_err "/boot partition must be first partition"
276227368Sjpaetzel        fi
277217229Sjpaetzel
278217229Sjpaetzel        if [ "${FS}" != "UFS" -a "${FS}" != "UFS+S" -a "${FS}" != "UFS+J" -a "${FS}" != "UFS+SUJ" ] ; then
279217229Sjpaetzel          exit_err "/boot partition must be formatted with UFS"
280217229Sjpaetzel        fi
281209513Simp      fi
282209513Simp
283209513Simp      # Generate a unique label name for this mount
284209513Simp      gen_glabel_name "${MNT}" "${FS}"
285209513Simp      PLABEL="${VAL}"
286209513Simp
287209513Simp      # Get any extra options for this fs / line
288217229Sjpaetzel      if [ "${_pType}" = "gpt" ] ; then
289217229Sjpaetzel        get_fs_line_xvars "${_pDisk}p${CURPART}" "${STRING}"
290217229Sjpaetzel      else
291227118Sjpaetzel        get_fs_line_xvars "${_wSlice}${PARTLETTER}" "${STRING}"
292217229Sjpaetzel      fi
293209513Simp      XTRAOPTS="${VAR}"
294209513Simp
295209513Simp      # Check if using zfs mirror
296220059Sjpaetzel      echo ${XTRAOPTS} | grep -q "mirror" 2>/dev/null
297225657Sjpaetzel      if [ $? -eq 0 -a "$FS" = "ZFS" ] ; then
298227368Sjpaetzel        if [ "${_pType}" = "gpt" -o "${_pType}" = "gptslice" ] ; then
299217229Sjpaetzel       	  XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_pDisk}p${CURPART}")
300217229Sjpaetzel        else
301217229Sjpaetzel       	  XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_wSlice}")
302217229Sjpaetzel        fi
303209513Simp      fi
304209513Simp
305209513Simp      # Figure out the gpart type to use
306209513Simp      case ${FS} in
307211730Simp        ZFS) PARTYPE="freebsd-zfs" ;;
308211730Simp        SWAP) PARTYPE="freebsd-swap" ;;
309211730Simp        *) PARTYPE="freebsd-ufs" ;;
310209513Simp      esac
311209513Simp
312209513Simp      # Create the partition
313217229Sjpaetzel      if [ "${_pType}" = "gpt" ] ; then
314217229Sjpaetzel	if [ "$CURPART" = "2" ] ; then
315217229Sjpaetzel	  # If this is GPT, make sure first partition is aligned to 4k
316227368Sjpaetzel          sleep 2
317232901Sjpaetzel          rc_halt "gpart add -a 4k ${SOUT} -t ${PARTYPE} ${_pDisk}"
318217229Sjpaetzel	else
319227368Sjpaetzel          sleep 2
320217229Sjpaetzel          rc_halt "gpart add ${SOUT} -t ${PARTYPE} ${_pDisk}"
321217229Sjpaetzel	fi
322227368Sjpaetzel      elif [ "${_pType}" = "gptslice" ]; then
323227368Sjpaetzel        sleep 2
324227368Sjpaetzel        rc_halt "gpart add ${SOUT} -t ${PARTYPE} ${_wSlice}"
325217229Sjpaetzel      else
326227368Sjpaetzel        sleep 2
327217229Sjpaetzel        rc_halt "gpart add ${SOUT} -t ${PARTYPE} -i ${CURPART} ${_wSlice}"
328217229Sjpaetzel      fi
329209513Simp
330209513Simp      # Check if this is a root / boot partition, and stamp the right loader
331209513Simp      for TESTMNT in `echo ${MNT} | sed 's|,| |g'`
332209513Simp      do
333209513Simp        if [ "${TESTMNT}" = "/" -a -z "${BOOTTYPE}" ] ; then
334209513Simp           BOOTTYPE="${PARTYPE}" 
335209513Simp        fi 
336209513Simp        if [ "${TESTMNT}" = "/boot" ]  ; then
337209513Simp           BOOTTYPE="${PARTYPE}" 
338209513Simp        fi 
339209513Simp      done 
340209513Simp
341209513Simp      # Save this data to our partition config dir
342217229Sjpaetzel      if [ "${_pType}" = "gpt" ] ; then
343220909Sjpaetzel	_dFile="`echo $_pDisk | sed 's|/|-|g'`"
344232899Sjpaetzel        echo "${FS}#${MNT}#${ENC}#${PLABEL}#GPT#${XTRAOPTS}" >${PARTDIR}/${_dFile}p${CURPART}
345209513Simp
346217229Sjpaetzel        # Clear out any headers
347217229Sjpaetzel        sleep 2
348220059Sjpaetzel        dd if=/dev/zero of=${_pDisk}p${CURPART} count=2048 2>/dev/null
349209513Simp
350217229Sjpaetzel        # If we have a enc password, save it as well
351220059Sjpaetzel        if [ -n "${ENCPASS}" ] ; then
352220909Sjpaetzel          echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}p${CURPART}-encpass
353217229Sjpaetzel        fi
354217229Sjpaetzel      else
355227368Sjpaetzel	# MBR Partition or GPT slice
356220909Sjpaetzel	_dFile="`echo $_wSlice | sed 's|/|-|g'`"
357232899Sjpaetzel        echo "${FS}#${MNT}#${ENC}#${PLABEL}#MBR#${XTRAOPTS}#${IMAGE}" >${PARTDIR}/${_dFile}${PARTLETTER}
358217229Sjpaetzel        # Clear out any headers
359217229Sjpaetzel        sleep 2
360220059Sjpaetzel        dd if=/dev/zero of=${_wSlice}${PARTLETTER} count=2048 2>/dev/null
361217229Sjpaetzel
362217229Sjpaetzel        # If we have a enc password, save it as well
363220059Sjpaetzel        if [ -n "${ENCPASS}" ] ; then
364220909Sjpaetzel          echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}${PARTLETTER}-encpass
365217229Sjpaetzel        fi
366209513Simp      fi
367209513Simp
368217229Sjpaetzel
369209513Simp      # Increment our parts counter
370217229Sjpaetzel      if [ "$_pType" = "gpt" ] ; then 
371220059Sjpaetzel          CURPART=$((CURPART+1))
372217229Sjpaetzel        # If this is a gpt partition, we can continue and skip the MBR part letter stuff
373217229Sjpaetzel        continue
374217229Sjpaetzel      else
375220059Sjpaetzel          CURPART=$((CURPART+1))
376217229Sjpaetzel        if [ "$CURPART" = "3" ] ; then CURPART="4" ; fi
377217229Sjpaetzel      fi
378209513Simp
379217229Sjpaetzel
380217229Sjpaetzel      # This partition letter is used, get the next one
381217229Sjpaetzel      case ${PARTLETTER} in
382217229Sjpaetzel        a) PARTLETTER="b" ;;
383217229Sjpaetzel        b) PARTLETTER="d" ;;
384217229Sjpaetzel        d) PARTLETTER="e" ;;
385217229Sjpaetzel        e) PARTLETTER="f" ;;
386217229Sjpaetzel        f) PARTLETTER="g" ;;
387217229Sjpaetzel        g) PARTLETTER="h" ;;
388217229Sjpaetzel        h) PARTLETTER="ERR" ;;
389217229Sjpaetzel        *) exit_err "ERROR: bsdlabel only supports up to letter h for partitions." ;;
390217229Sjpaetzel      esac
391217229Sjpaetzel
392209513Simp    fi # End of subsection locating a slice in config
393209513Simp
394220059Sjpaetzel    echo $line | grep -q "^commitDiskLabel" 2>/dev/null
395220059Sjpaetzel    if [ $? -eq 0 -a "${FOUNDPARTS}" = "0" ]
396209513Simp    then
397209513Simp
398209513Simp      # If this is the boot disk, stamp the right gptboot
399217229Sjpaetzel      if [ ! -z "${BOOTTYPE}" -a "$_pType" = "gpt" ] ; then
400209513Simp        case ${BOOTTYPE} in
401217229Sjpaetzel          freebsd-ufs) rc_halt "gpart bootcode -p /boot/gptboot -i 1 ${_pDisk}" ;;
402217229Sjpaetzel          freebsd-zfs) rc_halt "gpart bootcode -p /boot/gptzfsboot -i 1 ${_pDisk}" ;;
403209513Simp        esac 
404209513Simp      fi
405209513Simp
406217229Sjpaetzel      # Make sure to stamp the MBR loader
407217229Sjpaetzel      if [ "$_pType" = "mbr" ] ; then
408217229Sjpaetzel	rc_halt "gpart bootcode -b /boot/boot ${_wSlice}"
409217229Sjpaetzel      fi
410209513Simp
411209513Simp      # Found our flag to commit this label setup, check that we found at least 1 partition
412232899Sjpaetzel      if [ "${CURPART}" = "1" ] ; then
413209513Simp        exit_err "ERROR: commitDiskLabel was called without any partition entries for it!"
414209513Simp      fi
415209513Simp
416209513Simp      break
417209513Simp    fi
418209513Simp  done <${CFGF}
419209513Simp};
420209513Simp
421209513Simp# Reads through the config and sets up a BSDLabel for the given slice
422209513Simppopulate_disk_label()
423209513Simp{
424209513Simp  if [ -z "${1}" ]
425209513Simp  then
426209513Simp    exit_err "ERROR: populate_disk_label() called without argument!"
427209513Simp  fi
428209513Simp
429209513Simp  # Set some vars from the given working slice
430220909Sjpaetzel  diskid="`echo $1 | cut -d ':' -f 1`" 
431220909Sjpaetzel  disk="`echo $1 | cut -d ':' -f 1 | sed 's|-|/|g'`" 
432220909Sjpaetzel  slicenum="`echo $1 | cut -d ':' -f 2`" 
433220909Sjpaetzel  type="`echo $1 | cut -d ':' -f 3`" 
434209513Simp  
435209513Simp  # Set WRKSLICE based upon format we are using
436209513Simp  if [ "$type" = "mbr" ] ; then
437220909Sjpaetzel    wrkslice="${diskid}s${slicenum}"
438209513Simp  fi
439227368Sjpaetzel  if [ "$type" = "gpt" -o "$type" = "gptslice" ] ; then
440220909Sjpaetzel    wrkslice="${diskid}p${slicenum}"
441209513Simp  fi
442209513Simp
443220909Sjpaetzel  if [ ! -e "${SLICECFGDIR}/${wrkslice}" ] ; then
444209513Simp    exit_err "ERROR: Missing SLICETAG data. This shouldn't happen - please let the developers know"
445209513Simp  fi
446209513Simp
447220909Sjpaetzel  disktag="`cat ${SLICECFGDIR}/${wrkslice}`"
448220909Sjpaetzel  slicedev="`echo $wrkslice | sed 's|-|/|g'`"
449220909Sjpaetzel  
450217229Sjpaetzel  # Setup the partitions with gpart
451220909Sjpaetzel  setup_gpart_partitions "${disktag}" "${disk}" "${slicedev}" "${slicenum}" "${type}"
452209513Simp
453209513Simp};
454209513Simp
455209513Simp# Function which reads in the disk slice config, and performs it
456209513Simpsetup_disk_label()
457209513Simp{
458209513Simp  # We are ready to start setting up the label, lets read the config and do the actions
459209513Simp  # First confirm that we have a valid WORKINGSLICES
460213650Simp  if [ -z "${WORKINGSLICES}" ]; then
461209513Simp    exit_err "ERROR: No slices were setup! Please report this to the maintainers"
462209513Simp  fi
463209513Simp
464209513Simp  # Check that the slices we have did indeed get setup and gpart worked
465209513Simp  for i in $WORKINGSLICES
466209513Simp  do
467209513Simp    disk="`echo $i | cut -d '-' -f 1`" 
468209513Simp    pnum="`echo $i | cut -d '-' -f 2`" 
469209513Simp    type="`echo $i | cut -d '-' -f 3`" 
470220909Sjpaetzel    if [ "$type" = "mbr" -a ! -e "${disk}s${pnum}" ] ; then
471209513Simp      exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
472209513Simp    fi
473220909Sjpaetzel    if [ "$type" = "gpt" -a ! -e "${disk}p${pnum}" ] ; then
474209513Simp      exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
475209513Simp    fi
476227368Sjpaetzel    if [ "$type" = "gptslice" -a ! -e "${disk}p${pnum}" ] ; then
477227368Sjpaetzel      exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
478227368Sjpaetzel    fi
479209513Simp  done
480209513Simp
481209513Simp  # Setup some files which we'll be referring to
482220059Sjpaetzel  export LABELLIST="${TMPDIR}/workingLabels"
483209513Simp  rm $LABELLIST >/dev/null 2>/dev/null
484209513Simp
485209513Simp  # Set our flag to determine if we've got a valid root partition in this setup
486220059Sjpaetzel  export FOUNDROOT="-1"
487209513Simp
488209513Simp  # Check if we are using a /boot partition
489220059Sjpaetzel  export USINGBOOTPART="1"
490209513Simp 
491209513Simp  # Set encryption on root check
492220059Sjpaetzel  export USINGENCROOT="1"
493209513Simp  
494209513Simp  # Make the tmp directory where we'll store FS info & mount-points
495209513Simp  rm -rf ${PARTDIR} >/dev/null 2>/dev/null
496209513Simp  mkdir -p ${PARTDIR} >/dev/null 2>/dev/null
497209513Simp  rm -rf ${PARTDIR}-enc >/dev/null 2>/dev/null
498209513Simp  mkdir -p ${PARTDIR}-enc >/dev/null 2>/dev/null
499209513Simp
500209513Simp  for i in $WORKINGSLICES
501209513Simp  do
502209513Simp    populate_disk_label "${i}"
503209513Simp  done
504209513Simp
505209513Simp  # Check if we made a root partition
506209513Simp  if [ "$FOUNDROOT" = "-1" ]
507209513Simp  then
508209513Simp    exit_err "ERROR: No root (/) partition specified!!"
509209513Simp  fi
510209513Simp
511209513Simp  # Check if we made a root partition
512209513Simp  if [ "$FOUNDROOT" = "1" -a "${USINGBOOTPART}" != "0" ]
513209513Simp  then
514209513Simp    exit_err "ERROR: (/) partition isn't first partition on disk!"
515209513Simp  fi
516209513Simp
517209513Simp  if [ "${USINGENCROOT}" = "0" -a "${USINGBOOTPART}" != "0" ]
518209513Simp  then
519209513Simp    exit_err "ERROR: Can't encrypt (/) with no (/boot) partition!"
520209513Simp  fi
521209513Simp};
522209513Simp
523212337Simpcheck_fstab_mbr()
524212337Simp{
525212337Simp  local SLICE
526212337Simp  local FSTAB
527212337Simp
528212337Simp  if [ -z "$2" ]
529212337Simp  then
530212337Simp	return 1
531212337Simp  fi
532212337Simp
533212337Simp  SLICE="$1"
534212337Simp  FSTAB="$2/etc/fstab"
535212337Simp
536212337Simp  if [ -f "${FSTAB}" ]
537212337Simp  then
538212337Simp    PARTLETTER=`echo "$SLICE" | sed -E 's|^.+([a-h])$|\1|'`
539212337Simp
540220059Sjpaetzel    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
541220059Sjpaetzel    if [ $? -eq 0 ]
542212337Simp    then
543212337Simp      if [ "${PARTLETTER}" = "a" ]
544212337Simp      then
545212337Simp        FOUNDROOT="0"
546212337Simp      else
547212337Simp        FOUNDROOT="1"
548212337Simp      fi
549213650Simp
550213650Simp      ROOTIMAGE="1"
551213650Simp
552212337Simp      export FOUNDROOT
553213650Simp      export ROOTIMAGE
554212337Simp    fi
555212337Simp
556220059Sjpaetzel    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
557220059Sjpaetzel    if [ $? -eq 0 ]
558212337Simp    then
559212337Simp      if [ "${PARTLETTER}" = "a" ]
560212337Simp      then
561212337Simp        USINGBOOTPART="0"
562212337Simp      else 
563212337Simp        exit_err "/boot partition must be first partition"
564212337Simp      fi 
565212337Simp      export USINGBOOTPART
566212337Simp    fi
567212337Simp
568212337Simp    return 0
569212337Simp  fi
570212337Simp
571212337Simp  return 1
572212337Simp};
573212337Simp
574212337Simpcheck_fstab_gpt()
575212337Simp{
576212337Simp  local SLICE
577212337Simp  local FSTAB
578212337Simp
579212337Simp  if [ -z "$2" ]
580212337Simp  then
581212337Simp	return 1
582212337Simp  fi
583212337Simp
584212337Simp  SLICE="$1"
585212337Simp  FSTAB="$2/etc/fstab"
586212337Simp
587212337Simp  if [ -f "${FSTAB}" ]
588212337Simp  then
589212337Simp    PARTNUMBER=`echo "${SLICE}" | sed -E 's|^.+p([0-9]*)$|\1|'`
590212337Simp
591220059Sjpaetzel    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
592220059Sjpaetzel    if [ $? -eq 0 ]
593212337Simp    then
594212337Simp      if [ "${PARTNUMBER}" = "2" ]
595212337Simp      then
596212337Simp        FOUNDROOT="0"
597212337Simp      else
598212337Simp        FOUNDROOT="1"
599212337Simp      fi
600213650Simp
601213650Simp      ROOTIMAGE="1"
602213650Simp
603212337Simp      export FOUNDROOT
604213650Simp      export ROOTIMAGE
605212337Simp    fi
606212337Simp
607220059Sjpaetzel    cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
608220059Sjpaetzel    if [ $? -eq 0 ]
609212337Simp    then
610212337Simp      if [ "${PARTNUMBER}" = "2" ]
611212337Simp      then
612212337Simp        USINGBOOTPART="0"
613212337Simp      else 
614212337Simp        exit_err "/boot partition must be first partition"
615212337Simp      fi 
616212337Simp      export USINGBOOTPART
617212337Simp    fi
618212337Simp
619212337Simp    return 0
620212337Simp  fi
621212337Simp
622212337Simp
623212337Simp  return 1
624212337Simp};
625212337Simp
626213650Simpcheck_disk_layout()
627212337Simp{
628212337Simp  local SLICES
629212337Simp  local TYPE
630213650Simp  local DISK
631212337Simp  local RES
632212337Simp  local F
633212337Simp
634213650Simp  DISK="$1"
635212337Simp  TYPE="MBR"
636212337Simp
637213650Simp  if [ -z "${DISK}" ]
638212337Simp  then
639212337Simp	return 1
640212337Simp  fi
641212337Simp
642213650Simp  SLICES_MBR=`ls /dev/${DISK}s[1-4]*[a-h]* 2>/dev/null`
643213650Simp  SLICES_GPT=`ls /dev/${DISK}p[0-9]* 2>/dev/null`
644213650Simp  SLICES_SLICE=`ls /dev/${DISK}[a-h]* 2>/dev/null`
645213650Simp
646213650Simp  if [ -n "${SLICES_MBR}" ]
647212337Simp  then
648213650Simp    SLICES="${SLICES_MBR}"
649213650Simp    TYPE="MBR"
650213650Simp    RES=0
651212337Simp  fi
652213650Simp  if [ -n "${SLICES_GPT}" ]
653212337Simp  then
654213650Simp    SLICES="${SLICES_GPT}"
655213650Simp    TYPE="GPT"
656212337Simp    RES=0
657212337Simp  fi
658213650Simp  if [ -n "${SLICES_SLICE}" ]
659213650Simp  then
660213650Simp    SLICES="${SLICES_SLICE}"
661213650Simp    TYPE="MBR"
662213650Simp    RES=0
663213650Simp  fi
664213650Simp  
665212337Simp  for slice in ${SLICES}
666212337Simp  do
667212337Simp    F=1
668212337Simp    mount ${slice} /mnt 2>/dev/null
669220059Sjpaetzel    if [ $? -ne 0 ]
670212337Simp    then
671212337Simp      continue
672212337Simp    fi 
673212337Simp
674212337Simp    if [ "${TYPE}" = "MBR" ]
675212337Simp    then
676212337Simp	  check_fstab_mbr "${slice}" "/mnt"
677212337Simp      F="$?"
678212337Simp
679212337Simp    elif [ "${TYPE}" = "GPT" ]
680212337Simp    then
681212337Simp	  check_fstab_gpt "${slice}" "/mnt"
682212337Simp      F="$?"
683212337Simp    fi 
684212337Simp
685220059Sjpaetzel    if [ ${F} -eq 0 ]
686212337Simp    then
687212337Simp      umount /mnt
688212337Simp      break 
689212337Simp    fi
690212337Simp
691212337Simp    umount /mnt
692212337Simp  done
693212337Simp
694212337Simp  return ${RES}
695212337Simp};
696