1204076Spjd#!/bin/sh
2204076Spjd#
3204076Spjd# Copyright (c) 2010 The FreeBSD Foundation
4204076Spjd# All rights reserved.
5204076Spjd#
6204076Spjd# This software was developed by Pawel Jakub Dawidek under sponsorship from
7204076Spjd# the FreeBSD Foundation.
8204076Spjd#
9204076Spjd# Redistribution and use in source and binary forms, with or without
10204076Spjd# modification, are permitted provided that the following conditions
11204076Spjd# are met:
12204076Spjd# 1. Redistributions of source code must retain the above copyright
13204076Spjd#    notice, this list of conditions and the following disclaimer.
14204076Spjd# 2. Redistributions in binary form must reproduce the above copyright
15204076Spjd#    notice, this list of conditions and the following disclaimer in the
16204076Spjd#    documentation and/or other materials provided with the distribution.
17204076Spjd#
18204076Spjd# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19204076Spjd# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20204076Spjd# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21204076Spjd# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22204076Spjd# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23204076Spjd# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24204076Spjd# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25204076Spjd# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26204076Spjd# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27204076Spjd# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28204076Spjd# SUCH DAMAGE.
29204076Spjd#
30204076Spjd# $FreeBSD$
31204076Spjd
32204076Spjd# Resource name as defined in /etc/hast.conf.
33204076Spjdresource="test"
34204076Spjd# Supported file system types: UFS, ZFS
35204076Spjdfstype="UFS"
36204076Spjd# ZFS pool name. Required only when fstype == ZFS.
37204076Spjdpool="test"
38204076Spjd# File system mount point. Required only when fstype == UFS.
39204076Spjdmountpoint="/mnt/test"
40204076Spjd# Name of HAST provider as defined in /etc/hast.conf.
41204076Spjddevice="/dev/hast/${resource}"
42204076Spjd
43204076Spjdexport PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
44204076Spjd
45204076Spjd# If there is secondary worker process, it means that remote primary process is
46204076Spjd# still running. We have to wait for it to terminate.
47204076Spjdfor i in `jot 30`; do
48204076Spjd	pgrep -f "hastd: ${resource} \(secondary\)" >/dev/null 2>&1 || break
49204076Spjd	sleep 1
50204076Spjddone
51204076Spjdif pgrep -f "hastd: ${resource} \(secondary\)" >/dev/null 2>&1; then
52204076Spjd	logger -p local0.error -t hast "Secondary process for resource ${resource} is still running after 30 seconds."
53204076Spjd	exit 1
54204076Spjdfi
55204076Spjdlogger -p local0.debug -t hast "Secondary process in not running."
56204076Spjd
57204076Spjd# Change role to primary for our resource.
58204076Spjdout=`hastctl role primary "${resource}" 2>&1`
59204076Spjdif [ $? -ne 0 ]; then
60204076Spjd	logger -p local0.error -t hast "Unable to change to role to primary for resource ${resource}: ${out}."
61204076Spjd	exit 1
62204076Spjdfi
63204076Spjd# Wait few seconds for provider to appear.
64204076Spjdfor i in `jot 50`; do
65204076Spjd	[ -c "${device}" ] && break
66204076Spjd	sleep 0.1
67204076Spjddone
68204076Spjdif [ ! -c "${device}" ]; then
69204076Spjd	logger -p local0.error -t hast "Device ${device} didn't appear."
70204076Spjd	exit 1
71204076Spjdfi
72204076Spjdlogger -p local0.debug -t hast "Role for resource ${resource} changed to primary."
73204076Spjd
74204076Spjdcase "${fstype}" in
75204076SpjdUFS)
76204076Spjd	# Check the file system.
77204076Spjd	fsck -y -t ufs "${device}" >/dev/null 2>&1
78204076Spjd	if [ $? -ne 0 ]; then
79204076Spjd		logger -p local0.error -t hast "File system check for resource ${resource} failed."
80204076Spjd		exit 1
81204076Spjd	fi
82204076Spjd	logger -p local0.debug -t hast "File system check for resource ${resource} finished."
83204076Spjd	# Mount the file system.
84204076Spjd	out=`mount -t ufs "${device}" "${mountpoint}" 2>&1`
85204076Spjd	if [ $? -ne 0 ]; then
86204076Spjd		logger -p local0.error -t hast "File system mount for resource ${resource} failed: ${out}."
87204076Spjd		exit 1
88204076Spjd	fi
89204076Spjd	logger -p local0.debug -t hast "File system for resource ${resource} mounted."
90204076Spjd	;;
91204076SpjdZFS)
92204076Spjd	# Import ZFS pool. Do it forcibly as it remembers hostid of
93204076Spjd	# the other cluster node.
94204076Spjd	out=`zpool import -f "${pool}" 2>&1`
95204076Spjd	if [ $? -ne 0 ]; then
96204076Spjd		logger -p local0.error -t hast "ZFS pool import for resource ${resource} failed: ${out}."
97204076Spjd		exit 1
98204076Spjd	fi
99204076Spjd	logger -p local0.debug -t hast "ZFS pool for resource ${resource} imported."
100204076Spjd	;;
101204076Spjdesac
102204076Spjd
103204076Spjdlogger -p local0.info -t hast "Successfully switched to primary for resource ${resource}."
104204076Spjd
105204076Spjdexit 0
106