1 package PVE
::LXC
::Create
;
9 use PVE
::RPCEnvironment
;
10 use PVE
::Storage
::PBSPlugin
;
12 use PVE
::DataCenterConfig
;
15 use PVE
::VZDump
::ConvertOVZ
;
20 my ($storage_cfg, $archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
22 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($archive, 1);
23 if (defined($storeid)) {
24 my $scfg = PVE
::Storage
::storage_check_enabled
($storage_cfg, $storeid);
25 if ($scfg->{type
} eq 'pbs') {
26 return restore_proxmox_backup_archive
($storage_cfg, $archive, $rootdir, $conf, $no_unpack_error, $bwlimit);
30 $archive = PVE
::Storage
::abs_filesystem_path
($storage_cfg, $archive) if $archive ne '-';
31 restore_tar_archive
($archive, $rootdir, $conf, $no_unpack_error, $bwlimit);
34 sub restore_proxmox_backup_archive
{
35 my ($storage_cfg, $archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
37 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($archive);
38 my $scfg = PVE
::Storage
::storage_config
($storage_cfg, $storeid);
40 my ($vtype, $name, undef, undef, undef, undef, $format) =
41 PVE
::Storage
::parse_volname
($storage_cfg, $archive);
43 die "got unexpected vtype '$vtype'\n" if $vtype ne 'backup';
45 die "got unexpected backup format '$format'\n" if $format ne 'pbs-ct';
47 my ($id_map, $root_uid, $root_gid) = PVE
::LXC
::parse_id_maps
($conf);
48 my $userns_cmd = PVE
::LXC
::userns_command
($id_map);
51 my $param = [$name, "root.pxar", $rootdir, '--allow-existing-dirs'];
53 if ($no_unpack_error) {
54 push(@$param, '--ignore-extract-device-errors');
57 PVE
::Storage
::PBSPlugin
::run_raw_client_cmd
(
58 $scfg, $storeid, $cmd, $param, userns_cmd
=> $userns_cmd);
61 sub restore_tar_archive
{
62 my ($archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
64 my ($id_map, $root_uid, $root_gid) = PVE
::LXC
::parse_id_maps
($conf);
65 my $userns_cmd = PVE
::LXC
::userns_command
($id_map);
68 my $tar_input = '<&STDIN';
70 if ($archive ne '-') {
71 # GNU tar refuses to autodetect this... *sigh*
72 my %compression_map = (
79 if ($archive =~ /\.tar(\.[^.]+)?$/) {
81 die "unrecognized compression format: $1\n" if !defined($compression_map{$1});
82 @compression_opt = $compression_map{$1};
85 die "file does not look like a template archive: $archive\n";
87 sysopen($archive_fh, $archive, O_RDONLY
)
88 or die "failed to open '$archive': $!\n";
89 my $flags = $archive_fh->fcntl(Fcntl
::F_GETFD
(), 0);
90 $archive_fh->fcntl(Fcntl
::F_SETFD
(), $flags & ~(Fcntl
::FD_CLOEXEC
()));
91 $tar_input = '<&'.fileno($archive_fh);
94 my $cmd = [@$userns_cmd, 'tar', 'xpf', '-', @compression_opt, '--totals',
95 @PVE::Storage
::Plugin
::COMMON_TAR_FLAGS
,
98 # skip-old-files doesn't have anything to do with time (old/new), but is
99 # simply -k (annoyingly also called --keep-old-files) without the 'treat
100 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
102 push @$cmd, '--skip-old-files';
103 push @$cmd, '--anchored';
104 push @$cmd, '--exclude' , './dev/*';
106 if (defined($bwlimit)) {
107 $cmd = [ ['cstream', '-t', $bwlimit*1024], $cmd ];
110 if ($archive eq '-') {
111 print "extracting archive from STDIN\n";
113 print "extracting archive '$archive'\n";
115 eval { PVE
::Tools
::run_command
($cmd, input
=> $tar_input); };
117 close($archive_fh) if defined $archive_fh;
118 die $err if $err && !$no_unpack_error;
122 my ($storage_cfg, $volid, $vmid) = @_;
124 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
125 if (defined($storeid)) {
126 my $scfg = PVE
::Storage
::storage_check_enabled
($storage_cfg, $storeid);
127 if ($scfg->{type
} eq 'pbs') {
128 return recover_config_from_proxmox_backup
($storage_cfg, $volid, $vmid);
132 my $archive = PVE
::Storage
::abs_filesystem_path
($storage_cfg, $volid);
133 recover_config_from_tar
($archive, $vmid);
136 sub recover_config_from_proxmox_backup
{
137 my ($storage_cfg, $volid, $vmid) = @_;
141 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
142 my $scfg = PVE
::Storage
::storage_config
($storage_cfg, $storeid);
144 my ($vtype, $name, undef, undef, undef, undef, $format) =
145 PVE
::Storage
::parse_volname
($storage_cfg, $volid);
147 die "got unexpected vtype '$vtype'\n" if $vtype ne 'backup';
149 die "got unexpected backup format '$format'\n" if $format ne 'pbs-ct';
152 my $param = [$name, "pct.conf", "-"];
155 my $outfunc = sub { my $line = shift; $raw .= "$line\n"; };
156 PVE
::Storage
::PBSPlugin
::run_raw_client_cmd
(
157 $scfg, $storeid, $cmd, $param, outfunc
=> $outfunc);
159 my $conf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/${vmid}.conf" , $raw);
161 delete $conf->{snapshots
};
164 PVE
::LXC
::Config-
>foreach_volume($conf, sub {
165 my ($ms, $mountpoint) = @_;
166 $mp_param->{$ms} = $conf->{$ms};
169 return wantarray ?
($conf, $mp_param) : $conf;
172 sub recover_config_from_tar
{
173 my ($archive, $vmid) = @_;
175 my ($raw, $conf_file) = PVE
::Storage
::extract_vzdump_config_tar
($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
180 if ($conf_file =~ m/pct\.conf/) {
182 $conf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/${vmid}.conf" , $raw);
184 delete $conf->{snapshots
};
186 PVE
::LXC
::Config-
>foreach_volume($conf, sub {
187 my ($ms, $mountpoint) = @_;
188 $mp_param->{$ms} = $conf->{$ms};
191 } elsif ($conf_file =~ m/vps\.conf/) {
193 ($conf, $mp_param) = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
197 die "internal error";
200 return wantarray ?
($conf, $mp_param) : $conf;
203 sub restore_configuration
{
204 my ($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
206 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($archive, 1);
207 if (defined($storeid)) {
208 my $scfg = PVE
::Storage
::storage_config
($storage_cfg, $storeid);
209 if ($scfg->{type
} eq 'pbs') {
210 return restore_configuration_from_proxmox_backup
($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw);
213 restore_configuration_from_etc_vzdump
($vmid, $rootdir, $conf, $restricted, $unique, $skip_fw);
216 sub restore_configuration_from_proxmox_backup
{
217 my ($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
219 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($archive);
220 my $scfg = PVE
::Storage
::storage_config
($storage_cfg, $storeid);
222 my ($vtype, $name, undef, undef, undef, undef, $format) =
223 PVE
::Storage
::parse_volname
($storage_cfg, $archive);
225 my $oldconf = recover_config_from_proxmox_backup
($storage_cfg, $archive, $vmid);
227 sanitize_and_merge_config
($conf, $oldconf, $restricted, $unique);
231 my $list = PVE
::Storage
::PBSPlugin
::run_client_cmd
($scfg, $storeid, "files", [$name]);
232 my $has_fw_conf = grep { $_->{filename
} eq 'fw.conf.blob' } @$list;
235 my $pve_firewall_dir = '/etc/pve/firewall';
236 my $pct_fwcfg_target = "${pve_firewall_dir}/${vmid}.fw";
238 warn "ignoring firewall config from backup archive's 'fw.conf', lacking API permission to modify firewall.\n";
239 warn "old firewall configuration in '$pct_fwcfg_target' left in place!\n"
240 if -e
$pct_fwcfg_target;
242 mkdir $pve_firewall_dir; # make sure the directory exists
243 unlink $pct_fwcfg_target;
246 my $param = [$name, "fw.conf", $pct_fwcfg_target];
247 PVE
::Storage
::PBSPlugin
::run_raw_client_cmd
($scfg, $storeid, $cmd, $param);
252 sub sanitize_and_merge_config
{
253 my ($conf, $oldconf, $restricted, $unique) = @_;
255 my $rpcenv = PVE
::RPCEnvironment
::get
();
256 my $authuser = $rpcenv->get_user();
258 foreach my $key (keys %$oldconf) {
259 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
260 next if $key =~ /^mp\d+$/; # don't recover mountpoints
261 next if $key =~ /^unused\d+$/; # don't recover unused disks
262 # we know if it was a template in the restore API call and check if the target
263 # storage supports creating a template there
264 next if $key =~ /^template$/;
266 if ($restricted && $key eq 'features' && !$conf->{unprivileged
} && $oldconf->{unprivileged
}) {
267 warn "changing from unprivileged to privileged, skipping features\n";
271 if ($key eq 'lxc' && $restricted) {
272 my $lxc_list = $oldconf->{'lxc'};
274 my $msg = "skipping custom lxc options, restore manually as root:\n";
275 $msg .= "--------------------------------\n";
276 foreach my $lxc_opt (@$lxc_list) {
277 $msg .= "$lxc_opt->[0]: $lxc_opt->[1]\n"
279 $msg .= "--------------------------------";
286 if ($key =~ /^net\d+$/ && !defined($conf->{$key})) {
287 PVE
::LXC
::check_bridge_access
($rpcenv, $authuser, $oldconf->{$key});
290 if ($unique && $key =~ /^net\d+$/) {
291 my $net = PVE
::LXC
::Config-
>parse_lxc_network($oldconf->{$key});
292 my $dc = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
293 $net->{hwaddr
} = PVE
::Tools
::random_ether_addr
($dc->{mac_prefix
});
294 $conf->{$key} = PVE
::LXC
::Config-
>print_lxc_network($net);
297 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
301 sub restore_configuration_from_etc_vzdump
{
302 my ($vmid, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
304 # restore: try to extract configuration from archive
306 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
307 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
308 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
309 if (-f
$pct_cfg_fn) {
310 my $raw = PVE
::Tools
::file_get_contents
($pct_cfg_fn);
311 my $oldconf = PVE
::LXC
::Config
::parse_pct_config
("/lxc/$vmid.conf", $raw);
313 sanitize_and_merge_config
($conf, $oldconf, $restricted, $unique);
317 # note: this file is possibly from the container itself in backups
318 # created prior to pve-container 2.0-40 (PVE 5.x) / 3.0-5 (PVE 6.x)
319 # only copy non-empty, non-symlink files, and only if the user is
320 # allowed to modify the firewall config anyways
321 if (-f
$pct_fwcfg_fn && ! -l
$pct_fwcfg_fn && -s
$pct_fwcfg_fn) {
322 my $pve_firewall_dir = '/etc/pve/firewall';
323 my $pct_fwcfg_target = "${pve_firewall_dir}/${vmid}.fw";
325 warn "ignoring firewall config from backup archive's '$pct_fwcfg_fn', lacking API permission to modify firewall.\n";
326 warn "old firewall configuration in '$pct_fwcfg_target' left in place!\n"
327 if -e
$pct_fwcfg_target;
329 mkdir $pve_firewall_dir; # make sure the directory exists
330 PVE
::Tools
::file_copy
($pct_fwcfg_fn, $pct_fwcfg_target);
332 unlink $pct_fwcfg_fn;
335 } elsif (-f
$ovz_cfg_fn) {
336 print "###########################################################\n";
337 print "Converting OpenVZ configuration to LXC.\n";
338 print "Please check the configuration and reconfigure the network.\n";
339 print "###########################################################\n";
341 my $lxc_setup = PVE
::LXC
::Setup-
>new($conf, $rootdir); # detect OS
342 $conf->{ostype
} = $lxc_setup->{conf
}->{ostype
};
343 my $raw = PVE
::Tools
::file_get_contents
($ovz_cfg_fn);
344 my $oldconf = PVE
::VZDump
::ConvertOVZ
::convert_ovz
($raw);
345 foreach my $key (keys %$oldconf) {
346 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
351 print "###########################################################\n";
352 print "Backup archive does not contain any configuration\n";
353 print "###########################################################\n";