]>
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);
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 $rsync = ['rsync', '--stats', '-X', '-A', '--numeric-ids',
28 '-aH', '--delete', '--no-whole-file', '--inplace',
29 '--one-file-system', '--relative'];
30 push @$rsync, "--bwlimit=$opts->{bwlimit}" if $opts->{bwlimit
};
31 push @$rsync, map { "--exclude=$_" } @{$self->{vzdump
}->{findexcl
}};
32 push @$rsync, map { "--exclude=$_" } @{$task->{exclude_dirs
}};
34 my $starttime = time();
35 # See the rsync(1) manpage for --relative in conjunction with /./ in paths.
36 # This is the only way to have exclude-dirs work together with the
37 # --one-file-system option.
38 # This way we can pass multiple source paths and tell rsync which directory
39 # they're supposed to be relative to.
40 # Otherwise with eg. using multiple rsync commands means the --exclude
41 # directives need to be modified for every command as they are meant to be
42 # relative to the rootdir, while rsync treats them as relative to the
44 foreach my $disk (@$disks) {
45 push @$rsync, "$from/.$disk->{mp}";
47 $self->cmd([@$rsync, $to]);
48 my $delay = time () - $starttime;
50 $self->loginfo ("$text sync finished ($delay seconds)");
54 my ($class, $vzdump) = @_;
56 PVE
::VZDump
::check_bin
('lxc-stop');
57 PVE
::VZDump
::check_bin
('lxc-start');
58 PVE
::VZDump
::check_bin
('lxc-freeze');
59 PVE
::VZDump
::check_bin
('lxc-unfreeze');
63 $self->{vzdump
} = $vzdump;
64 $self->{storecfg
} = PVE
::Storage
::config
();
66 $self->{vmlist
} = PVE
::LXC
::config_list
();
76 my ($self, $vmid) = @_;
78 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
80 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
83 my $check_mountpoint_empty = sub {
84 my ($mountpoint) = @_;
86 die "mountpoint '$mountpoint' is not a directory\n" if ! -d
$mountpoint;
88 PVE
::Tools
::dir_glob_foreach
($mountpoint, qr/.*/, sub {
90 return if $entry eq '.' || $entry eq '..';
91 die "mountpoint '$mountpoint' not empty\n";
96 my ($self, $task, $vmid, $mode) = @_;
98 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::load_config
($vmid);
99 my $storage_cfg = $self->{storecfg
};
101 my $running = PVE
::LXC
::check_running
($vmid);
103 my $disks = $task->{disks
} = [];
104 my $exclude_dirs = $task->{exclude_dirs
} = [];
106 $task->{hostname
} = $conf->{'hostname'} || "CT$vmid";
108 PVE
::LXC
::foreach_mountpoint
($conf, sub {
109 my ($name, $data) = @_;
110 my $volid = $data->{volume
};
111 my $mount = $data->{mp
};
113 return if !$volid || !$mount || $volid =~ m
|^/|;
115 if ($name ne 'rootfs' && !$data->{backup
}) {
116 push @$exclude_dirs, $mount;
122 my $volid_list = [map { $_->{volume
} } @$disks];
124 if ($mode eq 'snapshot') {
125 if (!PVE
::LXC
::has_feature
('snapshot', $conf, $storage_cfg)) {
126 die "mode failure - some volumes does not support snapshots\n";
129 if ($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
}) {
130 $self->loginfo("found old vzdump snapshot (force removal)");
131 PVE
::LXC
::snapshot_delete
($vmid, 'vzdump', 0);
134 my $rootdir = $default_mount_point;
136 &$check_mountpoint_empty($rootdir);
138 # set snapshot_count (freezes CT it snapshot_count > 1)
139 $task->{snapshot_count
} = scalar(@$volid_list);
140 } elsif ($mode eq 'stop') {
141 my $rootdir = $default_mount_point;
143 &$check_mountpoint_empty($rootdir);
144 PVE
::Storage
::activate_volumes
($storage_cfg, $volid_list);
145 } elsif ($mode eq 'suspend') {
146 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
147 foreach my $disk (@$disks) {
148 $disk->{dir
} = "/proc/$pid/root$disk->{mp}";
150 $task->{snapdir
} = $task->{tmpdir
};
152 die "unknown mode '$mode'\n"; # should not happen
155 if ($mode ne 'suspend') {
156 # If we preform mount operations, let's unshare the mount namespace
157 # to not influence the running host.
158 PVE
::Tools
::unshare
(PVE
::Tools
::CLONE_NEWNS
);
159 PVE
::Tools
::run_command
(['mount', '--make-rprivate', '/']);
164 my ($self, $vmid) = @_;
166 PVE
::LXC
::lock_aquire
($vmid);
170 my ($self, $vmid) = @_;
172 PVE
::LXC
::lock_release
($vmid);
176 my ($self, $task, $vmid) = @_;
178 $self->loginfo("create storage snapshot snapshot");
180 # todo: freeze/unfreeze if we have more than one volid
181 PVE
::LXC
::snapshot_create
($vmid, 'vzdump', "vzdump backup snapshot");
182 $task->{cleanup
}->{remove_snapshot
} = 1;
185 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::load_config
($vmid);
186 die "unable to read vzdump shanpshot config - internal error"
187 if !($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
});
189 my $disks = $task->{disks
};
190 my $volid_list = [map { $_->{volume
} } @$disks];
192 my $rootdir = $default_mount_point;
193 my $storage_cfg = $self->{storecfg
};
195 PVE
::Storage
::activate_volumes
($storage_cfg, $volid_list, 'vzdump');
196 foreach my $disk (@$disks) {
197 $disk->{dir
} = "${rootdir}$disk->{mp}";
198 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg, 'vzdump');
201 $task->{snapdir
} = $rootdir;
204 sub copy_data_phase1
{
205 my ($self, $task) = @_;
207 $self->$rsync_vm($task, $task->{snapdir
}, "first");
210 sub copy_data_phase2
{
211 my ($self, $task) = @_;
213 $self->$rsync_vm($task, $task->{snapdir
}, "final");
217 my ($self, $task, $vmid) = @_;
219 $self->cmd("lxc-stop -n $vmid");
223 my ($self, $task, $vmid) = @_;
225 $self->cmd ("lxc-start -n $vmid");
229 my ($self, $task, $vmid) = @_;
231 $self->cmd ("lxc-freeze -n $vmid");
235 my ($self, $task, $vmid) = @_;
237 $self->cmd ("lxc-unfreeze -n $vmid");
241 my ($self, $task, $vmid) = @_;
243 my $tmpdir = $task->{tmpdir
};
245 mkpath
"$tmpdir/etc/vzdump/";
247 my $conf = PVE
::LXC
::load_config
($vmid);
248 delete $conf->{snapshots
};
249 delete $conf->{'pve.parent'};
251 PVE
::Tools
::file_set_contents
("$tmpdir/etc/vzdump/pct.conf", PVE
::LXC
::write_pct_config
("/lxc/$vmid.conf", $conf));
255 my ($self, $task, $vmid, $filename, $comp) = @_;
257 my $disks = $task->{disks
};
260 if ($task->{mode
} eq 'stop') {
261 my $rootdir = $default_mount_point;
262 my $storage_cfg = $self->{storecfg
};
263 foreach my $disk (@$disks) {
264 $disk->{dir
} = "${rootdir}$disk->{mp}";
265 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg);
266 # add every enabled mountpoint (since we use --one-file-system)
267 # mp already starts with a / so we only need to add the dot
268 push @sources, ".$disk->{mp}";
270 $task->{snapdir
} = $rootdir;
272 # the data was rsynced to a temporary location, only use '.' to avoid
273 # having mountpoints duplicated
277 my $opts = $self->{vzdump
}->{opts
};
278 my $snapdir = $task->{snapdir
};
279 my $tmpdir = $task->{tmpdir
};
281 my $tar = ['tar', 'cpf', '-', '--totals',
282 @$PVE::LXC
::COMMON_TAR_FLAGS
,
283 '--one-file-system', '--warning=no-file-ignored'];
285 # note: --remove-files does not work because we do not
286 # backup all files (filters). tar complains:
287 # Cannot rmdir: Directory not empty
288 # we we disable this optimization for now
289 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
290 # push @$tar, "--remove-files"; # try to save space
293 # The directory parameter can give a alternative directory as source.
294 # the second parameter gives the structure in the tar.
295 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
296 push @$tar, "--directory=$snapdir";
297 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump
}->{findexcl
}};
299 push @$tar, @sources;
303 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
304 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit
};
305 push @$cmd, [ $comp ] if $comp;
307 if ($opts->{stdout
}) {
308 push @{$cmd->[-1]}, \
(">&" . fileno($opts->{stdout
}));
310 push @{$cmd->[-1]}, \
(">" . PVE
::Tools
::shellquote
($filename));
316 my ($self, $task, $vmid) = @_;
318 my $conf = PVE
::LXC
::load_config
($vmid);
320 if ($task->{mode
} ne 'suspend') {
321 my $rootdir = $default_mount_point;
322 my $disks = $task->{disks
};
323 foreach my $disk (reverse @$disks) {
324 PVE
::Tools
::run_command
(['umount', '-l', '-d', $disk->{dir
}]) if $disk->{dir
};
328 if ($task->{cleanup
}->{remove_snapshot
}) {
329 $self->loginfo("remove vzdump snapshot");
330 PVE
::LXC
::snapshot_delete
($vmid, 'vzdump', 0);