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