Statistics
| Branch: | Tag: | Revision:

root / doc / examples / hooks / ethers @ 87a5035c

History | View | Annotate | Download (3.2 kB)

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

    
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
  [ -f "$DAEMON_PID_FILE" ] && kill -HUP $(< $DAEMON_PID_FILE)
61
}
62

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