]>
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;
60 PVE
::QemuServer
::foreach_drive
($conf, sub {
61 my ($ds, $drive) = @_;
63 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
65 if (defined($drive->{backup
}) && $drive->{backup
} eq "no") {
66 $self->loginfo("exclude disk '$ds' (backup=no)");
70 my $volid = $drive->{file
};
74 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
76 PVE
::Storage
::activate_storage
($self->{storecfg
}, $storeid);
77 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
84 die "no such volume '$volid'\n" if ! -e
$path;
86 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
87 snappath
=> $path, virtdev
=> $ds };
91 $diskinfo->{type
} = 'block';
93 $diskinfo->{filename
} = "vm-disk-$ds.raw";
95 if ($mode eq 'snapshot') {
96 my ($lvmvg, $lvmlv) = @{$lvmmap->{$path}} if defined ($lvmmap->{$path});
97 die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
99 $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
100 $diskinfo->{snapname
} = "vzsnap-$hostname-$ind->{$lvmvg}";
101 $diskinfo->{snapdev
} = "/dev/$lvmvg/$diskinfo->{snapname}";
102 $diskinfo->{lvmvg
} = $lvmvg;
103 $diskinfo->{lvmlv
} = $lvmlv;
104 $diskinfo->{snappath
} = $diskinfo->{snapdev
};
112 $diskinfo->{type
} = 'file';
114 my (undef, $dir, $ext) = fileparse
($path, qr/\.[^.]*/);
116 $diskinfo->{filename
} = "vm-disk-$ds$ext";
118 if ($mode eq 'snapshot') {
120 my ($srcdev, $lvmpath, $lvmvg, $lvmlv, $fstype) =
121 PVE
::VZDump
::get_lvm_device
($dir, $lvmmap);
123 my $targetdev = PVE
::VZDump
::get_lvm_device
($task->{dumpdir
}, $lvmmap);
125 die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
126 die ("mode failure - wrong lvm mount point '$lvmpath'\n") if $dir !~ m
|/?$lvmpath/?
|;
127 die ("mode failure - unable to dump into snapshot (use option --dumpdir)\n")
128 if $targetdev eq $srcdev;
130 $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
132 my $info = $mountinfo->{$lvmpath};
134 my $snapname = "vzsnap-$hostname-$ind->{$lvmvg}";
135 my $snapdev = "/dev/$lvmvg/$snapname";
136 $mountinfo->{$lvmpath} = $info = {
138 snapname
=> $snapname,
139 mountpoint
=> "/mnt/vzsnap$mountind",
147 $diskinfo->{snapdev
} = $info->{snapdev
};
148 $diskinfo->{snapname
} = $info->{snapname
};
149 $diskinfo->{mountpoint
} = $info->{mountpoint
};
151 $diskinfo->{lvmvg
} = $lvmvg;
152 $diskinfo->{lvmlv
} = $lvmlv;
154 $diskinfo->{fstype
} = $fstype;
155 $diskinfo->{lvmpath
} = $lvmpath;
157 $diskinfo->{snappath
} = $path;
158 $diskinfo->{snappath
} =~ s
|/?$lvmpath/?
|$diskinfo->{mountpoint
}/|;
162 push @{$task->{disks
}}, $diskinfo;
166 $task->{snapshot_count
} = $snapshot_count;
170 my ($self, $vmid) = @_;
172 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
174 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
178 my ($self, $vmid) = @_;
180 $self->cmd ("qm set $vmid --lock backup");
184 my ($self, $vmid) = @_;
186 $self->cmd ("qm unlock $vmid");
190 my ($self, $task, $vmid) = @_;
192 my $opts = $self->{vzdump
}->{opts
};
194 my $wait = $opts->{stopwait
} * 60;
195 # send shutdown and wait
196 $self->cmd ("qm shutdown $vmid --skiplock && qm wait $vmid --timeout $wait");
200 my ($self, $task, $vmid) = @_;
202 $self->cmd ("qm start $vmid --skiplock");
206 my ($self, $task, $vmid) = @_;
208 $self->cmd ("qm suspend $vmid --skiplock");
212 my ($self, $task, $vmid) = @_;
214 $self->cmd ("qm resume $vmid --skiplock");
218 my ($self, $volid, $name, $size, $srcdev) = @_;
220 my $cmd = "lvcreate --size ${size}M --snapshot --name '$name' '$srcdev'";
222 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
225 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
227 # lock shared storage
228 return PVE
::Storage
::cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
230 if ($scfg->{type
} eq 'lvm') {
231 my $vg = $scfg->{vgname
};
236 die "can't allocate snapshot on storage type '$scfg->{type}'\n";
245 my ($self, $volid, $name, $snapdev, $noerr) = @_;
247 my $cmd = "lvremove -f '$snapdev'";
250 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
253 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
255 # lock shared storage
256 return PVE
::Storage
::cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
258 if ($scfg->{type
} eq 'lvm') {
259 my $vg = $scfg->{vgname
};
264 die "can't allocate snapshot on storage type '$scfg->{type}'\n";
272 $self->logerr ($@) if $@;
276 my ($self, $task, $vmid) = @_;
278 my $opts = $self->{vzdump
}->{opts
};
282 foreach my $di (@{$task->{disks
}}) {
283 if ($di->{type
} eq 'block') {
285 if (-b
$di->{snapdev
}) {
286 $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
287 $self->snapshot_free ($di->{volid
}, $di->{snapname
}, $di->{snapdev
}, 1);
290 $di->{cleanup_lvm
} = 1;
291 $self->snapshot_alloc ($di->{volid
}, $di->{snapname
}, $opts->{size
},
292 "/dev/$di->{lvmvg}/$di->{lvmlv}");
294 } elsif ($di->{type
} eq 'file') {
296 next if defined ($mounts->{$di->{mountpoint
}}); # already mounted
298 # note: files are never on shared storage, so we use $di->{path} instead
299 # of $di->{volid} (avoid PVE:Storage calls because path start with /)
301 if (-b
$di->{snapdev
}) {
302 $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
304 $self->cmd_noerr ("umount $di->{mountpoint}");
306 $self->snapshot_free ($di->{path
}, $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->{path
}, $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 -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) = @_;
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 my $cmd = "/usr/lib/qemu-server/vmtar $files";
408 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
409 $cmd .= "|cstream -t $bwl" if $opts->{bwlimit
};
410 $cmd .= "|gzip" if $opts->{compress
};
412 if ($opts->{stdout
}) {
413 $self->cmd ($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
415 $self->cmd ("$cmd >$filename");
420 my ($self, $task, $vmid) = @_;
422 foreach my $di (@{$task->{disks
}}) {
424 if ($di->{snapshot_mount
}) {
425 $self->cmd_noerr ("umount $di->{mountpoint}");
428 if ($di->{cleanup_lvm
}) {
429 if (-b
$di->{snapdev
}) {
430 if ($di->{type
} eq 'block') {
431 $self->snapshot_free ($di->{volid
}, $di->{snapname
}, $di->{snapdev
}, 1);
432 } elsif ($di->{type
} eq 'file') {
433 $self->snapshot_free ($di->{path
}, $di->{snapname
}, $di->{snapdev
}, 1);