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