]>
git.proxmox.com Git - qemu-server.git/blob - PVE/VZDump/QemuServer.pm
1 package PVE
::VZDump
::QemuServer
;
9 use PVE
::Cluster
qw(cfs_read_file);
15 use base qw
(PVE
::VZDump
::Plugin
);
18 my ($class, $vzdump) = @_;
20 PVE
::VZDump
::check_bin
('qm');
22 my $self = bless { vzdump
=> $vzdump };
24 $self->{vmlist
} = PVE
::QemuServer
::vzlist
();
25 $self->{storecfg
} = PVE
::Storage
::config
();
38 return [ keys %{$self->{vmlist
}} ];
42 my ($self, $task, $vmid, $mode) = @_;
46 my $conf = $self->{vmlist
}->{$vmid} = PVE
::QemuServer
::load_config
($vmid);
48 $task->{hostname
} = $conf->{name
};
50 my $lvmmap = PVE
::VZDump
::get_lvm_mapping
();
52 my $hostname = PVE
::INotify
::nodename
();
58 my $snapshot_count = 0;
62 PVE
::QemuServer
::foreach_drive
($conf, sub {
63 my ($ds, $drive) = @_;
65 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
67 if (defined($drive->{backup
}) && $drive->{backup
} eq "no") {
68 $self->loginfo("exclude disk '$ds' (backup=no)");
72 my $volid = $drive->{file
};
74 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
75 push @$vollist, $volid if $storeid;
76 $drivehash->{$ds} = $drive;
79 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
81 while (my ($ds, $drive) = each %$drivehash) {
83 my $volid = $drive->{file
};
87 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
89 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
96 die "no such volume '$volid'\n" if ! -e
$path;
98 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
99 snappath
=> $path, virtdev
=> $ds };
103 $diskinfo->{type
} = 'block';
105 $diskinfo->{filename
} = "vm-disk-$ds.raw";
107 if ($mode eq 'snapshot') {
108 my ($lvmvg, $lvmlv) = @{$lvmmap->{$path}} if defined ($lvmmap->{$path});
109 die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
111 $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
112 $diskinfo->{snapname
} = "vzsnap-$hostname-$ind->{$lvmvg}";
113 $diskinfo->{snapdev
} = "/dev/$lvmvg/$diskinfo->{snapname}";
114 $diskinfo->{lvmvg
} = $lvmvg;
115 $diskinfo->{lvmlv
} = $lvmlv;
116 $diskinfo->{snappath
} = $diskinfo->{snapdev
};
124 $diskinfo->{type
} = 'file';
126 my (undef, $dir, $ext) = fileparse
($path, qr/\.[^.]*/);
128 $diskinfo->{filename
} = "vm-disk-$ds$ext";
130 if ($mode eq 'snapshot') {
132 my ($srcdev, $lvmpath, $lvmvg, $lvmlv, $fstype) =
133 PVE
::VZDump
::get_lvm_device
($dir, $lvmmap);
135 my $targetdev = PVE
::VZDump
::get_lvm_device
($task->{dumpdir
}, $lvmmap);
137 die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
138 die ("mode failure - wrong lvm mount point '$lvmpath'\n") if $dir !~ m
|/?$lvmpath/?
|;
139 die ("mode failure - unable to dump into snapshot (use option --dumpdir)\n")
140 if $targetdev eq $srcdev;
142 $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
144 my $info = $mountinfo->{$lvmpath};
146 my $snapname = "vzsnap-$hostname-$ind->{$lvmvg}";
147 my $snapdev = "/dev/$lvmvg/$snapname";
148 $mountinfo->{$lvmpath} = $info = {
150 snapname
=> $snapname,
151 mountpoint
=> "/mnt/vzsnap$mountind",
159 $diskinfo->{snapdev
} = $info->{snapdev
};
160 $diskinfo->{snapname
} = $info->{snapname
};
161 $diskinfo->{mountpoint
} = $info->{mountpoint
};
163 $diskinfo->{lvmvg
} = $lvmvg;
164 $diskinfo->{lvmlv
} = $lvmlv;
166 $diskinfo->{fstype
} = $fstype;
167 $diskinfo->{lvmpath
} = $lvmpath;
169 $diskinfo->{snappath
} = $path;
170 $diskinfo->{snappath
} =~ s
|/?$lvmpath/?
|$diskinfo->{mountpoint
}/|;
174 push @{$task->{disks
}}, $diskinfo;
177 $task->{snapshot_count
} = $snapshot_count;
181 my ($self, $vmid) = @_;
183 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
185 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
189 my ($self, $vmid) = @_;
191 $self->cmd ("qm set $vmid --lock backup");
195 my ($self, $vmid) = @_;
197 $self->cmd ("qm unlock $vmid");
201 my ($self, $task, $vmid) = @_;
203 my $opts = $self->{vzdump
}->{opts
};
205 my $wait = $opts->{stopwait
} * 60;
206 # send shutdown and wait
207 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
211 my ($self, $task, $vmid) = @_;
213 $self->cmd ("qm start $vmid --skiplock");
217 my ($self, $task, $vmid) = @_;
219 $self->cmd ("qm suspend $vmid --skiplock");
223 my ($self, $task, $vmid) = @_;
225 $self->cmd ("qm resume $vmid --skiplock");
229 my ($self, $storeid, $name, $size, $srcdev) = @_;
231 my $cmd = "lvcreate --size ${size}M --snapshot --name '$name' '$srcdev'";
235 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
237 # lock shared storage
238 return PVE
::Storage
::cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
247 my ($self, $storeid, $name, $snapdev, $noerr) = @_;
249 my $cmd = ['lvremove', '-f', $snapdev];
251 # loop, because we often get 'LV in use: not deactivating'
252 # we use run_command() because we do not want to log errors here
257 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
258 # lock shared storage
259 return PVE
::Storage
::cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
260 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> {});
263 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> {});
273 $self->loginfo("lvremove failed - trying again in $wait seconds") if $wait >= 8;
280 my ($self, $task, $vmid) = @_;
282 my $opts = $self->{vzdump
}->{opts
};
286 foreach my $di (@{$task->{disks
}}) {
287 if ($di->{type
} eq 'block') {
289 if (-b
$di->{snapdev
}) {
290 $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
291 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);
294 $di->{cleanup_lvm
} = 1;
295 $self->snapshot_alloc ($di->{storeid
}, $di->{snapname
}, $opts->{size
},
296 "/dev/$di->{lvmvg}/$di->{lvmlv}");
298 } elsif ($di->{type
} eq 'file') {
300 next if defined ($mounts->{$di->{mountpoint
}}); # already mounted
302 if (-b
$di->{snapdev
}) {
303 $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
305 $self->cmd_noerr ("umount $di->{mountpoint}");
306 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);
309 mkpath
$di->{mountpoint
}; # create mount point for lvm snapshot
311 $di->{cleanup_lvm
} = 1;
313 $self->snapshot_alloc ($di->{storeid
}, $di->{snapname
}, $opts->{size
},
314 "/dev/$di->{lvmvg}/$di->{lvmlv}");
316 my $mopts = $di->{fstype
} eq 'xfs' ?
"-o nouuid" : '';
318 $di->{snapshot_mount
} = 1;
320 $self->cmd ("mount -n -t $di->{fstype} $mopts $di->{snapdev} $di->{mountpoint}");
322 $mounts->{$di->{mountpoint
}} = 1;
336 my $fh = IO
::File-
>new ($path, "r");
337 die "unable to open '$path' to detect device size\n" if !$fh;
338 my $size = sysseek $fh, 0, 2;
340 die "unable to detect device size for '$path'\n" if !$size;
346 my ($self, $task, $vmid) = @_;
348 my $conffile = PVE
::QemuServer
::config_file
($vmid);
350 my $outfile = "$task->{tmpdir}/qemu-server.conf";
357 $outfd = IO
::File-
>new (">$outfile") ||
358 die "unable to open '$outfile'";
359 $conffd = IO
::File-
>new ($conffile, 'r') ||
360 die "unable open '$conffile'";
362 while (defined (my $line = <$conffd>)) {
363 next if $line =~ m/^\#vzdump\#/; # just to be sure
367 foreach my $di (@{$task->{disks
}}) {
368 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
369 my $size = get_size
($di->{snappath
});
370 my $storeid = $di->{storeid
} || '';
371 print $outfd "#vzdump#map:$di->{virtdev}:$di->{filename}:$size:$storeid:\n";
373 die "internal error";
379 close ($outfd) if $outfd;
380 close ($conffd) if $conffd;
386 my ($self, $task, $vmid, $filename, $comp) = @_;
388 my $conffile = "$task->{tmpdir}/qemu-server.conf";
390 my $opts = $self->{vzdump
}->{opts
};
392 my $starttime = time ();
396 my @filea = ($conffile, 'qemu-server.conf'); # always first file in tar
397 foreach my $di (@{$task->{disks
}}) {
398 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
399 push @filea, $di->{snappath
}, $di->{filename
};
405 my $files = join (' ', map { "'$_'" } @filea);
407 # no sparse file scan when we use compression
408 my $sparse = $comp ?
'' : '-s';
410 my $cmd = "/usr/lib/qemu-server/vmtar $sparse $files";
411 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
412 $cmd .= "|cstream -t $bwl" if $opts->{bwlimit
};
413 $cmd .= "|$comp" if $comp;
415 if ($opts->{stdout
}) {
416 $self->cmd ($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
418 $self->cmd ("$cmd >$filename");
423 my ($self, $task, $vmid) = @_;
425 foreach my $di (@{$task->{disks
}}) {
427 if ($di->{snapshot_mount
}) {
428 $self->cmd_noerr ("umount $di->{mountpoint}");
431 if ($di->{cleanup_lvm
}) {
432 if (-b
$di->{snapdev
}) {
433 if ($di->{type
} eq 'block') {
434 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);
435 } elsif ($di->{type
} eq 'file') {
436 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);