]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/pc-sysinstall/backend-partmanager/create-part.sh
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.sbin / pc-sysinstall / backend-partmanager / create-part.sh
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
33 if [ -z "${1}" ] ; then
34   echo "Error: No disk specified!"
35   exit 1
36 fi
37
38 if [ -z "${2}" ] ; then
39   echo "Error: No size specified!"
40   exit 1
41 fi
42
43 if [ ! -e "/dev/${1}" ] ; then
44   echo "Error: Disk /dev/${1} does not exist!"
45   exit 1
46 fi
47
48 DISK="${1}"
49 MB="${2}"
50 TYPE="${3}"
51 STARTBLOCK="${4}"
52
53 TOTALBLOCKS="`expr $MB \* 2048`"
54
55 # If no TYPE specified, default to MBR
56 if [ -z "$TYPE" ] ; then TYPE="mbr" ; fi
57
58 # Sanity check the gpart type
59 case $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 ;;
68 esac
69
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
73   LASTSLICE="1"
74 else
75   LASTSLICE="`expr $LASTSLICE + 1`"
76 fi
77
78 SLICENUM="${LASTSLICE}"
79
80 # Set a 4k Aligned start block if none specified
81 if [ "${SLICENUM}" = "1" -a -z "$STARTBLOCK" ] ; then
82   STARTBLOCK="2016"
83 fi
84
85
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"
91   else
92     flags="-s ${TYPE}"
93   fi
94   gpart create ${flags} ${DISK}
95 fi
96
97 # If we have a starting block, use it
98 if [ -n "$STARTBLOCK" ] ; then
99   sBLOCK="-b $STARTBLOCK"
100 fi
101
102 gpart add ${sBLOCK} -s ${TOTALBLOCKS} -t freebsd -i ${SLICENUM} ${DISK}
103 exit "$?"