this adds two optional filters to the migrateall api call:

onlineonly:

migrates only vms/ct which are online, ct now in restart mode

sharedonly:

migrates only vms/ct which have all disks/volumes/mp on shared storage
or marked as shared

Signed-off-by: Dominik Csapak <d.csa...@proxmox.com>
---
 PVE/API2/Nodes.pm | 35 ++++++++++++++++++++++++++++++-----
 1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm
index 457263a..8fe825f 100644
--- a/PVE/API2/Nodes.pm
+++ b/PVE/API2/Nodes.pm
@@ -1211,11 +1211,17 @@ __PACKAGE__->register_method({
     }});
 
 my $get_start_stop_list = sub {
-    my ($nodename, $autostart) = @_;
+    my ($nodename, $autostart, $sharedonly) = @_;
 
     my $vmlist = PVE::Cluster::get_vmlist();
 
     my $resList = {};
+
+    # read storage config only one time
+    my $scfg;
+    if ($sharedonly) {
+       $scfg = PVE::Storage::config();
+    }
     foreach my $vmid (keys %{$vmlist->{ids}}) {
        my $d = $vmlist->{ids}->{$vmid};
        my $startup;
@@ -1228,12 +1234,15 @@ my $get_start_stop_list = sub {
            my $conf;
            if ($d->{type} eq 'lxc') {
                $conf = PVE::LXC::Config->load_config($vmid);
+               return if $sharedonly && 
!PVE::LXC::Config->is_shared_only($conf, $scfg);
            } elsif ($d->{type} eq 'qemu') {
                $conf = PVE::QemuConfig->load_config($vmid);
+               return if $sharedonly && 
!PVE::QemuConfig->is_shared_only($conf, $scfg);
            } else {
                die "unknown VM type '$d->{type}'\n";
            }
 
+
            return if $autostart && !$conf->{onboot};
 
            if ($conf->{startup}) {
@@ -1493,16 +1502,22 @@ __PACKAGE__->register_method ({
     }});
 
 my $create_migrate_worker = sub {
-    my ($nodename, $type, $vmid, $target) = @_;
+    my ($nodename, $type, $vmid, $target, $onlineonly) = @_;
 
     my $upid;
     if ($type eq 'lxc') {
        my $online = PVE::LXC::check_running($vmid) ? 1 : 0;
+
+       return undef if $onlineonly && !$online;
+
        print STDERR "Migrating CT $vmid\n";
        $upid = PVE::API2::LXC->migrate_vm({node => $nodename, vmid => $vmid, 
target => $target,
-                                           online => $online });
+                                           restart => $online });
     } elsif ($type eq 'qemu') {
        my $online = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0;
+
+       return undef if $onlineonly && !$online;
+
        print STDERR "Migrating VM $vmid\n";
        $upid = PVE::API2::Qemu->migrate_vm({node => $nodename, vmid => $vmid, 
target => $target,
                                             online => $online });
@@ -1538,6 +1553,16 @@ __PACKAGE__->register_method ({
                 type => 'integer',
                 minimum => 1
             },
+           sharedonly => {
+               description => "Migrate only those guests with only shared 
storage",
+               optional => 1,
+               type => 'boolean'
+           },
+           onlineonly => {
+               description => "Migrate only those guests with only shared 
storage",
+               optional => 1,
+               type => 'boolean'
+           },
        },
     },
     returns => {
@@ -1563,7 +1588,7 @@ __PACKAGE__->register_method ({
 
            $rpcenv->{type} = 'priv'; # to start tasks in background
 
-           my $migrateList = &$get_start_stop_list($nodename);
+           my $migrateList = &$get_start_stop_list($nodename, undef, 
$param->{sharedonly});
 
            foreach my $order (sort {$b <=> $a} keys %$migrateList) {
                my $vmlist = $migrateList->{$order};
@@ -1571,7 +1596,7 @@ __PACKAGE__->register_method ({
                foreach my $vmid (sort {$b <=> $a} keys %$vmlist) {
                    my $d = $vmlist->{$vmid};
                    my $pid;
-                   eval { $pid = &$create_migrate_worker($nodename, 
$d->{type}, $vmid, $target); };
+                   eval { $pid = &$create_migrate_worker($nodename, 
$d->{type}, $vmid, $target, $param->{onlineonly}); };
                    warn $@ if $@;
                    next if !$pid;
 
-- 
2.1.4


_______________________________________________
pve-devel mailing list
pve-devel@pve.proxmox.com
http://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to