/[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 429 - (show annotations)
Sun Sep 13 15:51:03 2009 UTC (14 years, 7 months ago) by dpavlin
File size: 1961 byte(s)
use -vnc $server::ip:$nr as display in no $ENV{DISPLAY} is defined
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
54 my $ifname = 'kvm' . $nr;
55
56 warn "# kvm start $nr $mac $mon $ser $ifname";
57
58 # system "ifconfig $ifname down";
59 # system "brctl delif virtual $ifname";
60 # system "tunctl -d $ifname";
61
62 my $ifconfig = `/sbin/ifconfig $ifname`;
63
64 # FIXME tunctl -u $server::user
65 system "tunctl -t $ifname"
66 unless $ifconfig =~ m{$ifname};
67 system "ifconfig $ifname up"
68 unless $ifconfig =~ m{UP};
69 system "brctl addif virtual $ifname"
70 unless `brctl show` =~ m{$ifname};
71
72 my $name = client::conf( client::ip_from_mac $mac, 'hostname' );
73 $name ||= $nr;
74
75 my $kvm = qq|
76 kvm
77 -name "$name"
78 -m $memory_size -net nic,macaddr=$mac
79 -net tap,ifname=$ifname,script=no
80 -boot n
81 -monitor tcp:$mon,server,nowait
82 -serial tcp:$ser,server,nowait
83 |;
84
85 $kvm .= ' -vnc ' . port(0,':') unless $ENV{DISPLAY};
86
87 # -runas $server::user
88 $kvm =~ s{\s+}{ }gs;
89
90 warn $kvm;
91 exec $kvm;
92
93 }
94
95 sub nr_from_mac {
96 my $mac = shift;
97 $mac =~ s{^.+:([0-9a-f]{2})$}{hex($1)}e;
98 $mac;
99 }
100
101 sub next_nr {
102 my @kvms = glob "$server::conf/mac/AC:DE:48:00:00:*";
103 my $nr = nr_from_mac( pop @kvms );
104 return $nr + 1;
105 }
106
107 warn 'loaded';
108
109 1;

  ViewVC Help
Powered by ViewVC 1.1.26