1 package PVE
::LXC
::Config
;
6 use PVE
::AbstractConfig
;
7 use PVE
::Cluster
qw(cfs_register_file);
9 use PVE
::JSONSchema
qw(get_standard_option);
12 use base
qw(PVE::AbstractConfig);
14 my $nodename = PVE
::INotify
::nodename
();
15 my $lock_handles = {};
16 my $lockdir = "/run/lock/lxc";
18 mkdir "/etc/pve/nodes/$nodename/lxc";
19 my $MAX_MOUNT_POINTS = 10;
20 my $MAX_UNUSED_DISKS = $MAX_MOUNT_POINTS;
22 # BEGIN implemented abstract methods from PVE::AbstractConfig
28 sub __config_max_unused_disks
{
31 return $MAX_UNUSED_DISKS;
34 sub config_file_lock
{
35 my ($class, $vmid) = @_;
37 return "$lockdir/pve-config-${vmid}.lock";
41 my ($class, $vmid, $node) = @_;
43 $node = $nodename if !$node;
44 return "nodes/$node/lxc/$vmid.conf";
47 sub mountpoint_backup_enabled
{
48 my ($class, $mp_key, $mountpoint) = @_;
50 return 1 if $mp_key eq 'rootfs';
52 return 0 if $mountpoint->{type
} ne 'volume';
54 return 1 if $mountpoint->{backup
};
60 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
63 $class->foreach_mountpoint($conf, sub {
64 my ($ms, $mountpoint) = @_;
66 return if $err; # skip further test
67 return if $backup_only && !$class->mountpoint_backup_enabled($ms, $mountpoint);
70 if !PVE
::Storage
::volume_has_feature
($storecfg, $feature,
71 $mountpoint->{volume
},
78 sub __snapshot_save_vmstate
{
79 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
80 die "implement me - snapshot_save_vmstate\n";
83 sub __snapshot_check_running
{
84 my ($class, $vmid) = @_;
85 return PVE
::LXC
::check_running
($vmid);
88 sub __snapshot_check_freeze_needed
{
89 my ($class, $vmid, $config, $save_vmstate) = @_;
91 my $ret = $class->__snapshot_check_running($vmid);
95 sub __snapshot_freeze
{
96 my ($class, $vmid, $unfreeze) = @_;
99 eval { PVE
::Tools
::run_command
(['/usr/bin/lxc-unfreeze', '-n', $vmid]); };
102 PVE
::Tools
::run_command
(['/usr/bin/lxc-freeze', '-n', $vmid]);
103 PVE
::LXC
::sync_container_namespace
($vmid);
107 sub __snapshot_create_vol_snapshot
{
108 my ($class, $vmid, $ms, $mountpoint, $snapname) = @_;
110 my $storecfg = PVE
::Storage
::config
();
112 return if $snapname eq 'vzdump' &&
113 !$class->mountpoint_backup_enabled($ms, $mountpoint);
115 PVE
::Storage
::volume_snapshot
($storecfg, $mountpoint->{volume
}, $snapname);
118 sub __snapshot_delete_remove_drive
{
119 my ($class, $snap, $remove_drive) = @_;
121 if ($remove_drive eq 'vmstate') {
122 die "implement me - saving vmstate\n";
124 my $value = $snap->{$remove_drive};
125 my $mountpoint = $remove_drive eq 'rootfs' ?
$class->parse_ct_rootfs($value, 1) : $class->parse_ct_mountpoint($value, 1);
126 delete $snap->{$remove_drive};
128 $class->add_unused_volume($snap, $mountpoint->{volume
})
129 if ($mountpoint->{type
} eq 'volume');
133 sub __snapshot_delete_vmstate_file
{
134 my ($class, $snap, $force) = @_;
136 die "implement me - saving vmstate\n";
139 sub __snapshot_delete_vol_snapshot
{
140 my ($class, $vmid, $ms, $mountpoint, $snapname, $unused) = @_;
142 return if $snapname eq 'vzdump' &&
143 !$class->mountpoint_backup_enabled($ms, $mountpoint);
145 my $storecfg = PVE
::Storage
::config
();
146 PVE
::Storage
::volume_snapshot_delete
($storecfg, $mountpoint->{volume
}, $snapname);
147 push @$unused, $mountpoint->{volume
};
150 sub __snapshot_rollback_vol_possible
{
151 my ($class, $mountpoint, $snapname) = @_;
153 my $storecfg = PVE
::Storage
::config
();
154 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $mountpoint->{volume
}, $snapname);
157 sub __snapshot_rollback_vol_rollback
{
158 my ($class, $mountpoint, $snapname) = @_;
160 my $storecfg = PVE
::Storage
::config
();
161 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $mountpoint->{volume
}, $snapname);
164 sub __snapshot_rollback_vm_stop
{
165 my ($class, $vmid) = @_;
167 PVE
::Tools
::run_command
(['/usr/bin/lxc-stop', '-n', $vmid, '--kill'])
168 if $class->__snapshot_check_running($vmid);
171 sub __snapshot_rollback_vm_start
{
172 my ($class, $vmid, $vmstate, $forcemachine);
174 die "implement me - save vmstate\n";
177 sub __snapshot_rollback_get_unused
{
178 my ($class, $conf, $snap) = @_;
182 $class->__snapshot_foreach_volume($conf, sub {
183 my ($vs, $volume) = @_;
185 return if $volume->{type
} ne 'volume';
188 my $volid = $volume->{volume
};
190 $class->__snapshot_foreach_volume($snap, sub {
191 my ($ms, $mountpoint) = @_;
194 return if ($mountpoint->{type
} ne 'volume');
197 if ($mountpoint->{volume
} && $mountpoint->{volume
} eq $volid);
200 push @$unused, $volid if !$found;
206 sub __snapshot_foreach_volume
{
207 my ($class, $conf, $func) = @_;
209 $class->foreach_mountpoint($conf, $func);
212 # END implemented abstract methods from PVE::AbstractConfig
214 # BEGIN JSON config code
216 cfs_register_file
('/lxc/', \
&parse_pct_config
, \
&write_pct_config
);
222 format
=> 'pve-lxc-mp-string',
223 format_description
=> 'volume',
224 description
=> 'Volume, device or directory to mount into the container.',
228 format
=> 'disk-size',
229 format_description
=> 'DiskSize',
230 description
=> 'Volume size (read only value).',
235 description
=> 'Explicitly enable or disable ACL support.',
240 description
=> 'Read-only mount point',
245 description
=> 'Enable user quotas inside the container (not supported with zfs subvolumes)',
250 description
=> 'Mark this non-volume mount point as available on multiple nodes (see \'nodes\')',
251 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!",
257 PVE
::JSONSchema
::register_standard_option
('pve-ct-rootfs', {
258 type
=> 'string', format
=> $rootfs_desc,
259 description
=> "Use volume as container root.",
263 PVE
::JSONSchema
::register_standard_option
('pve-lxc-snapshot-name', {
264 description
=> "The name of the snapshot.",
265 type
=> 'string', format
=> 'pve-configid',
273 description
=> "Lock/unlock the VM.",
274 enum
=> [qw(migrate backup snapshot rollback)],
279 description
=> "Specifies whether a VM will be started during system bootup.",
282 startup
=> get_standard_option
('pve-startup-order'),
286 description
=> "Enable/disable Template.",
292 enum
=> ['amd64', 'i386'],
293 description
=> "OS architecture type.",
299 enum
=> [qw(debian ubuntu centos fedora opensuse archlinux alpine gentoo unmanaged)],
300 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.",
305 description
=> "Attach a console device (/dev/console) to the container.",
311 description
=> "Specify the number of tty available to the container",
319 description
=> "The number of cores assigned to the container. A container can use all available cores by default.",
326 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.",
334 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.",
342 description
=> "Amount of RAM for the VM in MB.",
349 description
=> "Amount of SWAP for the VM in MB.",
355 description
=> "Set a host name for the container.",
356 type
=> 'string', format
=> 'dns-name',
362 description
=> "Container description. Only used on the configuration web interface.",
366 type
=> 'string', format
=> 'dns-name-list',
367 description
=> "Sets DNS search domains for a container. Create will automatically use the setting from the host if you neither set searchdomain nor nameserver.",
371 type
=> 'string', format
=> 'address-list',
372 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.",
374 rootfs
=> get_standard_option
('pve-ct-rootfs'),
377 type
=> 'string', format
=> 'pve-configid',
379 description
=> "Parent snapshot name. This is used internally, and should not be modified.",
383 description
=> "Timestamp for snapshots.",
389 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).",
391 enum
=> ['shell', 'console', 'tty'],
397 description
=> "Sets the protection flag of the container. This will prevent the CT or CT's disk remove/update operation.",
403 description
=> "Makes the container run as unprivileged user. (Should not be modified manually.)",
408 my $valid_lxc_conf_keys = {
412 'lxc.haltsignal' => 1,
413 'lxc.rebootsignal' => 1,
414 'lxc.stopsignal' => 1,
416 'lxc.network.type' => 1,
417 'lxc.network.flags' => 1,
418 'lxc.network.link' => 1,
419 'lxc.network.mtu' => 1,
420 'lxc.network.name' => 1,
421 'lxc.network.hwaddr' => 1,
422 'lxc.network.ipv4' => 1,
423 'lxc.network.ipv4.gateway' => 1,
424 'lxc.network.ipv6' => 1,
425 'lxc.network.ipv6.gateway' => 1,
426 'lxc.network.script.up' => 1,
427 'lxc.network.script.down' => 1,
429 'lxc.console.logfile' => 1,
432 'lxc.devttydir' => 1,
433 'lxc.hook.autodev' => 1,
437 'lxc.mount.entry' => 1,
438 'lxc.mount.auto' => 1,
439 'lxc.rootfs' => 'lxc.rootfs is auto generated from rootfs',
440 'lxc.rootfs.mount' => 1,
441 'lxc.rootfs.options' => 'lxc.rootfs.options is not supported' .
442 ', please use mount point options in the "rootfs" key',
447 'lxc.aa_profile' => 1,
448 'lxc.aa_allow_incomplete' => 1,
449 'lxc.se_context' => 1,
452 'lxc.hook.pre-start' => 1,
453 'lxc.hook.pre-mount' => 1,
454 'lxc.hook.mount' => 1,
455 'lxc.hook.start' => 1,
456 'lxc.hook.stop' => 1,
457 'lxc.hook.post-stop' => 1,
458 'lxc.hook.clone' => 1,
459 'lxc.hook.destroy' => 1,
462 'lxc.start.auto' => 1,
463 'lxc.start.delay' => 1,
464 'lxc.start.order' => 1,
466 'lxc.environment' => 1,
469 our $netconf_desc = {
473 description
=> "Network interface type.",
478 format_description
=> 'string',
479 description
=> 'Name of the network device as seen from inside the container. (lxc.network.name)',
480 pattern
=> '[-_.\w\d]+',
484 format_description
=> 'bridge',
485 description
=> 'Bridge to attach the network device to.',
486 pattern
=> '[-_.\w\d]+',
491 format_description
=> "XX:XX:XX:XX:XX:XX",
492 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)',
493 pattern
=> qr/(?:[a-f0-9]{2}:){5}[a-f0-9]{2}/i,
498 description
=> 'Maximum transfer unit of the interface. (lxc.network.mtu)',
499 minimum
=> 64, # minimum ethernet frame is 64 bytes
504 format
=> 'pve-ipv4-config',
505 format_description
=> 'IPv4Format/CIDR',
506 description
=> 'IPv4 address in CIDR format.',
512 format_description
=> 'GatewayIPv4',
513 description
=> 'Default gateway for IPv4 traffic.',
518 format
=> 'pve-ipv6-config',
519 format_description
=> 'IPv6Format/CIDR',
520 description
=> 'IPv6 address in CIDR format.',
526 format_description
=> 'GatewayIPv6',
527 description
=> 'Default gateway for IPv6 traffic.',
532 description
=> "Controls whether this interface's firewall rules should be used.",
539 description
=> "VLAN tag for this interface.",
544 pattern
=> qr/\d+(?:;\d+)*/,
545 format_description
=> 'vlanid[;vlanid...]',
546 description
=> "VLAN ids to pass through the interface",
551 format_description
=> 'mbps',
552 description
=> "Apply rate limiting to the interface",
556 PVE
::JSONSchema
::register_format
('pve-lxc-network', $netconf_desc);
558 my $MAX_LXC_NETWORKS = 10;
559 for (my $i = 0; $i < $MAX_LXC_NETWORKS; $i++) {
560 $confdesc->{"net$i"} = {
562 type
=> 'string', format
=> $netconf_desc,
563 description
=> "Specifies network interfaces for the container.",
567 PVE
::JSONSchema
::register_format
('pve-lxc-mp-string', \
&verify_lxc_mp_string
);
568 sub verify_lxc_mp_string
{
569 my ($mp, $noerr) = @_;
573 # /. or /.. at the end
574 # ../ at the beginning
576 if($mp =~ m
@/\.\
.?
/@ ||
579 return undef if $noerr;
580 die "$mp contains illegal character sequences\n";
589 description
=> 'Whether to include the mount point in backups.',
590 verbose_description
=> 'Whether to include the mount point in backups '.
591 '(only used for volume mount points).',
596 format
=> 'pve-lxc-mp-string',
597 format_description
=> 'Path',
598 description
=> 'Path to the mount point as seen from inside the container '.
599 '(must not contain symlinks).',
600 verbose_description
=> "Path to the mount point as seen from inside the container.\n\n".
601 "NOTE: Must not contain any symlinks for security reasons."
604 PVE
::JSONSchema
::register_format
('pve-ct-mountpoint', $mp_desc);
608 type
=> 'string', format
=> 'pve-volume-id',
609 description
=> "Reference to unused volumes. This is used internally, and should not be modified manually.",
612 for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
613 $confdesc->{"mp$i"} = {
615 type
=> 'string', format
=> $mp_desc,
616 description
=> "Use volume as container mount point.",
621 for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
622 $confdesc->{"unused$i"} = $unuseddesc;
625 sub parse_pct_config
{
626 my ($filename, $raw) = @_;
628 return undef if !defined($raw);
631 digest
=> Digest
::SHA
::sha1_hex
($raw),
635 $filename =~ m
|/lxc/(\d
+).conf
$|
636 || die "got strange filename '$filename'";
644 my @lines = split(/\n/, $raw);
645 foreach my $line (@lines) {
646 next if $line =~ m/^\s*$/;
648 if ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
650 $conf->{description
} = $descr if $descr;
652 $conf = $res->{snapshots
}->{$section} = {};
656 if ($line =~ m/^\#(.*)\s*$/) {
657 $descr .= PVE
::Tools
::decode_text
($1) . "\n";
661 if ($line =~ m/^(lxc\.[a-z0-9_\-\.]+)(:|\s*=)\s*(.*?)\s*$/) {
664 my $validity = $valid_lxc_conf_keys->{$key} || 0;
665 if ($validity eq 1 || $key =~ m/^lxc\.(?:cgroup|limit)\./) {
666 push @{$conf->{lxc
}}, [$key, $value];
667 } elsif (my $errmsg = $validity) {
668 warn "vm $vmid - $key: $errmsg\n";
670 warn "vm $vmid - unable to parse config: $line\n";
672 } elsif ($line =~ m/^(description):\s*(.*\S)\s*$/) {
673 $descr .= PVE
::Tools
::decode_text
($2);
674 } elsif ($line =~ m/snapstate:\s*(prepare|delete)\s*$/) {
675 $conf->{snapstate
} = $1;
676 } elsif ($line =~ m/^([a-z][a-z_]*\d*):\s*(\S.*)\s*$/) {
679 eval { $value = PVE
::LXC
::Config-
>check_type($key, $value); };
680 warn "vm $vmid - unable to parse value of '$key' - $@" if $@;
681 $conf->{$key} = $value;
683 warn "vm $vmid - unable to parse config: $line\n";
687 $conf->{description
} = $descr if $descr;
689 delete $res->{snapstate
}; # just to be sure
694 sub write_pct_config
{
695 my ($filename, $conf) = @_;
697 delete $conf->{snapstate
}; # just to be sure
699 my $volidlist = PVE
::LXC
::Config-
>get_vm_volumes($conf);
700 my $used_volids = {};
701 foreach my $vid (@$volidlist) {
702 $used_volids->{$vid} = 1;
705 # remove 'unusedX' settings if the volume is still used
706 foreach my $key (keys %$conf) {
707 my $value = $conf->{$key};
708 if ($key =~ m/^unused/ && $used_volids->{$value}) {
709 delete $conf->{$key};
713 my $generate_raw_config = sub {
718 # add description as comment to top of file
719 my $descr = $conf->{description
} || '';
720 foreach my $cl (split(/\n/, $descr)) {
721 $raw .= '#' . PVE
::Tools
::encode_text
($cl) . "\n";
724 foreach my $key (sort keys %$conf) {
725 next if $key eq 'digest' || $key eq 'description' ||
726 $key eq 'pending' || $key eq 'snapshots' ||
727 $key eq 'snapname' || $key eq 'lxc';
728 my $value = $conf->{$key};
729 die "detected invalid newline inside property '$key'\n"
731 $raw .= "$key: $value\n";
734 if (my $lxcconf = $conf->{lxc
}) {
735 foreach my $entry (@$lxcconf) {
736 my ($k, $v) = @$entry;
744 my $raw = &$generate_raw_config($conf);
746 foreach my $snapname (sort keys %{$conf->{snapshots
}}) {
747 $raw .= "\n[$snapname]\n";
748 $raw .= &$generate_raw_config($conf->{snapshots
}->{$snapname});
754 sub update_pct_config
{
755 my ($class, $vmid, $conf, $running, $param, $delete) = @_;
764 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
765 $rootdir = "/proc/$pid/root";
768 my $hotplug_error = sub {
777 if (defined($delete)) {
778 foreach my $opt (@$delete) {
779 if (!exists($conf->{$opt})) {
784 if ($opt eq 'memory' || $opt eq 'rootfs') {
785 die "unable to delete required option '$opt'\n";
786 } elsif ($opt eq 'hostname') {
787 delete $conf->{$opt};
788 } elsif ($opt eq 'swap') {
789 delete $conf->{$opt};
790 PVE
::LXC
::write_cgroup_value
("memory", $vmid,
791 "memory.memsw.limit_in_bytes", -1);
792 } elsif ($opt eq 'description' || $opt eq 'onboot' || $opt eq 'startup') {
793 delete $conf->{$opt};
794 } elsif ($opt eq 'nameserver' || $opt eq 'searchdomain' ||
795 $opt eq 'tty' || $opt eq 'console' || $opt eq 'cmode') {
796 next if $hotplug_error->($opt);
797 delete $conf->{$opt};
798 } elsif ($opt eq 'cores') {
799 delete $conf->{$opt}; # rest is handled by pvestatd
800 } elsif ($opt eq 'cpulimit') {
801 PVE
::LXC
::write_cgroup_value
("cpu", $vmid, "cpu.cfs_quota_us", -1);
802 delete $conf->{$opt};
803 } elsif ($opt eq 'cpuunits') {
804 PVE
::LXC
::write_cgroup_value
("cpu", $vmid, "cpu.shares", $confdesc->{cpuunits
}->{default});
805 delete $conf->{$opt};
806 } elsif ($opt =~ m/^net(\d)$/) {
807 delete $conf->{$opt};
810 PVE
::Network
::veth_delete
("veth${vmid}i$netid");
811 } elsif ($opt eq 'protection') {
812 delete $conf->{$opt};
813 } elsif ($opt =~ m/^unused(\d+)$/) {
814 next if $hotplug_error->($opt);
815 PVE
::LXC
::Config-
>check_protection($conf, "can't remove CT $vmid drive '$opt'");
816 push @deleted_volumes, $conf->{$opt};
817 delete $conf->{$opt};
818 } elsif ($opt =~ m/^mp(\d+)$/) {
819 next if $hotplug_error->($opt);
820 PVE
::LXC
::Config-
>check_protection($conf, "can't remove CT $vmid drive '$opt'");
821 my $mp = PVE
::LXC
::Config-
>parse_ct_mountpoint($conf->{$opt});
822 delete $conf->{$opt};
823 if ($mp->{type
} eq 'volume') {
824 PVE
::LXC
::Config-
>add_unused_volume($conf, $mp->{volume
});
826 } elsif ($opt eq 'unprivileged') {
827 die "unable to delete read-only option: '$opt'\n";
829 die "implement me (delete: $opt)"
831 PVE
::LXC
::Config-
>write_config($vmid, $conf) if $running;
835 # There's no separate swap size to configure, there's memory and "total"
836 # memory (iow. memory+swap). This means we have to change them together.
837 my $wanted_memory = PVE
::Tools
::extract_param
($param, 'memory');
838 my $wanted_swap = PVE
::Tools
::extract_param
($param, 'swap');
839 if (defined($wanted_memory) || defined($wanted_swap)) {
841 my $old_memory = ($conf->{memory
} || 512);
842 my $old_swap = ($conf->{swap
} || 0);
844 $wanted_memory //= $old_memory;
845 $wanted_swap //= $old_swap;
847 my $total = $wanted_memory + $wanted_swap;
849 my $old_total = $old_memory + $old_swap;
850 if ($total > $old_total) {
851 PVE
::LXC
::write_cgroup_value
("memory", $vmid,
852 "memory.memsw.limit_in_bytes",
853 int($total*1024*1024));
854 PVE
::LXC
::write_cgroup_value
("memory", $vmid,
855 "memory.limit_in_bytes",
856 int($wanted_memory*1024*1024));
858 PVE
::LXC
::write_cgroup_value
("memory", $vmid,
859 "memory.limit_in_bytes",
860 int($wanted_memory*1024*1024));
861 PVE
::LXC
::write_cgroup_value
("memory", $vmid,
862 "memory.memsw.limit_in_bytes",
863 int($total*1024*1024));
866 $conf->{memory
} = $wanted_memory;
867 $conf->{swap
} = $wanted_swap;
869 PVE
::LXC
::Config-
>write_config($vmid, $conf) if $running;
872 my $used_volids = {};
873 my $check_content_type = sub {
875 my $sid = PVE
::Storage
::parse_volume_id
($mp->{volume
});
876 my $scfg = PVE
::Storage
::config
();
877 my $storage_config = PVE
::Storage
::storage_config
($scfg, $sid);
878 die "storage '$sid' does not allow content type 'rootdir' (Container)\n"
879 if !$storage_config->{content
}->{rootdir
};
882 foreach my $opt (keys %$param) {
883 my $value = $param->{$opt};
884 my $check_protection_msg = "can't update CT $vmid drive '$opt'";
885 if ($opt eq 'hostname' || $opt eq 'arch') {
886 $conf->{$opt} = $value;
887 } elsif ($opt eq 'onboot') {
888 $conf->{$opt} = $value ?
1 : 0;
889 } elsif ($opt eq 'startup') {
890 $conf->{$opt} = $value;
891 } elsif ($opt eq 'tty' || $opt eq 'console' || $opt eq 'cmode') {
892 next if $hotplug_error->($opt);
893 $conf->{$opt} = $value;
894 } elsif ($opt eq 'nameserver') {
895 next if $hotplug_error->($opt);
896 my $list = PVE
::LXC
::verify_nameserver_list
($value);
897 $conf->{$opt} = $list;
898 } elsif ($opt eq 'searchdomain') {
899 next if $hotplug_error->($opt);
900 my $list = PVE
::LXC
::verify_searchdomain_list
($value);
901 $conf->{$opt} = $list;
902 } elsif ($opt eq 'cores') {
903 $conf->{$opt} = $value;# rest is handled by pvestatd
904 } elsif ($opt eq 'cpulimit') {
906 PVE
::LXC
::write_cgroup_value
("cpu", $vmid, "cpu.cfs_quota_us", -1);
908 PVE
::LXC
::write_cgroup_value
("cpu", $vmid, "cpu.cfs_quota_us", int(100000*$value));
910 $conf->{$opt} = $value;
911 } elsif ($opt eq 'cpuunits') {
912 $conf->{$opt} = $value;
913 PVE
::LXC
::write_cgroup_value
("cpu", $vmid, "cpu.shares", $value);
914 } elsif ($opt eq 'description') {
915 $conf->{$opt} = PVE
::Tools
::encode_text
($value);
916 } elsif ($opt =~ m/^net(\d+)$/) {
918 my $net = PVE
::LXC
::Config-
>parse_lxc_network($value);
920 $conf->{$opt} = PVE
::LXC
::Config-
>print_lxc_network($net);
922 PVE
::LXC
::update_net
($vmid, $conf, $opt, $net, $netid, $rootdir);
924 } elsif ($opt eq 'protection') {
925 $conf->{$opt} = $value ?
1 : 0;
926 } elsif ($opt =~ m/^mp(\d+)$/) {
927 next if $hotplug_error->($opt);
928 PVE
::LXC
::Config-
>check_protection($conf, $check_protection_msg);
929 my $old = $conf->{$opt};
930 my $mp = PVE
::LXC
::Config-
>parse_ct_mountpoint($value);
931 if ($mp->{type
} eq 'volume') {
932 &$check_content_type($mp);
933 $used_volids->{$mp->{volume
}} = 1;
935 $conf->{$opt} = $value;
937 my $mp = PVE
::LXC
::Config-
>parse_ct_mountpoint($old);
938 if ($mp->{type
} eq 'volume') {
939 PVE
::LXC
::Config-
>add_unused_volume($conf, $mp->{volume
});
943 } elsif ($opt eq 'rootfs') {
944 next if $hotplug_error->($opt);
945 PVE
::LXC
::Config-
>check_protection($conf, $check_protection_msg);
946 my $old = $conf->{$opt};
947 $conf->{$opt} = $value;
948 my $mp = PVE
::LXC
::Config-
>parse_ct_rootfs($value);
949 if ($mp->{type
} eq 'volume') {
950 &$check_content_type($mp);
951 $used_volids->{$mp->{volume
}} = 1;
954 my $mp = PVE
::LXC
::Config-
>parse_ct_rootfs($old);
955 if ($mp->{type
} eq 'volume') {
956 PVE
::LXC
::Config-
>add_unused_volume($conf, $mp->{volume
});
960 } elsif ($opt eq 'unprivileged') {
961 die "unable to modify read-only option: '$opt'\n";
962 } elsif ($opt eq 'ostype') {
963 next if $hotplug_error->($opt);
964 $conf->{$opt} = $value;
966 die "implement me: $opt";
968 PVE
::LXC
::Config-
>write_config($vmid, $conf) if $running;
971 # Apply deletions and creations of new volumes
972 if (@deleted_volumes) {
973 my $storage_cfg = PVE
::Storage
::config
();
974 foreach my $volume (@deleted_volumes) {
975 next if $used_volids->{$volume}; # could have been re-added, too
976 # also check for references in snapshots
977 next if $class->is_volume_in_use($conf, $volume, 1);
978 PVE
::LXC
::delete_mountpoint_volume
($storage_cfg, $vmid, $volume);
983 my $storage_cfg = PVE
::Storage
::config
();
984 PVE
::LXC
::create_disks
($storage_cfg, $vmid, $conf, $conf);
987 # This should be the last thing we do here
988 if ($running && scalar(@nohotplug)) {
989 die "unable to modify " . join(',', @nohotplug) . " while container is running\n";
994 my ($class, $key, $value) = @_;
996 die "unknown setting '$key'\n" if !$confdesc->{$key};
998 my $type = $confdesc->{$key}->{type
};
1000 if (!defined($value)) {
1001 die "got undefined value\n";
1004 if ($value =~ m/[\n\r]/) {
1005 die "property contains a line feed\n";
1008 if ($type eq 'boolean') {
1009 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
1010 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
1011 die "type check ('boolean') failed - got '$value'\n";
1012 } elsif ($type eq 'integer') {
1013 return int($1) if $value =~ m/^(\d+)$/;
1014 die "type check ('integer') failed - got '$value'\n";
1015 } elsif ($type eq 'number') {
1016 return $value if $value =~ m/^(\d+)(\.\d+)?$/;
1017 die "type check ('number') failed - got '$value'\n";
1018 } elsif ($type eq 'string') {
1019 if (my $fmt = $confdesc->{$key}->{format
}) {
1020 PVE
::JSONSchema
::check_format
($fmt, $value);
1025 die "internal error"
1030 # add JSON properties for create and set function
1031 sub json_config_properties
{
1032 my ($class, $prop) = @_;
1034 foreach my $opt (keys %$confdesc) {
1035 next if $opt eq 'parent' || $opt eq 'snaptime';
1036 next if $prop->{$opt};
1037 $prop->{$opt} = $confdesc->{$opt};
1043 sub __parse_ct_mountpoint_full
{
1044 my ($class, $desc, $data, $noerr) = @_;
1049 eval { $res = PVE
::JSONSchema
::parse_property_string
($desc, $data) };
1051 return undef if $noerr;
1055 if (defined(my $size = $res->{size
})) {
1056 $size = PVE
::JSONSchema
::parse_size
($size);
1057 if (!defined($size)) {
1058 return undef if $noerr;
1059 die "invalid size: $size\n";
1061 $res->{size
} = $size;
1064 $res->{type
} = $class->classify_mountpoint($res->{volume
});
1069 sub parse_ct_rootfs
{
1070 my ($class, $data, $noerr) = @_;
1072 my $res = $class->__parse_ct_mountpoint_full($rootfs_desc, $data, $noerr);
1074 $res->{mp
} = '/' if defined($res);
1079 sub parse_ct_mountpoint
{
1080 my ($class, $data, $noerr) = @_;
1082 return $class->__parse_ct_mountpoint_full($mp_desc, $data, $noerr);
1085 sub print_ct_mountpoint
{
1086 my ($class, $info, $nomp) = @_;
1087 my $skip = [ 'type' ];
1088 push @$skip, 'mp' if $nomp;
1089 return PVE
::JSONSchema
::print_property_string
($info, $mp_desc, $skip);
1092 sub print_lxc_network
{
1093 my ($class, $net) = @_;
1094 return PVE
::JSONSchema
::print_property_string
($net, $netconf_desc);
1097 sub parse_lxc_network
{
1098 my ($class, $data) = @_;
1102 return $res if !$data;
1104 $res = PVE
::JSONSchema
::parse_property_string
($netconf_desc, $data);
1106 $res->{type
} = 'veth';
1107 if (!$res->{hwaddr
}) {
1108 my $dc = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
1109 $res->{hwaddr
} = PVE
::Tools
::random_ether_addr
($dc->{mac_prefix
});
1116 my ($class, $name) = @_;
1118 return defined($confdesc->{$name});
1120 # END JSON config code
1122 sub classify_mountpoint
{
1123 my ($class, $vol) = @_;
1124 if ($vol =~ m!^/!) {
1125 return 'device' if $vol =~ m!^/dev/!;
1131 sub is_volume_in_use
{
1132 my ($class, $config, $volid, $include_snapshots) = @_;
1135 $class->foreach_mountpoint($config, sub {
1136 my ($ms, $mountpoint) = @_;
1138 $used = $mountpoint->{type
} eq 'volume' && $mountpoint->{volume
} eq $volid;
1141 my $snapshots = $config->{snapshots
};
1142 if ($include_snapshots && $snapshots) {
1143 foreach my $snap (keys %$snapshots) {
1144 $used ||= $class->is_volume_in_use($snapshots->{$snap}, $volid);
1151 sub has_dev_console
{
1152 my ($class, $conf) = @_;
1154 return !(defined($conf->{console
}) && !$conf->{console
});
1158 my ($class, $conf, $keyname) = @_;
1160 if (my $lxcconf = $conf->{lxc
}) {
1161 foreach my $entry (@$lxcconf) {
1162 my ($key, undef) = @$entry;
1163 return 1 if $key eq $keyname;
1171 my ($class, $conf) = @_;
1173 return $conf->{tty
} // $confdesc->{tty
}->{default};
1177 my ($class, $conf) = @_;
1179 return $conf->{cmode
} // $confdesc->{cmode
}->{default};
1182 sub mountpoint_names
{
1183 my ($class, $reverse) = @_;
1185 my @names = ('rootfs');
1187 for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
1188 push @names, "mp$i";
1191 return $reverse ?
reverse @names : @names;
1194 sub foreach_mountpoint_full
{
1195 my ($class, $conf, $reverse, $func, @param) = @_;
1197 foreach my $key ($class->mountpoint_names($reverse)) {
1198 my $value = $conf->{$key};
1199 next if !defined($value);
1200 my $mountpoint = $key eq 'rootfs' ?
$class->parse_ct_rootfs($value, 1) : $class->parse_ct_mountpoint($value, 1);
1201 next if !defined($mountpoint);
1203 &$func($key, $mountpoint, @param);
1207 sub foreach_mountpoint
{
1208 my ($class, $conf, $func, @param) = @_;
1210 $class->foreach_mountpoint_full($conf, 0, $func, @param);
1213 sub foreach_mountpoint_reverse
{
1214 my ($class, $conf, $func, @param) = @_;
1216 $class->foreach_mountpoint_full($conf, 1, $func, @param);
1219 sub get_vm_volumes
{
1220 my ($class, $conf, $excludes) = @_;
1224 $class->foreach_mountpoint($conf, sub {
1225 my ($ms, $mountpoint) = @_;
1227 return if $excludes && $ms eq $excludes;
1229 my $volid = $mountpoint->{volume
};
1230 return if !$volid || $mountpoint->{type
} ne 'volume';
1232 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
1235 push @$vollist, $volid;