]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
effd0ec4855db914ce6247743900c9f6056b22b9
1 package PVE
::VZDump
::LXC
;
8 use PVE
::Cluster
qw(cfs_read_file);
14 use base qw
(PVE
::VZDump
::Plugin
);
16 my $default_mount_point = "/mnt/vzsnap0";
19 my ($self, $task, $to, $text) = @_;
21 my $disks = $task->{disks
};
22 my $from = $disks->[0]->{dir
} . '/';
23 $self->loginfo ("starting $text sync $from to $to");
25 my $opts = $self->{vzdump
}->{opts
};
27 my @xattr = $task->{no_xattrs
} ?
() : ('-X', '-A');
29 my $rsync = ['rsync', '--stats', @xattr, '--numeric-ids',
30 '-aH', '--delete', '--no-whole-file', '--inplace',
31 '--one-file-system', '--relative'];
32 push @$rsync, "--bwlimit=$opts->{bwlimit}" if $opts->{bwlimit
};
33 push @$rsync, map { "--exclude=$_" } @{$self->{vzdump
}->{findexcl
}};
34 push @$rsync, map { "--exclude=$_" } @{$task->{exclude_dirs
}};
36 my $starttime = time();
37 # See the rsync(1) manpage for --relative in conjunction with /./ in paths.
38 # This is the only way to have exclude-dirs work together with the
39 # --one-file-system option.
40 # This way we can pass multiple source paths and tell rsync which directory
41 # they're supposed to be relative to.
42 # Otherwise with eg. using multiple rsync commands means the --exclude
43 # directives need to be modified for every command as they are meant to be
44 # relative to the rootdir, while rsync treats them as relative to the
46 foreach my $disk (@$disks) {
47 push @$rsync, "$from/.$disk->{mp}";
49 $self->cmd([@$rsync, $to]);
50 my $delay = time () - $starttime;
52 $self->loginfo ("$text sync finished ($delay seconds)");
56 my ($class, $vzdump) = @_;
58 PVE
::VZDump
::check_bin
('lxc-stop');
59 PVE
::VZDump
::check_bin
('lxc-start');
60 PVE
::VZDump
::check_bin
('lxc-freeze');
61 PVE
::VZDump
::check_bin
('lxc-unfreeze');
65 $self->{vzdump
} = $vzdump;
66 $self->{storecfg
} = PVE
::Storage
::config
();
68 $self->{vmlist
} = PVE
::LXC
::config_list
();
78 my ($self, $vmid) = @_;
80 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
82 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
85 my $check_mountpoint_empty = sub {
86 my ($mountpoint) = @_;
88 die "mountpoint '$mountpoint' is not a directory\n" if ! -d
$mountpoint;
90 PVE
::Tools
::dir_glob_foreach
($mountpoint, qr/.*/, sub {
92 return if $entry eq '.' || $entry eq '..';
93 die "mountpoint '$mountpoint' not empty\n";
98 my ($self, $task, $vmid, $mode) = @_;
100 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::load_config
($vmid);
101 my $storage_cfg = $self->{storecfg
};
103 my $running = PVE
::LXC
::check_running
($vmid);
105 my $disks = $task->{disks
} = [];
106 my $exclude_dirs = $task->{exclude_dirs
} = [];
107 my $volids = $task->{volids
} = [];
109 $task->{hostname
} = $conf->{'hostname'} || "CT$vmid";
111 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
112 $task->{userns_cmd
} = PVE
::LXC
::userns_command
($id_map);
114 my $volids = $task->{volids
} = [];
115 PVE
::LXC
::foreach_mountpoint
($conf, sub {
116 my ($name, $data) = @_;
117 my $volid = $data->{volume
};
118 my $mount = $data->{mp
};
119 my $type = $data->{type
};
121 return if !$volid || !$mount;
123 if ($name ne 'rootfs' && !$data->{backup
}) {
124 push @$exclude_dirs, $mount;
129 push @$volids, $volid
130 if $type eq 'volume';
133 if ($mode eq 'snapshot') {
134 if (!PVE
::LXC
::has_feature
('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
135 die "mode failure - some volumes do not support snapshots\n";
138 unlock_vm
($self, $vmid);
140 if ($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
}) {
141 $self->loginfo("found old vzdump snapshot (force removal)");
142 PVE
::LXC
::snapshot_delete
($vmid, 'vzdump', 1);
145 my $rootdir = $default_mount_point;
147 &$check_mountpoint_empty($rootdir);
149 # set snapshot_count (freezes CT if snapshot_count > 1)
150 $task->{snapshot_count
} = scalar(@$volids);
151 } elsif ($mode eq 'stop') {
152 my $rootdir = $default_mount_point;
154 &$check_mountpoint_empty($rootdir);
155 PVE
::Storage
::activate_volumes
($storage_cfg, $volids);
156 } elsif ($mode eq 'suspend') {
157 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
158 foreach my $disk (@$disks) {
159 $disk->{dir
} = "/proc/$pid/root$disk->{mp}";
161 $task->{snapdir
} = $task->{tmpdir
};
163 unlock_vm
($self, $vmid);
164 die "unknown mode '$mode'\n"; # should not happen
167 if ($mode ne 'suspend') {
168 # If we perform mount operations, let's unshare the mount namespace
169 # to not influence the running host.
170 PVE
::Tools
::unshare
(PVE
::Tools
::CLONE_NEWNS
);
171 PVE
::Tools
::run_command
(['mount', '--make-rprivate', '/']);
176 my ($self, $vmid) = @_;
178 my $lockconfig = sub {
179 my ($self, $vmid) = @_;
181 my $conf = PVE
::LXC
::load_config
($vmid);
183 PVE
::LXC
::check_lock
($conf);
184 $conf->{lock} = 'backup';
186 PVE
::LXC
::write_config
($vmid, $conf);
189 PVE
::LXC
::lock_config
($vmid, $lockconfig, ($self, $vmid));
193 my ($self, $vmid) = @_;
195 my $unlockconfig = sub {
196 my ($self, $vmid) = @_;
198 my $conf = PVE
::LXC
::load_config
($vmid);
200 if ($conf->{lock} && $conf->{lock} eq 'backup') {
201 delete $conf->{lock};
202 PVE
::LXC
::write_config
($vmid, $conf);
206 PVE
::LXC
::lock_config
($vmid, $unlockconfig, ($self, $vmid));
210 my ($self, $task, $vmid) = @_;
212 $self->loginfo("create storage snapshot 'vzdump'");
214 # todo: freeze/unfreeze if we have more than one volid
215 PVE
::LXC
::snapshot_create
($vmid, 'vzdump', 0, "vzdump backup snapshot");
216 $task->{cleanup
}->{remove_snapshot
} = 1;
219 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::load_config
($vmid);
220 die "unable to read vzdump snapshot config - internal error"
221 if !($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
});
223 my $disks = $task->{disks
};
224 my $volids = $task->{volids
};
226 my $rootdir = $default_mount_point;
227 my $storage_cfg = $self->{storecfg
};
229 PVE
::Storage
::activate_volumes
($storage_cfg, $volids, 'vzdump');
230 foreach my $disk (@$disks) {
231 $disk->{dir
} = "${rootdir}$disk->{mp}";
232 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg, 'vzdump');
235 $task->{snapdir
} = $rootdir;
238 sub copy_data_phase1
{
239 my ($self, $task) = @_;
241 if (my $mntinfo = PVE
::VZDump
::get_mount_info
($task->{snapdir
})) {
242 if ($mntinfo->{fstype
} =~ /^nfs4?/) {
243 warn "temporary directory is on NFS, disabling xattr and acl support"
244 . ", consider configuring a local tmpdir via /etc/vzdump.conf\n";
245 $task->{no_xattrs
} = 1;
249 $self->$rsync_vm($task, $task->{snapdir
}, "first");
252 sub copy_data_phase2
{
253 my ($self, $task) = @_;
255 $self->$rsync_vm($task, $task->{snapdir
}, "final");
259 my ($self, $task, $vmid) = @_;
261 $self->cmd("lxc-stop -n $vmid");
263 # make sure container is stopped
264 $self->cmd("lxc-wait -n $vmid -s STOPPED");
268 my ($self, $task, $vmid) = @_;
270 $self->cmd ("lxc-start -n $vmid");
274 my ($self, $task, $vmid) = @_;
276 $self->cmd ("lxc-freeze -n $vmid");
280 my ($self, $task, $vmid) = @_;
282 $self->cmd ("lxc-unfreeze -n $vmid");
286 my ($self, $task, $vmid) = @_;
288 my $tmpdir = $task->{tmpdir
};
290 mkpath
"$tmpdir/etc/vzdump/";
292 my $conf = PVE
::LXC
::load_config
($vmid);
293 delete $conf->{lock};
294 delete $conf->{snapshots
};
295 delete $conf->{'pve.parent'};
297 PVE
::Tools
::file_set_contents
("$tmpdir/etc/vzdump/pct.conf", PVE
::LXC
::write_pct_config
("/lxc/$vmid.conf", $conf));
299 my $firewall ="/etc/pve/firewall/$vmid.fw";
301 PVE
::Tools
::file_copy
($firewall, "$tmpdir/etc/vzdump/pct.fw");
307 my ($self, $task, $vmid, $filename, $comp) = @_;
309 my $disks = $task->{disks
};
312 if ($task->{mode
} eq 'stop') {
313 my $rootdir = $default_mount_point;
314 my $storage_cfg = $self->{storecfg
};
315 foreach my $disk (@$disks) {
316 $disk->{dir
} = "${rootdir}$disk->{mp}";
317 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg);
318 # add every enabled mountpoint (since we use --one-file-system)
319 # mp already starts with a / so we only need to add the dot
320 push @sources, ".$disk->{mp}";
322 $task->{snapdir
} = $rootdir;
323 } elsif ($task->{mode
} eq 'snapshot') {
324 # mounting the vzdump snapshots and setting $snapdir is already done,
325 # but we need to include all mountpoints here!
326 foreach my $disk (@$disks) {
327 push @sources, ".$disk->{mp}";
330 # the data was rsynced to a temporary location, only use '.' to avoid
331 # having mountpoints duplicated
335 my $opts = $self->{vzdump
}->{opts
};
336 my $snapdir = $task->{snapdir
};
337 my $tmpdir = $task->{tmpdir
};
339 my $userns_cmd = $task->{userns_cmd
};
340 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
341 @$PVE::LXC
::COMMON_TAR_FLAGS
,
342 '--one-file-system', '--warning=no-file-ignored'];
344 # note: --remove-files does not work because we do not
345 # backup all files (filters). tar complains:
346 # Cannot rmdir: Directory not empty
347 # we disable this optimization for now
348 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
349 # push @$tar, "--remove-files"; # try to save space
352 # The directory parameter can give an alternative directory as source.
353 # the second parameter gives the structure in the tar.
354 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
355 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw
};
356 push @$tar, "--directory=$snapdir";
357 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
358 push @$tar, '--anchored';
359 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump
}->{findexcl
}};
361 push @$tar, @sources;
365 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
366 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit
};
367 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
369 if ($opts->{stdout
}) {
370 push @{$cmd->[-1]}, \
(">&" . fileno($opts->{stdout
}));
372 push @{$cmd->[-1]}, \
(">" . PVE
::Tools
::shellquote
($filename));
378 my ($self, $task, $vmid) = @_;
380 my $conf = PVE
::LXC
::load_config
($vmid);
382 if ($task->{mode
} ne 'suspend') {
383 my $rootdir = $default_mount_point;
384 my $disks = $task->{disks
};
385 foreach my $disk (reverse @$disks) {
386 PVE
::Tools
::run_command
(['umount', '-l', '-d', $disk->{dir
}]) if $disk->{dir
};
390 if ($task->{cleanup
}->{remove_snapshot
}) {
391 $self->loginfo("remove vzdump snapshot");
392 PVE
::LXC
::snapshot_delete
($vmid, 'vzdump', 0);