Xen-pcibackend Init-Skript

Aus Neobiker\\\'s Wiki
Zur Navigation springen Zur Suche springen

/etc/init.d/xen-pcibackend

#!/bin/sh
### BEGIN INIT INFO
# Provides:          xen-pciback
# Required-Start:    $remote_fs $syslog xen
# Required-Stop:     $remote_fs $syslog xen
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Bind devices to xen-pciback
# Description:       Bind devices to xen-pciback
### END INIT INFO

#
# Author: neobiker <neobiker@neobiker.de>
#

. /lib/init/vars.sh
. /lib/lsb/init-functions

xen list > /dev/null
if test $? -ne 0
then
        exit 0;
fi

TOOLSTACK=$(/usr/lib/xen-common/bin/xen-toolstack 2>/dev/null)
if [ $? -ne 0 ]; then
        log_warning_msg "No usable Xen toolstack selected"
        exit 0
fi
if [ "$(basename "$TOOLSTACK")" != xm ] && [ "$(basename "$TOOLSTACK")" != xl ]; then
        exit 0
fi

if ! [ -e /proc/xen/privcmd ]; then
        exit 0
fi

DESC="Bind devices to xen-pciback"
NAME=xen-pciback
MODULE=xen_pciback

# Default variables
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

SCRIPTNAME=/etc/init.d/$NAME
CONFIGNAME=/etc/xen/$NAME.conf

ROOT=$(/usr/lib/xen-common/bin/xen-dir 2>/dev/null)
if [ $? -ne 0 ]; then
        log_warning_msg "Not running within Xen or no compatible utils"
        exit 0
fi

#
# Make a device assignable for pci-passthru
#
do_pci_assignable_add()
{
    BDF=$1
    [ -z "$BDF" ] && return 0
    xl pci-assignable-add $BDF
    ret=$?
    if [ $ret -ne 0 ]; then
        log_warning_msg "Xen: couldn't assign PCI ($BDF)"
        return 1
    fi

    log_action_msg "Xen: PCI $BDF assigned to $NAME"
    return 0
}

#
# Remove a device from pci-passthru
#
do_pci_assignable_remove()
{
    BDF=$1
    xl pci-assignable-remove -r $BDF
    if [ $? -ne 0 ]; then
        log_warning_msg "Xen: couldn't remove PCI ($BDF)"
        return 1
    fi
    log_action_msg "Xen: PCI $BDF removed from $NAME"
    return 0
}

#
# Function that starts the daemon/service
#
do_start()
{
        # Exit if the package is not installed
        [ -e "$CONFIGNAME" ] || return 0

        cat $CONFIGNAME 2>/dev/null | awk '{if ($1 !~ /#/) print $0}' | while read PCI ; do
            [ -z "$PCI" ] && continue
            f1=`echo "$PCI" | awk '{print $1}'`
            do_pci_assignable_add $f1
        done || return 2
        return 0
}

#
# Function that stops the daemon/service
#
do_stop()
{
        # Exit if the package is not installed
        [ -e "$CONFIGNAME" ] || return 0

        cat $CONFIGNAME 2>/dev/null |
            awk '{if ($1 !~ /#/) print $0}' |
        while read PCI ; do
            [ -z "$PCI" ] && continue
            BDF=`echo "$PCI" | awk '{print $1}'`
            do_pci_assignable_remove $BDF
        done || return 2
        return 0
}

modprobe -q $MODULE

case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_action_begin_msg "Starting $DESC $NAME"
        do_start
        case "$?" in
                0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                1|2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC $NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop}" >&2
        exit 0
        ;;
esac
exit 0