]>
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 $base = ['rsync', '--stats', '-x', '-X', '--numeric-ids',
28 '-aH', '--delete', '--no-whole-file', '--inplace'];
29 push @$base, "--bwlimit=$opts->{bwlimit}" if $opts->{bwlimit
};
30 push @$base, map { "--exclude=$_" } @{$self->{vzdump
}->{findexcl
}};
31 push @$base, map { "--exclude=$_" } @{$task->{exclude_dirs
}};
33 # FIXME: to support --one-file-system we have to make all exclude paths
34 # relative to the current mountpoint
36 my $starttime = time();
37 $self->cmd([@$base, $from, $to]);
38 my $delay = time () - $starttime;
40 $self->loginfo ("$text sync finished ($delay seconds)");
44 my ($class, $vzdump) = @_;
46 PVE
::VZDump
::check_bin
('lxc-stop');
47 PVE
::VZDump
::check_bin
('lxc-start');
48 PVE
::VZDump
::check_bin
('lxc-freeze');
49 PVE
::VZDump
::check_bin
('lxc-unfreeze');
53 $self->{vzdump
} = $vzdump;
54 $self->{storecfg
} = PVE
::Storage
::config
();
56 $self->{vmlist
} = PVE
::LXC
::config_list
();
66 my ($self, $vmid) = @_;
68 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
70 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
73 my $check_mountpoint_empty = sub {
74 my ($mountpoint) = @_;
76 die "mountpoint '$mountpoint' is not a directory\n" if ! -d
$mountpoint;
78 PVE
::Tools
::dir_glob_foreach
($mountpoint, qr/.*/, sub {
80 return if $entry eq '.' || $entry eq '..';
81 die "mountpoint '$mountpoint' not empty\n";
85 # The container might have *different* symlinks than the host. realpath/abs_path
86 # use the actual filesystem to resolve links.
87 sub sanitize_mountpoint
{
89 $mp = '/' . $mp; # we always start with a slash
90 $mp =~ s
@/{2,}@/@g; # collapse sequences of slashes
91 $mp =~ s
@/\./@@g; # collapse /./
92 $mp =~ s
@/\.(/)?
$@$1@; # collapse a trailing /. or /./
93 $mp =~ s
@(.*)/[^/]+/\.\./@$1/@g; # collapse /../ without regard for symlinks
94 $mp =~ s
@/\.\
.(/)?
$@$1@; # collapse trailing /.. or /../ disregarding symlinks
99 my ($self, $task, $vmid, $mode) = @_;
101 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::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 # fixme: when do we deactivate ??
112 PVE
::LXC
::foreach_mountpoint
($conf, sub {
113 my ($name, $data) = @_;
114 my $volid = $data->{volume
};
115 my $mount = $data->{mp
};
117 $mount = $data->{mp
} = sanitize_mountpoint
($mount);
119 return if !$volid || !$mount || $volid =~ m
|^/|;
121 if ($name ne 'rootfs' && !$data->{backup
}) {
122 push @$exclude_dirs, $mount;
128 my $volid_list = [map { $_->{volume
} } @$disks];
129 PVE
::Storage
::activate_volumes
($storage_cfg, $volid_list);
131 if ($mode eq 'snapshot') {
132 if (!PVE
::LXC
::has_feature
('snapshot', $conf, $storage_cfg)) {
133 die "mode failure - some volumes does not support snapshots\n";
136 if ($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
}) {
137 $self->loginfo("found old vzdump snapshot (force removal)");
138 PVE
::LXC
::snapshot_delete
($vmid, 'vzdump', 0);
141 my $rootdir = $default_mount_point;
143 &$check_mountpoint_empty($rootdir);
145 # set snapshot_count (freezes CT it snapshot_count > 1)
146 $task->{snapshot_count
} = scalar(@$volid_list);
147 } elsif ($mode eq 'stop') {
148 my $rootdir = $default_mount_point;
150 &$check_mountpoint_empty($rootdir);
151 } elsif ($mode eq 'suspend') {
152 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
153 foreach my $disk (@$disks) {
154 $disk->{dir
} = "/proc/$pid/root$disk->{mp}";
156 $task->{snapdir
} = $task->{tmpdir
};
158 die "unknown mode '$mode'\n"; # should not happen
163 my ($self, $vmid) = @_;
165 PVE
::LXC
::lock_aquire
($vmid);
169 my ($self, $vmid) = @_;
171 PVE
::LXC
::lock_release
($vmid);
175 my ($self, $task, $vmid) = @_;
177 $self->loginfo("create storage snapshot snapshot");
179 # todo: freeze/unfreeze if we have more than one volid
180 PVE
::LXC
::snapshot_create
($vmid, 'vzdump', "vzdump backup snapshot");
181 $task->{cleanup
}->{remove_snapshot
} = 1;
184 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::load_config
($vmid);
185 die "unable to read vzdump shanpshot config - internal error"
186 if !($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
});
188 my $disks = $task->{disks
};
189 my $volid_list = [map { $_->{volume
} } @$disks];
191 my $rootdir = $default_mount_point;
192 my $storage_cfg = $self->{storecfg
};
194 foreach my $disk (@$disks) {
195 $disk->{dir
} = "${rootdir}$disk->{mp}";
196 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg, 'vzdump');
199 $task->{snapdir
} = $rootdir;
202 sub copy_data_phase1
{
203 my ($self, $task) = @_;
205 $self->$rsync_vm($task, $task->{snapdir
}, "first");
208 sub copy_data_phase2
{
209 my ($self, $task) = @_;
211 $self->$rsync_vm($task, $task->{snapdir
}, "final");
215 my ($self, $task, $vmid) = @_;
217 $self->cmd("lxc-stop -n $vmid");
221 my ($self, $task, $vmid) = @_;
223 $self->cmd ("lxc-start -n $vmid");
227 my ($self, $task, $vmid) = @_;
229 $self->cmd ("lxc-freeze -n $vmid");
233 my ($self, $task, $vmid) = @_;
235 $self->cmd ("lxc-unfreeze -n $vmid");
239 my ($self, $task, $vmid) = @_;
241 my $tmpdir = $task->{tmpdir
};
243 mkpath
"$tmpdir/etc/vzdump/";
245 my $conf = PVE
::LXC
::load_config
($vmid);
246 delete $conf->{snapshots
};
247 delete $conf->{'pve.parent'};
249 PVE
::Tools
::file_set_contents
("$tmpdir/etc/vzdump/pct.conf", PVE
::LXC
::write_pct_config
("/lxc/$vmid.conf", $conf));
253 my ($self, $task, $vmid, $filename, $comp) = @_;
255 if ($task->{mode
} eq 'stop') {
256 my $rootdir = $default_mount_point;
257 my $disks = $task->{disks
};
258 my $storage_cfg = $self->{storecfg
};
259 foreach my $disk (@$disks) {
260 $disk->{dir
} = "${rootdir}$disk->{mp}";
261 PVE
::LXC
::mountpoint_mount
($disk, $rootdir, $storage_cfg);
263 $task->{snapdir
} = $rootdir;
266 my $opts = $self->{vzdump
}->{opts
};
267 my $snapdir = $task->{snapdir
};
268 my $tmpdir = $task->{tmpdir
};
270 my $taropts = "--totals --sparse --numeric-owner --xattrs --one-file-system";
272 # note: --remove-files does not work because we do not
273 # backup all files (filters). tar complains:
274 # Cannot rmdir: Directory not empty
275 # we we disable this optimization for now
276 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
277 # $taropts .= " --remove-files"; # try to save space
280 my $cmd = "tar cpf - $taropts ";
281 # The directory parameter can give a alternative directory as source.
282 # the second parameter gives the structure in the tar.
283 $cmd .= "--directory=$tmpdir ./etc/vzdump/pct.conf ";
284 $cmd .= "--directory=$snapdir";
286 foreach my $exclude (@{$self->{vzdump
}->{findexcl
}}) {
287 $cmd .= " --exclude=.$exclude";
290 # add every enabled mountpoint (since we use --one-file-system)
291 my $disks = $task->{disks
};
292 # mp already starts with a / so we only need to add the dot
293 foreach my $disk (@$disks) {
294 $cmd .= " .$disk->{mp}";
297 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
298 $cmd .= "|cstream -t $bwl" if $opts->{bwlimit
};
299 $cmd .= "|$comp" if $comp;
301 if ($opts->{stdout
}) {
302 $self->cmd ($cmd, output
=> ">&" . fileno($opts->{stdout
}));
304 $self->cmd ("$cmd >" . PVE
::Tools
::shellquote
($filename));
309 my ($self, $task, $vmid) = @_;
311 my $conf = PVE
::LXC
::load_config
($vmid);
313 my $rootdir = $default_mount_point;
314 my $disks = $task->{disks
};
315 foreach my $disk (reverse @$disks) {
316 PVE
::Tools
::run_command
(['umount', '-l', '-d', $disk->{dir
}]) if $disk->{dir
};
319 if ($task->{cleanup
}->{remove_snapshot
}) {
320 $self->loginfo("remove vzdump snapshot");
321 PVE
::LXC
::snapshot_delete
($vmid, 'vzdump', 0);