]>
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, $first) = @_;
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',
32 ($first ?
'--sparse' : '--inplace'),
33 '--one-file-system', '--relative'];
34 push @$rsync, "--bwlimit=$opts->{bwlimit}" if $opts->{bwlimit
};
35 push @$rsync, map { "--exclude=$_" } @{$self->{vzdump
}->{findexcl
}};
36 push @$rsync, map { "--exclude=$_" } @{$task->{exclude_dirs
}};
38 my $starttime = time();
39 # See the rsync(1) manpage for --relative in conjunction with /./ in paths.
40 # This is the only way to have exclude-dirs work together with the
41 # --one-file-system option.
42 # This way we can pass multiple source paths and tell rsync which directory
43 # they're supposed to be relative to.
44 # Otherwise with eg. using multiple rsync commands means the --exclude
45 # directives need to be modified for every command as they are meant to be
46 # relative to the rootdir, while rsync treats them as relative to the
48 foreach my $disk (@$disks) {
49 push @$rsync, "$from/.$disk->{mp}";
51 $self->cmd([@$rsync, $to]);
52 my $delay = time () - $starttime;
54 $self->loginfo ("$text sync finished ($delay seconds)");
58 my ($class, $vzdump) = @_;
60 PVE
::VZDump
::check_bin
('lxc-stop');
61 PVE
::VZDump
::check_bin
('lxc-start');
62 PVE
::VZDump
::check_bin
('lxc-freeze');
63 PVE
::VZDump
::check_bin
('lxc-unfreeze');
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_mountpoint($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");
136 push @$volids, $volid
137 if $type eq 'volume';
140 if ($mode eq 'snapshot') {
141 if (!PVE
::LXC
::Config-
>has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
142 die "mode failure - some volumes do not support snapshots\n";
146 if ($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
}) {
147 $self->loginfo("found old vzdump snapshot (force removal)");
148 PVE
::LXC
::Config-
>lock_config($vmid, sub {
149 $self->unlock_vm($vmid);
150 PVE
::LXC
::Config-
>snapshot_delete($vmid, 'vzdump', 1);
151 $self->lock_vm($vmid);
155 my $rootdir = $default_mount_point;
157 &$check_mountpoint_empty($rootdir);
159 # set snapshot_count (freezes CT if snapshot_count > 1)
160 $task->{snapshot_count
} = scalar(@$volids);
161 } elsif ($mode eq 'stop') {
162 my $rootdir = $default_mount_point;
164 &$check_mountpoint_empty($rootdir);
165 PVE
::Storage
::activate_volumes
($storage_cfg, $volids);
166 } elsif ($mode eq 'suspend') {
167 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
168 foreach my $disk (@$disks) {
169 $disk->{dir
} = "/proc/$pid/root$disk->{mp}";
171 $task->{snapdir
} = $task->{tmpdir
};
173 unlock_vm
($self, $vmid);
174 die "unknown mode '$mode'\n"; # should not happen
177 if ($mode ne 'suspend') {
178 # If we perform mount operations, let's unshare the mount namespace
179 # to not influence the running host.
180 PVE
::Tools
::unshare
(PVE
::Tools
::CLONE_NEWNS
);
181 PVE
::Tools
::run_command
(['mount', '--make-rslave', '/']);
186 my ($self, $vmid) = @_;
188 PVE
::LXC
::Config-
>set_lock($vmid, 'backup');
192 my ($self, $vmid) = @_;
194 PVE
::LXC
::Config-
>remove_lock($vmid, 'backup')
198 my ($self, $task, $vmid) = @_;
200 $self->loginfo("create storage snapshot 'vzdump'");
202 # todo: freeze/unfreeze if we have more than one volid
203 PVE
::LXC
::Config-
>lock_config($vmid, sub {
204 $self->unlock_vm($vmid);
205 PVE
::LXC
::Config-
>snapshot_create($vmid, 'vzdump', 0, "vzdump backup snapshot");
206 $self->lock_vm($vmid);
208 $task->{cleanup
}->{remove_snapshot
} = 1;
211 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::Config-
>load_config($vmid);
212 die "unable to read vzdump snapshot config - internal error"
213 if !($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
});
215 my $disks = $task->{disks
};
216 my $volids = $task->{volids
};
218 my $rootdir = $default_mount_point;
219 my $storage_cfg = $self->{storecfg
};
221 PVE
::Storage
::activate_volumes
($storage_cfg, $volids, 'vzdump');
222 foreach my $disk (@$disks) {
223 $disk->{dir
} = "${rootdir}$disk->{mp}";
224 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg, 'vzdump', $task->{rootuid
}, $task->{rootgid
});
227 $task->{snapdir
} = $rootdir;
230 sub copy_data_phase1
{
231 my ($self, $task) = @_;
233 if (my $mntinfo = PVE
::VZDump
::get_mount_info
($task->{snapdir
})) {
234 if ($mntinfo->{fstype
} =~ /^nfs4?/) {
236 "temporary directory is on NFS, disabling xattr and acl"
237 ." support, consider configuring a local tmpdir via"
238 ." /etc/vzdump.conf\n");
239 $task->{no_xattrs
} = 1;
243 $self->$rsync_vm($task, $task->{snapdir
}, "first", 1);
246 sub copy_data_phase2
{
247 my ($self, $task) = @_;
249 $self->$rsync_vm($task, $task->{snapdir
}, "final", 0);
253 my ($self, $task, $vmid) = @_;
255 my $opts = $self->{vzdump
}->{opts
};
256 my $timeout = $opts->{stopwait
} * 60;
258 PVE
::LXC
::vm_stop
($vmid, 0, $timeout);
262 my ($self, $task, $vmid) = @_;
264 $self->cmd(['systemctl', 'start', "pve-container\@$vmid"]);
268 my ($self, $task, $vmid) = @_;
270 $self->cmd ("lxc-freeze -n $vmid");
274 my ($self, $task, $vmid) = @_;
276 $self->cmd ("lxc-unfreeze -n $vmid");
280 my ($self, $task, $vmid) = @_;
282 my $tmpdir = $task->{tmpdir
};
284 mkpath
"$tmpdir/etc/vzdump/";
286 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
287 delete $conf->{lock};
288 delete $conf->{snapshots
};
289 delete $conf->{parent
};
290 delete $conf->{pending
};
292 PVE
::Tools
::file_set_contents
("$tmpdir/etc/vzdump/pct.conf", PVE
::LXC
::Config
::write_pct_config
("/lxc/$vmid.conf", $conf));
294 my $firewall ="/etc/pve/firewall/$vmid.fw";
295 my $fwconftmp = "$tmpdir/etc/vzdump/pct.fw";
297 PVE
::Tools
::file_copy
($firewall, $fwconftmp);
299 PVE
::Tools
::file_set_contents
($fwconftmp, '');
305 my ($self, $task, $vmid, $filename, $comp) = @_;
307 my $disks = $task->{disks
};
310 if ($task->{mode
} eq 'stop') {
311 my $rootdir = $default_mount_point;
312 my $storage_cfg = $self->{storecfg
};
313 PVE
::Storage
::activate_volumes
($storage_cfg, $task->{volids
});
314 foreach my $disk (@$disks) {
315 $disk->{dir
} = "${rootdir}$disk->{mp}";
316 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg, undef, $task->{rootuid
}, $task->{rootgid
});
317 # add every enabled mountpoint (since we use --one-file-system)
318 # mp already starts with a / so we only need to add the dot
319 push @sources, ".$disk->{mp}";
321 $task->{snapdir
} = $rootdir;
322 } elsif ($task->{mode
} eq 'snapshot') {
323 # mounting the vzdump snapshots and setting $snapdir is already done,
324 # but we need to include all mountpoints here!
325 foreach my $disk (@$disks) {
326 push @sources, ".$disk->{mp}";
329 # the data was rsynced to a temporary location, only use '.' to avoid
330 # having mountpoints duplicated
334 my $opts = $self->{vzdump
}->{opts
};
335 my $snapdir = $task->{snapdir
};
336 my $tmpdir = $task->{tmpdir
};
338 my $userns_cmd = $task->{userns_cmd
};
339 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
340 @PVE::Storage
::Plugin
::COMMON_TAR_FLAGS
,
341 '--one-file-system', '--warning=no-file-ignored'];
343 # note: --remove-files does not work because we do not
344 # backup all files (filters). tar complains:
345 # Cannot rmdir: Directory not empty
346 # we disable this optimization for now
347 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
348 # push @$tar, "--remove-files"; # try to save space
351 # The directory parameter can give an alternative directory as source.
352 # the second parameter gives the structure in the tar.
353 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
354 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw
};
355 push @$tar, "--directory=$snapdir";
356 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
357 push @$tar, '--anchored';
358 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump
}->{findexcl
}};
360 push @$tar, @sources;
364 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
365 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit
};
366 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
368 if ($opts->{stdout
}) {
369 $self->cmd($cmd, output
=> ">&" . fileno($opts->{stdout
}));
371 push @{$cmd->[-1]}, \
(">" . PVE
::Tools
::shellquote
($filename));
377 my ($self, $task, $vmid) = @_;
379 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
381 if ($task->{mode
} ne 'suspend') {
382 my $rootdir = $default_mount_point;
383 my $disks = $task->{disks
};
384 foreach my $disk (reverse @$disks) {
385 PVE
::Tools
::run_command
(['umount', '-l', '-d', $disk->{dir
}]) if $disk->{dir
};
389 if ($task->{cleanup
}->{remove_snapshot
}) {
390 $self->loginfo("remove vzdump snapshot");
391 PVE
::LXC
::Config-
>snapshot_delete($vmid, 'vzdump', 0);