]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Create.pm
create: fix passing of template file descriptor
[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 = '<&STDIN';
69 my @compression_opt;
70 if ($archive ne '-') {
71 # GNU tar refuses to autodetect this... *sigh*
72 my %compression_map = (
73 '.gz' => '-z',
74 '.bz2' => '-j',
75 '.xz' => '-J',
76 );
77 if ($archive =~ /\.tar(\.[^.]+)?$/) {
78 if (defined($1)) {
79 @compression_opt = $compression_map{$1}
80 or die "unrecognized compression format: $1\n";
81 }
82 } else {
83 die "file does not look like a template archive: $archive\n";
84 }
85 sysopen($archive_fh, $archive, O_RDONLY)
86 or die "failed to open '$archive': $!\n";
87 my $flags = $archive_fh->fcntl(Fcntl::F_GETFD(), 0);
88 $archive_fh->fcntl(Fcntl::F_SETFD(), $flags & ~(Fcntl::FD_CLOEXEC()));
89 $tar_input = '<&'.fileno($archive_fh);
90 }
91
92 my $cmd = [@$userns_cmd, 'tar', 'xpf', '-', @compression_opt, '--totals',
93 @PVE::Storage::Plugin::COMMON_TAR_FLAGS,
94 '-C', $rootdir];
95
96 # skip-old-files doesn't have anything to do with time (old/new), but is
97 # simply -k (annoyingly also called --keep-old-files) without the 'treat
98 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
99 # *sigh*, gnu...
100 push @$cmd, '--skip-old-files';
101 push @$cmd, '--anchored';
102 push @$cmd, '--exclude' , './dev/*';
103
104 if ($archive eq '-') {
105 print "extracting archive from STDIN\n";
106 } else {
107 print "extracting archive '$archive'\n";
108 }
109 eval { PVE::Tools::run_command($cmd, input => $tar_input); };
110 my $err = $@;
111 close($archive_fh) if defined $archive_fh;
112 die $err if $err && !$no_unpack_error;
113
114 # if arch is set, we do not try to autodetect it
115 return if defined($conf->{arch});
116
117 $conf->{arch} = detect_architecture($rootdir);
118 }
119
120 sub recover_config {
121 my ($archive) = @_;
122
123 my ($raw, $conf_file) = PVE::Storage::extract_vzdump_config_tar($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
124 my $conf;
125 my $mp_param = {};
126
127 if ($conf_file =~ m/pct\.conf/) {
128
129 $conf = PVE::LXC::Config::parse_pct_config("/lxc/0.conf" , $raw);
130
131 delete $conf->{snapshots};
132 delete $conf->{template}; # restored CT is never a template
133
134 PVE::LXC::Config->foreach_mountpoint($conf, sub {
135 my ($ms, $mountpoint) = @_;
136 $mp_param->{$ms} = $conf->{$ms};
137 });
138
139 } elsif ($conf_file =~ m/vps\.conf/) {
140
141 ($conf, $mp_param) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
142
143 } else {
144
145 die "internal error";
146 }
147
148 return wantarray ? ($conf, $mp_param) : $conf;
149 }
150
151 sub restore_configuration {
152 my ($vmid, $rootdir, $conf, $restricted) = @_;
153
154 # restore: try to extract configuration from archive
155
156 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
157 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
158 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
159 if (-f $pct_cfg_fn) {
160 my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
161 my $oldconf = PVE::LXC::Config::parse_pct_config("/lxc/$vmid.conf", $raw);
162
163 foreach my $key (keys %$oldconf) {
164 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
165 next if $key =~ /^mp\d+$/; # don't recover mountpoints
166 next if $key =~ /^unused\d+$/; # don't recover unused disks
167 if ($restricted && $key eq 'lxc') {
168 warn "skipping custom lxc options, restore manually as root:\n";
169 warn "--------------------------------\n";
170 my $lxc_list = $oldconf->{'lxc'};
171 foreach my $lxc_opt (@$lxc_list) {
172 warn "$lxc_opt->[0]: $lxc_opt->[1]\n"
173 }
174 warn "--------------------------------\n";
175 next;
176 }
177 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
178 }
179 unlink($pct_cfg_fn);
180
181 if (-f $pct_fwcfg_fn) {
182 my $pve_firewall_dir = '/etc/pve/firewall';
183 mkdir $pve_firewall_dir; # make sure the directory exists
184 PVE::Tools::file_copy($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
185 unlink $pct_fwcfg_fn;
186 }
187
188 } elsif (-f $ovz_cfg_fn) {
189 print "###########################################################\n";
190 print "Converting OpenVZ configuration to LXC.\n";
191 print "Please check the configuration and reconfigure the network.\n";
192 print "###########################################################\n";
193
194 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
195 $conf->{ostype} = $lxc_setup->{conf}->{ostype};
196 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
197 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
198 foreach my $key (keys %$oldconf) {
199 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
200 }
201 unlink($ovz_cfg_fn);
202
203 } else {
204 print "###########################################################\n";
205 print "Backup archive does not contain any configuration\n";
206 print "###########################################################\n";
207 }
208 }
209
210 1;