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