]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Create.pm
followup: remove double parenthesis and hook regex
[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;
7use Data::Dumper;
07084526 8use Fcntl;
5b4657d0
DM
9
10use PVE::Storage;
11use PVE::LXC;
7af97ad5 12use PVE::LXC::Setup;
f507c3a7 13use PVE::VZDump::ConvertOVZ;
580b6916 14use PVE::Tools;
dc6e862c 15use POSIX;
5b4657d0 16
fbb31447
TL
17sub detect_architecture {
18 my ($rootdir) = @_;
dc6e862c 19
e1d54a38
DM
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',
fbb31447 27 };
dc6e862c 28
fbb31447
TL
29 my $elf_fn = '/bin/sh'; # '/bin/sh' is POSIX mandatory
30 my $detect_arch = sub {
dc6e862c
TL
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
fbb31447 36 open(my $fh, "<", $elf_fn) or die "open '$elf_fn' failed: $!\n";
dc6e862c
TL
37 binmode($fh);
38
e1d54a38 39 my $length = read($fh, my $data, 20) or die "read failed: $!\n";
dc6e862c 40
e1d54a38
DM
41 # 4 bytes ELF magic number and 1 byte ELF class, padding, machine
42 my ($magic, $class, undef, $machine) = unpack("A4CA12n", $data);
dc6e862c
TL
43
44 die "'$elf_fn' does not resolve to an ELF!\n"
45 if (!defined($class) || !defined($magic) || $magic ne "\177ELF");
46
e1d54a38
DM
47 my $arch = $supported_elf_machine->{$machine};
48 die "'$elf_fn' has unknown ELF machine '$machine'!\n"
49 if !defined($arch);
dc6e862c 50
e1d54a38 51 return $arch;
fbb31447 52 };
dc6e862c 53
fbb31447
TL
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";
dc6e862c 59 } else {
fbb31447 60 print "Detected container architecture: $arch\n";
dc6e862c
TL
61 }
62
fbb31447 63 return $arch;
dc6e862c
TL
64}
65
5b4657d0 66sub restore_archive {
f9b4407e 67 my ($archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
5b4657d0 68
c6a605f9 69 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
01dce99b 70 my $userns_cmd = PVE::LXC::userns_command($id_map);
5b4657d0 71
07084526 72 my $archive_fh;
f70a3d47
WB
73 my $tar_input = '<&STDIN';
74 my @compression_opt;
07084526 75 if ($archive ne '-') {
f70a3d47
WB
76 # GNU tar refuses to autodetect this... *sigh*
77 my %compression_map = (
78 '.gz' => '-z',
79 '.bz2' => '-j',
80 '.xz' => '-J',
a3bdf0c9 81 '.lzo' => '--lzop',
f70a3d47
WB
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 }
07084526
WB
91 sysopen($archive_fh, $archive, O_RDONLY)
92 or die "failed to open '$archive': $!\n";
07084526
WB
93 my $flags = $archive_fh->fcntl(Fcntl::F_GETFD(), 0);
94 $archive_fh->fcntl(Fcntl::F_SETFD(), $flags & ~(Fcntl::FD_CLOEXEC()));
f70a3d47 95 $tar_input = '<&'.fileno($archive_fh);
07084526
WB
96 }
97
f70a3d47 98 my $cmd = [@$userns_cmd, 'tar', 'xpf', '-', @compression_opt, '--totals',
5fa038ab 99 @PVE::Storage::Plugin::COMMON_TAR_FLAGS,
fc4e132e 100 '-C', $rootdir];
5b4657d0 101
112aeeb4
WB
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';
5b4657d0
DM
107 push @$cmd, '--anchored';
108 push @$cmd, '--exclude' , './dev/*';
109
f9b4407e
WB
110 if (defined($bwlimit)) {
111 $cmd = [ ['cstream', '-t', $bwlimit*1024], $cmd ];
112 }
113
6034ae50
DM
114 if ($archive eq '-') {
115 print "extracting archive from STDIN\n";
27916659 116 } else {
6034ae50 117 print "extracting archive '$archive'\n";
27916659 118 }
f70a3d47 119 eval { PVE::Tools::run_command($cmd, input => $tar_input); };
07084526
WB
120 my $err = $@;
121 close($archive_fh) if defined $archive_fh;
122 die $err if $err && !$no_unpack_error;
f31bd6ae
DC
123
124 # if arch is set, we do not try to autodetect it
125 return if defined($conf->{arch});
126
fbb31447 127 $conf->{arch} = detect_architecture($rootdir);
5b4657d0
DM
128}
129
f507c3a7 130sub recover_config {
effa4f43 131 my ($archive) = @_;
f507c3a7 132
0550795a 133 my ($raw, $conf_file) = PVE::Storage::extract_vzdump_config_tar($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
effa4f43 134 my $conf;
db18c1e4 135 my $mp_param = {};
f507c3a7 136
27916659 137 if ($conf_file =~ m/pct\.conf/) {
f507c3a7 138
1b4cf758 139 $conf = PVE::LXC::Config::parse_pct_config("/lxc/0.conf" , $raw);
f507c3a7 140
27916659 141 delete $conf->{snapshots};
bb1ac2de 142 delete $conf->{template}; # restored CT is never a template
db18c1e4
FG
143
144 PVE::LXC::Config->foreach_mountpoint($conf, sub {
145 my ($ms, $mountpoint) = @_;
146 $mp_param->{$ms} = $conf->{$ms};
147 });
148
effa4f43 149 } elsif ($conf_file =~ m/vps\.conf/) {
db18c1e4
FG
150
151 ($conf, $mp_param) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
152
effa4f43
DM
153 } else {
154
27916659 155 die "internal error";
f507c3a7
WL
156 }
157
db18c1e4 158 return wantarray ? ($conf, $mp_param) : $conf;
f507c3a7
WL
159}
160
51665c2d 161sub restore_configuration {
43912111 162 my ($vmid, $rootdir, $conf, $restricted, $unique) = @_;
51665c2d
FG
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) {
adcaa73e 174 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
51665c2d
FG
175 next if $key =~ /^mp\d+$/; # don't recover mountpoints
176 next if $key =~ /^unused\d+$/; # don't recover unused disks
ba0e2930
FG
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 }
1fe49f62 187 if ($unique && $key =~ /^net\d+$/) {
43912111
CE
188 my $net = PVE::LXC::Config->parse_lxc_network($oldconf->{$key});
189 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
190 $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
191 $conf->{$key} = PVE::LXC::Config->print_lxc_network($net);
192 next;
193 }
51665c2d
FG
194 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
195 }
196 unlink($pct_cfg_fn);
5b4657d0 197
51665c2d
FG
198 if (-f $pct_fwcfg_fn) {
199 my $pve_firewall_dir = '/etc/pve/firewall';
200 mkdir $pve_firewall_dir; # make sure the directory exists
201 PVE::Tools::file_copy($pct_fwcfg_fn, "${pve_firewall_dir}/$vmid.fw");
202 unlink $pct_fwcfg_fn;
27916659 203 }
5b4657d0 204
51665c2d
FG
205 } elsif (-f $ovz_cfg_fn) {
206 print "###########################################################\n";
207 print "Converting OpenVZ configuration to LXC.\n";
208 print "Please check the configuration and reconfigure the network.\n";
209 print "###########################################################\n";
148d1cb4 210
51665c2d
FG
211 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
212 $conf->{ostype} = $lxc_setup->{conf}->{ostype};
213 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
214 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
215 foreach my $key (keys %$oldconf) {
216 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
217 }
218 unlink($ovz_cfg_fn);
148d1cb4 219
51665c2d
FG
220 } else {
221 print "###########################################################\n";
222 print "Backup archive does not contain any configuration\n";
223 print "###########################################################\n";
148d1cb4 224 }
5b4657d0
DM
225}
226
2271;