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