132115b10SPawel Jakub Dawidek#!/bin/sh 232115b10SPawel Jakub Dawidek# 3*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause 4f0cfa1b1SPedro F. Giffuni# 532115b10SPawel Jakub Dawidek# Copyright (c) 2010 The FreeBSD Foundation 632115b10SPawel Jakub Dawidek# 732115b10SPawel Jakub Dawidek# This software was developed by Pawel Jakub Dawidek under sponsorship from 832115b10SPawel Jakub Dawidek# the FreeBSD Foundation. 932115b10SPawel Jakub Dawidek# 1032115b10SPawel Jakub Dawidek# Redistribution and use in source and binary forms, with or without 1132115b10SPawel Jakub Dawidek# modification, are permitted provided that the following conditions 1232115b10SPawel Jakub Dawidek# are met: 1332115b10SPawel Jakub Dawidek# 1. Redistributions of source code must retain the above copyright 1432115b10SPawel Jakub Dawidek# notice, this list of conditions and the following disclaimer. 1532115b10SPawel Jakub Dawidek# 2. Redistributions in binary form must reproduce the above copyright 1632115b10SPawel Jakub Dawidek# notice, this list of conditions and the following disclaimer in the 1732115b10SPawel Jakub Dawidek# documentation and/or other materials provided with the distribution. 1832115b10SPawel Jakub Dawidek# 1932115b10SPawel Jakub Dawidek# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 2032115b10SPawel Jakub Dawidek# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2132115b10SPawel Jakub Dawidek# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2232115b10SPawel Jakub Dawidek# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 2332115b10SPawel Jakub Dawidek# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2432115b10SPawel Jakub Dawidek# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2532115b10SPawel Jakub Dawidek# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2632115b10SPawel Jakub Dawidek# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2732115b10SPawel Jakub Dawidek# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2832115b10SPawel Jakub Dawidek# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2932115b10SPawel Jakub Dawidek# SUCH DAMAGE. 3032115b10SPawel Jakub Dawidek# 3132115b10SPawel Jakub Dawidek 3232115b10SPawel Jakub Dawidek# Resource name as defined in /etc/hast.conf. 3332115b10SPawel Jakub Dawidekresource="test" 3432115b10SPawel Jakub Dawidek# Supported file system types: UFS, ZFS 3532115b10SPawel Jakub Dawidekfstype="UFS" 3632115b10SPawel Jakub Dawidek# ZFS pool name. Required only when fstype == ZFS. 3732115b10SPawel Jakub Dawidekpool="test" 3832115b10SPawel Jakub Dawidek# File system mount point. Required only when fstype == UFS. 3932115b10SPawel Jakub Dawidekmountpoint="/mnt/test" 4032115b10SPawel Jakub Dawidek# Name of HAST provider as defined in /etc/hast.conf. 4132115b10SPawel Jakub Dawidekdevice="/dev/hast/${resource}" 4232115b10SPawel Jakub Dawidek 4332115b10SPawel Jakub Dawidekexport PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin 4432115b10SPawel Jakub Dawidek 4532115b10SPawel Jakub Dawidek# If there is secondary worker process, it means that remote primary process is 4632115b10SPawel Jakub Dawidek# still running. We have to wait for it to terminate. 4732115b10SPawel Jakub Dawidekfor i in `jot 30`; do 4832115b10SPawel Jakub Dawidek pgrep -f "hastd: ${resource} \(secondary\)" >/dev/null 2>&1 || break 4932115b10SPawel Jakub Dawidek sleep 1 5032115b10SPawel Jakub Dawidekdone 5132115b10SPawel Jakub Dawidekif pgrep -f "hastd: ${resource} \(secondary\)" >/dev/null 2>&1; then 5232115b10SPawel Jakub Dawidek logger -p local0.error -t hast "Secondary process for resource ${resource} is still running after 30 seconds." 5332115b10SPawel Jakub Dawidek exit 1 5432115b10SPawel Jakub Dawidekfi 5532115b10SPawel Jakub Dawideklogger -p local0.debug -t hast "Secondary process in not running." 5632115b10SPawel Jakub Dawidek 5732115b10SPawel Jakub Dawidek# Change role to primary for our resource. 5832115b10SPawel Jakub Dawidekout=`hastctl role primary "${resource}" 2>&1` 5932115b10SPawel Jakub Dawidekif [ $? -ne 0 ]; then 6032115b10SPawel Jakub Dawidek logger -p local0.error -t hast "Unable to change to role to primary for resource ${resource}: ${out}." 6132115b10SPawel Jakub Dawidek exit 1 6232115b10SPawel Jakub Dawidekfi 6332115b10SPawel Jakub Dawidek# Wait few seconds for provider to appear. 6432115b10SPawel Jakub Dawidekfor i in `jot 50`; do 6532115b10SPawel Jakub Dawidek [ -c "${device}" ] && break 6632115b10SPawel Jakub Dawidek sleep 0.1 6732115b10SPawel Jakub Dawidekdone 6832115b10SPawel Jakub Dawidekif [ ! -c "${device}" ]; then 6932115b10SPawel Jakub Dawidek logger -p local0.error -t hast "Device ${device} didn't appear." 7032115b10SPawel Jakub Dawidek exit 1 7132115b10SPawel Jakub Dawidekfi 7232115b10SPawel Jakub Dawideklogger -p local0.debug -t hast "Role for resource ${resource} changed to primary." 7332115b10SPawel Jakub Dawidek 7432115b10SPawel Jakub Dawidekcase "${fstype}" in 7532115b10SPawel Jakub DawidekUFS) 7632115b10SPawel Jakub Dawidek # Check the file system. 7732115b10SPawel Jakub Dawidek fsck -y -t ufs "${device}" >/dev/null 2>&1 7832115b10SPawel Jakub Dawidek if [ $? -ne 0 ]; then 7932115b10SPawel Jakub Dawidek logger -p local0.error -t hast "File system check for resource ${resource} failed." 8032115b10SPawel Jakub Dawidek exit 1 8132115b10SPawel Jakub Dawidek fi 8232115b10SPawel Jakub Dawidek logger -p local0.debug -t hast "File system check for resource ${resource} finished." 8332115b10SPawel Jakub Dawidek # Mount the file system. 8432115b10SPawel Jakub Dawidek out=`mount -t ufs "${device}" "${mountpoint}" 2>&1` 8532115b10SPawel Jakub Dawidek if [ $? -ne 0 ]; then 8632115b10SPawel Jakub Dawidek logger -p local0.error -t hast "File system mount for resource ${resource} failed: ${out}." 8732115b10SPawel Jakub Dawidek exit 1 8832115b10SPawel Jakub Dawidek fi 8932115b10SPawel Jakub Dawidek logger -p local0.debug -t hast "File system for resource ${resource} mounted." 9032115b10SPawel Jakub Dawidek ;; 9132115b10SPawel Jakub DawidekZFS) 9232115b10SPawel Jakub Dawidek # Import ZFS pool. Do it forcibly as it remembers hostid of 9332115b10SPawel Jakub Dawidek # the other cluster node. 9432115b10SPawel Jakub Dawidek out=`zpool import -f "${pool}" 2>&1` 9532115b10SPawel Jakub Dawidek if [ $? -ne 0 ]; then 9632115b10SPawel Jakub Dawidek logger -p local0.error -t hast "ZFS pool import for resource ${resource} failed: ${out}." 9732115b10SPawel Jakub Dawidek exit 1 9832115b10SPawel Jakub Dawidek fi 9932115b10SPawel Jakub Dawidek logger -p local0.debug -t hast "ZFS pool for resource ${resource} imported." 10032115b10SPawel Jakub Dawidek ;; 10132115b10SPawel Jakub Dawidekesac 10232115b10SPawel Jakub Dawidek 10332115b10SPawel Jakub Dawideklogger -p local0.info -t hast "Successfully switched to primary for resource ${resource}." 10432115b10SPawel Jakub Dawidek 10532115b10SPawel Jakub Dawidekexit 0 106