xref: /freebsd/share/examples/hast/ucarp_up.sh (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
1#!/bin/sh
2#
3# Copyright (c) 2010 The FreeBSD Foundation
4# All rights reserved.
5#
6# This software was developed by Pawel Jakub Dawidek under sponsorship from
7# the FreeBSD Foundation.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28# SUCH DAMAGE.
29#
30# $FreeBSD$
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.
41device="/dev/hast/${resource}"
42
43export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
44
45# If there is secondary worker process, it means that remote primary process is
46# still running. We have to wait for it to terminate.
47for i in `jot 30`; do
48	pgrep -f "hastd: ${resource} \(secondary\)" >/dev/null 2>&1 || break
49	sleep 1
50done
51if pgrep -f "hastd: ${resource} \(secondary\)" >/dev/null 2>&1; then
52	logger -p local0.error -t hast "Secondary process for resource ${resource} is still running after 30 seconds."
53	exit 1
54fi
55logger -p local0.debug -t hast "Secondary process in not running."
56
57# Change role to primary for our resource.
58out=`hastctl role primary "${resource}" 2>&1`
59if [ $? -ne 0 ]; then
60	logger -p local0.error -t hast "Unable to change to role to primary for resource ${resource}: ${out}."
61	exit 1
62fi
63# Wait few seconds for provider to appear.
64for i in `jot 50`; do
65	[ -c "${device}" ] && break
66	sleep 0.1
67done
68if [ ! -c "${device}" ]; then
69	logger -p local0.error -t hast "Device ${device} didn't appear."
70	exit 1
71fi
72logger -p local0.debug -t hast "Role for resource ${resource} changed to primary."
73
74case "${fstype}" in
75UFS)
76	# Check the file system.
77	fsck -y -t ufs "${device}" >/dev/null 2>&1
78	if [ $? -ne 0 ]; then
79		logger -p local0.error -t hast "File system check for resource ${resource} failed."
80		exit 1
81	fi
82	logger -p local0.debug -t hast "File system check for resource ${resource} finished."
83	# Mount the file system.
84	out=`mount -t ufs "${device}" "${mountpoint}" 2>&1`
85	if [ $? -ne 0 ]; then
86		logger -p local0.error -t hast "File system mount for resource ${resource} failed: ${out}."
87		exit 1
88	fi
89	logger -p local0.debug -t hast "File system for resource ${resource} mounted."
90	;;
91ZFS)
92	# Import ZFS pool. Do it forcibly as it remembers hostid of
93	# the other cluster node.
94	out=`zpool import -f "${pool}" 2>&1`
95	if [ $? -ne 0 ]; then
96		logger -p local0.error -t hast "ZFS pool import for resource ${resource} failed: ${out}."
97		exit 1
98	fi
99	logger -p local0.debug -t hast "ZFS pool for resource ${resource} imported."
100	;;
101esac
102
103logger -p local0.info -t hast "Successfully switched to primary for resource ${resource}."
104
105exit 0
106