Epics@GSI Webhome

Tips Linux Dhclient Dynamic Mount Point

Introduction

Using dhclient-script's exit hooks scripts allows to set a mount point depending on the network environment, in this example for the HADES setup.

Based on the articles: and its own origin

dhclient and its scripting capabilities on enter and exit hooks allows to react on dynamic network environments.

Provided you have root privileges this can be achieved by creating a script file mount_var_diskless created in /etc/dhcp/dhclient-exit-hooks.d/ (on Debian), which is sourced by dhclient after having updated/received its DHCP informations.

#!/bin/sh
    # Filename:     /etc/dhcp/dhclient-exit-hooks.d/mount_var_diskless
    # Purpose:      Used by dhclient-script to set mount HADES's var/diskless filesystem 
    #               from either the local net (preferred) or the GSI network
    #
    
    # Do not update hostname for virtual machine IP assignments
    if [ "$interface" != "eth0" ] && [ "$interface" != "eth1" ] && [ "$interface" != "wlan0" ]
    then
        return
    fi
    
    if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] && [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]
    then
            return
    fi
    
    var_diskless_hades=192.168.100.50
    var_diskless_gsi=140.181.75.158 
    var_diskless_path=/var/diskless/dreamplug
    var_diskless_mount=/var/diskless/dreamplug
    
    echo dhclient-exit-hooks.d/mount_var_diskless: new domain-name-servers: $new_domain_name_servers 
    
    echo "$new_domain_name_servers" | grep -q $var_diskless_hades && var_diskless_addr=$var_diskless_hades || var_diskless_addr=$var_diskless_gsi
    
    #clean up, but only different mounts
    grep -v "$var_diskless_addr:$var_diskless_path" /etc/mtab | grep -q "$var_diskless_mount" && umount $var_diskless_mount 
    
    #mount
    [ -d "$var_diskless_mount" ] || mkdir -p $var_diskless_mount
    grep "$var_diskless_addr:$var_diskless_path" /etc/mtab | grep -q "$var_diskless_mount" || mount $var_diskless_addr:$var_diskless_path $var_diskless_mount
    
    echo dhclient-exit-hooks.d/mount_var_diskless: mounted $var_diskless_addr:$var_diskless_path
    
    unset var_diskless_hades 
    unset var_diskless_gsi 
    unset var_diskless_path 
    unset var_diskless_mount
    
Finally make sure the file is readable:
chmod a+r /etc/dhcp/dhclient-exit-hooks.d/mount_var_diskless

Local testing at the prompt:
dhclient -v


-- PeterZumbruch - 21 Oct 2013
-- PeterZumbruch - 2020-11-09
Topic revision: r4 - 2020-11-09, PeterZumbruch
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding GSI Wiki? Send feedback | Legal notice | Privacy Policy (german)