LogoopenSUSE Build Service > Projects
Sign Up | Log In

View File dhclient-enter-hooks of Package dhcp-client-ep (Project home:ErlangPro)

#!/bin/bash

set_node_hostname() {
    [ -z "$new_host_name" ] && return 0
    
    local host_name=${new_host_name%%.*}
    if [ -z "$new_domain_name" ]; then
        local utsname=$new_host_name
    else
        local utsname=${host_name}.${new_domain_name}
    fi
    
    #Set fqdn hostname in hostsystem, dhclient-script sets only short hostname
    hostname $utsname
    
    #Set hostname in lxc-config
    sed -i "/^lxc.utsname/s/^.*$/lxc.utsname = $utsname/" /var/lib/lxc/ep-{head,worker}/config
    
    #Set hostname to container's /etc/hosts
    [ "$worker_rootfs" ] && [ -w "$worker_rootfs/etc/hosts" ] && echo -e "127.0.0.1\t$utsname $host_name" > "$worker_rootfs/etc/hosts"
    [ "$head_rootfs" ] && [ -w "$head_rootfs/etc/hosts" ] && echo -e "127.0.0.1\t$utsname $host_name" > "$head_rootfs/etc/hosts"
}

set_resolv_conf() {
    #Set resolv.conf in worker-lxc
    if [ "$worker_rootfs" ] && [ -w "$worker_rootfs/etc/resolv.conf" ]; then
        [ "$new_domain_name" ] && echo "search $new_domain_name" >"$worker_rootfs/etc/resolv.conf"
    
        if [ "$new_domain_name_servers" ]; then
            local srv=''; for srv in $new_domain_name_servers; do
                echo "nameserver $srv" >>"$worker_rootfs/etc/resolv.conf"
            done
        fi
    fi
    
    #Set resolv.conf in head-lxc
    if [ "$head_rootfs" ] && [ -w "$head_rootfs/etc/resolv.conf" ]; then
        [ "$new_domain_name" ] && echo "search $new_domain_name" >"$head_rootfs/etc/resolv.conf"
        echo "nameserver 127.0.0.1" >>"$head_rootfs/etc/resolv.conf"
    fi
}

check_nodetype() {
    #Check ErlangPro node type
    if grep -Eq '\<ep-role=head\>' /proc/cmdline; then
        echo head
    else
        echo worker
    fi
}

ep_configure() {
    if [ `check_nodetype` = worker ]; then
        if env | grep -q ^new_erlang_pro; then
            #Write all dhcp options to file
            env | sort -u | sed -n -e '/^new_/s/^new_//gp' > /etc/erlangpro/global/dhcpopts
            exit_status=0
        else
            #Specific configuration is not found, fail
            #Time to wait in dhcp-request loop
            sleep 10
            exit_status=1
            return 1
        fi
    else
            ### HEAD ###
            #Add local dns server
            echo "nameserver 127.0.0.1" >>/etc/resolv.conf
            exit_status=0
    fi
    
    set_node_hostname
    set_resolv_conf
    return 0
}

case "$reason" in
BOUND|RENEW|REBIND|REBOOT)
    if [ -f "/var/lib/lxc/ep-worker/config" ] || [ -f "/var/lib/lxc/ep-head/config" ]; then
        worker_rootfs=`sed -nre '/^lxc.rootfs/s/.*=\s*(.*)$/\1/p' /var/lib/lxc/ep-worker/config`
        head_rootfs=`sed -nre '/^lxc.rootfs/s/.*=\s*(.*)$/\1/p' /var/lib/lxc/ep-head/config`
    
        ep_configure
    fi
    ;;
esac