12 |
|
|
13 |
# AC = private, AD = multicast, AE = local |
# AC = private, AD = multicast, AE = local |
14 |
|
|
15 |
our $nr = 0; |
sub nr { $ENV{nr} || 0 }; |
16 |
|
|
17 |
sub port { |
sub port { |
18 |
my $del = shift; |
my ($port,$del) = @_; |
19 |
my $port = 10000 + $nr; |
$port += nr(); |
20 |
return join( $del, $server::ip, $port ); |
return join( $del, $server::ip, $port ); |
21 |
} |
} |
22 |
|
|
23 |
sub fork_if_active { qw/monitor/ } |
sub fork_if_active { qw/monitor serial/ } |
24 |
|
|
25 |
use x11; |
use x11; |
26 |
|
|
27 |
sub monitor { |
sub monitor { |
28 |
x11::xterm( "monitor $nr" => 'nc ' . port(' ') ); |
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/ } |
sub actions { qw/reset activate/ } |
36 |
|
|
37 |
sub reset { |
sub reset { |
38 |
my $sock = IO::Socket::INET->new( port(':') ); |
my $sock = IO::Socket::INET->new( port(10000,':') ); |
39 |
print $sock "system_reset\n"; |
print $sock "system_reset\n"; |
40 |
close $sock; |
close $sock; |
41 |
} |
} |
45 |
} |
} |
46 |
|
|
47 |
sub start { |
sub start { |
48 |
my $nr = shift || 0; |
my $nr = $ENV{nr} || 0; |
49 |
|
|
50 |
my $mac = sprintf('AC:DE:48:00:00:%02x', $nr); |
my $mac = sprintf('AC:DE:48:00:00:%02x', $nr); |
51 |
my $mon = port(':'); |
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 $ip = client::ip_from_mac $mac; |
73 |
|
|
74 |
|
my $name = client::conf( $ip, 'hostname' ); |
75 |
|
$name ||= $nr; |
76 |
|
|
77 |
|
my $kvm_bin = client::conf( $ip => 'kvm.bin' ) || 'kvm'; |
78 |
|
my $boot = client::conf( $ip, 'kvm.boot' ) || 'n'; # network by default |
79 |
|
|
80 |
my $kvm = qq| |
my $kvm = qq| |
81 |
vdeq kvm -m $memory_size -net nic,vlan=1,macaddr=$mac |
$kvm_bin |
82 |
-net vde,vlan=1,sock=/var/run/vde2/tap0.ctl |
-name "$name" |
83 |
-boot n |
-m $memory_size -net nic,macaddr=$mac |
84 |
|
-net tap,ifname=$ifname,script=no |
85 |
|
-boot $boot |
86 |
-monitor tcp:$mon,server,nowait |
-monitor tcp:$mon,server,nowait |
87 |
|
-serial tcp:$ser,server,nowait |
88 |
|; |
|; |
89 |
|
|
90 |
|
$kvm .= ' -vnc ' . port(0,':') unless $ENV{DISPLAY}; |
91 |
|
|
92 |
|
# -runas $server::user |
93 |
$kvm =~ s{\s+}{ }gs; |
$kvm =~ s{\s+}{ }gs; |
94 |
|
|
95 |
warn $kvm; |
warn $kvm; |
97 |
|
|
98 |
} |
} |
99 |
|
|
100 |
|
sub nr_from_mac { |
101 |
|
my $mac = shift; |
102 |
|
$mac =~ s<^.+:([0-9a-f]{2})$><hex($1)>e; |
103 |
|
$mac; |
104 |
|
} |
105 |
|
|
106 |
|
sub next_nr { |
107 |
|
my @kvms = glob "$server::conf/mac/AC:DE:48:00:00:*"; |
108 |
|
my $nr = nr_from_mac( pop @kvms ); |
109 |
|
return $nr + 1; |
110 |
|
} |
111 |
|
|
112 |
warn 'loaded'; |
warn 'loaded'; |
113 |
|
|
114 |
1; |
1; |