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
=> "Description for the Container. Shown in the web-interface CT's summary."
512 ." This is saved as comment inside the configuration file.",
513 maxLength
=> 1024 * 8,
517 type
=> 'string', format
=> 'dns-name-list',
518 description
=> "Sets DNS search domains for a container. Create will automatically use the setting from the host if you neither set searchdomain nor nameserver.",
522 type
=> 'string', format
=> 'lxc-ip-with-ll-iface-list',
523 description
=> "Sets DNS server IP address for a container. Create will automatically use the setting from the host if you neither set searchdomain nor nameserver.",
527 type
=> 'string', format
=> 'pve-ct-timezone',
528 description
=> "Time zone to use in the container. If option isn't set, then nothing will be done. Can be set to 'host' to match the host time zone, or an arbitrary time zone option from /usr/share/zoneinfo/zone.tab",
530 rootfs
=> get_standard_option
('pve-ct-rootfs'),
533 type
=> 'string', format
=> 'pve-configid',
535 description
=> "Parent snapshot name. This is used internally, and should not be modified.",
539 description
=> "Timestamp for snapshots.",
545 description
=> "Console mode. By default, the console command tries to open a connection to one of the available tty devices. By setting cmode to 'console' it tries to attach to /dev/console instead. If you set cmode to 'shell', it simply invokes a shell inside the container (no login).",
547 enum
=> ['shell', 'console', 'tty'],
553 description
=> "Sets the protection flag of the container. This will prevent the CT or CT's disk remove/update operation.",
559 description
=> "Makes the container run as unprivileged user. (Should not be modified manually.)",
565 format
=> $features_desc,
566 description
=> "Allow containers access to advanced features.",
571 format
=> 'pve-volume-id',
572 description
=> 'Script that will be exectued during various steps in the containers lifetime.',
575 type
=> 'string', format
=> 'pve-tag-list',
576 description
=> 'Tags of the Container. This is only meta information.',
582 description
=> "Try to be more verbose. For now this only enables debug log-level on start.",
587 my $valid_lxc_conf_keys = {
588 'lxc.apparmor.profile' => 1,
589 'lxc.apparmor.allow_incomplete' => 1,
590 'lxc.apparmor.allow_nesting' => 1,
591 'lxc.apparmor.raw' => 1,
592 'lxc.selinux.context' => 1,
596 'lxc.signal.halt' => 1,
597 'lxc.signal.reboot' => 1,
598 'lxc.signal.stop' => 1,
601 'lxc.console.logfile' => 1,
602 'lxc.console.path' => 1,
604 'lxc.devtty.dir' => 1,
605 'lxc.hook.autodev' => 1,
608 'lxc.mount.fstab' => 1,
609 'lxc.mount.entry' => 1,
610 'lxc.mount.auto' => 1,
611 'lxc.rootfs.path' => 'lxc.rootfs.path is auto generated from rootfs',
612 'lxc.rootfs.mount' => 1,
613 'lxc.rootfs.options' => 'lxc.rootfs.options is not supported' .
614 ', please use mount point options in the "rootfs" key',
620 'lxc.seccomp.profile' => 1,
621 'lxc.seccomp.notify.proxy' => 1,
622 'lxc.seccomp.notify.cookie' => 1,
624 'lxc.hook.pre-start' => 1,
625 'lxc.hook.pre-mount' => 1,
626 'lxc.hook.mount' => 1,
627 'lxc.hook.start' => 1,
628 'lxc.hook.stop' => 1,
629 'lxc.hook.post-stop' => 1,
630 'lxc.hook.clone' => 1,
631 'lxc.hook.destroy' => 1,
632 'lxc.hook.version' => 1,
633 'lxc.log.level' => 1,
635 'lxc.start.auto' => 1,
636 'lxc.start.delay' => 1,
637 'lxc.start.order' => 1,
639 'lxc.environment' => 1,
641 # All these are namespaced via CLONE_NEWIPC (see namespaces(7)).
642 'lxc.sysctl.fs.mqueue' => 1,
643 'lxc.sysctl.kernel.msgmax' => 1,
644 'lxc.sysctl.kernel.msgmnb' => 1,
645 'lxc.sysctl.kernel.msgmni' => 1,
646 'lxc.sysctl.kernel.sem' => 1,
647 'lxc.sysctl.kernel.shmall' => 1,
648 'lxc.sysctl.kernel.shmmax' => 1,
649 'lxc.sysctl.kernel.shmmni' => 1,
650 'lxc.sysctl.kernel.shm_rmid_forced' => 1,
653 my $deprecated_lxc_conf_keys = {
654 # Deprecated (removed with lxc 3.0):
655 'lxc.aa_profile' => 'lxc.apparmor.profile',
656 'lxc.aa_allow_incomplete' => 'lxc.apparmor.allow_incomplete',
657 'lxc.console' => 'lxc.console.path',
658 'lxc.devttydir' => 'lxc.tty.dir',
659 'lxc.haltsignal' => 'lxc.signal.halt',
660 'lxc.rebootsignal' => 'lxc.signal.reboot',
661 'lxc.stopsignal' => 'lxc.signal.stop',
662 'lxc.id_map' => 'lxc.idmap',
663 'lxc.init_cmd' => 'lxc.init.cmd',
664 'lxc.loglevel' => 'lxc.log.level',
665 'lxc.logfile' => 'lxc.log.file',
666 'lxc.mount' => 'lxc.mount.fstab',
667 'lxc.network.type' => 'lxc.net.INDEX.type',
668 'lxc.network.flags' => 'lxc.net.INDEX.flags',
669 'lxc.network.link' => 'lxc.net.INDEX.link',
670 'lxc.network.mtu' => 'lxc.net.INDEX.mtu',
671 'lxc.network.name' => 'lxc.net.INDEX.name',
672 'lxc.network.hwaddr' => 'lxc.net.INDEX.hwaddr',
673 'lxc.network.ipv4' => 'lxc.net.INDEX.ipv4.address',
674 'lxc.network.ipv4.gateway' => 'lxc.net.INDEX.ipv4.gateway',
675 'lxc.network.ipv6' => 'lxc.net.INDEX.ipv6.address',
676 'lxc.network.ipv6.gateway' => 'lxc.net.INDEX.ipv6.gateway',
677 'lxc.network.script.up' => 'lxc.net.INDEX.script.up',
678 'lxc.network.script.down' => 'lxc.net.INDEX.script.down',
679 'lxc.pts' => 'lxc.pty.max',
680 'lxc.se_context' => 'lxc.selinux.context',
681 'lxc.seccomp' => 'lxc.seccomp.profile',
682 'lxc.tty' => 'lxc.tty.max',
683 'lxc.utsname' => 'lxc.uts.name',
686 sub is_valid_lxc_conf_key
{
687 my ($vmid, $key) = @_;
688 if ($key =~ /^lxc\.limit\./) {
689 warn "vm $vmid - $key: lxc.limit.* was renamed to lxc.prlimit.*\n";
692 if (defined(my $new_name = $deprecated_lxc_conf_keys->{$key})) {
693 warn "vm $vmid - $key is deprecated and was renamed to $new_name\n";
696 my $validity = $valid_lxc_conf_keys->{$key};
697 return $validity if defined($validity);
698 return 1 if $key =~ /^lxc\.cgroup2?\./ # allow all cgroup values
699 || $key =~ /^lxc\.prlimit\./ # allow all prlimits
700 || $key =~ /^lxc\.net\./; # allow custom network definitions
704 our $netconf_desc = {
708 description
=> "Network interface type.",
713 format_description
=> 'string',
714 description
=> 'Name of the network device as seen from inside the container. (lxc.network.name)',
715 pattern
=> '[-_.\w\d]+',
719 format_description
=> 'bridge',
720 description
=> 'Bridge to attach the network device to.',
721 pattern
=> '[-_.\w\d]+',
724 hwaddr
=> get_standard_option
('mac-addr', {
725 description
=> 'The interface MAC address. This is dynamically allocated by default, but you can set that statically if needed, for example to always have the same link-local IPv6 address. (lxc.network.hwaddr)',
729 description
=> 'Maximum transfer unit of the interface. (lxc.network.mtu)',
730 minimum
=> 64, # minimum ethernet frame is 64 bytes
735 format
=> 'pve-ipv4-config',
736 format_description
=> '(IPv4/CIDR|dhcp|manual)',
737 description
=> 'IPv4 address in CIDR format.',
743 format_description
=> 'GatewayIPv4',
744 description
=> 'Default gateway for IPv4 traffic.',
749 format
=> 'pve-ipv6-config',
750 format_description
=> '(IPv6/CIDR|auto|dhcp|manual)',
751 description
=> 'IPv6 address in CIDR format.',
757 format_description
=> 'GatewayIPv6',
758 description
=> 'Default gateway for IPv6 traffic.',
763 description
=> "Controls whether this interface's firewall rules should be used.",
770 description
=> "VLAN tag for this interface.",
775 pattern
=> qr/\d+(?:;\d+)*/,
776 format_description
=> 'vlanid[;vlanid...]',
777 description
=> "VLAN ids to pass through the interface",
782 format_description
=> 'mbps',
783 description
=> "Apply rate limiting to the interface",
787 PVE
::JSONSchema
::register_format
('pve-lxc-network', $netconf_desc);
789 my $MAX_LXC_NETWORKS = 32;
790 for (my $i = 0; $i < $MAX_LXC_NETWORKS; $i++) {
791 $confdesc->{"net$i"} = {
793 type
=> 'string', format
=> $netconf_desc,
794 description
=> "Specifies network interfaces for the container.",
798 PVE
::JSONSchema
::register_format
('pve-ct-timezone', \
&verify_ct_timezone
);
799 sub verify_ct_timezone
{
800 my ($timezone, $noerr) = @_;
802 return if $timezone eq 'host'; # using host settings
804 PVE
::JSONSchema
::pve_verify_timezone
($timezone);
807 PVE
::JSONSchema
::register_format
('pve-lxc-mp-string', \
&verify_lxc_mp_string
);
808 sub verify_lxc_mp_string
{
809 my ($mp, $noerr) = @_;
813 # /. or /.. at the end
814 # ../ at the beginning
816 if($mp =~ m
@/\.\
.?
/@ ||
819 return undef if $noerr;
820 die "$mp contains illegal character sequences\n";
829 description
=> 'Whether to include the mount point in backups.',
830 verbose_description
=> 'Whether to include the mount point in backups '.
831 '(only used for volume mount points).',
836 format
=> 'pve-lxc-mp-string',
837 format_description
=> 'Path',
838 description
=> 'Path to the mount point as seen from inside the container '.
839 '(must not contain symlinks).',
840 verbose_description
=> "Path to the mount point as seen from inside the container.\n\n".
841 "NOTE: Must not contain any symlinks for security reasons."
844 PVE
::JSONSchema
::register_format
('pve-ct-mountpoint', $mp_desc);
850 format
=> 'pve-volume-id',
851 format_description
=> 'volume',
852 description
=> 'The volume that is not used currently.',
856 for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
857 $confdesc->{"mp$i"} = {
859 type
=> 'string', format
=> $mp_desc,
860 description
=> "Use volume as container mount point. Use the special " .
861 "syntax STORAGE_ID:SIZE_IN_GiB to allocate a new volume.",
866 for (my $i = 0; $i < $MAX_UNUSED_DISKS; $i++) {
867 $confdesc->{"unused$i"} = {
869 type
=> 'string', format
=> $unused_desc,
870 description
=> "Reference to unused volumes. This is used internally, and should not be modified manually.",
874 sub parse_pct_config
{
875 my ($filename, $raw) = @_;
877 return undef if !defined($raw);
880 digest
=> Digest
::SHA
::sha1_hex
($raw),
885 $filename =~ m
|/lxc/(\d
+).conf
$|
886 || die "got strange filename '$filename'";
894 my @lines = split(/\n/, $raw);
895 foreach my $line (@lines) {
896 next if $line =~ m/^\s*$/;
898 if ($line =~ m/^\[pve:pending\]\s*$/i) {
899 $section = 'pending';
900 $conf->{description
} = $descr if $descr;
902 $conf = $res->{$section} = {};
904 } elsif ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
906 $conf->{description
} = $descr if $descr;
908 $conf = $res->{snapshots
}->{$section} = {};
912 if ($line =~ m/^\#(.*)\s*$/) {
913 $descr .= PVE
::Tools
::decode_text
($1) . "\n";
917 if ($line =~ m/^(lxc\.[a-z0-9_\-\.]+)(:|\s*=)\s*(.*?)\s*$/) {
920 my $validity = is_valid_lxc_conf_key
($vmid, $key);
921 if ($validity eq 1) {
922 push @{$conf->{lxc
}}, [$key, $value];
923 } elsif (my $errmsg = $validity) {
924 warn "vm $vmid - $key: $errmsg\n";
926 warn "vm $vmid - unable to parse config: $line\n";
928 } elsif ($line =~ m/^(description):\s*(.*\S)\s*$/) {
929 $descr .= PVE
::Tools
::decode_text
($2);
930 } elsif ($line =~ m/snapstate:\s*(prepare|delete)\s*$/) {
931 $conf->{snapstate
} = $1;
932 } elsif ($line =~ m/^delete:\s*(.*\S)\s*$/) {
934 if ($section eq 'pending') {
935 $conf->{delete} = $value;
937 warn "vm $vmid - property 'delete' is only allowed in [pve:pending]\n";
939 } elsif ($line =~ m/^([a-z][a-z_]*\d*):\s*(.+?)\s*$/) {
942 eval { $value = PVE
::LXC
::Config-
>check_type($key, $value); };
943 warn "vm $vmid - unable to parse value of '$key' - $@" if $@;
944 $conf->{$key} = $value;
946 warn "vm $vmid - unable to parse config: $line\n";
950 $conf->{description
} = $descr if $descr;
952 delete $res->{snapstate
}; # just to be sure
957 sub write_pct_config
{
958 my ($filename, $conf) = @_;
960 delete $conf->{snapstate
}; # just to be sure
962 my $volidlist = PVE
::LXC
::Config-
>get_vm_volumes($conf);
963 my $used_volids = {};
964 foreach my $vid (@$volidlist) {
965 $used_volids->{$vid} = 1;
968 # remove 'unusedX' settings if the volume is still used
969 foreach my $key (keys %$conf) {
970 my $value = $conf->{$key};
971 if ($key =~ m/^unused/ && $used_volids->{$value}) {
972 delete $conf->{$key};
976 my $generate_raw_config = sub {
981 # add description as comment to top of file
982 my $descr = $conf->{description
} || '';
983 foreach my $cl (split(/\n/, $descr)) {
984 $raw .= '#' . PVE
::Tools
::encode_text
($cl) . "\n";
987 foreach my $key (sort keys %$conf) {
988 next if $key eq 'digest' || $key eq 'description' ||
989 $key eq 'pending' || $key eq 'snapshots' ||
990 $key eq 'snapname' || $key eq 'lxc';
991 my $value = $conf->{$key};
992 die "detected invalid newline inside property '$key'\n"
994 $raw .= "$key: $value\n";
997 if (my $lxcconf = $conf->{lxc
}) {
998 foreach my $entry (@$lxcconf) {
999 my ($k, $v) = @$entry;
1007 my $raw = &$generate_raw_config($conf);
1009 if (scalar(keys %{$conf->{pending
}})){
1010 $raw .= "\n[pve:pending]\n";
1011 $raw .= &$generate_raw_config($conf->{pending
});
1014 foreach my $snapname (sort keys %{$conf->{snapshots
}}) {
1015 $raw .= "\n[$snapname]\n";
1016 $raw .= &$generate_raw_config($conf->{snapshots
}->{$snapname});
1022 sub update_pct_config
{
1023 my ($class, $vmid, $conf, $running, $param, $delete, $revert) = @_;
1025 my $storage_cfg = PVE
::Storage
::config
();
1027 foreach my $opt (@$revert) {
1028 delete $conf->{pending
}->{$opt};
1029 $class->remove_from_pending_delete($conf, $opt); # also remove from deletion queue
1032 # write updates to pending section
1033 my $modified = {}; # record modified options
1035 foreach my $opt (@$delete) {
1036 if (!defined($conf->{$opt}) && !defined($conf->{pending
}->{$opt})) {
1037 warn "cannot delete '$opt' - not set in current configuration!\n";
1040 $modified->{$opt} = 1;
1041 if ($opt eq 'memory' || $opt eq 'rootfs' || $opt eq 'ostype') {
1042 die "unable to delete required option '$opt'\n";
1043 } elsif ($opt =~ m/^unused(\d+)$/) {
1044 $class->check_protection($conf, "can't remove CT $vmid drive '$opt'");
1045 } elsif ($opt =~ m/^mp(\d+)$/) {
1046 $class->check_protection($conf, "can't remove CT $vmid drive '$opt'");
1047 } elsif ($opt eq 'unprivileged') {
1048 die "unable to delete read-only option: '$opt'\n";
1050 $class->add_to_pending_delete($conf, $opt);
1053 my $check_content_type = sub {
1055 my $sid = PVE
::Storage
::parse_volume_id
($mp->{volume
});
1056 my $storage_config = PVE
::Storage
::storage_config
($storage_cfg, $sid);
1057 die "storage '$sid' does not allow content type 'rootdir' (Container)\n"
1058 if !$storage_config->{content
}->{rootdir
};
1061 foreach my $opt (sort keys %$param) { # add/change
1062 $modified->{$opt} = 1;
1063 my $value = $param->{$opt};
1064 if ($opt =~ m/^mp(\d+)$/ || $opt eq 'rootfs') {
1065 $class->check_protection($conf, "can't update CT $vmid drive '$opt'");
1066 my $mp = $class->parse_volume($opt, $value);
1067 $check_content_type->($mp) if ($mp->{type
} eq 'volume');
1068 } elsif ($opt eq 'hookscript') {
1069 PVE
::GuestHelpers
::check_hookscript
($value);
1070 } elsif ($opt eq 'nameserver') {
1071 $value = PVE
::LXC
::verify_nameserver_list
($value);
1072 } elsif ($opt eq 'searchdomain') {
1073 $value = PVE
::LXC
::verify_searchdomain_list
($value);
1074 } elsif ($opt eq 'unprivileged') {
1075 die "unable to modify read-only option: '$opt'\n";
1077 $conf->{pending
}->{$opt} = $value;
1078 $class->remove_from_pending_delete($conf, $opt);
1081 my $changes = $class->cleanup_pending($conf);
1085 $class->vmconfig_hotplug_pending($vmid, $conf, $storage_cfg, $modified, $errors);
1087 $class->vmconfig_apply_pending($vmid, $conf, $storage_cfg, $modified, $errors);
1094 my ($class, $key, $value) = @_;
1096 die "unknown setting '$key'\n" if !$confdesc->{$key};
1098 my $type = $confdesc->{$key}->{type
};
1100 if (!defined($value)) {
1101 die "got undefined value\n";
1104 if ($value =~ m/[\n\r]/) {
1105 die "property contains a line feed\n";
1108 if ($type eq 'boolean') {
1109 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
1110 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
1111 die "type check ('boolean') failed - got '$value'\n";
1112 } elsif ($type eq 'integer') {
1113 return int($1) if $value =~ m/^(\d+)$/;
1114 die "type check ('integer') failed - got '$value'\n";
1115 } elsif ($type eq 'number') {
1116 return $value if $value =~ m/^(\d+)(\.\d+)?$/;
1117 die "type check ('number') failed - got '$value'\n";
1118 } elsif ($type eq 'string') {
1119 if (my $fmt = $confdesc->{$key}->{format
}) {
1120 PVE
::JSONSchema
::check_format
($fmt, $value);
1125 die "internal error"
1130 # add JSON properties for create and set function
1131 sub json_config_properties
{
1132 my ($class, $prop) = @_;
1134 foreach my $opt (keys %$confdesc) {
1135 next if $opt eq 'parent' || $opt eq 'snaptime';
1136 next if $prop->{$opt};
1137 $prop->{$opt} = $confdesc->{$opt};
1143 my $parse_ct_mountpoint_full = sub {
1144 my ($class, $desc, $data, $noerr) = @_;
1149 eval { $res = PVE
::JSONSchema
::parse_property_string
($desc, $data) };
1151 return undef if $noerr;
1155 if (defined(my $size = $res->{size
})) {
1156 $size = PVE
::JSONSchema
::parse_size
($size);
1157 if (!defined($size)) {
1158 return undef if $noerr;
1159 die "invalid size: $size\n";
1161 $res->{size
} = $size;
1164 $res->{type
} = $class->classify_mountpoint($res->{volume
});
1169 sub print_ct_mountpoint
{
1170 my ($class, $info, $nomp) = @_;
1171 my $skip = [ 'type' ];
1172 push @$skip, 'mp' if $nomp;
1173 return PVE
::JSONSchema
::print_property_string
($info, $mp_desc, $skip);
1177 my ($class, $key, $volume_string, $noerr) = @_;
1179 if ($key eq 'rootfs') {
1180 my $res = $parse_ct_mountpoint_full->($class, $rootfs_desc, $volume_string, $noerr);
1181 $res->{mp
} = '/' if defined($res);
1183 } elsif ($key =~ m/^mp\d+$/) {
1184 return $parse_ct_mountpoint_full->($class, $mp_desc, $volume_string, $noerr);
1185 } elsif ($key =~ m/^unused\d+$/) {
1186 return $parse_ct_mountpoint_full->($class, $unused_desc, $volume_string, $noerr);
1189 die "parse_volume - unknown type: $key\n";
1193 my ($class, $key, $volume) = @_;
1195 return $class->print_ct_mountpoint($volume, $key eq 'rootfs');
1204 sub print_lxc_network
{
1205 my ($class, $net) = @_;
1206 return PVE
::JSONSchema
::print_property_string
($net, $netconf_desc);
1209 sub parse_lxc_network
{
1210 my ($class, $data) = @_;
1214 return $res if !$data;
1216 $res = PVE
::JSONSchema
::parse_property_string
($netconf_desc, $data);
1218 $res->{type
} = 'veth';
1219 if (!$res->{hwaddr
}) {
1220 my $dc = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
1221 $res->{hwaddr
} = PVE
::Tools
::random_ether_addr
($dc->{mac_prefix
});
1227 sub parse_features
{
1228 my ($class, $data) = @_;
1229 return {} if !$data;
1230 return PVE
::JSONSchema
::parse_property_string
($features_desc, $data);
1234 my ($class, $name) = @_;
1236 return defined($confdesc->{$name});
1238 # END JSON config code
1240 my $LXC_FASTPLUG_OPTIONS= {
1252 sub vmconfig_hotplug_pending
{
1253 my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_;
1255 my $pid = PVE
::LXC
::find_lxc_pid
($vmid);
1256 my $rootdir = "/proc/$pid/root";
1258 my $add_hotplug_error = sub {
1259 my ($opt, $msg) = @_;
1260 $errors->{$opt} = "unable to hotplug $opt: $msg";
1263 foreach my $opt (sort keys %{$conf->{pending
}}) { # add/change
1264 next if $selection && !$selection->{$opt};
1265 if ($LXC_FASTPLUG_OPTIONS->{$opt}) {
1266 $conf->{$opt} = delete $conf->{pending
}->{$opt};
1270 my $cgroup = PVE
::LXC
::CGroup-
>new($vmid);
1272 # There's no separate swap size to configure, there's memory and "total"
1273 # memory (iow. memory+swap). This means we have to change them together.
1274 my $hotplug_memory_done;
1275 my $hotplug_memory = sub {
1276 my ($wanted_memory, $wanted_swap) = @_;
1278 $wanted_memory = int($wanted_memory * 1024 * 1024) if defined($wanted_memory);
1279 $wanted_swap = int($wanted_swap * 1024 * 1024) if defined($wanted_swap);
1280 $cgroup->change_memory_limit($wanted_memory, $wanted_swap);
1282 $hotplug_memory_done = 1;
1285 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
1286 # FIXME: $force deletion is not implemented for CTs
1287 foreach my $opt (sort keys %$pending_delete_hash) {
1288 next if $selection && !$selection->{$opt};
1290 if ($LXC_FASTPLUG_OPTIONS->{$opt}) {
1292 } elsif ($opt =~ m/^unused(\d+)$/) {
1293 PVE
::LXC
::delete_mountpoint_volume
($storecfg, $vmid, $conf->{$opt})
1294 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1295 } elsif ($opt eq 'swap') {
1296 $hotplug_memory->(undef, 0);
1297 } elsif ($opt eq 'cpulimit') {
1298 $cgroup->change_cpu_quota(-1, 100000);
1299 } elsif ($opt eq 'cpuunits') {
1300 $cgroup->change_cpu_shares(undef, $confdesc->{cpuunits
}->{default});
1301 } elsif ($opt =~ m/^net(\d)$/) {
1303 PVE
::Network
::veth_delete
("veth${vmid}i$netid");
1305 die "skip\n"; # skip non-hotpluggable opts
1309 $add_hotplug_error->($opt, $err) if $err ne "skip\n";
1311 delete $conf->{$opt};
1312 $class->remove_from_pending_delete($conf, $opt);
1316 foreach my $opt (sort keys %{$conf->{pending
}}) {
1317 next if $opt eq 'delete'; # just to be sure
1318 next if $selection && !$selection->{$opt};
1319 my $value = $conf->{pending
}->{$opt};
1321 if ($opt eq 'cpulimit') {
1322 my $quota = 100000 * $value;
1323 $cgroup->change_cpu_quota(int(100000 * $value), 100000);
1324 } elsif ($opt eq 'cpuunits') {
1325 $cgroup->change_cpu_shares($value, $confdesc->{cpuunits
}->{default});
1326 } elsif ($opt =~ m/^net(\d+)$/) {
1328 my $net = $class->parse_lxc_network($value);
1329 $value = $class->print_lxc_network($net);
1330 PVE
::LXC
::update_net
($vmid, $conf, $opt, $net, $netid, $rootdir);
1331 } elsif ($opt eq 'memory' || $opt eq 'swap') {
1332 if (!$hotplug_memory_done) { # don't call twice if both opts are passed
1333 $hotplug_memory->($conf->{pending
}->{memory
}, $conf->{pending
}->{swap
});
1335 } elsif ($opt =~ m/^mp(\d+)$/) {
1336 if (!PVE
::LXC
::Tools
::can_use_new_mount_api
()) {
1340 if (exists($conf->{$opt})) {
1341 die "skip\n"; # don't try to hotplug over existing mp
1344 $class->apply_pending_mountpoint($vmid, $conf, $opt, $storecfg, 1);
1345 # apply_pending_mountpoint modifies the value if it creates a new disk
1346 $value = $conf->{pending
}->{$opt};
1348 die "skip\n"; # skip non-hotpluggable
1352 $add_hotplug_error->($opt, $err) if $err ne "skip\n";
1354 $conf->{$opt} = $value;
1355 delete $conf->{pending
}->{$opt};
1360 sub vmconfig_apply_pending
{
1361 my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_;
1363 my $add_apply_error = sub {
1364 my ($opt, $msg) = @_;
1365 my $err_msg = "unable to apply pending change $opt : $msg";
1366 $errors->{$opt} = $err_msg;
1370 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
1371 # FIXME: $force deletion is not implemented for CTs
1372 foreach my $opt (sort keys %$pending_delete_hash) {
1373 next if $selection && !$selection->{$opt};
1375 if ($opt =~ m/^mp(\d+)$/) {
1376 my $mp = $class->parse_volume($opt, $conf->{$opt});
1377 if ($mp->{type
} eq 'volume') {
1378 $class->add_unused_volume($conf, $mp->{volume
})
1379 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1381 } elsif ($opt =~ m/^unused(\d+)$/) {
1382 PVE
::LXC
::delete_mountpoint_volume
($storecfg, $vmid, $conf->{$opt})
1383 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1387 $add_apply_error->($opt, $err);
1389 delete $conf->{$opt};
1390 $class->remove_from_pending_delete($conf, $opt);
1394 $class->cleanup_pending($conf);
1396 foreach my $opt (sort keys %{$conf->{pending
}}) { # add/change
1397 next if $opt eq 'delete'; # just to be sure
1398 next if $selection && !$selection->{$opt};
1400 if ($opt =~ m/^mp(\d+)$/) {
1401 $class->apply_pending_mountpoint($vmid, $conf, $opt, $storecfg, 0);
1402 } elsif ($opt =~ m/^net(\d+)$/) {
1404 my $net = $class->parse_lxc_network($conf->{pending
}->{$opt});
1405 $conf->{pending
}->{$opt} = $class->print_lxc_network($net);
1409 $add_apply_error->($opt, $err);
1411 $conf->{$opt} = delete $conf->{pending
}->{$opt};
1416 my $rescan_volume = sub {
1417 my ($storecfg, $mp) = @_;
1419 $mp->{size
} = PVE
::Storage
::volume_size_info
($storecfg, $mp->{volume
}, 5);
1421 warn "Could not rescan volume size - $@\n" if $@;
1424 sub apply_pending_mountpoint
{
1425 my ($class, $vmid, $conf, $opt, $storecfg, $running) = @_;
1427 my $mp = $class->parse_volume($opt, $conf->{pending
}->{$opt});
1428 my $old = $conf->{$opt};
1429 if ($mp->{type
} eq 'volume') {
1430 if ($mp->{volume
} =~ $PVE::LXC
::NEW_DISK_RE
) {
1431 my $original_value = $conf->{pending
}->{$opt};
1432 my $vollist = PVE
::LXC
::create_disks
(
1435 { $opt => $original_value },
1440 # Re-parse mount point:
1441 my $mp = $class->parse_volume($opt, $conf->{pending
}->{$opt});
1443 PVE
::LXC
::mountpoint_hotplug
($vmid, $conf, $opt, $mp, $storecfg);
1447 PVE
::LXC
::destroy_disks
($storecfg, $vollist);
1448 # The pending-changes code collects errors but keeps on looping through further
1449 # pending changes, so unroll the change in $conf as well if destroy_disks()
1451 $conf->{pending
}->{$opt} = $original_value;
1456 die "skip\n" if $running && defined($old); # TODO: "changing" mount points?
1457 $rescan_volume->($storecfg, $mp);
1459 PVE
::LXC
::mountpoint_hotplug
($vmid, $conf, $opt, $mp, $storecfg);
1461 $conf->{pending
}->{$opt} = $class->print_ct_mountpoint($mp);
1465 if (defined($old)) {
1466 my $mp = $class->parse_volume($opt, $old);
1467 if ($mp->{type
} eq 'volume') {
1468 $class->add_unused_volume($conf, $mp->{volume
})
1469 if !$class->is_volume_in_use($conf, $conf->{$opt}, 1, 1);
1474 sub classify_mountpoint
{
1475 my ($class, $vol) = @_;
1476 if ($vol =~ m!^/!) {
1477 return 'device' if $vol =~ m!^/dev/!;
1483 my $__is_volume_in_use = sub {
1484 my ($class, $config, $volid) = @_;
1487 $class->foreach_volume($config, sub {
1488 my ($ms, $mountpoint) = @_;
1490 $used = $mountpoint->{type
} eq 'volume' && $mountpoint->{volume
} eq $volid;
1496 sub is_volume_in_use_by_snapshots
{
1497 my ($class, $config, $volid) = @_;
1499 if (my $snapshots = $config->{snapshots
}) {
1500 foreach my $snap (keys %$snapshots) {
1501 return 1 if $__is_volume_in_use->($class, $snapshots->{$snap}, $volid);
1508 sub is_volume_in_use
{
1509 my ($class, $config, $volid, $include_snapshots, $include_pending) = @_;
1510 return 1 if $__is_volume_in_use->($class, $config, $volid);
1511 return 1 if $include_snapshots && $class->is_volume_in_use_by_snapshots($config, $volid);
1512 return 1 if $include_pending && $__is_volume_in_use->($class, $config->{pending
}, $volid);
1516 sub has_dev_console
{
1517 my ($class, $conf) = @_;
1519 return !(defined($conf->{console
}) && !$conf->{console
});
1523 my ($class, $conf, $keyname) = @_;
1525 if (my $lxcconf = $conf->{lxc
}) {
1526 foreach my $entry (@$lxcconf) {
1527 my ($key, undef) = @$entry;
1528 return 1 if $key eq $keyname;
1536 my ($class, $conf) = @_;
1538 return $conf->{tty
} // $confdesc->{tty
}->{default};
1542 my ($class, $conf) = @_;
1544 return $conf->{cmode
} // $confdesc->{cmode
}->{default};
1547 sub valid_volume_keys
{
1548 my ($class, $reverse) = @_;
1550 my @names = ('rootfs');
1552 for (my $i = 0; $i < $MAX_MOUNT_POINTS; $i++) {
1553 push @names, "mp$i";
1556 return $reverse ?
reverse @names : @names;
1559 sub get_vm_volumes
{
1560 my ($class, $conf, $excludes) = @_;
1564 $class->foreach_volume($conf, sub {
1565 my ($ms, $mountpoint) = @_;
1567 return if $excludes && $ms eq $excludes;
1569 my $volid = $mountpoint->{volume
};
1570 return if !$volid || $mountpoint->{type
} ne 'volume';
1572 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
1575 push @$vollist, $volid;
1581 sub get_replicatable_volumes
{
1582 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
1586 my $test_volid = sub {
1587 my ($volid, $mountpoint) = @_;
1591 my $mptype = $mountpoint->{type
};
1592 my $replicate = $mountpoint->{replicate
} // 1;
1594 if ($mptype ne 'volume') {
1595 # skip bindmounts if replicate = 0 even for cleanup,
1596 # since bind mounts could not have been replicated ever
1597 return if !$replicate;
1598 die "unable to replicate mountpoint type '$mptype'\n";
1601 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, $noerr);
1602 return if !$storeid;
1604 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
1605 return if $scfg->{shared
};
1607 my ($path, $owner, $vtype) = PVE
::Storage
::path
($storecfg, $volid);
1608 return if !$owner || ($owner != $vmid);
1610 die "unable to replicate volume '$volid', type '$vtype'\n" if $vtype ne 'images';
1612 return if !$cleanup && !$replicate;
1614 if (!PVE
::Storage
::volume_has_feature
($storecfg, 'replicate', $volid)) {
1615 return if $cleanup || $noerr;
1616 die "missing replicate feature on volume '$volid'\n";
1619 $volhash->{$volid} = 1;
1622 $class->foreach_volume($conf, sub {
1623 my ($ms, $mountpoint) = @_;
1624 $test_volid->($mountpoint->{volume
}, $mountpoint);
1627 foreach my $snapname (keys %{$conf->{snapshots
}}) {
1628 my $snap = $conf->{snapshots
}->{$snapname};
1629 $class->foreach_volume($snap, sub {
1630 my ($ms, $mountpoint) = @_;
1631 $test_volid->($mountpoint->{volume
}, $mountpoint);
1635 # add 'unusedX' volumes to volhash
1636 foreach my $key (keys %$conf) {
1637 if ($key =~ m/^unused/) {
1638 $test_volid->($conf->{$key}, { type
=> 'volume', replicate
=> 1 });
1645 sub get_backup_volumes
{
1646 my ($class, $conf) = @_;
1648 my $return_volumes = [];
1650 my $test_mountpoint = sub {
1651 my ($key, $volume) = @_;
1653 my ($included, $reason) = $class->mountpoint_backup_enabled($key, $volume);
1655 push @$return_volumes, {
1657 included
=> $included,
1659 volume_config
=> $volume,
1663 PVE
::LXC
::Config-
>foreach_volume($conf, $test_mountpoint);
1665 return $return_volumes;