]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Config.pm
config: limit description/comment length to 8 KiB
[pve-container.git] / src / PVE / LXC / Config.pm
1 package PVE::LXC::Config;
2
3 use strict;
4 use warnings;
5 use Fcntl qw(O_RDONLY);
6
7 use PVE::AbstractConfig;
8 use PVE::Cluster qw(cfs_register_file);
9 use PVE::DataCenterConfig;
10 use PVE::GuestHelpers;
11 use PVE::INotify;
12 use PVE::JSONSchema qw(get_standard_option);
13 use PVE::Tools;
14
15 use base qw(PVE::AbstractConfig);
16
17 use constant {FIFREEZE => 0xc0045877,
18 FITHAW => 0xc0045878};
19
20 my $nodename = PVE::INotify::nodename();
21 my $lock_handles = {};
22 my $lockdir = "/run/lock/lxc";
23 mkdir $lockdir;
24 mkdir "/etc/pve/nodes/$nodename/lxc";
25 my $MAX_MOUNT_POINTS = 256;
26 my $MAX_UNUSED_DISKS = $MAX_MOUNT_POINTS;
27
28 # BEGIN implemented abstract methods from PVE::AbstractConfig
29
30 sub guest_type {
31 return "CT";
32 }
33
34 sub __config_max_unused_disks {
35 my ($class) = @_;
36
37 return $MAX_UNUSED_DISKS;
38 }
39
40 sub config_file_lock {
41 my ($class, $vmid) = @_;
42
43 return "$lockdir/pve-config-${vmid}.lock";
44 }
45
46 sub cfs_config_path {
47 my ($class, $vmid, $node) = @_;
48
49 $node = $nodename if !$node;
50 return "nodes/$node/lxc/$vmid.conf";
51 }
52
53 sub mountpoint_backup_enabled {
54 my ($class, $mp_key, $mountpoint) = @_;
55
56 my $enabled;
57 my $reason;
58
59 if ($mp_key eq 'rootfs') {
60 $enabled = 1;
61 $reason = 'rootfs';
62 } elsif ($mountpoint->{type} ne 'volume') {
63 $enabled = 0;
64 $reason = 'not a volume';
65 } elsif ($mountpoint->{backup}) {
66 $enabled = 1;
67 $reason = 'enabled';
68 } else {
69 $enabled = 0;
70 $reason = 'disabled';
71 }
72 return wantarray ? ($enabled, $reason) : $enabled;
73 }
74
75 sub has_feature {
76 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
77 my $err;
78
79 my $opts;
80 if ($feature eq 'copy' || $feature eq 'clone') {
81 $opts = {'valid_target_formats' => ['raw', 'subvol']};
82 }
83
84 $class->foreach_volume($conf, sub {
85 my ($ms, $mountpoint) = @_;
86
87 return if $err; # skip further test
88 return if $backup_only && !$class->mountpoint_backup_enabled($ms, $mountpoint);
89
90 $err = 1
91 if !PVE::Storage::volume_has_feature($storecfg, $feature,
92 $mountpoint->{volume},
93 $snapname, $running, $opts);
94 });
95
96 return $err ? 0 : 1;
97 }
98
99 sub __snapshot_save_vmstate {
100 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
101 die "implement me - snapshot_save_vmstate\n";
102 }
103
104 sub __snapshot_check_running {
105 my ($class, $vmid) = @_;
106 return PVE::LXC::check_running($vmid);
107 }
108
109 sub __snapshot_check_freeze_needed {
110 my ($class, $vmid, $config, $save_vmstate) = @_;
111
112 my $ret = $class->__snapshot_check_running($vmid);
113 return ($ret, $ret);
114 }
115
116 # implements similar functionality to fsfreeze(8)
117 sub fsfreeze_mountpoint {
118 my ($path, $thaw) = @_;
119
120 my $op = $thaw ? 'thaw' : 'freeze';
121 my $ioctl = $thaw ? FITHAW : FIFREEZE;
122
123 sysopen my $fd, $path, O_RDONLY or die "failed to open $path: $!\n";
124 my $ioctl_err;
125 if (!ioctl($fd, $ioctl, 0)) {
126 $ioctl_err = "$!";
127 }
128 close($fd);
129 die "fs$op '$path' failed - $ioctl_err\n" if defined $ioctl_err;
130 }
131
132 sub __snapshot_freeze {
133 my ($class, $vmid, $unfreeze) = @_;
134
135 my $conf = $class->load_config($vmid);
136 my $storagecfg = PVE::Storage::config();
137
138 my $freeze_mps = [];
139 $class->foreach_volume($conf, sub {
140 my ($ms, $mountpoint) = @_;
141
142 return if $mountpoint->{type} ne 'volume';
143
144 if (PVE::Storage::volume_snapshot_needs_fsfreeze($storagecfg, $mountpoint->{volume})) {
145 push @$freeze_mps, $mountpoint->{mp};
146 }
147 });
148
149 my $freeze_mountpoints = sub {
150 my ($thaw) = @_;
151
152 return if scalar(@$freeze_mps) == 0;
153
154 my $pid = PVE::LXC::find_lxc_pid($vmid);
155
156 for my $mp (@$freeze_mps) {
157 eval{ fsfreeze_mountpoint("/proc/${pid}/root/${mp}", $thaw); };
158 warn $@ if $@;
159 }
160 };
161
162 if ($unfreeze) {
163 eval { PVE::LXC::thaw($vmid); };
164 warn $@ if $@;
165 $freeze_mountpoints->(1);
166 } else {
167 PVE::LXC::freeze($vmid);
168 PVE::LXC::sync_container_namespace($vmid);
169 $freeze_mountpoints->(0);
170 }
171 }
172
173 sub __snapshot_create_vol_snapshot {
174 my ($class, $vmid, $ms, $mountpoint, $snapname) = @_;
175
176 my $storecfg = PVE::Storage::config();
177
178 return if $snapname eq 'vzdump' &&
179 !$class->mountpoint_backup_enabled($ms, $mountpoint);
180
181 PVE::Storage::volume_snapshot($storecfg, $mountpoint->{volume}, $snapname);
182 }
183
184 sub __snapshot_delete_remove_drive {
185 my ($class, $snap, $remove_drive) = @_;
186
187 if ($remove_drive eq 'vmstate') {
188 die "implement me - saving vmstate\n";
189 } else {
190 my $value = $snap->{$remove_drive};
191 my $mountpoint = $class->parse_volume($remove_drive, $value, 1);
192 delete $snap->{$remove_drive};
193
194 $class->add_unused_volume($snap, $mountpoint->{volume})
195 if ($mountpoint->{type} eq 'volume');
196 }
197 }
198
199 sub __snapshot_delete_vmstate_file {
200 my ($class, $snap, $force) = @_;
201
202 die "implement me - saving vmstate\n";
203 }
204
205 sub __snapshot_delete_vol_snapshot {
206 my ($class, $vmid, $ms, $mountpoint, $snapname, $unused) = @_;
207
208 return if $snapname eq 'vzdump' &&
209 !$class->mountpoint_backup_enabled($ms, $mountpoint);
210
211 my $storecfg = PVE::Storage::config();
212 PVE::Storage::volume_snapshot_delete($storecfg, $mountpoint->{volume}, $snapname);
213 push @$unused, $mountpoint->{volume};
214 }
215
216 sub __snapshot_rollback_vol_possible {
217 my ($class, $mountpoint, $snapname) = @_;
218
219 my $storecfg = PVE::Storage::config();
220 PVE::Storage::volume_rollback_is_possible($storecfg, $mountpoint->{volume}, $snapname);
221 }
222
223 sub __snapshot_rollback_vol_rollback {
224 my ($class, $mountpoint, $snapname) = @_;
225
226 my $storecfg = PVE::Storage::config();
227 PVE::Storage::volume_snapshot_rollback($storecfg, $mountpoint->{volume}, $snapname);
228 }
229
230 sub __snapshot_rollback_vm_stop {
231 my ($class, $vmid) = @_;
232
233 PVE::LXC::vm_stop($vmid, 1)
234 if $class->__snapshot_check_running($vmid);
235 }
236
237 sub __snapshot_rollback_vm_start {
238 my ($class, $vmid, $vmstate, $data);
239
240 die "implement me - save vmstate\n";
241 }
242
243 sub __snapshot_rollback_get_unused {
244 my ($class, $conf, $snap) = @_;
245
246 my $unused = [];
247
248 $class->foreach_volume($conf, sub {
249 my ($vs, $volume) = @_;
250
251 return if $volume->{type} ne 'volume';
252
253 my $found = 0;
254 my $volid = $volume->{volume};
255
256 $class->foreach_volume($snap, sub {
257 my ($ms, $mountpoint) = @_;
258
259 return if $found;
260 return if ($mountpoint->{type} ne 'volume');
261
262 $found = 1
263 if ($mountpoint->{volume} && $mountpoint->{volume} eq $volid);
264 });
265
266 push @$unused, $volid if !$found;
267 });
268
269 return $unused;
270 }
271
272 # END implemented abstract methods from PVE::AbstractConfig
273
274 # BEGIN JSON config code
275
276 cfs_register_file('/lxc/', \&parse_pct_config, \&write_pct_config);
277
278
279 my $valid_mount_option_re = qr/(noatime|nodev|nosuid|noexec)/;
280
281 sub is_valid_mount_option {
282 my ($option) = @_;
283 return $option =~ $valid_mount_option_re;
284 }
285
286 my $rootfs_desc = {
287 volume => {
288 type => 'string',
289 default_key => 1,
290 format => 'pve-lxc-mp-string',
291 format_description => 'volume',
292 description => 'Volume, device or directory to mount into the container.',
293 },
294 size => {
295 type => 'string',
296 format => 'disk-size',
297 format_description => 'DiskSize',
298 description => 'Volume size (read only value).',
299 optional => 1,
300 },
301 acl => {
302 type => 'boolean',
303 description => 'Explicitly enable or disable ACL support.',
304 optional => 1,
305 },
306 mountoptions => {
307 optional => 1,
308 type => 'string',
309 description => 'Extra mount options for rootfs/mps.',
310 format_description => 'opt[;opt...]',
311 pattern => qr/$valid_mount_option_re(;$valid_mount_option_re)*/,
312 },
313 ro => {
314 type => 'boolean',
315 description => 'Read-only mount point',
316 optional => 1,
317 },
318 quota => {
319 type => 'boolean',
320 description => 'Enable user quotas inside the container (not supported with zfs subvolumes)',
321 optional => 1,
322 },
323 replicate => {
324 type => 'boolean',
325 description => 'Will include this volume to a storage replica job.',
326 optional => 1,
327 default => 1,
328 },
329 shared => {
330 type => 'boolean',
331 description => 'Mark this non-volume mount point as available on multiple nodes (see \'nodes\')',
332 verbose_description => "Mark this non-volume mount point as available on all nodes.\n\nWARNING: This option does not share the mount point automatically, it assumes it is shared already!",
333 optional => 1,
334 default => 0,
335 },
336 };
337
338 PVE::JSONSchema::register_standard_option('pve-ct-rootfs', {
339 type => 'string', format => $rootfs_desc,
340 description => "Use volume as container root.",
341 optional => 1,
342 });
343
344 # IP address with optional interface suffix for link local ipv6 addresses
345 PVE::JSONSchema::register_format('lxc-ip-with-ll-iface', \&verify_ip_with_ll_iface);
346 sub verify_ip_with_ll_iface {
347 my ($addr, $noerr) = @_;
348
349 if (my ($addr, $iface) = ($addr =~ /^(fe80:[^%]+)%(.*)$/)) {
350 if (PVE::JSONSchema::pve_verify_ip($addr, 1)
351 && PVE::JSONSchema::pve_verify_iface($iface, 1))
352 {
353 return $addr;
354 }
355 }
356
357 return PVE::JSONSchema::pve_verify_ip($addr, $noerr);
358 }
359
360
361 my $features_desc = {
362 mount => {
363 optional => 1,
364 type => 'string',
365 description => "Allow mounting file systems of specific types."
366 ." This should be a list of file system types as used with the mount command."
367 ." Note that this can have negative effects on the container's security."
368 ." With access to a loop device, mounting a file can circumvent the mknod"
369 ." permission of the devices cgroup, mounting an NFS file system can"
370 ." block the host's I/O completely and prevent it from rebooting, etc.",
371 format_description => 'fstype;fstype;...',
372 pattern => qr/[a-zA-Z0-9_; ]+/,
373 },
374 nesting => {
375 optional => 1,
376 type => 'boolean',
377 default => 0,
378 description => "Allow nesting."
379 ." Best used with unprivileged containers with additional id mapping."
380 ." Note that this will expose procfs and sysfs contents of the host"
381 ." to the guest.",
382 },
383 keyctl => {
384 optional => 1,
385 type => 'boolean',
386 default => 0,
387 description => "For unprivileged containers only: Allow the use of the keyctl() system call."
388 ." This is required to use docker inside a container."
389 ." By default unprivileged containers will see this system call as non-existent."
390 ." This is mostly a workaround for systemd-networkd, as it will treat it as a fatal"
391 ." error when some keyctl() operations are denied by the kernel due to lacking permissions."
392 ." Essentially, you can choose between running systemd-networkd or docker.",
393 },
394 fuse => {
395 optional => 1,
396 type => 'boolean',
397 default => 0,
398 description => "Allow using 'fuse' file systems in a container."
399 ." Note that interactions between fuse and the freezer cgroup can potentially cause I/O deadlocks.",
400 },
401 mknod => {
402 optional => 1,
403 type => 'boolean',
404 default => 0,
405 description => "Allow unprivileged containers to use mknod() to add certain device nodes."
406 ." This requires a kernel with seccomp trap to user space support (5.3 or newer)."
407 ." This is experimental.",
408 },
409 force_rw_sys => {
410 optional => 1,
411 type => 'boolean',
412 default => 0,
413 description => "Mount /sys in unprivileged containers as `rw` instead of `mixed`."
414 ." This can break networking under newer (>= v245) systemd-network use."
415 },
416 };
417
418 my $confdesc = {
419 lock => {
420 optional => 1,
421 type => 'string',
422 description => "Lock/unlock the VM.",
423 enum => [qw(backup create destroyed disk fstrim migrate mounted rollback snapshot snapshot-delete)],
424 },
425 onboot => {
426 optional => 1,
427 type => 'boolean',
428 description => "Specifies whether a VM will be started during system bootup.",
429 default => 0,
430 },
431 startup => get_standard_option('pve-startup-order'),
432 template => {
433 optional => 1,
434 type => 'boolean',
435 description => "Enable/disable Template.",
436 default => 0,
437 },
438 arch => {
439 optional => 1,
440 type => 'string',
441 enum => ['amd64', 'i386', 'arm64', 'armhf'],
442 description => "OS architecture type.",
443 default => 'amd64',
444 },
445 ostype => {
446 optional => 1,
447 type => 'string',
448 enum => [qw(debian devuan ubuntu centos fedora opensuse archlinux alpine gentoo unmanaged)],
449 description => "OS type. This is used to setup configuration inside the container, and corresponds to lxc setup scripts in /usr/share/lxc/config/<ostype>.common.conf. Value 'unmanaged' can be used to skip and OS specific setup.",
450 },
451 console => {
452 optional => 1,
453 type => 'boolean',
454 description => "Attach a console device (/dev/console) to the container.",
455 default => 1,
456 },
457 tty => {
458 optional => 1,
459 type => 'integer',
460 description => "Specify the number of tty available to the container",
461 minimum => 0,
462 maximum => 6,
463 default => 2,
464 },
465 cores => {
466 optional => 1,
467 type => 'integer',
468 description => "The number of cores assigned to the container. A container can use all available cores by default.",
469 minimum => 1,
470 maximum => 8192,
471 },
472 cpulimit => {
473 optional => 1,
474 type => 'number',
475 description => "Limit of CPU usage.\n\nNOTE: If the computer has 2 CPUs, it has a total of '2' CPU time. Value '0' indicates no CPU limit.",
476 minimum => 0,
477 maximum => 8192,
478 default => 0,
479 },
480 cpuunits => {
481 optional => 1,
482 type => 'integer',
483 description => "CPU weight for a VM. Argument is used in the kernel fair scheduler. The larger the number is, the more CPU time this VM gets. Number is relative to the weights of all the other running VMs.\n\nNOTE: You can disable fair-scheduler configuration by setting this to 0.",
484 minimum => 0,
485 maximum => 500000,
486 default => 1024,
487 },
488 memory => {
489 optional => 1,
490 type => 'integer',
491 description => "Amount of RAM for the VM in MB.",
492 minimum => 16,
493 default => 512,
494 },
495 swap => {
496 optional => 1,
497 type => 'integer',
498 description => "Amount of SWAP for the VM in MB.",
499 minimum => 0,
500 default => 512,
501 },
502 hostname => {
503 optional => 1,
504 description => "Set a host name for the container.",
505 type => 'string', format => 'dns-name',
506 maxLength => 255,
507 },
508 description => {
509 optional => 1,
510 type => 'string',
511 description => "Description for the Container. Shown in the web-interface CT's summary."
512 ." This is saved as comment inside the configuration file.",
513 maxLength => 1024 * 8,
514 },
515 searchdomain => {
516 optional => 1,
517 type => 'string', format => 'dns-name-list',
518 description => "Sets DNS search domains for a container. Create will automatically use the setting from the host if you neither set searchdomain nor nameserver.",
519 },
520 nameserver => {
521 optional => 1,
522 type => 'string', format => 'lxc-ip-with-ll-iface-list',
523 description => "Sets DNS server IP address for a container. Create will automatically use the setting from the host if you neither set searchdomain nor nameserver.",
524 },
525 timezone => {
526 optional => 1,
527 type => 'string', format => 'pve-ct-timezone',
528 description => "Time zone to use in the container. If option isn't set, then nothing will be done. Can be set to 'host' to match the host time zone, or an arbitrary time zone option from /usr/share/zoneinfo/zone.tab",
529 },
530 rootfs => get_standard_option('pve-ct-rootfs'),
531 parent => {
532 optional => 1,
533 type => 'string', format => 'pve-configid',
534 maxLength => 40,
535 description => "Parent snapshot name. This is used internally, and should not be modified.",
536 },
537 snaptime => {
538 optional => 1,
539 description => "Timestamp for snapshots.",
540 type => 'integer',
541 minimum => 0,
542 },
543 cmode => {
544 optional => 1,
545 description => "Console mode. By default, the console command tries to open a connection to one of the available tty devices. By setting cmode to 'console' it tries to attach to /dev/console instead. If you set cmode to 'shell', it simply invokes a shell inside the container (no login).",
546 type => 'string',
547 enum => ['shell', 'console', 'tty'],
548 default => 'tty',
549 },
550 protection => {
551 optional => 1,
552 type => 'boolean',
553 description => "Sets the protection flag of the container. This will prevent the CT or CT's disk remove/update operation.",
554 default => 0,
555 },
556 unprivileged => {
557 optional => 1,
558 type => 'boolean',
559 description => "Makes the container run as unprivileged user. (Should not be modified manually.)",
560 default => 0,
561 },
562 features => {
563 optional => 1,
564 type => 'string',
565 format => $features_desc,
566 description => "Allow containers access to advanced features.",
567 },
568 hookscript => {
569 optional => 1,
570 type => 'string',
571 format => 'pve-volume-id',
572 description => 'Script that will be exectued during various steps in the containers lifetime.',
573 },
574 tags => {
575 type => 'string', format => 'pve-tag-list',
576 description => 'Tags of the Container. This is only meta information.',
577 optional => 1,
578 },
579 debug => {
580 optional => 1,
581 type => 'boolean',
582 description => "Try to be more verbose. For now this only enables debug log-level on start.",
583 default => 0,
584 },
585 };
586
587 my $valid_lxc_conf_keys = {
588 'lxc.apparmor.profile' => 1,
589 'lxc.apparmor.allow_incomplete' => 1,
590 'lxc.apparmor.allow_nesting' => 1,
591 'lxc.apparmor.raw' => 1,
592 'lxc.selinux.context' => 1,
593 'lxc.include' => 1,
594 'lxc.arch' => 1,
595 'lxc.uts.name' => 1,
596 'lxc.signal.halt' => 1,
597 'lxc.signal.reboot' => 1,
598 'lxc.signal.stop' => 1,
599 'lxc.init.cmd' => 1,
600 'lxc.pty.max' => 1,
601 'lxc.console.logfile' => 1,
602 'lxc.console.path' => 1,
603 'lxc.tty.max' => 1,
604 'lxc.devtty.dir' => 1,
605 'lxc.hook.autodev' => 1,
606 'lxc.autodev' => 1,
607 'lxc.kmsg' => 1,
608 'lxc.mount.fstab' => 1,
609 'lxc.mount.entry' => 1,
610 'lxc.mount.auto' => 1,
611 'lxc.rootfs.path' => 'lxc.rootfs.path is auto generated from rootfs',
612 'lxc.rootfs.mount' => 1,
613 'lxc.rootfs.options' => 'lxc.rootfs.options is not supported' .
614 ', please use mount point options in the "rootfs" key',
615 # lxc.cgroup.*
616 # lxc.prlimit.*
617 # lxc.net.*
618 'lxc.cap.drop' => 1,
619 'lxc.cap.keep' => 1,
620 'lxc.seccomp.profile' => 1,
621 'lxc.seccomp.notify.proxy' => 1,
622 'lxc.seccomp.notify.cookie' => 1,
623 'lxc.idmap' => 1,
624 'lxc.hook.pre-start' => 1,
625 'lxc.hook.pre-mount' => 1,
626 'lxc.hook.mount' => 1,
627 'lxc.hook.start' => 1,
628 'lxc.hook.stop' => 1,
629 'lxc.hook.post-stop' => 1,
630 'lxc.hook.clone' => 1,
631 'lxc.hook.destroy' => 1,
632 'lxc.hook.version' => 1,
633 'lxc.log.level' => 1,
634 'lxc.log.file' => 1,
635 'lxc.start.auto' => 1,
636 'lxc.start.delay' => 1,
637 'lxc.start.order' => 1,
638 'lxc.group' => 1,
639 'lxc.environment' => 1,
640
641 # All these are namespaced via CLONE_NEWIPC (see namespaces(7)).
642 'lxc.sysctl.fs.mqueue' => 1,
643 'lxc.sysctl.kernel.msgmax' => 1,
644 'lxc.sysctl.kernel.msgmnb' => 1,
645 'lxc.sysctl.kernel.msgmni' => 1,
646 'lxc.sysctl.kernel.sem' => 1,
647 'lxc.sysctl.kernel.shmall' => 1,
648 'lxc.sysctl.kernel.shmmax' => 1,
649 'lxc.sysctl.kernel.shmmni' => 1,
650 'lxc.sysctl.kernel.shm_rmid_forced' => 1,
651 };
652
653 my $deprecated_lxc_conf_keys = {
654 # Deprecated (removed with lxc 3.0):
655 'lxc.aa_profile' => 'lxc.apparmor.profile',
656 'lxc.aa_allow_incomplete' => 'lxc.apparmor.allow_incomplete',
657 'lxc.console' => 'lxc.console.path',
658 'lxc.devttydir' => 'lxc.tty.dir',
659 'lxc.haltsignal' => 'lxc.signal.halt',
660 'lxc.rebootsignal' => 'lxc.signal.reboot',
661 'lxc.stopsignal' => 'lxc.signal.stop',
662 'lxc.id_map' => 'lxc.idmap',
663 'lxc.init_cmd' => 'lxc.init.cmd',
664 'lxc.loglevel' => 'lxc.log.level',
665 'lxc.logfile' => 'lxc.log.file',
666 'lxc.mount' => 'lxc.mount.fstab',
667 'lxc.network.type' => 'lxc.net.INDEX.type',
668 'lxc.network.flags' => 'lxc.net.INDEX.flags',
669 'lxc.network.link' => 'lxc.net.INDEX.link',
670 'lxc.network.mtu' => 'lxc.net.INDEX.mtu',
671 'lxc.network.name' => 'lxc.net.INDEX.name',
672 'lxc.network.hwaddr' => 'lxc.net.INDEX.hwaddr',
673 'lxc.network.ipv4' => 'lxc.net.INDEX.ipv4.address',
674 'lxc.network.ipv4.gateway' => 'lxc.net.INDEX.ipv4.gateway',
675 'lxc.network.ipv6' => 'lxc.net.INDEX.ipv6.address',
676 'lxc.network.ipv6.gateway' => 'lxc.net.INDEX.ipv6.gateway',
677 'lxc.network.script.up' => 'lxc.net.INDEX.script.up',
678 'lxc.network.script.down' => 'lxc.net.INDEX.script.down',
679 'lxc.pts' => 'lxc.pty.max',
680 'lxc.se_context' => 'lxc.selinux.context',
681 'lxc.seccomp' => 'lxc.seccomp.profile',
682 'lxc.tty' => 'lxc.tty.max',
683 'lxc.utsname' => 'lxc.uts.name',
684 };
685
686 sub is_valid_lxc_conf_key {
687 my ($vmid, $key) = @_;
688 if ($key =~ /^lxc\.limit\./) {
689 warn "vm $vmid - $key: lxc.limit.* was renamed to lxc.prlimit.*\n";
690 return 1;
691 }
692 if (defined(my $new_name = $deprecated_lxc_conf_keys->{$key})) {
693 warn "vm $vmid - $key is deprecated and was renamed to $new_name\n";
694 return 1;
695 }
696 my $validity = $valid_lxc_conf_keys->{$key};
697 return $validity if defined($validity);
698 return 1 if $key =~ /^lxc\.cgroup2?\./ # allow all cgroup values
699 || $key =~ /^lxc\.prlimit\./ # allow all prlimits
700 || $key =~ /^lxc\.net\./; # allow custom network definitions
701 return 0;
702 }
703
704 our $netconf_desc = {
705 type => {
706 type => 'string',
707 optional => 1,
708 description => "Network interface type.",
709 enum => [qw(veth)],
710 },
711 name => {
712 type => 'string',
713 format_description => 'string',
714 description => 'Name of the network device as seen from inside the container. (lxc.network.name)',
715 pattern => '[-_.\w\d]+',
716 },
717 bridge => {
718 type => 'string',
719 format_description => 'bridge',
720 description => 'Bridge to attach the network device to.',
721 pattern => '[-_.\w\d]+',
722 optional => 1,
723 },
724 hwaddr => get_standard_option('mac-addr', {
725 description => 'The interface MAC address. This is dynamically allocated by default, but you can set that statically if needed, for example to always have the same link-local IPv6 address. (lxc.network.hwaddr)',
726 }),
727 mtu => {
728 type => 'integer',
729 description => 'Maximum transfer unit of the interface. (lxc.network.mtu)',
730 minimum => 64, # minimum ethernet frame is 64 bytes
731 optional => 1,
732 },
733 ip => {
734 type => 'string',
735 format => 'pve-ipv4-config',
736 format_description => '(IPv4/CIDR|dhcp|manual)',
737 description => 'IPv4 address in CIDR format.',
738 optional => 1,
739 },
740 gw => {
741 type => 'string',
742 format => 'ipv4',
743 format_description => 'GatewayIPv4',
744 description => 'Default gateway for IPv4 traffic.',
745 optional => 1,
746 },
747 ip6 => {
748 type => 'string',
749 format => 'pve-ipv6-config',
750 format_description => '(IPv6/CIDR|auto|dhcp|manual)',
751 description => 'IPv6 address in CIDR format.',
752 optional => 1,
753 },
754 gw6 => {
755 type => 'string',
756 format => 'ipv6',
757 format_description => 'GatewayIPv6',
758 description => 'Default gateway for IPv6 traffic.',
759 optional => 1,
760 },
761 firewall => {
762 type => 'boolean',
763 description => "Controls whether this interface's firewall rules should be used.",
764 optional => 1,
765 },
766 tag => {
767 type => 'integer',
768 minimum => 1,
769 maximum => 4094,
770 description => "VLAN tag for this interface.",
771 optional => 1,
772 },
773 trunks => {
774 type => 'string',
775 pattern => qr/\d+(?:;\d+)*/,
776 format_description => 'vlanid[;vlanid...]',
777 description => "VLAN ids to pass through the interface",
778 optional => 1,
779 },
780 rate => {
781 type => 'number',
782 format_description => 'mbps',
783 description => "Apply rate limiting to the interface",
784 optional => 1,
785 },
786 };
787 PVE::JSONSchema::register_format('pve-lxc-network', $netconf_desc);
788
789 my $MAX_LXC_NETWORKS = 32;
790 for (my $i = 0; $i < $MAX_LXC_NETWORKS; $i++) {
791 $confdesc->{"net$i"} = {
792 optional => 1,
793 type => 'string', format => $netconf_desc,
794 description => "Specifies network interfaces for the container.",
795 };
796 }
797
798 PVE::JSONSchema::register_format('pve-ct-timezone', \&verify_ct_timezone);
799 sub verify_ct_timezone {
800 my ($timezone, $noerr) = @_;
801
802 return if $timezone eq 'host'; # using host settings
803
804 PVE::JSONSchema::pve_verify_timezone($timezone);
805 }
806
807 PVE::JSONSchema::register_format('pve-lxc-mp-string', \&verify_lxc_mp_string);
808 sub verify_lxc_mp_string {
809 my ($mp, $noerr) = @_;
810
811 # do not allow:
812 # /./ or /../
813 # /. or /.. at the end
814 # ../ at the beginning
815
816 if($mp =~ m@/\.\.?/@ ||
817 $mp =~ m@/\.\.?$@ ||
818 $mp =~ m@^\.\./@) {
819 return undef if $noerr;
820 die "$mp contains illegal character sequences\n";
821 }
822 return $mp;
823 }
824
825 my $mp_desc = {
826 %$rootfs_desc,
827 backup => {
828 type => 'boolean',
829 description => 'Whether to include the mount point in backups.',
830 verbose_description => 'Whether to include the mount point in backups '.
831 '(only used for volume mount points).',
832 optional => 1,
833 },
834 mp => {
835 type => 'string',
836 format => 'pve-lxc-mp-string',
837 format_description => 'Path',
838 description => 'Path to the mount point as seen from inside the container '.
839 '(must not contain symlinks).',
840 verbose_description => "Path to the mount point as seen from inside the container.\n\n".
841 "NOTE: Must not contain any symlinks for security reasons."
842 },
843 };
844 PVE::JSONSchema::register_format('pve-ct-mountpoint', $mp_desc);
845
846 my $unused_desc = {
847 volume => {
848 type => 'string',
849 default_key => 1,
850 format => 'pve-volume-id',
851 format_description => 'volume',
852 description => 'The volume that is not used currently.',
853 }
854 };
855
856 for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
857 $confdesc->{"mp$i"} = {
858 optional => 1,
859 type => 'string', format => $mp_desc,
860 description => "Use volume as container mount point. Use the special " .
861 "syntax STORAGE_ID:SIZE_IN_GiB to allocate a new volume.",
862 optional => 1,
863 };
864 }
865
866 for (my $i = 0; $i < $MAX_UNUSED_DISKS; $i++) {
867 $confdesc->{"unused$i"} = {
868 optional => 1,
869 type => 'string', format => $unused_desc,
870 description => "Reference to unused volumes. This is used internally, and should not be modified manually.",
871 }
872 }
873
874 sub parse_pct_config {
875 my ($filename, $raw) = @_;
876
877 return undef if !defined($raw);
878
879 my $res = {
880 digest => Digest::SHA::sha1_hex($raw),
881 snapshots => {},
882 pending => {},
883 };
884
885 $filename =~ m|/lxc/(\d+).conf$|
886 || die "got strange filename '$filename'";
887
888 my $vmid = $1;
889
890 my $conf = $res;
891 my $descr = '';
892 my $section = '';
893
894 my @lines = split(/\n/, $raw);
895 foreach my $line (@lines) {
896 next if $line =~ m/^\s*$/;
897
898 if ($line =~ m/^\[pve:pending\]\s*$/i) {
899 $section = 'pending';
900 $conf->{description} = $descr if $descr;
901 $descr = '';
902 $conf = $res->{$section} = {};
903 next;
904 } elsif ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
905 $section = $1;
906 $conf->{description} = $descr if $descr;
907 $descr = '';
908 $conf = $res->{snapshots}->{$section} = {};
909 next;
910 }
911
912 if ($line =~ m/^\#(.*)\s*$/) {
913 $descr .= PVE::Tools::decode_text($1) . "\n";
914 next;
915 }
916
917 if ($line =~ m/^(lxc\.[a-z0-9_\-\.]+)(:|\s*=)\s*(.*?)\s*$/) {
918 my $key = $1;
919 my $value = $3;
920 my $validity = is_valid_lxc_conf_key($vmid, $key);
921 if ($validity eq 1) {
922 push @{$conf->{lxc}}, [$key, $value];
923 } elsif (my $errmsg = $validity) {
924 warn "vm $vmid - $key: $errmsg\n";
925 } else {
926 warn "vm $vmid - unable to parse config: $line\n";
927 }
928 } elsif ($line =~ m/^(description):\s*(.*\S)\s*$/) {
929 $descr .= PVE::Tools::decode_text($2);
930 } elsif ($line =~ m/snapstate:\s*(prepare|delete)\s*$/) {
931 $conf->{snapstate} = $1;
932 } elsif ($line =~ m/^delete:\s*(.*\S)\s*$/) {
933 my $value = $1;
934 if ($section eq 'pending') {
935 $conf->{delete} = $value;
936 } else {
937 warn "vm $vmid - property 'delete' is only allowed in [pve:pending]\n";
938 }
939 } elsif ($line =~ m/^([a-z][a-z_]*\d*):\s*(.+?)\s*$/) {
940 my $key = $1;
941 my $value = $2;
942 eval { $value = PVE::LXC::Config->check_type($key, $value); };
943 warn "vm $vmid - unable to parse value of '$key' - $@" if $@;
944 $conf->{$key} = $value;
945 } else {
946 warn "vm $vmid - unable to parse config: $line\n";
947 }
948 }
949
950 $conf->{description} = $descr if $descr;
951
952 delete $res->{snapstate}; # just to be sure
953
954 return $res;
955 }
956
957 sub write_pct_config {
958 my ($filename, $conf) = @_;
959
960 delete $conf->{snapstate}; # just to be sure
961
962 my $volidlist = PVE::LXC::Config->get_vm_volumes($conf);
963 my $used_volids = {};
964 foreach my $vid (@$volidlist) {
965 $used_volids->{$vid} = 1;
966 }
967
968 # remove 'unusedX' settings if the volume is still used
969 foreach my $key (keys %$conf) {
970 my $value = $conf->{$key};
971 if ($key =~ m/^unused/ && $used_volids->{$value}) {
972 delete $conf->{$key};
973 }
974 }
975
976 my $generate_raw_config = sub {
977 my ($conf) = @_;
978
979 my $raw = '';
980
981 # add description as comment to top of file
982 my $descr = $conf->{description} || '';
983 foreach my $cl (split(/\n/, $descr)) {
984 $raw .= '#' . PVE::Tools::encode_text($cl) . "\n";
985 }
986
987 foreach my $key (sort keys %$conf) {
988 next if $key eq 'digest' || $key eq 'description' ||
989 $key eq 'pending' || $key eq 'snapshots' ||
990 $key eq 'snapname' || $key eq 'lxc';
991 my $value = $conf->{$key};
992 die "detected invalid newline inside property '$key'\n"
993 if $value =~ m/\n/;
994 $raw .= "$key: $value\n";
995 }
996
997 if (my $lxcconf = $conf->{lxc}) {
998 foreach my $entry (@$lxcconf) {
999 my ($k, $v) = @$entry;
1000 $raw .= "$k: $v\n";
1001 }
1002 }
1003
1004 return $raw;
1005 };
1006
1007 my $raw = &$generate_raw_config($conf);
1008
1009 if (scalar(keys %{$conf->{pending}})){
1010 $raw .= "\n[pve:pending]\n";
1011 $raw .= &$generate_raw_config($conf->{pending});
1012 }
1013
1014 foreach my $snapname (sort keys %{$conf->{snapshots}}) {
1015 $raw .= "\n[$snapname]\n";
1016 $raw .= &$generate_raw_config($conf->{snapshots}->{$snapname});
1017 }
1018
1019 return $raw;
1020 }
1021
1022 sub update_pct_config {
1023 my ($class, $vmid, $conf, $running, $param, $delete, $revert) = @_;
1024
1025 my $storage_cfg = PVE::Storage::config();
1026
1027 foreach my $opt (@$revert) {
1028 delete $conf->{pending}->{$opt};
1029 $class->remove_from_pending_delete($conf, $opt); # also remove from deletion queue
1030 }
1031
1032 # write updates to pending section
1033 my $modified = {}; # record modified options
1034
1035 foreach my $opt (@$delete) {
1036 if (!defined($conf->{$opt}) && !defined($conf->{pending}->{$opt})) {
1037 warn "cannot delete '$opt' - not set in current configuration!\n";
1038 next;
1039 }
1040 $modified->{$opt} = 1;
1041 if ($opt eq 'memory' || $opt eq 'rootfs' || $opt eq 'ostype') {
1042 die "unable to delete required option '$opt'\n";
1043 } elsif ($opt =~ m/^unused(\d+)$/) {
1044 $class->check_protection($conf, "can't remove CT $vmid drive '$opt'");
1045 } elsif ($opt =~ m/^mp(\d+)$/) {
1046 $class->check_protection($conf, "can't remove CT $vmid drive '$opt'");
1047 } elsif ($opt eq 'unprivileged') {
1048 die "unable to delete read-only option: '$opt'\n";
1049 }
1050 $class->add_to_pending_delete($conf, $opt);
1051 }
1052
1053 my $check_content_type = sub {
1054 my ($mp) = @_;
1055 my $sid = PVE::Storage::parse_volume_id($mp->{volume});
1056 my $storage_config = PVE::Storage::storage_config($storage_cfg, $sid);
1057 die "storage '$sid' does not allow content type 'rootdir' (Container)\n"
1058 if !$storage_config->{content}->{rootdir};
1059 };
1060
1061 foreach my $opt (sort keys %$param) { # add/change
1062 $modified->{$opt} = 1;
1063 my $value = $param->{$opt};
1064 if ($opt =~ m/^mp(\d+)$/ || $opt eq 'rootfs') {
1065 $class->check_protection($conf, "can't update CT $vmid drive '$opt'");
1066 my $mp = $class->parse_volume($opt, $value);
1067 $check_content_type->($mp) if ($mp->{type} eq 'volume');
1068 } elsif ($opt eq 'hookscript') {
1069 PVE::GuestHelpers::check_hookscript($value);
1070 } elsif ($opt eq 'nameserver') {
1071 $value = PVE::LXC::verify_nameserver_list($value);
1072 } elsif ($opt eq 'searchdomain') {
1073 $value = PVE::LXC::verify_searchdomain_list($value);
1074 } elsif ($opt eq 'unprivileged') {
1075 die "unable to modify read-only option: '$opt'\n";
1076 }
1077 $conf->{pending}->{$opt} = $value;
1078 $class->remove_from_pending_delete($conf, $opt);
1079 }
1080
1081 my $changes = $class->cleanup_pending($conf);
1082
1083 my $errors = {};
1084 if ($running) {
1085 $class->vmconfig_hotplug_pending($vmid, $conf, $storage_cfg, $modified, $errors);
1086 } else {
1087 $class->vmconfig_apply_pending($vmid, $conf, $storage_cfg, $modified, $errors);
1088 }
1089
1090 return $errors;
1091 }
1092
1093 sub check_type {
1094 my ($class, $key, $value) = @_;
1095
1096 die "unknown setting '$key'\n" if !$confdesc->{$key};
1097
1098 my $type = $confdesc->{$key}->{type};
1099
1100 if (!defined($value)) {
1101 die "got undefined value\n";
1102 }
1103
1104 if ($value =~ m/[\n\r]/) {
1105 die "property contains a line feed\n";
1106 }
1107
1108 if ($type eq 'boolean') {
1109 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
1110 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
1111 die "type check ('boolean') failed - got '$value'\n";
1112 } elsif ($type eq 'integer') {
1113 return int($1) if $value =~ m/^(\d+)$/;
1114 die "type check ('integer') failed - got '$value'\n";
1115 } elsif ($type eq 'number') {
1116 return $value if $value =~ m/^(\d+)(\.\d+)?$/;
1117 die "type check ('number') failed - got '$value'\n";
1118 } elsif ($type eq 'string') {
1119 if (my $fmt = $confdesc->{$key}->{format}) {
1120 PVE::JSONSchema::check_format($fmt, $value);
1121 return $value;
1122 }
1123 return $value;
1124 } else {
1125 die "internal error"
1126 }
1127 }
1128
1129
1130 # add JSON properties for create and set function
1131 sub json_config_properties {
1132 my ($class, $prop) = @_;
1133
1134 foreach my $opt (keys %$confdesc) {
1135 next if $opt eq 'parent' || $opt eq 'snaptime';
1136 next if $prop->{$opt};
1137 $prop->{$opt} = $confdesc->{$opt};
1138 }
1139
1140 return $prop;
1141 }
1142
1143 my $parse_ct_mountpoint_full = sub {
1144 my ($class, $desc, $data, $noerr) = @_;
1145
1146 $data //= '';
1147
1148 my $res;
1149 eval { $res = PVE::JSONSchema::parse_property_string($desc, $data) };
1150 if ($@) {
1151 return undef if $noerr;
1152 die $@;
1153 }
1154
1155 if (defined(my $size = $res->{size})) {
1156 $size = PVE::JSONSchema::parse_size($size);
1157 if (!defined($size)) {
1158 return undef if $noerr;
1159 die "invalid size: $size\n";
1160 }
1161 $res->{size} = $size;
1162 }
1163
1164 $res->{type} = $class->classify_mountpoint($res->{volume});
1165
1166 return $res;
1167 };
1168
1169 sub print_ct_mountpoint {
1170 my ($class, $info, $nomp) = @_;
1171 my $skip = [ 'type' ];
1172 push @$skip, 'mp' if $nomp;
1173 return PVE::JSONSchema::print_property_string($info, $mp_desc, $skip);
1174 }
1175
1176 sub parse_volume {
1177 my ($class, $key, $volume_string, $noerr) = @_;
1178
1179 if ($key eq 'rootfs') {
1180 my $res = $parse_ct_mountpoint_full->($class, $rootfs_desc, $volume_string, $noerr);
1181 $res->{mp} = '/' if defined($res);
1182 return $res;
1183 } elsif ($key =~ m/^mp\d+$/) {
1184 return $parse_ct_mountpoint_full->($class, $mp_desc, $volume_string, $noerr);
1185 } elsif ($key =~ m/^unused\d+$/) {
1186 return $parse_ct_mountpoint_full->($class, $unused_desc, $volume_string, $noerr);
1187 }
1188
1189 die "parse_volume - unknown type: $key\n";
1190 }
1191
1192 sub print_volume {
1193 my ($class, $key, $volume) = @_;
1194
1195 return $class->print_ct_mountpoint($volume, $key eq 'rootfs');
1196 }
1197
1198 sub volid_key {
1199 my ($class) = @_;
1200
1201 return 'volume';
1202 }
1203
1204 sub print_lxc_network {
1205 my ($class, $net) = @_;
1206 return PVE::JSONSchema::print_property_string($net, $netconf_desc);
1207 }
1208
1209 sub parse_lxc_network {
1210 my ($class, $data) = @_;
1211
1212 my $res = {};
1213
1214 return $res if !$data;
1215
1216 $res = PVE::JSONSchema::parse_property_string($netconf_desc, $data);
1217
1218 $res->{type} = 'veth';
1219 if (!$res->{hwaddr}) {
1220 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
1221 $res->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
1222 }
1223
1224 return $res;
1225 }
1226
1227 sub parse_features {
1228 my ($class, $data) = @_;
1229 return {} if !$data;
1230 return PVE::JSONSchema::parse_property_string($features_desc, $data);
1231 }
1232
1233 sub option_exists {
1234 my ($class, $name) = @_;
1235
1236 return defined($confdesc->{$name});
1237 }
1238 # END JSON config code
1239
1240 my $LXC_FASTPLUG_OPTIONS= {
1241 'description' => 1,
1242 'onboot' => 1,
1243 'startup' => 1,
1244 'protection' => 1,
1245 'hostname' => 1,
1246 'hookscript' => 1,
1247 'cores' => 1,
1248 'tags' => 1,
1249 'lock' => 1,
1250 };
1251
1252 sub vmconfig_hotplug_pending {
1253 my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_;
1254
1255 my $pid = PVE::LXC::find_lxc_pid($vmid);
1256 my $rootdir = "/proc/$pid/root";
1257
1258 my $add_hotplug_error = sub {
1259 my ($opt, $msg) = @_;
1260 $errors->{$opt} = "unable to hotplug $opt: $msg";
1261 };
1262
1263 foreach my $opt (sort keys %{$conf->{pending}}) { # add/change
1264 next if $selection && !$selection->{$opt};
1265 if ($LXC_FASTPLUG_OPTIONS->{$opt}) {
1266 $conf->{$opt} = delete $conf->{pending}->{$opt};
1267 }
1268 }
1269
1270 my $cgroup = PVE::LXC::CGroup->new($vmid);
1271
1272 # There's no separate swap size to configure, there's memory and "total"
1273 # memory (iow. memory+swap). This means we have to change them together.
1274 my $hotplug_memory_done;
1275 my $hotplug_memory = sub {
1276 my ($wanted_memory, $wanted_swap) = @_;
1277
1278 $wanted_memory = int($wanted_memory * 1024 * 1024) if defined($wanted_memory);
1279 $wanted_swap = int($wanted_swap * 1024 * 1024) if defined($wanted_swap);
1280 $cgroup->change_memory_limit($wanted_memory, $wanted_swap);
1281
1282 $hotplug_memory_done = 1;
1283 };
1284
1285 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
1286 # FIXME: $force deletion is not implemented for CTs
1287 foreach my $opt (sort keys %$pending_delete_hash) {
1288 next if $selection && !$selection->{$opt};
1289 eval {
1290 if ($LXC_FASTPLUG_OPTIONS->{$opt}) {
1291 # pass
1292 } elsif ($opt =~ m/^unused(\d+)$/) {
1293 PVE::LXC::delete_mountpoint_volume($storecfg, $vmid, $conf->{$opt})
1294 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1295 } elsif ($opt eq 'swap') {
1296 $hotplug_memory->(undef, 0);
1297 } elsif ($opt eq 'cpulimit') {
1298 $cgroup->change_cpu_quota(-1, 100000);
1299 } elsif ($opt eq 'cpuunits') {
1300 $cgroup->change_cpu_shares(undef, $confdesc->{cpuunits}->{default});
1301 } elsif ($opt =~ m/^net(\d)$/) {
1302 my $netid = $1;
1303 PVE::Network::veth_delete("veth${vmid}i$netid");
1304 } else {
1305 die "skip\n"; # skip non-hotpluggable opts
1306 }
1307 };
1308 if (my $err = $@) {
1309 $add_hotplug_error->($opt, $err) if $err ne "skip\n";
1310 } else {
1311 delete $conf->{$opt};
1312 $class->remove_from_pending_delete($conf, $opt);
1313 }
1314 }
1315
1316 foreach my $opt (sort keys %{$conf->{pending}}) {
1317 next if $opt eq 'delete'; # just to be sure
1318 next if $selection && !$selection->{$opt};
1319 my $value = $conf->{pending}->{$opt};
1320 eval {
1321 if ($opt eq 'cpulimit') {
1322 my $quota = 100000 * $value;
1323 $cgroup->change_cpu_quota(int(100000 * $value), 100000);
1324 } elsif ($opt eq 'cpuunits') {
1325 $cgroup->change_cpu_shares($value, $confdesc->{cpuunits}->{default});
1326 } elsif ($opt =~ m/^net(\d+)$/) {
1327 my $netid = $1;
1328 my $net = $class->parse_lxc_network($value);
1329 $value = $class->print_lxc_network($net);
1330 PVE::LXC::update_net($vmid, $conf, $opt, $net, $netid, $rootdir);
1331 } elsif ($opt eq 'memory' || $opt eq 'swap') {
1332 if (!$hotplug_memory_done) { # don't call twice if both opts are passed
1333 $hotplug_memory->($conf->{pending}->{memory}, $conf->{pending}->{swap});
1334 }
1335 } elsif ($opt =~ m/^mp(\d+)$/) {
1336 if (!PVE::LXC::Tools::can_use_new_mount_api()) {
1337 die "skip\n";
1338 }
1339
1340 if (exists($conf->{$opt})) {
1341 die "skip\n"; # don't try to hotplug over existing mp
1342 }
1343
1344 $class->apply_pending_mountpoint($vmid, $conf, $opt, $storecfg, 1);
1345 # apply_pending_mountpoint modifies the value if it creates a new disk
1346 $value = $conf->{pending}->{$opt};
1347 } else {
1348 die "skip\n"; # skip non-hotpluggable
1349 }
1350 };
1351 if (my $err = $@) {
1352 $add_hotplug_error->($opt, $err) if $err ne "skip\n";
1353 } else {
1354 $conf->{$opt} = $value;
1355 delete $conf->{pending}->{$opt};
1356 }
1357 }
1358 }
1359
1360 sub vmconfig_apply_pending {
1361 my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_;
1362
1363 my $add_apply_error = sub {
1364 my ($opt, $msg) = @_;
1365 my $err_msg = "unable to apply pending change $opt : $msg";
1366 $errors->{$opt} = $err_msg;
1367 warn $err_msg;
1368 };
1369
1370 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
1371 # FIXME: $force deletion is not implemented for CTs
1372 foreach my $opt (sort keys %$pending_delete_hash) {
1373 next if $selection && !$selection->{$opt};
1374 eval {
1375 if ($opt =~ m/^mp(\d+)$/) {
1376 my $mp = $class->parse_volume($opt, $conf->{$opt});
1377 if ($mp->{type} eq 'volume') {
1378 $class->add_unused_volume($conf, $mp->{volume})
1379 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1380 }
1381 } elsif ($opt =~ m/^unused(\d+)$/) {
1382 PVE::LXC::delete_mountpoint_volume($storecfg, $vmid, $conf->{$opt})
1383 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1384 }
1385 };
1386 if (my $err = $@) {
1387 $add_apply_error->($opt, $err);
1388 } else {
1389 delete $conf->{$opt};
1390 $class->remove_from_pending_delete($conf, $opt);
1391 }
1392 }
1393
1394 $class->cleanup_pending($conf);
1395
1396 foreach my $opt (sort keys %{$conf->{pending}}) { # add/change
1397 next if $opt eq 'delete'; # just to be sure
1398 next if $selection && !$selection->{$opt};
1399 eval {
1400 if ($opt =~ m/^mp(\d+)$/) {
1401 $class->apply_pending_mountpoint($vmid, $conf, $opt, $storecfg, 0);
1402 } elsif ($opt =~ m/^net(\d+)$/) {
1403 my $netid = $1;
1404 my $net = $class->parse_lxc_network($conf->{pending}->{$opt});
1405 $conf->{pending}->{$opt} = $class->print_lxc_network($net);
1406 }
1407 };
1408 if (my $err = $@) {
1409 $add_apply_error->($opt, $err);
1410 } else {
1411 $conf->{$opt} = delete $conf->{pending}->{$opt};
1412 }
1413 }
1414 }
1415
1416 my $rescan_volume = sub {
1417 my ($storecfg, $mp) = @_;
1418 eval {
1419 $mp->{size} = PVE::Storage::volume_size_info($storecfg, $mp->{volume}, 5);
1420 };
1421 warn "Could not rescan volume size - $@\n" if $@;
1422 };
1423
1424 sub apply_pending_mountpoint {
1425 my ($class, $vmid, $conf, $opt, $storecfg, $running) = @_;
1426
1427 my $mp = $class->parse_volume($opt, $conf->{pending}->{$opt});
1428 my $old = $conf->{$opt};
1429 if ($mp->{type} eq 'volume') {
1430 if ($mp->{volume} =~ $PVE::LXC::NEW_DISK_RE) {
1431 my $original_value = $conf->{pending}->{$opt};
1432 my $vollist = PVE::LXC::create_disks(
1433 $storecfg,
1434 $vmid,
1435 { $opt => $original_value },
1436 $conf,
1437 1,
1438 );
1439 if ($running) {
1440 # Re-parse mount point:
1441 my $mp = $class->parse_volume($opt, $conf->{pending}->{$opt});
1442 eval {
1443 PVE::LXC::mountpoint_hotplug($vmid, $conf, $opt, $mp, $storecfg);
1444 };
1445 my $err = $@;
1446 if ($err) {
1447 PVE::LXC::destroy_disks($storecfg, $vollist);
1448 # The pending-changes code collects errors but keeps on looping through further
1449 # pending changes, so unroll the change in $conf as well if destroy_disks()
1450 # didn't die().
1451 $conf->{pending}->{$opt} = $original_value;
1452 die $err;
1453 }
1454 }
1455 } else {
1456 die "skip\n" if $running && defined($old); # TODO: "changing" mount points?
1457 $rescan_volume->($storecfg, $mp);
1458 if ($running) {
1459 PVE::LXC::mountpoint_hotplug($vmid, $conf, $opt, $mp, $storecfg);
1460 }
1461 $conf->{pending}->{$opt} = $class->print_ct_mountpoint($mp);
1462 }
1463 }
1464
1465 if (defined($old)) {
1466 my $mp = $class->parse_volume($opt, $old);
1467 if ($mp->{type} eq 'volume') {
1468 $class->add_unused_volume($conf, $mp->{volume})
1469 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1470 }
1471 }
1472 }
1473
1474 sub classify_mountpoint {
1475 my ($class, $vol) = @_;
1476 if ($vol =~ m!^/!) {
1477 return 'device' if $vol =~ m!^/dev/!;
1478 return 'bind';
1479 }
1480 return 'volume';
1481 }
1482
1483 my $__is_volume_in_use = sub {
1484 my ($class, $config, $volid) = @_;
1485 my $used = 0;
1486
1487 $class->foreach_volume($config, sub {
1488 my ($ms, $mountpoint) = @_;
1489 return if $used;
1490 $used = $mountpoint->{type} eq 'volume' && $mountpoint->{volume} eq $volid;
1491 });
1492
1493 return $used;
1494 };
1495
1496 sub is_volume_in_use_by_snapshots {
1497 my ($class, $config, $volid) = @_;
1498
1499 if (my $snapshots = $config->{snapshots}) {
1500 foreach my $snap (keys %$snapshots) {
1501 return 1 if $__is_volume_in_use->($class, $snapshots->{$snap}, $volid);
1502 }
1503 }
1504
1505 return 0;
1506 }
1507
1508 sub is_volume_in_use {
1509 my ($class, $config, $volid, $include_snapshots, $include_pending) = @_;
1510 return 1 if $__is_volume_in_use->($class, $config, $volid);
1511 return 1 if $include_snapshots && $class->is_volume_in_use_by_snapshots($config, $volid);
1512 return 1 if $include_pending && $__is_volume_in_use->($class, $config->{pending}, $volid);
1513 return 0;
1514 }
1515
1516 sub has_dev_console {
1517 my ($class, $conf) = @_;
1518
1519 return !(defined($conf->{console}) && !$conf->{console});
1520 }
1521
1522 sub has_lxc_entry {
1523 my ($class, $conf, $keyname) = @_;
1524
1525 if (my $lxcconf = $conf->{lxc}) {
1526 foreach my $entry (@$lxcconf) {
1527 my ($key, undef) = @$entry;
1528 return 1 if $key eq $keyname;
1529 }
1530 }
1531
1532 return 0;
1533 }
1534
1535 sub get_tty_count {
1536 my ($class, $conf) = @_;
1537
1538 return $conf->{tty} // $confdesc->{tty}->{default};
1539 }
1540
1541 sub get_cmode {
1542 my ($class, $conf) = @_;
1543
1544 return $conf->{cmode} // $confdesc->{cmode}->{default};
1545 }
1546
1547 sub valid_volume_keys {
1548 my ($class, $reverse) = @_;
1549
1550 my @names = ('rootfs');
1551
1552 for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
1553 push @names, "mp$i";
1554 }
1555
1556 return $reverse ? reverse @names : @names;
1557 }
1558
1559 sub get_vm_volumes {
1560 my ($class, $conf, $excludes) = @_;
1561
1562 my $vollist = [];
1563
1564 $class->foreach_volume($conf, sub {
1565 my ($ms, $mountpoint) = @_;
1566
1567 return if $excludes && $ms eq $excludes;
1568
1569 my $volid = $mountpoint->{volume};
1570 return if !$volid || $mountpoint->{type} ne 'volume';
1571
1572 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
1573 return if !$sid;
1574
1575 push @$vollist, $volid;
1576 });
1577
1578 return $vollist;
1579 }
1580
1581 sub get_replicatable_volumes {
1582 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
1583
1584 my $volhash = {};
1585
1586 my $test_volid = sub {
1587 my ($volid, $mountpoint) = @_;
1588
1589 return if !$volid;
1590
1591 my $mptype = $mountpoint->{type};
1592 my $replicate = $mountpoint->{replicate} // 1;
1593
1594 if ($mptype ne 'volume') {
1595 # skip bindmounts if replicate = 0 even for cleanup,
1596 # since bind mounts could not have been replicated ever
1597 return if !$replicate;
1598 die "unable to replicate mountpoint type '$mptype'\n";
1599 }
1600
1601 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, $noerr);
1602 return if !$storeid;
1603
1604 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
1605 return if $scfg->{shared};
1606
1607 my ($path, $owner, $vtype) = PVE::Storage::path($storecfg, $volid);
1608 return if !$owner || ($owner != $vmid);
1609
1610 die "unable to replicate volume '$volid', type '$vtype'\n" if $vtype ne 'images';
1611
1612 return if !$cleanup && !$replicate;
1613
1614 if (!PVE::Storage::volume_has_feature($storecfg, 'replicate', $volid)) {
1615 return if $cleanup || $noerr;
1616 die "missing replicate feature on volume '$volid'\n";
1617 }
1618
1619 $volhash->{$volid} = 1;
1620 };
1621
1622 $class->foreach_volume($conf, sub {
1623 my ($ms, $mountpoint) = @_;
1624 $test_volid->($mountpoint->{volume}, $mountpoint);
1625 });
1626
1627 foreach my $snapname (keys %{$conf->{snapshots}}) {
1628 my $snap = $conf->{snapshots}->{$snapname};
1629 $class->foreach_volume($snap, sub {
1630 my ($ms, $mountpoint) = @_;
1631 $test_volid->($mountpoint->{volume}, $mountpoint);
1632 });
1633 }
1634
1635 # add 'unusedX' volumes to volhash
1636 foreach my $key (keys %$conf) {
1637 if ($key =~ m/^unused/) {
1638 $test_volid->($conf->{$key}, { type => 'volume', replicate => 1 });
1639 }
1640 }
1641
1642 return $volhash;
1643 }
1644
1645 sub get_backup_volumes {
1646 my ($class, $conf) = @_;
1647
1648 my $return_volumes = [];
1649
1650 my $test_mountpoint = sub {
1651 my ($key, $volume) = @_;
1652
1653 my ($included, $reason) = $class->mountpoint_backup_enabled($key, $volume);
1654
1655 push @$return_volumes, {
1656 key => $key,
1657 included => $included,
1658 reason => $reason,
1659 volume_config => $volume,
1660 };
1661 };
1662
1663 PVE::LXC::Config->foreach_volume($conf, $test_mountpoint);
1664
1665 return $return_volumes;
1666 }
1667
1668 1;