1#!/bin/sh
2#-
3# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
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.
13#
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
24# SUCH DAMAGE.
25#
26# $FreeBSD$
27
28# Create partitions on a target disk
29#############################
30
31. ${PROGDIR}/backend/functions.sh
32
33if [ -z "${1}" ] ; then
34  echo "Error: No disk specified!"
35  exit 1
36fi
37
38if [ -z "${2}" ] ; then
39  echo "Error: No size specified!"
40  exit 1
41fi
42
43if [ ! -e "/dev/${1}" ] ; then
44  echo "Error: Disk /dev/${1} does not exist!"
45  exit 1
46fi
47
48DISK="${1}"
49MB="${2}"
50TYPE="${3}"
51STARTBLOCK="${4}"
52
53TOTALBLOCKS="`expr $MB \* 2048`"
54
55# If no TYPE specified, default to MBR
56if [ -z "$TYPE" ] ; then TYPE="mbr" ; fi
57
58# Sanity check the gpart type
59case $TYPE in
60	apm|APM) ;;
61	bsd|BSD) ;;
62	ebr|EBR) ;;
63      pc98|pc98) ;;
64	gpt|GPT) ;;
65	mbr|MBR) ;;
66    vtoc8|VTOC8) ;;
67	*) echo "Error: Unknown gpart type: $TYPE" ; exit 1 ;;
68esac
69
70# Lets figure out what number this partition will be
71LASTSLICE="`gpart show $DISK | grep -v -e $DISK -e '\- free \-' -e '^$' | awk 'END {print $3}'`"
72if [ -z "${LASTSLICE}" ] ; then
73  LASTSLICE="1"
74else
75  LASTSLICE="`expr $LASTSLICE + 1`"
76fi
77
78SLICENUM="${LASTSLICE}"
79
80# Set a 4k Aligned start block if none specified
81if [ "${SLICENUM}" = "1" -a -z "$STARTBLOCK" ] ; then
82  STARTBLOCK="2016"
83fi
84
85
86# If this is an empty disk, see if we need to create a new scheme for it
87gpart show ${DISK} >/dev/null 2>/dev/null
88if [ $? -eq 0 -a "${SLICENUM}" = "1" ] ; then
89  if [ "${TYPE}" = "mbr" -o "${TYPE}" = "MBR" ] ; then 
90    flags="-s ${TYPE} -f active"
91  else
92    flags="-s ${TYPE}"
93  fi
94  gpart create ${flags} ${DISK}
95fi
96
97# If we have a starting block, use it
98if [ -n "$STARTBLOCK" ] ; then
99  sBLOCK="-b $STARTBLOCK"
100fi
101
102gpart add ${sBLOCK} -s ${TOTALBLOCKS} -t freebsd -i ${SLICENUM} ${DISK}
103exit "$?"
104