3 # Copyright (C) 2009 Google Inc.
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.
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.
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
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".
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.
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).
34 # TARGET_BRIDGE: We'll only add the first nic which gets connected to this
35 # bridge to /etc/ethers.
37 DAEMON_PID_FILE="/var/run/dnsmasq.pid"
38 LOCKFILE="/var/lock/ganeti_ethers.lock"
40 hooks_path=$GANETI_HOOKS_PATH
41 [ -n "$hooks_path" ] || exit 1
42 instance=$GANETI_INSTANCE_NAME
43 [ -n "$instance" ] || exit 1
44 nic_count=$GANETI_INSTANCE_NIC_COUNT
47 if ! ( set -o noclobber; echo "$$" > $LOCKFILE) 2> /dev/null; then
48 logger -s "Cannot acquire lockfile for ethers update"
51 trap "rm -f $LOCKFILE" EXIT
54 update_ethers_from_new() {
55 chmod 644 /etc/ethers.new
56 mv /etc/ethers.new /etc/ethers
57 [ -f "$DAEMON_PID_FILE" ] && kill -HUP $(< $DAEMON_PID_FILE)
60 if [ "$hooks_path" = "instance-add" -o \
61 "$hooks_path" = "instance-modify" -o \
62 "$hooks_path" = "instance-mirror-replace" ]
64 for i in $(seq 0 $((nic_count - 1)) ); do
65 bridge_var="GANETI_INSTANCE_NIC${i}_BRIDGE"
67 if [ -n "$bridge" -a "$bridge" = "$TARGET_BRIDGE" ]; then
68 mac_var="GANETI_INSTANCE_NIC${i}_MAC"
71 cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/;
72 END {print \"$mac\t$instance\"}" > /etc/ethers.new
73 update_ethers_from_new
78 if [ "$hooks_path" = "instance-remove" -o \
79 \( "$hooks_path" = "instance-modify" -a "$nic_count" -eq 0 \) ]; then
81 cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/" \
83 update_ethers_from_new