-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathZFS
executable file
·211 lines (189 loc) · 6.54 KB
/
ZFS
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/sh
#
# License: GNU General Public License (GPL)
# Support: zfs@lists.illumos.org
# Written by: Saso Kiselkov
#
# This script manages ZFS pools
# It can import a ZFS pool or export it
#
# usage: $0 {start|stop|status|monitor|validate-all|meta-data}
#
# The "start" arg imports a ZFS pool.
# The "stop" arg exports it.
#
# OCF parameters are as follows
# OCF_RESKEY_pool - the pool to import/export
#
#######################################################################
# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
: ${HELPERS_DIR=${OCF_ROOT}/lib/heartbeat/helpers}
# Set the variable below to '1' to enable zpool import/export logging.
# This is useful to determine how long these operations take on your system.
# DEBUG=0
DEBUGLOG="/var/log/ZFS_cluster_debug.log"
USAGE="usage: $0 {start|stop|status|monitor|validate-all|meta-data}";
#######################################################################
debug_log () {
if [ "$DEBUG" -eq 1 ]; then
echo "`date`:${OCF_RESKEY_pool}:" $@ >> "$DEBUGLOG"
fi
}
meta_data() {
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="ZFS">
<version>1.0</version>
<longdesc lang="en">
This script manages ZFS pools
It can import a ZFS pool or export it
</longdesc>
<shortdesc lang="en">Manages ZFS pools</shortdesc>
<parameters>
<parameter name="pool" unique="1" required="1">
<longdesc lang="en">
The name of the ZFS pool to manage, e.g. "tank".
</longdesc>
<shortdesc lang="en">ZFS pool name</shortdesc>
<content type="string" default="" />
</parameter>
<parameter name="importargs" unique="0" required="0">
<longdesc lang="en">
Arguments to zpool import, e.g. "-d /dev/disk/by-id".
</longdesc>
<shortdesc lang="en">Import arguments</shortdesc>
<content type="string" default="" />
</parameter>
</parameters>
<actions>
<action name="start" timeout="60s" />
<action name="stop" timeout="60s" />
<action name="monitor" depth="0" timeout="30s" interval="5s" />
<action name="validate-all" timeout="30s" />
<action name="meta-data" timeout="5s" />
</actions>
</resource-agent>
END
exit $OCF_SUCCESS
}
zpool_is_imported () {
zpool list -H "$OCF_RESKEY_pool" > /dev/null
}
# Forcibly imports a ZFS pool, mounting all of its auto-mounted filesystems
# (as configured in the `mountpoint' and `canmount' properties)
# If the pool is already imported, no operation is taken.
# You can use the zfs-helper script to assist pool setup before and/or
# after import.
zpool_import () {
if ! zpool_is_imported; then
debug_log "starting import"
if [ -x "${HELPERS_DIR}/zfs-helper" ]; then
debug_log "pre-import: start"
"${HELPERS_DIR}/zfs-helper" pre-import "$OCF_RESKEY_pool"
debug_log "pre-import: done"
fi
# The meanings of the options to import are as follows:
# -f : import even if the pool is marked as imported to another
# system - the system may have failed and not exported it
# cleanly.
# -o cachefile=none : the import should be temporary, so do not
# cache it persistently (across machine reboots). We want
# the CRM to explicitly control imports of this pool.
if zpool import -f $OCF_RESKEY_importargs -o cachefile=none "$OCF_RESKEY_pool" ; then
debug_log "import successful"
if [ -x "${HELPERS_DIR}/zfs-helper" ]; then
debug_log "post-import: starting"
"${HELPERS_DIR}/zfs-helper" post-import "$OCF_RESKEY_pool"
debug_log "post-import: done"
fi
return $OCF_SUCCESS
else
debug_log "import failed"
return $OCF_ERR_GENERIC
fi
fi
}
# Forcibly exports a ZFS pool, unmounting all of its filesystems in the process
# If the pool is not imported, no operation is taken.
# You can use the zfs-helper script to assist pool setup before and/or
# after export.
zpool_export () {
if zpool_is_imported; then
debug_log "starting export"
if [ -x "${HELPERS_DIR}/zfs-helper" ]; then
debug_log "pre-export: start"
"${HELPERS_DIR}/zfs-helper" pre-export "$OCF_RESKEY_pool"
debug_log "pre-export: done"
fi
# -f : force the export, even if we have mounted filesystems
# Please note that this may fail with a "busy" error if there are
# other kernel subsystems accessing the pool (e.g. SCSI targets).
# Always make sure the pool export is last in your failover logic.
if zpool export -f "$OCF_RESKEY_pool" ; then
debug_log "export successful"
if [ -x "${HELPERS_DIR}/zfs-helper" ]; then
debug_log "post-export: starting"
"${HELPERS_DIR}/zfs-helper" post-export "$OCF_RESKEY_pool"
debug_log "post-export: done"
fi
return $OCF_SUCCESS
else
debug_log "export failed"
return $OCF_ERR_GENERIC
fi
fi
}
# Monitors the health of a ZFS pool resource. Please note that this only
# checks whether the pool is imported and functional, not whether it has
# any degraded devices (use monitoring systems such as Zabbix for that).
zpool_monitor () {
# If the pool is not imported, then we can't monitor its health
if ! zpool_is_imported; then
return $OCF_NOT_RUNNING
fi
# Check the pool status
HEALTH=`zpool list -H -o health "$OCF_RESKEY_pool"`
case "$HEALTH" in
ONLINE|DEGRADED) return $OCF_SUCCESS;;
FAULTED) return $OCF_NOT_RUNNING;;
*) return $OCF_ERR_GENERIC;;
esac
}
# Validates whether we can import a given ZFS pool
zpool_validate () {
# Check that the `zpool' command is known
if ! which zpool > /dev/null; then
return $OCF_ERR_INSTALLED
fi
# If the pool is imported, then it is obviously valid
if zpool_is_imported; then
return $OCF_SUCCESS
fi
# Check that the pool can be imported
if zpool import $OCF_RESKEY_importargs | grep 'pool:' | grep "\\<$OCF_RESKEY_pool\\>" > /dev/null;
then
return $OCF_SUCCESS
else
return $OCF_ERR_CONFIGURED
fi
}
usage () {
echo $USAGE >&2
return $1
}
if [ $# -ne 1 ]; then
usage $OCF_ERR_ARGS
fi
case $1 in
meta-data) meta_data;;
start) zpool_import;;
stop) zpool_export;;
status|monitor) zpool_monitor;;
validate-all) zpool_validate;;
usage) usage $OCF_SUCCESS;;
*) usage $OCF_ERR_UNIMPLEMENTED;;
esac
exit $?