xref: /freebsd/share/examples/hast/ucarp_up.sh (revision a0b9e2e854027e6ff61fb075a1309dbc71c42b54)
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.
43device="/dev/hast/${resource}"
44
45export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
46
47# If there is secondary worker process, it means that remote primary process is
48# still running. We have to wait for it to terminate.
49for i in `jot 30`; do
50	pgrep -f "hastd: ${resource} \(secondary\)" >/dev/null 2>&1 || break
51	sleep 1
52done
53if pgrep -f "hastd: ${resource} \(secondary\)" >/dev/null 2>&1; then
54	logger -p local0.error -t hast "Secondary process for resource ${resource} is still running after 30 seconds."
55	exit 1
56fi
57logger -p local0.debug -t hast "Secondary process in not running."
58
59# Change role to primary for our resource.
60out=`hastctl role primary "${resource}" 2>&1`
61if [ $? -ne 0 ]; then
62	logger -p local0.error -t hast "Unable to change to role to primary for resource ${resource}: ${out}."
63	exit 1
64fi
65# Wait few seconds for provider to appear.
66for i in `jot 50`; do
67	[ -c "${device}" ] && break
68	sleep 0.1
69done
70if [ ! -c "${device}" ]; then
71	logger -p local0.error -t hast "Device ${device} didn't appear."
72	exit 1
73fi
74logger -p local0.debug -t hast "Role for resource ${resource} changed to primary."
75
76case "${fstype}" in
77UFS)
78	# Check the file system.
79	fsck -y -t ufs "${device}" >/dev/null 2>&1
80	if [ $? -ne 0 ]; then
81		logger -p local0.error -t hast "File system check for resource ${resource} failed."
82		exit 1
83	fi
84	logger -p local0.debug -t hast "File system check for resource ${resource} finished."
85	# Mount the file system.
86	out=`mount -t ufs "${device}" "${mountpoint}" 2>&1`
87	if [ $? -ne 0 ]; then
88		logger -p local0.error -t hast "File system mount for resource ${resource} failed: ${out}."
89		exit 1
90	fi
91	logger -p local0.debug -t hast "File system for resource ${resource} mounted."
92	;;
93ZFS)
94	# Import ZFS pool. Do it forcibly as it remembers hostid of
95	# the other cluster node.
96	out=`zpool import -f "${pool}" 2>&1`
97	if [ $? -ne 0 ]; then
98		logger -p local0.error -t hast "ZFS pool import for resource ${resource} failed: ${out}."
99		exit 1
100	fi
101	logger -p local0.debug -t hast "ZFS pool for resource ${resource} imported."
102	;;
103esac
104
105logger -p local0.info -t hast "Successfully switched to primary for resource ${resource}."
106
107exit 0
108