Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly check free storage size #1980

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/Ravada/Domain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,9 @@ sub _around_add_volume {
($name) = $file =~ m{.*/(.*)} if !$name && $file;
$name = $self->name if !$name;

$name .= "-".$args{target}."-".Ravada::Utils::random_name(4);
$name .= "-".$args{target}."-".Ravada::Utils::random_name(4)
if $name !~ /\.iso$/;

$args{name} = $name;
}

Expand All @@ -715,10 +717,12 @@ sub _around_add_volume {
$args{allocation} = Ravada::Utils::size_to_number($args{allocation})
if exists $args{allocation} && defined $args{allocation};

my $free = $self->_vm->free_disk();
my $storage = $args{storage};

my $free = $self->_vm->free_disk($storage);
my $free_out = int($free / 1024 / 1024 / 1024 ) * 1024 *1024 *1024;

confess "Error creating volume, out of space $size . Disk free: "
die "Error creating volume, out of space $size . Disk free: "
.Ravada::Utils::number_to_size($free_out)
."\n"
if exists $args{size} && $args{size} && $args{size} >= $free;
Expand Down Expand Up @@ -1939,9 +1943,6 @@ sub display($self, $user) {

my ($display_info) = grep { $_->{driver} !~ /-tls$/ } @display_info;

confess "Error: I can't find builtin display info for ".$self->name." ".ref($self)."\n".Dumper($display_info)
if !exists $display_info->{port};

return '' if !$display_info->{driver} || !$display_info->{ip}
|| !$display_info->{port};

Expand Down
9 changes: 6 additions & 3 deletions t/lib/Test/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ create_domain
remove_old_users
remove_old_users_ldap
remove_qemu_pools
mangle_volume
test_volume_contents
test_volume_format
Expand Down Expand Up @@ -273,6 +275,7 @@ sub create_domain_v2(%args) {
my $user = (delete $args{user} or $USER_ADMIN);
my $iso_name = delete $args{iso_name};
my $id_iso = delete $args{id_iso};
my $disk = delete $args{disk};
croak "Error: supply either iso_name or id_iso ".Dumper(\%args)
if $iso_name && $id_iso;

Expand All @@ -298,13 +301,12 @@ sub create_domain_v2(%args) {
if keys %args;

my $iso;
my $disk;
if ($vm->type eq 'KVM' && (!$iso_name || $iso_name !~ /Alpine/i)) {
$iso = $vm->_search_iso($id_iso);
$disk = ($iso->{min_disk_size} or 2 );
$disk = ($iso->{min_disk_size} or 2 ) if !$disk;
diag("Creating [$id_iso] $iso->{name}");
} else {
$disk = 2;
$disk = 2 if !$disk;
}

if ($vm->type eq 'KVM' && !$options ) {
Expand Down Expand Up @@ -1510,6 +1512,7 @@ sub remove_old_storage_pools_void() {
}

sub remove_old_storage_pools_kvm() {
remove_qemu_pools();
}

sub remove_old_storage_pools() {
Expand Down
24 changes: 12 additions & 12 deletions t/vm/20_base.t
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,15 @@ sub test_displays_added_on_refresh($domain, $n_expected, $delete=1) {

_wait_ip($domain);

my $domain_f0 = Ravada::Front::Domain->open($domain->id);
my $display0_raw = $domain_f0->info(user_admin)->{hardware}->{display};

my @display0 = grep { $_->{is_secondary} == 0 } @$display0_raw;
my $n_expected2 = scalar(@display0);

if ($delete) {
my $sth = connector->dbh->prepare("DELETE FROM domain_displays WHERE id_domain=?");
$sth->execute($domain->id);
}else {
my $sth = connector->dbh->prepare("SELECT count(*) FROM domain_displays WHERE id_domain=?");
$sth->execute($domain->id);
my ($n_exp2) = $sth->fetchrow;
if ($n_exp2 && $n_expected != $n_exp2) {
cluck "n_expected was $n_expected, but it should be $n_exp2";
$n_expected = $n_exp2;
}
}
my $req;
for ( 1 .. 3 ) {
Expand All @@ -333,7 +331,7 @@ sub test_displays_added_on_refresh($domain, $n_expected, $delete=1) {
my $count;
for ( 1 .. 10 ) {
my $sth_count = connector->dbh->prepare(
"SELECT count(*) FROM domain_displays WHERE id_domain=?");
"SELECT count(*) FROM domain_displays WHERE id_domain=? AND is_secondary=0");
$sth_count->execute($domain->id);
($count) = $sth_count->fetchrow;
last if $count;
Expand All @@ -344,11 +342,13 @@ sub test_displays_added_on_refresh($domain, $n_expected, $delete=1) {
);
wait_request();
}
ok($count>=$n_expected,"Got $count, expecting >$n_expected displays on table domain_displays for ".$domain->name) or confess;
ok($count>=$n_expected || $count >=$n_expected2,"Got $count, expecting >$n_expected or $n_expected2 displays on table domain_displays for ".$domain->name) or confess;

my $domain_f = Ravada::Front::Domain->open($domain->id);
my $display = $domain_f->info(user_admin)->{hardware}->{display};
is(scalar(@$display), $n_expected,"Expecting $n_expected displays on info->{hardware}->{display} in ".$domain->name) or confess Dumper($display);
my $display_raw = $domain_f->info(user_admin)->{hardware}->{display};
my @display = grep { $_->{is_secondary} == 0 } @$display_raw;
ok(scalar(@display) == $n_expected || scalar(@display) == $n_expected2
,"Expecting $n_expected or $n_expected2 displays on info->{hardware}->{display} in ".$domain->name) or confess Dumper(@display);

}

Expand Down
48 changes: 48 additions & 0 deletions t/vm/s30_storage.t
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,48 @@ sub _clean_local {
my $file = "$dir/check_storage";# or die "$!";
unlink $file or die "$! $file" if -e $file;
}

sub test_storage_full($vm) {

my $dir = "/run";
$dir.="/user/".$< if $<;
my $storage_name = new_domain_name();

$dir.= "/".$storage_name;

mkdir $dir or die "$! $dir" if ! -e $dir;

if (! grep { $_ eq $storage_name} $vm->list_storage_pools) {
$vm->create_storage_pool($storage_name,$dir);
}

my($out,$err) = $vm->run_command("df");

my ($available) = $out =~ m{(\d+)\s+\d+\% /run}ms;

$available = $available*10;

$vm->default_storage_pool_name($storage_name);

my $name = new_domain_name;
my $req = Ravada::Request->create_domain(
vm => $vm->type
,id_owner => user_admin->id
,name => $name
,disk => $available
,storage => 'default'
,id_iso => search_id_iso('Alpine%64')
);
wait_request(debug => 0);

my $domain = $vm->search_domain($name);
for my $vol ($domain->list_volumes) {
next if $vol =~ /iso$/;
unlike($vol,qr{^/run});
}

}

###########################################################

_clean_local();
Expand All @@ -101,6 +143,12 @@ for my $vm_name (vm_names() ) {
}

skip($msg,10) if !$vm;

if ($vm->type eq 'KVM') {
remove_qemu_pools($vm);
}
test_storage_full($vm);

test_storage_pools($vm);
test_storage_pools_fail($vm);
}
Expand Down
Loading