]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Create.pm
error when failing to extract rather than warn
[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
9 use PVE::Storage;
10 use PVE::LXC;
11 use PVE::LXC::Setup;
12 use PVE::VZDump::ConvertOVZ;
13
14 sub next_free_nbd_dev {
15
16 for(my $i = 0;;$i++) {
17 my $dev = "/dev/nbd$i";
18 last if ! -b $dev;
19 next if -f "/sys/block/nbd$i/pid"; # busy
20 return $dev;
21 }
22 die "unable to find free nbd device\n";
23 }
24
25 sub restore_archive {
26 my ($archive, $rootdir, $conf) = @_;
27
28 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
29 my $userns_cmd = PVE::LXC::userns_command($id_map);
30
31 my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--totals',
32 @$PVE::LXC::COMMON_TAR_FLAGS,
33 '-C', $rootdir];
34
35 # skip-old-files doesn't have anything to do with time (old/new), but is
36 # simply -k (annoyingly also called --keep-old-files) without the 'treat
37 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
38 # *sigh*, gnu...
39 push @$cmd, '--skip-old-files';
40 push @$cmd, '--anchored';
41 push @$cmd, '--exclude' , './dev/*';
42
43 if ($archive eq '-') {
44 print "extracting archive from STDIN\n";
45 PVE::Tools::run_command($cmd, input => "<&STDIN");
46 } else {
47 print "extracting archive '$archive'\n";
48 PVE::Tools::run_command($cmd);
49 }
50
51 # determine file type of /usr/bin/file itself to get guests' architecture
52 $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/usr/bin/file"];
53 PVE::Tools::run_command($cmd, outfunc => sub {
54 shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
55 my $arch_str = $1;
56 $conf->{'arch'} = 'amd64'; # defaults to 64bit
57 if(defined($arch_str)) {
58 $conf->{'arch'} = 'i386' if $arch_str =~ /32/;
59 print "Detected container architecture: $conf->{'arch'}\n";
60 } else {
61 print "CT architecture detection failed, falling back to amd64.\n" .
62 "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}/config " .
63 "to set another architecture.\n";
64 }
65 });
66 }
67
68 sub tar_archive_search_conf {
69 my ($archive) = @_;
70
71 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
72
73 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
74 die "unable to open file '$archive'\n";
75
76 my $file;
77 while (defined($file = <$fh>)) {
78 if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
79 $file = $1; # untaint
80 last;
81 }
82 }
83
84 kill 15, $pid;
85 waitpid $pid, 0;
86 close $fh;
87
88 die "ERROR: archive contains no configuration file\n" if !$file;
89 chomp $file;
90
91 return $file;
92 }
93
94 sub recover_config {
95 my ($archive) = @_;
96
97 my $conf_file = tar_archive_search_conf($archive);
98
99 my $raw = '';
100 my $out = sub {
101 my $output = shift;
102 $raw .= "$output\n";
103 };
104
105 PVE::Tools::run_command(['tar', '-xpOf', $archive, $conf_file, '--occurrence'], outfunc => $out);
106
107 my $conf;
108 my $disksize;
109
110 if ($conf_file =~ m/pct\.conf/) {
111
112 $conf = PVE::LXC::parse_pct_config("/lxc/0.conf" , $raw);
113
114 delete $conf->{snapshots};
115 delete $conf->{template}; # restored CT is never a template
116
117 if (defined($conf->{rootfs})) {
118 my $rootinfo = PVE::LXC::parse_ct_mountpoint($conf->{rootfs});
119 $disksize = $rootinfo->{size} if defined($rootinfo->{size});
120 }
121
122 } elsif ($conf_file =~ m/vps\.conf/) {
123
124 ($conf, $disksize) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
125
126 } else {
127
128 die "internal error";
129 }
130
131 return wantarray ? ($conf, $disksize) : $conf;
132 }
133
134 sub restore_and_configure {
135 my ($vmid, $archive, $rootdir, $conf, $password, $restore) = @_;
136
137 restore_archive($archive, $rootdir, $conf);
138
139 if (!$restore) {
140 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
141
142 PVE::LXC::write_config($vmid, $conf); # safe config (after OS detection)
143 $lxc_setup->post_create_hook($password);
144 } else {
145 # restore: try to extract configuration from archive
146
147 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
148 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
149 if (-f $pct_cfg_fn) {
150 my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
151 my $oldconf = PVE::LXC::parse_pct_config("/lxc/$vmid.conf", $raw);
152
153 foreach my $key (keys %$oldconf) {
154 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged';
155 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
156 }
157 unlink($pct_cfg_fn);
158
159 } elsif (-f $ovz_cfg_fn) {
160 print "###########################################################\n";
161 print "Converting OpenVZ configuration to LXC.\n";
162 print "Please check the configuration and reconfigure the network.\n";
163 print "###########################################################\n";
164
165 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
166 $conf->{ostype} = $lxc_setup->{conf}->{ostype};
167 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
168 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
169 foreach my $key (keys %$oldconf) {
170 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
171 }
172 unlink($ovz_cfg_fn);
173
174 } else {
175 print "###########################################################\n";
176 print "Backup archive does not contain any configuration\n";
177 print "###########################################################\n";
178 }
179 }
180 }
181
182 sub create_rootfs {
183 my ($storage_cfg, $vmid, $conf, $archive, $password, $restore) = @_;
184
185 my $config_fn = PVE::LXC::config_file($vmid);
186 if (-f $config_fn) {
187 die "container exists" if !$restore; # just to be sure
188
189 my $old_conf = PVE::LXC::load_config($vmid);
190
191 # destroy old container volume
192 PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf);
193
194 # do not copy all settings to restored container
195 foreach my $opt (qw(rootfs digest snapshots arch ostype unprivileged)) {
196 delete $old_conf->{$opt};
197 }
198 foreach my $opt (keys %$old_conf) {
199 delete $old_conf->{$opt} if $opt =~ m/^mp\d+$/;
200 }
201
202 PVE::LXC::update_pct_config($vmid, $conf, 0, $old_conf);
203
204 PVE::LXC::create_config($vmid, $conf);
205
206 } else {
207
208 PVE::LXC::create_config($vmid, $conf);
209 }
210
211 eval {
212 my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf);
213 restore_and_configure($vmid, $archive, $rootdir, $conf, $password, $restore);
214 };
215 my $err = $@;
216 PVE::LXC::umount_all($vmid, $storage_cfg, $conf, $err ? 1 : 0);
217 PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::get_vm_volumes($conf));
218 die $err if $err;
219 }
220
221 1;