]> git.proxmox.com Git - pve-container.git/blame_incremental - src/PVE/LXC/Create.pm
detect_architecture: use ELF machine header to detect ISA
[pve-container.git] / src / PVE / LXC / Create.pm
... / ...
CommitLineData
1package PVE::LXC::Create;
2
3use strict;
4use warnings;
5use File::Basename;
6use File::Path;
7use Data::Dumper;
8use Fcntl;
9
10use PVE::Storage;
11use PVE::LXC;
12use PVE::LXC::Setup;
13use PVE::VZDump::ConvertOVZ;
14use PVE::Tools;
15use POSIX;
16
17sub detect_architecture {
18 my ($rootdir) = @_;
19
20 # see https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
21
22 my $supported_elf_machine = {
23 0x03 => 'i386',
24 0x3e => 'amd64',
25 0x28 => 'armhf',
26 0xb7 => 'arm64',
27 };
28
29 my $elf_fn = '/bin/sh'; # '/bin/sh' is POSIX mandatory
30 my $detect_arch = sub {
31 # chroot avoids a problem where we check the binary of the host system
32 # if $elf_fn is an absolut symlink (e.g. $rootdir/bin/sh -> /bin/bash)
33 chroot($rootdir) or die "chroot '$rootdir' failed: $!\n";
34 chdir('/') or die "failed to change to root directory\n";
35
36 open(my $fh, "<", $elf_fn) or die "open '$elf_fn' failed: $!\n";
37 binmode($fh);
38
39 my $length = read($fh, my $data, 20) or die "read failed: $!\n";
40
41 # 4 bytes ELF magic number and 1 byte ELF class, padding, machine
42 my ($magic, $class, undef, $machine) = unpack("A4CA12n", $data);
43
44 die "'$elf_fn' does not resolve to an ELF!\n"
45 if (!defined($class) || !defined($magic) || $magic ne "\177ELF");
46
47 my $arch = $supported_elf_machine->{$machine};
48 die "'$elf_fn' has unknown ELF machine '$machine'!\n"
49 if !defined($arch);
50
51 return $arch;
52 };
53
54 my $arch = eval { PVE::Tools::run_fork_with_timeout(5, $detect_arch) };
55 if (my $err = $@) {
56 $arch = 'amd64';
57 print "Architecture detection failed: $err\nFalling back to amd64.\n" .
58 "Use `pct set VMID --arch ARCH` to change.\n";
59 } else {
60 print "Detected container architecture: $arch\n";
61 }
62
63 return $arch;
64}
65
66sub restore_archive {
67 my ($archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
68
69 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
70 my $userns_cmd = PVE::LXC::userns_command($id_map);
71
72 my $archive_fh;
73 my $tar_input = '<&STDIN';
74 my @compression_opt;
75 if ($archive ne '-') {
76 # GNU tar refuses to autodetect this... *sigh*
77 my %compression_map = (
78 '.gz' => '-z',
79 '.bz2' => '-j',
80 '.xz' => '-J',
81 '.lzo' => '--lzop',
82 );
83 if ($archive =~ /\.tar(\.[^.]+)?$/) {
84 if (defined($1)) {
85 @compression_opt = $compression_map{$1}
86 or die "unrecognized compression format: $1\n";
87 }
88 } else {
89 die "file does not look like a template archive: $archive\n";
90 }
91 sysopen($archive_fh, $archive, O_RDONLY)
92 or die "failed to open '$archive': $!\n";
93 my $flags = $archive_fh->fcntl(Fcntl::F_GETFD(), 0);
94 $archive_fh->fcntl(Fcntl::F_SETFD(), $flags & ~(Fcntl::FD_CLOEXEC()));
95 $tar_input = '<&'.fileno($archive_fh);
96 }
97
98 my $cmd = [@$userns_cmd, 'tar', 'xpf', '-', @compression_opt, '--totals',
99 @PVE::Storage::Plugin::COMMON_TAR_FLAGS,
100 '-C', $rootdir];
101
102 # skip-old-files doesn't have anything to do with time (old/new), but is
103 # simply -k (annoyingly also called --keep-old-files) without the 'treat
104 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
105 # *sigh*, gnu...
106 push @$cmd, '--skip-old-files';
107 push @$cmd, '--anchored';
108 push @$cmd, '--exclude' , './dev/*';
109
110 if (defined($bwlimit)) {
111 $cmd = [ ['cstream', '-t', $bwlimit*1024], $cmd ];
112 }
113
114 if ($archive eq '-') {
115 print "extracting archive from STDIN\n";
116 } else {
117 print "extracting archive '$archive'\n";
118 }
119 eval { PVE::Tools::run_command($cmd, input => $tar_input); };
120 my $err = $@;
121 close($archive_fh) if defined $archive_fh;
122 die $err if $err && !$no_unpack_error;
123
124 # if arch is set, we do not try to autodetect it
125 return if defined($conf->{arch});
126
127 $conf->{arch} = detect_architecture($rootdir);
128}
129
130sub recover_config {
131 my ($archive) = @_;
132
133 my ($raw, $conf_file) = PVE::Storage::extract_vzdump_config_tar($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
134 my $conf;
135 my $mp_param = {};
136
137 if ($conf_file =~ m/pct\.conf/) {
138
139 $conf = PVE::LXC::Config::parse_pct_config("/lxc/0.conf" , $raw);
140
141 delete $conf->{snapshots};
142 delete $conf->{template}; # restored CT is never a template
143
144 PVE::LXC::Config->foreach_mountpoint($conf, sub {
145 my ($ms, $mountpoint) = @_;
146 $mp_param->{$ms} = $conf->{$ms};
147 });
148
149 } elsif ($conf_file =~ m/vps\.conf/) {
150
151 ($conf, $mp_param) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
152
153 } else {
154
155 die "internal error";
156 }
157
158 return wantarray ? ($conf, $mp_param) : $conf;
159}
160
161sub restore_configuration {
162 my ($vmid, $rootdir, $conf, $restricted) = @_;
163
164 # restore: try to extract configuration from archive
165
166 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
167 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
168 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
169 if (-f $pct_cfg_fn) {
170 my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
171 my $oldconf = PVE::LXC::Config::parse_pct_config("/lxc/$vmid.conf", $raw);
172
173 foreach my $key (keys %$oldconf) {
174 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
175 next if $key =~ /^mp\d+$/; # don't recover mountpoints
176 next if $key =~ /^unused\d+$/; # don't recover unused disks
177 if ($restricted && $key eq 'lxc') {
178 warn "skipping custom lxc options, restore manually as root:\n";
179 warn "--------------------------------\n";
180 my $lxc_list = $oldconf->{'lxc'};
181 foreach my $lxc_opt (@$lxc_list) {
182 warn "$lxc_opt->[0]: $lxc_opt->[1]\n"
183 }
184 warn "--------------------------------\n";
185 next;
186 }
187 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
188 }
189 unlink($pct_cfg_fn);
190
191 if (-f $pct_fwcfg_fn) {
192 my $pve_firewall_dir = '/etc/pve/firewall';
193 mkdir $pve_firewall_dir; # make sure the directory exists
194 PVE::Tools::file_copy($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
195 unlink $pct_fwcfg_fn;
196 }
197
198 } elsif (-f $ovz_cfg_fn) {
199 print "###########################################################\n";
200 print "Converting OpenVZ configuration to LXC.\n";
201 print "Please check the configuration and reconfigure the network.\n";
202 print "###########################################################\n";
203
204 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
205 $conf->{ostype} = $lxc_setup->{conf}->{ostype};
206 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
207 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
208 foreach my $key (keys %$oldconf) {
209 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
210 }
211 unlink($ovz_cfg_fn);
212
213 } else {
214 print "###########################################################\n";
215 print "Backup archive does not contain any configuration\n";
216 print "###########################################################\n";
217 }
218}
219
2201;