]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuMigrate.pm
fix local disk migration when no target storage is set
[qemu-server.git] / PVE / QemuMigrate.pm
index 04a0776852fe755fba5d1ae8aec8780f1b7c20a7..b12034f331b1e221e33dfdb75361403ffb5095d0 100644 (file)
@@ -404,10 +404,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 +442,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 = $self->{opts}->{targetstorage} ? $self->{opts}->{targetstorage} : $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 +551,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;
@@ -692,6 +689,8 @@ sub phase2 {
     # set cachesize to 10% of the total memory
     my $memory =  $conf->{memory} || $defaults->{memory};
     my $cachesize = int($memory * 1048576 / 10);
+    $cachesize = round_powerof2($cachesize);
+
     $self->log('info', "set cachesize: $cachesize");
     eval {
        PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-cache-size", value => int($cachesize));
@@ -964,6 +963,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
@@ -1032,4 +1036,9 @@ sub final_cleanup {
     # nothing to do
 }
 
+sub round_powerof2 {
+    return 1 if $_[0] < 2;
+    return 2 << int(log($_[0]-1)/log(2));
+}
+
 1;