dhcrelay 在 CentOS 6.2 上作为 DHCP 和 DHCPv6 中继代理运行

dhcrelay 在 CentOS 6.2 上作为 DHCP 和 DHCPv6 中继代理运行

我正在尝试设置一个 DHCP 中继代理,它将中继 IPv4 和 IPv6 的 DHCP 请求。我使用的是 CentOS 6.2,并且使用的是dhcrelayISC DHCP 实现。我想将其设置为服务,但手册页指出dhcrelay

-6   Run dhcrelay as a DHCPv6 relay agent.  Incompatible with the -4 option.

-4   Run  dhcrelay as a DHCPv4/BOOTP relay agent.  This is the default mode of operation, so the argu-
     ment is not necessary, but may be specified for clarity.  Incompatible with -6.

-6和选项似乎-4不兼容。我如何才能让它同时适用于这两种协议,而无需为这两种情况推出自己的服务包装器?

这是服务脚本(/etc/init.d/dhcrelay):

#!/bin/sh
#
### BEGIN INIT INFO
# Provides: dhcrelay
# Default-Start:
# Default-Stop:
# Should-Start:
# Required-Start: $network
# Required-Stop:
# Short-Description: Start and stop the DHCP relay server
# Description: dhcrelay provides the Dynamic Host Configuration Protocol (DHCP)
#              relay server.  This is required when your DHCP server is on
#              another network segment from the clients.
### END INIT INFO
#
# The fields below are left around for legacy tools (will remove later).
#
# chkconfig: - 65 35
# description: dhcrelay provides a relay for Dynamic Host Control Protocol.
# processname: dhcrelay
# # pidfile: /var/run/dhcrelay.pid

. /etc/rc.d/init.d/functions

RETVAL=0

prog=dhcrelay
exec=/usr/sbin/dhcrelay
lockfile=/var/lock/subsys/dhcrelay
pidfile=/var/run/dhcrelay.pid
config=/etc/sysconfig/dhcrelay

# The dhcrelay daemon uses the sysconfig file for configuration information.
# There is no native configuration file for this program and you must specify
# its settings on the command line.
[ -f /etc/sysconfig/dhcrelay ] && . /etc/sysconfig/dhcrelay

configtest() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    [ -z "$DHCPSERVERS" ] && exit 6
    return 0
}

rh_status() {
    status $exec
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

start() {
    [ `id -u` -eq 0 ] || exit 4
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6

    rh_status_q && return 0

    echo -n $"Starting $prog: "
    daemon $exec $DHCRELAYARGS $([ -n "$INTERFACES" ] && for int in $INTERFACES ; do echo -n " -i $int" ; done) $DHCPSERVERS 2>/dev/null
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch $lockfile
    return $RETVAL
}

stop() {
    [ `id -u` -eq 0 ] || exit 4

    rh_status_q || return 0

    echo -n $"Shutting down $prog: "
    killproc $prog
    RETVAL=$?

    echo
    [ $RETVAL -eq 0 ] && rm -f $lockfile
    return $RETVAL
}

usage() {
    echo $"Usage: $0 {start|stop|restart|force-reload|condrestart|try-restart|configtest|status}"
}

if [ $# -gt 1 ]; then
    exit 2
fi

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart|force-reload)
        stop ; start
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        stop ; start
        ;;
    reload)
        usage
        # unimplemented feature
        exit 3
        ;;
    configtest)
        configtest
        ;;
    status)
        rh_status
        ;;
    *)
        usage
        exit 2
        ;;
esac

exit $?

还有etc/sysconfig/dhcrelay

DHCRELAYARGS="-4 153.5.240.2"
# DHCPv4 only
INTERFACES=""
# DHCPv4 only
DHCPSERVERS=""

答案1

您必须为额外的 IPv6 中继制作一个新的初始化脚本。但这并不难。

复制/etc/init.d/dhcrelay/etc/init.d/dhcrelay6

編輯/etc/init.d/dhcrelay6

#!/bin/sh
#
### BEGIN INIT INFO
# Provides: dhcrelay6
# Default-Start:
# Default-Stop:
# Should-Start:
# Required-Start: $network
# Required-Stop:
# Short-Description: Start and stop the DHCP relay server
# Description: dhcrelay provides the Dynamic Host Configuration Protocol (DHCP)
#              relay server.  This is required when your DHCP server is on
#              another network segment from the clients.
### END INIT INFO
#
# The fields below are left around for legacy tools (will remove later).
#
# chkconfig: - 65 35
# description: dhcrelay provides a relay for Dynamic Host Control Protocol for IPv6.
# processname: dhcrelay6
# # pidfile: /var/run/dhcrelay6.pid

. /etc/rc.d/init.d/functions

RETVAL=0

prog=dhcrelay6
exec=/usr/sbin/dhcrelay6
lockfile=/var/lock/subsys/dhcrelay6
pidfile=/var/run/dhcrelay6.pid
config=/etc/sysconfig/dhcrelay6

# The dhcrelay daemon uses the sysconfig file for configuration information.
# There is no native configuration file for this program and you must specify
# its settings on the command line.
[ -f /etc/sysconfig/dhcrelay6 ] && . /etc/sysconfig/dhcrelay6

configtest() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    [ -z "$DHCPSERVERS" ] && exit 6
    return 0
}

rh_status() {
    status $exec
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

start() {
    [ `id -u` -eq 0 ] || exit 4
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6

    rh_status_q && return 0

    echo -n $"Starting $prog: "
    daemon $exec -6 $DHCRELAYARGS $([ -n "$LOWER" ] && for int in $LOWER; do echo -n " -l $int" ; done) $([ -n "$UPPER " ] && for int in $UPPER ; do echo -n " -u $int" ; done) 2>/dev/null
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch $lockfile
    return $RETVAL
}

stop() {
    [ `id -u` -eq 0 ] || exit 4

    rh_status_q || return 0

    echo -n $"Shutting down $prog: "
    killproc $prog
    RETVAL=$?

    echo
    [ $RETVAL -eq 0 ] && rm -f $lockfile
    return $RETVAL
}

usage() {
    echo $"Usage: $0 {start|stop|restart|force-reload|condrestart|try-restart|configtest|status}"
}

if [ $# -gt 1 ]; then
    exit 2
fi

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart|force-reload)
        stop ; start
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        stop ; start
        ;;
    reload)
        usage
        # unimplemented feature
        exit 3
        ;;
    configtest)
        configtest
        ;;
    status)
        rh_status
        ;;
    *)
        usage
        exit 2
        ;;
esac

exit $?

复制/etc/sysconfig/dhcrelay/etc/sysconfig/dhcrelay

編輯/etc/sysconfig/dhcrelay

DHCRELAYARGS=""
# Downstream interfaces (Clients)
LOWER=""
# Upstream interfaces (Servers)
UPPER=""

执行

ln -s /usr/sbin/dhcrelay /usr/sbin/dhcrelay6

这是必要的,因为该服务可以独立于 IPv4 中继停止,因为它不使用 PID 文件并且由基本名称停止。

由于我没有安装 CentOS,因此没有测试过,但它应该可以工作。如果不行,请联系我,我们会解决问题。

答案2

您不需要进行二进制链接。您说“它不使用 PID 文件”——那么为什么pid在脚本中指定该文件init呢?

事实上,守护进程为 ipv4 模式dhcprelay生成一个文件,为 ipv6 模式生成一个不同的文件,因此您只需在每个脚本中指定正确的文件,启动和停止守护进程的每个实例(ipv4 和 ipv6)就可以正常工作。pidpidpidinit

相关内容