summaryrefslogtreecommitdiffstats
path: root/heartbeat/vdo-vol
blob: 29bd7b8fd3755e99e34a40d1fc34ac0ea44b8943 (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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/sh
#
#  License:      GNU General Public License (GPL)
#  (c) 2018 O. Albrigtsen
#           and Linux-HA contributors
#
# -----------------------------------------------------------------------------
#      O C F    R E S O U R C E    S C R I P T   S P E C I F I C A T I O N
# -----------------------------------------------------------------------------
#
# NAME
#       vdo-vol : OCF resource agent script for VDO (Virtual Data Optimizer)
#

# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Defaults
OCF_RESKEY_volume_default=""

: ${OCF_RESKEY_volume=${OCF_RESKEY_volume_default}}


vdo_usage() {
  cat <<END
    usage: $0 (start|stop|validate-all|meta-data|help|usage|monitor)
    $0 manages VDO (Virtual Data Optimizer) volume(s) as an OCF HA resource.
    The 'start' operation starts the instance.
    The 'stop' operation stops the instance.
    The 'status' operation reports whether the instance is running
    The 'monitor' operation reports whether the instance seems to be working
    The 'validate-all' operation reports whether the parameters are valid
END
}

vdo_meta_data() {
        cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="vdo-vol" version="0.75">
<version>1.0</version>

<longdesc lang="en">
OCF Resource script for VDO (Virtual Data Optimizer) volume(s). It manages VDO volume(s) as a HA resource.

The configuration file needs to be synced to all nodes, and the systemd vdo service must be disabled when
using this agent.
</longdesc>
<shortdesc lang="en">VDO resource agent</shortdesc>

<parameters>

<parameter name="config">
    <longdesc lang="en">Configuration file</longdesc>
    <shortdesc lang="en">Config file</shortdesc>
    <content type="string" default="${OCF_RESKEY_config_default}" />
</parameter>

<parameter name="volume">
    <longdesc lang="en">VDO Volume (leave empty for all)</longdesc>
    <shortdesc lang="en">Volume (empty for all)</shortdesc>
    <content type="string" default="${OCF_RESKEY_volume_default}" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="60s" />
<action name="stop" timeout="20s" />
<action name="status" timeout="20s" />
<action name="monitor" depth="0" timeout="20s" interval="10s" start-delay="10s" />
<action name="validate-all" timeout="20s" />
<action name="meta-data" timeout="20s" />
</actions>
</resource-agent>
END
}


rebuild() {
		ocf_log warn "${OCF_RESKEY_volume} is in $MODE mode, starting in rebuild mode"

		vdo stop $OPTIONS

		while vdo_monitor skiprocheck; do
			sleep 1
		done

		vdo start $OPTIONS --forceRebuild

		while ! vdo_monitor; do
			sleep 1
		done

		return $?
}

vdo_start() {
	# if resource is already running,no need to continue code after this.
	if vdo_monitor; then
		ocf_log info "VDO volume(s): ${OCF_RESKEY_volume} is already active"
		return $OCF_SUCCESS
	fi

	vdo activate $OPTIONS
	vdo start $OPTIONS

	while ! vdo_monitor skiprocheck; do
		sleep 1
	done

	MODE=$(vdostats --verbose ${OCF_RESKEY_volume} | grep "operating mode" | awk '{print $NF}')
	if [ $(echo "$MODE" | grep -v "normal" | wc -l) -gt 0 ]; then
		rebuild
	fi

	if [ $? -eq $OCF_SUCCESS ]; then
		ocf_log info "VDO volume(s): ${OCF_RESKEY_volume} activated"
		return ${OCF_SUCCESS}
	fi

	return $?
}

vdo_stop() {
	vdo_monitor skiprocheck
	if [ $? -ne $OCF_SUCCESS ]; then
		# Currently not running. Nothing to do.
		ocf_log info "VDO volume(s): ${OCF_RESKEY_volume} already deactivated"

		return $OCF_SUCCESS
	fi

	vdo stop $OPTIONS
	vdo deactivate $OPTIONS

	# Wait for process to stop
	while vdo_monitor skiprocheck; do
		sleep 1
	done

	return $OCF_SUCCESS
}

vdo_monitor(){
	status=$(vdo status $OPTIONS 2>&1)
	MODE=$(vdostats --verbose ${OCF_RESKEY_volume} | grep "operating mode" | awk '{print $NF}')

	case "$status" in
		*"ERROR - vdodumpconfig: Failed to make FileLayer from"*)
			if ocf_is_probe; then
			    return $OCF_NOT_RUNNING
			fi
			return $OCF_ERR_GENERIC
			;;
		*"Device mapper status: not available"*)
			return $OCF_NOT_RUNNING
			;;
		*"Device mapper status: "*online*)
			if [ "$MODE" = "read-only" ] && [ "$1" != "skiprocheck" ]; then
				ocf_log err "VDO volume(s): ${OCF_RESKEY_volume} is in $MODE mode."
				return $OCF_ERR_GENERIC
			else
				return $OCF_SUCCESS
			fi
			;;
		*)
			ocf_log err "VDO volume(s): ${OCF_RESKEY_volume} failed\n$status"
			return $OCF_ERR_GENERIC;;
	esac
}

vdo_validate_all(){
	check_binary "vdo"

	if systemctl is-enabled vdo > /dev/null 2>&1; then
		ocf_exit_reason "systemd service vdo needs to be disabled"
		exit $OCF_ERR_CONFIGURED
	fi

	if [ -n "${OCF_RESKEY_config}" ] && [ ! -f "${OCF_RESKEY_config}" ]; then
		ocf_exit_reason "Configuration file: ${OCF_RESKEY_config} not found"
		exit $OCF_ERR_CONFIGURED
	fi

	return $OCF_SUCCESS
}


# **************************** MAIN SCRIPT ************************************

# Make sure meta-data and usage always succeed
case $__OCF_ACTION in
	meta-data)
		vdo_meta_data
		exit $OCF_SUCCESS
		;;
	usage|help)
		vdo_usage
		exit $OCF_SUCCESS
		;;
esac

# This OCF agent script need to be run as root user.
if ! ocf_is_root; then
        echo  "$0 agent script need to be run as root user."
        ocf_log debug "$0 agent script need to be run as root user."
        exit $OCF_ERR_GENERIC
fi

if [ -z "${OCF_RESKEY_volume}" ]; then
	OPTIONS="-a"
else
	OPTIONS="-n ${OCF_RESKEY_volume}"
fi

if [ -n "${OCF_RESKEY_config}" ]; then
	OPTIONS="$OPTIONS -f ${OCF_RESKEY_config}"
fi

# Translate each action into the appropriate function call
case $__OCF_ACTION in
	start)
		vdo_validate_all
		vdo_start;;
	stop)
		vdo_stop;;
	status|monitor)
		vdo_monitor;;
	validate-all)
		;;
	*)
		vdo_usage
                exit $OCF_ERR_UNIMPLEMENTED;;
esac

exit $?

# End of this script