pf-tools commit: r593 [ccaillet-guest] - in /trunk: debian/changelog lib/PFTools/Bridge.pm lib/PFTools/Net.pm

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Tue Apr 29 12:03:27 UTC 2008


Author: ccaillet-guest
Date: Tue Apr 29 12:03:26 2008
New Revision: 593

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=593
Log:
* Exporting more functions from Net.pm needed by tool umlaunch
* Adding first version of Bridge.pm for managing bridge for uml or xen or
  what is used for virtualization

Added:
    trunk/lib/PFTools/Bridge.pm   (with props)
Modified:
    trunk/debian/changelog
    trunk/lib/PFTools/Net.pm

Modified: trunk/debian/changelog
URL: http://svn.debian.org/wsvn/pf-tools/trunk/debian/changelog?rev=593&op=diff
==============================================================================
--- trunk/debian/changelog (original)
+++ trunk/debian/changelog Tue Apr 29 12:03:26 2008
@@ -47,6 +47,9 @@
     debian-installer is used. Adding automated md5sum on this file
   * Fixing pxetemplate for debian-installer
   * Fixing Makefile for PXE template files install and installers install
+  * Exporting more functions from Net.pm needed by tool umlaunch
+  * Adding first version of Bridge.pm for managing bridge for uml or xen or
+    what is used for virtualization
 
   [ Thomas Parmelan ]
   * lib-net: if no comment is specified in a zone, network or server
@@ -59,7 +62,7 @@
   * Merge the remaining changes from 0.32.47-1 and 0.32.48-1.
   * Bugfix for hosts without shortname definition.
 
- -- Christophe Caillet <quadchris at free.fr>  Thu, 17 Apr 2008 14:03:17 +0200
+ -- Christophe Caillet <quadchris at free.fr>  Tue, 29 Apr 2008 14:00:13 +0200
 
 pf-tools (0.32.48-1) unstable; urgency=low
 

Added: trunk/lib/PFTools/Bridge.pm
URL: http://svn.debian.org/wsvn/pf-tools/trunk/lib/PFTools/Bridge.pm?rev=593&op=file
==============================================================================
--- trunk/lib/PFTools/Bridge.pm (added)
+++ trunk/lib/PFTools/Bridge.pm Tue Apr 29 12:03:26 2008
@@ -1,0 +1,239 @@
+package PFTools::Bridge ;
+##
+##  $Id$
+##
+##  Copyright (C) 2008 Christophe Caillet <quadchris at free.fr>
+##
+##  This program is free software; you can redistribute it and/or
+##  modify it under the terms of the GNU General Public License
+##  as published by the Free Software Foundation; either version 2
+##  of the License, or (at your option) any later version.
+##
+##  This program is distributed in the hope that it will be useful,
+##  but WITHOUT ANY WARRANTY; without even the implied warranty of
+##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+##  GNU General Public License for more details.
+##
+##  You should have received a copy of the GNU General Public License
+##  along with this program; if not, write to the Free Software
+##  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+##
+
+use strict ;
+use warnings ;
+use Data::Dumper ;
+
+BEGIN {
+	if (   `which vconfig 2>/dev/null` eq ""
+		|| `which brctl 2>/dev/null`  eq ""
+		|| `which tunctl 2>/dev/null` eq ""
+		|| `which ip 2>/dev/null` eq "" )
+	{
+		die "Sorry, I need vlan, bridge-utils, uml-utilities and iproute" ;
+	}
+}
+
+use Exporter;
+
+our @ISA = ('Exporter');
+
+our @EXPORT = qw(
+	Get_all_bridges
+	Check_host_bridge
+	
+	Bridge_primary_exist
+	Bridge_exist
+	
+	Mk_primary_bridge
+	Rm_primary_bridge
+	
+	Attach_host_bridge
+	Detach_host_bridge
+	Add_host_bridge
+	Del_host_bridge
+);
+
+our @EXPORT_OK = qw();
+
+
+my $brctl_cmd	= '/usr/sbin/brctl' ;
+my $tunctl_cmd	= '/usr/sbin/tunctl' ;
+my $ip_cmd	= '/sbin/ip' ;
+my $vconf_cmd	= '/sbin/vconfig' ;
+
+my $DEBUG	= 1 ;
+my $ETHTRUNK	= 'eth1' ;
+my $XEN_CFG_DIR = '' ;
+my $XEN_USER	= 'root' ;
+
+sub Sysexec ($) {
+	my ( $cmd ) = @_ ;
+	
+	print "Executing command ".$cmd."\n" ;
+	system ( $cmd ) if ( ! $DEBUG );
+	if ( $? ) {
+		if ( $? == -1 ) {
+			warn "failed to execute: $!\n" ;
+			return 0 ;
+		} elsif ( $? & 127 ) {
+			printf STDERR "child died with signal %d, %s coredump\n", ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without' ;
+			return 0 ;
+		} else {
+			printf STDERR "child exited with value %d\n", $? >> 8 ;
+			return 0 ;
+		}
+	}
+	return 1 ;
+}
+
+sub Get_all_bridges () {
+	my $res = {} ;
+	my ( $cur_br, $brlist ) ;
+	
+	unless ( open ( $brlist, $brctl_cmd.' show |' ) ) {
+		die "Unable top execute command /usr/sbin/brctl show\n" ;
+	}
+	while ( <$brlist> ) {
+		chomp ;
+		if ( /^br([\d]+)/ ) {
+			my ( $brname, $br_id, $stp, $iface ) = split ( /\s+/, $_ ) ;
+			$cur_br = $brname ;
+			push ( @{$res->{$cur_br}->{'iface'}}, $iface ) ;
+		} elsif ( /^bridge/ ) {
+			next ;
+		} else {
+			/^\s*([\S]+)\s*$/ ;
+			push ( @{$res->{$cur_br}->{'iface'}}, $1 ) ;
+		}
+	}
+	close ( $brlist ) ;
+	return $res ;
+}
+
+sub Bridge_primary_exist ($) {
+	my ( $br_name, $br_list ) = @_ ;
+	
+	( defined $br_list->{$br_name} ) ? return 1 : return 0 ;
+}
+
+sub Bridge_exist ($$) {
+	my ( $br_name, $br_list ) = @_ ;
+
+	my ( $hostname, $tag ) = split ( /\./, $br_name ) ;
+	return grep /^$br_name$/, @{$br_list->{'br.'.$tag}->{'iface'}} ;
+}
+
+sub Mk_primary_bridge ($) {
+	my ( $primary ) = @_ ;
+	$primary	=~ /^br([\d]+)$/ ;
+	my $tag		= $1 ;
+
+	my $cmd = $brctl_cmd." addbr ".$primary ;
+	if ( ! Sysexec ( $cmd ) ) {
+		warn "Unable to add primary bridge ".$primary."\n" ;
+		return 0 ;
+	}
+	if ( ! Sysexec ( $ip_cmd." addr show dev ".$ETHTRUNK.".".$tag ) ) {
+		print "Need to add vlan tagged as ".$tag." on iface ".$ETHTRUNK."\n" ;
+		if ( ! Sysexec ( $vconf_cmd." add ".$ETHTRUNK." ".$tag ) ) {
+			warn "Unable to add vlan tagged as ".$tag." on ".$ETHTRUNK."\n" ;
+			return 0 ;
+		}
+	}
+	print "Attaching primary bridge ".$primary." to ".$ETHTRUNK.".".$tag."\n" ;
+	return Sysexec ( $brctl_cmd." addif ".$primary." ".$ETHTRUNK.".".$tag ) ;
+}
+
+sub Rm_primary_bridge ($;$) {
+	my ( $primary, $ref_brlist ) = @_ ;
+	
+	if ( ! defined $ref_brlist ) {
+		print "Need to retrive bridges configuration on host\n" ;
+		$ref_brlist = Get_all_bridges () ;
+		if ( ! defined $ref_brlist ) {
+			warn "Unable to retrieve briges list : unable to remove primary bridge ".$primary."\n" ;
+			return 0 ;
+		}
+	}
+	if ( ! defined $ref_brlist->{$primary} ) {
+		warn "No such primary bridge ".$primary."\n" ;
+		return 0 ;
+	}
+	foreach my $iface ( @{$ref_brlist->{$primary}} ) {
+		if ( ! Detach_host_bridge ( $iface, $primary ) ) {
+			warn "Unable to detach interface ".$iface." from primary bridge ".$primary." before removing it\n" ;
+			return 0 ;
+		}
+	}
+	return Sysexec ( $brctl_cmd." delbr ".$primary ) ;
+}
+
+sub Detach_host_bridge ($$) {
+	my ( $br_name, $primary ) = @_ ;
+	
+	my $cmd = $brctl_cmd." delif ".$primary." ".$br_name ;
+	return Sysexec ( $cmd ) ;
+}
+
+sub Attach_host_bridge ($$) {
+	my ( $br_name, $primary ) = @_ ;
+	
+	my $cmd = $brctl_cmd." addif ".$primary." ".$br_name ;
+	return Sysexec ( $cmd ) ;
+}
+
+sub Add_host_bridge ($$) {
+	my ( $hostname, $tag ) = @_ ;
+	my $primary = "br".$tag ;
+
+	my $cmd = $tunctl_cmd." -b -u ".$XEN_USER." -t ".$hostname.".".$tag ;
+	if ( ! Bridge_primary_exist ( $primary ) ) {
+		print "Need to create primary bridge ".$primary."\n" if ( $DEBUG ) ;
+		if ( ! Mk_primary_bridge ( $primary ) ) {
+			warn "Unable to create primary bridge ".$primary."\n" ;
+			return 0 ;
+		}
+	}
+	if ( ! Sysexec ( $cmd ) ) {
+		warn "Unable to add host bridge ".$hostname.".".$tag."\n" ;
+		return 0 ;
+	}
+	print "Attaching new TUN/TAP ".$hostname.".".$tag." to primary bridge ".$primary."\n" ;
+	return Attach_host_bridge ( $primary, $hostname.".".$tag ) ;
+}
+
+sub Del_host_bridge ($$) {
+	my ( $hostname, $tag ) = @_ ;
+	my $primary = "br".$tag ;
+
+	print "Need to detach ".$hostname.".".$tag." from the primary bridge\n" ;
+	if ( ! Detach_host_bridge ( $hostname.".".$tag, $primary ) ) {
+		warn "Unable to detach ".$hostname.".".$tag." from ".$primary."\n" ;
+		return 0 ;
+	}
+	return Sysexec ( $tunctl_cmd." -b -d ".$hostname.".".$tag ) ;
+}
+
+sub Check_host_bridge ($$$) {
+	my ( $ref_net, $hostname, $ref_brlist ) = @_ ;
+	
+	my $hosttype	= $hostname ;
+	$hosttype	=~ s/[\d]+$// ;
+	
+	print "$hosttype\n" ;
+	if ( ! defined ( $ref_net->{'SERVERS'}->{'BY_NAME'}->{$hosttype}->{'SRVLIST'}->{$hostname} ) ) {
+		die "Hostname ".$hostname." doesn't exist\n" ;
+	}
+	my $HOST = $ref_net->{'SERVERS'}->{'BY_NAME'}->{$hosttype}->{'SRVLIST'}->{$hostname} ;
+	foreach my $vlan ( keys %{$HOST->{'ifup'}} ) {
+		$vlan =~ s/^$hostname\.// ;
+		die "No tag defined for vlan ".$vlan."\n" if ( ! defined ( $ref_net->{'NETWORK'}->{'BY_NAME'}->{$vlan}->{'tag'} ) ) ;
+		my $tag = $ref_net->{'NETWORK'}->{'BY_NAME'}->{$vlan}->{'tag'} ;
+		if ( ! Bridge_exist ( $hostname.'.'.$tag, $ref_brlist ) ) {
+			print "Need to create bridge ".$hostname.".".$tag."\n" ;
+			Add_host_bridge ( $hostname, $tag ) ;
+		} else {
+			print "Bridge ".$hostname.".".$tag." already exists\n" ;
+		}
+	}
+}

Propchange: trunk/lib/PFTools/Bridge.pm
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Tue Apr 29 12:03:26 2008
@@ -1,0 +1,2 @@
+Id
+Revision

Modified: trunk/lib/PFTools/Net.pm
URL: http://svn.debian.org/wsvn/pf-tools/trunk/lib/PFTools/Net.pm?rev=593&op=diff
==============================================================================
--- trunk/lib/PFTools/Net.pm (original)
+++ trunk/lib/PFTools/Net.pm Tue Apr 29 12:03:26 2008
@@ -34,9 +34,14 @@
     Init_lib_net
 
     Get_Active_Filename
+    Get_Ordered_Filtered_Hosts
     Get_dns_from_hostname
     Get_dns_from_zone
     Get_If
+    Get_UM_If
+    Get_Dhcp_Infos
+    Get_Cmdline
+    Get_Initrd_Filename
     Get_Ramdisk_size_from_Initrd
     Resolv
 




More information about the pf-tools-commits mailing list