Skip to content

Instantly share code, notes, and snippets.

@roa
Created July 30, 2013 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roa/6111444 to your computer and use it in GitHub Desktop.
Save roa/6111444 to your computer and use it in GitHub Desktop.
kvm article source
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
use Template;
our $PROGNAME = basename($0);
my $nodeCount;
my $qemuDir = '/etc/libvirt/qemu';
my $networkConf = 'custom.xml';
my $existingNodes = [];
GetOptions( "n=i" => \$nodeCount, );
unless ( defined($nodeCount) ) {
print "usage: $PROGNAME --n <number of machines to clone>\n";
exit(0);
}
# get the highest VM number
sub getCurrentMax {
my $maxNodeNo = 0;
opendir( my $dh, $qemuDir ) or die $!;
while ( my $node = readdir($dh) ) {
next if ( $node !~ m/VM\d+\.xml$/ );
if ( $node =~ /\d+/ ) { $maxNodeNo = $& }
}
close($dh);
return $maxNodeNo;
}
# the mac address for the first machine created
sub getInitialMac {
my $macStr = '00:00:00:00:00:02';
for ( my $i = 0; $i < getCurrentMax(); $i++ ) {
$macStr = incrMac($macStr);
}
return $macStr;
}
# increment the mac address by 1
sub incrMac {
my $macStr = shift;
( my $macHex = $macStr ) =~ s/://g;
my ( $macHi, $macLo ) = unpack( " nN ", pack( 'H*', $macHex ) );
if ( $macLo == 0xFFFFFFFF ) {
$macHi = ( $macHi + 1 ) & 0xFFFF;
$macLo = 0;
}
else {
++$macLo;
}
$macHex = sprintf( "%04X%08X ", $macHi, $macLo );
return join( ':', $macHex =~ /../sg );
}
# write the network.xml file
sub writeVirtNet {
my $nodeCount = shift;
my $macStr = '00:00:00:00:00:02';
my $tt = Template->new();
my $futCount = $nodeCount + getCurrentMax();
my $nodes = [];
for ( my $i = 0; $i < $futCount; $i++ ) {
my $node = {
'ip' => '192.168.122.' . ( $i + 2 ),
'name' => 'proxy' . ( $i + 1 ),
'mac' => $macStr,
};
$macStr = incrMac($macStr);
push( @$nodes, $node );
}
$tt->process( 'network.xml.tt', { 'hostList' => $nodes },
$networkConf, );
}
# delete the old default network and load the new one
sub updateNetwork {
writeVirtNet(shift);
system("virsh net-destroy default");
system("virsh net-undefine default");
system("virsh net-define $networkConf");
system("virsh net-autostart default");
system('/etc/init.d/libvirtd restart');
}
updateNetwork($nodeCount);
my $macStr = getInitialMac();
for ( my $i = 0; $i < $nodeCount; $i++ ) {
my $no = getCurrentMax() + 1;
# clone the VM
system(
"virt-clone --connect=qemu:///system -o Vanilla -n VM$no -f /var/lib/libvirt/images/VM$no.img -m \'$macStr\' "
);
# start the VM
system("virsh --connect=qemu:///system start VM$no");
$macStr = incrMac($macStr);
}
<network>
<name>default</name>
<uuid>ff5e37ba-9646-4f4a-8ec9-e393d0d91d66</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0' />
<mac address='52:54:00:67:08:55'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254' />
[% FOREACH entry = hostList -%]
<host mac='[% entry.mac %]' name='[% entry.name %]' ip='[% entry.ip %]' />
[% END -%]
</dhcp>
</ip>
</network>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment