Skip to content

Instantly share code, notes, and snippets.

@oakwhiz
Last active August 29, 2015 14:02
Show Gist options
  • Save oakwhiz/742f7fdf84700496f054 to your computer and use it in GitHub Desktop.
Save oakwhiz/742f7fdf84700496f054 to your computer and use it in GitHub Desktop.
opensm initscript (fixed for systemd)
#!/bin/bash
#
# Bring up/down opensm (script fixed for systemd)
#
# chkconfig: - 15 85
# description: Activates/Deactivates InfiniBand Subnet Manager
#
### BEGIN INIT INFO
# Provides: opensm
# Required-Start: $syslog openibd $local_fs
# Required-Stop: $syslog openibd
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Manage OpenSM
### END INIT INFO
#
# Copyright (c) 2008 Voltaire, Inc. All rights reserved.
# Copyright (c) 2006 Mellanox Technologies. All rights reserved.
#
# This Software is licensed under one of the following licenses:
#
# 1) under the terms of the "Common Public License 1.0" a copy of which is
# available from the Open Source Initiative, see
# http://www.opensource.org/licenses/cpl.php.
#
# 2) under the terms of the "The BSD License" a copy of which is
# available from the Open Source Initiative, see
# http://www.opensource.org/licenses/bsd-license.php.
#
# 3) under the terms of the "GNU General Public License (GPL) Version 2" a
# copy of which is available from the Open Source Initiative, see
# http://www.opensource.org/licenses/gpl-license.php.
#
# Licensee has the right to choose one of the above licenses.
#
# Redistributions of source code must retain the above copyright
# notice and one of the license notices.
#
# Redistributions in binary form must reproduce both the above copyright
# notice, one of the license notices in the documentation
# and/or other materials provided with the distribution.
#
#
# $Id: openib-1.0-opensm.init,v 1.5 2006/08/02 18:18:23 dledford Exp $
#
# processname: /usr/sbin/opensm
# config: /etc/sysconfig/opensm
# pidfile: /var/run/opensm.pid
prefix=/usr
exec_prefix=/usr
. /etc/rc.d/init.d/functions
CONFIG=/etc/sysconfig/opensm
if [ -f $CONFIG ]; then
. $CONFIG
fi
prog=/usr/sbin/opensm
bin=${prog##*/}
# Handover daemon for updating guid2lid cache file
sldd_prog=/usr/sbin/sldd.sh
sldd_bin=${sldd_prog##*/}
sldd_pid_file=/var/run/sldd.pid
ACTION=$1
# Setting OpenSM start parameters
PID_FILE=/var/run/${bin}.pid
touch $PID_FILE
if [[ -n "${OSM_HOSTS}" && $(echo -n ${OSM_HOSTS} | wc -w | tr -d '[:space:]') -gt 1 ]]; then
HONORE_GUID2LID="--honor_guid2lid"
fi
#########################################################################
start_sldd()
{
if [ -f $sldd_pid_file ]; then
local line p
read line < $sldd_pid_file
for p in $line ; do
[ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && sldd_pid="$sldd_pid $p"
done
fi
if [ -z "$sldd_pid" ]; then
sldd_pid=`pidof -x $sldd_bin`
fi
if [ -n "${sldd_pid:-}" ] ; then
kill -9 ${sldd_pid} > /dev/null 2>&1
fi
$sldd_prog > /dev/null 2>&1 &
sldd_pid=$!
echo ${sldd_pid} > $sldd_pid_file
# Sleep is needed in order to update local gid2lid cache file before running opensm
sleep 3
}
stop_sldd()
{
if [ -f $sldd_pid_file ]; then
local line p
read line < $sldd_pid_file
for p in $line ; do
[ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && sldd_pid="$sldd_pid $p"
done
fi
if [ -z "$sldd_pid" ]; then
sldd_pid=`pidof -x $sldd_bin`
fi
if [ -n "${sldd_pid:-}" ] ; then
kill -15 ${sldd_pid} > /dev/null 2>&1
fi
}
start()
{
local OSM_PID=
pid=""
if [ -f $PID_FILE ]; then
local line p
read line < $PID_FILE
for p in $line ; do
[ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
done
fi
if [ -z "$pid" ]; then
pid=`pidof -o $$ -o $PPID -o %PPID -x $bin`
fi
if [ -n "${pid:-}" ] ; then
echo $"${bin} (pid $pid) is already running..."
else
if [ -n "${HONORE_GUID2LID}" ]; then
# Run sldd daemod
start_sldd
fi
# Start opensm
echo -n "Starting IB Subnet Manager"
$prog --daemon ${HONORE_GUID2LID} ${OPTIONS} > /dev/null
cnt=0; alive=0
while [ $cnt -lt 6 -a $alive -ne 1 ]; do
echo -n ".";
sleep 1
alive=0
OSM_PID=`pidof $prog`
if [ "$OSM_PID" != "" ]; then
alive=1
fi
let cnt++;
done
echo $OSM_PID > $PID_FILE
checkpid $OSM_PID
RC=$?
[ $RC -eq 0 ] && echo_success || echo_failure
[ $RC -eq 0 ] && touch /var/lock/subsys/opensm
echo
fi
return $RC
}
stop()
{
local pid=
local pid1=
local pid2=
# Stop sldd daemon
stop_sldd
if [ -f $PID_FILE ]; then
local line p
read line < $PID_FILE
for p in $line ; do
[ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid1="$pid1 $p"
done
fi
pid2=`pidof -o $$ -o $PPID -o %PPID -x $bin`
pid=`echo "$pid1 $pid2" | sed -e 's/\ /\n/g' | sort -n | uniq | sed -e 's/\n/\ /g'`
if [ -n "${pid:-}" ] ; then
# Kill opensm
echo -n "Stopping IB Subnet Manager."
kill -15 $pid > /dev/null 2>&1
cnt=0; alive=1
while [ $cnt -lt 6 -a $alive -ne 0 ]; do
echo -n ".";
alive=0
for p in $pid; do
if checkpid $p ; then alive=1; echo -n "-"; fi
done
let cnt++;
sleep $alive
done
for p in $pid
do
while checkpid $p ; do
kill -KILL $p > /dev/null 2>&1
echo -n "+"
sleep 1
done
done
checkpid $pid
RC=$?
[ $RC -eq 0 ] && echo_failure || echo_success
echo
RC=$((! $RC))
else
echo -n "Stopping IB Subnet Manager."
echo_failure
echo
RC=1
fi
# Remove pid file if any.
rm -f $PID_FILE
rm -f /var/lock/subsys/opensm
return $RC
}
status()
{
local pid
# First try "pidof"
pid=`pidof -o $$ -o $PPID -o %PPID -x ${bin}`
if [ -n "$pid" ]; then
echo $"${bin} (pid $pid) is running..."
return 0
fi
# Next try "/var/run/opensm.pid" files
if [ -f $PID_FILE ] ; then
read pid < $PID_FILE
if [ -n "$pid" ]; then
echo $"${bin} dead but pid file $PID_FILE exists"
return 1
fi
fi
echo $"${bin} is stopped"
return 3
}
case $ACTION in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
condrestart)
pid=`pidof -o $$ -o $PPID -o %PPID -x $bin`
if [ -n "$pid" ]; then
stop
sleep 1
start
fi
;;
*)
echo
echo "Usage: `basename $0` {start|stop|restart|status}"
echo
exit 1
;;
esac
RC=$?
exit $RC
@oakwhiz
Copy link
Author

oakwhiz commented Jun 3, 2014

Initially I was getting this error:

OpenSM[559]: Cannot open file '/var/log/opensm.log' for appending: Read-only file system

opensmd was not starting on boot because systemd was starting it before the root filesystem was writable. If I manually started the service after the system was finished booting, it would start successfully. This is because the opensmd init script is broken and systemd doesn't seem to add the default dependency on $local_fs, so it was starting it too soon. To solve the problem, you just have to change Default-Start: from null to the runlevels 2 3 4 5. You should not need to add $local_fs to Required-Start: since systemd should infer it implicitly, but I added it anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment