/[pxelator]/lib/PXElator/kvm.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/PXElator/kvm.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 518 - (show annotations)
Wed Jul 21 19:01:46 2010 UTC (10 years, 7 months ago) by dpavlin
File size: 2252 byte(s)
use DEV as bridge for kvm if specified

1 package kvm;
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 use server;
8
9 use Data::Dump qw/dump/;
10
11 our $memory_size = 512;
12
13 # AC = private, AD = multicast, AE = local
14
15 sub nr { $ENV{nr} || 0 };
16
17 sub port {
18 my ($port,$del) = @_;
19 $port += nr();
20 return join( $del, $server::ip, $port );
21 }
22
23 sub fork_if_active { qw/monitor serial/ }
24
25 use x11;
26
27 sub monitor {
28 x11::xterm( 'monitor ' . nr() => 'nc ' . port(10000,' ') );
29 }
30
31 sub serial {
32 x11::xterm( 'serial ' . nr() => 'nc ' . port(11000,' ') );
33 }
34
35 sub actions { qw/reset activate/ }
36
37 sub reset {
38 my $sock = IO::Socket::INET->new( port(10000,':') );
39 print $sock "system_reset\n";
40 close $sock;
41 }
42
43 sub activate {
44 system "xdotool windowactivate `xdotool search --class kvm`"
45 }
46
47 sub start {
48 my $nr = $ENV{nr} || 0;
49
50 my $mac = sprintf('AC:DE:48:00:00:%02x', $nr);
51 my $mon = port(10000,':');
52 my $ser = port(11000,':');
53 my $ip = client::ip_from_mac $mac;
54
55 my $bridge = client::conf( $ip, 'kvm.bridge', default => 'virtual' );
56 $bridge = $ENV{DEV} if $ENV{DEV};
57
58 my $ifname = 'kvm' . $nr;
59
60 warn "# kvm $ip start $nr $mac $mon $ser $ifname $bridge";
61
62 # system "ifconfig $ifname down";
63 # system "brctl delif virtual $ifname";
64 # system "tunctl -d $ifname";
65
66 my $ifconfig = `/sbin/ifconfig $ifname`;
67
68 # FIXME tunctl -u $server::user
69 system "tunctl -t $ifname"
70 unless $ifconfig =~ m{$ifname};
71 system "ifconfig $ifname up"
72 unless $ifconfig =~ m{UP};
73 system "brctl addif $bridge $ifname"
74 unless `brctl show` =~ m{$ifname};
75
76 my $name = client::conf( $ip, 'hostname' );
77 $name ||= $nr;
78
79 my $kvm_bin = client::conf( $ip => 'kvm.bin' ) || 'kvm';
80 my $boot = client::conf( $ip, 'kvm.boot' ) || 'n'; # network by default
81
82 my $kvm = qq|
83 $kvm_bin
84 -name "$name"
85 -m $memory_size -net nic,macaddr=$mac
86 -net tap,ifname=$ifname,script=no
87 -boot $boot
88 -monitor tcp:$mon,server,nowait
89 -serial tcp:$ser,server,nowait
90 |;
91
92 $kvm .= ' -vnc ' . port(0,':') unless $ENV{DISPLAY};
93
94 # -runas $server::user
95 $kvm =~ s{\s+}{ }gs;
96
97 warn $kvm;
98 exec $kvm;
99
100 }
101
102 sub nr_from_mac {
103 my $mac = shift;
104 my $nr = (split(/:/,$mac,6))[-1];
105 $nr = hex($nr);
106 return $nr;
107 }
108
109 sub next_nr {
110 my @kvms = glob "$server::conf/mac/AC:DE:48:00:00:*";
111 my $nr = nr_from_mac( pop @kvms );
112 return $nr + 1;
113 }
114
115 warn 'loaded';
116
117 1;

  ViewVC Help
Powered by ViewVC 1.1.26