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 | cut -d ' ' -f 4 | grep -q '(' 2>/dev/null
54 if [ $? -ne 0 ] ; then return ; fi
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 4-20 |cut -d '(' -f 2- | cut -d ')' -f 1 | xargs`"
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 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 # If we got here, set VAR to empty and export
92 # Init each zfs mirror disk with a boot sector so we can failover
93 setup_zfs_mirror_parts()
97 ZTYPE="`echo ${1} | awk '{print $1}'`"
99 # Using mirroring, setup boot partitions on each disk
100 _mirrline="`echo ${1} | sed 's|mirror ||g' | sed 's|raidz1 ||g' | sed 's|raidz2 ||g' | sed 's|raidz3 ||g' | sed 's|raidz ||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 disk $_zvars" >>${LOGOUT}
111 init_gpt_full_disk "$_zvars" >/dev/null 2>/dev/null
112 rc_halt "gpart add -a 4k -t freebsd-zfs ${_zvars}" >/dev/null 2>/dev/null
113 rc_halt "gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ${_zvars}" >/dev/null 2>/dev/null
114 _nZFS="$_nZFS ${_zvars}p2"
116 _nZFS="$_nZFS ${_zvars}"
119 echo "$ZTYPE $2 `echo $_nZFS | tr -s ' '`"
122 # Function which creates a unique label name for the specified mount
130 if [ "$TYPE" = "ZFS" ] ; then
132 elif [ "$MOUNT" = "/" ] ; then
135 # If doing a swap partition, also rename it
136 if [ "${TYPE}" = "SWAP" ]
140 NAME="`echo $MOUNT | sed 's|/||g' | sed 's| ||g'`"
144 # Loop through and break when we find our first available label
148 glabel status | grep -q "${NAME}${NUM}" 2>/dev/null
156 if [ $NUM -gt $MAXNUM ]
158 exit_err "Cannot allocate additional glabel name for $NAME"
164 export VAL="${NAME}${NUM}"
167 # Function to setup partitions using gpart
168 setup_gpart_partitions()
177 # Lets read in the config file now and setup our partitions
178 if [ "${_pType}" = "gpt" ] ; then
180 elif [ "${_pType}" = "apm" ] ; then
185 if [ "${_pType}" = "mbr" ] ; then
186 rc_halt "gpart create -s BSD ${_wSlice}"
192 # Check for data on this slice
193 echo $line | grep -q "^${_dTag}-part=" 2>/dev/null
197 # Found a slice- entry, lets get the slice info
198 get_value_from_string "${line}"
201 # We need to split up the string now, and pick out the variables
202 FS=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 1`
203 SIZE=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 2`
204 MNT=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 3`
206 # Check if we have a .eli extension on this FS
207 echo ${FS} | grep -q ".eli" 2>/dev/null
210 FS="`echo ${FS} | cut -d '.' -f 1`"
212 check_for_enc_pass "${line}"
213 if [ "${VAL}" != "" ] ; then
214 # We have a user supplied password, save it for later
221 # Check if the user tried to setup / as an encrypted partition
222 check_for_mount "${MNT}" "/"
223 if [ $? -eq 0 -a "${ENC}" = "ON" ]
225 export USINGENCROOT="0"
228 # Now check that these values are sane
230 UFS|UFS+S|UFS+J|UFS+SUJ|ZFS|SWAP) ;;
231 *) exit_err "ERROR: Invalid file system specified on $line" ;;
234 # Check that we have a valid size number
235 expr $SIZE + 1 >/dev/null 2>/dev/null
236 if [ $? -ne 0 ]; then
237 exit_err "ERROR: The size specified on $line is invalid"
240 # Check that the mount-point starts with /
241 echo "$MNT" | grep -qe "^/" -e "^none" 2>/dev/null
242 if [ $? -ne 0 ]; then
243 exit_err "ERROR: The mount-point specified on $line is invalid"
253 # Check if we found a valid root partition
254 check_for_mount "${MNT}" "/"
255 if [ $? -eq 0 ] ; then
257 if [ "${CURPART}" = "2" -a "$_pType" = "gpt" ] ; then
260 if [ "${CURPART}" = "3" -a "$_pType" = "apm" ] ; then
263 if [ "${CURPART}" = "1" -a "$_pType" = "mbr" ] ; then
266 if [ "${CURPART}" = "1" -a "$_pType" = "gptslice" ] ; then
271 check_for_mount "${MNT}" "/boot"
272 if [ $? -eq 0 ] ; then
273 export USINGBOOTPART="0"
274 if [ "${CURPART}" != "2" -a "${_pType}" = "gpt" ] ; then
275 exit_err "/boot partition must be first partition"
277 if [ "${CURPART}" != "3" -a "${_pType}" = "apm" ] ; then
278 exit_err "/boot partition must be first partition"
280 if [ "${CURPART}" != "1" -a "${_pType}" = "mbr" ] ; then
281 exit_err "/boot partition must be first partition"
283 if [ "${CURPART}" != "1" -a "${_pType}" = "gptslice" ] ; then
284 exit_err "/boot partition must be first partition"
287 if [ "${FS}" != "UFS" -a "${FS}" != "UFS+S" -a "${FS}" != "UFS+J" -a "${FS}" != "UFS+SUJ" ] ; then
288 exit_err "/boot partition must be formatted with UFS"
292 # Generate a unique label name for this mount
293 gen_glabel_name "${MNT}" "${FS}"
296 # Get any extra options for this fs / line
297 if [ "${_pType}" = "gpt" ] ; then
298 get_fs_line_xvars "${_pDisk}p${CURPART}" "${STRING}"
299 elif [ "${_pType}" = "apm" ] ; then
300 get_fs_line_xvars "${_pDisk}s${CURPART}" "${STRING}"
302 get_fs_line_xvars "${_wSlice}${PARTLETTER}" "${STRING}"
306 # Check if using zfs mirror
307 echo ${XTRAOPTS} | grep -q -e "mirror" -e "raidz"
308 if [ $? -eq 0 -a "$FS" = "ZFS" ] ; then
309 if [ "${_pType}" = "gpt" -o "${_pType}" = "gptslice" ] ; then
310 XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_pDisk}p${CURPART}")
311 elif [ "${_pType}" = "apm" ] ; then
312 XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_pDisk}s${CURPART}")
314 XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_wSlice}${PARTLETTER}")
318 # Figure out the gpart type to use
320 ZFS) PARTYPE="freebsd-zfs" ;;
321 SWAP) PARTYPE="freebsd-swap" ;;
322 *) PARTYPE="freebsd-ufs" ;;
325 # Create the partition
326 if [ "${_pType}" = "gpt" ] ; then
327 if [ "$CURPART" = "2" ] ; then
328 # If this is GPT, make sure first partition is aligned to 4k
330 rc_halt "gpart add -a 4k ${SOUT} -t ${PARTYPE} ${_pDisk}"
333 rc_halt "gpart add ${SOUT} -t ${PARTYPE} ${_pDisk}"
335 elif [ "${_pType}" = "gptslice" ]; then
337 rc_halt "gpart add ${SOUT} -t ${PARTYPE} ${_wSlice}"
338 elif [ "${_pType}" = "apm" ]; then
340 rc_halt "gpart add ${SOUT} -t ${PARTYPE} ${_pDisk}"
343 rc_halt "gpart add ${SOUT} -t ${PARTYPE} -i ${CURPART} ${_wSlice}"
346 # Check if this is a root / boot partition, and stamp the right loader
347 for TESTMNT in `echo ${MNT} | sed 's|,| |g'`
349 if [ "${TESTMNT}" = "/" -a -z "${BOOTTYPE}" ] ; then
350 BOOTTYPE="${PARTYPE}"
352 if [ "${TESTMNT}" = "/boot" ] ; then
353 BOOTTYPE="${PARTYPE}"
357 # Save this data to our partition config dir
358 if [ "${_pType}" = "gpt" ] ; then
359 _dFile="`echo $_pDisk | sed 's|/|-|g'`"
360 echo "${FS}#${MNT}#${ENC}#${PLABEL}#GPT#${XTRAOPTS}" >${PARTDIR}/${_dFile}p${CURPART}
362 # Clear out any headers
364 dd if=/dev/zero of=${_pDisk}p${CURPART} count=2048 2>/dev/null
366 # If we have a enc password, save it as well
367 if [ -n "${ENCPASS}" ] ; then
368 echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}p${CURPART}-encpass
370 elif [ "${_pType}" = "apm" ] ; then
371 _dFile="`echo $_pDisk | sed 's|/|-|g'`"
372 echo "${FS}#${MNT}#${ENC}#${PLABEL}#GPT#${XTRAOPTS}" >${PARTDIR}/${_dFile}s${CURPART}
374 # Clear out any headers
376 dd if=/dev/zero of=${_pDisk}s${CURPART} count=2048 2>/dev/null
378 # If we have a enc password, save it as well
379 if [ -n "${ENCPASS}" ] ; then
380 echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}s${CURPART}-encpass
383 # MBR Partition or GPT slice
384 _dFile="`echo $_wSlice | sed 's|/|-|g'`"
385 echo "${FS}#${MNT}#${ENC}#${PLABEL}#MBR#${XTRAOPTS}#${IMAGE}" >${PARTDIR}/${_dFile}${PARTLETTER}
386 # Clear out any headers
388 dd if=/dev/zero of=${_wSlice}${PARTLETTER} count=2048 2>/dev/null
390 # If we have a enc password, save it as well
391 if [ -n "${ENCPASS}" ] ; then
392 echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}${PARTLETTER}-encpass
397 # Increment our parts counter
398 if [ "$_pType" = "gpt" -o "$_pType" = "apm" ] ; then
399 CURPART=$((CURPART+1))
400 # If this is a gpt/apm partition,
401 # we can continue and skip the MBR part letter stuff
404 CURPART=$((CURPART+1))
405 if [ "$CURPART" = "3" ] ; then CURPART="4" ; fi
409 # This partition letter is used, get the next one
410 case ${PARTLETTER} in
417 h) PARTLETTER="ERR" ;;
418 *) exit_err "ERROR: bsdlabel only supports up to letter h for partitions." ;;
421 fi # End of subsection locating a slice in config
423 echo $line | grep -q "^commitDiskLabel" 2>/dev/null
424 if [ $? -eq 0 -a "${FOUNDPARTS}" = "0" ]
427 # If this is the boot disk, stamp the right gptboot
428 if [ ! -z "${BOOTTYPE}" -a "$_pType" = "gpt" ] ; then
430 freebsd-ufs) rc_halt "gpart bootcode -p /boot/gptboot -i 1 ${_pDisk}" ;;
431 freebsd-zfs) rc_halt "gpart bootcode -p /boot/gptzfsboot -i 1 ${_pDisk}" ;;
435 # Make sure to stamp the MBR loader
436 if [ "$_pType" = "mbr" ] ; then
437 rc_halt "gpart bootcode -b /boot/boot ${_wSlice}"
440 # Found our flag to commit this label setup, check that we found at least 1 partition
441 if [ "${CURPART}" = "1" ] ; then
442 exit_err "ERROR: commitDiskLabel was called without any partition entries for it!"
450 # Reads through the config and sets up a BSDLabel for the given slice
451 populate_disk_label()
455 exit_err "ERROR: populate_disk_label() called without argument!"
458 # Set some vars from the given working slice
459 diskid="`echo $1 | cut -d ':' -f 1`"
460 disk="`echo $1 | cut -d ':' -f 1 | sed 's|-|/|g'`"
461 slicenum="`echo $1 | cut -d ':' -f 2`"
462 type="`echo $1 | cut -d ':' -f 3`"
464 # Set WRKSLICE based upon format we are using
465 if [ "$type" = "mbr" ] ; then
466 wrkslice="${diskid}s${slicenum}"
468 if [ "$type" = "apm" ] ; then
469 wrkslice="${diskid}s${slicenum}"
471 if [ "$type" = "gpt" -o "$type" = "gptslice" ] ; then
472 wrkslice="${diskid}p${slicenum}"
475 if [ ! -e "${SLICECFGDIR}/${wrkslice}" ] ; then
476 exit_err "ERROR: Missing SLICETAG data. This shouldn't happen - please let the developers know"
479 disktag="`cat ${SLICECFGDIR}/${wrkslice}`"
480 slicedev="`echo $wrkslice | sed 's|-|/|g'`"
482 # Setup the partitions with gpart
483 setup_gpart_partitions "${disktag}" "${disk}" "${slicedev}" "${slicenum}" "${type}"
487 # Function which reads in the disk slice config, and performs it
490 # We are ready to start setting up the label, lets read the config and do the actions
491 # First confirm that we have a valid WORKINGSLICES
492 if [ -z "${WORKINGSLICES}" ]; then
493 exit_err "ERROR: No slices were setup! Please report this to the maintainers"
496 # Check that the slices we have did indeed get setup and gpart worked
497 for i in $WORKINGSLICES
499 disk="`echo $i | cut -d '-' -f 1`"
500 pnum="`echo $i | cut -d '-' -f 2`"
501 type="`echo $i | cut -d '-' -f 3`"
502 if [ "$type" = "mbr" -a ! -e "${disk}s${pnum}" ] ; then
503 exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
505 if [ "$type" = "gpt" -a ! -e "${disk}p${pnum}" ] ; then
506 exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
508 if [ "$type" = "apm" -a ! -e "${disk}s${pnum}" ] ; then
509 exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
511 if [ "$type" = "gptslice" -a ! -e "${disk}p${pnum}" ] ; then
512 exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
516 # Setup some files which we'll be referring to
517 export LABELLIST="${TMPDIR}/workingLabels"
518 rm $LABELLIST >/dev/null 2>/dev/null
520 # Set our flag to determine if we've got a valid root partition in this setup
521 export FOUNDROOT="-1"
523 # Check if we are using a /boot partition
524 export USINGBOOTPART="1"
526 # Set encryption on root check
527 export USINGENCROOT="1"
529 # Make the tmp directory where we'll store FS info & mount-points
530 rm -rf ${PARTDIR} >/dev/null 2>/dev/null
531 mkdir -p ${PARTDIR} >/dev/null 2>/dev/null
532 rm -rf ${PARTDIR}-enc >/dev/null 2>/dev/null
533 mkdir -p ${PARTDIR}-enc >/dev/null 2>/dev/null
535 for i in $WORKINGSLICES
537 populate_disk_label "${i}"
540 # Check if we made a root partition
541 if [ "$FOUNDROOT" = "-1" ]
543 exit_err "ERROR: No root (/) partition specified!!"
546 # Check if we made a root partition
547 if [ "$FOUNDROOT" = "1" -a "${USINGBOOTPART}" != "0" ]
549 exit_err "ERROR: (/) partition isn't first partition on disk!"
552 if [ "${USINGENCROOT}" = "0" -a "${USINGBOOTPART}" != "0" ]
554 exit_err "ERROR: Can't encrypt (/) with no (/boot) partition!"
573 PARTLETTER=`echo "$SLICE" | sed -E 's|^.+([a-h])$|\1|'`
575 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
578 if [ "${PARTLETTER}" = "a" ]
591 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
594 if [ "${PARTLETTER}" = "a" ]
598 exit_err "/boot partition must be first partition"
624 PARTNUMBER=`echo "${SLICE}" | sed -E 's|^.+p([0-9]*)$|\1|'`
626 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
629 if [ "${PARTNUMBER}" = "2" ]
642 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
645 if [ "${PARTNUMBER}" = "2" ]
649 exit_err "/boot partition must be first partition"
677 SLICES_MBR=`ls /dev/${DISK}s[1-4]*[a-h]* 2>/dev/null`
678 SLICES_GPT=`ls /dev/${DISK}p[0-9]* 2>/dev/null`
679 SLICES_SLICE=`ls /dev/${DISK}[a-h]* 2>/dev/null`
681 if [ -n "${SLICES_MBR}" ]
683 SLICES="${SLICES_MBR}"
687 if [ -n "${SLICES_GPT}" ]
689 SLICES="${SLICES_GPT}"
693 if [ -n "${SLICES_SLICE}" ]
695 SLICES="${SLICES_SLICE}"
700 for slice in ${SLICES}
703 mount ${slice} /mnt 2>/dev/null
709 if [ "${TYPE}" = "MBR" ]
711 check_fstab_mbr "${slice}" "/mnt"
714 elif [ "${TYPE}" = "GPT" ]
716 check_fstab_gpt "${slice}" "/mnt"