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 determine the size we can safely use when 0 is specified
170 # Disk tag to look for
174 get_disk_mediasize_mb "$2"
179 # Check for data on this slice
180 echo $line | grep -q "^${_dTag}-part=" 2>/dev/null
181 if [ $? -ne 0 ] ; then continue ; fi
183 get_value_from_string "${line}"
186 # Get the size of this partition
187 SIZE=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 2`
188 if [ $SIZE -eq 0 ] ; then continue ; fi
189 _aSize=`expr $_aSize - $SIZE`
193 _aSize=`expr $_aSize - 2`
199 # Function to setup partitions using gpart
200 setup_gpart_partitions()
210 # Lets read in the config file now and setup our partitions
211 if [ "${_pType}" = "gpt" ] ; then
213 elif [ "${_pType}" = "apm" ] ; then
218 if [ "${_pType}" = "mbr" ] ; then
219 rc_halt "gpart create -s BSD ${_wSlice}"
225 # Check for data on this slice
226 echo $line | grep -q "^${_dTag}-part=" 2>/dev/null
230 # Found a slice- entry, lets get the slice info
231 get_value_from_string "${line}"
234 # We need to split up the string now, and pick out the variables
235 FS=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 1`
236 SIZE=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 2`
237 MNT=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 3`
239 # Check if we have a .eli extension on this FS
240 echo ${FS} | grep -q ".eli" 2>/dev/null
243 FS="`echo ${FS} | cut -d '.' -f 1`"
245 check_for_enc_pass "${line}"
246 if [ "${VAL}" != "" ] ; then
247 # We have a user supplied password, save it for later
254 # Check if the user tried to setup / as an encrypted partition
255 check_for_mount "${MNT}" "/"
256 if [ $? -eq 0 -a "${ENC}" = "ON" ]
258 export USINGENCROOT="0"
261 # Now check that these values are sane
263 UFS|UFS+S|UFS+J|UFS+SUJ|ZFS|SWAP) ;;
264 *) exit_err "ERROR: Invalid file system specified on $line" ;;
267 # Check that we have a valid size number
268 expr $SIZE + 1 >/dev/null 2>/dev/null
269 if [ $? -ne 0 ]; then
270 exit_err "ERROR: The size specified on $line is invalid"
273 # Check that the mount-point starts with /
274 echo "$MNT" | grep -qe "^/" -e "^none" 2>/dev/null
275 if [ $? -ne 0 ]; then
276 exit_err "ERROR: The mount-point specified on $line is invalid"
281 if [ $USEDAUTOSIZE -eq 1 ] ; then
282 exit_err "ERROR: You can not have two partitions with a size of 0 specified!"
285 gpt|apm) get_autosize "${_dTag}" "$_pDisk" ;;
286 *) get_autosize "${_dTag}" "$_wSlice" ;;
294 # Check if we found a valid root partition
295 check_for_mount "${MNT}" "/"
296 if [ $? -eq 0 ] ; then
298 if [ "${CURPART}" = "2" -a "$_pType" = "gpt" ] ; then
301 if [ "${CURPART}" = "3" -a "$_pType" = "apm" ] ; then
304 if [ "${CURPART}" = "1" -a "$_pType" = "mbr" ] ; then
307 if [ "${CURPART}" = "1" -a "$_pType" = "gptslice" ] ; then
312 check_for_mount "${MNT}" "/boot"
313 if [ $? -eq 0 ] ; then
314 export USINGBOOTPART="0"
315 if [ "${CURPART}" != "2" -a "${_pType}" = "gpt" ] ; then
316 exit_err "/boot partition must be first partition"
318 if [ "${CURPART}" != "3" -a "${_pType}" = "apm" ] ; then
319 exit_err "/boot partition must be first partition"
321 if [ "${CURPART}" != "1" -a "${_pType}" = "mbr" ] ; then
322 exit_err "/boot partition must be first partition"
324 if [ "${CURPART}" != "1" -a "${_pType}" = "gptslice" ] ; then
325 exit_err "/boot partition must be first partition"
328 if [ "${FS}" != "UFS" -a "${FS}" != "UFS+S" -a "${FS}" != "UFS+J" -a "${FS}" != "UFS+SUJ" ] ; then
329 exit_err "/boot partition must be formatted with UFS"
333 # Generate a unique label name for this mount
334 gen_glabel_name "${MNT}" "${FS}"
337 # Get any extra options for this fs / line
338 if [ "${_pType}" = "gpt" ] ; then
339 get_fs_line_xvars "${_pDisk}p${CURPART}" "${STRING}"
340 elif [ "${_pType}" = "apm" ] ; then
341 get_fs_line_xvars "${_pDisk}s${CURPART}" "${STRING}"
343 get_fs_line_xvars "${_wSlice}${PARTLETTER}" "${STRING}"
347 # Check if using zfs mirror
348 echo ${XTRAOPTS} | grep -q -e "mirror" -e "raidz"
349 if [ $? -eq 0 -a "$FS" = "ZFS" ] ; then
350 if [ "${_pType}" = "gpt" -o "${_pType}" = "gptslice" ] ; then
351 XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_pDisk}p${CURPART}")
352 elif [ "${_pType}" = "apm" ] ; then
353 XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_pDisk}s${CURPART}")
355 XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_wSlice}${PARTLETTER}")
359 # Figure out the gpart type to use
361 ZFS) PARTYPE="freebsd-zfs" ;;
362 SWAP) PARTYPE="freebsd-swap" ;;
363 *) PARTYPE="freebsd-ufs" ;;
366 # Create the partition
367 if [ "${_pType}" = "gpt" ] ; then
368 if [ "$CURPART" = "2" ] ; then
369 # If this is GPT, make sure first partition is aligned to 4k
371 rc_halt "gpart add -a 4k ${SOUT} -t ${PARTYPE} ${_pDisk}"
374 rc_halt "gpart add ${SOUT} -t ${PARTYPE} ${_pDisk}"
376 elif [ "${_pType}" = "gptslice" ]; then
378 rc_halt "gpart add ${SOUT} -t ${PARTYPE} ${_wSlice}"
379 elif [ "${_pType}" = "apm" ]; then
381 rc_halt "gpart add ${SOUT} -t ${PARTYPE} ${_pDisk}"
384 rc_halt "gpart add ${SOUT} -t ${PARTYPE} -i ${CURPART} ${_wSlice}"
387 # Check if this is a root / boot partition, and stamp the right loader
388 for TESTMNT in `echo ${MNT} | sed 's|,| |g'`
390 if [ "${TESTMNT}" = "/" -a -z "${BOOTTYPE}" ] ; then
391 BOOTTYPE="${PARTYPE}"
393 if [ "${TESTMNT}" = "/boot" ] ; then
394 BOOTTYPE="${PARTYPE}"
398 # Save this data to our partition config dir
399 if [ "${_pType}" = "gpt" ] ; then
400 _dFile="`echo $_pDisk | sed 's|/|-|g'`"
401 echo "${FS}#${MNT}#${ENC}#${PLABEL}#GPT#${XTRAOPTS}" >${PARTDIR}/${_dFile}p${CURPART}
403 # Clear out any headers
405 dd if=/dev/zero of=${_pDisk}p${CURPART} count=2048 2>/dev/null
407 # If we have a enc password, save it as well
408 if [ -n "${ENCPASS}" ] ; then
409 echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}p${CURPART}-encpass
411 elif [ "${_pType}" = "apm" ] ; then
412 _dFile="`echo $_pDisk | sed 's|/|-|g'`"
413 echo "${FS}#${MNT}#${ENC}#${PLABEL}#GPT#${XTRAOPTS}" >${PARTDIR}/${_dFile}s${CURPART}
415 # Clear out any headers
417 dd if=/dev/zero of=${_pDisk}s${CURPART} count=2048 2>/dev/null
419 # If we have a enc password, save it as well
420 if [ -n "${ENCPASS}" ] ; then
421 echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}s${CURPART}-encpass
424 # MBR Partition or GPT slice
425 _dFile="`echo $_wSlice | sed 's|/|-|g'`"
426 echo "${FS}#${MNT}#${ENC}#${PLABEL}#MBR#${XTRAOPTS}#${IMAGE}" >${PARTDIR}/${_dFile}${PARTLETTER}
427 # Clear out any headers
429 dd if=/dev/zero of=${_wSlice}${PARTLETTER} count=2048 2>/dev/null
431 # If we have a enc password, save it as well
432 if [ -n "${ENCPASS}" ] ; then
433 echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}${PARTLETTER}-encpass
438 # Increment our parts counter
439 if [ "$_pType" = "gpt" -o "$_pType" = "apm" ] ; then
440 CURPART=$((CURPART+1))
441 # If this is a gpt/apm partition,
442 # we can continue and skip the MBR part letter stuff
445 CURPART=$((CURPART+1))
446 if [ "$CURPART" = "3" ] ; then CURPART="4" ; fi
450 # This partition letter is used, get the next one
451 case ${PARTLETTER} in
458 h) PARTLETTER="ERR" ;;
459 *) exit_err "ERROR: bsdlabel only supports up to letter h for partitions." ;;
462 fi # End of subsection locating a slice in config
464 echo $line | grep -q "^commitDiskLabel" 2>/dev/null
465 if [ $? -eq 0 -a "${FOUNDPARTS}" = "0" ]
468 # If this is the boot disk, stamp the right gptboot
469 if [ ! -z "${BOOTTYPE}" -a "$_pType" = "gpt" ] ; then
471 freebsd-ufs) rc_halt "gpart bootcode -p /boot/gptboot -i 1 ${_pDisk}" ;;
472 freebsd-zfs) rc_halt "gpart bootcode -p /boot/gptzfsboot -i 1 ${_pDisk}" ;;
476 # Make sure to stamp the MBR loader
477 if [ "$_pType" = "mbr" ] ; then
478 rc_halt "gpart bootcode -b /boot/boot ${_wSlice}"
481 # Found our flag to commit this label setup, check that we found at least 1 partition
482 if [ "${CURPART}" = "1" ] ; then
483 exit_err "ERROR: commitDiskLabel was called without any partition entries for it!"
491 # Reads through the config and sets up a BSDLabel for the given slice
492 populate_disk_label()
496 exit_err "ERROR: populate_disk_label() called without argument!"
499 # Set some vars from the given working slice
500 diskid="`echo $1 | cut -d ':' -f 1`"
501 disk="`echo $1 | cut -d ':' -f 1 | sed 's|-|/|g'`"
502 slicenum="`echo $1 | cut -d ':' -f 2`"
503 type="`echo $1 | cut -d ':' -f 3`"
505 # Set WRKSLICE based upon format we are using
506 if [ "$type" = "mbr" ] ; then
507 wrkslice="${diskid}s${slicenum}"
509 if [ "$type" = "apm" ] ; then
510 wrkslice="${diskid}s${slicenum}"
512 if [ "$type" = "gpt" -o "$type" = "gptslice" ] ; then
513 wrkslice="${diskid}p${slicenum}"
516 if [ ! -e "${SLICECFGDIR}/${wrkslice}" ] ; then
517 exit_err "ERROR: Missing SLICETAG data. This shouldn't happen - please let the developers know"
520 disktag="`cat ${SLICECFGDIR}/${wrkslice}`"
521 slicedev="`echo $wrkslice | sed 's|-|/|g'`"
523 # Setup the partitions with gpart
524 setup_gpart_partitions "${disktag}" "${disk}" "${slicedev}" "${slicenum}" "${type}"
528 # Function which reads in the disk slice config, and performs it
531 # We are ready to start setting up the label, lets read the config and do the actions
532 # First confirm that we have a valid WORKINGSLICES
533 if [ -z "${WORKINGSLICES}" ]; then
534 exit_err "ERROR: No slices were setup! Please report this to the maintainers"
537 # Check that the slices we have did indeed get setup and gpart worked
538 for i in $WORKINGSLICES
540 disk="`echo $i | cut -d '-' -f 1`"
541 pnum="`echo $i | cut -d '-' -f 2`"
542 type="`echo $i | cut -d '-' -f 3`"
543 if [ "$type" = "mbr" -a ! -e "${disk}s${pnum}" ] ; then
544 exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
546 if [ "$type" = "gpt" -a ! -e "${disk}p${pnum}" ] ; then
547 exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
549 if [ "$type" = "apm" -a ! -e "${disk}s${pnum}" ] ; then
550 exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
552 if [ "$type" = "gptslice" -a ! -e "${disk}p${pnum}" ] ; then
553 exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
557 # Setup some files which we'll be referring to
558 export LABELLIST="${TMPDIR}/workingLabels"
559 rm $LABELLIST >/dev/null 2>/dev/null
561 # Set our flag to determine if we've got a valid root partition in this setup
562 export FOUNDROOT="-1"
564 # Check if we are using a /boot partition
565 export USINGBOOTPART="1"
567 # Set encryption on root check
568 export USINGENCROOT="1"
570 # Make the tmp directory where we'll store FS info & mount-points
571 rm -rf ${PARTDIR} >/dev/null 2>/dev/null
572 mkdir -p ${PARTDIR} >/dev/null 2>/dev/null
573 rm -rf ${PARTDIR}-enc >/dev/null 2>/dev/null
574 mkdir -p ${PARTDIR}-enc >/dev/null 2>/dev/null
576 for i in $WORKINGSLICES
578 populate_disk_label "${i}"
581 # Check if we made a root partition
582 if [ "$FOUNDROOT" = "-1" ]
584 exit_err "ERROR: No root (/) partition specified!!"
587 # Check if we made a root partition
588 if [ "$FOUNDROOT" = "1" -a "${USINGBOOTPART}" != "0" ]
590 exit_err "ERROR: (/) partition isn't first partition on disk!"
593 if [ "${USINGENCROOT}" = "0" -a "${USINGBOOTPART}" != "0" ]
595 exit_err "ERROR: Can't encrypt (/) with no (/boot) partition!"
614 PARTLETTER=`echo "$SLICE" | sed -E 's|^.+([a-h])$|\1|'`
616 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
619 if [ "${PARTLETTER}" = "a" ]
632 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
635 if [ "${PARTLETTER}" = "a" ]
639 exit_err "/boot partition must be first partition"
665 PARTNUMBER=`echo "${SLICE}" | sed -E 's|^.+p([0-9]*)$|\1|'`
667 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
670 if [ "${PARTNUMBER}" = "2" ]
683 cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
686 if [ "${PARTNUMBER}" = "2" ]
690 exit_err "/boot partition must be first partition"
718 SLICES_MBR=`ls /dev/${DISK}s[1-4]*[a-h]* 2>/dev/null`
719 SLICES_GPT=`ls /dev/${DISK}p[0-9]* 2>/dev/null`
720 SLICES_SLICE=`ls /dev/${DISK}[a-h]* 2>/dev/null`
722 if [ -n "${SLICES_MBR}" ]
724 SLICES="${SLICES_MBR}"
728 if [ -n "${SLICES_GPT}" ]
730 SLICES="${SLICES_GPT}"
734 if [ -n "${SLICES_SLICE}" ]
736 SLICES="${SLICES_SLICE}"
741 for slice in ${SLICES}
744 mount ${slice} /mnt 2>/dev/null
750 if [ "${TYPE}" = "MBR" ]
752 check_fstab_mbr "${slice}" "/mnt"
755 elif [ "${TYPE}" = "GPT" ]
757 check_fstab_gpt "${slice}" "/mnt"