1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
#!/usr/bin/perl
use warnings;
use strict;
use XML::Parser;
use Getopt::Std;
# args
my %opts;
getopts('dm', \%opts);
my $name = shift;
my $debug = defined($opts{'d'}) ? 1 : 0;
my $showmon = defined($opts{'m'}) ? 1 : 0;
# config
my $tapup = "/etc/qemu-virbr0-ifup";
my $tapdown = "/etc/qemu-virbr0-ifdown";
my $mondir = $ENV{HOME} . "/.qemu-gtk";
my $monitor = $mondir . "/" . $name;
# vars
my ($xml, $help, $parser, @cmdline, $pid);
my $level = 0;
my $indent = 3;
my $skip_qemu = 0;
my @xml_path;
my $xml_string;
# domain config
my %xml_elems;
my (@disks,@nics);
my $ndisk = 0;
my $nnic = 0;
my $usb = 0;
my $usbtablet = 0;
my $graphics = 0;
my $soundhw;
########################################################################
# map tables
my %ide_table = (
'hda' => 'if=ide,bus=0,unit=0',
'hdb' => 'if=ide,bus=0,unit=1',
'hdc' => 'if=ide,bus=1,unit=0',
'hdd' => 'if=ide,bus=1,unit=1'
);
########################################################################
# helper functions
sub xml_start {
my $expat = shift;
my $element = shift;
my $path;
my %attrs;
while (defined($_[0])) { $attrs{$_[0]} = $_[1]; shift; shift; }
push @xml_path, $element;
printf("start: %*s%-*s | %s\n",
$level * $indent, "",
20 - $level * $indent, $element,
join(", ", map { "$_=$attrs{$_}" } sort keys %attrs))
if $debug;
$path = join("-", @xml_path);
# disks
if ($path eq "domain-devices-disk" ||
$path eq "domain-devices-disk-source" ||
$path eq "domain-devices-disk-target") {
for my $elem ('device', 'bus', 'dev', 'file') {
next unless defined($attrs{$elem});
$disks[$ndisk]{"$element-$elem"} = $attrs{$elem};
}
}
# nics
if ($path eq "domain-devices-interface-mac" ||
$path eq "domain-devices-interface-model") {
for my $elem ('address', 'type') {
next unless defined($attrs{$elem});
$nics[$nnic]{"$element-$elem"} = $attrs{$elem};
}
}
# input
if ($element eq "input" && $attrs{'type'} eq "tablet") {
$usb = 1;
$usbtablet = 1;
}
# sound
if ($element eq "sound" && defined($attrs{'model'})) {
$soundhw = $attrs{'model'};
}
# graphics
if ($element eq "graphics") {
$graphics = 1;
}
undef $xml_string;
$level++;
}
sub xml_char {
my $expat = shift;
my $string = shift;
return unless $string =~ m/\S/;
$xml_string = $string;
}
sub xml_end {
my $expat = shift;
my $element = shift;
my $path;
$level--;
if (defined($xml_string)) {
printf("/end : %*s%-*s | %s\n",
$level * $indent, "",
20 - $level * $indent, $element,
$xml_string)
if $debug;
$xml_elems{$element} = $xml_string;
}
$path = join("-", @xml_path);
if ($path eq "domain-devices-disk") {
$ndisk++;
}
if ($path eq "domain-devices-interface") {
$nics[$nnic]{'vlan'} = $nnic;
$nnic++;
}
pop @xml_path;
}
########################################################################
# main
# read config
open XML, "-|", "virsh dumpxml $name";
{ local $/; undef $/; $xml = <XML>; };
close XML;
# set defaults
$xml_elems{'emulator'} = "qemu";
# parse config
$parser = new XML::Parser();
$parser->setHandlers(Start => \&xml_start,
Char => \&xml_char,
End => \&xml_end);
$parser->parse($xml) or die "domain config parse error";
# get help text
open HELP, "-|", "$xml_elems{'emulator'} --help";
{ local $/; undef $/; $help = <HELP>; };
close HELP;
# build cmdline -- basic setup
print "-- \n" if $debug;
push @cmdline, $xml_elems{'emulator'};
if (defined($xml_elems{'name'}) && $help =~ m/-name/) {
push @cmdline, "-name";
push @cmdline, $xml_elems{'name'};
}
if (defined($xml_elems{'uuid'}) && $help =~ m/-uuid/) {
push @cmdline, "-uuid";
push @cmdline, $xml_elems{'uuid'};
}
if (defined($xml_elems{'memory'})) {
push @cmdline, "-m";
push @cmdline, $xml_elems{'memory'} / 1024;
}
if (defined($xml_elems{'vcpu'})) {
push @cmdline, "-smp";
push @cmdline, $xml_elems{'vcpu'};
}
if ($usb) {
push @cmdline, "-usb";
}
if ($usbtablet) {
push @cmdline, "-usbdevice";
push @cmdline, "tablet";
}
if (defined($soundhw)) {
push @cmdline, "-soundhw";
push @cmdline, $soundhw;
}
# build cmdline -- disks
foreach my $disk (@disks) {
print "disk: " . join (", ", map {
$_ . "=" . $disk->{$_};
} keys %{$disk}) . "\n"
if $debug;
$disk->{'config'} = "media=" . $disk->{'disk-device'};
$disk->{'config'} .= ",file=" . $disk->{'source-dev'}
if defined($disk->{'source-dev'});
$disk->{'config'} .= ",file=" . $disk->{'source-file'}
if defined($disk->{'source-file'});
if ($disk->{'target-bus'} eq "ide") {
# handle ide
$disk->{'config'} .= "," . $ide_table{$disk->{'target-dev'}};
} else {
# default
$disk->{'config'} .= ",if=" . $disk->{'target-bus'};
}
push @cmdline, "-drive";
push @cmdline, $disk->{'config'};
}
# build cmdline -- nics
foreach my $nic (@nics) {
print "nic: " . join (", ", map {
$_ . "=" . $nic->{$_};
} keys %{$nic}) . "\n"
if $debug;
$nic->{'netconfig'} = "tap,vlan=" . $nic->{'vlan'};
$nic->{'nicconfig'} = "nic,vlan=" . $nic->{'vlan'};
$nic->{'netconfig'} .= ",script=" . $tapup;
$nic->{'netconfig'} .= ",downscript=" . $tapdown;
$nic->{'nicconfig'} .= ",macaddr=" . $nic->{'mac-address'}
if defined($nic->{'mac-address'});
$nic->{'nicconfig'} .= ",model=" . $nic->{'model-type'}
if defined($nic->{'model-type'});
push @cmdline, "-net";
push @cmdline, $nic->{'nicconfig'};
push @cmdline, "-net";
push @cmdline, $nic->{'netconfig'};
}
# build cmdline -- hardwired stuff
push @cmdline, "-monitor";
push @cmdline, "unix:" . $monitor . ",server,nowait";
push @cmdline, "-serial";
push @cmdline, "unix:,server,nowait";
if ($graphics) {
push @cmdline, "-vnc";
push @cmdline, "127.0.0.1:0,to=128,password";
}
# prepare
print "-- \n" if $debug;
mkdir $mondir unless -d $mondir;
# run qemu emulator
if (-S $monitor and !system("fuser", "-s", $monitor)) {
# still running
printf "VM %s still running, reconnecting\n", $name;
} else {
$pid = fork();
die "fork: $!" unless defined($pid);
if (0 == $pid) {
# child
print join(", ", map { "\"$_\"" } @cmdline) . "\n"
if $debug;
exec(@cmdline);
exit(1);
}
}
# run qemu-gtk
@cmdline = ();
push @cmdline, "qemu-gtk";
push @cmdline, "-m" if $showmon;
push @cmdline, "unix:" . $monitor . ",server,nowait";
foreach my $i (0 ... 100) {
last if -S $monitor;
sleep(0.1);
}
print join(", ", map { "\"$_\"" } @cmdline) . "\n"
if $debug;
exec(@cmdline);
|