1 package PVE
::LXC
::Config
;
5 use Fcntl
qw(O_RDONLY);
7 use PVE
::AbstractConfig
;
8 use PVE
::Cluster
qw(cfs_register_file);
9 use PVE
::DataCenterConfig
;
10 use PVE
::GuestHelpers
;
12 use PVE
::JSONSchema
qw(get_standard_option);
15 use base
qw(PVE::AbstractConfig);
17 use constant {FIFREEZE
=> 0xc0045877,
18 FITHAW
=> 0xc0045878};
20 my $nodename = PVE
::INotify
::nodename
();
21 my $lock_handles = {};
22 my $lockdir = "/run/lock/lxc";
24 mkdir "/etc/pve/nodes/$nodename/lxc";
25 my $MAX_MOUNT_POINTS = 256;
26 my $MAX_UNUSED_DISKS = $MAX_MOUNT_POINTS;
28 # BEGIN implemented abstract methods from PVE::AbstractConfig
34 sub __config_max_unused_disks
{
37 return $MAX_UNUSED_DISKS;
40 sub config_file_lock
{
41 my ($class, $vmid) = @_;
43 return "$lockdir/pve-config-${vmid}.lock";
47 my ($class, $vmid, $node) = @_;
49 $node = $nodename if !$node;
50 return "nodes/$node/lxc/$vmid.conf";
53 sub mountpoint_backup_enabled
{
54 my ($class, $mp_key, $mountpoint) = @_;
59 if ($mp_key eq 'rootfs') {
62 } elsif ($mountpoint->{type
} ne 'volume') {
64 $reason = 'not a volume';
65 } elsif ($mountpoint->{backup
}) {
72 return wantarray ?
($enabled, $reason) : $enabled;
76 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
80 if ($feature eq 'copy' || $feature eq 'clone') {
81 $opts = {'valid_target_formats' => ['raw', 'subvol']};
84 $class->foreach_volume($conf, sub {
85 my ($ms, $mountpoint) = @_;
87 return if $err; # skip further test
88 return if $backup_only && !$class->mountpoint_backup_enabled($ms, $mountpoint);
91 if !PVE
::Storage
::volume_has_feature
($storecfg, $feature,
92 $mountpoint->{volume
},
93 $snapname, $running, $opts);
99 sub __snapshot_save_vmstate
{
100 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
101 die "implement me - snapshot_save_vmstate\n";
104 sub __snapshot_check_running
{
105 my ($class, $vmid) = @_;
106 return PVE
::LXC
::check_running
($vmid);
109 sub __snapshot_check_freeze_needed
{
110 my ($class, $vmid, $config, $save_vmstate) = @_;
112 my $ret = $class->__snapshot_check_running($vmid);
116 # implements similar functionality to fsfreeze(8)
117 sub fsfreeze_mountpoint
{
118 my ($path, $thaw) = @_;
120 my $op = $thaw ?
'thaw' : 'freeze';
121 my $ioctl = $thaw ? FITHAW
: FIFREEZE
;
123 sysopen my $fd, $path, O_RDONLY
or die "failed to open $path: $!\n";
125 if (!ioctl($fd, $ioctl, 0)) {
129 die "fs$op '$path' failed - $ioctl_err\n" if defined $ioctl_err;
132 sub __snapshot_freeze
{
133 my ($class, $vmid, $unfreeze) = @_;
135 my $conf = $class->load_config($vmid);
136 my $storagecfg = PVE
::Storage
::config
();
139 $class->foreach_volume($conf, sub {
140 my ($ms, $mountpoint) = @_;
142 return if $mountpoint->{type
} ne 'volume';
144 if (PVE
::Storage
::volume_snapshot_needs_fsfreeze
($storagecfg, $mountpoint->{volume
})) {
145 push @$freeze_mps, $mountpoint->{mp
};
149 my $freeze_mountpoints = sub {
152 return if scalar(@$freeze_mps) == 0;
154 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
156 for my $mp (@$freeze_mps) {
157 eval{ fsfreeze_mountpoint
("/proc/${pid}/root/${mp}", $thaw); };
163 eval { PVE
::LXC
::thaw
($vmid); };
165 $freeze_mountpoints->(1);
167 PVE
::LXC
::freeze
($vmid);
168 PVE
::LXC
::sync_container_namespace
($vmid);
169 $freeze_mountpoints->(0);
173 sub __snapshot_create_vol_snapshot
{
174 my ($class, $vmid, $ms, $mountpoint, $snapname) = @_;
176 my $storecfg = PVE
::Storage
::config
();
178 return if $snapname eq 'vzdump' &&
179 !$class->mountpoint_backup_enabled($ms, $mountpoint);
181 PVE
::Storage
::volume_snapshot
($storecfg, $mountpoint->{volume
}, $snapname);
184 sub __snapshot_delete_remove_drive
{
185 my ($class, $snap, $remove_drive) = @_;
187 if ($remove_drive eq 'vmstate') {
188 die "implement me - saving vmstate\n";
190 my $value = $snap->{$remove_drive};
191 my $mountpoint = $class->parse_volume($remove_drive, $value, 1);
192 delete $snap->{$remove_drive};
194 $class->add_unused_volume($snap, $mountpoint->{volume
})
195 if ($mountpoint->{type
} eq 'volume');
199 sub __snapshot_delete_vmstate_file
{
200 my ($class, $snap, $force) = @_;
202 die "implement me - saving vmstate\n";
205 sub __snapshot_delete_vol_snapshot
{
206 my ($class, $vmid, $ms, $mountpoint, $snapname, $unused) = @_;
208 return if $snapname eq 'vzdump' &&
209 !$class->mountpoint_backup_enabled($ms, $mountpoint);
211 my $storecfg = PVE
::Storage
::config
();
212 PVE
::Storage
::volume_snapshot_delete
($storecfg, $mountpoint->{volume
}, $snapname);
213 push @$unused, $mountpoint->{volume
};
216 sub __snapshot_rollback_vol_possible
{
217 my ($class, $mountpoint, $snapname) = @_;
219 my $storecfg = PVE
::Storage
::config
();
220 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $mountpoint->{volume
}, $snapname);
223 sub __snapshot_rollback_vol_rollback
{
224 my ($class, $mountpoint, $snapname) = @_;
226 my $storecfg = PVE
::Storage
::config
();
227 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $mountpoint->{volume
}, $snapname);
230 sub __snapshot_rollback_vm_stop
{
231 my ($class, $vmid) = @_;
233 PVE
::LXC
::vm_stop
($vmid, 1)
234 if $class->__snapshot_check_running($vmid);
237 sub __snapshot_rollback_vm_start
{
238 my ($class, $vmid, $vmstate, $data);
240 die "implement me - save vmstate\n";
243 sub __snapshot_rollback_get_unused
{
244 my ($class, $conf, $snap) = @_;
248 $class->foreach_volume($conf, sub {
249 my ($vs, $volume) = @_;
251 return if $volume->{type
} ne 'volume';
254 my $volid = $volume->{volume
};
256 $class->foreach_volume($snap, sub {
257 my ($ms, $mountpoint) = @_;
260 return if ($mountpoint->{type
} ne 'volume');
263 if ($mountpoint->{volume
} && $mountpoint->{volume
} eq $volid);
266 push @$unused, $volid if !$found;
272 # END implemented abstract methods from PVE::AbstractConfig
274 # BEGIN JSON config code
276 cfs_register_file
('/lxc/', \
&parse_pct_config
, \
&write_pct_config
);
279 my $valid_mount_option_re = qr/(noatime|nodev|nosuid|noexec)/;
281 sub is_valid_mount_option
{
283 return $option =~ $valid_mount_option_re;
290 format
=> 'pve-lxc-mp-string',
291 format_description
=> 'volume',
292 description
=> 'Volume, device or directory to mount into the container.',
296 format
=> 'disk-size',
297 format_description
=> 'DiskSize',
298 description
=> 'Volume size (read only value).',
303 description
=> 'Explicitly enable or disable ACL support.',
309 description
=> 'Extra mount options for rootfs/mps.',
310 format_description
=> 'opt[;opt...]',
311 pattern
=> qr/$valid_mount_option_re(;$valid_mount_option_re)*/,
315 description
=> 'Read-only mount point',
320 description
=> 'Enable user quotas inside the container (not supported with zfs subvolumes)',
325 description
=> 'Will include this volume to a storage replica job.',
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!",
338 PVE
::JSONSchema
::register_standard_option
('pve-ct-rootfs', {
339 type
=> 'string', format
=> $rootfs_desc,
340 description
=> "Use volume as container root.",
344 # IP address with optional interface suffix for link local ipv6 addresses
345 PVE
::JSONSchema
::register_format
('lxc-ip-with-ll-iface', \
&verify_ip_with_ll_iface
);
346 sub verify_ip_with_ll_iface
{
347 my ($addr, $noerr) = @_;
349 if (my ($addr, $iface) = ($addr =~ /^(fe80:[^%]+)%(.*)$/)) {
350 if (PVE
::JSONSchema
::pve_verify_ip
($addr, 1)
351 && PVE
::JSONSchema
::pve_verify_iface
($iface, 1))
357 return PVE
::JSONSchema
::pve_verify_ip
($addr, $noerr);
361 my $features_desc = {
365 description
=> "Allow mounting file systems of specific types."
366 ." This should be a list of file system types as used with the mount command."
367 ." Note that this can have negative effects on the container's security."
368 ." With access to a loop device, mounting a file can circumvent the mknod"
369 ." permission of the devices cgroup, mounting an NFS file system can"
370 ." block the host's I/O completely and prevent it from rebooting, etc.",
371 format_description
=> 'fstype;fstype;...',
372 pattern
=> qr/[a-zA-Z0-9_; ]+/,
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"
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.",
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.",
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.",
413 description
=> "Mount /sys in unprivileged containers as `rw` instead of `mixed`."
414 ." This can break networking under newer (>= v245) systemd-network use."
422 description
=> "Lock/unlock the VM.",
423 enum
=> [qw(backup create destroyed disk fstrim migrate mounted rollback snapshot snapshot-delete)],
428 description
=> "Specifies whether a VM will be started during system bootup.",
431 startup
=> get_standard_option
('pve-startup-order'),
435 description
=> "Enable/disable Template.",
441 enum
=> ['amd64', 'i386', 'arm64', 'armhf'],
442 description
=> "OS architecture type.",
448 enum
=> [qw(debian devuan ubuntu centos fedora opensuse archlinux alpine gentoo unmanaged)],
449 description
=> "OS type. This is used to setup configuration inside the container, and corresponds to lxc setup scripts in /usr/share/lxc/config/<ostype>.common.conf. Value 'unmanaged' can be used to skip and OS specific setup.",
454 description
=> "Attach a console device (/dev/console) to the container.",
460 description
=> "Specify the number of tty available to the container",
468 description
=> "The number of cores assigned to the container. A container can use all available cores by default.",
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.",
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.",
491 description
=> "Amount of RAM for the VM in MB.",
498 description
=> "Amount of SWAP for the VM in MB.",
504 description
=> "Set a host name for the container.",
505 type
=> 'string', format
=> 'dns-name',
511 description
=> "Container description. Only used on the configuration web interface.",
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.",
520 type
=> 'string', format
=> 'lxc-ip-with-ll-iface-list',
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.",
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",
528 rootfs
=> get_standard_option
('pve-ct-rootfs'),
531 type
=> 'string', format
=> 'pve-configid',
533 description
=> "Parent snapshot name. This is used internally, and should not be modified.",
537 description
=> "Timestamp for snapshots.",
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).",
545 enum
=> ['shell', 'console', 'tty'],
551 description
=> "Sets the protection flag of the container. This will prevent the CT or CT's disk remove/update operation.",
557 description
=> "Makes the container run as unprivileged user. (Should not be modified manually.)",
563 format
=> $features_desc,
564 description
=> "Allow containers access to advanced features.",
569 format
=> 'pve-volume-id',
570 description
=> 'Script that will be exectued during various steps in the containers lifetime.',
573 type
=> 'string', format
=> 'pve-tag-list',
574 description
=> 'Tags of the Container. This is only meta information.',
580 description
=> "Try to be more verbose. For now this only enables debug log-level on start.",
585 my $valid_lxc_conf_keys = {
586 'lxc.apparmor.profile' => 1,
587 'lxc.apparmor.allow_incomplete' => 1,
588 'lxc.apparmor.allow_nesting' => 1,
589 'lxc.apparmor.raw' => 1,
590 'lxc.selinux.context' => 1,
594 'lxc.signal.halt' => 1,
595 'lxc.signal.reboot' => 1,
596 'lxc.signal.stop' => 1,
599 'lxc.console.logfile' => 1,
600 'lxc.console.path' => 1,
602 'lxc.devtty.dir' => 1,
603 'lxc.hook.autodev' => 1,
606 'lxc.mount.fstab' => 1,
607 'lxc.mount.entry' => 1,
608 'lxc.mount.auto' => 1,
609 'lxc.rootfs.path' => 'lxc.rootfs.path is auto generated from rootfs',
610 'lxc.rootfs.mount' => 1,
611 'lxc.rootfs.options' => 'lxc.rootfs.options is not supported' .
612 ', please use mount point options in the "rootfs" key',
618 'lxc.seccomp.profile' => 1,
619 'lxc.seccomp.notify.proxy' => 1,
620 'lxc.seccomp.notify.cookie' => 1,
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,
630 'lxc.hook.version' => 1,
631 'lxc.log.level' => 1,
633 'lxc.start.auto' => 1,
634 'lxc.start.delay' => 1,
635 'lxc.start.order' => 1,
637 'lxc.environment' => 1,
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,
651 my $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',
684 sub 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";
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";
694 my $validity = $valid_lxc_conf_keys->{$key};
695 return $validity if defined($validity);
696 return 1 if $key =~ /^lxc\.cgroup2?\./ # allow all cgroup values
697 || $key =~ /^lxc\.prlimit\./ # allow all prlimits
698 || $key =~ /^lxc\.net\./; # allow custom network definitions
702 our $netconf_desc = {
706 description
=> "Network interface type.",
711 format_description
=> 'string',
712 description
=> 'Name of the network device as seen from inside the container. (lxc.network.name)',
713 pattern
=> '[-_.\w\d]+',
717 format_description
=> 'bridge',
718 description
=> 'Bridge to attach the network device to.',
719 pattern
=> '[-_.\w\d]+',
722 hwaddr
=> get_standard_option
('mac-addr', {
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)',
727 description
=> 'Maximum transfer unit of the interface. (lxc.network.mtu)',
728 minimum
=> 64, # minimum ethernet frame is 64 bytes
733 format
=> 'pve-ipv4-config',
734 format_description
=> '(IPv4/CIDR|dhcp|manual)',
735 description
=> 'IPv4 address in CIDR format.',
741 format_description
=> 'GatewayIPv4',
742 description
=> 'Default gateway for IPv4 traffic.',
747 format
=> 'pve-ipv6-config',
748 format_description
=> '(IPv6/CIDR|auto|dhcp|manual)',
749 description
=> 'IPv6 address in CIDR format.',
755 format_description
=> 'GatewayIPv6',
756 description
=> 'Default gateway for IPv6 traffic.',
761 description
=> "Controls whether this interface's firewall rules should be used.",
768 description
=> "VLAN tag for this interface.",
773 pattern
=> qr/\d+(?:;\d+)*/,
774 format_description
=> 'vlanid[;vlanid...]',
775 description
=> "VLAN ids to pass through the interface",
780 format_description
=> 'mbps',
781 description
=> "Apply rate limiting to the interface",
785 PVE
::JSONSchema
::register_format
('pve-lxc-network', $netconf_desc);
787 my $MAX_LXC_NETWORKS = 32;
788 for (my $i = 0; $i < $MAX_LXC_NETWORKS; $i++) {
789 $confdesc->{"net$i"} = {
791 type
=> 'string', format
=> $netconf_desc,
792 description
=> "Specifies network interfaces for the container.",
796 PVE
::JSONSchema
::register_format
('pve-ct-timezone', \
&verify_ct_timezone
);
797 sub verify_ct_timezone
{
798 my ($timezone, $noerr) = @_;
800 return if $timezone eq 'host'; # using host settings
802 PVE
::JSONSchema
::pve_verify_timezone
($timezone);
805 PVE
::JSONSchema
::register_format
('pve-lxc-mp-string', \
&verify_lxc_mp_string
);
806 sub verify_lxc_mp_string
{
807 my ($mp, $noerr) = @_;
811 # /. or /.. at the end
812 # ../ at the beginning
814 if($mp =~ m
@/\.\
.?
/@ ||
817 return undef if $noerr;
818 die "$mp contains illegal character sequences\n";
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).',
834 format
=> 'pve-lxc-mp-string',
835 format_description
=> 'Path',
836 description
=> 'Path to the mount point as seen from inside the container '.
837 '(must not contain symlinks).',
838 verbose_description
=> "Path to the mount point as seen from inside the container.\n\n".
839 "NOTE: Must not contain any symlinks for security reasons."
842 PVE
::JSONSchema
::register_format
('pve-ct-mountpoint', $mp_desc);
848 format
=> 'pve-volume-id',
849 format_description
=> 'volume',
850 description
=> 'The volume that is not used currently.',
854 for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
855 $confdesc->{"mp$i"} = {
857 type
=> 'string', format
=> $mp_desc,
858 description
=> "Use volume as container mount point.",
863 for (my $i = 0; $i < $MAX_UNUSED_DISKS; $i++) {
864 $confdesc->{"unused$i"} = {
866 type
=> 'string', format
=> $unused_desc,
867 description
=> "Reference to unused volumes. This is used internally, and should not be modified manually.",
871 sub parse_pct_config
{
872 my ($filename, $raw) = @_;
874 return undef if !defined($raw);
877 digest
=> Digest
::SHA
::sha1_hex
($raw),
882 $filename =~ m
|/lxc/(\d
+).conf
$|
883 || die "got strange filename '$filename'";
891 my @lines = split(/\n/, $raw);
892 foreach my $line (@lines) {
893 next if $line =~ m/^\s*$/;
895 if ($line =~ m/^\[pve:pending\]\s*$/i) {
896 $section = 'pending';
897 $conf->{description
} = $descr if $descr;
899 $conf = $res->{$section} = {};
901 } elsif ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
903 $conf->{description
} = $descr if $descr;
905 $conf = $res->{snapshots
}->{$section} = {};
909 if ($line =~ m/^\#(.*)\s*$/) {
910 $descr .= PVE
::Tools
::decode_text
($1) . "\n";
914 if ($line =~ m/^(lxc\.[a-z0-9_\-\.]+)(:|\s*=)\s*(.*?)\s*$/) {
917 my $validity = is_valid_lxc_conf_key
($vmid, $key);
918 if ($validity eq 1) {
919 push @{$conf->{lxc
}}, [$key, $value];
920 } elsif (my $errmsg = $validity) {
921 warn "vm $vmid - $key: $errmsg\n";
923 warn "vm $vmid - unable to parse config: $line\n";
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;
929 } elsif ($line =~ m/^delete:\s*(.*\S)\s*$/) {
931 if ($section eq 'pending') {
932 $conf->{delete} = $value;
934 warn "vm $vmid - property 'delete' is only allowed in [pve:pending]\n";
936 } elsif ($line =~ m/^([a-z][a-z_]*\d*):\s*(\S.*)\s*$/) {
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;
943 warn "vm $vmid - unable to parse config: $line\n";
947 $conf->{description
} = $descr if $descr;
949 delete $res->{snapstate
}; # just to be sure
954 sub write_pct_config
{
955 my ($filename, $conf) = @_;
957 delete $conf->{snapstate
}; # just to be sure
959 my $volidlist = PVE
::LXC
::Config-
>get_vm_volumes($conf);
960 my $used_volids = {};
961 foreach my $vid (@$volidlist) {
962 $used_volids->{$vid} = 1;
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};
973 my $generate_raw_config = sub {
978 # add description as comment to top of file
979 my $descr = $conf->{description
} || '';
980 foreach my $cl (split(/\n/, $descr)) {
981 $raw .= '#' . PVE
::Tools
::encode_text
($cl) . "\n";
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"
991 $raw .= "$key: $value\n";
994 if (my $lxcconf = $conf->{lxc
}) {
995 foreach my $entry (@$lxcconf) {
996 my ($k, $v) = @$entry;
1004 my $raw = &$generate_raw_config($conf);
1006 if (scalar(keys %{$conf->{pending
}})){
1007 $raw .= "\n[pve:pending]\n";
1008 $raw .= &$generate_raw_config($conf->{pending
});
1011 foreach my $snapname (sort keys %{$conf->{snapshots
}}) {
1012 $raw .= "\n[$snapname]\n";
1013 $raw .= &$generate_raw_config($conf->{snapshots
}->{$snapname});
1019 sub update_pct_config
{
1020 my ($class, $vmid, $conf, $running, $param, $delete, $revert) = @_;
1022 my $storage_cfg = PVE
::Storage
::config
();
1024 foreach my $opt (@$revert) {
1025 delete $conf->{pending
}->{$opt};
1026 $class->remove_from_pending_delete($conf, $opt); # also remove from deletion queue
1029 # write updates to pending section
1030 my $modified = {}; # record modified options
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";
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";
1047 $class->add_to_pending_delete($conf, $opt);
1050 my $check_content_type = sub {
1052 my $sid = PVE
::Storage
::parse_volume_id
($mp->{volume
});
1053 my $storage_config = PVE
::Storage
::storage_config
($storage_cfg, $sid);
1054 die "storage '$sid' does not allow content type 'rootdir' (Container)\n"
1055 if !$storage_config->{content
}->{rootdir
};
1058 foreach my $opt (sort keys %$param) { # add/change
1059 $modified->{$opt} = 1;
1060 my $value = $param->{$opt};
1061 if ($opt =~ m/^mp(\d+)$/ || $opt eq 'rootfs') {
1062 $class->check_protection($conf, "can't update CT $vmid drive '$opt'");
1063 my $mp = $class->parse_volume($opt, $value);
1064 $check_content_type->($mp) if ($mp->{type
} eq 'volume');
1065 } elsif ($opt eq 'hookscript') {
1066 PVE
::GuestHelpers
::check_hookscript
($value);
1067 } elsif ($opt eq 'nameserver') {
1068 $value = PVE
::LXC
::verify_nameserver_list
($value);
1069 } elsif ($opt eq 'searchdomain') {
1070 $value = PVE
::LXC
::verify_searchdomain_list
($value);
1071 } elsif ($opt eq 'unprivileged') {
1072 die "unable to modify read-only option: '$opt'\n";
1074 $conf->{pending
}->{$opt} = $value;
1075 $class->remove_from_pending_delete($conf, $opt);
1078 my $changes = $class->cleanup_pending($conf);
1082 $class->vmconfig_hotplug_pending($vmid, $conf, $storage_cfg, $modified, $errors);
1084 $class->vmconfig_apply_pending($vmid, $conf, $storage_cfg, $modified, $errors);
1091 my ($class, $key, $value) = @_;
1093 die "unknown setting '$key'\n" if !$confdesc->{$key};
1095 my $type = $confdesc->{$key}->{type
};
1097 if (!defined($value)) {
1098 die "got undefined value\n";
1101 if ($value =~ m/[\n\r]/) {
1102 die "property contains a line feed\n";
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);
1122 die "internal error"
1127 # add JSON properties for create and set function
1128 sub json_config_properties
{
1129 my ($class, $prop) = @_;
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};
1140 my $parse_ct_mountpoint_full = sub {
1141 my ($class, $desc, $data, $noerr) = @_;
1146 eval { $res = PVE
::JSONSchema
::parse_property_string
($desc, $data) };
1148 return undef if $noerr;
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";
1158 $res->{size
} = $size;
1161 $res->{type
} = $class->classify_mountpoint($res->{volume
});
1166 sub 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);
1174 my ($class, $key, $volume_string, $noerr) = @_;
1176 if ($key eq 'rootfs') {
1177 my $res = $parse_ct_mountpoint_full->($class, $rootfs_desc, $volume_string, $noerr);
1178 $res->{mp
} = '/' if defined($res);
1180 } elsif ($key =~ m/^mp\d+$/) {
1181 return $parse_ct_mountpoint_full->($class, $mp_desc, $volume_string, $noerr);
1182 } elsif ($key =~ m/^unused\d+$/) {
1183 return $parse_ct_mountpoint_full->($class, $unused_desc, $volume_string, $noerr);
1186 die "parse_volume - unknown type: $key\n";
1190 my ($class, $key, $volume) = @_;
1192 return $class->print_ct_mountpoint($volume, $key eq 'rootfs');
1201 sub print_lxc_network
{
1202 my ($class, $net) = @_;
1203 return PVE
::JSONSchema
::print_property_string
($net, $netconf_desc);
1206 sub parse_lxc_network
{
1207 my ($class, $data) = @_;
1211 return $res if !$data;
1213 $res = PVE
::JSONSchema
::parse_property_string
($netconf_desc, $data);
1215 $res->{type
} = 'veth';
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
});
1224 sub parse_features
{
1225 my ($class, $data) = @_;
1226 return {} if !$data;
1227 return PVE
::JSONSchema
::parse_property_string
($features_desc, $data);
1231 my ($class, $name) = @_;
1233 return defined($confdesc->{$name});
1235 # END JSON config code
1237 my $LXC_FASTPLUG_OPTIONS= {
1249 sub vmconfig_hotplug_pending
{
1250 my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_;
1252 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
1253 my $rootdir = "/proc/$pid/root";
1255 my $add_hotplug_error = sub {
1256 my ($opt, $msg) = @_;
1257 $errors->{$opt} = "unable to hotplug $opt: $msg";
1260 foreach my $opt (sort keys %{$conf->{pending
}}) { # add/change
1261 next if $selection && !$selection->{$opt};
1262 if ($LXC_FASTPLUG_OPTIONS->{$opt}) {
1263 $conf->{$opt} = delete $conf->{pending
}->{$opt};
1267 my $cgroup = PVE
::LXC
::CGroup-
>new($vmid);
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) = @_;
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);
1279 $hotplug_memory_done = 1;
1282 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
1283 # FIXME: $force deletion is not implemented for CTs
1284 foreach my $opt (sort keys %$pending_delete_hash) {
1285 next if $selection && !$selection->{$opt};
1287 if ($LXC_FASTPLUG_OPTIONS->{$opt}) {
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') {
1295 $cgroup->change_cpu_quota(-1, 100000);
1296 } elsif ($opt eq 'cpuunits') {
1297 $cgroup->change_cpu_shares(undef, $confdesc->{cpuunits
}->{default});
1298 } elsif ($opt =~ m/^net(\d)$/) {
1300 PVE
::Network
::veth_delete
("veth${vmid}i$netid");
1302 die "skip\n"; # skip non-hotpluggable opts
1306 $add_hotplug_error->($opt, $err) if $err ne "skip\n";
1308 delete $conf->{$opt};
1309 $class->remove_from_pending_delete($conf, $opt);
1313 foreach my $opt (sort keys %{$conf->{pending
}}) {
1314 next if $opt eq 'delete'; # just to be sure
1315 next if $selection && !$selection->{$opt};
1316 my $value = $conf->{pending
}->{$opt};
1318 if ($opt eq 'cpulimit') {
1319 my $quota = 100000 * $value;
1320 $cgroup->change_cpu_quota(int(100000 * $value), 100000);
1321 } elsif ($opt eq 'cpuunits') {
1322 $cgroup->change_cpu_shares($value, $confdesc->{cpuunits
}->{default});
1323 } elsif ($opt =~ m/^net(\d+)$/) {
1325 my $net = $class->parse_lxc_network($value);
1326 $value = $class->print_lxc_network($net);
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
});
1332 } elsif ($opt =~ m/^mp(\d+)$/) {
1333 if (!PVE
::LXC
::Tools
::can_use_new_mount_api
()) {
1337 if (exists($conf->{$opt})) {
1338 die "skip\n"; # don't try to hotplug over existing mp
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};
1345 die "skip\n"; # skip non-hotpluggable
1349 $add_hotplug_error->($opt, $err) if $err ne "skip\n";
1351 $conf->{$opt} = $value;
1352 delete $conf->{pending
}->{$opt};
1357 sub vmconfig_apply_pending
{
1358 my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_;
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;
1367 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
1368 # FIXME: $force deletion is not implemented for CTs
1369 foreach my $opt (sort keys %$pending_delete_hash) {
1370 next if $selection && !$selection->{$opt};
1372 if ($opt =~ m/^mp(\d+)$/) {
1373 my $mp = $class->parse_volume($opt, $conf->{$opt});
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);
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);
1384 $add_apply_error->($opt, $err);
1386 delete $conf->{$opt};
1387 $class->remove_from_pending_delete($conf, $opt);
1391 $class->cleanup_pending($conf);
1393 foreach my $opt (sort keys %{$conf->{pending
}}) { # add/change
1394 next if $opt eq 'delete'; # just to be sure
1395 next if $selection && !$selection->{$opt};
1397 if ($opt =~ m/^mp(\d+)$/) {
1398 $class->apply_pending_mountpoint($vmid, $conf, $opt, $storecfg, 0);
1399 } elsif ($opt =~ m/^net(\d+)$/) {
1401 my $net = $class->parse_lxc_network($conf->{pending
}->{$opt});
1402 $conf->{pending
}->{$opt} = $class->print_lxc_network($net);
1406 $add_apply_error->($opt, $err);
1408 $conf->{$opt} = delete $conf->{pending
}->{$opt};
1413 my $rescan_volume = sub {
1414 my ($storecfg, $mp) = @_;
1416 $mp->{size
} = PVE
::Storage
::volume_size_info
($storecfg, $mp->{volume
}, 5);
1418 warn "Could not rescan volume size - $@\n" if $@;
1421 sub apply_pending_mountpoint
{
1422 my ($class, $vmid, $conf, $opt, $storecfg, $running) = @_;
1424 my $mp = $class->parse_volume($opt, $conf->{pending
}->{$opt});
1425 my $old = $conf->{$opt};
1426 if ($mp->{type
} eq 'volume') {
1427 if ($mp->{volume
} =~ $PVE::LXC
::NEW_DISK_RE
) {
1428 my $original_value = $conf->{pending
}->{$opt};
1429 my $vollist = PVE
::LXC
::create_disks
(
1432 { $opt => $original_value },
1437 # Re-parse mount point:
1438 my $mp = $class->parse_volume($opt, $conf->{pending
}->{$opt});
1440 PVE
::LXC
::mountpoint_hotplug
($vmid, $conf, $opt, $mp, $storecfg);
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()
1448 $conf->{pending
}->{$opt} = $original_value;
1453 die "skip\n" if $running && defined($old); # TODO: "changing" mount points?
1454 $rescan_volume->($storecfg, $mp);
1456 PVE
::LXC
::mountpoint_hotplug
($vmid, $conf, $opt, $mp, $storecfg);
1458 $conf->{pending
}->{$opt} = $class->print_ct_mountpoint($mp);
1462 if (defined($old)) {
1463 my $mp = $class->parse_volume($opt, $old);
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);
1471 sub classify_mountpoint
{
1472 my ($class, $vol) = @_;
1473 if ($vol =~ m!^/!) {
1474 return 'device' if $vol =~ m!^/dev/!;
1480 my $__is_volume_in_use = sub {
1481 my ($class, $config, $volid) = @_;
1484 $class->foreach_volume($config, sub {
1485 my ($ms, $mountpoint) = @_;
1487 $used = $mountpoint->{type
} eq 'volume' && $mountpoint->{volume
} eq $volid;
1493 sub is_volume_in_use_by_snapshots
{
1494 my ($class, $config, $volid) = @_;
1496 if (my $snapshots = $config->{snapshots
}) {
1497 foreach my $snap (keys %$snapshots) {
1498 return 1 if $__is_volume_in_use->($class, $snapshots->{$snap}, $volid);
1505 sub is_volume_in_use
{
1506 my ($class, $config, $volid, $include_snapshots, $include_pending) = @_;
1507 return 1 if $__is_volume_in_use->($class, $config, $volid);
1508 return 1 if $include_snapshots && $class->is_volume_in_use_by_snapshots($config, $volid);
1509 return 1 if $include_pending && $__is_volume_in_use->($class, $config->{pending
}, $volid);
1513 sub has_dev_console
{
1514 my ($class, $conf) = @_;
1516 return !(defined($conf->{console
}) && !$conf->{console
});
1520 my ($class, $conf, $keyname) = @_;
1522 if (my $lxcconf = $conf->{lxc
}) {
1523 foreach my $entry (@$lxcconf) {
1524 my ($key, undef) = @$entry;
1525 return 1 if $key eq $keyname;
1533 my ($class, $conf) = @_;
1535 return $conf->{tty
} // $confdesc->{tty
}->{default};
1539 my ($class, $conf) = @_;
1541 return $conf->{cmode
} // $confdesc->{cmode
}->{default};
1544 sub valid_volume_keys
{
1545 my ($class, $reverse) = @_;
1547 my @names = ('rootfs');
1549 for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
1550 push @names, "mp$i";
1553 return $reverse ?
reverse @names : @names;
1556 sub get_vm_volumes
{
1557 my ($class, $conf, $excludes) = @_;
1561 $class->foreach_volume($conf, sub {
1562 my ($ms, $mountpoint) = @_;
1564 return if $excludes && $ms eq $excludes;
1566 my $volid = $mountpoint->{volume
};
1567 return if !$volid || $mountpoint->{type
} ne 'volume';
1569 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
1572 push @$vollist, $volid;
1578 sub get_replicatable_volumes
{
1579 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
1583 my $test_volid = sub {
1584 my ($volid, $mountpoint) = @_;
1588 my $mptype = $mountpoint->{type
};
1589 my $replicate = $mountpoint->{replicate
} // 1;
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";
1598 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, $noerr);
1599 return if !$storeid;
1601 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
1602 return if $scfg->{shared
};
1604 my ($path, $owner, $vtype) = PVE
::Storage
::path
($storecfg, $volid);
1605 return if !$owner || ($owner != $vmid);
1607 die "unable to replicate volume '$volid', type '$vtype'\n" if $vtype ne 'images';
1609 return if !$cleanup && !$replicate;
1611 if (!PVE
::Storage
::volume_has_feature
($storecfg, 'replicate', $volid)) {
1612 return if $cleanup || $noerr;
1613 die "missing replicate feature on volume '$volid'\n";
1616 $volhash->{$volid} = 1;
1619 $class->foreach_volume($conf, sub {
1620 my ($ms, $mountpoint) = @_;
1621 $test_volid->($mountpoint->{volume
}, $mountpoint);
1624 foreach my $snapname (keys %{$conf->{snapshots
}}) {
1625 my $snap = $conf->{snapshots
}->{$snapname};
1626 $class->foreach_volume($snap, sub {
1627 my ($ms, $mountpoint) = @_;
1628 $test_volid->($mountpoint->{volume
}, $mountpoint);
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 });
1642 sub get_backup_volumes
{
1643 my ($class, $conf) = @_;
1645 my $return_volumes = [];
1647 my $test_mountpoint = sub {
1648 my ($key, $volume) = @_;
1650 my ($included, $reason) = $class->mountpoint_backup_enabled($key, $volume);
1652 push @$return_volumes, {
1654 included
=> $included,
1656 volume_config
=> $volume,
1660 PVE
::LXC
::Config-
>foreach_volume($conf, $test_mountpoint);
1662 return $return_volumes;