]>
Commit | Line | Data |
---|---|---|
c9c0d073 BB |
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 [hvcp] | |
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 | -c Configuration for zpool | |
27 | -p Name for zpool | |
28 | -d Destroy zpool (default create) | |
29 | -l Additional zpool options | |
30 | -s Additional zfs options | |
31 | ||
32 | EOF | |
33 | } | |
34 | ||
35 | check_config() { | |
36 | ||
37 | if [ ! -f ${ZPOOL_CONFIG} ]; then | |
38 | local NAME=`basename ${ZPOOL_CONFIG} .sh` | |
39 | ERROR="Unknown config '${NAME}', available configs are:\n" | |
40 | ||
41 | for CFG in `ls ${ZPOOLDIR}/ | grep ".sh"`; do | |
42 | local NAME=`basename ${CFG} .sh` | |
43 | ERROR="${ERROR}${NAME}\n" | |
44 | done | |
45 | ||
46 | return 1 | |
47 | fi | |
48 | ||
49 | return 0 | |
50 | } | |
51 | ||
52 | ZPOOL_CONFIG=unknown | |
53 | ZPOOL_NAME=tank | |
54 | ZPOOL_DESTROY= | |
55 | ZPOOL_OPTIONS="" | |
56 | ZFS_OPTIONS="" | |
57 | ||
58 | while getopts 'hvfc:p:dl:s:' OPTION; do | |
59 | case $OPTION in | |
60 | h) | |
61 | usage | |
62 | exit 1 | |
63 | ;; | |
64 | v) | |
65 | VERBOSE=1 | |
66 | VERBOSE_FLAG="-v" | |
67 | ;; | |
68 | f) | |
69 | FORCE=1 | |
70 | FORCE_FLAG="-f" | |
71 | ;; | |
72 | c) | |
73 | ZPOOL_CONFIG=${ZPOOLDIR}/${OPTARG}.sh | |
74 | ;; | |
75 | p) | |
76 | ZPOOL_NAME=${OPTARG} | |
77 | ;; | |
78 | d) | |
79 | ZPOOL_DESTROY=1 | |
80 | ;; | |
81 | l) | |
82 | ZPOOL_OPTIONS=${OPTARG} | |
83 | ;; | |
84 | s) | |
85 | ZFS_OPTIONS=${OPTARG} | |
86 | ;; | |
87 | ?) | |
88 | usage | |
89 | exit 1 | |
90 | ;; | |
91 | esac | |
92 | done | |
93 | ||
94 | if [ $(id -u) != 0 ]; then | |
95 | die "Must run as root" | |
96 | fi | |
97 | ||
98 | check_config || die "${ERROR}" | |
99 | . ${ZPOOL_CONFIG} | |
100 | ||
101 | if [ ${ZPOOL_DESTROY} ]; then | |
102 | zpool_destroy | |
103 | else | |
104 | zpool_create | |
105 | ||
106 | if [ "${ZPOOL_OPTIONS}" ]; then | |
107 | if [ ${VERBOSE} ]; then | |
108 | echo | |
109 | echo "${ZPOOL} ${ZPOOL_OPTIONS} ${ZPOOL_NAME}" | |
110 | fi | |
111 | ${ZPOOL} ${ZPOOL_OPTIONS} ${ZPOOL_NAME} || exit 1 | |
112 | fi | |
113 | ||
114 | if [ "${ZFS_OPTIONS}" ]; then | |
115 | if [ ${VERBOSE} ]; then | |
116 | echo | |
117 | echo "${ZFS} ${ZFS_OPTIONS} ${ZPOOL_NAME}" | |
118 | fi | |
119 | ${ZFS} ${ZFS_OPTIONS} ${ZPOOL_NAME} || exit 1 | |
120 | fi | |
121 | ||
122 | if [ ${VERBOSE} ]; then | |
123 | echo | |
124 | echo "zpool list" | |
125 | ${ZPOOL} list || exit 1 | |
126 | ||
127 | echo | |
128 | echo "zpool status ${ZPOOL_NAME}" | |
129 | ${ZPOOL} status ${ZPOOL_NAME} || exit 1 | |
130 | fi | |
131 | fi | |
132 | ||
133 | exit 0 |