xref: /freebsd/share/examples/hast/ucarp_down.sh (revision 069ac184)
1#!/bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2010 The FreeBSD Foundation
6# All rights reserved.
7#
8# This software was developed by Pawel Jakub Dawidek under sponsorship from
9# the FreeBSD Foundation.
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
14# 1. Redistributions of source code must retain the above copyright
15#    notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the above copyright
17#    notice, this list of conditions and the following disclaimer in the
18#    documentation and/or other materials provided with the distribution.
19#
20# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30# SUCH DAMAGE.
31#
32
33# Resource name as defined in /etc/hast.conf.
34resource="test"
35# Supported file system types: UFS, ZFS
36fstype="UFS"
37# ZFS pool name. Required only when fstype == ZFS.
38pool="test"
39# File system mount point. Required only when fstype == UFS.
40mountpoint="/mnt/test"
41# Name of HAST provider as defined in /etc/hast.conf.
42# Required only when fstype == UFS.
43device="/dev/hast/${resource}"
44
45export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
46
47# KIll UP script if it still runs in the background.
48sig="TERM"
49for i in `jot 30`; do
50	pgid=`pgrep -f ucarp_up.sh | head -1`
51	[ -n "${pgid}" ] || break
52	kill -${sig} -- -${pgid}
53	sig="KILL"
54	sleep 1
55done
56if [ -n "${pgid}" ]; then
57	logger -p local0.error -t hast "UCARP UP process for resource ${resource} is still running after 30 seconds."
58	exit 1
59fi
60logger -p local0.debug -t hast "UCARP UP is not running."
61
62case "${fstype}" in
63UFS)
64	mount | egrep -q "^${device} on "
65	if [ $? -eq 0 ]; then
66		# Forcibly unmount file system.
67		out=`umount -f "${mountpoint}" 2>&1`
68		if [ $? -ne 0 ]; then
69			logger -p local0.error -t hast "Unable to unmount file system for resource ${resource}: ${out}."
70			exit 1
71		fi
72		logger -p local0.debug -t hast "File system for resource ${resource} unmounted."
73	fi
74	;;
75ZFS)
76	zpool list | egrep -q "^${pool} "
77	if [ $? -eq 0 ]; then
78		# Forcibly export file pool.
79		out=`zpool export -f "${pool}" 2>&1`
80		if [ $? -ne 0 ]; then
81			logger -p local0.error -t hast "Unable to export pool for resource ${resource}: ${out}."
82			exit 1
83		fi
84		logger -p local0.debug -t hast "ZFS pool for resource ${resource} exported."
85	fi
86	;;
87esac
88
89# Change role to secondary for our resource.
90out=`hastctl role secondary "${resource}" 2>&1`
91if [ $? -ne 0 ]; then
92	logger -p local0.error -t hast "Unable to change to role to secondary for resource ${resource}: ${out}."
93	exit 1
94fi
95logger -p local0.debug -t hast "Role for resource ${resource} changed to secondary."
96
97logger -p local0.info -t hast "Successfully switched to secondary for resource ${resource}."
98
99exit 0
100