Skip to content

Commit

Permalink
wip: import volume defined by name
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejol committed Jul 11, 2023
1 parent 4d511fe commit f71452e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
18 changes: 16 additions & 2 deletions lib/Ravada/Domain/KVM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ sub _disk_device($self, $with_info=undef, $attribute=undef, $value=undef) {
for my $disk ($doc->findnodes('/domain/devices/disk')) {
my ($source_node) = $disk->findnodes('source');
my $file;
$file = $source_node->getAttribute('file') if $source_node;
if ( $source_node ) {
$file = $self->_get_volume_file($source_node);
}

my ($target_node) = $disk->findnodes('target');
my ($driver_node) = $disk->findnodes('driver');
Expand Down Expand Up @@ -571,7 +573,7 @@ sub _set_volumes_backing_store($self) {
for my $disk ($doc->findnodes('/domain/devices/disk')) {
next if $disk->getAttribute('device') ne 'disk';
for my $source( $disk->findnodes('source')) {
my $file = $source->getAttribute('file');
my $file = $self->_get_volume_file($source);
my $backing_file = $vol{$file}->backing_file();

$self->_set_backing_store($disk, $backing_file);
Expand All @@ -581,6 +583,18 @@ sub _set_volumes_backing_store($self) {
$self->reload_config($doc);
}

sub _get_volume_file($self, $source) {
return $source->getAttribute('file') if $source->getAttribute('file');

my $pool_name = $source->getAttribute('pool') or die "Error: I need pool or file in ".$source->toString();
my $volume = $source->getAttribute('volume') or die "Error: I need pool or file in ".$source->toString();
my $pool = $self->_vm->vm->get_storage_pool_by_name($pool_name)
or die "Error: no pool $pool_name";
my $vol = $pool->get_volume_by_name($volume);
return $vol->get_path;

}


sub _store_xml($self) {
my $xml = $self->domain->get_xml_description(Sys::Virt::Domain::XML_INACTIVE);
Expand Down
4 changes: 4 additions & 0 deletions lib/Ravada/Volume.pm
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ sub BUILD($self, $arg) {
} elsif (exists $arg->{info}) {
if (exists $arg->{info}->{device} && $arg->{info}->{device} eq 'cdrom') {
$class = "Ravada::Volume::ISO";
} elsif(exists $arg->{info}->{driver} && exists $arg->{info}->{driver}->{type}) {
my $name = 'unknown';
$name = $arg->{info}->{name} if exists $arg->{info}->{name};
$class = "Ravada::Volume::"._type_from_extension("$name.".$arg->{info}->{driver}->{type});
} else {
confess "I can't guess class from ".Dumper($arg);
}
Expand Down
36 changes: 33 additions & 3 deletions t/kvm/40_import.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use warnings;
use strict;

use Data::Dumper;
use IPC::Run3 qw(run3);
use Test::More;

use lib 't/lib';
Expand Down Expand Up @@ -151,11 +152,25 @@ sub test_import_spinoff {

}

sub _create_vol($vm, $name) {
my @cmd = ("qemu-img",'create','-f','qcow2', $vm->dir_img."/".$name,'512M');
my ($in, $out, $err);
run3(\@cmd , \$in, \$out, \$err);
die $err if $err;
diag($out) if $out;
}

sub test_volume($vm) {

my $dom_name = new_domain_name();
my $vol_name = new_domain_name();
_create_vol($vm, $vol_name);
$vm->refresh_storage_pools();
return if $vm->type ne 'KVM';
my $xml =<<EOT;
<domain type='kvm'>
<name>Metasploitable3-win2k8</name>
<uuid>6f6c9b78-3ce4-4a4e-a025-b1c7ae1965e5</uuid>
<name>$dom_name</name>
<uuid>6f6c9b78-3ce4-4a4e-a025-b1c7ae1965e0</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://microsoft.com/win/2k8r2"/>
Expand Down Expand Up @@ -183,7 +198,7 @@ my $xml =<<EOT;
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type='volume' device='disk'>
<driver name='qemu' type='qcow2'/>
<source pool='default' volume='Metasploitable3-win2k8-sda'/>
<source pool='default' volume='$vol_name'/>
<target dev='sda' bus='sata'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
Expand Down Expand Up @@ -245,6 +260,21 @@ my $xml =<<EOT;
</domain>
EOT

$vm->vm->define_domain($xml);

my $domain;
eval {
$domain = $RVD_BACK->import_domain(
vm => $vm->type
,name => $dom_name
,user => $USER->name
);
};
diag($@) if $@;
is(''.$@,'') or exit;
ok($domain,"Importing domain $dom_name") or exit;


}

############################################################################
Expand Down

0 comments on commit f71452e

Please sign in to comment.