xref: /freebsd/share/examples/hast/ucarp_down.sh (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1#!/bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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# $FreeBSD$
33
34# Resource name as defined in /etc/hast.conf.
35resource="test"
36# Supported file system types: UFS, ZFS
37fstype="UFS"
38# ZFS pool name. Required only when fstype == ZFS.
39pool="test"
40# File system mount point. Required only when fstype == UFS.
41mountpoint="/mnt/test"
42# Name of HAST provider as defined in /etc/hast.conf.
43# Required only when fstype == UFS.
44device="/dev/hast/${resource}"
45
46export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
47
48# KIll UP script if it still runs in the background.
49sig="TERM"
50for i in `jot 30`; do
51	pgid=`pgrep -f ucarp_up.sh | head -1`
52	[ -n "${pgid}" ] || break
53	kill -${sig} -- -${pgid}
54	sig="KILL"
55	sleep 1
56done
57if [ -n "${pgid}" ]; then
58	logger -p local0.error -t hast "UCARP UP process for resource ${resource} is still running after 30 seconds."
59	exit 1
60fi
61logger -p local0.debug -t hast "UCARP UP is not running."
62
63case "${fstype}" in
64UFS)
65	mount | egrep -q "^${device} on "
66	if [ $? -eq 0 ]; then
67		# Forcibly unmount file system.
68		out=`umount -f "${mountpoint}" 2>&1`
69		if [ $? -ne 0 ]; then
70			logger -p local0.error -t hast "Unable to unmount file system for resource ${resource}: ${out}."
71			exit 1
72		fi
73		logger -p local0.debug -t hast "File system for resource ${resource} unmounted."
74	fi
75	;;
76ZFS)
77	zpool list | egrep -q "^${pool} "
78	if [ $? -eq 0 ]; then
79		# Forcibly export file pool.
80		out=`zpool export -f "${pool}" 2>&1`
81		if [ $? -ne 0 ]; then
82			logger -p local0.error -t hast "Unable to export pool for resource ${resource}: ${out}."
83			exit 1
84		fi
85		logger -p local0.debug -t hast "ZFS pool for resource ${resource} exported."
86	fi
87	;;
88esac
89
90# Change role to secondary for our resource.
91out=`hastctl role secondary "${resource}" 2>&1`
92if [ $? -ne 0 ]; then
93	logger -p local0.error -t hast "Unable to change to role to secondary for resource ${resource}: ${out}."
94	exit 1
95fi
96logger -p local0.debug -t hast "Role for resource ${resource} changed to secondary."
97
98logger -p local0.info -t hast "Successfully switched to secondary for resource ${resource}."
99
100exit 0
101