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