1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-11 05:17:41 +03:00
one/share/hooks/ebtables-kvm
2011-02-25 14:34:44 +01:00

94 lines
3.4 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'rexml/document'
#------------------------------------------------------------------------------
# Configuration Options for the script. Change it to fit your installation
#------------------------------------------------------------------------------
CONF = {
:ebtables => "sudo /sbin/ebtables",
:brctl => "/usr/sbin/brctl",
:virsh => "virsh -c qemu:///system"
}
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Applies a given rule to the ebtables
#------------------------------------------------------------------------------
def activate(rule)
system "#{CONF[:ebtables]} -A #{rule}"
end
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Gets the interfaces attached to a given bridge
#------------------------------------------------------------------------------
def get_interfaces
bridges = Hash.new
brctl_exit =`#{CONF[:brctl]} show`
cur_bridge = ""
brctl_exit.split("\n")[1..-1].each do |l|
l = l.split
if l.length > 1
cur_bridge = l[0]
bridges[cur_bridge] = Array.new
bridges[cur_bridge] << l[3]
else
bridges[cur_bridge] << l[0]
end
end
bridges
end
#------------------------------------------------------------------------------
###############################################################################
# Main
###############################################################################
VM_NAME = ARGV[0]
nets = `#{CONF[:virsh]} dumpxml #{VM_NAME}`
doc = REXML::Document.new(nets).root
interfaces = get_interfaces()
doc.elements.each('/domain/devices/interface') {|net|
tap = net.elements['target'].attributes['dev']
if interfaces.include? tap
iface_mac = net.elements['mac'].attributes['address']
mac = iface_mac.split(':')
mac[-1] = '00'
net_mac = mac.join(':')
in_rule="FORWARD -s ! #{net_mac}/ff:ff:ff:ff:ff:00 -o #{tap} -j DROP"
out_rule="FORWARD -s ! #{iface_mac} -i #{tap} -j DROP"
activate(in_rule)
activate(out_rule)
end
}