| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Rex::Virtualization::LibVirt::import; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Rex::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Rex::Helper::Run; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Rex::Commands::Fs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Rex::Virtualization::LibVirt::create; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # %opt = (cpus => 2, memory => 512) | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: not called CC=17 SCAR=57.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 0 | my ( $class, $arg1, %opt ) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | unless ($arg1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | $opt{storage_path} = $cwd . '/storage' unless ( $opt{storage_path} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ( exists $opt{serial_devices} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | @serial_devices = @{ $opt{serial_devices} }; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ( $opt{file} =~ m/\.ova$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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}'"; | |||||||||||||||||||||||||||
| 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 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ( $file =~ m/\.gz$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'", | |||||||||||||||||||||||||||
| 81 | fail_ok => 1; | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ( $format_out =~ m/^file format: (.*)$/i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | $format = $1; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | my @network = values %{ $opt{__network} }; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ( scalar @network == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | $_->{type} ||= "network"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_->{"type"} ||= "network"
MC/DC0%0/1
$_->{"type"} ||= "network"
$_->{"type"}
Truth table0%0/2
A
A= $_->{"type"}
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $_->{type} = "bridge" if ( $_->{type} && $_->{type} eq "bridged" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_->{"type"} and $_->{"type"} eq "bridged"
MC/DC0%0/2
$_->{"type"} and $_->{"type"} eq "bridged"
$_->{"type"}
$_->{"type"} eq "bridged"
Truth table0%0/3
A and B
A= $_->{"type"}B= $_->{"type"} eq "bridged"
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | $_->{type} = "network" if ( $_->{type} eq "nat" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ( $_->{type} eq "network" && !exists $_->{network} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_->{"type"} eq "network" and not exists $_->{"network"}
MC/DC0%0/2
$_->{"type"} eq "network" and not exists $_->{"network"}
$_->{"type"} eq "network"
not exists $_->{"network"}
Truth table0%0/3
A and not(B)
A= $_->{"type"} eq "network"B= not 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 | ▶ | if ( exists $opt{__forward_port} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 1; | |||||||||||||||||||||||||||