]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Create.pm
cleanup directory structure
[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 $userns_cmd = [];
29
30 # we always use the same mapping: 'b:0:100000:65536'
31 # if ($conf->{'lxc.id_map'}) {
32 # $userns_cmd = ['lxc-usernsexec', '-m', 'b:0:100000:65536', '--'];
33 # PVE::Tools::run_command(['chown', '-R', '100000:100000', $rootdir]);
34 # }
35
36 my $cmd = [@$userns_cmd, 'tar', 'xpf', $archive, '--numeric-owner', '--totals',
37 '--sparse', '-C', $rootdir];
38
39 push @$cmd, '--anchored';
40 push @$cmd, '--exclude' , './dev/*';
41
42 if ($archive eq '-') {
43 print "extracting archive from STDIN\n";
44 PVE::Tools::run_command($cmd, input => "<&STDIN");
45 } else {
46 print "extracting archive '$archive'\n";
47 PVE::Tools::run_command($cmd);
48 }
49
50 # determine file type of /usr/bin/file itself to get guests' architecture
51 $cmd = [@$userns_cmd, '/usr/bin/file', '-b', '-L', "$rootdir/usr/bin/file"];
52 PVE::Tools::run_command($cmd, outfunc => sub {
53 shift =~ /^ELF (\d{2}-bit)/; # safely assumes x86 linux
54 my $arch_str = $1;
55 $conf->{'arch'} = 'amd64'; # defaults to 64bit
56 if(defined($arch_str)) {
57 $conf->{'arch'} = 'i386' if $arch_str =~ /32/;
58 print "Detected container architecture: $conf->{'arch'}\n";
59 } else {
60 print "CT architecture detection failed, falling back to amd64.\n" .
61 "Edit the config in /etc/pve/nodes/{node}/lxc/{vmid}/config " .
62 "to set another architecture.\n";
63 }
64 });
65 }
66
67 sub tar_archive_search_conf {
68 my ($archive) = @_;
69
70 die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
71
72 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
73 die "unable to open file '$archive'\n";
74
75 my $file;
76 while (defined($file = <$fh>)) {
77 if ($file =~ m!^(\./etc/vzdump/(pct|vps)\.conf)$!) {
78 $file = $1; # untaint
79 last;
80 }
81 }
82
83 kill 15, $pid;
84 waitpid $pid, 0;
85 close $fh;
86
87 die "ERROR: archive contains no configuration file\n" if !$file;
88 chomp $file;
89
90 return $file;
91 }
92
93 sub recover_config {
94 my ($archive) = @_;
95
96 my $conf_file = tar_archive_search_conf($archive);
97
98 my $raw = '';
99 my $out = sub {
100 my $output = shift;
101 $raw .= "$output\n";
102 };
103
104 PVE::Tools::run_command(['tar', '-xpOf', $archive, $conf_file, '--occurrence'], outfunc => $out);
105
106 my $conf;
107 my $disksize;
108
109 if ($conf_file =~ m/pct\.conf/) {
110
111 $conf = PVE::LXC::parse_pct_config("/lxc/0.conf" , $raw);
112
113 delete $conf->{snapshots};
114 delete $conf->{template}; # restored CT is never a template
115
116 if (defined($conf->{rootfs})) {
117 my $rootinfo = PVE::LXC::parse_ct_mountpoint($conf->{rootfs});
118 $disksize = $rootinfo->{size} if defined($rootinfo->{size});
119 }
120
121 } elsif ($conf_file =~ m/vps\.conf/) {
122
123 ($conf, $disksize) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
124
125 } else {
126
127 die "internal error";
128 }
129
130 return wantarray ? ($conf, $disksize) : $conf;
131 }
132
133 sub restore_and_configure {
134 my ($vmid, $archive, $rootdir, $conf, $password, $restore) = @_;
135
136 restore_archive($archive, $rootdir, $conf);
137
138 if (!$restore) {
139 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
140
141 PVE::LXC::write_config($vmid, $conf); # safe config (after OS detection)
142 $lxc_setup->post_create_hook($password);
143 } else {
144 # restore: try to extract configuration from archive
145
146 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
147 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
148 if (-f $pct_cfg_fn) {
149 my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
150 my $oldconf = PVE::LXC::parse_pct_config("/lxc/$vmid.conf", $raw);
151
152 foreach my $key (keys %$oldconf) {
153 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots';
154 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
155 }
156
157 } elsif (-f $ovz_cfg_fn) {
158 print "###########################################################\n";
159 print "Converting OpenVZ configuration to LXC.\n";
160 print "Please check the configuration and reconfigure the network.\n";
161 print "###########################################################\n";
162
163 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
164 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
165 foreach my $key (keys %$oldconf) {
166 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
167 }
168
169 } else {
170 print "###########################################################\n";
171 print "Backup archive does not contain any configuration\n";
172 print "###########################################################\n";
173 }
174 }
175 }
176
177 # use new subvolume API
178 sub create_rootfs_subvol {
179 my ($storage_conf, $storage, $volid, $vmid, $conf, $archive, $password, $restore) = @_;
180
181 my $private = PVE::Storage::path($storage_conf, $volid);
182 (-d $private) || die "unable to get container private dir '$private' - $!\n";
183
184 restore_and_configure($vmid, $archive, $private, $conf, $password, $restore);
185 }
186
187 # direct mount
188 sub create_rootfs_dev {
189 my ($storage_conf, $storage, $volid, $vmid, $conf, $archive, $password, $restore) = @_;
190
191 my $image_path = PVE::Storage::path($storage_conf, $volid);
192
193 my $cmd = ['mkfs.ext4', $image_path];
194 PVE::Tools::run_command($cmd);
195
196 my $mountpoint;
197
198 eval {
199 my $tmp = "/var/lib/lxc/$vmid/rootfs";
200 File::Path::mkpath($tmp);
201 PVE::Tools::run_command(['mount', '-t', 'ext4', $image_path, $tmp]);
202 $mountpoint = $tmp;
203
204 restore_and_configure($vmid, $archive, $mountpoint, $conf, $password, $restore);
205 };
206 if (my $err = $@) {
207 if ($mountpoint) {
208 eval { PVE::Tools::run_command(['umount', $mountpoint]) };
209 warn $@ if $@;
210 }
211 die $err;
212 }
213
214 PVE::Tools::run_command(['umount', '-l', $mountpoint]);
215 }
216
217 # create a raw file, then loop mount
218 sub create_rootfs_dir_loop {
219 my ($storage_conf, $storage, $volid, $vmid, $conf, $archive, $password, $restore) = @_;
220
221 my $image_path = PVE::Storage::path($storage_conf, $volid);
222
223 my $cmd = ['mkfs.ext4', $image_path];
224 PVE::Tools::run_command($cmd);
225
226 my $mountpoint;
227
228 my $loopdev;
229 eval {
230 my $parser = sub {
231 my $line = shift;
232 $loopdev = $line if $line =~m|^/dev/loop\d+$|;
233 };
234 PVE::Tools::run_command(['losetup', '--find', '--show', $image_path], outfunc => $parser);
235
236 my $tmp = "/var/lib/lxc/$vmid/rootfs";
237 File::Path::mkpath($tmp);
238 PVE::Tools::run_command(['mount', '-t', 'ext4', $loopdev, $tmp]);
239 $mountpoint = $tmp;
240
241 restore_and_configure($vmid, $archive, $mountpoint, $conf, $password, $restore);
242 };
243 if (my $err = $@) {
244 if ($mountpoint) {
245 eval { PVE::Tools::run_command(['umount', '-d', $mountpoint]) };
246 warn $@ if $@;
247 } else {
248 eval { PVE::Tools::run_command(['losetup', '-d', $loopdev]) if $loopdev; };
249 warn $@ if $@;
250 }
251 die $err;
252 }
253
254 PVE::Tools::run_command(['umount', '-l', '-d', $mountpoint]);
255 }
256
257 sub create_rootfs {
258 my ($storage_cfg, $storage, $volid, $vmid, $conf, $archive, $password, $restore) = @_;
259
260 my $config_fn = PVE::LXC::config_file($vmid);
261 if (-f $config_fn) {
262 die "container exists" if !$restore; # just to be sure
263
264 my $old_conf = PVE::LXC::load_config($vmid);
265
266 # destroy old container volume
267 PVE::LXC::destory_lxc_container($storage_cfg, $vmid, $old_conf);
268
269 # do not copy all settings to restored container
270 foreach my $opt (qw(rootfs digest snapshots)) {
271 delete $old_conf->{$opt};
272 }
273 PVE::LXC::update_pct_config($vmid, $conf, 0, $old_conf);
274
275 PVE::LXC::create_config($vmid, $conf);
276
277 } else {
278
279 PVE::LXC::create_config($vmid, $conf);
280 }
281
282 my ($vtype, undef, undef, undef, undef, $isBase, $format) =
283 PVE::Storage::parse_volname($storage_cfg, $volid);
284
285 die "got strange vtype '$vtype'\n" if $vtype ne 'images';
286
287 die "unable to install into base volume" if $isBase;
288
289 if ($format eq 'subvol') {
290 create_rootfs_subvol($storage_cfg, $storage, $volid, $vmid, $conf, $archive, $password, $restore);
291 } elsif ($format eq 'raw') {
292 my $scfg = PVE::Storage::storage_config($storage_cfg, $storage);
293 PVE::Storage::activate_storage($storage_cfg, $storage);
294 PVE::Storage::activate_volumes($storage_cfg, [$volid]);
295 if ($scfg->{path}) {
296 create_rootfs_dir_loop($storage_cfg, $storage, $volid, $vmid, $conf, $archive, $password, $restore);
297 } elsif ($scfg->{type} eq 'drbd' || $scfg->{type} eq 'rbd') {
298 create_rootfs_dev($storage_cfg, $storage, $volid, $vmid, $conf, $archive, $password, $restore);
299 } else {
300 die "unable to create containers on storage type '$scfg->{type}'\n";
301 }
302 PVE::Storage::deactivate_volumes($storage_cfg, [$volid]);
303 } else {
304 die "unsupported image format '$format'\n";
305 }
306 }
307
308 1;