]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
1bad3fd73a8f3991dc58f586b45d7a991376c685
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
::Config-
>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
} = [];
108 $task->{hostname
} = $conf->{'hostname'} || "CT$vmid";
110 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
111 $task->{userns_cmd
} = PVE
::LXC
::userns_command
($id_map);
113 my $volids = $task->{volids
} = [];
114 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
115 my ($name, $data) = @_;
116 my $volid = $data->{volume
};
117 my $mount = $data->{mp
};
118 my $type = $data->{type
};
120 return if !$volid || !$mount;
122 if ($name ne 'rootfs' && !$data->{backup
}) {
123 push @$exclude_dirs, $mount;
128 push @$volids, $volid
129 if $type eq 'volume';
132 if ($mode eq 'snapshot') {
133 if (!PVE
::LXC
::Config-
>has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
134 die "mode failure - some volumes do not support snapshots\n";
137 unlock_vm
($self, $vmid);
139 if ($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
}) {
140 $self->loginfo("found old vzdump snapshot (force removal)");
141 PVE
::LXC
::Config-
>snapshot_delete($vmid, 'vzdump', 1);
144 my $rootdir = $default_mount_point;
146 &$check_mountpoint_empty($rootdir);
148 # set snapshot_count (freezes CT if snapshot_count > 1)
149 $task->{snapshot_count
} = scalar(@$volids);
150 } elsif ($mode eq 'stop') {
151 my $rootdir = $default_mount_point;
153 &$check_mountpoint_empty($rootdir);
154 PVE
::Storage
::activate_volumes
($storage_cfg, $volids);
155 } elsif ($mode eq 'suspend') {
156 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
157 foreach my $disk (@$disks) {
158 $disk->{dir
} = "/proc/$pid/root$disk->{mp}";
160 $task->{snapdir
} = $task->{tmpdir
};
162 unlock_vm
($self, $vmid);
163 die "unknown mode '$mode'\n"; # should not happen
166 if ($mode ne 'suspend') {
167 # If we perform mount operations, let's unshare the mount namespace
168 # to not influence the running host.
169 PVE
::Tools
::unshare
(PVE
::Tools
::CLONE_NEWNS
);
170 PVE
::Tools
::run_command
(['mount', '--make-rprivate', '/']);
175 my ($self, $vmid) = @_;
177 PVE
::LXC
::Config-
>set_lock($vmid, 'backup');
181 my ($self, $vmid) = @_;
183 PVE
::LXC
::Config-
>remove_lock($vmid, 'backup')
187 my ($self, $task, $vmid) = @_;
189 $self->loginfo("create storage snapshot 'vzdump'");
191 # todo: freeze/unfreeze if we have more than one volid
192 PVE
::LXC
::Config-
>snapshot_create($vmid, 'vzdump', 0, "vzdump backup snapshot");
193 $task->{cleanup
}->{remove_snapshot
} = 1;
196 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::Config-
>load_config($vmid);
197 die "unable to read vzdump snapshot config - internal error"
198 if !($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
});
200 my $disks = $task->{disks
};
201 my $volids = $task->{volids
};
203 my $rootdir = $default_mount_point;
204 my $storage_cfg = $self->{storecfg
};
206 PVE
::Storage
::activate_volumes
($storage_cfg, $volids, 'vzdump');
207 foreach my $disk (@$disks) {
208 $disk->{dir
} = "${rootdir}$disk->{mp}";
209 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg, 'vzdump');
212 $task->{snapdir
} = $rootdir;
215 sub copy_data_phase1
{
216 my ($self, $task) = @_;
218 if (my $mntinfo = PVE
::VZDump
::get_mount_info
($task->{snapdir
})) {
219 if ($mntinfo->{fstype
} =~ /^nfs4?/) {
220 warn "temporary directory is on NFS, disabling xattr and acl support"
221 . ", consider configuring a local tmpdir via /etc/vzdump.conf\n";
222 $task->{no_xattrs
} = 1;
226 $self->$rsync_vm($task, $task->{snapdir
}, "first");
229 sub copy_data_phase2
{
230 my ($self, $task) = @_;
232 $self->$rsync_vm($task, $task->{snapdir
}, "final");
236 my ($self, $task, $vmid) = @_;
238 $self->cmd("lxc-stop -n $vmid");
240 # make sure container is stopped
241 $self->cmd("lxc-wait -n $vmid -s STOPPED");
245 my ($self, $task, $vmid) = @_;
247 $self->cmd ("lxc-start -n $vmid");
251 my ($self, $task, $vmid) = @_;
253 $self->cmd ("lxc-freeze -n $vmid");
257 my ($self, $task, $vmid) = @_;
259 $self->cmd ("lxc-unfreeze -n $vmid");
263 my ($self, $task, $vmid) = @_;
265 my $tmpdir = $task->{tmpdir
};
267 mkpath
"$tmpdir/etc/vzdump/";
269 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
270 delete $conf->{lock};
271 delete $conf->{snapshots
};
272 delete $conf->{'pve.parent'};
274 PVE
::Tools
::file_set_contents
("$tmpdir/etc/vzdump/pct.conf", PVE
::LXC
::Config
::write_pct_config
("/lxc/$vmid.conf", $conf));
276 my $firewall ="/etc/pve/firewall/$vmid.fw";
278 PVE
::Tools
::file_copy
($firewall, "$tmpdir/etc/vzdump/pct.fw");
284 my ($self, $task, $vmid, $filename, $comp) = @_;
286 my $disks = $task->{disks
};
289 if ($task->{mode
} eq 'stop') {
290 my $rootdir = $default_mount_point;
291 my $storage_cfg = $self->{storecfg
};
292 foreach my $disk (@$disks) {
293 $disk->{dir
} = "${rootdir}$disk->{mp}";
294 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg);
295 # add every enabled mountpoint (since we use --one-file-system)
296 # mp already starts with a / so we only need to add the dot
297 push @sources, ".$disk->{mp}";
299 $task->{snapdir
} = $rootdir;
300 } elsif ($task->{mode
} eq 'snapshot') {
301 # mounting the vzdump snapshots and setting $snapdir is already done,
302 # but we need to include all mountpoints here!
303 foreach my $disk (@$disks) {
304 push @sources, ".$disk->{mp}";
307 # the data was rsynced to a temporary location, only use '.' to avoid
308 # having mountpoints duplicated
312 my $opts = $self->{vzdump
}->{opts
};
313 my $snapdir = $task->{snapdir
};
314 my $tmpdir = $task->{tmpdir
};
316 my $userns_cmd = $task->{userns_cmd
};
317 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
318 @$PVE::LXC
::COMMON_TAR_FLAGS
,
319 '--one-file-system', '--warning=no-file-ignored'];
321 # note: --remove-files does not work because we do not
322 # backup all files (filters). tar complains:
323 # Cannot rmdir: Directory not empty
324 # we disable this optimization for now
325 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
326 # push @$tar, "--remove-files"; # try to save space
329 # The directory parameter can give an alternative directory as source.
330 # the second parameter gives the structure in the tar.
331 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
332 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw
};
333 push @$tar, "--directory=$snapdir";
334 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
335 push @$tar, '--anchored';
336 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump
}->{findexcl
}};
338 push @$tar, @sources;
342 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
343 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit
};
344 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
346 if ($opts->{stdout
}) {
347 push @{$cmd->[-1]}, \
(">&" . fileno($opts->{stdout
}));
349 push @{$cmd->[-1]}, \
(">" . PVE
::Tools
::shellquote
($filename));
355 my ($self, $task, $vmid) = @_;
357 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
359 if ($task->{mode
} ne 'suspend') {
360 my $rootdir = $default_mount_point;
361 my $disks = $task->{disks
};
362 foreach my $disk (reverse @$disks) {
363 PVE
::Tools
::run_command
(['umount', '-l', '-d', $disk->{dir
}]) if $disk->{dir
};
367 if ($task->{cleanup
}->{remove_snapshot
}) {
368 $self->loginfo("remove vzdump snapshot");
369 PVE
::LXC
::Config-
>snapshot_delete($vmid, 'vzdump', 0);