| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# |
|
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
|
3
|
|
|
|
|
|
|
# |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Rex::Virtualization::LibVirt::import; |
|
6
|
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
13
|
use v5.12.5; |
|
|
1
|
|
|
|
|
4
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
40
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.14.3'; # VERSION |
|
11
|
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
7
|
use Rex::Logger; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
7
|
|
|
13
|
1
|
|
|
1
|
|
33
|
use Rex::Helper::Run; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
55
|
|
|
14
|
1
|
|
|
1
|
|
9
|
use Rex::Commands::Fs; |
|
|
1
|
|
|
|
|
8
|
|
|
|
1
|
|
|
|
|
10
|
|
|
15
|
1
|
|
|
1
|
|
8
|
use File::Basename; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
143
|
|
|
16
|
1
|
|
|
1
|
|
7
|
use Rex::Virtualization::LibVirt::create; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
10
|
|
|
17
|
1
|
|
|
1
|
|
26
|
use Data::Dumper; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
893
|
|
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# |
|
20
|
|
|
|
|
|
|
# %opt = (cpus => 2, memory => 512) |
|
21
|
|
|
|
|
|
|
# |
|
22
|
|
|
|
|
|
|
sub execute { |
|
23
|
0
|
|
|
0
|
0
|
|
my ( $class, $arg1, %opt ) = @_; |
|
24
|
|
|
|
|
|
|
|
|
25
|
0
|
0
|
|
|
|
|
unless ($arg1) { |
|
26
|
0
|
|
|
|
|
|
die("You have to define the vm name!"); |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
my $dom = $arg1; |
|
30
|
0
|
|
|
|
|
|
Rex::Logger::debug( "importing: $dom -> " . $opt{file} ); |
|
31
|
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
my $cwd = i_run "pwd"; |
|
33
|
0
|
|
|
|
|
|
chomp $cwd; |
|
34
|
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
my $dir = dirname $opt{file}; |
|
36
|
0
|
|
|
|
|
|
my ( undef, undef, $suffix ) = fileparse( $opt{file}, qr{\.[^.]*} ); |
|
37
|
0
|
0
|
|
|
|
|
$opt{storage_path} = $cwd . '/storage' unless ( $opt{storage_path} ); |
|
38
|
0
|
|
|
|
|
|
my $file = $opt{storage_path} . '/' . $dom . $suffix; |
|
39
|
0
|
|
|
|
|
|
i_run "mkdir -p $opt{storage_path}"; |
|
40
|
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
my $format = "qcow2"; |
|
42
|
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my @serial_devices; |
|
44
|
0
|
0
|
|
|
|
|
if ( exists $opt{serial_devices} ) { |
|
45
|
0
|
|
|
|
|
|
@serial_devices = @{ $opt{serial_devices} }; |
|
|
0
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
if ( $opt{file} =~ m/\.ova$/ ) { |
|
49
|
0
|
|
|
|
|
|
Rex::Logger::debug("Importing ova file. Try to convert with qemu-img"); |
|
50
|
0
|
|
|
|
|
|
$file =~ s/\.[a-z]+$//; |
|
51
|
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
my @vmdk = grep { m/\.vmdk$/ } i_run "tar -C '$dir' -vxf '$opt{file}'"; |
|
|
0
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
Rex::Logger::debug("converting '$cwd/tmp/$vmdk[0]' -> '$file.qcow2'"); |
|
55
|
0
|
|
|
|
|
|
i_run "qemu-img convert -O qcow2 '$cwd/tmp/$vmdk[0]' '$file.qcow2'", |
|
56
|
|
|
|
|
|
|
fail_ok => 1; |
|
57
|
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
if ( $? != 0 ) { |
|
59
|
0
|
|
|
|
|
|
Rex::Logger::info( |
|
60
|
|
|
|
|
|
|
"Can't import and convert $opt{file}. You qemu-img version seems not " |
|
61
|
|
|
|
|
|
|
. " to support this format.", |
|
62
|
|
|
|
|
|
|
"warn" |
|
63
|
|
|
|
|
|
|
); |
|
64
|
0
|
|
|
|
|
|
die("Error importing VM $opt{file}"); |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
$file = "$file.qcow2"; |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
else { |
|
70
|
0
|
|
|
|
|
|
Rex::Logger::debug("Importing kvm compatible file."); |
|
71
|
0
|
|
|
|
|
|
Rex::Logger::debug("Copying $opt{file} -> $file"); |
|
72
|
0
|
|
|
|
|
|
cp $opt{file}, $file; |
|
73
|
0
|
0
|
|
|
|
|
if ( $file =~ m/\.gz$/ ) { |
|
74
|
0
|
|
|
|
|
|
Rex::Logger::debug("Extracting gzip'ed file $file"); |
|
75
|
0
|
|
|
|
|
|
i_run "gunzip -q -f '$file'"; |
|
76
|
0
|
|
|
|
|
|
$file =~ s/\.gz$//; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my ($format_out) = grep { m/^file format:/ } i_run "qemu-img info '$file'", |
|
|
0
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
fail_ok => 1; |
|
82
|
0
|
0
|
|
|
|
|
if ( $format_out =~ m/^file format: (.*)$/i ) { |
|
83
|
0
|
|
|
|
|
|
$format = $1; |
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
my @network = values %{ $opt{__network} }; |
|
|
0
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
|
if ( scalar @network == 0 ) { |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# create default network |
|
90
|
0
|
|
|
|
|
|
push @network, |
|
91
|
|
|
|
|
|
|
{ |
|
92
|
|
|
|
|
|
|
type => "network", |
|
93
|
|
|
|
|
|
|
network => "default", |
|
94
|
|
|
|
|
|
|
}; |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
for (@network) { |
|
98
|
0
|
|
0
|
|
|
|
$_->{type} ||= "network"; |
|
99
|
0
|
0
|
0
|
|
|
|
$_->{type} = "bridge" if ( $_->{type} && $_->{type} eq "bridged" ); |
|
100
|
0
|
0
|
|
|
|
|
$_->{type} = "network" if ( $_->{type} eq "nat" ); |
|
101
|
0
|
0
|
0
|
|
|
|
if ( $_->{type} eq "network" && !exists $_->{network} ) { |
|
102
|
0
|
|
|
|
|
|
$_->{network} = "default"; |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Rex::Virtualization::LibVirt::create->execute( |
|
107
|
|
|
|
|
|
|
$dom, |
|
108
|
|
|
|
|
|
|
storage => [ |
|
109
|
|
|
|
|
|
|
{ |
|
110
|
|
|
|
|
|
|
file => "$file", |
|
111
|
|
|
|
|
|
|
dev => "vda", |
|
112
|
|
|
|
|
|
|
driver_type => $format, |
|
113
|
|
|
|
|
|
|
}, |
|
114
|
|
|
|
|
|
|
], |
|
115
|
|
|
|
|
|
|
network => \@network, |
|
116
|
|
|
|
|
|
|
serial_devices => \@serial_devices, |
|
117
|
|
|
|
|
|
|
memory => $opt{memory}, |
|
118
|
|
|
|
|
|
|
cpus => $opt{cpus}, |
|
119
|
0
|
|
|
|
|
|
); |
|
120
|
|
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
|
if ( exists $opt{__forward_port} ) { |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# currently not supported |
|
124
|
0
|
|
|
|
|
|
Rex::Logger::info( |
|
125
|
|
|
|
|
|
|
"Port-forwarding is currently not supported for KVM boxes.", "warn" ); |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
1; |