/[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

Annotation of /lib/PXElator/kvm.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26