]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - scripts/zpool-create.sh
Add support for asynchronous zvol minor operations
[FreeBSD/FreeBSD.git] / scripts / zpool-create.sh
1 #!/bin/bash
2
3 basedir="$(dirname $0)"
4
5 SCRIPT_COMMON=common.sh
6 if [ -f "${basedir}/${SCRIPT_COMMON}" ]; then
7 . "${basedir}/${SCRIPT_COMMON}"
8 else
9 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
10 fi
11
12 PROG=zpool-create.sh
13
14 usage() {
15 cat << EOF
16 USAGE:
17 $0 [hvfxcp]
18
19 DESCRIPTION:
20         Create one of several predefined zpool configurations.
21
22 OPTIONS:
23         -h      Show this message
24         -v      Verbose
25         -f      Force everything
26         -x      Disable all zpool features
27         -c      Configuration for zpool
28         -p      Name for zpool
29         -d      Destroy zpool (default create)
30         -l      Additional zpool options
31         -s      Additional zfs options
32
33 EOF
34 }
35
36 check_config() {
37
38         if [ ! -f ${ZPOOL_CONFIG} ]; then
39                 local NAME=`basename ${ZPOOL_CONFIG} .sh`
40                 ERROR="Unknown config '${NAME}', available configs are:\n"
41
42                 for CFG in `ls ${ZPOOLDIR}/ | grep ".sh"`; do
43                         local NAME=`basename ${CFG} .sh`
44                         ERROR="${ERROR}${NAME}\n"
45                 done
46
47                 return 1
48         fi
49
50         return 0
51 }
52
53 ZPOOL_CONFIG=unknown
54 ZPOOL_NAME=tank
55 ZPOOL_DESTROY=
56 ZPOOL_FLAGS=${ZPOOL_FLAGS:-""}
57 ZPOOL_OPTIONS=""
58 ZFS_OPTIONS=""
59
60 while getopts 'hvfxc:p:dl:s:' OPTION; do
61         case $OPTION in
62         h)
63                 usage
64                 exit 1
65                 ;;
66         v)
67                 VERBOSE=1
68                 VERBOSE_FLAG="-v"
69                 ;;
70         f)
71                 FORCE=1
72                 ZPOOL_FLAGS="$ZPOOL_FLAGS -f"
73                 ;;
74         x)
75                 NO_FEATURES=1
76                 ZPOOL_FLAGS="$ZPOOL_FLAGS -d"
77                 ;;
78         c)
79                 ZPOOL_CONFIG=${ZPOOLDIR}/${OPTARG}.sh
80                 ;;
81         p)
82                 ZPOOL_NAME=${OPTARG}
83                 ;;
84         d)
85                 ZPOOL_DESTROY=1
86                 ;;
87         l)
88                 ZPOOL_OPTIONS=${OPTARG}
89                 ;;
90         s)
91                 ZFS_OPTIONS=${OPTARG}
92                 ;;
93         ?)
94                 usage
95                 exit 1
96                 ;;
97         esac
98 done
99
100 if [ $(id -u) != 0 ]; then
101         die "Must run as root"
102 fi
103
104 check_config || die "${ERROR}"
105 . ${ZPOOL_CONFIG}
106
107 if [ ${ZPOOL_DESTROY} ]; then
108         zpool_destroy
109 else
110         zpool_create
111
112         if [ "${ZPOOL_OPTIONS}" ]; then
113                 if [ ${VERBOSE} ]; then
114                         echo
115                         echo "${ZPOOL} ${ZPOOL_OPTIONS} ${ZPOOL_NAME}"
116                 fi
117                 ${ZPOOL} ${ZPOOL_OPTIONS} ${ZPOOL_NAME} || exit 1
118         fi
119
120         if [ "${ZFS_OPTIONS}" ]; then
121                 if [ ${VERBOSE} ]; then
122                         echo
123                         echo "${ZFS} ${ZFS_OPTIONS} ${ZPOOL_NAME}"
124                 fi
125                 ${ZFS} ${ZFS_OPTIONS} ${ZPOOL_NAME} || exit 1
126         fi
127
128         if [ ${VERBOSE} ]; then
129                 echo
130                 echo "zpool list"
131                 ${ZPOOL} list || exit 1
132
133                 echo
134                 echo "zpool status ${ZPOOL_NAME}"
135                 ${ZPOOL} status ${ZPOOL_NAME} || exit 1
136         fi
137 fi
138
139 exit 0