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