]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Create.pm
restore: pass target vmid to config recovery
[pve-container.git] / src / PVE / LXC / Create.pm
CommitLineData
7af97ad5 1package PVE::LXC::Create;
5b4657d0
DM
2
3use strict;
4use warnings;
5use File::Basename;
6use File::Path;
07084526 7use Fcntl;
5b4657d0 8
99a0bcb0 9use PVE::Storage::PBSPlugin;
5b4657d0 10use PVE::Storage;
8b076579 11use PVE::DataCenterConfig;
5b4657d0 12use PVE::LXC;
7af97ad5 13use PVE::LXC::Setup;
f507c3a7 14use PVE::VZDump::ConvertOVZ;
580b6916 15use PVE::Tools;
dc6e862c 16use POSIX;
5b4657d0 17
fbb31447
TL
18sub detect_architecture {
19 my ($rootdir) = @_;
dc6e862c 20
e1d54a38
DM
21 # see https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
22
23 my $supported_elf_machine = {
24 0x03 => 'i386',
25 0x3e => 'amd64',
26 0x28 => 'armhf',
27 0xb7 => 'arm64',
fbb31447 28 };
dc6e862c 29
fbb31447
TL
30 my $elf_fn = '/bin/sh'; # '/bin/sh' is POSIX mandatory
31 my $detect_arch = sub {
dc6e862c
TL
32 # chroot avoids a problem where we check the binary of the host system
33 # if $elf_fn is an absolut symlink (e.g. $rootdir/bin/sh -> /bin/bash)
34 chroot($rootdir) or die "chroot '$rootdir' failed: $!\n";
35 chdir('/') or die "failed to change to root directory\n";
36
fbb31447 37 open(my $fh, "<", $elf_fn) or die "open '$elf_fn' failed: $!\n";
dc6e862c
TL
38 binmode($fh);
39
e1d54a38 40 my $length = read($fh, my $data, 20) or die "read failed: $!\n";
dc6e862c 41
e1d54a38
DM
42 # 4 bytes ELF magic number and 1 byte ELF class, padding, machine
43 my ($magic, $class, undef, $machine) = unpack("A4CA12n", $data);
dc6e862c
TL
44
45 die "'$elf_fn' does not resolve to an ELF!\n"
46 if (!defined($class) || !defined($magic) || $magic ne "\177ELF");
47
e1d54a38
DM
48 my $arch = $supported_elf_machine->{$machine};
49 die "'$elf_fn' has unknown ELF machine '$machine'!\n"
50 if !defined($arch);
dc6e862c 51
e1d54a38 52 return $arch;
fbb31447 53 };
dc6e862c 54
fbb31447
TL
55 my $arch = eval { PVE::Tools::run_fork_with_timeout(5, $detect_arch) };
56 if (my $err = $@) {
57 $arch = 'amd64';
58 print "Architecture detection failed: $err\nFalling back to amd64.\n" .
59 "Use `pct set VMID --arch ARCH` to change.\n";
dc6e862c 60 } else {
fbb31447 61 print "Detected container architecture: $arch\n";
dc6e862c
TL
62 }
63
fbb31447 64 return $arch;
dc6e862c
TL
65}
66
5b4657d0 67sub restore_archive {
99a0bcb0
DM
68 my ($storage_cfg, $archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
69
70 my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive, 1);
71 if (defined($storeid)) {
72 my $scfg = PVE::Storage::storage_check_enabled($storage_cfg, $storeid);
73 if ($scfg->{type} eq 'pbs') {
74 return restore_proxmox_backup_archive($storage_cfg, $archive, $rootdir, $conf, $no_unpack_error, $bwlimit);
75 }
76 }
77
78 $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $archive) if $archive ne '-';
79 restore_tar_archive($archive, $rootdir, $conf, $no_unpack_error, $bwlimit);
80}
81
82sub restore_proxmox_backup_archive {
83 my ($storage_cfg, $archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
84
85 my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive);
86 my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
87
88 my ($vtype, $name, undef, undef, undef, undef, $format) =
89 PVE::Storage::parse_volname($storage_cfg, $archive);
90
91 die "got unexpected vtype '$vtype'\n" if $vtype ne 'backup';
92
93 die "got unexpected backup format '$format'\n" if $format ne 'pbs-ct';
94
95 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
96 my $userns_cmd = PVE::LXC::userns_command($id_map);
97
98 my $cmd = "restore";
99 my $param = [$name, "root.pxar", $rootdir, '--allow-existing-dirs'];
100
101 PVE::Storage::PBSPlugin::run_raw_client_cmd(
102 $scfg, $storeid, $cmd, $param, userns_cmd => $userns_cmd);
103
104 # if arch is set, we do not try to autodetect it
105 return if defined($conf->{arch});
106
107 $conf->{arch} = detect_architecture($rootdir);
108}
109
110sub restore_tar_archive {
f9b4407e 111 my ($archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
5b4657d0 112
c6a605f9 113 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
01dce99b 114 my $userns_cmd = PVE::LXC::userns_command($id_map);
5b4657d0 115
07084526 116 my $archive_fh;
f70a3d47
WB
117 my $tar_input = '<&STDIN';
118 my @compression_opt;
07084526 119 if ($archive ne '-') {
f70a3d47
WB
120 # GNU tar refuses to autodetect this... *sigh*
121 my %compression_map = (
122 '.gz' => '-z',
123 '.bz2' => '-j',
124 '.xz' => '-J',
a3bdf0c9 125 '.lzo' => '--lzop',
cef90911 126 '.zst' => '--zstd',
f70a3d47
WB
127 );
128 if ($archive =~ /\.tar(\.[^.]+)?$/) {
129 if (defined($1)) {
68300601 130 die "unrecognized compression format: $1\n" if !defined($compression_map{$1});
539660e2 131 @compression_opt = $compression_map{$1};
f70a3d47
WB
132 }
133 } else {
134 die "file does not look like a template archive: $archive\n";
135 }
07084526
WB
136 sysopen($archive_fh, $archive, O_RDONLY)
137 or die "failed to open '$archive': $!\n";
07084526
WB
138 my $flags = $archive_fh->fcntl(Fcntl::F_GETFD(), 0);
139 $archive_fh->fcntl(Fcntl::F_SETFD(), $flags & ~(Fcntl::FD_CLOEXEC()));
f70a3d47 140 $tar_input = '<&'.fileno($archive_fh);
07084526
WB
141 }
142
f70a3d47 143 my $cmd = [@$userns_cmd, 'tar', 'xpf', '-', @compression_opt, '--totals',
5fa038ab 144 @PVE::Storage::Plugin::COMMON_TAR_FLAGS,
fc4e132e 145 '-C', $rootdir];
5b4657d0 146
112aeeb4
WB
147 # skip-old-files doesn't have anything to do with time (old/new), but is
148 # simply -k (annoyingly also called --keep-old-files) without the 'treat
149 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
150 # *sigh*, gnu...
151 push @$cmd, '--skip-old-files';
5b4657d0
DM
152 push @$cmd, '--anchored';
153 push @$cmd, '--exclude' , './dev/*';
154
f9b4407e
WB
155 if (defined($bwlimit)) {
156 $cmd = [ ['cstream', '-t', $bwlimit*1024], $cmd ];
157 }
158
6034ae50
DM
159 if ($archive eq '-') {
160 print "extracting archive from STDIN\n";
27916659 161 } else {
6034ae50 162 print "extracting archive '$archive'\n";
27916659 163 }
f70a3d47 164 eval { PVE::Tools::run_command($cmd, input => $tar_input); };
07084526
WB
165 my $err = $@;
166 close($archive_fh) if defined $archive_fh;
167 die $err if $err && !$no_unpack_error;
f31bd6ae
DC
168
169 # if arch is set, we do not try to autodetect it
170 return if defined($conf->{arch});
171
fbb31447 172 $conf->{arch} = detect_architecture($rootdir);
5b4657d0
DM
173}
174
f507c3a7 175sub recover_config {
12aec1d2 176 my ($storage_cfg, $volid, $vmid) = @_;
99a0bcb0
DM
177
178 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
179 if (defined($storeid)) {
180 my $scfg = PVE::Storage::storage_check_enabled($storage_cfg, $storeid);
181 if ($scfg->{type} eq 'pbs') {
12aec1d2 182 return recover_config_from_proxmox_backup($storage_cfg, $volid, $vmid);
99a0bcb0
DM
183 }
184 }
185
186 my $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $volid);
12aec1d2 187 recover_config_from_tar($archive, $vmid);
99a0bcb0
DM
188}
189
190sub recover_config_from_proxmox_backup {
12aec1d2
FG
191 my ($storage_cfg, $volid, $vmid) = @_;
192
193 $vmid //= 0;
99a0bcb0
DM
194
195 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
196 my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
197
198 my ($vtype, $name, undef, undef, undef, undef, $format) =
199 PVE::Storage::parse_volname($storage_cfg, $volid);
200
201 die "got unexpected vtype '$vtype'\n" if $vtype ne 'backup';
202
203 die "got unexpected backup format '$format'\n" if $format ne 'pbs-ct';
204
205 my $cmd = "restore";
206 my $param = [$name, "pct.conf", "-"];
207
208 my $raw = '';
209 my $outfunc = sub { my $line = shift; $raw .= "$line\n"; };
210 PVE::Storage::PBSPlugin::run_raw_client_cmd(
211 $scfg, $storeid, $cmd, $param, outfunc => $outfunc);
212
12aec1d2 213 my $conf = PVE::LXC::Config::parse_pct_config("/lxc/${vmid}.conf" , $raw);
99a0bcb0
DM
214
215 delete $conf->{snapshots};
216
217 my $mp_param = {};
015740e6 218 PVE::LXC::Config->foreach_volume($conf, sub {
99a0bcb0
DM
219 my ($ms, $mountpoint) = @_;
220 $mp_param->{$ms} = $conf->{$ms};
221 });
222
223 return wantarray ? ($conf, $mp_param) : $conf;
224}
225
226sub recover_config_from_tar {
12aec1d2 227 my ($archive, $vmid) = @_;
f507c3a7 228
0550795a 229 my ($raw, $conf_file) = PVE::Storage::extract_vzdump_config_tar($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
effa4f43 230 my $conf;
db18c1e4 231 my $mp_param = {};
12aec1d2 232 $vmid //= 0;
f507c3a7 233
27916659 234 if ($conf_file =~ m/pct\.conf/) {
f507c3a7 235
12aec1d2 236 $conf = PVE::LXC::Config::parse_pct_config("/lxc/${vmid}.conf" , $raw);
f507c3a7 237
27916659 238 delete $conf->{snapshots};
db18c1e4 239
015740e6 240 PVE::LXC::Config->foreach_volume($conf, sub {
db18c1e4
FG
241 my ($ms, $mountpoint) = @_;
242 $mp_param->{$ms} = $conf->{$ms};
243 });
244
effa4f43 245 } elsif ($conf_file =~ m/vps\.conf/) {
db18c1e4
FG
246
247 ($conf, $mp_param) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
248
effa4f43
DM
249 } else {
250
27916659 251 die "internal error";
f507c3a7
WL
252 }
253
db18c1e4 254 return wantarray ? ($conf, $mp_param) : $conf;
f507c3a7
WL
255}
256
51665c2d 257sub restore_configuration {
99a0bcb0
DM
258 my ($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
259
3e111b4e 260 my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive, 1);
99a0bcb0
DM
261 if (defined($storeid)) {
262 my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
263 if ($scfg->{type} eq 'pbs') {
264 return restore_configuration_from_proxmox_backup($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw);
265 }
266 }
267 restore_configuration_from_etc_vzdump($vmid, $rootdir, $conf, $restricted, $unique, $skip_fw);
268}
269
270sub restore_configuration_from_proxmox_backup {
271 my ($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
272
273 my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive);
274 my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
275
276 my ($vtype, $name, undef, undef, undef, undef, $format) =
277 PVE::Storage::parse_volname($storage_cfg, $archive);
278
12aec1d2 279 my $oldconf = recover_config_from_proxmox_backup($storage_cfg, $archive, $vmid);
99a0bcb0
DM
280
281 sanitize_and_merge_config($conf, $oldconf, $restricted, $unique);
282
283 my $cmd = "files";
284
285 my $list = PVE::Storage::PBSPlugin::run_client_cmd($scfg, $storeid, "files", [$name]);
08db7875 286 my $has_fw_conf = grep { $_->{filename} eq 'fw.conf.blob' } @$list;
99a0bcb0
DM
287
288 if ($has_fw_conf) {
289 my $pve_firewall_dir = '/etc/pve/firewall';
290 my $pct_fwcfg_target = "${pve_firewall_dir}/${vmid}.fw";
291 if ($skip_fw) {
292 warn "ignoring firewall config from backup archive's 'fw.conf', lacking API permission to modify firewall.\n";
293 warn "old firewall configuration in '$pct_fwcfg_target' left in place!\n"
294 if -e $pct_fwcfg_target;
295 } else {
296 mkdir $pve_firewall_dir; # make sure the directory exists
297
298 my $cmd = "restore";
299 my $param = [$name, "fw.conf", $pct_fwcfg_target];
300 PVE::Storage::PBSPlugin::run_raw_client_cmd($scfg, $storeid, $cmd, $param);
301 }
302 }
303}
304
305sub sanitize_and_merge_config {
306 my ($conf, $oldconf, $restricted, $unique) = @_;
307
308 foreach my $key (keys %$oldconf) {
309 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
310 next if $key =~ /^mp\d+$/; # don't recover mountpoints
311 next if $key =~ /^unused\d+$/; # don't recover unused disks
312 # we know if it was a template in the restore API call and check if the target
313 # storage supports creating a template there
314 next if $key =~ /^template$/;
315
316 if ($key eq 'lxc' && $restricted) {
317 my $lxc_list = $oldconf->{'lxc'};
318 warn "skipping custom lxc options, restore manually as root:\n";
319 warn "--------------------------------\n";
320 foreach my $lxc_opt (@$lxc_list) {
321 warn "$lxc_opt->[0]: $lxc_opt->[1]\n"
322 }
323 warn "--------------------------------\n";
324 next;
325 }
326
327 if ($unique && $key =~ /^net\d+$/) {
328 my $net = PVE::LXC::Config->parse_lxc_network($oldconf->{$key});
329 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
330 $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
331 $conf->{$key} = PVE::LXC::Config->print_lxc_network($net);
332 next;
333 }
334 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
335 }
336}
337
338sub restore_configuration_from_etc_vzdump {
39170644 339 my ($vmid, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
51665c2d
FG
340
341 # restore: try to extract configuration from archive
342
343 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
344 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
345 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
346 if (-f $pct_cfg_fn) {
347 my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
348 my $oldconf = PVE::LXC::Config::parse_pct_config("/lxc/$vmid.conf", $raw);
349
99a0bcb0 350 sanitize_and_merge_config($conf, $oldconf, $restricted, $unique);
82bfeccb 351
51665c2d 352 unlink($pct_cfg_fn);
5b4657d0 353
39170644
FG
354 # note: this file is possibly from the container itself in backups
355 # created prior to pve-container 2.0-40 (PVE 5.x) / 3.0-5 (PVE 6.x)
356 # only copy non-empty, non-symlink files, and only if the user is
357 # allowed to modify the firewall config anyways
358 if (-f $pct_fwcfg_fn && ! -l $pct_fwcfg_fn && -s $pct_fwcfg_fn) {
51665c2d 359 my $pve_firewall_dir = '/etc/pve/firewall';
39170644
FG
360 my $pct_fwcfg_target = "${pve_firewall_dir}/${vmid}.fw";
361 if ($skip_fw) {
362 warn "ignoring firewall config from backup archive's '$pct_fwcfg_fn', lacking API permission to modify firewall.\n";
363 warn "old firewall configuration in '$pct_fwcfg_target' left in place!\n"
364 if -e $pct_fwcfg_target;
365 } else {
366 mkdir $pve_firewall_dir; # make sure the directory exists
367 PVE::Tools::file_copy($pct_fwcfg_fn, $pct_fwcfg_target);
368 }
51665c2d 369 unlink $pct_fwcfg_fn;
27916659 370 }
5b4657d0 371
51665c2d
FG
372 } elsif (-f $ovz_cfg_fn) {
373 print "###########################################################\n";
374 print "Converting OpenVZ configuration to LXC.\n";
375 print "Please check the configuration and reconfigure the network.\n";
376 print "###########################################################\n";
148d1cb4 377
51665c2d
FG
378 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
379 $conf->{ostype} = $lxc_setup->{conf}->{ostype};
380 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
381 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
382 foreach my $key (keys %$oldconf) {
383 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
384 }
385 unlink($ovz_cfg_fn);
148d1cb4 386
51665c2d
FG
387 } else {
388 print "###########################################################\n";
389 print "Backup archive does not contain any configuration\n";
390 print "###########################################################\n";
148d1cb4 391 }
5b4657d0
DM
392}
393
3941;