Skip to content

Commit

Permalink
Fix import volume referenced by name (#1975)
Browse files Browse the repository at this point in the history
fix: import volume defined by name
  • Loading branch information
frankiejol authored Jul 18, 2023
1 parent 678475c commit 3fc544e
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 2 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
128 changes: 128 additions & 0 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 @@ -149,6 +150,131 @@ 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>$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"/>
</libosinfo:libosinfo>
</metadata>
<memory unit='KiB'>1273856</memory>
<currentMemory unit='KiB'>1273856</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-q35-6.2'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='none'>
<model fallback='forbid'>qemu64</model>
</cpu>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type='volume' device='disk'>
<driver name='qemu' type='qcow2'/>
<source pool='default' volume='$vol_name'/>
<target dev='sda' bus='sata'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<controller type='usb' index='0' model='qemu-xhci'>
<address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
</controller>
<controller type='sata' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x1f' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pcie-root'/>
<controller type='pci' index='1' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='1' port='0x10'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0' multifunction='on'/>
</controller>
<controller type='pci' index='2' model='pcie-to-pci-bridge'>
<model name='pcie-pci-bridge'/>
<address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
</controller>
<controller type='pci' index='3' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='3' port='0x11'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x1'/>
</controller>
<controller type='pci' index='4' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='4' port='0x12'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x2'/>
</controller>
<interface type='bridge'>
<mac address='52:54:00:71:50:60'/>
<source bridge='HostOnly'/>
<model type='rtl8139'/>
<address type='pci' domain='0x0000' bus='0x02' slot='0x01' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes'>
<listen type='address'/>
</graphics>
<audio id='1' type='none'/>
<video>
<model type='cirrus' vram='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0'/>
</video>
<memballoon model='none'/>
</devices>
</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 All @@ -169,6 +295,8 @@ for my $vm_name (@VMS) {
diag("Testing import in $vm_name");
test_wrong_args($vm_name, $vm);

test_volume($vm);

my $domain = test_already_there($vm_name, $vm);
test_import($vm_name, $vm, $domain) if $domain;

Expand Down

0 comments on commit 3fc544e

Please sign in to comment.