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 # Create partitions on a target disk
29 #############################
31 . ${PROGDIR}/backend/functions.sh
33 if [ -z "${1}" ] ; then
34 echo "Error: No disk specified!"
38 if [ -z "${2}" ] ; then
39 echo "Error: No size specified!"
43 if [ ! -e "/dev/${1}" ] ; then
44 echo "Error: Disk /dev/${1} does not exist!"
53 TOTALBLOCKS="`expr $MB \* 2048`"
55 # If no TYPE specified, default to MBR
56 if [ -z "$TYPE" ] ; then TYPE="mbr" ; fi
58 # Sanity check the gpart type
67 *) echo "Error: Unknown gpart type: $TYPE" ; exit 1 ;;
70 # Lets figure out what number this partition will be
71 LASTSLICE="`gpart show $DISK | grep -v -e $DISK -e '\- free \-' -e '^$' | awk 'END {print $3}'`"
72 if [ -z "${LASTSLICE}" ] ; then
75 LASTSLICE="`expr $LASTSLICE + 1`"
78 SLICENUM="${LASTSLICE}"
80 # Set a 4k Aligned start block if none specified
81 if [ "${SLICENUM}" = "1" -a -z "$STARTBLOCK" ] ; then
86 # If this is an empty disk, see if we need to create a new scheme for it
87 gpart show ${DISK} >/dev/null 2>/dev/null
88 if [ $? -eq 0 -a "${SLICENUM}" = "1" ] ; then
89 if [ "${TYPE}" = "mbr" -o "${TYPE}" = "MBR" ] ; then
90 flags="-s ${TYPE} -f active"
94 gpart create ${flags} ${DISK}
97 # If we have a starting block, use it
98 if [ -n "$STARTBLOCK" ] ; then
99 sBLOCK="-b $STARTBLOCK"
102 gpart add ${sBLOCK} -s ${TOTALBLOCKS} -t freebsd -i ${SLICENUM} ${DISK}