Skip to content

Commit

Permalink
wip: remove storage pool
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejol committed Oct 19, 2023
1 parent 9d06a07 commit 00d405b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 90 deletions.
13 changes: 13 additions & 0 deletions lib/Ravada/VM/KVM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ sub search_volume_re($self,$pattern,$refresh=0) {
my @vols;
for ( 1 .. 10) {
eval { @vols = $pool->list_all_volumes() };
last if $@ && ref($@) && $@->code == 55;
last if $@ && $@ =~ /code: (55|38),/;
last if !$@ || $@ =~ / no storage pool with matching uuid/;
warn "WARNING: on search volume_re: $@";
sleep 1;
Expand Down Expand Up @@ -385,7 +387,9 @@ sub _list_volumes($self) {
for my $pool (_list_storage_pools($self->vm)) {
my @vols;
for ( 1 .. 10) {
next if !$pool->is_active;
eval { @vols = $pool->list_all_volumes() };
last if $@ && ref($@) && $@->code == 55;
last if !$@ || $@ =~ / no storage pool with matching uuid/;
warn "WARNING: on search volume_re: $@";
sleep 1;
Expand Down Expand Up @@ -587,6 +591,7 @@ sub file_exists($self, $file) {
sub _file_exists_remote($self, $file) {
$file = $self->_follow_link($file) unless $file =~ /which$/;
for my $pool ($self->vm->list_all_storage_pools ) {
next if !$pool->is_active;
$self->_wait_storage( sub { $pool->refresh() } );
my @volumes = $self->_wait_storage( sub { $pool->list_all_volumes });
for my $vol ( @volumes ) {
Expand Down Expand Up @@ -698,6 +703,14 @@ sub create_storage_pool($self, $name, $dir, $vm=$self->vm) {

}

sub remove_storage_pool($self, $name) {
my $sp = $self->vm->get_storage_pool_by_name($name);
return if !$sp;

$sp->destroy if $sp->is_active;
$sp->undefine();
}

sub _create_default_pool($self, $vm=$self->vm) {
my $dir = "/var/lib/libvirt/images";
mkdir $dir if ! -e $dir;
Expand Down
13 changes: 13 additions & 0 deletions lib/Ravada/VM/Void.pm
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,19 @@ sub active_storage_pool($self, $name, $value) {
$self->write_file($file_sp, Dump( \@list));
}

sub remove_storage_pool($self, $name) {
die "TODO remote VM" unless $self->is_local;

my $file_sp = $self->dir_img."/.storage_pools.yml";
my $sp_list = LoadFile($file_sp);
my @sp2;
for my $sp (@$sp_list) {
push @sp2,($sp) if $sp->{name} ne $name;
}

$self->write_file($file_sp, Dump( \@sp2));
}

#########################################################################3

1;
27 changes: 18 additions & 9 deletions t/lib/Test/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,12 @@ sub _activate_storage_pools($vm) {
next if $sp->is_active;
next unless $sp->get_name =~ /^tst_/;
diag("Activating sp ".$sp->get_name." on ".$vm->name);
$sp->build() unless $sp->is_active;
my $xml = XML::LibXML->load_xml(string => $sp->get_xml_description());
my ($path) = $xml->findnodes('/pool/target/path');
my $dir = $path->textContent();
mkdir $dir or die "$! $dir" if ! -e $dir;

$sp->build();
$sp->create() unless $sp->is_active;
}
}
Expand Down Expand Up @@ -1426,7 +1431,7 @@ sub _qemu_storage_pool {
}

sub remove_qemu_pools($vm=undef) {
return if !$VM_VALID{'KVM'} || $>;
return if !$vm && (!$VM_VALID{'KVM'} || $>);
return if defined $vm && $vm->type eq 'Void';
if (!defined $vm) {
eval { $vm = rvd_back->search_vm('KVM') };
Expand All @@ -1441,18 +1446,22 @@ sub remove_qemu_pools($vm=undef) {

my $base = base_pool_name();
$vm->connect();
for my $pool ( Ravada::VM::KVM::_list_storage_pools($vm->vm)) {
for my $pool ( $vm->vm->list_all_storage_pools) {
my $name = $pool->get_name;
next if $name !~ qr/^$base/;
diag($name);

eval {$pool->build(Sys::Virt::StoragePool::BUILD_NEW); $pool->create() };
warn $@ if $@ && $@ !~ /already active/;
next if $name !~ qr/^$base/;
diag("Removing ".$vm->name." storage_pool ".$pool->get_name);
for my $vol ( $pool->list_volumes ) {
diag("Removing ".$pool->get_name." vol ".$vol->get_name);
$vol->delete();
if ($pool->is_active) {
diag("Removing ".$vm->name." storage_pool ".$pool->get_name);
for my $vol ( $pool->list_volumes ) {
diag("Removing ".$pool->get_name." vol ".$vol->get_name);
$vol->delete();
}
}
_delete_qemu_pool($pool);
$pool->destroy();
$pool->destroy() if $pool->is_active;
eval { $pool->undefine() };
warn $@ if $@;
warn $@ if$@ && $@ !~ /libvirt error code: 49,/;
Expand Down
Loading

0 comments on commit 00d405b

Please sign in to comment.