]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
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');
67 $self->{vzdump
} = $vzdump;
68 $self->{storecfg
} = PVE
::Storage
::config
();
70 $self->{vmlist
} = PVE
::LXC
::config_list
();
80 my ($self, $vmid) = @_;
82 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
84 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
87 my $check_mountpoint_empty = sub {
88 my ($mountpoint) = @_;
90 die "mount point '$mountpoint' is not a directory\n" if ! -d
$mountpoint;
92 PVE
::Tools
::dir_glob_foreach
($mountpoint, qr/.*/, sub {
94 return if $entry eq '.' || $entry eq '..';
95 die "mount point '$mountpoint' not empty\n";
100 my ($self, $task, $vmid, $mode) = @_;
102 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::Config-
>load_config($vmid);
103 my $storage_cfg = $self->{storecfg
};
105 $self->loginfo("CT Name: $conf->{hostname}")
106 if defined($conf->{hostname
});
108 my $running = PVE
::LXC
::check_running
($vmid);
110 my $disks = $task->{disks
} = [];
111 my $exclude_dirs = $task->{exclude_dirs
} = [];
113 $task->{hostname
} = $conf->{'hostname'} || "CT$vmid";
115 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
116 $task->{userns_cmd
} = PVE
::LXC
::userns_command
($id_map);
117 $task->{rootuid
} = $rootuid;
118 $task->{rootgid
} = $rootgid;
120 my $volids = $task->{volids
} = [];
121 PVE
::LXC
::Config-
>foreach_volume($conf, sub {
122 my ($name, $data) = @_;
123 my $volid = $data->{volume
};
124 my $mount = $data->{mp
};
125 my $type = $data->{type
};
127 return if !$volid || !$mount;
129 if (!PVE
::LXC
::Config-
>mountpoint_backup_enabled($name, $data)) {
130 push @$exclude_dirs, $mount;
131 $self->loginfo("excluding $type mount point $name ('$mount') from backup");
135 $data->{name
} = $name;
137 # immutable raw base images need RO mount
138 if ($conf->{template
} && !defined($data->{ro
})) {
142 push @$volids, $volid
143 if $type eq 'volume';
146 if ($mode eq 'snapshot') {
147 if (!PVE
::LXC
::Config-
>has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
148 die "mode failure - some volumes do not support snapshots\n";
152 if ($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
}) {
153 $self->loginfo("found old vzdump snapshot (force removal)");
154 PVE
::LXC
::Config-
>lock_config($vmid, sub {
155 $self->unlock_vm($vmid);
156 PVE
::LXC
::Config-
>snapshot_delete($vmid, 'vzdump', 1);
157 $self->lock_vm($vmid);
161 my $rootdir = $default_mount_point;
163 &$check_mountpoint_empty($rootdir);
165 # set snapshot_count (freezes CT if snapshot_count > 1)
166 $task->{snapshot_count
} = scalar(@$volids);
167 } elsif ($mode eq 'stop') {
168 my $rootdir = $default_mount_point;
170 &$check_mountpoint_empty($rootdir);
171 } elsif ($mode eq 'suspend') {
172 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
173 foreach my $disk (@$disks) {
174 $disk->{dir
} = "/proc/$pid/root$disk->{mp}";
176 $task->{snapdir
} = $task->{tmpdir
};
178 unlock_vm
($self, $vmid);
179 die "unknown mode '$mode'\n"; # should not happen
182 if ($mode ne 'suspend') {
183 # If we perform mount operations, let's unshare the mount namespace
184 # to not influence the running host.
185 PVE
::Tools
::unshare
(PVE
::Tools
::CLONE_NEWNS
);
186 PVE
::Tools
::run_command
(['mount', '--make-rslave', '/']);
191 my ($self, $vmid) = @_;
193 PVE
::LXC
::Config-
>set_lock($vmid, 'backup');
197 my ($self, $vmid) = @_;
199 PVE
::LXC
::Config-
>remove_lock($vmid, 'backup')
203 my ($self, $task, $vmid) = @_;
205 $self->loginfo("create storage snapshot 'vzdump'");
207 # todo: freeze/unfreeze if we have more than one volid
208 PVE
::LXC
::Config-
>lock_config($vmid, sub {
209 $self->unlock_vm($vmid);
210 PVE
::LXC
::Config-
>snapshot_create($vmid, 'vzdump', 0, "vzdump backup snapshot");
211 $self->lock_vm($vmid);
213 $task->{cleanup
}->{remove_snapshot
} = 1;
216 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::Config-
>load_config($vmid);
217 die "unable to read vzdump snapshot config - internal error"
218 if !($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
});
220 my $disks = $task->{disks
};
221 my $volids = $task->{volids
};
223 my $rootdir = $default_mount_point;
224 my $storage_cfg = $self->{storecfg
};
226 PVE
::Storage
::activate_volumes
($storage_cfg, $volids, 'vzdump');
227 foreach my $disk (@$disks) {
228 $disk->{dir
} = "${rootdir}$disk->{mp}";
229 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg, 'vzdump', $task->{rootuid
}, $task->{rootgid
});
232 $task->{snapdir
} = $rootdir;
235 sub copy_data_phase1
{
236 my ($self, $task) = @_;
238 if (my $mntinfo = PVE
::VZDump
::get_mount_info
($task->{snapdir
})) {
239 if ($mntinfo->{fstype
} =~ /^nfs4?/) {
241 "temporary directory is on NFS, disabling xattr and acl"
242 ." support, consider configuring a local tmpdir via"
243 ." /etc/vzdump.conf\n");
244 $task->{no_xattrs
} = 1;
248 $self->$rsync_vm($task, $task->{snapdir
}, "first", 1);
251 sub copy_data_phase2
{
252 my ($self, $task) = @_;
254 $self->$rsync_vm($task, $task->{snapdir
}, "final", 0);
258 my ($self, $task, $vmid) = @_;
260 my $opts = $self->{vzdump
}->{opts
};
261 my $timeout = $opts->{stopwait
} * 60;
263 PVE
::LXC
::vm_stop
($vmid, 0, $timeout);
267 my ($self, $task, $vmid) = @_;
269 $self->cmd(['systemctl', 'start', "pve-container\@$vmid"]);
273 my ($self, $task, $vmid) = @_;
275 PVE
::LXC
::freeze
($vmid);
279 my ($self, $task, $vmid) = @_;
281 PVE
::LXC
::thaw
($vmid);
285 my ($self, $task, $vmid) = @_;
287 my $opts = $self->{vzdump
}->{opts
};
289 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
290 delete $conf->{lock};
291 delete $conf->{snapshots
};
292 delete $conf->{parent
};
293 delete $conf->{pending
};
295 my $tmpdir = $task->{tmpdir
};
297 mkpath
"$tmpdir/etc/vzdump/";
299 PVE
::Tools
::file_set_contents
("$tmpdir/etc/vzdump/pct.conf", PVE
::LXC
::Config
::write_pct_config
("/lxc/$vmid.conf", $conf));
301 my $firewall ="/etc/pve/firewall/$vmid.fw";
302 my $fwconftmp = "$tmpdir/etc/vzdump/pct.fw";
304 if ($self->{vzdump
}->{opts
}->{pbs
}) {
305 # fixme: do not store pct.conf and fw.conf into $tmpdir
307 PVE
::Tools
::file_copy
($firewall, $fwconftmp);
311 PVE
::Tools
::file_copy
($firewall, $fwconftmp);
313 PVE
::Tools
::file_set_contents
($fwconftmp, '');
320 my ($self, $task, $vmid, $filename, $comp) = @_;
322 my $disks = $task->{disks
};
325 if ($task->{mode
} eq 'stop') {
326 my $storage_cfg = $self->{storecfg
};
328 PVE
::Storage
::activate_volumes
($storage_cfg, $task->{volids
});
330 my $rootdir = $default_mount_point;
331 foreach my $disk (@$disks) {
332 $disk->{dir
} = "${rootdir}$disk->{mp}";
333 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg, undef, $task->{rootuid
}, $task->{rootgid
});
334 # add every enabled mountpoint (since we use --one-file-system)
335 # mp already starts with a / so we only need to add the dot
336 push @sources, ".$disk->{mp}";
338 $task->{snapdir
} = $rootdir;
339 } elsif ($task->{mode
} eq 'snapshot') {
340 # mounting the vzdump snapshots and setting $snapdir is already done,
341 # but we need to include all mountpoints here!
342 foreach my $disk (@$disks) {
343 push @sources, ".$disk->{mp}";
346 # the data was rsynced to a temporary location, only use '.' to avoid
347 # having mountpoints duplicated
351 my $opts = $self->{vzdump
}->{opts
};
352 my $snapdir = $task->{snapdir
};
353 my $tmpdir = $task->{tmpdir
};
355 my $userns_cmd = $task->{userns_cmd
};
357 if ($self->{vzdump
}->{opts
}->{pbs
}) {
359 my $rootdir = $default_mount_point;
362 push @$param, "pct.conf:$tmpdir/etc/vzdump/pct.conf";
364 my $fw_conf = "$tmpdir/etc/vzdump/pct.fw";
366 push @$param, "fw.conf:$fw_conf";
369 push @$param, "root.pxar:$rootdir";
371 foreach my $disk (@$disks) {
372 push @$param, '--include-dev', $disk->{dir
};
375 push @$param, '--skip-lost-and-found' if $userns_cmd;
377 push @$param, '--backup-type', 'ct';
378 push @$param, '--backup-id', $vmid;
379 push @$param, '--backup-time', $task->{backup_time
};
381 my $logfunc = sub { my $line = shift; $self->loginfo($line); };
382 PVE
::Storage
::PBSPlugin
::run_raw_client_cmd
(
383 $opts->{scfg
}, $opts->{storage
}, 'backup', $param,
384 logfunc
=> $logfunc, userns_cmd
=> $userns_cmd);
388 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
389 @PVE::Storage
::Plugin
::COMMON_TAR_FLAGS
,
390 '--one-file-system', '--warning=no-file-ignored'];
392 # note: --remove-files does not work because we do not
393 # backup all files (filters). tar complains:
394 # Cannot rmdir: Directory not empty
395 # we disable this optimization for now
396 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
397 # push @$tar, "--remove-files"; # try to save space
400 # The directory parameter can give an alternative directory as source.
401 # the second parameter gives the structure in the tar.
402 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
403 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw
};
404 push @$tar, "--directory=$snapdir";
405 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
406 push @$tar, '--anchored';
407 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump
}->{findexcl
}};
409 push @$tar, @sources;
413 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
414 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit
};
415 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
417 if ($opts->{stdout
}) {
418 $self->cmd($cmd, output
=> ">&" . fileno($opts->{stdout
}));
420 push @{$cmd->[-1]}, \
(">" . PVE
::Tools
::shellquote
($filename));
427 my ($self, $task, $vmid) = @_;
429 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
431 if ($task->{mode
} ne 'suspend') {
432 my $rootdir = $default_mount_point;
433 my $disks = $task->{disks
};
434 foreach my $disk (reverse @$disks) {
435 PVE
::Tools
::run_command
(['umount', '-l', '-d', $disk->{dir
}]) if $disk->{dir
};
439 if ($task->{cleanup
}->{remove_snapshot
}) {
440 $self->loginfo("remove vzdump snapshot");
441 PVE
::LXC
::Config-
>snapshot_delete($vmid, 'vzdump', 0);