2010-06-24 22:21:47 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#-
|
2010-06-27 16:46:11 +00:00
|
|
|
# Copyright (c) 2010 iXsystems, Inc. All rights reserved.
|
2010-06-24 22:21:47 +00:00
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
|
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
# SUCH DAMAGE.
|
|
|
|
#
|
|
|
|
# $FreeBSD$
|
|
|
|
|
|
|
|
# Functions related to disk operations using bsdlabel
|
|
|
|
|
|
|
|
# Check if we are are provided a geli password on the nextline of the config
|
|
|
|
check_for_enc_pass()
|
|
|
|
{
|
|
|
|
CURLINE="${1}"
|
|
|
|
|
|
|
|
get_next_cfg_line "${CFGF}" "${CURLINE}"
|
2011-03-27 16:57:54 +00:00
|
|
|
echo ${VAL} | grep -q "^encpass=" 2>/dev/null
|
|
|
|
if [ $? -eq 0 ] ; then
|
2010-06-24 22:21:47 +00:00
|
|
|
# Found a password, return it
|
|
|
|
get_value_from_string "${VAL}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2011-03-27 16:57:54 +00:00
|
|
|
export VAL=""
|
2010-06-24 22:21:47 +00:00
|
|
|
return
|
|
|
|
};
|
|
|
|
|
|
|
|
# On check on the disk-label line if we have any extra vars for this device
|
|
|
|
get_fs_line_xvars()
|
|
|
|
{
|
|
|
|
ACTIVEDEV="${1}"
|
|
|
|
LINE="${2}"
|
|
|
|
|
2012-05-03 21:53:25 +00:00
|
|
|
echo $LINE | cut -d ' ' -f 4 | grep -q '(' 2>/dev/null
|
|
|
|
if [ $? -ne 0 ] ; then return ; fi
|
|
|
|
|
|
|
|
# See if we are looking for ZFS specific options
|
|
|
|
echo $LINE | grep -q '^ZFS' 2>/dev/null
|
2011-03-27 16:57:54 +00:00
|
|
|
if [ $? -eq 0 ] ; then
|
2012-05-03 21:53:25 +00:00
|
|
|
ZTYPE="NONE"
|
|
|
|
ZFSVARS="`echo $LINE | cut -d ' ' -f 4-20 |cut -d '(' -f 2- | cut -d ')' -f 1 | xargs`"
|
2010-06-24 22:21:47 +00:00
|
|
|
|
2012-05-03 21:53:25 +00:00
|
|
|
echo $ZFSVARS | grep -qE "^(disk|file|mirror|raidz(1|2|3)?|spare|log|cache):" 2>/dev/null
|
2011-03-27 16:57:54 +00:00
|
|
|
if [ $? -eq 0 ] ; then
|
2010-08-19 06:05:05 +00:00
|
|
|
ZTYPE=`echo $ZFSVARS | cut -f1 -d:`
|
|
|
|
ZFSVARS=`echo $ZFSVARS | sed "s|$ZTYPE: ||g" | sed "s|$ZTYPE:||g"`
|
2012-05-03 21:53:25 +00:00
|
|
|
fi
|
2010-06-24 22:21:47 +00:00
|
|
|
|
2012-05-03 21:53:25 +00:00
|
|
|
# Return the ZFS options
|
|
|
|
if [ "${ZTYPE}" = "NONE" ] ; then
|
|
|
|
VAR="${ACTIVEDEV} ${ZFSVARS}"
|
|
|
|
else
|
|
|
|
VAR="${ZTYPE} ${ACTIVEDEV} ${ZFSVARS}"
|
2011-09-19 05:12:53 +00:00
|
|
|
fi
|
2012-05-03 21:53:25 +00:00
|
|
|
export VAR
|
|
|
|
return
|
|
|
|
fi # End of ZFS block
|
2011-09-19 05:12:53 +00:00
|
|
|
|
2012-05-03 21:53:25 +00:00
|
|
|
# See if we are looking for UFS specific newfs options
|
|
|
|
echo $LINE | grep -q '^UFS' 2>/dev/null
|
|
|
|
if [ $? -eq 0 ] ; then
|
|
|
|
FSVARS="`echo $LINE | cut -d '(' -f 2- | cut -d ')' -f 1 | xargs`"
|
|
|
|
VAR="${FSVARS}"
|
|
|
|
export VAR
|
|
|
|
return
|
|
|
|
fi
|
2010-06-24 22:21:47 +00:00
|
|
|
|
|
|
|
# If we got here, set VAR to empty and export
|
2011-03-27 16:57:54 +00:00
|
|
|
export VAR=""
|
2010-06-24 22:21:47 +00:00
|
|
|
return
|
|
|
|
};
|
|
|
|
|
|
|
|
# Init each zfs mirror disk with a boot sector so we can failover
|
2010-08-24 06:11:46 +00:00
|
|
|
setup_zfs_mirror_parts()
|
|
|
|
{
|
|
|
|
_nZFS=""
|
|
|
|
|
2012-05-03 21:53:25 +00:00
|
|
|
ZTYPE="`echo ${1} | awk '{print $1}'`"
|
|
|
|
|
2010-08-24 06:11:46 +00:00
|
|
|
# Using mirroring, setup boot partitions on each disk
|
2012-05-03 21:53:25 +00:00
|
|
|
_mirrline="`echo ${1} | sed 's|mirror ||g' | sed 's|raidz1 ||g' | sed 's|raidz2 ||g' | sed 's|raidz3 ||g' | sed 's|raidz ||g'`"
|
2010-08-24 06:11:46 +00:00
|
|
|
for _zvars in $_mirrline
|
|
|
|
do
|
|
|
|
echo "Looping through _zvars: $_zvars" >>${LOGOUT}
|
2011-03-27 16:57:54 +00:00
|
|
|
echo "$_zvars" | grep -q "${2}" 2>/dev/null
|
|
|
|
if [ $? -eq 0 ] ; then continue ; fi
|
2010-08-24 06:11:46 +00:00
|
|
|
if [ -z "$_zvars" ] ; then continue ; fi
|
|
|
|
|
|
|
|
is_disk "$_zvars" >/dev/null 2>/dev/null
|
2011-03-27 16:57:54 +00:00
|
|
|
if [ $? -eq 0 ] ; then
|
2012-05-03 21:53:25 +00:00
|
|
|
echo "Setting up ZFS disk $_zvars" >>${LOGOUT}
|
2010-08-24 06:11:46 +00:00
|
|
|
init_gpt_full_disk "$_zvars" >/dev/null 2>/dev/null
|
2012-05-03 21:53:25 +00:00
|
|
|
rc_halt "gpart add -a 4k -t freebsd-zfs ${_zvars}" >/dev/null 2>/dev/null
|
|
|
|
rc_halt "gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ${_zvars}" >/dev/null 2>/dev/null
|
2010-08-24 06:11:46 +00:00
|
|
|
_nZFS="$_nZFS ${_zvars}p2"
|
|
|
|
else
|
|
|
|
_nZFS="$_nZFS ${_zvars}"
|
|
|
|
fi
|
|
|
|
done
|
2012-05-03 21:53:25 +00:00
|
|
|
echo "$ZTYPE $2 `echo $_nZFS | tr -s ' '`"
|
2010-06-24 22:21:47 +00:00
|
|
|
} ;
|
|
|
|
|
|
|
|
# Function which creates a unique label name for the specified mount
|
|
|
|
gen_glabel_name()
|
|
|
|
{
|
|
|
|
MOUNT="$1"
|
|
|
|
TYPE="$2"
|
|
|
|
NUM="0"
|
|
|
|
MAXNUM="20"
|
|
|
|
|
2012-03-12 21:32:43 +00:00
|
|
|
if [ "$TYPE" = "ZFS" ] ; then
|
|
|
|
NAME="zpool"
|
|
|
|
elif [ "$MOUNT" = "/" ] ; then
|
2010-06-24 22:21:47 +00:00
|
|
|
NAME="rootfs"
|
|
|
|
else
|
|
|
|
# If doing a swap partition, also rename it
|
|
|
|
if [ "${TYPE}" = "SWAP" ]
|
|
|
|
then
|
|
|
|
NAME="swap"
|
|
|
|
else
|
|
|
|
NAME="`echo $MOUNT | sed 's|/||g' | sed 's| ||g'`"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Loop through and break when we find our first available label
|
|
|
|
while
|
|
|
|
Z=1
|
|
|
|
do
|
2011-03-27 16:57:54 +00:00
|
|
|
glabel status | grep -q "${NAME}${NUM}" 2>/dev/null
|
|
|
|
if [ $? -ne 0 ]
|
2010-06-24 22:21:47 +00:00
|
|
|
then
|
|
|
|
break
|
|
|
|
else
|
2011-03-27 16:57:54 +00:00
|
|
|
NUM=$((NUM+1))
|
2010-06-24 22:21:47 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $NUM -gt $MAXNUM ]
|
|
|
|
then
|
|
|
|
exit_err "Cannot allocate additional glabel name for $NAME"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
|
2011-03-27 16:57:54 +00:00
|
|
|
export VAL="${NAME}${NUM}"
|
2010-06-24 22:21:47 +00:00
|
|
|
};
|
|
|
|
|
2011-01-10 19:11:25 +00:00
|
|
|
# Function to setup partitions using gpart
|
|
|
|
setup_gpart_partitions()
|
2010-06-24 22:21:47 +00:00
|
|
|
{
|
2011-01-10 19:11:25 +00:00
|
|
|
local _dTag="$1"
|
|
|
|
local _pDisk="$2"
|
|
|
|
local _wSlice="$3"
|
|
|
|
local _sNum="$4"
|
|
|
|
local _pType="$5"
|
2010-06-24 22:21:47 +00:00
|
|
|
FOUNDPARTS="1"
|
|
|
|
|
2011-01-10 19:11:25 +00:00
|
|
|
# Lets read in the config file now and setup our partitions
|
|
|
|
if [ "${_pType}" = "gpt" ] ; then
|
|
|
|
CURPART="2"
|
|
|
|
else
|
|
|
|
PARTLETTER="a"
|
|
|
|
CURPART="1"
|
2011-11-08 23:44:26 +00:00
|
|
|
if [ "${_pType}" = "mbr" ] ; then
|
|
|
|
rc_halt "gpart create -s BSD ${_wSlice}"
|
|
|
|
fi
|
2011-01-10 19:11:25 +00:00
|
|
|
fi
|
2010-06-24 22:21:47 +00:00
|
|
|
|
|
|
|
while read line
|
|
|
|
do
|
|
|
|
# Check for data on this slice
|
2011-03-27 16:57:54 +00:00
|
|
|
echo $line | grep -q "^${_dTag}-part=" 2>/dev/null
|
|
|
|
if [ $? -eq 0 ]
|
2010-06-24 22:21:47 +00:00
|
|
|
then
|
|
|
|
FOUNDPARTS="0"
|
|
|
|
# Found a slice- entry, lets get the slice info
|
|
|
|
get_value_from_string "${line}"
|
|
|
|
STRING="$VAL"
|
|
|
|
|
|
|
|
# We need to split up the string now, and pick out the variables
|
|
|
|
FS=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 1`
|
|
|
|
SIZE=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 2`
|
|
|
|
MNT=`echo $STRING | tr -s '\t' ' ' | cut -d ' ' -f 3`
|
|
|
|
|
|
|
|
# Check if we have a .eli extension on this FS
|
2011-03-27 16:57:54 +00:00
|
|
|
echo ${FS} | grep -q ".eli" 2>/dev/null
|
|
|
|
if [ $? -eq 0 ]
|
2010-06-24 22:21:47 +00:00
|
|
|
then
|
|
|
|
FS="`echo ${FS} | cut -d '.' -f 1`"
|
|
|
|
ENC="ON"
|
|
|
|
check_for_enc_pass "${line}"
|
|
|
|
if [ "${VAL}" != "" ] ; then
|
|
|
|
# We have a user supplied password, save it for later
|
|
|
|
ENCPASS="${VAL}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
ENC="OFF"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if the user tried to setup / as an encrypted partition
|
|
|
|
check_for_mount "${MNT}" "/"
|
2011-03-27 16:57:54 +00:00
|
|
|
if [ $? -eq 0 -a "${ENC}" = "ON" ]
|
2010-06-24 22:21:47 +00:00
|
|
|
then
|
2011-03-27 16:57:54 +00:00
|
|
|
export USINGENCROOT="0"
|
2010-06-24 22:21:47 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Now check that these values are sane
|
|
|
|
case $FS in
|
2010-10-09 07:45:24 +00:00
|
|
|
UFS|UFS+S|UFS+J|UFS+SUJ|ZFS|SWAP) ;;
|
2010-06-24 22:21:47 +00:00
|
|
|
*) exit_err "ERROR: Invalid file system specified on $line" ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Check that we have a valid size number
|
|
|
|
expr $SIZE + 1 >/dev/null 2>/dev/null
|
2011-03-27 16:57:54 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
2010-06-24 22:21:47 +00:00
|
|
|
exit_err "ERROR: The size specified on $line is invalid"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check that the mount-point starts with /
|
2011-03-27 16:57:54 +00:00
|
|
|
echo "$MNT" | grep -qe "^/" -e "^none" 2>/dev/null
|
|
|
|
if [ $? -ne 0 ]; then
|
2010-06-24 22:21:47 +00:00
|
|
|
exit_err "ERROR: The mount-point specified on $line is invalid"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$SIZE" = "0" ]
|
|
|
|
then
|
|
|
|
SOUT=""
|
|
|
|
else
|
|
|
|
SOUT="-s ${SIZE}M"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if we found a valid root partition
|
|
|
|
check_for_mount "${MNT}" "/"
|
2011-03-27 16:57:54 +00:00
|
|
|
if [ $? -eq 0 ] ; then
|
|
|
|
export FOUNDROOT="1"
|
2011-01-10 19:11:25 +00:00
|
|
|
if [ "${CURPART}" = "2" -a "$_pType" = "gpt" ] ; then
|
2011-03-27 16:57:54 +00:00
|
|
|
export FOUNDROOT="0"
|
2011-01-10 19:11:25 +00:00
|
|
|
fi
|
|
|
|
if [ "${CURPART}" = "1" -a "$_pType" = "mbr" ] ; then
|
2011-03-27 16:57:54 +00:00
|
|
|
export FOUNDROOT="0"
|
2010-06-24 22:21:47 +00:00
|
|
|
fi
|
2011-11-08 23:44:26 +00:00
|
|
|
if [ "${CURPART}" = "1" -a "$_pType" = "gptslice" ] ; then
|
|
|
|
export FOUNDROOT="0"
|
|
|
|
fi
|
2010-06-24 22:21:47 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
check_for_mount "${MNT}" "/boot"
|
2011-03-27 16:57:54 +00:00
|
|
|
if [ $? -eq 0 ] ; then
|
|
|
|
export USINGBOOTPART="0"
|
2011-01-10 19:11:25 +00:00
|
|
|
if [ "${CURPART}" != "2" -a "${_pType}" = "gpt" ] ; then
|
|
|
|
exit_err "/boot partition must be first partition"
|
|
|
|
fi
|
|
|
|
if [ "${CURPART}" != "1" -a "${_pType}" = "mbr" ] ; then
|
2010-06-24 22:21:47 +00:00
|
|
|
exit_err "/boot partition must be first partition"
|
|
|
|
fi
|
2011-11-08 23:44:26 +00:00
|
|
|
if [ "${CURPART}" != "1" -a "${_pType}" = "gptslice" ] ; then
|
|
|
|
exit_err "/boot partition must be first partition"
|
|
|
|
fi
|
2011-01-10 19:11:25 +00:00
|
|
|
|
|
|
|
if [ "${FS}" != "UFS" -a "${FS}" != "UFS+S" -a "${FS}" != "UFS+J" -a "${FS}" != "UFS+SUJ" ] ; then
|
|
|
|
exit_err "/boot partition must be formatted with UFS"
|
|
|
|
fi
|
2010-06-24 22:21:47 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Generate a unique label name for this mount
|
|
|
|
gen_glabel_name "${MNT}" "${FS}"
|
|
|
|
PLABEL="${VAL}"
|
|
|
|
|
|
|
|
# Get any extra options for this fs / line
|
2011-01-10 19:11:25 +00:00
|
|
|
if [ "${_pType}" = "gpt" ] ; then
|
|
|
|
get_fs_line_xvars "${_pDisk}p${CURPART}" "${STRING}"
|
|
|
|
else
|
2011-11-05 19:25:21 +00:00
|
|
|
get_fs_line_xvars "${_wSlice}${PARTLETTER}" "${STRING}"
|
2011-01-10 19:11:25 +00:00
|
|
|
fi
|
2012-05-03 21:53:25 +00:00
|
|
|
XTRAOPTS="$VAR"
|
2010-06-24 22:21:47 +00:00
|
|
|
|
|
|
|
# Check if using zfs mirror
|
2012-05-03 21:53:25 +00:00
|
|
|
echo ${XTRAOPTS} | grep -q -e "mirror" -e "raidz"
|
2011-09-19 05:12:53 +00:00
|
|
|
if [ $? -eq 0 -a "$FS" = "ZFS" ] ; then
|
2011-11-08 23:44:26 +00:00
|
|
|
if [ "${_pType}" = "gpt" -o "${_pType}" = "gptslice" ] ; then
|
2011-01-10 19:11:25 +00:00
|
|
|
XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_pDisk}p${CURPART}")
|
|
|
|
else
|
2012-05-03 21:53:25 +00:00
|
|
|
XTRAOPTS=$(setup_zfs_mirror_parts "$XTRAOPTS" "${_wSlice}${PARTLETTER}")
|
2011-01-10 19:11:25 +00:00
|
|
|
fi
|
2010-06-24 22:21:47 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Figure out the gpart type to use
|
|
|
|
case ${FS} in
|
2010-08-24 06:11:46 +00:00
|
|
|
ZFS) PARTYPE="freebsd-zfs" ;;
|
|
|
|
SWAP) PARTYPE="freebsd-swap" ;;
|
|
|
|
*) PARTYPE="freebsd-ufs" ;;
|
2010-06-24 22:21:47 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
# Create the partition
|
2011-01-10 19:11:25 +00:00
|
|
|
if [ "${_pType}" = "gpt" ] ; then
|
|
|
|
if [ "$CURPART" = "2" ] ; then
|
|
|
|
# If this is GPT, make sure first partition is aligned to 4k
|
2011-11-08 23:44:26 +00:00
|
|
|
sleep 2
|
2012-03-12 21:41:29 +00:00
|
|
|
rc_halt "gpart add -a 4k ${SOUT} -t ${PARTYPE} ${_pDisk}"
|
2011-01-10 19:11:25 +00:00
|
|
|
else
|
2011-11-08 23:44:26 +00:00
|
|
|
sleep 2
|
2011-01-10 19:11:25 +00:00
|
|
|
rc_halt "gpart add ${SOUT} -t ${PARTYPE} ${_pDisk}"
|
|
|
|
fi
|
2011-11-08 23:44:26 +00:00
|
|
|
elif [ "${_pType}" = "gptslice" ]; then
|
|
|
|
sleep 2
|
|
|
|
rc_halt "gpart add ${SOUT} -t ${PARTYPE} ${_wSlice}"
|
2011-01-10 19:11:25 +00:00
|
|
|
else
|
2011-11-08 23:44:26 +00:00
|
|
|
sleep 2
|
2011-01-10 19:11:25 +00:00
|
|
|
rc_halt "gpart add ${SOUT} -t ${PARTYPE} -i ${CURPART} ${_wSlice}"
|
|
|
|
fi
|
2010-06-24 22:21:47 +00:00
|
|
|
|
|
|
|
# Check if this is a root / boot partition, and stamp the right loader
|
|
|
|
for TESTMNT in `echo ${MNT} | sed 's|,| |g'`
|
|
|
|
do
|
|
|
|
if [ "${TESTMNT}" = "/" -a -z "${BOOTTYPE}" ] ; then
|
|
|
|
BOOTTYPE="${PARTYPE}"
|
|
|
|
fi
|
|
|
|
if [ "${TESTMNT}" = "/boot" ] ; then
|
|
|
|
BOOTTYPE="${PARTYPE}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# Save this data to our partition config dir
|
2011-01-10 19:11:25 +00:00
|
|
|
if [ "${_pType}" = "gpt" ] ; then
|
2011-04-21 06:25:12 +00:00
|
|
|
_dFile="`echo $_pDisk | sed 's|/|-|g'`"
|
2012-03-12 21:32:43 +00:00
|
|
|
echo "${FS}#${MNT}#${ENC}#${PLABEL}#GPT#${XTRAOPTS}" >${PARTDIR}/${_dFile}p${CURPART}
|
2010-06-24 22:21:47 +00:00
|
|
|
|
2011-01-10 19:11:25 +00:00
|
|
|
# Clear out any headers
|
|
|
|
sleep 2
|
2011-03-27 16:57:54 +00:00
|
|
|
dd if=/dev/zero of=${_pDisk}p${CURPART} count=2048 2>/dev/null
|
2010-06-24 22:21:47 +00:00
|
|
|
|
2011-01-10 19:11:25 +00:00
|
|
|
# If we have a enc password, save it as well
|
2011-03-27 16:57:54 +00:00
|
|
|
if [ -n "${ENCPASS}" ] ; then
|
2011-04-21 06:25:12 +00:00
|
|
|
echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}p${CURPART}-encpass
|
2011-01-10 19:11:25 +00:00
|
|
|
fi
|
|
|
|
else
|
2011-11-08 23:44:26 +00:00
|
|
|
# MBR Partition or GPT slice
|
2011-04-21 06:25:12 +00:00
|
|
|
_dFile="`echo $_wSlice | sed 's|/|-|g'`"
|
2012-03-12 21:32:43 +00:00
|
|
|
echo "${FS}#${MNT}#${ENC}#${PLABEL}#MBR#${XTRAOPTS}#${IMAGE}" >${PARTDIR}/${_dFile}${PARTLETTER}
|
2011-01-10 19:11:25 +00:00
|
|
|
# Clear out any headers
|
|
|
|
sleep 2
|
2011-03-27 16:57:54 +00:00
|
|
|
dd if=/dev/zero of=${_wSlice}${PARTLETTER} count=2048 2>/dev/null
|
2011-01-10 19:11:25 +00:00
|
|
|
|
|
|
|
# If we have a enc password, save it as well
|
2011-03-27 16:57:54 +00:00
|
|
|
if [ -n "${ENCPASS}" ] ; then
|
2011-04-21 06:25:12 +00:00
|
|
|
echo "${ENCPASS}" >${PARTDIR}-enc/${_dFile}${PARTLETTER}-encpass
|
2011-01-10 19:11:25 +00:00
|
|
|
fi
|
2010-06-24 22:21:47 +00:00
|
|
|
fi
|
|
|
|
|
2011-01-10 19:11:25 +00:00
|
|
|
|
2010-06-24 22:21:47 +00:00
|
|
|
# Increment our parts counter
|
2011-01-10 19:11:25 +00:00
|
|
|
if [ "$_pType" = "gpt" ] ; then
|
2011-03-27 16:57:54 +00:00
|
|
|
CURPART=$((CURPART+1))
|
2011-01-10 19:11:25 +00:00
|
|
|
# If this is a gpt partition, we can continue and skip the MBR part letter stuff
|
|
|
|
continue
|
|
|
|
else
|
2011-03-27 16:57:54 +00:00
|
|
|
CURPART=$((CURPART+1))
|
2011-01-10 19:11:25 +00:00
|
|
|
if [ "$CURPART" = "3" ] ; then CURPART="4" ; fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# This partition letter is used, get the next one
|
|
|
|
case ${PARTLETTER} in
|
|
|
|
a) PARTLETTER="b" ;;
|
|
|
|
b) PARTLETTER="d" ;;
|
|
|
|
d) PARTLETTER="e" ;;
|
|
|
|
e) PARTLETTER="f" ;;
|
|
|
|
f) PARTLETTER="g" ;;
|
|
|
|
g) PARTLETTER="h" ;;
|
|
|
|
h) PARTLETTER="ERR" ;;
|
|
|
|
*) exit_err "ERROR: bsdlabel only supports up to letter h for partitions." ;;
|
|
|
|
esac
|
2010-06-24 22:21:47 +00:00
|
|
|
|
|
|
|
fi # End of subsection locating a slice in config
|
|
|
|
|
2011-03-27 16:57:54 +00:00
|
|
|
echo $line | grep -q "^commitDiskLabel" 2>/dev/null
|
|
|
|
if [ $? -eq 0 -a "${FOUNDPARTS}" = "0" ]
|
2010-06-24 22:21:47 +00:00
|
|
|
then
|
|
|
|
|
|
|
|
# If this is the boot disk, stamp the right gptboot
|
2011-01-10 19:11:25 +00:00
|
|
|
if [ ! -z "${BOOTTYPE}" -a "$_pType" = "gpt" ] ; then
|
2010-06-24 22:21:47 +00:00
|
|
|
case ${BOOTTYPE} in
|
2011-01-10 19:11:25 +00:00
|
|
|
freebsd-ufs) rc_halt "gpart bootcode -p /boot/gptboot -i 1 ${_pDisk}" ;;
|
|
|
|
freebsd-zfs) rc_halt "gpart bootcode -p /boot/gptzfsboot -i 1 ${_pDisk}" ;;
|
2010-06-24 22:21:47 +00:00
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
2011-01-10 19:11:25 +00:00
|
|
|
# Make sure to stamp the MBR loader
|
|
|
|
if [ "$_pType" = "mbr" ] ; then
|
|
|
|
rc_halt "gpart bootcode -b /boot/boot ${_wSlice}"
|
|
|
|
fi
|
2010-06-24 22:21:47 +00:00
|
|
|
|
|
|
|
# Found our flag to commit this label setup, check that we found at least 1 partition
|
2012-03-12 21:32:43 +00:00
|
|
|
if [ "${CURPART}" = "1" ] ; then
|
2010-06-24 22:21:47 +00:00
|
|
|
exit_err "ERROR: commitDiskLabel was called without any partition entries for it!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done <${CFGF}
|
|
|
|
};
|
|
|
|
|
|
|
|
# Reads through the config and sets up a BSDLabel for the given slice
|
|
|
|
populate_disk_label()
|
|
|
|
{
|
|
|
|
if [ -z "${1}" ]
|
|
|
|
then
|
|
|
|
exit_err "ERROR: populate_disk_label() called without argument!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set some vars from the given working slice
|
2011-04-21 06:25:12 +00:00
|
|
|
diskid="`echo $1 | cut -d ':' -f 1`"
|
|
|
|
disk="`echo $1 | cut -d ':' -f 1 | sed 's|-|/|g'`"
|
|
|
|
slicenum="`echo $1 | cut -d ':' -f 2`"
|
|
|
|
type="`echo $1 | cut -d ':' -f 3`"
|
2010-06-24 22:21:47 +00:00
|
|
|
|
|
|
|
# Set WRKSLICE based upon format we are using
|
|
|
|
if [ "$type" = "mbr" ] ; then
|
2011-04-21 06:25:12 +00:00
|
|
|
wrkslice="${diskid}s${slicenum}"
|
2010-06-24 22:21:47 +00:00
|
|
|
fi
|
2011-11-08 23:44:26 +00:00
|
|
|
if [ "$type" = "gpt" -o "$type" = "gptslice" ] ; then
|
2011-04-21 06:25:12 +00:00
|
|
|
wrkslice="${diskid}p${slicenum}"
|
2010-06-24 22:21:47 +00:00
|
|
|
fi
|
|
|
|
|
2011-04-21 06:25:12 +00:00
|
|
|
if [ ! -e "${SLICECFGDIR}/${wrkslice}" ] ; then
|
2010-06-24 22:21:47 +00:00
|
|
|
exit_err "ERROR: Missing SLICETAG data. This shouldn't happen - please let the developers know"
|
|
|
|
fi
|
|
|
|
|
2011-04-21 06:25:12 +00:00
|
|
|
disktag="`cat ${SLICECFGDIR}/${wrkslice}`"
|
|
|
|
slicedev="`echo $wrkslice | sed 's|-|/|g'`"
|
|
|
|
|
2011-01-10 19:11:25 +00:00
|
|
|
# Setup the partitions with gpart
|
2011-04-21 06:25:12 +00:00
|
|
|
setup_gpart_partitions "${disktag}" "${disk}" "${slicedev}" "${slicenum}" "${type}"
|
2010-06-24 22:21:47 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
# Function which reads in the disk slice config, and performs it
|
|
|
|
setup_disk_label()
|
|
|
|
{
|
|
|
|
# We are ready to start setting up the label, lets read the config and do the actions
|
|
|
|
# First confirm that we have a valid WORKINGSLICES
|
2010-10-09 08:52:09 +00:00
|
|
|
if [ -z "${WORKINGSLICES}" ]; then
|
2010-06-24 22:21:47 +00:00
|
|
|
exit_err "ERROR: No slices were setup! Please report this to the maintainers"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check that the slices we have did indeed get setup and gpart worked
|
|
|
|
for i in $WORKINGSLICES
|
|
|
|
do
|
|
|
|
disk="`echo $i | cut -d '-' -f 1`"
|
|
|
|
pnum="`echo $i | cut -d '-' -f 2`"
|
|
|
|
type="`echo $i | cut -d '-' -f 3`"
|
2011-04-21 06:25:12 +00:00
|
|
|
if [ "$type" = "mbr" -a ! -e "${disk}s${pnum}" ] ; then
|
2010-06-24 22:21:47 +00:00
|
|
|
exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
|
|
|
|
fi
|
2011-04-21 06:25:12 +00:00
|
|
|
if [ "$type" = "gpt" -a ! -e "${disk}p${pnum}" ] ; then
|
2010-06-24 22:21:47 +00:00
|
|
|
exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
|
|
|
|
fi
|
2011-11-08 23:44:26 +00:00
|
|
|
if [ "$type" = "gptslice" -a ! -e "${disk}p${pnum}" ] ; then
|
|
|
|
exit_err "ERROR: The partition ${i} doesn't exist! gpart failure!"
|
|
|
|
fi
|
2010-06-24 22:21:47 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# Setup some files which we'll be referring to
|
2011-03-27 16:57:54 +00:00
|
|
|
export LABELLIST="${TMPDIR}/workingLabels"
|
2010-06-24 22:21:47 +00:00
|
|
|
rm $LABELLIST >/dev/null 2>/dev/null
|
|
|
|
|
|
|
|
# Set our flag to determine if we've got a valid root partition in this setup
|
2011-03-27 16:57:54 +00:00
|
|
|
export FOUNDROOT="-1"
|
2010-06-24 22:21:47 +00:00
|
|
|
|
|
|
|
# Check if we are using a /boot partition
|
2011-03-27 16:57:54 +00:00
|
|
|
export USINGBOOTPART="1"
|
2010-06-24 22:21:47 +00:00
|
|
|
|
|
|
|
# Set encryption on root check
|
2011-03-27 16:57:54 +00:00
|
|
|
export USINGENCROOT="1"
|
2010-06-24 22:21:47 +00:00
|
|
|
|
|
|
|
# Make the tmp directory where we'll store FS info & mount-points
|
|
|
|
rm -rf ${PARTDIR} >/dev/null 2>/dev/null
|
|
|
|
mkdir -p ${PARTDIR} >/dev/null 2>/dev/null
|
|
|
|
rm -rf ${PARTDIR}-enc >/dev/null 2>/dev/null
|
|
|
|
mkdir -p ${PARTDIR}-enc >/dev/null 2>/dev/null
|
|
|
|
|
|
|
|
for i in $WORKINGSLICES
|
|
|
|
do
|
|
|
|
populate_disk_label "${i}"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Check if we made a root partition
|
|
|
|
if [ "$FOUNDROOT" = "-1" ]
|
|
|
|
then
|
|
|
|
exit_err "ERROR: No root (/) partition specified!!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if we made a root partition
|
|
|
|
if [ "$FOUNDROOT" = "1" -a "${USINGBOOTPART}" != "0" ]
|
|
|
|
then
|
|
|
|
exit_err "ERROR: (/) partition isn't first partition on disk!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "${USINGENCROOT}" = "0" -a "${USINGBOOTPART}" != "0" ]
|
|
|
|
then
|
|
|
|
exit_err "ERROR: Can't encrypt (/) with no (/boot) partition!"
|
|
|
|
fi
|
|
|
|
};
|
|
|
|
|
2010-09-08 20:10:24 +00:00
|
|
|
check_fstab_mbr()
|
|
|
|
{
|
|
|
|
local SLICE
|
|
|
|
local FSTAB
|
|
|
|
|
|
|
|
if [ -z "$2" ]
|
|
|
|
then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
SLICE="$1"
|
|
|
|
FSTAB="$2/etc/fstab"
|
|
|
|
|
|
|
|
if [ -f "${FSTAB}" ]
|
|
|
|
then
|
|
|
|
PARTLETTER=`echo "$SLICE" | sed -E 's|^.+([a-h])$|\1|'`
|
|
|
|
|
2011-03-27 16:57:54 +00:00
|
|
|
cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
|
|
|
|
if [ $? -eq 0 ]
|
2010-09-08 20:10:24 +00:00
|
|
|
then
|
|
|
|
if [ "${PARTLETTER}" = "a" ]
|
|
|
|
then
|
|
|
|
FOUNDROOT="0"
|
|
|
|
else
|
|
|
|
FOUNDROOT="1"
|
|
|
|
fi
|
2010-10-09 08:52:09 +00:00
|
|
|
|
|
|
|
ROOTIMAGE="1"
|
|
|
|
|
2010-09-08 20:10:24 +00:00
|
|
|
export FOUNDROOT
|
2010-10-09 08:52:09 +00:00
|
|
|
export ROOTIMAGE
|
2010-09-08 20:10:24 +00:00
|
|
|
fi
|
|
|
|
|
2011-03-27 16:57:54 +00:00
|
|
|
cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
|
|
|
|
if [ $? -eq 0 ]
|
2010-09-08 20:10:24 +00:00
|
|
|
then
|
|
|
|
if [ "${PARTLETTER}" = "a" ]
|
|
|
|
then
|
|
|
|
USINGBOOTPART="0"
|
|
|
|
else
|
|
|
|
exit_err "/boot partition must be first partition"
|
|
|
|
fi
|
|
|
|
export USINGBOOTPART
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1
|
|
|
|
};
|
|
|
|
|
|
|
|
check_fstab_gpt()
|
|
|
|
{
|
|
|
|
local SLICE
|
|
|
|
local FSTAB
|
|
|
|
|
|
|
|
if [ -z "$2" ]
|
|
|
|
then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
SLICE="$1"
|
|
|
|
FSTAB="$2/etc/fstab"
|
|
|
|
|
|
|
|
if [ -f "${FSTAB}" ]
|
|
|
|
then
|
|
|
|
PARTNUMBER=`echo "${SLICE}" | sed -E 's|^.+p([0-9]*)$|\1|'`
|
|
|
|
|
2011-03-27 16:57:54 +00:00
|
|
|
cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/$' 2>&1
|
|
|
|
if [ $? -eq 0 ]
|
2010-09-08 20:10:24 +00:00
|
|
|
then
|
|
|
|
if [ "${PARTNUMBER}" = "2" ]
|
|
|
|
then
|
|
|
|
FOUNDROOT="0"
|
|
|
|
else
|
|
|
|
FOUNDROOT="1"
|
|
|
|
fi
|
2010-10-09 08:52:09 +00:00
|
|
|
|
|
|
|
ROOTIMAGE="1"
|
|
|
|
|
2010-09-08 20:10:24 +00:00
|
|
|
export FOUNDROOT
|
2010-10-09 08:52:09 +00:00
|
|
|
export ROOTIMAGE
|
2010-09-08 20:10:24 +00:00
|
|
|
fi
|
|
|
|
|
2011-03-27 16:57:54 +00:00
|
|
|
cat "${FSTAB}" | awk '{ print $2 }' | grep -qE '^/boot$' 2>&1
|
|
|
|
if [ $? -eq 0 ]
|
2010-09-08 20:10:24 +00:00
|
|
|
then
|
|
|
|
if [ "${PARTNUMBER}" = "2" ]
|
|
|
|
then
|
|
|
|
USINGBOOTPART="0"
|
|
|
|
else
|
|
|
|
exit_err "/boot partition must be first partition"
|
|
|
|
fi
|
|
|
|
export USINGBOOTPART
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
return 1
|
|
|
|
};
|
|
|
|
|
2010-10-09 08:52:09 +00:00
|
|
|
check_disk_layout()
|
2010-09-08 20:10:24 +00:00
|
|
|
{
|
|
|
|
local SLICES
|
|
|
|
local TYPE
|
2010-10-09 08:52:09 +00:00
|
|
|
local DISK
|
2010-09-08 20:10:24 +00:00
|
|
|
local RES
|
|
|
|
local F
|
|
|
|
|
2010-10-09 08:52:09 +00:00
|
|
|
DISK="$1"
|
2010-09-08 20:10:24 +00:00
|
|
|
TYPE="MBR"
|
|
|
|
|
2010-10-09 08:52:09 +00:00
|
|
|
if [ -z "${DISK}" ]
|
2010-09-08 20:10:24 +00:00
|
|
|
then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2010-10-09 08:52:09 +00:00
|
|
|
SLICES_MBR=`ls /dev/${DISK}s[1-4]*[a-h]* 2>/dev/null`
|
|
|
|
SLICES_GPT=`ls /dev/${DISK}p[0-9]* 2>/dev/null`
|
|
|
|
SLICES_SLICE=`ls /dev/${DISK}[a-h]* 2>/dev/null`
|
|
|
|
|
|
|
|
if [ -n "${SLICES_MBR}" ]
|
2010-09-08 20:10:24 +00:00
|
|
|
then
|
2010-10-09 08:52:09 +00:00
|
|
|
SLICES="${SLICES_MBR}"
|
|
|
|
TYPE="MBR"
|
|
|
|
RES=0
|
2010-09-08 20:10:24 +00:00
|
|
|
fi
|
2010-10-09 08:52:09 +00:00
|
|
|
if [ -n "${SLICES_GPT}" ]
|
2010-09-08 20:10:24 +00:00
|
|
|
then
|
2010-10-09 08:52:09 +00:00
|
|
|
SLICES="${SLICES_GPT}"
|
|
|
|
TYPE="GPT"
|
2010-09-08 20:10:24 +00:00
|
|
|
RES=0
|
|
|
|
fi
|
2010-10-09 08:52:09 +00:00
|
|
|
if [ -n "${SLICES_SLICE}" ]
|
|
|
|
then
|
|
|
|
SLICES="${SLICES_SLICE}"
|
|
|
|
TYPE="MBR"
|
|
|
|
RES=0
|
|
|
|
fi
|
|
|
|
|
2010-09-08 20:10:24 +00:00
|
|
|
for slice in ${SLICES}
|
|
|
|
do
|
|
|
|
F=1
|
|
|
|
mount ${slice} /mnt 2>/dev/null
|
2011-03-27 16:57:54 +00:00
|
|
|
if [ $? -ne 0 ]
|
2010-09-08 20:10:24 +00:00
|
|
|
then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "${TYPE}" = "MBR" ]
|
|
|
|
then
|
|
|
|
check_fstab_mbr "${slice}" "/mnt"
|
|
|
|
F="$?"
|
|
|
|
|
|
|
|
elif [ "${TYPE}" = "GPT" ]
|
|
|
|
then
|
|
|
|
check_fstab_gpt "${slice}" "/mnt"
|
|
|
|
F="$?"
|
|
|
|
fi
|
|
|
|
|
2011-03-27 16:57:54 +00:00
|
|
|
if [ ${F} -eq 0 ]
|
2010-09-08 20:10:24 +00:00
|
|
|
then
|
|
|
|
umount /mnt
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
|
|
|
|
umount /mnt
|
|
|
|
done
|
|
|
|
|
|
|
|
return ${RES}
|
|
|
|
};
|