]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
1 package PVE
::VZDump
::LXC
;
8 use PVE
::Cluster
qw(cfs_read_file);
15 use base qw
(PVE
::VZDump
::Plugin
);
17 my $default_mount_point = "/mnt/vzsnap0";
20 my ($self, $task, $to, $text) = @_;
22 my $disks = $task->{disks
};
23 my $from = $disks->[0]->{dir
} . '/';
24 $self->loginfo ("starting $text sync $from to $to");
26 my $opts = $self->{vzdump
}->{opts
};
28 my @xattr = $task->{no_xattrs
} ?
() : ('-X', '-A');
30 my $rsync = ['rsync', '--stats', @xattr, '--numeric-ids',
31 '-aH', '--delete', '--no-whole-file', '--inplace',
32 '--one-file-system', '--relative'];
33 push @$rsync, "--bwlimit=$opts->{bwlimit}" if $opts->{bwlimit
};
34 push @$rsync, map { "--exclude=$_" } @{$self->{vzdump
}->{findexcl
}};
35 push @$rsync, map { "--exclude=$_" } @{$task->{exclude_dirs
}};
37 my $starttime = time();
38 # See the rsync(1) manpage for --relative in conjunction with /./ in paths.
39 # This is the only way to have exclude-dirs work together with the
40 # --one-file-system option.
41 # This way we can pass multiple source paths and tell rsync which directory
42 # they're supposed to be relative to.
43 # Otherwise with eg. using multiple rsync commands means the --exclude
44 # directives need to be modified for every command as they are meant to be
45 # relative to the rootdir, while rsync treats them as relative to the
47 foreach my $disk (@$disks) {
48 push @$rsync, "$from/.$disk->{mp}";
50 $self->cmd([@$rsync, $to]);
51 my $delay = time () - $starttime;
53 $self->loginfo ("$text sync finished ($delay seconds)");
57 my ($class, $vzdump) = @_;
59 PVE
::VZDump
::check_bin
('lxc-stop');
60 PVE
::VZDump
::check_bin
('lxc-start');
61 PVE
::VZDump
::check_bin
('lxc-freeze');
62 PVE
::VZDump
::check_bin
('lxc-unfreeze');
66 $self->{vzdump
} = $vzdump;
67 $self->{storecfg
} = PVE
::Storage
::config
();
69 $self->{vmlist
} = PVE
::LXC
::config_list
();
79 my ($self, $vmid) = @_;
81 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
83 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
86 my $check_mountpoint_empty = sub {
87 my ($mountpoint) = @_;
89 die "mountpoint '$mountpoint' is not a directory\n" if ! -d
$mountpoint;
91 PVE
::Tools
::dir_glob_foreach
($mountpoint, qr/.*/, sub {
93 return if $entry eq '.' || $entry eq '..';
94 die "mountpoint '$mountpoint' not empty\n";
99 my ($self, $task, $vmid, $mode) = @_;
101 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::Config-
>load_config($vmid);
102 my $storage_cfg = $self->{storecfg
};
104 my $running = PVE
::LXC
::check_running
($vmid);
106 my $disks = $task->{disks
} = [];
107 my $exclude_dirs = $task->{exclude_dirs
} = [];
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
::Config-
>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 (!PVE
::LXC
::Config-
>mountpoint_backup_enabled($name, $data)) {
124 push @$exclude_dirs, $mount;
129 push @$volids, $volid
130 if $type eq 'volume';
133 if ($mode eq 'snapshot') {
134 if (!PVE
::LXC
::Config-
>has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
135 die "mode failure - some volumes do not support snapshots\n";
139 if ($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
}) {
140 $self->loginfo("found old vzdump snapshot (force removal)");
141 PVE
::LXC
::Config-
>lock_config($vmid, sub {
142 $self->unlock_vm($vmid);
143 PVE
::LXC
::Config-
>snapshot_delete($vmid, 'vzdump', 1);
144 $self->lock_vm($vmid);
148 my $rootdir = $default_mount_point;
150 &$check_mountpoint_empty($rootdir);
152 # set snapshot_count (freezes CT if snapshot_count > 1)
153 $task->{snapshot_count
} = scalar(@$volids);
154 } elsif ($mode eq 'stop') {
155 my $rootdir = $default_mount_point;
157 &$check_mountpoint_empty($rootdir);
158 PVE
::Storage
::activate_volumes
($storage_cfg, $volids);
159 } elsif ($mode eq 'suspend') {
160 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
161 foreach my $disk (@$disks) {
162 $disk->{dir
} = "/proc/$pid/root$disk->{mp}";
164 $task->{snapdir
} = $task->{tmpdir
};
166 unlock_vm
($self, $vmid);
167 die "unknown mode '$mode'\n"; # should not happen
170 if ($mode ne 'suspend') {
171 # If we perform mount operations, let's unshare the mount namespace
172 # to not influence the running host.
173 PVE
::Tools
::unshare
(PVE
::Tools
::CLONE_NEWNS
);
174 PVE
::Tools
::run_command
(['mount', '--make-rprivate', '/']);
179 my ($self, $vmid) = @_;
181 PVE
::LXC
::Config-
>set_lock($vmid, 'backup');
185 my ($self, $vmid) = @_;
187 PVE
::LXC
::Config-
>remove_lock($vmid, 'backup')
191 my ($self, $task, $vmid) = @_;
193 $self->loginfo("create storage snapshot 'vzdump'");
195 # todo: freeze/unfreeze if we have more than one volid
196 PVE
::LXC
::Config-
>lock_config($vmid, sub {
197 $self->unlock_vm($vmid);
198 PVE
::LXC
::Config-
>snapshot_create($vmid, 'vzdump', 0, "vzdump backup snapshot");
199 $self->lock_vm($vmid);
201 $task->{cleanup
}->{remove_snapshot
} = 1;
204 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::Config-
>load_config($vmid);
205 die "unable to read vzdump snapshot config - internal error"
206 if !($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
});
208 my $disks = $task->{disks
};
209 my $volids = $task->{volids
};
211 my $rootdir = $default_mount_point;
212 my $storage_cfg = $self->{storecfg
};
214 PVE
::Storage
::activate_volumes
($storage_cfg, $volids, 'vzdump');
215 foreach my $disk (@$disks) {
216 $disk->{dir
} = "${rootdir}$disk->{mp}";
217 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg, 'vzdump');
220 $task->{snapdir
} = $rootdir;
223 sub copy_data_phase1
{
224 my ($self, $task) = @_;
226 if (my $mntinfo = PVE
::VZDump
::get_mount_info
($task->{snapdir
})) {
227 if ($mntinfo->{fstype
} =~ /^nfs4?/) {
228 warn "temporary directory is on NFS, disabling xattr and acl support"
229 . ", consider configuring a local tmpdir via /etc/vzdump.conf\n";
230 $task->{no_xattrs
} = 1;
234 $self->$rsync_vm($task, $task->{snapdir
}, "first");
237 sub copy_data_phase2
{
238 my ($self, $task) = @_;
240 $self->$rsync_vm($task, $task->{snapdir
}, "final");
244 my ($self, $task, $vmid) = @_;
246 $self->cmd("lxc-stop -n $vmid");
248 # make sure container is stopped
249 $self->cmd("lxc-wait -n $vmid -s STOPPED");
253 my ($self, $task, $vmid) = @_;
255 $self->cmd ("lxc-start -n $vmid");
259 my ($self, $task, $vmid) = @_;
261 $self->cmd ("lxc-freeze -n $vmid");
265 my ($self, $task, $vmid) = @_;
267 $self->cmd ("lxc-unfreeze -n $vmid");
271 my ($self, $task, $vmid) = @_;
273 my $tmpdir = $task->{tmpdir
};
275 mkpath
"$tmpdir/etc/vzdump/";
277 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
278 delete $conf->{lock};
279 delete $conf->{snapshots
};
280 delete $conf->{'pve.parent'};
282 PVE
::Tools
::file_set_contents
("$tmpdir/etc/vzdump/pct.conf", PVE
::LXC
::Config
::write_pct_config
("/lxc/$vmid.conf", $conf));
284 my $firewall ="/etc/pve/firewall/$vmid.fw";
286 PVE
::Tools
::file_copy
($firewall, "$tmpdir/etc/vzdump/pct.fw");
292 my ($self, $task, $vmid, $filename, $comp) = @_;
294 my $disks = $task->{disks
};
297 if ($task->{mode
} eq 'stop') {
298 my $rootdir = $default_mount_point;
299 my $storage_cfg = $self->{storecfg
};
300 foreach my $disk (@$disks) {
301 $disk->{dir
} = "${rootdir}$disk->{mp}";
302 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg);
303 # add every enabled mountpoint (since we use --one-file-system)
304 # mp already starts with a / so we only need to add the dot
305 push @sources, ".$disk->{mp}";
307 $task->{snapdir
} = $rootdir;
308 } elsif ($task->{mode
} eq 'snapshot') {
309 # mounting the vzdump snapshots and setting $snapdir is already done,
310 # but we need to include all mountpoints here!
311 foreach my $disk (@$disks) {
312 push @sources, ".$disk->{mp}";
315 # the data was rsynced to a temporary location, only use '.' to avoid
316 # having mountpoints duplicated
320 my $opts = $self->{vzdump
}->{opts
};
321 my $snapdir = $task->{snapdir
};
322 my $tmpdir = $task->{tmpdir
};
324 my $userns_cmd = $task->{userns_cmd
};
325 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
326 @$PVE::LXC
::COMMON_TAR_FLAGS
,
327 '--one-file-system', '--warning=no-file-ignored'];
329 # note: --remove-files does not work because we do not
330 # backup all files (filters). tar complains:
331 # Cannot rmdir: Directory not empty
332 # we disable this optimization for now
333 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
334 # push @$tar, "--remove-files"; # try to save space
337 # The directory parameter can give an alternative directory as source.
338 # the second parameter gives the structure in the tar.
339 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
340 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw
};
341 push @$tar, "--directory=$snapdir";
342 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
343 push @$tar, '--anchored';
344 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump
}->{findexcl
}};
346 push @$tar, @sources;
350 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
351 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit
};
352 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
354 if ($opts->{stdout
}) {
355 $self->cmd($cmd, output
=> ">&" . fileno($opts->{stdout
}));
357 push @{$cmd->[-1]}, \
(">" . PVE
::Tools
::shellquote
($filename));
363 my ($self, $task, $vmid) = @_;
365 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
367 if ($task->{mode
} ne 'suspend') {
368 my $rootdir = $default_mount_point;
369 my $disks = $task->{disks
};
370 foreach my $disk (reverse @$disks) {
371 PVE
::Tools
::run_command
(['umount', '-l', '-d', $disk->{dir
}]) if $disk->{dir
};
375 if ($task->{cleanup
}->{remove_snapshot
}) {
376 $self->loginfo("remove vzdump snapshot");
377 PVE
::LXC
::Config-
>snapshot_delete($vmid, 'vzdump', 0);