]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
ccad9cd561273a316ffb2d9ae46186b84fdd0252
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, $from, $to, $text) = @_;
21 $self->loginfo ("starting $text sync $from to $to");
23 my $starttime = time();
25 my $opts = $self->{vzdump
}->{opts
};
27 my $rsyncopts = "--stats -x -X --numeric-ids";
29 $rsyncopts .= " --bwlimit=$opts->{bwlimit}" if $opts->{bwlimit
};
31 $self->cmd ("rsync $rsyncopts -aH --delete --no-whole-file --inplace '$from' '$to'");
33 my $delay = time () - $starttime;
35 $self->loginfo ("$text sync finished ($delay seconds)");
39 my ($class, $vzdump) = @_;
41 PVE
::VZDump
::check_bin
('lxc-stop');
42 PVE
::VZDump
::check_bin
('lxc-start');
43 PVE
::VZDump
::check_bin
('lxc-freeze');
44 PVE
::VZDump
::check_bin
('lxc-unfreeze');
48 $self->{vzdump
} = $vzdump;
49 $self->{storecfg
} = PVE
::Storage
::config
();
51 $self->{vmlist
} = PVE
::LXC
::config_list
();
61 my ($self, $vmid) = @_;
63 my $running = PVE
::LXC
::check_running
($vmid) ?
1 : 0;
65 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
68 my $check_mountpoint_empty = sub {
69 my ($mountpoint) = @_;
71 die "mountpoint '$mountpoint' is not a directory\n" if ! -d
$mountpoint;
73 PVE
::Tools
::dir_glob_foreach
($mountpoint, qr/.*/, sub {
75 return if $entry eq '.' || $entry eq '..';
76 die "mountpoint '$mountpoint' not empty\n";
81 my ($self, $task, $vmid, $mode) = @_;
83 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::load_config
($vmid);
85 PVE
::LXC
::foreach_mountpoint
($conf, sub {
86 my ($ms, $mountpoint) = @_;
88 return if $ms eq 'rootfs';
89 # TODO: implement support for mountpoints
90 die "unable to backup mountpoint '$ms' - feature not implemented\n";
93 my $running = PVE
::LXC
::check_running
($vmid);
95 my $diskinfo = $task->{diskinfo
} = {};
97 $task->{hostname
} = $conf->{'hostname'} || "CT$vmid";
99 my $rootinfo = PVE
::LXC
::parse_ct_mountpoint
($conf->{rootfs
});
100 $diskinfo->{volid
} = $rootinfo->{volume
};
102 die "missing root volid (no volid)\n" if !$diskinfo->{volid
};
104 # fixme: when do we deactivate ??
105 PVE
::Storage
::activate_volumes
($self->{storecfg
}, [$diskinfo->{volid
}]);
107 if ($mode eq 'snapshot') {
109 if (!PVE
::LXC
::has_feature
('snapshot', $conf, $self->{storecfg
})) {
110 die "mode failure - some volumes does not support snapshots\n";
113 if ($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
}) {
114 $self->loginfo("found old vzdump snapshot (force removal)");
115 PVE
::LXC
::snapshot_delete
($vmid, 'vzdump', 0);
118 my $rootdir = $default_mount_point;
120 &$check_mountpoint_empty($rootdir);
122 # set snapshot_count (freezes CT it snapshot_count > 1)
123 my $volid_list = PVE
::LXC
::get_vm_volumes
($conf);
124 $task->{snapshot_count
} = scalar(@$volid_list);
126 } elsif ($mode eq 'stop') {
127 my $rootdir = $default_mount_point;
129 &$check_mountpoint_empty($rootdir);
130 } elsif ($mode eq 'suspend') {
131 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
132 $diskinfo->{dir
} = "/proc/$pid/root";
133 $task->{snapdir
} = $task->{tmpdir
};
135 die "unknown mode '$mode'\n"; # should not happen
140 my ($self, $vmid) = @_;
142 PVE
::LXC
::lock_aquire
($vmid);
146 my ($self, $vmid) = @_;
148 PVE
::LXC
::lock_release
($vmid);
152 my ($self, $task, $vmid) = @_;
154 my $diskinfo = $task->{diskinfo
};
156 $self->loginfo("create storage snapshot snapshot");
158 # todo: freeze/unfreeze if we have more than one volid
159 PVE
::LXC
::snapshot_create
($vmid, 'vzdump', "vzdump backup snapshot");
160 $task->{cleanup
}->{remove_snapshot
} = 1;
163 my $conf = $self->{vmlist
}->{$vmid} = PVE
::LXC
::load_config
($vmid);
164 die "unable to read vzdump shanpshot config - internal error"
165 if !($conf->{snapshots
} && $conf->{snapshots
}->{vzdump
});
167 # my $snapconf = $conf->{snapshots}->{vzdump};
168 # my $volid_list = PVE::LXC::get_vm_volumes($snapconf);
169 my $volid_list = [$diskinfo->{volid
}];
171 my $rootdir = $default_mount_point;
173 my $mp = { volume
=> $diskinfo->{volid
}, mp
=> "/" };
174 PVE
::LXC
::mountpoint_mount
($mp, $rootdir, $self->{storecfg
}, 'vzdump');
176 $diskinfo->{dir
} = $diskinfo->{mountpoint
} = $rootdir;
177 $task->{snapdir
} = $diskinfo->{dir
};
180 sub copy_data_phase1
{
181 my ($self, $task) = @_;
183 $self->$rsync_vm($task, "$task->{diskinfo}->{dir}/", $task->{snapdir
}, "first");
186 sub copy_data_phase2
{
187 my ($self, $task) = @_;
189 $self->$rsync_vm ($task, "$task->{diskinfo}->{dir}/", $task->{snapdir
}, "final");
193 my ($self, $task, $vmid) = @_;
195 $self->cmd("lxc-stop -n $vmid");
199 my ($self, $task, $vmid) = @_;
201 $self->cmd ("lxc-start -n $vmid");
205 my ($self, $task, $vmid) = @_;
207 $self->cmd ("lxc-freeze -n $vmid");
211 my ($self, $task, $vmid) = @_;
213 $self->cmd ("lxc-unfreeze -n $vmid");
217 my ($self, $task, $vmid) = @_;
219 my $tmpdir = $task->{tmpdir
};
221 mkpath
"$tmpdir/etc/vzdump/";
223 my $conf = PVE
::LXC
::load_config
($vmid);
224 delete $conf->{snapshots
};
225 delete $conf->{'pve.parent'};
227 PVE
::Tools
::file_set_contents
("$tmpdir/etc/vzdump/pct.conf", PVE
::LXC
::write_pct_config
("/lxc/$vmid.conf", $conf));
231 my ($self, $task, $vmid, $filename, $comp) = @_;
233 if ($task->{mode
} eq 'stop') {
234 my $rootdir = $default_mount_point;
235 my $diskinfo = $task->{diskinfo
};
237 my $volid_list = [$diskinfo->{volid
}];
238 my $mp = { volume
=> $diskinfo->{volid
}, mp
=> "/" };
240 $self->loginfo("mounting container root at '$rootdir'");
241 PVE
::LXC
::mountpoint_mount
($mp, $rootdir, $self->{storecfg
});
243 $diskinfo->{dir
} = $diskinfo->{mountpoint
} = $rootdir;
244 $task->{snapdir
} = $diskinfo->{dir
};
247 my $findexcl = $self->{vzdump
}->{findexcl
};
248 push @$findexcl, "'('", '-path', "./etc/vzdump", "-prune", "')'", '-o';
250 my $findargs = join (' ', @$findexcl) . ' -print0';
251 my $opts = $self->{vzdump
}->{opts
};
253 my $srcdir = $task->{diskinfo
}->{dir
};
254 my $snapdir = $task->{snapdir
};
255 my $tmpdir = $task->{tmpdir
};
257 my $taropts = "--totals --sparse --numeric-owner --no-recursion --xattrs --one-file-system";
259 # note: --remove-files does not work because we do not
260 # backup all files (filters). tar complains:
261 # Cannot rmdir: Directory not empty
262 # we we disable this optimization for now
263 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
264 # $taropts .= " --remove-files"; # try to save space
269 $cmd .= "cd $snapdir;find . $findargs|sed 's/\\\\/\\\\\\\\/g'|";
270 $cmd .= "tar cpf - $taropts ";
271 # The directory parameter can give a alternative directory as source.
272 # the second parameter gives the structure in the tar.
273 $cmd .= "--directory=$tmpdir ./etc/vzdump/pct.conf ";
274 $cmd .= "--directory=$snapdir --null -T -";
276 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
277 $cmd .= "|cstream -t $bwl" if $opts->{bwlimit
};
278 $cmd .= "|$comp" if $comp;
282 if ($opts->{stdout
}) {
283 $self->cmd ($cmd, output
=> ">&" . fileno($opts->{stdout
}));
285 $self->cmd ("$cmd >$filename");
290 my ($self, $task, $vmid) = @_;
292 my $diskinfo = $task->{diskinfo
};
294 if (my $rootdir = $diskinfo->{mountpoint
}) {
295 PVE
::Tools
::run_command
(['umount', '-l', '-d', $rootdir]);
298 if ($task->{cleanup
}->{remove_snapshot
}) {
299 $self->loginfo("remove vzdump snapshot");
300 PVE
::LXC
::snapshot_delete
($vmid, 'vzdump', 0);