Merge branch 'devel-2.5'
[ganeti-local] / doc / examples / hooks / ethers
1 #!/bin/bash
2
3 # Copyright (C) 2009 Google Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19
20 # This is an example ganeti hook that writes the instance mac addresses in the
21 # node's /etc/ether file. It will pic up the first nic connected to the
22 # TARGET_BRIDGE bridge, and write it down with the syntax "MAC  INSTANCE_NAME".
23
24 # The hook will also send a HUP signal the daemon whose PID is in
25 # DAEMON_PID_FILE, so that it can load the new /etc/ethers file and use it.
26 # This has been tested in conjunction with dnsmasq's dhcp implementation.
27
28 # It will also remove any other occurrences for the same instance in the
29 # aformentioned file. This hook supports the "instance-add", "instance-modify"
30 # "instance-remove", and "instance-mirror-replace" ganeti post hook paths. To
31 # install it add a symlink from those hooks' directories to where this file is
32 # installed (with a mode which permits execution).
33
34 # TARGET_BRIDGE: We'll only add the first nic which gets connected to this
35 # bridge to /etc/ethers.
36 TARGET_BRIDGE="br0"
37 DAEMON_PID_FILES="/var/run/dnsmasq.pid /var/run/dnsmasq/dnsmasq.pid"
38
39 # In order to handle concurrent execution of this lock, we use the $LOCKFILE.
40 # LOCKFILE_CREATE and LOCKFILE_REMOVE are the path names for the lockfile-progs
41 # programs which we use as helpers.
42 LOCKFILE="/var/lock/ganeti_ethers"
43 LOCKFILE_CREATE="/usr/bin/lockfile-create"
44 LOCKFILE_REMOVE="/usr/bin/lockfile-remove"
45
46 hooks_path=$GANETI_HOOKS_PATH
47 [ -n "$hooks_path" ] || exit 1
48 instance=$GANETI_INSTANCE_NAME
49 [ -n "$instance" ] || exit 1
50 nic_count=$GANETI_INSTANCE_NIC_COUNT
51
52 acquire_lockfile() {
53   $LOCKFILE_CREATE $LOCKFILE || exit 1
54   trap "$LOCKFILE_REMOVE $LOCKFILE" EXIT
55 }
56
57 update_ethers_from_new() {
58   chmod 644 /etc/ethers.new
59   mv /etc/ethers.new /etc/ethers
60   for file in $DAEMON_PID_FILES; do
61     [ -f "$file" ] && kill -HUP $(< $file)
62   done
63 }
64
65 if [ "$hooks_path" = "instance-add" -o \
66      "$hooks_path" = "instance-modify" -o \
67      "$hooks_path" = "instance-mirror-replace" ]
68 then
69   for i in $(seq 0 $((nic_count - 1)) ); do
70     bridge_var="GANETI_INSTANCE_NIC${i}_BRIDGE"
71     bridge=${!bridge_var}
72     if [ -n "$bridge" -a "$bridge" = "$TARGET_BRIDGE" ]; then
73       mac_var="GANETI_INSTANCE_NIC${i}_MAC"
74       mac=${!mac_var}
75       acquire_lockfile
76       cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/;
77       END {print \"$mac\t$instance\"}" > /etc/ethers.new
78       update_ethers_from_new
79       break
80     fi
81   done
82 fi
83 if [ "$hooks_path" = "instance-remove" -o \
84        \( "$hooks_path" = "instance-modify" -a "$nic_count" -eq 0 \) ]; then
85   acquire_lockfile
86   cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/" \
87     > /etc/ethers.new
88   update_ethers_from_new
89 fi
90