]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/VZDump/QemuServer.pm
vzdump: log 'finishing' state
[qemu-server.git] / PVE / VZDump / QemuServer.pm
index 8629c4966d5a387446fa6e72a985a0fe1d7a64c0..575abb3a8adea77251815f98ef564797a63e7725 100644 (file)
@@ -7,6 +7,7 @@ use File::Basename;
 use File::Path;
 use IO::File;
 use IPC::Open3;
+use JSON;
 
 use PVE::Cluster qw(cfs_read_file);
 use PVE::INotify;
@@ -252,27 +253,124 @@ sub archive {
     }
 }
 
+# number, [precision=1]
+my $num2str = sub {
+    return sprintf( "%." . ( $_[1] || 1 ) . "f", $_[0] );
+};
+my sub bytes_to_human {
+    my ($bytes, $precission) = @_;
+
+    return $num2str->($bytes, $precission) . ' B' if $bytes < 1024;
+    my $kb = $bytes/1024;
+
+    return $num2str->($kb, $precission) . " KiB" if $kb < 1024;
+    my $mb = $kb/1024;
+
+    return $num2str->($mb, $precission) . " MiB" if $mb < 1024;
+    my $gb = $mb/1024;
+
+    return $num2str->($gb, $precission) . " GiB" if $gb < 1024;
+    my $tb = $gb/1024;
+
+    return $num2str->($tb, $precission) . " TiB";
+}
+my sub duration_to_human {
+    my ($seconds) = @_;
+
+    return sprintf('%2ds', $seconds) if $seconds < 60;
+    my $minutes = $seconds / 60;
+    $seconds = $seconds % 60;
+
+    return sprintf('%2dm %2ds', $minutes, $seconds) if $minutes < 60;
+    my $hours = $minutes / 60;
+    $minutes = $minutes % 60;
+
+    return sprintf('%2dh %2dm %2ds', $hours, $minutes, $seconds) if $hours < 24;
+    my $days = $hours / 24;
+    $hours = $hours % 24;
+
+    return sprintf('%2dd %2dh %2dm', $days, $hours, $minutes);
+}
+
+my $bitmap_action_to_human = sub {
+    my ($self, $info) = @_;
+
+    my $action = $info->{action};
+
+    if ($action eq "not-used") {
+       return "disabled (no support)" if $self->{vm_was_running};
+       return "disabled (VM not running)";
+    } elsif ($action eq "not-used-removed") {
+       return "disabled (old bitmap cleared)";
+    } elsif ($action eq "new") {
+       return "created new";
+    } elsif ($action eq "used") {
+       if ($info->{dirty} == 0) {
+           return "OK (drive clean)";
+       } else {
+           my $size = bytes_to_human($info->{size});
+           my $dirty = bytes_to_human($info->{dirty});
+           return "OK ($dirty of $size dirty)";
+       }
+    } elsif ($action eq "invalid") {
+       return "existing bitmap was invalid and has been cleared";
+    } else {
+       return "unknown";
+    }
+};
+
 my $query_backup_status_loop = sub {
-    my ($self, $vmid, $job_uuid) = @_;
+    my ($self, $vmid, $job_uuid, $qemu_support) = @_;
 
     my $starttime = time ();
     my $last_time = $starttime;
-    my ($last_percent, $last_total, $last_zero, $last_transferred) = (-1, 0, 0, 0);
-    my $transferred;
+    my ($last_percent, $last_total, $last_target, $last_zero, $last_transferred) = (-1, 0, 0, 0, 0);
+    my ($transferred, $reused);
 
     my $get_mbps = sub {
        my ($mb, $delta) = @_;
-       return ($mb > 0) ? int(($mb / $delta) / (1000 * 1000)) : 0;
+       return "0 B/s" if $mb <= 0;
+       my $bw = int(($mb / $delta));
+       return bytes_to_human($bw) . "/s";
     };
 
+    my $target = 0;
+    my $last_reused = 0;
+    my $has_query_bitmap = $qemu_support && $qemu_support->{'query-bitmap-info'};
+    my $is_template = PVE::QemuConfig->is_template($self->{vmlist}->{$vmid});
+    if ($has_query_bitmap) {
+       my $total = 0;
+       my $bitmap_info = mon_cmd($vmid, 'query-pbs-bitmap-info');
+       for my $info (sort { $a->{drive} cmp $b->{drive} } @$bitmap_info) {
+           if (!$is_template) {
+               my $text = $bitmap_action_to_human->($self, $info);
+               my $drive = $info->{drive};
+               $drive =~ s/^drive-//; # for consistency
+               $self->loginfo("$drive: dirty-bitmap status: $text");
+           }
+           $target += $info->{dirty};
+           $total += $info->{size};
+           $last_reused += $info->{size} - $info->{dirty};
+       }
+       if ($target < $total) {
+           my $total_h = bytes_to_human($total);
+           my $target_h = bytes_to_human($target);
+           $self->loginfo("using fast incremental mode (dirty-bitmap), $target_h dirty of $total_h total");
+       }
+    }
+
+    my $first_round = 1;
+    my $last_finishing = 0;
     while(1) {
        my $status = mon_cmd($vmid, 'query-backup');
 
        my $total = $status->{total} || 0;
+       my $dirty = $status->{dirty};
+       $target = (defined($dirty) && $dirty < $total) ? $dirty : $total if !$has_query_bitmap;
        $transferred = $status->{transferred} || 0;
-       my $percent = $total ? int(($transferred * 100)/$total) : 0;
+       $reused = $status->{reused};
+       my $percent = $target ? int(($transferred * 100)/$target) : 100;
        my $zero = $status->{'zero-bytes'} || 0;
-       my $zero_per = $total ? int(($zero * 100)/$total) : 0;
 
        die "got unexpected uuid\n" if !$status->{uuid} || ($status->{uuid} ne $job_uuid);
 
@@ -280,42 +378,88 @@ my $query_backup_status_loop = sub {
        my $duration = $ctime - $starttime;
 
        my $rbytes = $transferred - $last_transferred;
-       my $wbytes = $rbytes - ($zero - $last_zero);
+       my $wbytes;
+       if ($reused) {
+           # reused includes zero bytes for PBS
+           $wbytes = $rbytes - ($reused - $last_reused);
+       } else {
+           $wbytes = $rbytes - ($zero - $last_zero);
+       }
 
        my $timediff = ($ctime - $last_time) || 1; # fixme
        my $mbps_read = $get_mbps->($rbytes, $timediff);
        my $mbps_write = $get_mbps->($wbytes, $timediff);
+       my $target_h = bytes_to_human($target);
+       my $transferred_h = bytes_to_human($transferred);
 
-       my $statusline = "status: $percent% ($transferred/$total), sparse ${zero_per}% ($zero), duration $duration, read/write $mbps_read/$mbps_write MB/s";
+       if (!$has_query_bitmap && $first_round && $target != $total) { # FIXME: remove with PVE 7.0
+           my $total_h = bytes_to_human($total);
+           $self->loginfo("using fast incremental mode (dirty-bitmap), $target_h dirty of $total_h total");
+       }
+
+       my $statusline = sprintf("%3d%% ($transferred_h of $target_h) in %s"
+           .", read: $mbps_read, write: $mbps_write", $percent, duration_to_human($duration));
 
        my $res = $status->{status} || 'unknown';
        if ($res ne 'active') {
-           $self->loginfo($statusline);
+           if ($last_percent < 100) {
+               $self->loginfo($statusline);
+           }
            if ($res ne 'done') {
                die (($status->{errmsg} || "unknown error") . "\n") if $res eq 'error';
                die "got unexpected status '$res'\n";
-           } elsif ($total != $transferred) {
-               die "got wrong number of transfered bytes ($total != $transferred)\n";
            }
+           $last_target = $target if $target;
+           $last_total = $total if $total;
+           $last_zero = $zero if $zero;
+           $last_transferred = $transferred if $transferred;
            last;
        }
        if ($percent != $last_percent && ($timediff > 2)) {
            $self->loginfo($statusline);
            $last_percent = $percent;
+           $last_target = $target if $target;
            $last_total = $total if $total;
            $last_zero = $zero if $zero;
            $last_transferred = $transferred if $transferred;
            $last_time = $ctime;
+           $last_reused = $reused;
+
+           if (!$last_finishing && $status->{finishing}) {
+               $self->loginfo("Waiting for server to finish verification...");
+           }
+           $last_finishing = $status->{finishing};
        }
        sleep(1);
+       $first_round = 0 if $first_round;
     }
 
     my $duration = time() - $starttime;
-    if ($transferred && $duration) {
-       my $mb = int($transferred / (1000 * 1000));
-       my $mbps = $get_mbps->($transferred, $duration);
-       $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
+
+    if ($last_zero) {
+       my $zero_per = $last_target ? int(($last_zero * 100)/$last_target) : 0;
+       my $zero_h = bytes_to_human($last_zero, 2);
+       $self->loginfo("backup is sparse: $zero_h (${zero_per}%) total zero data");
     }
+    if ($reused) {
+       my $reused_h = bytes_to_human($reused, 2);
+       my $reuse_per = int($reused * 100 / $last_total);
+       $self->loginfo("backup was done incrementally, reused $reused_h (${reuse_per}%)");
+    }
+    if ($transferred) {
+       my $transferred_h = bytes_to_human($transferred, 2);
+       if ($duration) {
+           my $mbps = $get_mbps->($transferred, $duration);
+           $self->loginfo("transferred $transferred_h in $duration seconds ($mbps)");
+       } else {
+           $self->loginfo("transferred $transferred_h in <1 seconds");
+       }
+    }
+
+    return {
+       total => $last_total,
+       reused => $reused,
+    };
 };
 
 sub archive_pbs {
@@ -336,21 +480,14 @@ sub archive_pbs {
 
     my $repo = "$username\@$server:$datastore";
     my $password = PVE::Storage::PBSPlugin::pbs_get_password($scfg, $opts->{storage});
+    my $keyfile = PVE::Storage::PBSPlugin::pbs_encryption_key_file_name($scfg, $opts->{storage});
 
     my $diskcount = scalar(@{$task->{disks}});
-    if (PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}) || !$diskcount) {
+    # proxmox-backup-client can only handle raw files and block devs
+    # only use it (directly) for disk-less VMs
+    if (!$diskcount) {
        my @pathlist;
-       foreach my $di (@{$task->{disks}}) {
-           if ($di->{type} eq 'block' || $di->{type} eq 'file') {
-               push @pathlist, "$di->{qmdevice}.img:$di->{path}";
-           } else {
-               die "implement me (type $di->{type})";
-           }
-       }
-
-       if (!$diskcount) {
-           $self->loginfo("backup contains no disks");
-       }
+       $self->loginfo("backup contains no disks");
 
        local $ENV{PBS_PASSWORD} = $password;
        local $ENV{PBS_FINGERPRINT} = $fingerprint if defined($fingerprint);
@@ -365,7 +502,6 @@ sub archive_pbs {
 
        push @$cmd, "qemu-server.conf:$conffile";
        push @$cmd, "fw.conf:$firewall" if -e $firewall;
-       push @$cmd, @pathlist if scalar(@pathlist);
 
        $self->loginfo("starting template backup");
        $self->loginfo(join(' ', @$cmd));
@@ -386,6 +522,12 @@ sub archive_pbs {
            die "interrupted by signal\n";
        };
 
+       my $qemu_support = eval { mon_cmd($vmid, "query-proxmox-support") };
+       if (!$qemu_support) {
+           die "PBS backups are not supported by the running QEMU version. Please make "
+             . "sure you've installed the latest version and the VM has been restarted.\n";
+       }
+
        my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
 
        my $params = {
@@ -397,8 +539,22 @@ sub archive_pbs {
            devlist => $devlist,
            'config-file' => $conffile,
        };
+       $params->{speed} = $opts->{bwlimit}*1024 if $opts->{bwlimit};
        $params->{fingerprint} = $fingerprint if defined($fingerprint);
        $params->{'firewall-file'} = $firewall if -e $firewall;
+       if (-e $keyfile) {
+           $self->loginfo("enabling encryption");
+           $params->{keyfile} = $keyfile;
+           $params->{encrypt} = JSON::true;
+       } else {
+           $params->{encrypt} = JSON::false;
+       }
+
+       my $is_template = PVE::QemuConfig->is_template($self->{vmlist}->{$vmid});
+       $params->{'use-dirty-bitmap'} = JSON::true
+           if $qemu_support->{'pbs-dirty-bitmap'} && $self->{vm_was_running} && !$is_template;
+
+       $params->{timeout} = 60; # give some time to connect to the backup server
 
        my $res = eval { mon_cmd($vmid, "backup", %$params) };
        my $qmperr = $@;
@@ -415,7 +571,8 @@ sub archive_pbs {
 
        $self->resume_vm_after_job_start($task, $vmid);
 
-       $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
+       my $stat = $query_backup_status_loop->($self, $vmid, $backup_job_uuid, $qemu_support);
+       $task->{size} = $stat->{total};
     };
     my $err = $@;
     if ($err) {
@@ -676,6 +833,7 @@ sub enforce_vm_running_for_backup {
        # start with skiplock
        my $params = {
            skiplock => 1,
+           skiptemplate => 1,
            paused => 1,
        };
        PVE::QemuServer::vm_start($self->{storecfg}, $vmid, $params);