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