Skip to content

Commit

Permalink
Fix backingstore (#1390)
Browse files Browse the repository at this point in the history
* wip(backend): set real file format in definition

issue #1324

* refactor(test): best way to clean iptables

issue #1324
  • Loading branch information
frankiejol authored Oct 6, 2020
1 parent f1c7873 commit eaca73a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 32 deletions.
15 changes: 12 additions & 3 deletions lib/Ravada/Domain/KVM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,24 @@ sub _set_volumes_backing_store($self) {
for my $source( $disk->findnodes('source')) {
my $file = $source->getAttribute('file');
my $backing_file = $vol{$file}->backing_file();
my $backing_file_format = $vol{$file}->_qemu_info('backing file format');


my ($backing_store) = $disk->findnodes('backingStore');
if ($backing_file) {
my $vol_backing_file = Ravada::Volume->new(
file => $backing_file
,vm => $self->_vm
);
my $backing_file_format = (
$vol_backing_file->_qemu_info('file format')
or 'qcow2'
);

$backing_store = $disk->addNewChild(undef,'backingStore') if !$backing_store;
$backing_store->setAttribute('type' => 'file');

my $format = $backing_store->findnodes('format');
$format = $backing_store->addNewChild(undef,'format');
my ($format) = $backing_store->findnodes('format');
$format = $backing_store->addNewChild(undef,'format') if !$format;
$format->setAttribute('type' => $backing_file_format);

my ($source_bf) = $backing_store->findnodes('source');
Expand Down
31 changes: 9 additions & 22 deletions lib/Ravada/Volume/QCOW2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,6 @@ sub prepare_base($self) {

}

sub _convert_to_qcow($self) {
my ($out, $err) = $self->vm->run_command("file",$self->file);
return if $out =~ /QEMU QCOW/;
warn "converting to qcow $out";

my $tmp = $self->file.".tmp";
($out, $err) = $self->vm->run_command(_cmd_convert($self->file, $tmp));
confess $err if $err;

my @cmd = ("cp",$self->file,$self->file.".".time().".backup");
($out, $err) = $self->vm->run_command(@cmd);
confess "@cmd $err" if $err;

@cmd = ("cp",$tmp,$self->file);
($out, $err) = $self->vm->run_command(@cmd);
confess "@cmd $err" if $err;
}

sub clone($self, $file_clone) {
my $n = 10;
for (;;) {
Expand All @@ -88,9 +70,9 @@ sub clone($self, $file_clone) {
confess if $self->file =~ /ISO/i;
confess if $file_clone =~ /ISO/i;

$self->_convert_to_qcow();
my $base_format = lc(Ravada::Volume::_type_from_file($self->file, $self->vm));
my @cmd = ($QEMU_IMG,'create'
,'-F','qcow2'
,'-F',$base_format
,'-f','qcow2'
,"-b", $self->file
,$file_clone
Expand Down Expand Up @@ -129,9 +111,11 @@ sub backing_file($self) {
}

sub rebase($self, $new_base) {

my $base_format = lc(Ravada::Volume::_type_from_file($new_base, $self->vm));
my @cmd = ($QEMU_IMG,'rebase'
,'-f','qcow2'
,'-F','qcow2'
,'-F',$base_format
,'-b',$new_base,$self->file);
my ($out, $err) = $self->vm->run_command(@cmd);
confess $err if $err;
Expand Down Expand Up @@ -179,7 +163,10 @@ sub _qemu_info($self, $field=undef) {
return $self->{_qemu_info}->{$field};
}

return {} if ! $self->vm->file_exists($self->file);
if ( ! $self->vm->file_exists($self->file) ) {
return if defined $field;
return {};
}
my @cmd = ( $QEMU_IMG,'info',$self->file,'-U');

my ($out, $err) = $self->vm->run_command(@cmd);
Expand Down
39 changes: 37 additions & 2 deletions t/lib/Test/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1193,8 +1193,24 @@ sub _unlock_all {
}
}

sub _clean_iptables_ravada($node) {
my ($out, $err) = $node->run_command("iptables-save","-t","filter");
is($err,'');
for my $line (split /\n/,$out) {
my ($rule) = $line =~ /-A (.*RAVADA.*)/i;
next if !$rule;
if (!$node->is_local) {
my ($out2, $err2) = $node->run_command("iptables","-t","filter","-D",$rule);
warn $node->name.": '-D $rule' $err2" if $err2;
} else {
`iptables -D $rule`;
}
}
}

sub flush_rules_node($node) {
_lock_fw();
_clean_iptables_ravada($node);
$node->create_iptables_chain($CHAIN);
my ($out, $err) = $node->run_command("iptables","-F", $CHAIN);
is($err,'');
Expand Down Expand Up @@ -1836,8 +1852,8 @@ sub mangle_volume($vm,$name,@vol) {
}
}

sub _mount_qcow($vm, $vol) {
my ($in,$out, $err);
sub _load_nbd($vm) {
my ($in, $out, $err);
if (!$MOD_NBD++) {
my @cmd =("/sbin/modprobe","nbd", "max_part=63");
run3(\@cmd, \$in, \$out, \$err);
Expand All @@ -1846,6 +1862,25 @@ sub _mount_qcow($vm, $vol) {
my @cmd = ($QEMU_NBD,"-d", $DEV_NBD);
($out,$err) = $vm->run_command(@cmd);
die "@cmd : $err" if $err;

($out, $err) = $vm->run_command("lsof",$DEV_NBD);
my @line = split /\n/,$out;
return if scalar(@line) < 2;

my ($dev,$n) = $DEV_NBD =~ /(.*?)(\d+)$/;
$n++;

$DEV_NBD = "$dev$n";
diag("Trying new NBD device $DEV_NBD");
die "Error: I can't find more NBD devices ( $DEV_NBD) "
if ! -e $DEV_NBD;

return _load_nbd($vm);
}

sub _mount_qcow($vm, $vol) {
_load_nbd($vm);
my ($in,$out, $err);
for ( 1 .. 10 ) {
($out, $err) = $vm->run_command($QEMU_NBD,"-c",$DEV_NBD, $vol);
last if !$err;
Expand Down
24 changes: 21 additions & 3 deletions t/vm/40_volumes.t
Original file line number Diff line number Diff line change
Expand Up @@ -636,18 +636,33 @@ sub _check_backing_store($xml, $name=undef) {
return 1;
}


sub _convert_file_to_raw($vm, @files) {
for my $file ( @files ) {
my $file_dst = "$file.raw";
my @cmd = ('qemu-img','convert',"-O","raw",$file,$file_dst);
my ($out, $err) = $vm->run_command(@cmd);
die $err if $err;
copy($file_dst,$file) or die "$! $file_dst -> $file";
unlink $file_dst or die "$! $file_dst";
}
}

sub _create_domain_no_backing_store($vm) {
#standalone has no backingStore entries
my $standalone = create_domain($vm);
$standalone->add_volume(type => 'TMP' , format => 'raw' ,size => 1024 * 10);
my $doc = _remove_backing_store($standalone->domain->get_xml_description);
$standalone->_post_change_hardware($doc);
_check_no_backing_store($standalone->domain->get_xml_description, $standalone->name);

# base XML has no backingStore entries
my $base = create_domain($vm);
$base->add_volume(type => 'TMP' , format => 'raw', size => 1024 * 10);
$base->prepare_base(user_admin);

my ($file) = grep { /TMP/ } $base->list_files_base;
_convert_file_to_raw($vm, $file);

my $base_doc = _remove_backing_store($base->get_xml_base);
my $sth = connector->_dbh->prepare(
"UPDATE base_xml set xml=? WHERE id_domain = ? "
Expand Down Expand Up @@ -679,14 +694,17 @@ sub test_upgrade($vm) {
$standalone->shutdown_now(user_admin);
ok(_check_empty_backing_store($standalone));

$clone->start(user_admin);
eval { $clone->start(user_admin) };
is(''.$@,'', "Expecting no error starting ".$clone->name);

is($clone->is_active,1);
$clone->shutdown_now(user_admin);
ok(_check_backing_store($clone));
ok(_check_backing_store($base));
ok(_check_backing_store($base->domain->get_xml_description,"base ".$base->name));

$clone->remove(user_admin);
eval { $clone->remove(user_admin) };
is(''.$@,'');
$base->remove_base(user_admin);
ok(_check_no_backing_store($base));
$base->start(user_admin);
Expand Down
5 changes: 3 additions & 2 deletions t/vm/45_vol_swap.t
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ sub test_clone_raw($domain ) {
is($format->getAttribute('type'),'qcow2',"Expecting format ".$format->toString)
or exit;
}
is($found,2) or exit;
is($found,2);

$clone->start(user_admin);
eval { $clone->start(user_admin) };
is(''.$@,'',"starting ".$clone->name) or exit;
is($clone->is_active,1);
$clone->remove(user_admin);
}
Expand Down

0 comments on commit eaca73a

Please sign in to comment.