]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Create.pm
setup: warn on unknown os-release ID
[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',
f70a3d47
WB
126 );
127 if ($archive =~ /\.tar(\.[^.]+)?$/) {
128 if (defined($1)) {
68300601 129 die "unrecognized compression format: $1\n" if !defined($compression_map{$1});
539660e2 130 @compression_opt = $compression_map{$1};
f70a3d47
WB
131 }
132 } else {
133 die "file does not look like a template archive: $archive\n";
134 }
07084526
WB
135 sysopen($archive_fh, $archive, O_RDONLY)
136 or die "failed to open '$archive': $!\n";
07084526
WB
137 my $flags = $archive_fh->fcntl(Fcntl::F_GETFD(), 0);
138 $archive_fh->fcntl(Fcntl::F_SETFD(), $flags & ~(Fcntl::FD_CLOEXEC()));
f70a3d47 139 $tar_input = '<&'.fileno($archive_fh);
07084526
WB
140 }
141
f70a3d47 142 my $cmd = [@$userns_cmd, 'tar', 'xpf', '-', @compression_opt, '--totals',
5fa038ab 143 @PVE::Storage::Plugin::COMMON_TAR_FLAGS,
fc4e132e 144 '-C', $rootdir];
5b4657d0 145
112aeeb4
WB
146 # skip-old-files doesn't have anything to do with time (old/new), but is
147 # simply -k (annoyingly also called --keep-old-files) without the 'treat
148 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
149 # *sigh*, gnu...
150 push @$cmd, '--skip-old-files';
5b4657d0
DM
151 push @$cmd, '--anchored';
152 push @$cmd, '--exclude' , './dev/*';
153
f9b4407e
WB
154 if (defined($bwlimit)) {
155 $cmd = [ ['cstream', '-t', $bwlimit*1024], $cmd ];
156 }
157
6034ae50
DM
158 if ($archive eq '-') {
159 print "extracting archive from STDIN\n";
27916659 160 } else {
6034ae50 161 print "extracting archive '$archive'\n";
27916659 162 }
f70a3d47 163 eval { PVE::Tools::run_command($cmd, input => $tar_input); };
07084526
WB
164 my $err = $@;
165 close($archive_fh) if defined $archive_fh;
166 die $err if $err && !$no_unpack_error;
f31bd6ae
DC
167
168 # if arch is set, we do not try to autodetect it
169 return if defined($conf->{arch});
170
fbb31447 171 $conf->{arch} = detect_architecture($rootdir);
5b4657d0
DM
172}
173
f507c3a7 174sub recover_config {
99a0bcb0
DM
175 my ($storage_cfg, $volid) = @_;
176
177 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
178 if (defined($storeid)) {
179 my $scfg = PVE::Storage::storage_check_enabled($storage_cfg, $storeid);
180 if ($scfg->{type} eq 'pbs') {
181 return recover_config_from_proxmox_backup($storage_cfg, $volid);
182 }
183 }
184
185 my $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $volid);
186 recover_config_from_tar($archive);
187}
188
189sub recover_config_from_proxmox_backup {
190 my ($storage_cfg, $volid) = @_;
191
192 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
193 my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
194
195 my ($vtype, $name, undef, undef, undef, undef, $format) =
196 PVE::Storage::parse_volname($storage_cfg, $volid);
197
198 die "got unexpected vtype '$vtype'\n" if $vtype ne 'backup';
199
200 die "got unexpected backup format '$format'\n" if $format ne 'pbs-ct';
201
202 my $cmd = "restore";
203 my $param = [$name, "pct.conf", "-"];
204
205 my $raw = '';
206 my $outfunc = sub { my $line = shift; $raw .= "$line\n"; };
207 PVE::Storage::PBSPlugin::run_raw_client_cmd(
208 $scfg, $storeid, $cmd, $param, outfunc => $outfunc);
209
210 my $conf = PVE::LXC::Config::parse_pct_config("/lxc/0.conf" , $raw);
211
212 delete $conf->{snapshots};
213
214 my $mp_param = {};
215 PVE::LXC::Config->foreach_mountpoint($conf, sub {
216 my ($ms, $mountpoint) = @_;
217 $mp_param->{$ms} = $conf->{$ms};
218 });
219
220 return wantarray ? ($conf, $mp_param) : $conf;
221}
222
223sub recover_config_from_tar {
effa4f43 224 my ($archive) = @_;
f507c3a7 225
0550795a 226 my ($raw, $conf_file) = PVE::Storage::extract_vzdump_config_tar($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
effa4f43 227 my $conf;
db18c1e4 228 my $mp_param = {};
f507c3a7 229
27916659 230 if ($conf_file =~ m/pct\.conf/) {
f507c3a7 231
1b4cf758 232 $conf = PVE::LXC::Config::parse_pct_config("/lxc/0.conf" , $raw);
f507c3a7 233
27916659 234 delete $conf->{snapshots};
db18c1e4
FG
235
236 PVE::LXC::Config->foreach_mountpoint($conf, sub {
237 my ($ms, $mountpoint) = @_;
238 $mp_param->{$ms} = $conf->{$ms};
239 });
240
effa4f43 241 } elsif ($conf_file =~ m/vps\.conf/) {
db18c1e4
FG
242
243 ($conf, $mp_param) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
244
effa4f43
DM
245 } else {
246
27916659 247 die "internal error";
f507c3a7
WL
248 }
249
db18c1e4 250 return wantarray ? ($conf, $mp_param) : $conf;
f507c3a7
WL
251}
252
51665c2d 253sub restore_configuration {
99a0bcb0
DM
254 my ($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
255
256 my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive);
257 if (defined($storeid)) {
258 my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
259 if ($scfg->{type} eq 'pbs') {
260 return restore_configuration_from_proxmox_backup($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw);
261 }
262 }
263 restore_configuration_from_etc_vzdump($vmid, $rootdir, $conf, $restricted, $unique, $skip_fw);
264}
265
266sub restore_configuration_from_proxmox_backup {
267 my ($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
268
269 my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive);
270 my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
271
272 my ($vtype, $name, undef, undef, undef, undef, $format) =
273 PVE::Storage::parse_volname($storage_cfg, $archive);
274
275 my $oldconf = recover_config_from_proxmox_backup($storage_cfg, $archive);
276
277 sanitize_and_merge_config($conf, $oldconf, $restricted, $unique);
278
279 my $cmd = "files";
280
281 my $list = PVE::Storage::PBSPlugin::run_client_cmd($scfg, $storeid, "files", [$name]);
282 my $has_fw_conf = grep { $_ eq 'fw.conf' } @$list;
283
284 if ($has_fw_conf) {
285 my $pve_firewall_dir = '/etc/pve/firewall';
286 my $pct_fwcfg_target = "${pve_firewall_dir}/${vmid}.fw";
287 if ($skip_fw) {
288 warn "ignoring firewall config from backup archive's 'fw.conf', lacking API permission to modify firewall.\n";
289 warn "old firewall configuration in '$pct_fwcfg_target' left in place!\n"
290 if -e $pct_fwcfg_target;
291 } else {
292 mkdir $pve_firewall_dir; # make sure the directory exists
293
294 my $cmd = "restore";
295 my $param = [$name, "fw.conf", $pct_fwcfg_target];
296 PVE::Storage::PBSPlugin::run_raw_client_cmd($scfg, $storeid, $cmd, $param);
297 }
298 }
299}
300
301sub sanitize_and_merge_config {
302 my ($conf, $oldconf, $restricted, $unique) = @_;
303
304 foreach my $key (keys %$oldconf) {
305 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
306 next if $key =~ /^mp\d+$/; # don't recover mountpoints
307 next if $key =~ /^unused\d+$/; # don't recover unused disks
308 # we know if it was a template in the restore API call and check if the target
309 # storage supports creating a template there
310 next if $key =~ /^template$/;
311
312 if ($key eq 'lxc' && $restricted) {
313 my $lxc_list = $oldconf->{'lxc'};
314 warn "skipping custom lxc options, restore manually as root:\n";
315 warn "--------------------------------\n";
316 foreach my $lxc_opt (@$lxc_list) {
317 warn "$lxc_opt->[0]: $lxc_opt->[1]\n"
318 }
319 warn "--------------------------------\n";
320 next;
321 }
322
323 if ($unique && $key =~ /^net\d+$/) {
324 my $net = PVE::LXC::Config->parse_lxc_network($oldconf->{$key});
325 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
326 $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
327 $conf->{$key} = PVE::LXC::Config->print_lxc_network($net);
328 next;
329 }
330 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
331 }
332}
333
334sub restore_configuration_from_etc_vzdump {
39170644 335 my ($vmid, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
51665c2d
FG
336
337 # restore: try to extract configuration from archive
338
339 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
340 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
341 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
342 if (-f $pct_cfg_fn) {
343 my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
344 my $oldconf = PVE::LXC::Config::parse_pct_config("/lxc/$vmid.conf", $raw);
345
99a0bcb0 346 sanitize_and_merge_config($conf, $oldconf, $restricted, $unique);
82bfeccb 347
51665c2d 348 unlink($pct_cfg_fn);
5b4657d0 349
39170644
FG
350 # note: this file is possibly from the container itself in backups
351 # created prior to pve-container 2.0-40 (PVE 5.x) / 3.0-5 (PVE 6.x)
352 # only copy non-empty, non-symlink files, and only if the user is
353 # allowed to modify the firewall config anyways
354 if (-f $pct_fwcfg_fn && ! -l $pct_fwcfg_fn && -s $pct_fwcfg_fn) {
51665c2d 355 my $pve_firewall_dir = '/etc/pve/firewall';
39170644
FG
356 my $pct_fwcfg_target = "${pve_firewall_dir}/${vmid}.fw";
357 if ($skip_fw) {
358 warn "ignoring firewall config from backup archive's '$pct_fwcfg_fn', lacking API permission to modify firewall.\n";
359 warn "old firewall configuration in '$pct_fwcfg_target' left in place!\n"
360 if -e $pct_fwcfg_target;
361 } else {
362 mkdir $pve_firewall_dir; # make sure the directory exists
363 PVE::Tools::file_copy($pct_fwcfg_fn, $pct_fwcfg_target);
364 }
51665c2d 365 unlink $pct_fwcfg_fn;
27916659 366 }
5b4657d0 367
51665c2d
FG
368 } elsif (-f $ovz_cfg_fn) {
369 print "###########################################################\n";
370 print "Converting OpenVZ configuration to LXC.\n";
371 print "Please check the configuration and reconfigure the network.\n";
372 print "###########################################################\n";
148d1cb4 373
51665c2d
FG
374 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
375 $conf->{ostype} = $lxc_setup->{conf}->{ostype};
376 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
377 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
378 foreach my $key (keys %$oldconf) {
379 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
380 }
381 unlink($ovz_cfg_fn);
148d1cb4 382
51665c2d
FG
383 } else {
384 print "###########################################################\n";
385 print "Backup archive does not contain any configuration\n";
386 print "###########################################################\n";
148d1cb4 387 }
5b4657d0
DM
388}
389
3901;