]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Create.pm
d/control: bump versioned dependency for guest-common
[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 Fcntl;
8
9 use PVE::RPCEnvironment;
10 use PVE::Storage::PBSPlugin;
11 use PVE::Storage;
12 use PVE::DataCenterConfig;
13 use PVE::LXC;
14 use PVE::LXC::Setup;
15 use PVE::VZDump::ConvertOVZ;
16 use PVE::Tools;
17 use POSIX;
18
19 sub restore_archive {
20 my ($storage_cfg, $archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
21
22 my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive, 1);
23 if (defined($storeid)) {
24 my $scfg = PVE::Storage::storage_check_enabled($storage_cfg, $storeid);
25 if ($scfg->{type} eq 'pbs') {
26 return restore_proxmox_backup_archive($storage_cfg, $archive, $rootdir, $conf, $no_unpack_error, $bwlimit);
27 }
28 }
29
30 $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $archive) if $archive ne '-';
31 restore_tar_archive($archive, $rootdir, $conf, $no_unpack_error, $bwlimit);
32 }
33
34 sub restore_proxmox_backup_archive {
35 my ($storage_cfg, $archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
36
37 my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive);
38 my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
39
40 my ($vtype, $name, undef, undef, undef, undef, $format) =
41 PVE::Storage::parse_volname($storage_cfg, $archive);
42
43 die "got unexpected vtype '$vtype'\n" if $vtype ne 'backup';
44
45 die "got unexpected backup format '$format'\n" if $format ne 'pbs-ct';
46
47 my ($id_map, $root_uid, $root_gid) = PVE::LXC::parse_id_maps($conf);
48 my $userns_cmd = PVE::LXC::userns_command($id_map);
49
50 my $cmd = "restore";
51 my $param = [$name, "root.pxar", $rootdir, '--allow-existing-dirs'];
52
53 if ($no_unpack_error) {
54 push(@$param, '--ignore-extract-device-errors');
55 }
56
57 PVE::Storage::PBSPlugin::run_raw_client_cmd(
58 $scfg, $storeid, $cmd, $param, userns_cmd => $userns_cmd);
59 }
60
61 sub restore_tar_archive {
62 my ($archive, $rootdir, $conf, $no_unpack_error, $bwlimit) = @_;
63
64 my ($id_map, $root_uid, $root_gid) = PVE::LXC::parse_id_maps($conf);
65 my $userns_cmd = PVE::LXC::userns_command($id_map);
66
67 my $archive_fh;
68 my $tar_input = '<&STDIN';
69 my @compression_opt;
70 if ($archive ne '-') {
71 # GNU tar refuses to autodetect this... *sigh*
72 my %compression_map = (
73 '.gz' => '-z',
74 '.bz2' => '-j',
75 '.xz' => '-J',
76 '.lzo' => '--lzop',
77 '.zst' => '--zstd',
78 );
79 if ($archive =~ /\.tar(\.[^.]+)?$/) {
80 if (defined($1)) {
81 die "unrecognized compression format: $1\n" if !defined($compression_map{$1});
82 @compression_opt = $compression_map{$1};
83 }
84 } else {
85 die "file does not look like a template archive: $archive\n";
86 }
87 sysopen($archive_fh, $archive, O_RDONLY)
88 or die "failed to open '$archive': $!\n";
89 my $flags = $archive_fh->fcntl(Fcntl::F_GETFD(), 0);
90 $archive_fh->fcntl(Fcntl::F_SETFD(), $flags & ~(Fcntl::FD_CLOEXEC()));
91 $tar_input = '<&'.fileno($archive_fh);
92 }
93
94 my $cmd = [@$userns_cmd, 'tar', 'xpf', '-', @compression_opt, '--totals',
95 @PVE::Storage::Plugin::COMMON_TAR_FLAGS,
96 '-C', $rootdir];
97
98 # skip-old-files doesn't have anything to do with time (old/new), but is
99 # simply -k (annoyingly also called --keep-old-files) without the 'treat
100 # existing files as errors' part... iow. it's bsdtar's interpretation of -k
101 # *sigh*, gnu...
102 push @$cmd, '--skip-old-files';
103 push @$cmd, '--anchored';
104 push @$cmd, '--exclude' , './dev/*';
105
106 if (defined($bwlimit)) {
107 $cmd = [ ['cstream', '-t', $bwlimit*1024], $cmd ];
108 }
109
110 if ($archive eq '-') {
111 print "extracting archive from STDIN\n";
112 } else {
113 print "extracting archive '$archive'\n";
114 }
115 eval { PVE::Tools::run_command($cmd, input => $tar_input); };
116 my $err = $@;
117 close($archive_fh) if defined $archive_fh;
118 die $err if $err && !$no_unpack_error;
119 }
120
121 sub recover_config {
122 my ($storage_cfg, $volid, $vmid) = @_;
123
124 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
125 if (defined($storeid)) {
126 my $scfg = PVE::Storage::storage_check_enabled($storage_cfg, $storeid);
127 if ($scfg->{type} eq 'pbs') {
128 return recover_config_from_proxmox_backup($storage_cfg, $volid, $vmid);
129 }
130 }
131
132 my $archive = PVE::Storage::abs_filesystem_path($storage_cfg, $volid);
133 recover_config_from_tar($archive, $vmid);
134 }
135
136 sub recover_config_from_proxmox_backup {
137 my ($storage_cfg, $volid, $vmid) = @_;
138
139 $vmid //= 0;
140
141 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
142 my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
143
144 my ($vtype, $name, undef, undef, undef, undef, $format) =
145 PVE::Storage::parse_volname($storage_cfg, $volid);
146
147 die "got unexpected vtype '$vtype'\n" if $vtype ne 'backup';
148
149 die "got unexpected backup format '$format'\n" if $format ne 'pbs-ct';
150
151 my $cmd = "restore";
152 my $param = [$name, "pct.conf", "-"];
153
154 my $raw = '';
155 my $outfunc = sub { my $line = shift; $raw .= "$line\n"; };
156 PVE::Storage::PBSPlugin::run_raw_client_cmd(
157 $scfg, $storeid, $cmd, $param, outfunc => $outfunc);
158
159 my $conf = PVE::LXC::Config::parse_pct_config("/lxc/${vmid}.conf" , $raw);
160
161 delete $conf->{snapshots};
162
163 my $mp_param = {};
164 PVE::LXC::Config->foreach_volume($conf, sub {
165 my ($ms, $mountpoint) = @_;
166 $mp_param->{$ms} = $conf->{$ms};
167 });
168
169 return wantarray ? ($conf, $mp_param) : $conf;
170 }
171
172 sub recover_config_from_tar {
173 my ($archive, $vmid) = @_;
174
175 my ($raw, $conf_file) = PVE::Storage::extract_vzdump_config_tar($archive, qr!(\./etc/vzdump/(pct|vps)\.conf)$!);
176 my $conf;
177 my $mp_param = {};
178 $vmid //= 0;
179
180 if ($conf_file =~ m/pct\.conf/) {
181
182 $conf = PVE::LXC::Config::parse_pct_config("/lxc/${vmid}.conf" , $raw);
183
184 delete $conf->{snapshots};
185
186 PVE::LXC::Config->foreach_volume($conf, sub {
187 my ($ms, $mountpoint) = @_;
188 $mp_param->{$ms} = $conf->{$ms};
189 });
190
191 } elsif ($conf_file =~ m/vps\.conf/) {
192
193 ($conf, $mp_param) = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
194
195 } else {
196
197 die "internal error";
198 }
199
200 return wantarray ? ($conf, $mp_param) : $conf;
201 }
202
203 sub restore_configuration {
204 my ($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
205
206 my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive, 1);
207 if (defined($storeid)) {
208 my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
209 if ($scfg->{type} eq 'pbs') {
210 return restore_configuration_from_proxmox_backup($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw);
211 }
212 }
213 restore_configuration_from_etc_vzdump($vmid, $rootdir, $conf, $restricted, $unique, $skip_fw);
214 }
215
216 sub restore_configuration_from_proxmox_backup {
217 my ($vmid, $storage_cfg, $archive, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
218
219 my ($storeid, $volname) = PVE::Storage::parse_volume_id($archive);
220 my $scfg = PVE::Storage::storage_config($storage_cfg, $storeid);
221
222 my ($vtype, $name, undef, undef, undef, undef, $format) =
223 PVE::Storage::parse_volname($storage_cfg, $archive);
224
225 my $oldconf = recover_config_from_proxmox_backup($storage_cfg, $archive, $vmid);
226
227 sanitize_and_merge_config($conf, $oldconf, $restricted, $unique);
228
229 my $cmd = "files";
230
231 my $list = PVE::Storage::PBSPlugin::run_client_cmd($scfg, $storeid, "files", [$name]);
232 my $has_fw_conf = grep { $_->{filename} eq 'fw.conf.blob' } @$list;
233
234 if ($has_fw_conf) {
235 my $pve_firewall_dir = '/etc/pve/firewall';
236 my $pct_fwcfg_target = "${pve_firewall_dir}/${vmid}.fw";
237 if ($skip_fw) {
238 warn "ignoring firewall config from backup archive's 'fw.conf', lacking API permission to modify firewall.\n";
239 warn "old firewall configuration in '$pct_fwcfg_target' left in place!\n"
240 if -e $pct_fwcfg_target;
241 } else {
242 mkdir $pve_firewall_dir; # make sure the directory exists
243 unlink $pct_fwcfg_target;
244
245 my $cmd = "restore";
246 my $param = [$name, "fw.conf", $pct_fwcfg_target];
247 PVE::Storage::PBSPlugin::run_raw_client_cmd($scfg, $storeid, $cmd, $param);
248 }
249 }
250 }
251
252 sub sanitize_and_merge_config {
253 my ($conf, $oldconf, $restricted, $unique) = @_;
254
255 my $rpcenv = PVE::RPCEnvironment::get();
256 my $authuser = $rpcenv->get_user();
257
258 foreach my $key (keys %$oldconf) {
259 next if $key eq 'digest' || $key eq 'rootfs' || $key eq 'snapshots' || $key eq 'unprivileged' || $key eq 'parent';
260 next if $key =~ /^mp\d+$/; # don't recover mountpoints
261 next if $key =~ /^unused\d+$/; # don't recover unused disks
262 # we know if it was a template in the restore API call and check if the target
263 # storage supports creating a template there
264 next if $key =~ /^template$/;
265
266 if ($restricted && $key eq 'features' && !$conf->{unprivileged} && $oldconf->{unprivileged}) {
267 warn "changing from unprivileged to privileged, skipping features\n";
268 next;
269 }
270
271 if ($key eq 'lxc' && $restricted) {
272 my $lxc_list = $oldconf->{'lxc'};
273
274 my $msg = "skipping custom lxc options, restore manually as root:\n";
275 $msg .= "--------------------------------\n";
276 foreach my $lxc_opt (@$lxc_list) {
277 $msg .= "$lxc_opt->[0]: $lxc_opt->[1]\n"
278 }
279 $msg .= "--------------------------------";
280
281 $rpcenv->warn($msg);
282
283 next;
284 }
285
286 if ($key =~ /^net\d+$/ && !defined($conf->{$key})) {
287 PVE::LXC::check_bridge_access($rpcenv, $authuser, $oldconf->{$key});
288 }
289
290 if ($unique && $key =~ /^net\d+$/) {
291 my $net = PVE::LXC::Config->parse_lxc_network($oldconf->{$key});
292 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
293 $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
294 $conf->{$key} = PVE::LXC::Config->print_lxc_network($net);
295 next;
296 }
297 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
298 }
299 }
300
301 sub restore_configuration_from_etc_vzdump {
302 my ($vmid, $rootdir, $conf, $restricted, $unique, $skip_fw) = @_;
303
304 # restore: try to extract configuration from archive
305
306 my $pct_cfg_fn = "$rootdir/etc/vzdump/pct.conf";
307 my $pct_fwcfg_fn = "$rootdir/etc/vzdump/pct.fw";
308 my $ovz_cfg_fn = "$rootdir/etc/vzdump/vps.conf";
309 if (-f $pct_cfg_fn) {
310 my $raw = PVE::Tools::file_get_contents($pct_cfg_fn);
311 my $oldconf = PVE::LXC::Config::parse_pct_config("/lxc/$vmid.conf", $raw);
312
313 sanitize_and_merge_config($conf, $oldconf, $restricted, $unique);
314
315 unlink($pct_cfg_fn);
316
317 # note: this file is possibly from the container itself in backups
318 # created prior to pve-container 2.0-40 (PVE 5.x) / 3.0-5 (PVE 6.x)
319 # only copy non-empty, non-symlink files, and only if the user is
320 # allowed to modify the firewall config anyways
321 if (-f $pct_fwcfg_fn && ! -l $pct_fwcfg_fn && -s $pct_fwcfg_fn) {
322 my $pve_firewall_dir = '/etc/pve/firewall';
323 my $pct_fwcfg_target = "${pve_firewall_dir}/${vmid}.fw";
324 if ($skip_fw) {
325 warn "ignoring firewall config from backup archive's '$pct_fwcfg_fn', lacking API permission to modify firewall.\n";
326 warn "old firewall configuration in '$pct_fwcfg_target' left in place!\n"
327 if -e $pct_fwcfg_target;
328 } else {
329 mkdir $pve_firewall_dir; # make sure the directory exists
330 PVE::Tools::file_copy($pct_fwcfg_fn, $pct_fwcfg_target);
331 }
332 unlink $pct_fwcfg_fn;
333 }
334
335 } elsif (-f $ovz_cfg_fn) {
336 print "###########################################################\n";
337 print "Converting OpenVZ configuration to LXC.\n";
338 print "Please check the configuration and reconfigure the network.\n";
339 print "###########################################################\n";
340
341 my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
342 $conf->{ostype} = $lxc_setup->{conf}->{ostype};
343 my $raw = PVE::Tools::file_get_contents($ovz_cfg_fn);
344 my $oldconf = PVE::VZDump::ConvertOVZ::convert_ovz($raw);
345 foreach my $key (keys %$oldconf) {
346 $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
347 }
348 unlink($ovz_cfg_fn);
349
350 } else {
351 print "###########################################################\n";
352 print "Backup archive does not contain any configuration\n";
353 print "###########################################################\n";
354 }
355 }
356
357 1;