]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuMigrate.pm
migrate: fix local disk migration with online VMs
[qemu-server.git] / PVE / QemuMigrate.pm
index 27cf7e36eb3706d3cc73cadad0f0ea436a7fa4dc..07d57ed3b70f2836be69ef111064112d5dc3efd1 100644 (file)
@@ -234,7 +234,7 @@ sub prepare {
        my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
 
        # check if storage is available on both nodes
-       my $targetsid = $self->{opts}->{targetstorage} ? $self->{opts}->{targetstorage} : $sid;
+       my $targetsid = $self->{opts}->{targetstorage} // $sid;
 
        my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid);
        PVE::Storage::storage_check_node($self->{storecfg}, $targetsid, $self->{node});
@@ -269,6 +269,8 @@ sub sync_disks {
     # local volumes which have been copied
     $self->{volumes} = [];
 
+    my $override_targetsid = $self->{opts}->{targetstorage};
+
     eval {
 
        # found local volumes and their origin
@@ -301,7 +303,7 @@ sub sync_disks {
 
            next if @{$dl->{$storeid}} == 0;
 
-           my $targetsid = $self->{opts}->{targetstorage} ? $self->{opts}->{targetstorage} : $storeid;
+           my $targetsid = $override_targetsid // $storeid;
 
            # check if storage is available on target node
            PVE::Storage::storage_check_node($self->{storecfg}, $targetsid, $self->{node});
@@ -340,7 +342,7 @@ sub sync_disks {
 
            my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
 
-           my $targetsid = $self->{opts}->{targetstorage} ? $self->{opts}->{targetstorage} : $sid;
+           my $targetsid = $override_targetsid // $sid;
            # check if storage is available on both nodes
            my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid);
            PVE::Storage::storage_check_node($self->{storecfg}, $targetsid, $self->{node});
@@ -404,10 +406,6 @@ sub sync_disks {
            $self->log('warn', "$err");
        }
 
-       if ($self->{running} && !$sharedvm && !$self->{opts}->{targetstorage}) {
-           $self->{opts}->{targetstorage} = 1; #use same sid for remote local
-       }
-
        if ($abort) {
            die "can't migrate VM - check log\n";
        }
@@ -446,14 +444,15 @@ sub sync_disks {
 
        foreach my $volid (keys %$local_volumes) {
            my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
-           if ($self->{running} && $self->{opts}->{targetstorage} && $local_volumes->{$volid}->{ref} eq 'config') {
+           my $targetsid = $override_targetsid // $sid;
+           if ($self->{running} && $local_volumes->{$volid}->{ref} eq 'config') {
                push @{$self->{online_local_volumes}}, $volid;
            } else {
                next if $rep_volumes->{$volid};
                push @{$self->{volumes}}, $volid;
                my $insecure = $self->{opts}->{migration_type} eq 'insecure';
                my $with_snapshots = $local_volumes->{$volid}->{snapshots};
-               PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{ssh_info}, $sid,
+               PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{ssh_info}, $targetsid,
                                              undef, undef, undef, undef, $insecure, $with_snapshots);
            }
        }
@@ -554,8 +553,8 @@ sub phase2 {
        push @$cmd, '--machine', $self->{forcemachine};
     }
 
-    if ($self->{opts}->{targetstorage}) {
-       push @$cmd, '--targetstorage', $self->{opts}->{targetstorage};
+    if ($self->{online_local_volumes}) {
+       push @$cmd, '--targetstorage', ($self->{opts}->{targetstorage} // '1');
     }
 
     my $spice_port;
@@ -642,7 +641,7 @@ sub phase2 {
 
     my $start = time();
 
-    if ($self->{opts}->{targetstorage} && defined($self->{online_local_volumes})) {
+    if (defined($self->{online_local_volumes})) {
        $self->{storage_migration} = 1;
        $self->{storage_migration_jobs} = {};
        $self->log('info', "starting storage migration");
@@ -966,6 +965,11 @@ sub phase3_cleanup {
                $self->{errors} = 1;
            }
        }
+
+       if ($self->{storage_migration} && PVE::QemuServer::parse_guest_agent($conf)->{fstrim_cloned_disks} && $self->{running}) {
+           my $cmd = [@{$self->{rem_ssh}}, 'qm', 'guest', 'cmd', $vmid, 'fstrim'];
+           eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) };
+       }
     }
 
     # close tunnel on successful migration, on error phase2_cleanup closed it