3 # Copyright (c) 2010 iXsystems, Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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
28 # Functions related to disk operations using bsdlabel
30 # Check if we are are provided a geli password on the nextline of the config
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}"
47 # On check on the disk-label line if we have any extra vars for this device
53 echo $LINE | grep -q ' (' 2>/dev/null
54 if [ $? -eq 0 ] ; then
56 # See if we are looking for ZFS specific options
57 echo $LINE | grep -q '^ZFS' 2>/dev/null
58 if [ $? -eq 0 ] ; then
60 ZFSVARS="`echo $LINE | cut -d '(' -f 2- | cut -d ')' -f 1 | xargs`"
62 echo $ZFSVARS | grep -qE "^(disk|file|mirror|raidz(1|2)?|spare|log|cache):" 2>/dev/null
63 if [ $? -eq 0 ] ; then
64 ZTYPE=`echo $ZFSVARS | cut -f1 -d:`
65 ZFSVARS=`echo $ZFSVARS | sed "s|$ZTYPE: ||g" | sed "s|$ZTYPE:||g"`
68 # Return the ZFS options
69 if [ "${ZTYPE}" = "NONE" ] ; then
70 VAR="${ACTIVEDEV} ${ZFSVARS}"
72 VAR="${ZTYPE} ${ACTIVEDEV} ${ZFSVARS}"
78 # See if we are looking for UFS specific newfs options
79 echo $LINE | grep -q '^UFS' 2>/dev/null
80 if [ $? -eq 0 ] ; then
81 FSVARS="`echo $LINE | cut -d '(' -f 2- | cut -d ')' -f 1 | xargs`"
87 fi # End of xtra-options block
89 # If we got here, set VAR to empty and export
94 # Init each zfs mirror disk with a boot sector so we can failover
95 setup_zfs_mirror_parts()
99 # Using mirroring, setup boot partitions on each disk
100 _mirrline="`echo ${1} | sed 's|mirror ||g'`"
101 for _zvars in $_mirrline
103 echo "Looping through _zvars: $_zvars" >>${LOGOUT}
104 echo "$_zvars" | grep -q "${2}" 2>/dev/null
105 if [ $? -eq 0 ] ; then continue ; fi
106 if [ -z "$_zvars" ] ; then continue ; fi
108 is_disk "$_zvars" >/dev/null 2>/dev/null
109 if [ $? -eq 0 ] ; then
110 echo "Setting up ZFS mirror disk $_zvars" >>${LOGOUT}
111 init_gpt_full_disk "$_zvars" >/dev/null 2>/dev/null
112 rc_halt "gpart add -t freebsd-zfs ${_zvars}" >/dev/null 2>/dev/null
113 _nZFS="$_nZFS ${_zvars}p2"
115 _nZFS="$_nZFS ${_zvars}"
118 echo "mirror $2 `echo $_nZFS | tr -s ' '`"
121 # Function which creates a unique label name for the specified mount
129 # Check if we are doing /, and rename it
130 if [ "$MOUNT" = "/" ]
134 # If doing a swap partition, also rename it
135 if [ "${TYPE}" = "SWAP" ]
139 NAME="`echo $MOUNT | sed 's|/||g' | sed 's| ||g'`"
143 # Loop through and break when we find our first available label
147 glabel status | grep -q "${NAME}${NUM}" 2>/dev/null
155 if [ $NUM -gt $MAXNUM ]
157 exit_err "Cannot allocate additional glabel name for $NAME"
163 export VAL="${NAME}${NUM}"
166 # Function to setup partitions using gpart
167 setup_gpart_partitions()
176 # Lets read in the config file now and setup our partitions
177 if [ "${_pType}" = "gpt" ] ; then
182 rc_halt "gpart create -s BSD ${_wSlice}"
187 # Check for data on this slice
188 echo $line | grep -q "^${_dTag}-part=" 2>/dev/null
192 # Found a slice- entry, lets get the slice info
193 get_value_from_string "${line}"
196 # We need to split up the string now, and pick out the variables
197 FS=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 1`
198 SIZE=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 2`
199 MNT=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 3`
201 # Check if we have a .eli extension on this FS
202 echo ${FS} | grep -q ".eli" 2>/dev/null
205 FS="`echo ${FS} | cut -d '.' -f 1`"
207 check_for_enc_pass "${line}"
208 if [ "${VAL}" != "" ] ; then
209 # We have a user supplied password, save it for later
216 # Check if the user tried to setup / as an encrypted partition
217 check_for_mount "${MNT}" "/"
218 if [ $? -eq 0 -a "${ENC}" = "ON" ]
220 export USINGENCROOT="0"
223 # Now check that these values are sane
225 UFS|UFS+S|UFS+J|UFS+SUJ|ZFS|SWAP) ;;
226 *) exit_err "ERROR: Invalid file system specified on $line" ;;
229 # Check that we have a valid size number
230 expr $SIZE + 1 >/dev/null 2>/dev/null
231 if [ $? -ne 0 ]; then
232 exit_err "ERROR: The size specified on $line is invalid"
235 # Check that the mount-point starts with /
236 echo "$MNT" | grep -qe "^/" -e "^none" 2>/dev/null
237 if [ $? -ne 0 ]; then
238 exit_err "ERROR: The mount-point specified on $line is invalid"
248 # Check if we found a valid root partition
249 check_for_mount "${MNT}" "/"
250 if [ $? -eq 0 ] ; then
252 if [ "${CURPART}" = "2" -a "$_pType" = "gpt" ] ; then
255 if [ "${CURPART}" = "1" -a "$_pType" = "mbr" ] ; then
260 check_for_mount "${MNT}" "/boot"
261 if [ $? -eq 0 ] ; then
262 export USINGBOOTPART="0"
263 if [ "${CURPART}" != "2" -a "${_pType}" = "gpt" ] ; then
264 exit_err "/boot partition must be first partition"
266 if [ "${CURPART}" != "1" -a "${_pType}" = "mbr" ] ; then
267 exit_err "/boot partition must be first partition"
270 if [ "${FS}" != "UFS" -a "${FS}" != "UFS+S" -a "${FS}" != "UFS+J" -a "${FS}" != "UFS+SUJ" ] ; then
271 exit_err "/boot partition must be formatted with UFS"
275 # Generate a unique label name for this mount
276 gen_glabel_name "${MNT}" "${FS}"
279 # Get any extra options for this fs / line
280 if [ "${_pType}" = "gpt" ] ; then
281 get_fs_line_xvars "${_pDisk}p${CURPART}" "${STRING}"
283 get_fs_line_xvars "${_wSlice}${PARTLETTER}" "${STRING}"
287 # Check if using zfs mirror
288 echo ${XTRAOPTS} | grep -q "mirror" 2>/dev/null
289 if [ $? -eq 0 -a "$FS" = "ZFS" ] ; then
290 if [ "${_pType}" = "gpt" ] ; then
291 XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_pDisk}p${CURPART}")
293 XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_wSlice}")
297 # Figure out the gpart type to use
299 ZFS) PARTYPE="freebsd-zfs" ;;
300 SWAP) PARTYPE="freebsd-swap" ;;
301 *) PARTYPE="freebsd-ufs" ;;
304 # Create the partition
305 if [ "${_pType}" = "gpt" ] ; then
306 if [ "$CURPART" = "2" ] ; then
307 # If this is GPT, make sure first partition is aligned to 4k
308 rc_halt "gpart add -b 2016 ${SOUT} -t ${PARTYPE} ${_pDisk}"
310 rc_halt "gpart add ${SOUT} -t ${PARTYPE} ${_pDisk}"
313 rc_halt "gpart add ${SOUT} -t ${PARTYPE} -i ${CURPART} ${_wSlice}"
316 # Check if this is a root / boot partition, and stamp the right loader
317 for TESTMNT in `echo ${MNT} | sed 's|,| |g'`
319 if [ "${TESTMNT}" = "/" -a -z "${BOOTTYPE}" ] ; then
320 BOOTTYPE="${PARTYPE}"
322 if [ "${TESTMNT}" = "/boot" ] ; then
323 BOOTTYPE="${PARTYPE}"
327 # Save this data to our partition config dir
328 if [ "${_pType}" = "gpt" ] ; then
329 _dFile="`echo $_pDisk | sed 's|/|-|g'`"
330 echo "${FS}:${MNT}:${ENC}:${PLABEL}:GPT:${XTRAOPTS}" >${PARTDIR}/${_dFile}p${CURPART}
332 # Clear out any headers
334 dd if=/dev/zero of=${_pDisk}p${CURPART} count=2048 2>/dev/null
336 # If we have a enc password, save it as well
337 if [ -n "${ENCPASS}" ] ; then
338 echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}p${CURPART}-encpass
342 _dFile="`echo $_wSlice | sed 's|/|-|g'`"
343 echo "${FS}:${MNT}:${ENC}:${PLABEL}:MBR:${XTRAOPTS}:${IMAGE}" >${PARTDIR}/${_dFile}${PARTLETTER}
344 # Clear out any headers
346 dd if=/dev/zero of=${_wSlice}${PARTLETTER} count=2048 2>/dev/null
348 # If we have a enc password, save it as well
349 if [ -n "${ENCPASS}" ] ; then
350 echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}${PARTLETTER}-encpass
355 # Increment our parts counter
356 if [ "$_pType" = "gpt" ] ; then
357 CURPART=$((CURPART+1))
358 # If this is a gpt partition, we can continue and skip the MBR part letter stuff
361 CURPART=$((CURPART+1))
362 if [ "$CURPART" = "3" ] ; then CURPART="4" ; fi
366 # This partition letter is used, get the next one
367 case ${PARTLETTER} in
374 h) PARTLETTER="ERR" ;;
375 *) exit_err "ERROR: bsdlabel only supports up to letter h for partitions." ;;
378 fi # End of subsection locating a slice in config
380 echo $line | grep -q "^commitDiskLabel" 2>/dev/null
381 if [ $? -eq 0 -a "${FOUNDPARTS}" = "0" ]
384 # If this is the boot disk, stamp the right gptboot
385 if [ ! -z "${BOOTTYPE}" -a "$_pType" = "gpt" ] ; then
387 freebsd-ufs) rc_halt "gpart bootcode -p /boot/gptboot -i 1 ${_pDisk}" ;;
388 freebsd-zfs) rc_halt "gpart bootcode -p /boot/gptzfsboot -i 1 ${_pDisk}" ;;
392 # Make sure to stamp the MBR loader
393 if [ "$_pType" = "mbr" ] ; then
394 rc_halt "gpart bootcode -b /boot/boot ${_wSlice}"
397 # Found our flag to commit this label setup, check that we found at least 1 partition
398 if [ "${CURPART}" = "2" ] ; then
399 exit_err "ERROR: commitDiskLabel was called without any partition entries for it!"
407 # Reads through the config and sets up a BSDLabel for the given slice
408 populate_disk_label()
412 exit_err "ERROR: populate_disk_label() called without argument!"
415 # Set some vars from the given working slice
416 diskid="`echo $1 | cut -d ':' -f 1`"
417 disk="`echo $1 | cut -d ':' -f 1 | sed 's|-|/|g'`"
418 slicenum="`echo $1 | cut -d ':' -f 2`"
419 type="`echo $1 | cut -d ':' -f 3`"
421 # Set WRKSLICE based upon format we are using
422 if [ "$type" = "mbr" ] ; then
423 wrkslice="${diskid}s${slicenum}"
425 if [ "$type" = "gpt" ] ; then
426 wrkslice="${diskid}p${slicenum}"
429 if [ ! -e "${SLICECFGDIR}/${wrkslice}" ] ; then
430 exit_err "ERROR: Missing SLICETAG data. This shouldn't happen - please let the developers know"
433 disktag="`cat ${SLICECFGDIR}/${wrkslice}`"
434 slicedev="`echo $wrkslice | sed 's|-|/|g'`"
436 # Setup the partitions with gpart
437 setup_gpart_partitions "${disktag}" "${disk}" "${slicedev}" "${slicenum}" "${type}"
441 # Function which reads in the disk slice config, and performs it
444 # We are ready to start setting up the label, lets read the config and do the actions
445 # First confirm that we have a valid WORKINGSLICES
446 if [ -z "${WORKINGSLICES}" ]; then
447 exit_err "ERROR: No slices were setup! Please report this to the maintainers"
450 # Check that the slices we have did indeed get setup and gpart worked
451 for i in $WORKINGSLICES
453 disk="`echo $i | cut -d '-' -f 1`"
454 pnum="`echo $i | cut -d '-' -f 2`"
455 type="`echo $i | cut -d '-' -f 3`"
456 if [ "$type" = "mbr" -a ! -e "${disk}s${pnum}" ] ; then
457 exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
459 if [ "$type" = "gpt" -a ! -e "${disk}p${pnum}" ] ; then
460 exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
464 # Setup some files which we'll be referring to
465 export LABELLIST="${TMPDIR}/workingLabels"
466 rm $LABELLIST >/dev/null 2>/dev/null
468 # Set our flag to determine if we've got a valid root partition in this setup
469 export FOUNDROOT="-1"
471 # Check if we are using a /boot partition
472 export USINGBOOTPART="1"
474 # Set encryption on root check
475 export USINGENCROOT="1"
477 # Make the tmp directory where we'll store FS info & mount-points
478 rm -rf ${PARTDIR} >/dev/null 2>/dev/null
479 mkdir -p ${PARTDIR} >/dev/null 2>/dev/null
480 rm -rf ${PARTDIR}-enc >/dev/null 2>/dev/null
481 mkdir -p ${PARTDIR}-enc >/dev/null 2>/dev/null
483 for i in $WORKINGSLICES
485 populate_disk_label "${i}"
488 # Check if we made a root partition
489 if [ "$FOUNDROOT" = "-1" ]
491 exit_err "ERROR: No root (/) partition specified!!"
494 # Check if we made a root partition
495 if [ "$FOUNDROOT" = "1" -a "${USINGBOOTPART}" != "0" ]
497 exit_err "ERROR: (/) partition isn't first partition on disk!"
500 if [ "${USINGENCROOT}" = "0" -a "${USINGBOOTPART}" != "0" ]
502 exit_err "ERROR: Can't encrypt (/) with no (/boot) partition!"
521 PARTLETTER=`echo "$SLICE" | sed -E 's|^.+([a-h])$|\1|'`
523 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
526 if [ "${PARTLETTER}" = "a" ]
539 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
542 if [ "${PARTLETTER}" = "a" ]
546 exit_err "/boot partition must be first partition"
572 PARTNUMBER=`echo "${SLICE}" | sed -E 's|^.+p([0-9]*)$|\1|'`
574 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
577 if [ "${PARTNUMBER}" = "2" ]
590 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
593 if [ "${PARTNUMBER}" = "2" ]
597 exit_err "/boot partition must be first partition"
625 SLICES_MBR=`ls /dev/${DISK}s[1-4]*[a-h]* 2>/dev/null`
626 SLICES_GPT=`ls /dev/${DISK}p[0-9]* 2>/dev/null`
627 SLICES_SLICE=`ls /dev/${DISK}[a-h]* 2>/dev/null`
629 if [ -n "${SLICES_MBR}" ]
631 SLICES="${SLICES_MBR}"
635 if [ -n "${SLICES_GPT}" ]
637 SLICES="${SLICES_GPT}"
641 if [ -n "${SLICES_SLICE}" ]
643 SLICES="${SLICES_SLICE}"
648 for slice in ${SLICES}
651 mount ${slice} /mnt 2>/dev/null
657 if [ "${TYPE}" = "MBR" ]
659 check_fstab_mbr "${slice}" "/mnt"
662 elif [ "${TYPE}" = "GPT" ]
664 check_fstab_gpt "${slice}" "/mnt"