Revision 93db3d8f

b/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_FILE="/var/run/dnsmasq.pid"
38
LOCKFILE="/var/lock/ganeti_ethers.lock"
39

  
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
45

  
46
acquire_lockfile() {
47
  if ! ( set -o noclobber; echo "$$" > $LOCKFILE) 2> /dev/null; then
48
    logger -s "Cannot acquire lockfile for ethers update"
49
    exit 1
50
  fi
51
  trap "rm -f $LOCKFILE" EXIT
52
}
53

  
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)
58
}
59

  
60
if [ "$hooks_path" = "instance-add" -o \
61
     "$hooks_path" = "instance-modify" -o \
62
     "$hooks_path" = "instance-mirror-replace" ]
63
then
64
  for i in $(seq 0 $((nic_count - 1)) ); do
65
    bridge_var="GANETI_INSTANCE_NIC${i}_BRIDGE"
66
    bridge=${!bridge_var}
67
    if [ -n "$bridge" -a "$bridge" = "$TARGET_BRIDGE" ]; then
68
      mac_var="GANETI_INSTANCE_NIC${i}_MAC"
69
      mac=${!mac_var}
70
      acquire_lockfile
71
      cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/;
72
      END {print \"$mac\t$instance\"}" > /etc/ethers.new
73
      update_ethers_from_new
74
      break
75
    fi
76
  done
77
fi
78
if [ "$hooks_path" = "instance-remove" -o \
79
       \( "$hooks_path" = "instance-modify" -a "$nic_count" -eq 0 \) ]; then
80
  acquire_lockfile
81
  cat /etc/ethers | awk -- "! /^([[:xdigit:]:]*)[[:blank:]]+$instance\>/" \
82
    > /etc/ethers.new
83
  update_ethers_from_new
84
fi
85

  

Also available in: Unified diff