]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Config.pm
d/control: bump pve-storage dependency for new vdisk param
[pve-container.git] / src / PVE / LXC / Config.pm
CommitLineData
67afe46e
FG
1package PVE::LXC::Config;
2
3use strict;
4use warnings;
8463099d 5use Fcntl qw(O_RDONLY);
67afe46e
FG
6
7use PVE::AbstractConfig;
8use PVE::Cluster qw(cfs_register_file);
8b076579 9use PVE::DataCenterConfig;
1a416433 10use PVE::GuestHelpers;
67afe46e
FG
11use PVE::INotify;
12use PVE::JSONSchema qw(get_standard_option);
13use PVE::Tools;
14
15use base qw(PVE::AbstractConfig);
16
40cd2e89
SI
17use constant {FIFREEZE => 0xc0045877,
18 FITHAW => 0xc0045878};
19
67afe46e
FG
20my $nodename = PVE::INotify::nodename();
21my $lock_handles = {};
22my $lockdir = "/run/lock/lxc";
23mkdir $lockdir;
24mkdir "/etc/pve/nodes/$nodename/lxc";
717f70b7 25my $MAX_MOUNT_POINTS = 256;
67afe46e
FG
26my $MAX_UNUSED_DISKS = $MAX_MOUNT_POINTS;
27
28# BEGIN implemented abstract methods from PVE::AbstractConfig
29
30sub guest_type {
31 return "CT";
32}
33
4518000b
FG
34sub __config_max_unused_disks {
35 my ($class) = @_;
36
37 return $MAX_UNUSED_DISKS;
38}
39
67afe46e
FG
40sub config_file_lock {
41 my ($class, $vmid) = @_;
42
43 return "$lockdir/pve-config-${vmid}.lock";
44}
45
46sub cfs_config_path {
47 my ($class, $vmid, $node) = @_;
48
49 $node = $nodename if !$node;
50 return "nodes/$node/lxc/$vmid.conf";
51}
52
1a8269bc 53sub mountpoint_backup_enabled {
ec7f0f09 54 my ($class, $mp_key, $mountpoint) = @_;
1a8269bc 55
dd4fa308
AL
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;
1a8269bc
DM
73}
74
4518000b
FG
75sub has_feature {
76 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
77 my $err;
78
4a954457
FE
79 my $opts;
80 if ($feature eq 'copy' || $feature eq 'clone') {
81 $opts = {'valid_target_formats' => ['raw', 'subvol']};
82 }
83
015740e6 84 $class->foreach_volume($conf, sub {
4518000b
FG
85 my ($ms, $mountpoint) = @_;
86
87 return if $err; # skip further test
ec7f0f09 88 return if $backup_only && !$class->mountpoint_backup_enabled($ms, $mountpoint);
4518000b
FG
89
90 $err = 1
91 if !PVE::Storage::volume_has_feature($storecfg, $feature,
92 $mountpoint->{volume},
4a954457 93 $snapname, $running, $opts);
4518000b
FG
94 });
95
96 return $err ? 0 : 1;
97}
98
99sub __snapshot_save_vmstate {
100 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
101 die "implement me - snapshot_save_vmstate\n";
102}
103
104sub __snapshot_check_running {
105 my ($class, $vmid) = @_;
106 return PVE::LXC::check_running($vmid);
107}
108
109sub __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
40cd2e89
SI
116# implements similar functionality to fsfreeze(8)
117sub 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
4518000b
FG
132sub __snapshot_freeze {
133 my ($class, $vmid, $unfreeze) = @_;
134
8463099d
SI
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
7a8591e8
SI
142 return if $mountpoint->{type} ne 'volume';
143
8463099d
SI
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
4518000b 162 if ($unfreeze) {
89424a8b 163 eval { PVE::LXC::thaw($vmid); };
4518000b 164 warn $@ if $@;
8463099d 165 $freeze_mountpoints->(1);
4518000b 166 } else {
89424a8b 167 PVE::LXC::freeze($vmid);
4518000b 168 PVE::LXC::sync_container_namespace($vmid);
8463099d 169 $freeze_mountpoints->(0);
4518000b
FG
170 }
171}
172
173sub __snapshot_create_vol_snapshot {
174 my ($class, $vmid, $ms, $mountpoint, $snapname) = @_;
175
176 my $storecfg = PVE::Storage::config();
177
1a8269bc 178 return if $snapname eq 'vzdump' &&
ec7f0f09 179 !$class->mountpoint_backup_enabled($ms, $mountpoint);
1a8269bc 180
4518000b
FG
181 PVE::Storage::volume_snapshot($storecfg, $mountpoint->{volume}, $snapname);
182}
183
184sub __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};
e4034859 191 my $mountpoint = $class->parse_volume($remove_drive, $value, 1);
4518000b 192 delete $snap->{$remove_drive};
d103721f
FG
193
194 $class->add_unused_volume($snap, $mountpoint->{volume})
195 if ($mountpoint->{type} eq 'volume');
4518000b
FG
196 }
197}
198
199sub __snapshot_delete_vmstate_file {
200 my ($class, $snap, $force) = @_;
201
202 die "implement me - saving vmstate\n";
203}
204
205sub __snapshot_delete_vol_snapshot {
a8e9a4ea 206 my ($class, $vmid, $ms, $mountpoint, $snapname, $unused) = @_;
4518000b 207
d103721f
FG
208 return if $snapname eq 'vzdump' &&
209 !$class->mountpoint_backup_enabled($ms, $mountpoint);
210
4518000b
FG
211 my $storecfg = PVE::Storage::config();
212 PVE::Storage::volume_snapshot_delete($storecfg, $mountpoint->{volume}, $snapname);
a8e9a4ea 213 push @$unused, $mountpoint->{volume};
4518000b
FG
214}
215
216sub __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
223sub __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
230sub __snapshot_rollback_vm_stop {
231 my ($class, $vmid) = @_;
232
b1bad293 233 PVE::LXC::vm_stop($vmid, 1)
4518000b
FG
234 if $class->__snapshot_check_running($vmid);
235}
236
237sub __snapshot_rollback_vm_start {
67779c3c 238 my ($class, $vmid, $vmstate, $data);
4518000b
FG
239
240 die "implement me - save vmstate\n";
241}
242
a8656869
FG
243sub __snapshot_rollback_get_unused {
244 my ($class, $conf, $snap) = @_;
245
246 my $unused = [];
247
5e5d76cf 248 $class->foreach_volume($conf, sub {
a8656869
FG
249 my ($vs, $volume) = @_;
250
251 return if $volume->{type} ne 'volume';
252
253 my $found = 0;
254 my $volid = $volume->{volume};
255
5e5d76cf 256 $class->foreach_volume($snap, sub {
a8656869
FG
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
67afe46e
FG
272# END implemented abstract methods from PVE::AbstractConfig
273
1b4cf758
FG
274# BEGIN JSON config code
275
276cfs_register_file('/lxc/', \&parse_pct_config, \&write_pct_config);
277
2bf24eb3 278
e80cb0cd
TL
279my $valid_mount_option_re = qr/(noatime|nodev|nosuid|noexec)/;
280
281sub is_valid_mount_option {
282 my ($option) = @_;
283 return $option =~ $valid_mount_option_re;
2bf24eb3
OB
284}
285
1b4cf758
FG
286my $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 },
1b4cf758
FG
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',
1b4cf758
FG
303 description => 'Explicitly enable or disable ACL support.',
304 optional => 1,
305 },
2bf24eb3
OB
306 mountoptions => {
307 optional => 1,
308 type => 'string',
309 description => 'Extra mount options for rootfs/mps.',
310 format_description => 'opt[;opt...]',
e80cb0cd 311 pattern => qr/$valid_mount_option_re(;$valid_mount_option_re)*/,
2bf24eb3 312 },
1b4cf758
FG
313 ro => {
314 type => 'boolean',
235dbdf3 315 description => 'Read-only mount point',
1b4cf758
FG
316 optional => 1,
317 },
318 quota => {
319 type => 'boolean',
1b4cf758
FG
320 description => 'Enable user quotas inside the container (not supported with zfs subvolumes)',
321 optional => 1,
322 },
76ec0820 323 replicate => {
f8aa3d35
WL
324 type => 'boolean',
325 description => 'Will include this volume to a storage replica job.',
326 optional => 1,
327 default => 1,
328 },
552e168f
FG
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 },
1b4cf758
FG
336};
337
338PVE::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
08a58f12
WB
344# IP address with optional interface suffix for link local ipv6 addresses
345PVE::JSONSchema::register_format('lxc-ip-with-ll-iface', \&verify_ip_with_ll_iface);
346sub 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
5a63f1c5
WB
361my $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;...',
e188f1bb 372 pattern => qr/[a-zA-Z0-9_; ]+/,
5a63f1c5
WB
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 },
96f8d2a2
WB
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 },
2df08734
WB
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 },
741b7737
TL
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 },
5a63f1c5
WB
416};
417
1b4cf758
FG
418my $confdesc = {
419 lock => {
420 optional => 1,
421 type => 'string',
422 description => "Lock/unlock the VM.",
7fc1d9eb 423 enum => [qw(backup create destroyed disk fstrim migrate mounted rollback snapshot snapshot-delete)],
1b4cf758
FG
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',
e1d54a38 441 enum => ['amd64', 'i386', 'arm64', 'armhf'],
1b4cf758
FG
442 description => "OS architecture type.",
443 default => 'amd64',
444 },
445 ostype => {
446 optional => 1,
447 type => 'string',
3ef1f6e1 448 enum => [qw(debian devuan ubuntu centos fedora opensuse archlinux alpine gentoo unmanaged)],
1b4cf758
FG
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 },
f2357408
DM
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,
a804f2d1 470 maximum => 8192,
f2357408 471 },
1b4cf758
FG
472 cpulimit => {
473 optional => 1,
474 type => 'number',
064529c3 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.",
1b4cf758 476 minimum => 0,
a804f2d1 477 maximum => 8192,
1b4cf758
FG
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',
a069f163 511 description => "Container description. Only used on the configuration web interface.",
1b4cf758
FG
512 },
513 searchdomain => {
514 optional => 1,
515 type => 'string', format => 'dns-name-list',
516 description => "Sets DNS search domains for a container. Create will automatically use the setting from the host if you neither set searchdomain nor nameserver.",
517 },
518 nameserver => {
519 optional => 1,
08a58f12 520 type => 'string', format => 'lxc-ip-with-ll-iface-list',
1b4cf758
FG
521 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.",
522 },
e6e308ae
OB
523 timezone => {
524 optional => 1,
525 type => 'string', format => 'pve-ct-timezone',
526 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",
527 },
1b4cf758
FG
528 rootfs => get_standard_option('pve-ct-rootfs'),
529 parent => {
530 optional => 1,
531 type => 'string', format => 'pve-configid',
532 maxLength => 40,
533 description => "Parent snapshot name. This is used internally, and should not be modified.",
534 },
535 snaptime => {
536 optional => 1,
537 description => "Timestamp for snapshots.",
538 type => 'integer',
539 minimum => 0,
540 },
541 cmode => {
542 optional => 1,
543 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).",
544 type => 'string',
545 enum => ['shell', 'console', 'tty'],
546 default => 'tty',
547 },
548 protection => {
549 optional => 1,
550 type => 'boolean',
551 description => "Sets the protection flag of the container. This will prevent the CT or CT's disk remove/update operation.",
552 default => 0,
553 },
554 unprivileged => {
555 optional => 1,
556 type => 'boolean',
557 description => "Makes the container run as unprivileged user. (Should not be modified manually.)",
558 default => 0,
559 },
5a63f1c5
WB
560 features => {
561 optional => 1,
562 type => 'string',
563 format => $features_desc,
564 description => "Allow containers access to advanced features.",
565 },
1a416433
DC
566 hookscript => {
567 optional => 1,
568 type => 'string',
569 format => 'pve-volume-id',
570 description => 'Script that will be exectued during various steps in the containers lifetime.',
571 },
733e52ec
DC
572 tags => {
573 type => 'string', format => 'pve-tag-list',
574 description => 'Tags of the Container. This is only meta information.',
575 optional => 1,
576 },
cc9967d2
TL
577 debug => {
578 optional => 1,
579 type => 'boolean',
580 description => "Try to be more verbose. For now this only enables debug log-level on start.",
581 default => 0,
582 },
1b4cf758
FG
583};
584
585my $valid_lxc_conf_keys = {
108c6cab
WB
586 'lxc.apparmor.profile' => 1,
587 'lxc.apparmor.allow_incomplete' => 1,
d494e03c
WB
588 'lxc.apparmor.allow_nesting' => 1,
589 'lxc.apparmor.raw' => 1,
108c6cab 590 'lxc.selinux.context' => 1,
1b4cf758
FG
591 'lxc.include' => 1,
592 'lxc.arch' => 1,
108c6cab
WB
593 'lxc.uts.name' => 1,
594 'lxc.signal.halt' => 1,
595 'lxc.signal.reboot' => 1,
596 'lxc.signal.stop' => 1,
597 'lxc.init.cmd' => 1,
598 'lxc.pty.max' => 1,
1b4cf758 599 'lxc.console.logfile' => 1,
108c6cab
WB
600 'lxc.console.path' => 1,
601 'lxc.tty.max' => 1,
602 'lxc.devtty.dir' => 1,
1b4cf758
FG
603 'lxc.hook.autodev' => 1,
604 'lxc.autodev' => 1,
605 'lxc.kmsg' => 1,
108c6cab 606 'lxc.mount.fstab' => 1,
1b4cf758
FG
607 'lxc.mount.entry' => 1,
608 'lxc.mount.auto' => 1,
108c6cab 609 'lxc.rootfs.path' => 'lxc.rootfs.path is auto generated from rootfs',
1b4cf758
FG
610 'lxc.rootfs.mount' => 1,
611 'lxc.rootfs.options' => 'lxc.rootfs.options is not supported' .
235dbdf3 612 ', please use mount point options in the "rootfs" key',
1b4cf758 613 # lxc.cgroup.*
108c6cab 614 # lxc.prlimit.*
6eb23d1e 615 # lxc.net.*
1b4cf758
FG
616 'lxc.cap.drop' => 1,
617 'lxc.cap.keep' => 1,
108c6cab 618 'lxc.seccomp.profile' => 1,
832b4a02
WB
619 'lxc.seccomp.notify.proxy' => 1,
620 'lxc.seccomp.notify.cookie' => 1,
108c6cab 621 'lxc.idmap' => 1,
1b4cf758
FG
622 'lxc.hook.pre-start' => 1,
623 'lxc.hook.pre-mount' => 1,
624 'lxc.hook.mount' => 1,
625 'lxc.hook.start' => 1,
626 'lxc.hook.stop' => 1,
627 'lxc.hook.post-stop' => 1,
628 'lxc.hook.clone' => 1,
629 'lxc.hook.destroy' => 1,
f71db91b 630 'lxc.hook.version' => 1,
108c6cab
WB
631 'lxc.log.level' => 1,
632 'lxc.log.file' => 1,
1b4cf758
FG
633 'lxc.start.auto' => 1,
634 'lxc.start.delay' => 1,
635 'lxc.start.order' => 1,
636 'lxc.group' => 1,
637 'lxc.environment' => 1,
d4a135f7
WB
638
639 # All these are namespaced via CLONE_NEWIPC (see namespaces(7)).
640 'lxc.sysctl.fs.mqueue' => 1,
641 'lxc.sysctl.kernel.msgmax' => 1,
642 'lxc.sysctl.kernel.msgmnb' => 1,
643 'lxc.sysctl.kernel.msgmni' => 1,
644 'lxc.sysctl.kernel.sem' => 1,
645 'lxc.sysctl.kernel.shmall' => 1,
646 'lxc.sysctl.kernel.shmmax' => 1,
647 'lxc.sysctl.kernel.shmmni' => 1,
648 'lxc.sysctl.kernel.shm_rmid_forced' => 1,
1b4cf758
FG
649};
650
108c6cab
WB
651my $deprecated_lxc_conf_keys = {
652 # Deprecated (removed with lxc 3.0):
653 'lxc.aa_profile' => 'lxc.apparmor.profile',
654 'lxc.aa_allow_incomplete' => 'lxc.apparmor.allow_incomplete',
655 'lxc.console' => 'lxc.console.path',
656 'lxc.devttydir' => 'lxc.tty.dir',
657 'lxc.haltsignal' => 'lxc.signal.halt',
658 'lxc.rebootsignal' => 'lxc.signal.reboot',
659 'lxc.stopsignal' => 'lxc.signal.stop',
660 'lxc.id_map' => 'lxc.idmap',
661 'lxc.init_cmd' => 'lxc.init.cmd',
662 'lxc.loglevel' => 'lxc.log.level',
663 'lxc.logfile' => 'lxc.log.file',
664 'lxc.mount' => 'lxc.mount.fstab',
665 'lxc.network.type' => 'lxc.net.INDEX.type',
666 'lxc.network.flags' => 'lxc.net.INDEX.flags',
667 'lxc.network.link' => 'lxc.net.INDEX.link',
668 'lxc.network.mtu' => 'lxc.net.INDEX.mtu',
669 'lxc.network.name' => 'lxc.net.INDEX.name',
670 'lxc.network.hwaddr' => 'lxc.net.INDEX.hwaddr',
671 'lxc.network.ipv4' => 'lxc.net.INDEX.ipv4.address',
672 'lxc.network.ipv4.gateway' => 'lxc.net.INDEX.ipv4.gateway',
673 'lxc.network.ipv6' => 'lxc.net.INDEX.ipv6.address',
674 'lxc.network.ipv6.gateway' => 'lxc.net.INDEX.ipv6.gateway',
675 'lxc.network.script.up' => 'lxc.net.INDEX.script.up',
676 'lxc.network.script.down' => 'lxc.net.INDEX.script.down',
677 'lxc.pts' => 'lxc.pty.max',
678 'lxc.se_context' => 'lxc.selinux.context',
679 'lxc.seccomp' => 'lxc.seccomp.profile',
680 'lxc.tty' => 'lxc.tty.max',
681 'lxc.utsname' => 'lxc.uts.name',
682};
683
684sub is_valid_lxc_conf_key {
685 my ($vmid, $key) = @_;
686 if ($key =~ /^lxc\.limit\./) {
687 warn "vm $vmid - $key: lxc.limit.* was renamed to lxc.prlimit.*\n";
688 return 1;
689 }
690 if (defined(my $new_name = $deprecated_lxc_conf_keys->{$key})) {
691 warn "vm $vmid - $key is deprecated and was renamed to $new_name\n";
692 return 1;
693 }
694 my $validity = $valid_lxc_conf_keys->{$key};
695 return $validity if defined($validity);
979ea389 696 return 1 if $key =~ /^lxc\.cgroup2?\./ # allow all cgroup values
108c6cab
WB
697 || $key =~ /^lxc\.prlimit\./ # allow all prlimits
698 || $key =~ /^lxc\.net\./; # allow custom network definitions
699 return 0;
700}
701
5e5915c5 702our $netconf_desc = {
1b4cf758
FG
703 type => {
704 type => 'string',
705 optional => 1,
706 description => "Network interface type.",
707 enum => [qw(veth)],
708 },
709 name => {
710 type => 'string',
a069f163
DM
711 format_description => 'string',
712 description => 'Name of the network device as seen from inside the container. (lxc.network.name)',
1b4cf758
FG
713 pattern => '[-_.\w\d]+',
714 },
715 bridge => {
716 type => 'string',
a069f163 717 format_description => 'bridge',
1b4cf758
FG
718 description => 'Bridge to attach the network device to.',
719 pattern => '[-_.\w\d]+',
720 optional => 1,
721 },
603536e0 722 hwaddr => get_standard_option('mac-addr', {
e6f20294 723 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)',
603536e0 724 }),
1b4cf758
FG
725 mtu => {
726 type => 'integer',
1b4cf758
FG
727 description => 'Maximum transfer unit of the interface. (lxc.network.mtu)',
728 minimum => 64, # minimum ethernet frame is 64 bytes
729 optional => 1,
730 },
731 ip => {
732 type => 'string',
733 format => 'pve-ipv4-config',
718a67d6 734 format_description => '(IPv4/CIDR|dhcp|manual)',
1b4cf758
FG
735 description => 'IPv4 address in CIDR format.',
736 optional => 1,
737 },
738 gw => {
739 type => 'string',
740 format => 'ipv4',
741 format_description => 'GatewayIPv4',
742 description => 'Default gateway for IPv4 traffic.',
743 optional => 1,
744 },
745 ip6 => {
746 type => 'string',
747 format => 'pve-ipv6-config',
6ea7095c 748 format_description => '(IPv6/CIDR|auto|dhcp|manual)',
1b4cf758
FG
749 description => 'IPv6 address in CIDR format.',
750 optional => 1,
751 },
752 gw6 => {
753 type => 'string',
754 format => 'ipv6',
755 format_description => 'GatewayIPv6',
756 description => 'Default gateway for IPv6 traffic.',
757 optional => 1,
758 },
759 firewall => {
760 type => 'boolean',
1b4cf758
FG
761 description => "Controls whether this interface's firewall rules should be used.",
762 optional => 1,
763 },
764 tag => {
765 type => 'integer',
6b202dd5
DM
766 minimum => 1,
767 maximum => 4094,
1b4cf758
FG
768 description => "VLAN tag for this interface.",
769 optional => 1,
770 },
771 trunks => {
772 type => 'string',
773 pattern => qr/\d+(?:;\d+)*/,
774 format_description => 'vlanid[;vlanid...]',
775 description => "VLAN ids to pass through the interface",
776 optional => 1,
777 },
380962c7
WB
778 rate => {
779 type => 'number',
780 format_description => 'mbps',
781 description => "Apply rate limiting to the interface",
782 optional => 1,
783 },
1b4cf758
FG
784};
785PVE::JSONSchema::register_format('pve-lxc-network', $netconf_desc);
786
6dd2d4cd 787my $MAX_LXC_NETWORKS = 32;
1b4cf758
FG
788for (my $i = 0; $i < $MAX_LXC_NETWORKS; $i++) {
789 $confdesc->{"net$i"} = {
790 optional => 1,
791 type => 'string', format => $netconf_desc,
792 description => "Specifies network interfaces for the container.",
793 };
794}
795
e6e308ae
OB
796PVE::JSONSchema::register_format('pve-ct-timezone', \&verify_ct_timezone);
797sub verify_ct_timezone {
798 my ($timezone, $noerr) = @_;
799
800 return if $timezone eq 'host'; # using host settings
801
802 PVE::JSONSchema::pve_verify_timezone($timezone);
803}
804
1b4cf758
FG
805PVE::JSONSchema::register_format('pve-lxc-mp-string', \&verify_lxc_mp_string);
806sub verify_lxc_mp_string {
807 my ($mp, $noerr) = @_;
808
809 # do not allow:
810 # /./ or /../
811 # /. or /.. at the end
812 # ../ at the beginning
813
814 if($mp =~ m@/\.\.?/@ ||
815 $mp =~ m@/\.\.?$@ ||
816 $mp =~ m@^\.\./@) {
817 return undef if $noerr;
818 die "$mp contains illegal character sequences\n";
819 }
820 return $mp;
821}
822
823my $mp_desc = {
824 %$rootfs_desc,
84820d40
DM
825 backup => {
826 type => 'boolean',
235dbdf3
FG
827 description => 'Whether to include the mount point in backups.',
828 verbose_description => 'Whether to include the mount point in backups '.
829 '(only used for volume mount points).',
84820d40
DM
830 optional => 1,
831 },
1b4cf758
FG
832 mp => {
833 type => 'string',
834 format => 'pve-lxc-mp-string',
835 format_description => 'Path',
235dbdf3 836 description => 'Path to the mount point as seen from inside the container '.
52b6f941 837 '(must not contain symlinks).',
235dbdf3 838 verbose_description => "Path to the mount point as seen from inside the container.\n\n".
52b6f941 839 "NOTE: Must not contain any symlinks for security reasons."
1b4cf758
FG
840 },
841};
842PVE::JSONSchema::register_format('pve-ct-mountpoint', $mp_desc);
843
9dabb518
FE
844my $unused_desc = {
845 volume => {
846 type => 'string',
847 default_key => 1,
848 format => 'pve-volume-id',
849 format_description => 'volume',
850 description => 'The volume that is not used currently.',
851 }
1b4cf758
FG
852};
853
854for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
855 $confdesc->{"mp$i"} = {
856 optional => 1,
857 type => 'string', format => $mp_desc,
2928e616 858 description => "Use volume as container mount point.",
1b4cf758
FG
859 optional => 1,
860 };
861}
862
9dabb518
FE
863for (my $i = 0; $i < $MAX_UNUSED_DISKS; $i++) {
864 $confdesc->{"unused$i"} = {
865 optional => 1,
866 type => 'string', format => $unused_desc,
867 description => "Reference to unused volumes. This is used internally, and should not be modified manually.",
868 }
1b4cf758
FG
869}
870
871sub parse_pct_config {
872 my ($filename, $raw) = @_;
873
874 return undef if !defined($raw);
875
876 my $res = {
877 digest => Digest::SHA::sha1_hex($raw),
878 snapshots => {},
7547dc63 879 pending => {},
1b4cf758
FG
880 };
881
882 $filename =~ m|/lxc/(\d+).conf$|
883 || die "got strange filename '$filename'";
884
885 my $vmid = $1;
886
887 my $conf = $res;
888 my $descr = '';
889 my $section = '';
890
891 my @lines = split(/\n/, $raw);
892 foreach my $line (@lines) {
893 next if $line =~ m/^\s*$/;
894
7547dc63
OB
895 if ($line =~ m/^\[pve:pending\]\s*$/i) {
896 $section = 'pending';
897 $conf->{description} = $descr if $descr;
898 $descr = '';
899 $conf = $res->{$section} = {};
900 next;
901 } elsif ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
1b4cf758
FG
902 $section = $1;
903 $conf->{description} = $descr if $descr;
904 $descr = '';
905 $conf = $res->{snapshots}->{$section} = {};
906 next;
907 }
908
909 if ($line =~ m/^\#(.*)\s*$/) {
910 $descr .= PVE::Tools::decode_text($1) . "\n";
911 next;
912 }
913
914 if ($line =~ m/^(lxc\.[a-z0-9_\-\.]+)(:|\s*=)\s*(.*?)\s*$/) {
915 my $key = $1;
916 my $value = $3;
108c6cab
WB
917 my $validity = is_valid_lxc_conf_key($vmid, $key);
918 if ($validity eq 1) {
1b4cf758
FG
919 push @{$conf->{lxc}}, [$key, $value];
920 } elsif (my $errmsg = $validity) {
921 warn "vm $vmid - $key: $errmsg\n";
922 } else {
923 warn "vm $vmid - unable to parse config: $line\n";
924 }
925 } elsif ($line =~ m/^(description):\s*(.*\S)\s*$/) {
926 $descr .= PVE::Tools::decode_text($2);
927 } elsif ($line =~ m/snapstate:\s*(prepare|delete)\s*$/) {
928 $conf->{snapstate} = $1;
7547dc63
OB
929 } elsif ($line =~ m/^delete:\s*(.*\S)\s*$/) {
930 my $value = $1;
931 if ($section eq 'pending') {
932 $conf->{delete} = $value;
933 } else {
934 warn "vm $vmid - property 'delete' is only allowed in [pve:pending]\n";
935 }
648529ba 936 } elsif ($line =~ m/^([a-z][a-z_]*\d*):\s*(.+?)\s*$/) {
1b4cf758
FG
937 my $key = $1;
938 my $value = $2;
939 eval { $value = PVE::LXC::Config->check_type($key, $value); };
940 warn "vm $vmid - unable to parse value of '$key' - $@" if $@;
941 $conf->{$key} = $value;
942 } else {
943 warn "vm $vmid - unable to parse config: $line\n";
944 }
945 }
946
947 $conf->{description} = $descr if $descr;
948
949 delete $res->{snapstate}; # just to be sure
950
951 return $res;
952}
953
954sub write_pct_config {
955 my ($filename, $conf) = @_;
956
957 delete $conf->{snapstate}; # just to be sure
958
959 my $volidlist = PVE::LXC::Config->get_vm_volumes($conf);
960 my $used_volids = {};
961 foreach my $vid (@$volidlist) {
962 $used_volids->{$vid} = 1;
963 }
964
965 # remove 'unusedX' settings if the volume is still used
966 foreach my $key (keys %$conf) {
967 my $value = $conf->{$key};
968 if ($key =~ m/^unused/ && $used_volids->{$value}) {
969 delete $conf->{$key};
970 }
971 }
972
973 my $generate_raw_config = sub {
974 my ($conf) = @_;
975
976 my $raw = '';
977
978 # add description as comment to top of file
979 my $descr = $conf->{description} || '';
980 foreach my $cl (split(/\n/, $descr)) {
7547dc63 981 $raw .= '#' . PVE::Tools::encode_text($cl) . "\n";
1b4cf758
FG
982 }
983
984 foreach my $key (sort keys %$conf) {
985 next if $key eq 'digest' || $key eq 'description' ||
986 $key eq 'pending' || $key eq 'snapshots' ||
987 $key eq 'snapname' || $key eq 'lxc';
988 my $value = $conf->{$key};
989 die "detected invalid newline inside property '$key'\n"
990 if $value =~ m/\n/;
991 $raw .= "$key: $value\n";
992 }
993
994 if (my $lxcconf = $conf->{lxc}) {
995 foreach my $entry (@$lxcconf) {
996 my ($k, $v) = @$entry;
997 $raw .= "$k: $v\n";
998 }
999 }
1000
1001 return $raw;
1002 };
1003
1004 my $raw = &$generate_raw_config($conf);
1005
7547dc63
OB
1006 if (scalar(keys %{$conf->{pending}})){
1007 $raw .= "\n[pve:pending]\n";
1008 $raw .= &$generate_raw_config($conf->{pending});
1009 }
1010
1b4cf758
FG
1011 foreach my $snapname (sort keys %{$conf->{snapshots}}) {
1012 $raw .= "\n[$snapname]\n";
1013 $raw .= &$generate_raw_config($conf->{snapshots}->{$snapname});
1014 }
1015
1016 return $raw;
1017}
1018
1019sub update_pct_config {
6517e001 1020 my ($class, $vmid, $conf, $running, $param, $delete, $revert) = @_;
1b4cf758 1021
6517e001 1022 my $storage_cfg = PVE::Storage::config();
1b4cf758 1023
6517e001
OB
1024 foreach my $opt (@$revert) {
1025 delete $conf->{pending}->{$opt};
1026 $class->remove_from_pending_delete($conf, $opt); # also remove from deletion queue
1b4cf758
FG
1027 }
1028
6517e001
OB
1029 # write updates to pending section
1030 my $modified = {}; # record modified options
1b4cf758 1031
6517e001
OB
1032 foreach my $opt (@$delete) {
1033 if (!defined($conf->{$opt}) && !defined($conf->{pending}->{$opt})) {
1034 warn "cannot delete '$opt' - not set in current configuration!\n";
1035 next;
1b4cf758 1036 }
6517e001
OB
1037 $modified->{$opt} = 1;
1038 if ($opt eq 'memory' || $opt eq 'rootfs' || $opt eq 'ostype') {
1039 die "unable to delete required option '$opt'\n";
1040 } elsif ($opt =~ m/^unused(\d+)$/) {
1041 $class->check_protection($conf, "can't remove CT $vmid drive '$opt'");
1042 } elsif ($opt =~ m/^mp(\d+)$/) {
1043 $class->check_protection($conf, "can't remove CT $vmid drive '$opt'");
1044 } elsif ($opt eq 'unprivileged') {
1045 die "unable to delete read-only option: '$opt'\n";
1b4cf758 1046 }
6517e001 1047 $class->add_to_pending_delete($conf, $opt);
1b4cf758
FG
1048 }
1049
1b3213ae
FG
1050 my $check_content_type = sub {
1051 my ($mp) = @_;
1052 my $sid = PVE::Storage::parse_volume_id($mp->{volume});
6517e001 1053 my $storage_config = PVE::Storage::storage_config($storage_cfg, $sid);
1b3213ae
FG
1054 die "storage '$sid' does not allow content type 'rootdir' (Container)\n"
1055 if !$storage_config->{content}->{rootdir};
1056 };
1b4cf758 1057
c10951f7 1058 foreach my $opt (sort keys %$param) { # add/change
6517e001 1059 $modified->{$opt} = 1;
1b4cf758 1060 my $value = $param->{$opt};
6517e001
OB
1061 if ($opt =~ m/^mp(\d+)$/ || $opt eq 'rootfs') {
1062 $class->check_protection($conf, "can't update CT $vmid drive '$opt'");
e4034859 1063 my $mp = $class->parse_volume($opt, $value);
3927ae96 1064 $check_content_type->($mp) if ($mp->{type} eq 'volume');
6517e001
OB
1065 } elsif ($opt eq 'hookscript') {
1066 PVE::GuestHelpers::check_hookscript($value);
1b4cf758 1067 } elsif ($opt eq 'nameserver') {
6517e001 1068 $value = PVE::LXC::verify_nameserver_list($value);
1b4cf758 1069 } elsif ($opt eq 'searchdomain') {
6517e001 1070 $value = PVE::LXC::verify_searchdomain_list($value);
1b4cf758
FG
1071 } elsif ($opt eq 'unprivileged') {
1072 die "unable to modify read-only option: '$opt'\n";
1b4cf758 1073 }
6517e001
OB
1074 $conf->{pending}->{$opt} = $value;
1075 $class->remove_from_pending_delete($conf, $opt);
f8aa3d35
WL
1076 }
1077
6517e001 1078 my $changes = $class->cleanup_pending($conf);
1b4cf758 1079
6517e001
OB
1080 my $errors = {};
1081 if ($running) {
1082 $class->vmconfig_hotplug_pending($vmid, $conf, $storage_cfg, $modified, $errors);
1083 } else {
1084 $class->vmconfig_apply_pending($vmid, $conf, $storage_cfg, $modified, $errors);
1b4cf758
FG
1085 }
1086
6517e001 1087 return $errors;
1b4cf758
FG
1088}
1089
1090sub check_type {
1091 my ($class, $key, $value) = @_;
1092
1093 die "unknown setting '$key'\n" if !$confdesc->{$key};
1094
1095 my $type = $confdesc->{$key}->{type};
1096
1097 if (!defined($value)) {
1098 die "got undefined value\n";
1099 }
1100
1101 if ($value =~ m/[\n\r]/) {
1102 die "property contains a line feed\n";
1103 }
1104
1105 if ($type eq 'boolean') {
1106 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
1107 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
1108 die "type check ('boolean') failed - got '$value'\n";
1109 } elsif ($type eq 'integer') {
1110 return int($1) if $value =~ m/^(\d+)$/;
1111 die "type check ('integer') failed - got '$value'\n";
1112 } elsif ($type eq 'number') {
1113 return $value if $value =~ m/^(\d+)(\.\d+)?$/;
1114 die "type check ('number') failed - got '$value'\n";
1115 } elsif ($type eq 'string') {
1116 if (my $fmt = $confdesc->{$key}->{format}) {
1117 PVE::JSONSchema::check_format($fmt, $value);
1118 return $value;
1119 }
1120 return $value;
1121 } else {
1122 die "internal error"
1123 }
1124}
1125
1126
1127# add JSON properties for create and set function
1128sub json_config_properties {
1129 my ($class, $prop) = @_;
1130
1131 foreach my $opt (keys %$confdesc) {
1132 next if $opt eq 'parent' || $opt eq 'snaptime';
1133 next if $prop->{$opt};
1134 $prop->{$opt} = $confdesc->{$opt};
1135 }
1136
1137 return $prop;
1138}
1139
c0a17956 1140my $parse_ct_mountpoint_full = sub {
1b4cf758
FG
1141 my ($class, $desc, $data, $noerr) = @_;
1142
1143 $data //= '';
1144
1145 my $res;
1146 eval { $res = PVE::JSONSchema::parse_property_string($desc, $data) };
1147 if ($@) {
1148 return undef if $noerr;
1149 die $@;
1150 }
1151
1152 if (defined(my $size = $res->{size})) {
1153 $size = PVE::JSONSchema::parse_size($size);
1154 if (!defined($size)) {
1155 return undef if $noerr;
1156 die "invalid size: $size\n";
1157 }
1158 $res->{size} = $size;
1159 }
1160
1161 $res->{type} = $class->classify_mountpoint($res->{volume});
1162
1163 return $res;
1164};
1165
1b4cf758
FG
1166sub print_ct_mountpoint {
1167 my ($class, $info, $nomp) = @_;
1168 my $skip = [ 'type' ];
1169 push @$skip, 'mp' if $nomp;
1170 return PVE::JSONSchema::print_property_string($info, $mp_desc, $skip);
1171}
1172
5e5d76cf
FE
1173sub parse_volume {
1174 my ($class, $key, $volume_string, $noerr) = @_;
1175
1176 if ($key eq 'rootfs') {
c0a17956 1177 my $res = $parse_ct_mountpoint_full->($class, $rootfs_desc, $volume_string, $noerr);
e4034859
FE
1178 $res->{mp} = '/' if defined($res);
1179 return $res;
9dabb518 1180 } elsif ($key =~ m/^mp\d+$/) {
c0a17956 1181 return $parse_ct_mountpoint_full->($class, $mp_desc, $volume_string, $noerr);
9dabb518
FE
1182 } elsif ($key =~ m/^unused\d+$/) {
1183 return $parse_ct_mountpoint_full->($class, $unused_desc, $volume_string, $noerr);
5e5d76cf
FE
1184 }
1185
1186 die "parse_volume - unknown type: $key\n";
1187}
1188
1189sub print_volume {
1190 my ($class, $key, $volume) = @_;
1191
1192 return $class->print_ct_mountpoint($volume, $key eq 'rootfs');
1193}
1194
1195sub volid_key {
1196 my ($class) = @_;
1197
1198 return 'volume';
1199}
1200
1b4cf758
FG
1201sub print_lxc_network {
1202 my ($class, $net) = @_;
1203 return PVE::JSONSchema::print_property_string($net, $netconf_desc);
1204}
1205
1206sub parse_lxc_network {
1207 my ($class, $data) = @_;
1208
1209 my $res = {};
1210
1211 return $res if !$data;
1212
1213 $res = PVE::JSONSchema::parse_property_string($netconf_desc, $data);
1214
1215 $res->{type} = 'veth';
2f19133b
WB
1216 if (!$res->{hwaddr}) {
1217 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
1218 $res->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
1219 }
1b4cf758
FG
1220
1221 return $res;
1222}
1223
5a63f1c5
WB
1224sub parse_features {
1225 my ($class, $data) = @_;
1226 return {} if !$data;
1227 return PVE::JSONSchema::parse_property_string($features_desc, $data);
1228}
1229
1b4cf758
FG
1230sub option_exists {
1231 my ($class, $name) = @_;
1232
1233 return defined($confdesc->{$name});
1234}
1235# END JSON config code
1236
32e15a2b
OB
1237my $LXC_FASTPLUG_OPTIONS= {
1238 'description' => 1,
1239 'onboot' => 1,
1240 'startup' => 1,
1241 'protection' => 1,
1242 'hostname' => 1,
1243 'hookscript' => 1,
1244 'cores' => 1,
1245 'tags' => 1,
db15c375 1246 'lock' => 1,
32e15a2b
OB
1247};
1248
1249sub vmconfig_hotplug_pending {
1250 my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_;
1251
1252 my $pid = PVE::LXC::find_lxc_pid($vmid);
1253 my $rootdir = "/proc/$pid/root";
1254
1255 my $add_hotplug_error = sub {
1256 my ($opt, $msg) = @_;
1257 $errors->{$opt} = "unable to hotplug $opt: $msg";
1258 };
1259
c10951f7 1260 foreach my $opt (sort keys %{$conf->{pending}}) { # add/change
32e15a2b
OB
1261 next if $selection && !$selection->{$opt};
1262 if ($LXC_FASTPLUG_OPTIONS->{$opt}) {
1263 $conf->{$opt} = delete $conf->{pending}->{$opt};
32e15a2b
OB
1264 }
1265 }
1266
2a4fddef
WB
1267 my $cgroup = PVE::LXC::CGroup->new($vmid);
1268
32e15a2b
OB
1269 # There's no separate swap size to configure, there's memory and "total"
1270 # memory (iow. memory+swap). This means we have to change them together.
1271 my $hotplug_memory_done;
1272 my $hotplug_memory = sub {
1273 my ($wanted_memory, $wanted_swap) = @_;
2a4fddef
WB
1274
1275 $wanted_memory = int($wanted_memory * 1024 * 1024) if defined($wanted_memory);
1276 $wanted_swap = int($wanted_swap * 1024 * 1024) if defined($wanted_swap);
1277 $cgroup->change_memory_limit($wanted_memory, $wanted_swap);
1278
32e15a2b
OB
1279 $hotplug_memory_done = 1;
1280 };
1281
1282 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
1283 # FIXME: $force deletion is not implemented for CTs
8ec10817 1284 foreach my $opt (sort keys %$pending_delete_hash) {
32e15a2b
OB
1285 next if $selection && !$selection->{$opt};
1286 eval {
1287 if ($LXC_FASTPLUG_OPTIONS->{$opt}) {
1288 # pass
1289 } elsif ($opt =~ m/^unused(\d+)$/) {
1290 PVE::LXC::delete_mountpoint_volume($storecfg, $vmid, $conf->{$opt})
1291 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1292 } elsif ($opt eq 'swap') {
1293 $hotplug_memory->(undef, 0);
1294 } elsif ($opt eq 'cpulimit') {
6e8ce610 1295 $cgroup->change_cpu_quota(-1, 100000);
32e15a2b 1296 } elsif ($opt eq 'cpuunits') {
6e8ce610 1297 $cgroup->change_cpu_shares(undef, $confdesc->{cpuunits}->{default});
32e15a2b
OB
1298 } elsif ($opt =~ m/^net(\d)$/) {
1299 my $netid = $1;
1300 PVE::Network::veth_delete("veth${vmid}i$netid");
1301 } else {
1302 die "skip\n"; # skip non-hotpluggable opts
1303 }
1304 };
1305 if (my $err = $@) {
1306 $add_hotplug_error->($opt, $err) if $err ne "skip\n";
1307 } else {
1308 delete $conf->{$opt};
1309 $class->remove_from_pending_delete($conf, $opt);
1310 }
1311 }
1312
c10951f7 1313 foreach my $opt (sort keys %{$conf->{pending}}) {
32e15a2b
OB
1314 next if $opt eq 'delete'; # just to be sure
1315 next if $selection && !$selection->{$opt};
1316 my $value = $conf->{pending}->{$opt};
1317 eval {
1318 if ($opt eq 'cpulimit') {
6e8ce610
WB
1319 my $quota = 100000 * $value;
1320 $cgroup->change_cpu_quota(int(100000 * $value), 100000);
32e15a2b 1321 } elsif ($opt eq 'cpuunits') {
6e8ce610 1322 $cgroup->change_cpu_shares($value, $confdesc->{cpuunits}->{default});
32e15a2b
OB
1323 } elsif ($opt =~ m/^net(\d+)$/) {
1324 my $netid = $1;
1325 my $net = $class->parse_lxc_network($value);
7eff309b 1326 $value = $class->print_lxc_network($net);
32e15a2b
OB
1327 PVE::LXC::update_net($vmid, $conf, $opt, $net, $netid, $rootdir);
1328 } elsif ($opt eq 'memory' || $opt eq 'swap') {
1329 if (!$hotplug_memory_done) { # don't call twice if both opts are passed
1330 $hotplug_memory->($conf->{pending}->{memory}, $conf->{pending}->{swap});
1331 }
b2de4c04
WB
1332 } elsif ($opt =~ m/^mp(\d+)$/) {
1333 if (!PVE::LXC::Tools::can_use_new_mount_api()) {
1334 die "skip\n";
1335 }
1336
c7ce07e0
OB
1337 if (exists($conf->{$opt})) {
1338 die "skip\n"; # don't try to hotplug over existing mp
1339 }
1340
b2de4c04
WB
1341 $class->apply_pending_mountpoint($vmid, $conf, $opt, $storecfg, 1);
1342 # apply_pending_mountpoint modifies the value if it creates a new disk
1343 $value = $conf->{pending}->{$opt};
32e15a2b
OB
1344 } else {
1345 die "skip\n"; # skip non-hotpluggable
1346 }
1347 };
1348 if (my $err = $@) {
1349 $add_hotplug_error->($opt, $err) if $err ne "skip\n";
1350 } else {
1351 $conf->{$opt} = $value;
1352 delete $conf->{pending}->{$opt};
1353 }
1354 }
32e15a2b
OB
1355}
1356
1357sub vmconfig_apply_pending {
1358 my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_;
1359
1360 my $add_apply_error = sub {
1361 my ($opt, $msg) = @_;
1362 my $err_msg = "unable to apply pending change $opt : $msg";
1363 $errors->{$opt} = $err_msg;
1364 warn $err_msg;
1365 };
1366
32e15a2b
OB
1367 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
1368 # FIXME: $force deletion is not implemented for CTs
8ec10817 1369 foreach my $opt (sort keys %$pending_delete_hash) {
32e15a2b 1370 next if $selection && !$selection->{$opt};
32e15a2b
OB
1371 eval {
1372 if ($opt =~ m/^mp(\d+)$/) {
e4034859 1373 my $mp = $class->parse_volume($opt, $conf->{$opt});
32e15a2b
OB
1374 if ($mp->{type} eq 'volume') {
1375 $class->add_unused_volume($conf, $mp->{volume})
1376 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1377 }
1378 } elsif ($opt =~ m/^unused(\d+)$/) {
1379 PVE::LXC::delete_mountpoint_volume($storecfg, $vmid, $conf->{$opt})
1380 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1381 }
1382 };
1383 if (my $err = $@) {
1384 $add_apply_error->($opt, $err);
1385 } else {
1386 delete $conf->{$opt};
1387 $class->remove_from_pending_delete($conf, $opt);
1388 }
1389 }
1390
132c0a90
OB
1391 $class->cleanup_pending($conf);
1392
c10951f7 1393 foreach my $opt (sort keys %{$conf->{pending}}) { # add/change
32e15a2b
OB
1394 next if $opt eq 'delete'; # just to be sure
1395 next if $selection && !$selection->{$opt};
1396 eval {
1397 if ($opt =~ m/^mp(\d+)$/) {
869081a2 1398 $class->apply_pending_mountpoint($vmid, $conf, $opt, $storecfg, 0);
7eff309b
OB
1399 } elsif ($opt =~ m/^net(\d+)$/) {
1400 my $netid = $1;
1401 my $net = $class->parse_lxc_network($conf->{pending}->{$opt});
1402 $conf->{pending}->{$opt} = $class->print_lxc_network($net);
32e15a2b
OB
1403 }
1404 };
1405 if (my $err = $@) {
1406 $add_apply_error->($opt, $err);
1407 } else {
32e15a2b
OB
1408 $conf->{$opt} = delete $conf->{pending}->{$opt};
1409 }
1410 }
32e15a2b
OB
1411}
1412
869081a2
WB
1413my $rescan_volume = sub {
1414 my ($storecfg, $mp) = @_;
1415 eval {
0c69dcfc 1416 $mp->{size} = PVE::Storage::volume_size_info($storecfg, $mp->{volume}, 5);
869081a2
WB
1417 };
1418 warn "Could not rescan volume size - $@\n" if $@;
1419};
1420
1421sub apply_pending_mountpoint {
1422 my ($class, $vmid, $conf, $opt, $storecfg, $running) = @_;
1423
e4034859 1424 my $mp = $class->parse_volume($opt, $conf->{pending}->{$opt});
869081a2
WB
1425 my $old = $conf->{$opt};
1426 if ($mp->{type} eq 'volume') {
1427 if ($mp->{volume} =~ $PVE::LXC::NEW_DISK_RE) {
b2de4c04 1428 my $original_value = $conf->{pending}->{$opt};
869081a2
WB
1429 my $vollist = PVE::LXC::create_disks(
1430 $storecfg,
1431 $vmid,
b2de4c04 1432 { $opt => $original_value },
869081a2
WB
1433 $conf,
1434 1,
1435 );
b2de4c04
WB
1436 if ($running) {
1437 # Re-parse mount point:
e4034859 1438 my $mp = $class->parse_volume($opt, $conf->{pending}->{$opt});
b2de4c04
WB
1439 eval {
1440 PVE::LXC::mountpoint_hotplug($vmid, $conf, $opt, $mp, $storecfg);
1441 };
1442 my $err = $@;
1443 if ($err) {
1444 PVE::LXC::destroy_disks($storecfg, $vollist);
1445 # The pending-changes code collects errors but keeps on looping through further
1446 # pending changes, so unroll the change in $conf as well if destroy_disks()
1447 # didn't die().
1448 $conf->{pending}->{$opt} = $original_value;
1449 die $err;
1450 }
1451 }
869081a2 1452 } else {
b2de4c04 1453 die "skip\n" if $running && defined($old); # TODO: "changing" mount points?
869081a2 1454 $rescan_volume->($storecfg, $mp);
b2de4c04
WB
1455 if ($running) {
1456 PVE::LXC::mountpoint_hotplug($vmid, $conf, $opt, $mp, $storecfg);
1457 }
869081a2
WB
1458 $conf->{pending}->{$opt} = $class->print_ct_mountpoint($mp);
1459 }
1460 }
1461
1462 if (defined($old)) {
e4034859 1463 my $mp = $class->parse_volume($opt, $old);
869081a2
WB
1464 if ($mp->{type} eq 'volume') {
1465 $class->add_unused_volume($conf, $mp->{volume})
1466 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1467 }
1468 }
1469}
1470
d250604f
FG
1471sub classify_mountpoint {
1472 my ($class, $vol) = @_;
1473 if ($vol =~ m!^/!) {
1474 return 'device' if $vol =~ m!^/dev/!;
1475 return 'bind';
1476 }
1477 return 'volume';
1478}
1479
7b4237c5 1480my $__is_volume_in_use = sub {
72e6bc20 1481 my ($class, $config, $volid) = @_;
d250604f
FG
1482 my $used = 0;
1483
015740e6 1484 $class->foreach_volume($config, sub {
d250604f
FG
1485 my ($ms, $mountpoint) = @_;
1486 return if $used;
1487 $used = $mountpoint->{type} eq 'volume' && $mountpoint->{volume} eq $volid;
1488 });
1489
72e6bc20
WB
1490 return $used;
1491};
1492
1493sub is_volume_in_use_by_snapshots {
1494 my ($class, $config, $volid) = @_;
1495
1496 if (my $snapshots = $config->{snapshots}) {
d250604f 1497 foreach my $snap (keys %$snapshots) {
7b4237c5 1498 return 1 if $__is_volume_in_use->($class, $snapshots->{$snap}, $volid);
d250604f
FG
1499 }
1500 }
1501
72e6bc20 1502 return 0;
7b4237c5 1503}
72e6bc20
WB
1504
1505sub is_volume_in_use {
d063af00 1506 my ($class, $config, $volid, $include_snapshots, $include_pending) = @_;
7b4237c5 1507 return 1 if $__is_volume_in_use->($class, $config, $volid);
72e6bc20 1508 return 1 if $include_snapshots && $class->is_volume_in_use_by_snapshots($config, $volid);
d063af00 1509 return 1 if $include_pending && $__is_volume_in_use->($class, $config->{pending}, $volid);
72e6bc20 1510 return 0;
d250604f
FG
1511}
1512
1513sub has_dev_console {
1514 my ($class, $conf) = @_;
1515
1516 return !(defined($conf->{console}) && !$conf->{console});
1517}
1518
be7942f0
DM
1519sub has_lxc_entry {
1520 my ($class, $conf, $keyname) = @_;
1521
1522 if (my $lxcconf = $conf->{lxc}) {
1523 foreach my $entry (@$lxcconf) {
1524 my ($key, undef) = @$entry;
1525 return 1 if $key eq $keyname;
1526 }
1527 }
1528
1529 return 0;
1530}
1531
1b4cf758
FG
1532sub get_tty_count {
1533 my ($class, $conf) = @_;
1534
1535 return $conf->{tty} // $confdesc->{tty}->{default};
1536}
1537
1538sub get_cmode {
1539 my ($class, $conf) = @_;
1540
1541 return $conf->{cmode} // $confdesc->{cmode}->{default};
1542}
1543
5e5d76cf 1544sub valid_volume_keys {
d250604f
FG
1545 my ($class, $reverse) = @_;
1546
1547 my @names = ('rootfs');
1548
1549 for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
1550 push @names, "mp$i";
1551 }
1552
1553 return $reverse ? reverse @names : @names;
1554}
1555
d250604f
FG
1556sub get_vm_volumes {
1557 my ($class, $conf, $excludes) = @_;
1558
1559 my $vollist = [];
1560
015740e6 1561 $class->foreach_volume($conf, sub {
d250604f
FG
1562 my ($ms, $mountpoint) = @_;
1563
1564 return if $excludes && $ms eq $excludes;
1565
1566 my $volid = $mountpoint->{volume};
1567 return if !$volid || $mountpoint->{type} ne 'volume';
1568
1569 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
1570 return if !$sid;
1571
1572 push @$vollist, $volid;
1573 });
1574
1575 return $vollist;
1576}
1577
f78c87a8 1578sub get_replicatable_volumes {
70996986 1579 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
f78c87a8
DM
1580
1581 my $volhash = {};
1582
1583 my $test_volid = sub {
1584 my ($volid, $mountpoint) = @_;
1585
1586 return if !$volid;
1587
5cf90b0c 1588 my $mptype = $mountpoint->{type};
896cd762 1589 my $replicate = $mountpoint->{replicate} // 1;
d2a046b7
DC
1590
1591 if ($mptype ne 'volume') {
1592 # skip bindmounts if replicate = 0 even for cleanup,
1593 # since bind mounts could not have been replicated ever
1594 return if !$replicate;
1595 die "unable to replicate mountpoint type '$mptype'\n";
1596 }
5cf90b0c
DM
1597
1598 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, $noerr);
1599 return if !$storeid;
1600
af21e699 1601 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
5cf90b0c
DM
1602 return if $scfg->{shared};
1603
1604 my ($path, $owner, $vtype) = PVE::Storage::path($storecfg, $volid);
1605 return if !$owner || ($owner != $vmid);
1606
1607 die "unable to replicate volume '$volid', type '$vtype'\n" if $vtype ne 'images';
1608
d2a046b7 1609 return if !$cleanup && !$replicate;
f78c87a8
DM
1610
1611 if (!PVE::Storage::volume_has_feature($storecfg, 'replicate', $volid)) {
e65dce6b 1612 return if $cleanup || $noerr;
f78c87a8
DM
1613 die "missing replicate feature on volume '$volid'\n";
1614 }
1615
1616 $volhash->{$volid} = 1;
1617 };
1618
015740e6 1619 $class->foreach_volume($conf, sub {
f78c87a8
DM
1620 my ($ms, $mountpoint) = @_;
1621 $test_volid->($mountpoint->{volume}, $mountpoint);
1622 });
1623
1624 foreach my $snapname (keys %{$conf->{snapshots}}) {
1625 my $snap = $conf->{snapshots}->{$snapname};
015740e6 1626 $class->foreach_volume($snap, sub {
f78c87a8
DM
1627 my ($ms, $mountpoint) = @_;
1628 $test_volid->($mountpoint->{volume}, $mountpoint);
1629 });
1630 }
1631
b03664ae
DM
1632 # add 'unusedX' volumes to volhash
1633 foreach my $key (keys %$conf) {
1634 if ($key =~ m/^unused/) {
1635 $test_volid->($conf->{$key}, { type => 'volume', replicate => 1 });
1636 }
1637 }
1638
f78c87a8
DM
1639 return $volhash;
1640}
1641
efd1706d
AL
1642sub get_backup_volumes {
1643 my ($class, $conf) = @_;
1644
1645 my $return_volumes = [];
1646
1647 my $test_mountpoint = sub {
1648 my ($key, $volume) = @_;
1649
1650 my ($included, $reason) = $class->mountpoint_backup_enabled($key, $volume);
1651
efd1706d
AL
1652 push @$return_volumes, {
1653 key => $key,
1654 included => $included,
1655 reason => $reason,
1656 volume_config => $volume,
1657 };
1658 };
1659
1660 PVE::LXC::Config->foreach_volume($conf, $test_mountpoint);
1661
1662 return $return_volumes;
1663}
1664
f78c87a8 16651;