]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
09c4d470a13ac443d72edffd01750e377f2d36d2
1 package PVE
::VZDump
::LXC
;
9 use PVE
::Cluster
qw(cfs_read_file);
17 use base qw
(PVE
::VZDump
::Plugin
);
19 my $default_mount_point = "/mnt/vzsnap0";
22 my ($self, $task, $to, $text, $first) = @_;
24 my $disks = $task->{disks
};
25 my $from = $disks->[0]->{dir
} . '/';
26 $self->loginfo ("starting $text sync $from to $to");
28 my $opts = $self->{vzdump
}->{opts
};
30 my @xattr = $task->{no_xattrs
} ?
() : ('-X', '-A');
32 my $rsync = ['rsync', '--stats', @xattr, '--numeric-ids',
33 '-aH', '--delete', '--no-whole-file',
34 ($first ?
'--sparse' : '--inplace'),
35 '--one-file-system', '--relative'];
36 push @$rsync, "--bwlimit=$opts->{bwlimit}" if $opts->{bwlimit
};
37 push @$rsync, map { "--exclude=$_" } @{$self->{vzdump
}->{findexcl
}};
38 push @$rsync, map { "--exclude=$_" } @{$task->{exclude_dirs
}};
40 my $starttime = time();
41 # See the rsync(1) manpage for --relative in conjunction with /./ in paths.
42 # This is the only way to have exclude-dirs work together with the
43 # --one-file-system option.
44 # This way we can pass multiple source paths and tell rsync which directory
45 # they're supposed to be relative to.
46 # Otherwise with eg. using multiple rsync commands means the --exclude
47 # directives need to be modified for every command as they are meant to be
48 # relative to the rootdir, while rsync treats them as relative to the
50 foreach my $disk (@$disks) {
51 push @$rsync, "$from/.$disk->{mp}";
53 $self->cmd([@$rsync, $to]);
54 my $delay = time () - $starttime;
56 $self->loginfo ("$text sync finished ($delay seconds)");
60 my ($class, $vzdump) = @_;
62 PVE
::VZDump
::check_bin
('lxc-stop');
63 PVE
::VZDump
::check_bin
('lxc-start');
64 PVE
::VZDump
::check_bin
('lxc-freeze');
65 PVE
::VZDump
::check_bin
('lxc-unfreeze');
69 $self->{vzdump
} = $vzdump;
70 $self->{storecfg
} = PVE
::Storage
::config
();
72 $self->{vmlist
} = PVE
::LXC
::config_list
();
82 my ($self, $vmid) = @_;
84 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
86 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
89 my $check_mountpoint_empty = sub {
90 my ($mountpoint) = @_;
92 die "mount point '$mountpoint' is not a directory\n" if ! -d
$mountpoint;
94 PVE
::Tools
::dir_glob_foreach
($mountpoint, qr/.*/, sub {
96 return if $entry eq '.' || $entry eq '..';
97 die "mount point '$mountpoint' not empty\n";
102 my ($self, $task, $vmid, $mode) = @_;
104 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::Config-
>load_config($vmid);
105 my $storage_cfg = $self->{storecfg
};
107 $self->loginfo("CT Name: $conf->{hostname}")
108 if defined($conf->{hostname
});
110 my $running = PVE
::LXC
::check_running
($vmid);
112 my $disks = $task->{disks
} = [];
113 my $exclude_dirs = $task->{exclude_dirs
} = [];
115 $task->{hostname
} = $conf->{'hostname'} || "CT$vmid";
117 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
118 $task->{userns_cmd
} = PVE
::LXC
::userns_command
($id_map);
119 $task->{rootuid
} = $rootuid;
120 $task->{rootgid
} = $rootgid;
122 my $volids = $task->{volids
} = [];
123 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
124 my ($name, $data) = @_;
125 my $volid = $data->{volume
};
126 my $mount = $data->{mp
};
127 my $type = $data->{type
};
129 return if !$volid || !$mount;
131 if (!PVE
::LXC
::Config-
>mountpoint_backup_enabled($name, $data)) {
132 push @$exclude_dirs, $mount;
133 $self->loginfo("excluding $type mount point $name ('$mount') from backup");
137 $data->{name
} = $name;
139 # immutable raw base images need RO mount
140 if ($conf->{template
} && !defined($data->{ro
})) {
144 push @$volids, $volid
145 if $type eq 'volume';
148 if ($mode eq 'snapshot') {
149 if (!PVE
::LXC
::Config-
>has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
150 die "mode failure - some volumes do not support snapshots\n";
154 if ($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
}) {
155 $self->loginfo("found old vzdump snapshot (force removal)");
156 PVE
::LXC
::Config-
>lock_config($vmid, sub {
157 $self->unlock_vm($vmid);
158 PVE
::LXC
::Config-
>snapshot_delete($vmid, 'vzdump', 1);
159 $self->lock_vm($vmid);
163 my $rootdir = $default_mount_point;
165 &$check_mountpoint_empty($rootdir);
167 # set snapshot_count (freezes CT if snapshot_count > 1)
168 $task->{snapshot_count
} = scalar(@$volids);
169 } elsif ($mode eq 'stop') {
170 my $rootdir = $default_mount_point;
172 &$check_mountpoint_empty($rootdir);
173 } elsif ($mode eq 'suspend') {
174 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
175 foreach my $disk (@$disks) {
176 $disk->{dir
} = "/proc/$pid/root$disk->{mp}";
178 $task->{snapdir
} = $task->{tmpdir
};
180 unlock_vm
($self, $vmid);
181 die "unknown mode '$mode'\n"; # should not happen
184 if ($mode ne 'suspend') {
185 # If we perform mount operations, let's unshare the mount namespace
186 # to not influence the running host.
187 PVE
::Tools
::unshare
(PVE
::Tools
::CLONE_NEWNS
);
188 PVE
::Tools
::run_command
(['mount', '--make-rslave', '/']);
193 my ($self, $vmid) = @_;
195 PVE
::LXC
::Config-
>set_lock($vmid, 'backup');
199 my ($self, $vmid) = @_;
201 PVE
::LXC
::Config-
>remove_lock($vmid, 'backup')
205 my ($self, $task, $vmid) = @_;
207 $self->loginfo("create storage snapshot 'vzdump'");
209 # todo: freeze/unfreeze if we have more than one volid
210 PVE
::LXC
::Config-
>lock_config($vmid, sub {
211 $self->unlock_vm($vmid);
212 PVE
::LXC
::Config-
>snapshot_create($vmid, 'vzdump', 0, "vzdump backup snapshot");
213 $self->lock_vm($vmid);
215 $task->{cleanup
}->{remove_snapshot
} = 1;
218 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::Config-
>load_config($vmid);
219 die "unable to read vzdump snapshot config - internal error"
220 if !($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
});
222 my $disks = $task->{disks
};
223 my $volids = $task->{volids
};
225 my $rootdir = $default_mount_point;
226 my $storage_cfg = $self->{storecfg
};
228 PVE
::Storage
::activate_volumes
($storage_cfg, $volids, 'vzdump');
229 foreach my $disk (@$disks) {
230 $disk->{dir
} = "${rootdir}$disk->{mp}";
231 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg, 'vzdump', $task->{rootuid
}, $task->{rootgid
});
234 $task->{snapdir
} = $rootdir;
237 sub copy_data_phase1
{
238 my ($self, $task) = @_;
240 if (my $mntinfo = PVE
::VZDump
::get_mount_info
($task->{snapdir
})) {
241 if ($mntinfo->{fstype
} =~ /^nfs4?/) {
243 "temporary directory is on NFS, disabling xattr and acl"
244 ." support, consider configuring a local tmpdir via"
245 ." /etc/vzdump.conf\n");
246 $task->{no_xattrs
} = 1;
250 $self->$rsync_vm($task, $task->{snapdir
}, "first", 1);
253 sub copy_data_phase2
{
254 my ($self, $task) = @_;
256 $self->$rsync_vm($task, $task->{snapdir
}, "final", 0);
260 my ($self, $task, $vmid) = @_;
262 my $opts = $self->{vzdump
}->{opts
};
263 my $timeout = $opts->{stopwait
} * 60;
265 PVE
::LXC
::vm_stop
($vmid, 0, $timeout);
269 my ($self, $task, $vmid) = @_;
271 $self->cmd(['systemctl', 'start', "pve-container\@$vmid"]);
275 my ($self, $task, $vmid) = @_;
277 $self->cmd ("lxc-freeze -n $vmid");
281 my ($self, $task, $vmid) = @_;
283 $self->cmd ("lxc-unfreeze -n $vmid");
287 my ($self, $task, $vmid) = @_;
289 my $opts = $self->{vzdump
}->{opts
};
291 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
292 delete $conf->{lock};
293 delete $conf->{snapshots
};
294 delete $conf->{parent
};
295 delete $conf->{pending
};
297 my $tmpdir = $task->{tmpdir
};
299 mkpath
"$tmpdir/etc/vzdump/";
301 PVE
::Tools
::file_set_contents
("$tmpdir/etc/vzdump/pct.conf", PVE
::LXC
::Config
::write_pct_config
("/lxc/$vmid.conf", $conf));
303 my $firewall ="/etc/pve/firewall/$vmid.fw";
304 my $fwconftmp = "$tmpdir/etc/vzdump/pct.fw";
306 if ($opts->{scfg
}->{type
} eq 'pbs') {
307 # fixme: do not store pct.conf and fw.conf into $tmpdir
309 PVE
::Tools
::file_copy
($firewall, $fwconftmp);
313 PVE
::Tools
::file_copy
($firewall, $fwconftmp);
315 PVE
::Tools
::file_set_contents
($fwconftmp, '');
322 my ($self, $task, $vmid, $filename, $comp) = @_;
324 my $disks = $task->{disks
};
327 if ($task->{mode
} eq 'stop') {
328 my $storage_cfg = $self->{storecfg
};
330 PVE
::Storage
::activate_volumes
($storage_cfg, $task->{volids
});
332 my $rootdir = $default_mount_point;
333 foreach my $disk (@$disks) {
334 $disk->{dir
} = "${rootdir}$disk->{mp}";
335 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg, undef, $task->{rootuid
}, $task->{rootgid
});
336 # add every enabled mountpoint (since we use --one-file-system)
337 # mp already starts with a / so we only need to add the dot
338 push @sources, ".$disk->{mp}";
340 $task->{snapdir
} = $rootdir;
341 } elsif ($task->{mode
} eq 'snapshot') {
342 # mounting the vzdump snapshots and setting $snapdir is already done,
343 # but we need to include all mountpoints here!
344 foreach my $disk (@$disks) {
345 push @sources, ".$disk->{mp}";
348 # the data was rsynced to a temporary location, only use '.' to avoid
349 # having mountpoints duplicated
353 my $opts = $self->{vzdump
}->{opts
};
354 my $snapdir = $task->{snapdir
};
355 my $tmpdir = $task->{tmpdir
};
357 my $userns_cmd = $task->{userns_cmd
};
359 if ($opts->{scfg
}->{type
} eq 'pbs') {
361 my $rootdir = $default_mount_point;
364 push @$param, "pct.conf:$tmpdir/etc/vzdump/pct.conf";
366 my $fw_conf = "$tmpdir/etc/vzdump/pct.fw";
368 push @$param, "fw.conf:$fw_conf";
371 push @$param, "root.pxar:$rootdir";
373 foreach my $disk (@$disks) {
374 push @$param, '--include-dev', $disk->{dir
};
377 push @$param, '--skip-lost-and-found' if $userns_cmd;
379 push @$param, '--backup-type', 'ct';
380 push @$param, '--backup-id', $vmid;
381 push @$param, '--backup-time', $task->{backup_time
};
383 my $logfunc = sub { my $line = shift; $self->loginfo($line); };
384 PVE
::Storage
::PBSPlugin
::run_raw_client_cmd
(
385 $opts->{scfg
}, $opts->{storage
}, 'backup', $param,
386 logfunc
=> $logfunc, userns_cmd
=> $userns_cmd);
390 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
391 @PVE::Storage
::Plugin
::COMMON_TAR_FLAGS
,
392 '--one-file-system', '--warning=no-file-ignored'];
394 # note: --remove-files does not work because we do not
395 # backup all files (filters). tar complains:
396 # Cannot rmdir: Directory not empty
397 # we disable this optimization for now
398 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
399 # push @$tar, "--remove-files"; # try to save space
402 # The directory parameter can give an alternative directory as source.
403 # the second parameter gives the structure in the tar.
404 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
405 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw
};
406 push @$tar, "--directory=$snapdir";
407 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
408 push @$tar, '--anchored';
409 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump
}->{findexcl
}};
411 push @$tar, @sources;
415 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
416 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit
};
417 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
419 if ($opts->{stdout
}) {
420 $self->cmd($cmd, output
=> ">&" . fileno($opts->{stdout
}));
422 push @{$cmd->[-1]}, \
(">" . PVE
::Tools
::shellquote
($filename));
429 my ($self, $task, $vmid) = @_;
431 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
433 if ($task->{mode
} ne 'suspend') {
434 my $rootdir = $default_mount_point;
435 my $disks = $task->{disks
};
436 foreach my $disk (reverse @$disks) {
437 PVE
::Tools
::run_command
(['umount', '-l', '-d', $disk->{dir
}]) if $disk->{dir
};
441 if ($task->{cleanup
}->{remove_snapshot
}) {
442 $self->loginfo("remove vzdump snapshot");
443 PVE
::LXC
::Config-
>snapshot_delete($vmid, 'vzdump', 0);