summaryrefslogtreecommitdiffstats
path: root/scripts/zpool-create.sh
blob: 16f829cf6aa22640b299b833bde9c471f222cdb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash

SCRIPT_COMMON=common.sh
if [ -f ./${SCRIPT_COMMON} ]; then
. ./${SCRIPT_COMMON}
elif [ -f /usr/libexec/zfs/${SCRIPT_COMMON} ]; then
. /usr/libexec/zfs/${SCRIPT_COMMON}
else
echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
fi

PROG=zpool-create.sh

usage() {
cat << EOF
USAGE:
$0 [hvcp]

DESCRIPTION:
        Create one of several predefined zpool configurations.

OPTIONS:
        -h      Show this message
        -v      Verbose
        -c      Configuration for zpool
        -p      Name for zpool
        -d      Destroy zpool (default create)
        -l      Additional zpool options
        -s      Additional zfs options

EOF
}

check_config() {

	if [ ! -f ${ZPOOL_CONFIG} ]; then
		local NAME=`basename ${ZPOOL_CONFIG} .sh`
		ERROR="Unknown config '${NAME}', available configs are:\n"

		for CFG in `ls ${ZPOOLDIR}/ | grep ".sh"`; do
			local NAME=`basename ${CFG} .sh`
			ERROR="${ERROR}${NAME}\n"
		done

		return 1
	fi

	return 0
}

ZPOOL_CONFIG=unknown
ZPOOL_NAME=tank
ZPOOL_DESTROY=
ZPOOL_OPTIONS=""
ZFS_OPTIONS=""

while getopts 'hvc:p:dl:s:' OPTION; do
	case $OPTION in
	h)
		usage
		exit 1
		;;
	v)
		VERBOSE=1
		;;
	c)
		ZPOOL_CONFIG=${ZPOOLDIR}/${OPTARG}.sh
		;;
	p)
		ZPOOL_NAME=${OPTARG}
		;;
	d)
		ZPOOL_DESTROY=1
		;;
	l)
		ZPOOL_OPTIONS=${OPTARG}
		;;
	s)
		ZFS_OPTIONS=${OPTARG}
		;;
	?)
		usage
		exit 1
		;;
	esac
done

if [ $(id -u) != 0 ]; then
        die "Must run as root"
fi

check_config || die "${ERROR}"
. ${ZPOOL_CONFIG}

if [ ${ZPOOL_DESTROY} ]; then
	zpool_destroy
else
	zpool_create

	if [ "${ZPOOL_OPTIONS}" ]; then
		if [ ${VERBOSE} ]; then
			echo
			echo "${ZPOOL} ${ZPOOL_OPTIONS} ${ZPOOL_NAME}"
		fi
		${ZPOOL} ${ZPOOL_OPTIONS} ${ZPOOL_NAME} || exit 1
	fi

	if [ "${ZFS_OPTIONS}" ]; then
		if [ ${VERBOSE} ]; then
			echo
			echo "${ZFS} ${ZFS_OPTIONS} ${ZPOOL_NAME}"
		fi
		${ZFS} ${ZFS_OPTIONS} ${ZPOOL_NAME} || exit 1
	fi

	if [ ${VERBOSE} ]; then
		echo
		echo "zpool list"
		${ZPOOL} list || exit 1

		echo
		echo "zpool status ${ZPOOL_NAME}"
		${ZPOOL} status ${ZPOOL_NAME} || exit 1
	fi
fi

exit 0