1 package PVE
::QemuServer
;
11 use File
::Copy
qw(copy);
24 use Storable
qw(dclone);
25 use Time
::HiRes
qw(gettimeofday usleep);
29 use PVE
::Cluster
qw(cfs_register_file cfs_read_file cfs_write_file);
32 use PVE
::DataCenterConfig
;
33 use PVE
::Exception
qw(raise raise_param_exc);
34 use PVE
::Format
qw(render_duration render_bytes);
35 use PVE
::GuestHelpers
qw(safe_string_ne safe_num_ne safe_boolean_ne);
37 use PVE
::JSONSchema
qw(get_standard_option parse_property_string);
40 use PVE
::RESTEnvironment
qw(log_warn);
41 use PVE
::RPCEnvironment
;
45 use PVE
::Tools
qw(run_command file_read_firstline file_get_contents dir_glob_foreach get_host_arch $IPV6RE);
49 use PVE
::QemuServer
::Helpers
qw(min_version config_aware_timeout windows_version);
50 use PVE
::QemuServer
::Cloudinit
;
51 use PVE
::QemuServer
::CGroup
;
52 use PVE
::QemuServer
::CPUConfig
qw(print_cpu_device get_cpu_options);
53 use PVE
::QemuServer
::Drive
qw(is_valid_drivename drive_is_cloudinit drive_is_cdrom drive_is_read_only parse_drive print_drive);
54 use PVE
::QemuServer
::Machine
;
55 use PVE
::QemuServer
::Memory
;
56 use PVE
::QemuServer
::Monitor
qw(mon_cmd);
57 use PVE
::QemuServer
::PCI
qw(print_pci_addr print_pcie_addr print_pcie_root_port parse_hostpci);
58 use PVE
::QemuServer
::USB
qw(parse_usb_device);
62 require PVE
::Network
::SDN
::Zones
;
66 my $EDK2_FW_BASE = '/usr/share/pve-edk2-firmware/';
70 "$EDK2_FW_BASE/OVMF_CODE_4M.fd",
71 "$EDK2_FW_BASE/OVMF_VARS_4M.fd",
74 "$EDK2_FW_BASE/OVMF_CODE_4M.fd",
75 "$EDK2_FW_BASE/OVMF_VARS_4M.ms.fd",
78 "$EDK2_FW_BASE/OVMF_CODE_4M.secboot.fd",
79 "$EDK2_FW_BASE/OVMF_VARS_4M.fd",
82 "$EDK2_FW_BASE/OVMF_CODE_4M.secboot.fd",
83 "$EDK2_FW_BASE/OVMF_VARS_4M.ms.fd",
86 "$EDK2_FW_BASE/OVMF_CODE.fd",
87 "$EDK2_FW_BASE/OVMF_VARS.fd",
92 "$EDK2_FW_BASE/AAVMF_CODE.fd",
93 "$EDK2_FW_BASE/AAVMF_VARS.fd",
98 my $cpuinfo = PVE
::ProcFSTools
::read_cpuinfo
();
100 # Note about locking: we use flock on the config file protect against concurent actions.
101 # Aditionaly, we have a 'lock' setting in the config file. This can be set to 'migrate',
102 # 'backup', 'snapshot' or 'rollback'. Most actions are not allowed when such lock is set.
103 # But you can ignore this kind of lock with the --skiplock flag.
105 cfs_register_file
('/qemu-server/',
109 PVE
::JSONSchema
::register_standard_option
('pve-qm-stateuri', {
110 description
=> "Some command save/restore state from this location.",
116 PVE
::JSONSchema
::register_standard_option
('pve-qemu-machine', {
117 description
=> "Specifies the Qemu machine type.",
119 pattern
=> '(pc|pc(-i440fx)?-\d+(\.\d+)+(\+pve\d+)?(\.pxe)?|q35|pc-q35-\d+(\.\d+)+(\+pve\d+)?(\.pxe)?|virt(?:-\d+(\.\d+)+)?(\+pve\d+)?)',
124 #no warnings 'redefine';
128 $nodename_cache //= PVE
::INotify
::nodename
();
129 return $nodename_cache;
136 enum
=> [qw(i6300esb ib700)],
137 description
=> "Watchdog type to emulate.",
138 default => 'i6300esb',
143 enum
=> [qw(reset shutdown poweroff pause debug none)],
144 description
=> "The action to perform if after activation the guest fails to poll the watchdog in time.",
148 PVE
::JSONSchema
::register_format
('pve-qm-watchdog', $watchdog_fmt);
152 description
=> "Enable/disable communication with a Qemu Guest Agent (QGA) running in the VM.",
157 fstrim_cloned_disks
=> {
158 description
=> "Run fstrim after moving a disk or migrating the VM.",
164 description
=> "Select the agent type",
168 enum
=> [qw(virtio isa)],
174 description
=> "Select the VGA type.",
179 enum
=> [qw(cirrus qxl qxl2 qxl3 qxl4 none serial0 serial1 serial2 serial3 std virtio virtio-gl vmware)],
182 description
=> "Sets the VGA memory (in MiB). Has no effect with serial display.",
194 description
=> "The size of the file in MB.",
198 pattern
=> '[a-zA-Z0-9\-]+',
200 format_description
=> 'string',
201 description
=> "The name of the file. Will be prefixed with 'pve-shm-'. Default is the VMID. Will be deleted when the VM is stopped.",
208 enum
=> [qw(ich9-intel-hda intel-hda AC97)],
209 description
=> "Configure an audio device."
213 enum
=> ['spice', 'none'],
216 description
=> "Driver backend for the audio device."
220 my $spice_enhancements_fmt = {
225 description
=> "Enable folder sharing via SPICE. Needs Spice-WebDAV daemon installed in the VM."
229 enum
=> ['off', 'all', 'filter'],
232 description
=> "Enable video streaming. Uses compression for detected video streams."
239 enum
=> ['/dev/urandom', '/dev/random', '/dev/hwrng'],
241 description
=> "The file on the host to gather entropy from. In most cases '/dev/urandom'"
242 ." should be preferred over '/dev/random' to avoid entropy-starvation issues on the"
243 ." host. Using urandom does *not* decrease security in any meaningful way, as it's"
244 ." still seeded from real entropy, and the bytes provided will most likely be mixed"
245 ." with real entropy on the guest as well. '/dev/hwrng' can be used to pass through"
246 ." a hardware RNG from the host.",
250 description
=> "Maximum bytes of entropy allowed to get injected into the guest every"
251 ." 'period' milliseconds. Prefer a lower value when using '/dev/random' as source. Use"
252 ." `0` to disable limiting (potentially dangerous!).",
255 # default is 1 KiB/s, provides enough entropy to the guest to avoid boot-starvation issues
256 # (e.g. systemd etc...) while allowing no chance of overwhelming the host, provided we're
257 # reading from /dev/urandom
262 description
=> "Every 'period' milliseconds the entropy-injection quota is reset, allowing"
263 ." the guest to retrieve another 'max_bytes' of entropy.",
269 my $meta_info_fmt = {
272 description
=> "The guest creation timestamp as UNIX epoch time",
278 description
=> "The QEMU (machine) version from the time this VM was created.",
279 pattern
=> '\d+(\.\d+)+',
288 description
=> "Specifies whether a VM will be started during system bootup.",
294 description
=> "Automatic restart after crash (currently ignored).",
299 type
=> 'string', format
=> 'pve-hotplug-features',
300 description
=> "Selectively enable hotplug features. This is a comma separated list of"
301 ." hotplug features: 'network', 'disk', 'cpu', 'memory', 'usb' and 'cloudinit'. Use '0' to disable"
302 ." hotplug completely. Using '1' as value is an alias for the default `network,disk,usb`.",
303 default => 'network,disk,usb',
308 description
=> "Allow reboot. If set to '0' the VM exit on reboot.",
314 description
=> "Lock/unlock the VM.",
315 enum
=> [qw(backup clone create migrate rollback snapshot snapshot-delete suspending suspended)],
320 description
=> "Limit of CPU usage.",
321 verbose_description
=> "Limit of CPU usage.\n\nNOTE: If the computer has 2 CPUs, it has"
322 ." total of '2' CPU time. Value '0' indicates no CPU limit.",
330 description
=> "CPU weight for a VM, will be clamped to [1, 10000] in cgroup v2.",
331 verbose_description
=> "CPU weight for a VM. Argument is used in the kernel fair scheduler."
332 ." The larger the number is, the more CPU time this VM gets. Number is relative to"
333 ." weights of all the other running VMs.",
336 default => 'cgroup v1: 1024, cgroup v2: 100',
341 description
=> "Amount of RAM for the VM in MB. This is the maximum available memory when"
342 ." you use the balloon device.",
349 description
=> "Amount of target RAM for the VM in MB. Using zero disables the ballon driver.",
355 description
=> "Amount of memory shares for auto-ballooning. The larger the number is, the"
356 ." more memory this VM gets. Number is relative to weights of all other running VMs."
357 ." Using zero disables auto-ballooning. Auto-ballooning is done by pvestatd.",
365 description
=> "Keyboard layout for VNC server. This option is generally not required and"
366 ." is often better handled from within the guest OS.",
367 enum
=> PVE
::Tools
::kvmkeymaplist
(),
372 type
=> 'string', format
=> 'dns-name',
373 description
=> "Set a name for the VM. Only used on the configuration web interface.",
378 description
=> "SCSI controller model",
379 enum
=> [qw(lsi lsi53c810 virtio-scsi-pci virtio-scsi-single megasas pvscsi)],
385 description
=> "Description for the VM. Shown in the web-interface VM's summary."
386 ." This is saved as comment inside the configuration file.",
387 maxLength
=> 1024 * 8,
392 enum
=> [qw(other wxp w2k w2k3 w2k8 wvista win7 win8 win10 win11 l24 l26 solaris)],
393 description
=> "Specify guest operating system.",
394 verbose_description
=> <<EODESC,
395 Specify guest operating system. This is used to enable special
396 optimization/features for specific operating systems:
399 other;; unspecified OS
400 wxp;; Microsoft Windows XP
401 w2k;; Microsoft Windows 2000
402 w2k3;; Microsoft Windows 2003
403 w2k8;; Microsoft Windows 2008
404 wvista;; Microsoft Windows Vista
405 win7;; Microsoft Windows 7
406 win8;; Microsoft Windows 8/2012/2012r2
407 win10;; Microsoft Windows 10/2016/2019
408 win11;; Microsoft Windows 11/2022
409 l24;; Linux 2.4 Kernel
410 l26;; Linux 2.6 - 5.X Kernel
411 solaris;; Solaris/OpenSolaris/OpenIndiania kernel
416 type
=> 'string', format
=> 'pve-qm-boot',
417 description
=> "Specify guest boot order. Use the 'order=' sub-property as usage with no"
418 ." key or 'legacy=' is deprecated.",
422 type
=> 'string', format
=> 'pve-qm-bootdisk',
423 description
=> "Enable booting from specified disk. Deprecated: Use 'boot: order=foo;bar' instead.",
424 pattern
=> '(ide|sata|scsi|virtio)\d+',
429 description
=> "The number of CPUs. Please use option -sockets instead.",
436 description
=> "The number of CPU sockets.",
443 description
=> "The number of cores per socket.",
450 description
=> "Enable/disable NUMA.",
456 description
=> "Enable/disable hugepages memory.",
457 enum
=> [qw(any 2 1024)],
463 description
=> "Use together with hugepages. If enabled, hugepages will not not be deleted"
464 ." after VM shutdown and can be used for subsequent starts.",
469 description
=> "Number of hotplugged vcpus.",
476 description
=> "Enable/disable ACPI.",
481 description
=> "Enable/disable communication with the Qemu Guest Agent and its properties.",
483 format
=> $agent_fmt,
488 description
=> "Enable/disable KVM hardware virtualization.",
494 description
=> "Enable/disable time drift fix.",
500 description
=> "Set the real time clock (RTC) to local time. This is enabled by default if"
501 ." the `ostype` indicates a Microsoft Windows OS.",
506 description
=> "Freeze CPU at startup (use 'c' monitor command to start execution).",
510 type
=> 'string', format
=> $vga_fmt,
511 description
=> "Configure the VGA hardware.",
512 verbose_description
=> "Configure the VGA Hardware. If you want to use high resolution"
513 ." modes (>= 1280x1024x16) you may need to increase the vga memory option. Since QEMU"
514 ." 2.9 the default VGA display type is 'std' for all OS types besides some Windows"
515 ." versions (XP and older) which use 'cirrus'. The 'qxl' option enables the SPICE"
516 ." display server. For win* OS you can select how many independent displays you want,"
517 ." Linux guests can add displays them self.\nYou can also run without any graphic card,"
518 ." using a serial device as terminal.",
522 type
=> 'string', format
=> 'pve-qm-watchdog',
523 description
=> "Create a virtual hardware watchdog device.",
524 verbose_description
=> "Create a virtual hardware watchdog device. Once enabled (by a guest"
525 ." action), the watchdog must be periodically polled by an agent inside the guest or"
526 ." else the watchdog will reset the guest (or execute the respective action specified)",
531 typetext
=> "(now | YYYY-MM-DD | YYYY-MM-DDTHH:MM:SS)",
532 description
=> "Set the initial date of the real time clock. Valid format for date are:"
533 ."'now' or '2006-06-17T16:01:21' or '2006-06-17'.",
534 pattern
=> '(now|\d{4}-\d{1,2}-\d{1,2}(T\d{1,2}:\d{1,2}:\d{1,2})?)',
537 startup
=> get_standard_option
('pve-startup-order'),
541 description
=> "Enable/disable Template.",
547 description
=> "Arbitrary arguments passed to kvm.",
548 verbose_description
=> <<EODESCR,
549 Arbitrary arguments passed to kvm, for example:
551 args: -no-reboot -no-hpet
553 NOTE: this option is for experts only.
560 description
=> "Enable/disable the USB tablet device.",
561 verbose_description
=> "Enable/disable the USB tablet device. This device is usually needed"
562 ." to allow absolute mouse positioning with VNC. Else the mouse runs out of sync with"
563 ." normal VNC clients. If you're running lots of console-only guests on one host, you"
564 ." may consider disabling this to save some context switches. This is turned off by"
565 ." default if you use spice (`qm set <vmid> --vga qxl`).",
570 description
=> "Set maximum speed (in MB/s) for migrations. Value 0 is no limit.",
574 migrate_downtime
=> {
577 description
=> "Set maximum tolerated downtime (in seconds) for migrations.",
583 type
=> 'string', format
=> 'pve-qm-ide',
584 typetext
=> '<volume>',
585 description
=> "This is an alias for option -ide2",
589 description
=> "Emulated CPU type.",
591 format
=> 'pve-vm-cpu-conf',
593 parent
=> get_standard_option
('pve-snapshot-name', {
595 description
=> "Parent snapshot name. This is used internally, and should not be modified.",
599 description
=> "Timestamp for snapshots.",
605 type
=> 'string', format
=> 'pve-volume-id',
606 description
=> "Reference to a volume which stores the VM state. This is used internally"
609 vmstatestorage
=> get_standard_option
('pve-storage-id', {
610 description
=> "Default storage for VM state volumes/files.",
613 runningmachine
=> get_standard_option
('pve-qemu-machine', {
614 description
=> "Specifies the QEMU machine type of the running vm. This is used internally"
618 description
=> "Specifies the QEMU '-cpu' parameter of the running vm. This is used"
619 ." internally for snapshots.",
622 pattern
=> $PVE::QemuServer
::CPUConfig
::qemu_cmdline_cpu_re
,
623 format_description
=> 'QEMU -cpu parameter'
625 machine
=> get_standard_option
('pve-qemu-machine'),
627 description
=> "Virtual processor architecture. Defaults to the host.",
630 enum
=> [qw(x86_64 aarch64)],
633 description
=> "Specify SMBIOS type 1 fields.",
634 type
=> 'string', format
=> 'pve-qm-smbios1',
641 description
=> "Sets the protection flag of the VM. This will disable the remove VM and"
642 ." remove disk operations.",
648 enum
=> [ qw(seabios ovmf) ],
649 description
=> "Select BIOS implementation.",
650 default => 'seabios',
654 pattern
=> '(?:[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}|[01])',
655 format_description
=> 'UUID',
656 description
=> "Set VM Generation ID. Use '1' to autogenerate on create or update, pass '0'"
657 ." to disable explicitly.",
658 verbose_description
=> "The VM generation ID (vmgenid) device exposes a 128-bit integer"
659 ." value identifier to the guest OS. This allows to notify the guest operating system"
660 ." when the virtual machine is executed with a different configuration (e.g. snapshot"
661 ." execution or creation from a template). The guest operating system notices the"
662 ." change, and is then able to react as appropriate by marking its copies of"
663 ." distributed databases as dirty, re-initializing its random number generator, etc.\n"
664 ."Note that auto-creation only works when done through API/CLI create or update methods"
665 .", but not when manually editing the config file.",
666 default => "1 (autogenerated)",
671 format
=> 'pve-volume-id',
673 description
=> "Script that will be executed during various steps in the vms lifetime.",
677 format
=> $ivshmem_fmt,
678 description
=> "Inter-VM shared memory. Useful for direct communication between VMs, or to"
684 format
=> $audio_fmt,
685 description
=> "Configure a audio device, useful in combination with QXL/Spice.",
688 spice_enhancements
=> {
690 format
=> $spice_enhancements_fmt,
691 description
=> "Configure additional enhancements for SPICE.",
695 type
=> 'string', format
=> 'pve-tag-list',
696 description
=> 'Tags of the VM. This is only meta information.',
702 description
=> "Configure a VirtIO-based Random Number Generator.",
707 format
=> $meta_info_fmt,
708 description
=> "Some (read-only) meta-information about this guest.",
712 type
=> 'string', format
=> 'pve-cpuset',
713 description
=> "List of host cores used to execute guest processes.",
722 description
=> 'Specify a custom file containing all meta data passed to the VM via"
723 ." cloud-init. This is provider specific meaning configdrive2 and nocloud differ.',
724 format
=> 'pve-volume-id',
725 format_description
=> 'volume',
730 description
=> 'Specify a custom file containing all network data passed to the VM via'
732 format
=> 'pve-volume-id',
733 format_description
=> 'volume',
738 description
=> 'Specify a custom file containing all user data passed to the VM via'
740 format
=> 'pve-volume-id',
741 format_description
=> 'volume',
746 description
=> 'Specify a custom file containing all vendor data passed to the VM via'
748 format
=> 'pve-volume-id',
749 format_description
=> 'volume',
752 PVE
::JSONSchema
::register_format
('pve-qm-cicustom', $cicustom_fmt);
754 my $confdesc_cloudinit = {
758 description
=> 'Specifies the cloud-init configuration format. The default depends on the'
759 .' configured operating system type (`ostype`. We use the `nocloud` format for Linux,'
760 .' and `configdrive2` for windows.',
761 enum
=> ['configdrive2', 'nocloud', 'opennebula'],
766 description
=> "cloud-init: User name to change ssh keys and password for instead of the"
767 ." image's configured default user.",
772 description
=> 'cloud-init: Password to assign the user. Using this is generally not'
773 .' recommended. Use ssh keys instead. Also note that older cloud-init versions do not'
774 .' support hashed passwords.',
779 description
=> 'cloud-init: Specify custom files to replace the automatically generated'
781 format
=> 'pve-qm-cicustom',
786 description
=> 'cloud-init: Sets DNS search domains for a container. Create will'
787 .' automatically use the setting from the host if neither searchdomain nor nameserver'
792 type
=> 'string', format
=> 'address-list',
793 description
=> 'cloud-init: Sets DNS server IP address for a container. Create will'
794 .' automatically use the setting from the host if neither searchdomain nor nameserver'
800 format
=> 'urlencoded',
801 description
=> "cloud-init: Setup public SSH keys (one key per line, OpenSSH format).",
805 # what about other qemu settings ?
807 #machine => 'string',
820 ##soundhw => 'string',
822 while (my ($k, $v) = each %$confdesc) {
823 PVE
::JSONSchema
::register_standard_option
("pve-qm-$k", $v);
826 my $MAX_USB_DEVICES = 5;
828 my $MAX_SERIAL_PORTS = 4;
829 my $MAX_PARALLEL_PORTS = 3;
835 pattern
=> qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
836 description
=> "CPUs accessing this NUMA node.",
837 format_description
=> "id[-id];...",
841 description
=> "Amount of memory this NUMA node provides.",
846 pattern
=> qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
847 description
=> "Host NUMA nodes to use.",
848 format_description
=> "id[-id];...",
853 enum
=> [qw(preferred bind interleave)],
854 description
=> "NUMA allocation policy.",
858 PVE
::JSONSchema
::register_format
('pve-qm-numanode', $numa_fmt);
861 type
=> 'string', format
=> $numa_fmt,
862 description
=> "NUMA topology.",
864 PVE
::JSONSchema
::register_standard_option
("pve-qm-numanode", $numadesc);
866 for (my $i = 0; $i < $MAX_NUMA; $i++) {
867 $confdesc->{"numa$i"} = $numadesc;
870 my $nic_model_list = [
886 my $nic_model_list_txt = join(' ', sort @$nic_model_list);
888 my $net_fmt_bridge_descr = <<__EOD__;
889 Bridge to attach the network device to. The Proxmox VE standard bridge
892 If you do not specify a bridge, we create a kvm user (NATed) network
893 device, which provides DHCP and DNS services. The following addresses
900 The DHCP server assign addresses to the guest starting from 10.0.2.15.
904 macaddr
=> get_standard_option
('mac-addr', {
905 description
=> "MAC address. That address must be unique withing your network. This is"
906 ." automatically generated if not specified.",
910 description
=> "Network Card Model. The 'virtio' model provides the best performance with"
911 ." very low CPU overhead. If your guest does not support this driver, it is usually"
912 ." best to use 'e1000'.",
913 enum
=> $nic_model_list,
916 (map { $_ => { keyAlias
=> 'model', alias
=> 'macaddr' }} @$nic_model_list),
917 bridge
=> get_standard_option
('pve-bridge-id', {
918 description
=> $net_fmt_bridge_descr,
923 minimum
=> 0, maximum
=> 16,
924 description
=> 'Number of packet queues to be used on the device.',
930 description
=> "Rate limit in mbps (megabytes per second) as floating point number.",
935 minimum
=> 1, maximum
=> 4094,
936 description
=> 'VLAN tag to apply to packets on this interface.',
941 pattern
=> qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
942 description
=> 'VLAN trunks to pass through this interface.',
943 format_description
=> 'vlanid[;vlanid...]',
948 description
=> 'Whether this interface should be protected by the firewall.',
953 description
=> 'Whether this interface should be disconnected (like pulling the plug).',
958 minimum
=> 1, maximum
=> 65520,
959 description
=> "Force MTU, for VirtIO only. Set to '1' to use the bridge MTU",
966 type
=> 'string', format
=> $net_fmt,
967 description
=> "Specify network devices.",
970 PVE
::JSONSchema
::register_standard_option
("pve-qm-net", $netdesc);
975 format
=> 'pve-ipv4-config',
976 format_description
=> 'IPv4Format/CIDR',
977 description
=> 'IPv4 address in CIDR format.',
984 format_description
=> 'GatewayIPv4',
985 description
=> 'Default gateway for IPv4 traffic.',
991 format
=> 'pve-ipv6-config',
992 format_description
=> 'IPv6Format/CIDR',
993 description
=> 'IPv6 address in CIDR format.',
1000 format_description
=> 'GatewayIPv6',
1001 description
=> 'Default gateway for IPv6 traffic.',
1006 PVE
::JSONSchema
::register_format
('pve-qm-ipconfig', $ipconfig_fmt);
1007 my $ipconfigdesc = {
1009 type
=> 'string', format
=> 'pve-qm-ipconfig',
1010 description
=> <<'EODESCR',
1011 cloud-init: Specify IP addresses and gateways for the corresponding interface.
1013 IP addresses use CIDR notation, gateways are optional but need an IP of the same type specified.
1015 The special string 'dhcp' can be used for IP addresses to use DHCP, in which case no explicit
1016 gateway should be provided.
1017 For IPv6 the special string 'auto' can be used to use stateless autoconfiguration. This requires
1018 cloud-init 19.4 or newer.
1020 If cloud-init is enabled and neither an IPv4 nor an IPv6 address is specified, it defaults to using
1024 PVE
::JSONSchema
::register_standard_option
("pve-qm-ipconfig", $netdesc);
1026 for (my $i = 0; $i < $MAX_NETS; $i++) {
1027 $confdesc->{"net$i"} = $netdesc;
1028 $confdesc_cloudinit->{"ipconfig$i"} = $ipconfigdesc;
1031 foreach my $key (keys %$confdesc_cloudinit) {
1032 $confdesc->{$key} = $confdesc_cloudinit->{$key};
1035 PVE
::JSONSchema
::register_format
('pve-cpuset', \
&pve_verify_cpuset
);
1036 sub pve_verify_cpuset
{
1037 my ($set_text, $noerr) = @_;
1039 my ($count, $members) = eval { PVE
::CpuSet
::parse_cpuset
($set_text) };
1043 die "unable to parse cpuset option\n";
1046 return PVE
::CpuSet-
>new($members)->short_string();
1049 PVE
::JSONSchema
::register_format
('pve-volume-id-or-qm-path', \
&verify_volume_id_or_qm_path
);
1050 sub verify_volume_id_or_qm_path
{
1051 my ($volid, $noerr) = @_;
1053 return $volid if $volid eq 'none' || $volid eq 'cdrom';
1055 return verify_volume_id_or_absolute_path
($volid, $noerr);
1058 PVE
::JSONSchema
::register_format
('pve-volume-id-or-absolute-path', \
&verify_volume_id_or_absolute_path
);
1059 sub verify_volume_id_or_absolute_path
{
1060 my ($volid, $noerr) = @_;
1062 return $volid if $volid =~ m
|^/|;
1064 $volid = eval { PVE
::JSONSchema
::check_format
('pve-volume-id', $volid, '') };
1075 type
=> 'string', format
=> 'pve-qm-usb-device',
1076 format_description
=> 'HOSTUSBDEVICE|spice',
1077 description
=> <<EODESCR,
1078 The Host USB device or port or the value 'spice'. HOSTUSBDEVICE syntax is:
1080 'bus-port(.port)*' (decimal numbers) or
1081 'vendor_id:product_id' (hexadeciaml numbers) or
1084 You can use the 'lsusb -t' command to list existing usb devices.
1086 NOTE: This option allows direct access to host hardware. So it is no longer possible to migrate such
1087 machines - use with special care.
1089 The value 'spice' can be used to add a usb redirection devices for spice.
1095 description
=> "Specifies whether if given host option is a USB3 device or port.",
1102 type
=> 'string', format
=> $usb_fmt,
1103 description
=> "Configure an USB device (n is 0 to 4).",
1105 PVE
::JSONSchema
::register_standard_option
("pve-qm-usb", $usbdesc);
1110 pattern
=> '(/dev/.+|socket)',
1111 description
=> "Create a serial device inside the VM (n is 0 to 3)",
1112 verbose_description
=> <<EODESCR,
1113 Create a serial device inside the VM (n is 0 to 3), and pass through a
1114 host serial device (i.e. /dev/ttyS0), or create a unix socket on the
1115 host side (use 'qm terminal' to open a terminal connection).
1117 NOTE: If you pass through a host serial device, it is no longer possible to migrate such machines -
1118 use with special care.
1120 CAUTION: Experimental! User reported problems with this option.
1127 pattern
=> '/dev/parport\d+|/dev/usb/lp\d+',
1128 description
=> "Map host parallel devices (n is 0 to 2).",
1129 verbose_description
=> <<EODESCR,
1130 Map host parallel devices (n is 0 to 2).
1132 NOTE: This option allows direct access to host hardware. So it is no longer possible to migrate such
1133 machines - use with special care.
1135 CAUTION: Experimental! User reported problems with this option.
1139 for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
1140 $confdesc->{"parallel$i"} = $paralleldesc;
1143 for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
1144 $confdesc->{"serial$i"} = $serialdesc;
1147 for (my $i = 0; $i < $PVE::QemuServer
::PCI
::MAX_HOSTPCI_DEVICES
; $i++) {
1148 $confdesc->{"hostpci$i"} = $PVE::QemuServer
::PCI
::hostpcidesc
;
1151 for my $key (keys %{$PVE::QemuServer
::Drive
::drivedesc_hash
}) {
1152 $confdesc->{$key} = $PVE::QemuServer
::Drive
::drivedesc_hash-
>{$key};
1155 for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
1156 $confdesc->{"usb$i"} = $usbdesc;
1164 description
=> "Boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)."
1165 . " Deprecated, use 'order=' instead.",
1166 pattern
=> '[acdn]{1,4}',
1167 format_description
=> "[acdn]{1,4}",
1169 # note: this is also the fallback if boot: is not given at all
1175 format
=> 'pve-qm-bootdev-list',
1176 format_description
=> "device[;device...]",
1177 description
=> <<EODESC,
1178 The guest will attempt to boot from devices in the order they appear here.
1180 Disks, optical drives and passed-through storage USB devices will be directly
1181 booted from, NICs will load PXE, and PCIe devices will either behave like disks
1182 (e.g. NVMe) or load an option ROM (e.g. RAID controller, hardware NIC).
1184 Note that only devices in this list will be marked as bootable and thus loaded
1185 by the guest firmware (BIOS/UEFI). If you require multiple disks for booting
1186 (e.g. software-raid), you need to specify all of them here.
1188 Overrides the deprecated 'legacy=[acdn]*' value when given.
1192 PVE
::JSONSchema
::register_format
('pve-qm-boot', $boot_fmt);
1194 PVE
::JSONSchema
::register_format
('pve-qm-bootdev', \
&verify_bootdev
);
1195 sub verify_bootdev
{
1196 my ($dev, $noerr) = @_;
1198 my $special = $dev =~ m/^efidisk/ || $dev =~ m/^tpmstate/;
1199 return $dev if PVE
::QemuServer
::Drive
::is_valid_drivename
($dev) && !$special;
1203 return 0 if $dev !~ m/^$base\d+$/;
1204 return 0 if !$confdesc->{$dev};
1208 return $dev if $check->("net");
1209 return $dev if $check->("usb");
1210 return $dev if $check->("hostpci");
1213 die "invalid boot device '$dev'\n";
1216 sub print_bootorder
{
1218 return "" if !@$devs;
1219 my $data = { order
=> join(';', @$devs) };
1220 return PVE
::JSONSchema
::print_property_string
($data, $boot_fmt);
1223 my $kvm_api_version = 0;
1226 return $kvm_api_version if $kvm_api_version;
1228 open my $fh, '<', '/dev/kvm' or return;
1230 # 0xae00 => KVM_GET_API_VERSION
1231 $kvm_api_version = ioctl($fh, 0xae00, 0);
1234 return $kvm_api_version;
1237 my $kvm_user_version = {};
1240 sub kvm_user_version
{
1243 $binary //= get_command_for_arch
(get_host_arch
()); # get the native arch by default
1244 my $st = stat($binary);
1246 my $cachedmtime = $kvm_mtime->{$binary} // -1;
1247 return $kvm_user_version->{$binary} if $kvm_user_version->{$binary} &&
1248 $cachedmtime == $st->mtime;
1250 $kvm_user_version->{$binary} = 'unknown';
1251 $kvm_mtime->{$binary} = $st->mtime;
1255 if ($line =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?)(\.\d+)?[,\s]/) {
1256 $kvm_user_version->{$binary} = $2;
1260 eval { run_command
([$binary, '--version'], outfunc
=> $code); };
1263 return $kvm_user_version->{$binary};
1266 my sub extract_version
{
1267 my ($machine_type, $version) = @_;
1268 $version = kvm_user_version
() if !defined($version);
1269 return PVE
::QemuServer
::Machine
::extract_version
($machine_type, $version)
1272 sub kernel_has_vhost_net
{
1273 return -c
'/dev/vhost-net';
1278 return defined($confdesc->{$key});
1282 sub get_cdrom_path
{
1284 return $cdrom_path if $cdrom_path;
1286 return $cdrom_path = "/dev/cdrom" if -l
"/dev/cdrom";
1287 return $cdrom_path = "/dev/cdrom1" if -l
"/dev/cdrom1";
1288 return $cdrom_path = "/dev/cdrom2" if -l
"/dev/cdrom2";
1292 my ($storecfg, $vmid, $cdrom) = @_;
1294 if ($cdrom eq 'cdrom') {
1295 return get_cdrom_path
();
1296 } elsif ($cdrom eq 'none') {
1298 } elsif ($cdrom =~ m
|^/|) {
1301 return PVE
::Storage
::path
($storecfg, $cdrom);
1305 # try to convert old style file names to volume IDs
1306 sub filename_to_volume_id
{
1307 my ($vmid, $file, $media) = @_;
1309 if (!($file eq 'none' || $file eq 'cdrom' ||
1310 $file =~ m
|^/dev/.+| || $file =~ m/^([^:]+):(.+)$/)) {
1312 return if $file =~ m
|/|;
1314 if ($media && $media eq 'cdrom') {
1315 $file = "local:iso/$file";
1317 $file = "local:$vmid/$file";
1324 sub verify_media_type
{
1325 my ($opt, $vtype, $media) = @_;
1330 if ($media eq 'disk') {
1332 } elsif ($media eq 'cdrom') {
1335 die "internal error";
1338 return if ($vtype eq $etype);
1340 raise_param_exc
({ $opt => "unexpected media type ($vtype != $etype)" });
1343 sub cleanup_drive_path
{
1344 my ($opt, $storecfg, $drive) = @_;
1346 # try to convert filesystem paths to volume IDs
1348 if (($drive->{file
} !~ m/^(cdrom|none)$/) &&
1349 ($drive->{file
} !~ m
|^/dev/.+|) &&
1350 ($drive->{file
} !~ m/^([^:]+):(.+)$/) &&
1351 ($drive->{file
} !~ m/^\d+$/)) {
1352 my ($vtype, $volid) = PVE
::Storage
::path_to_volume_id
($storecfg, $drive->{file
});
1353 raise_param_exc
({ $opt => "unable to associate path '$drive->{file}' to any storage"})
1355 $drive->{media
} = 'cdrom' if !$drive->{media
} && $vtype eq 'iso';
1356 verify_media_type
($opt, $vtype, $drive->{media
});
1357 $drive->{file
} = $volid;
1360 $drive->{media
} = 'cdrom' if !$drive->{media
} && $drive->{file
} =~ m/^(cdrom|none)$/;
1363 sub parse_hotplug_features
{
1368 return $res if $data eq '0';
1370 $data = $confdesc->{hotplug
}->{default} if $data eq '1';
1372 foreach my $feature (PVE
::Tools
::split_list
($data)) {
1373 if ($feature =~ m/^(network|disk|cpu|memory|usb|cloudinit)$/) {
1376 die "invalid hotplug feature '$feature'\n";
1382 PVE
::JSONSchema
::register_format
('pve-hotplug-features', \
&pve_verify_hotplug_features
);
1383 sub pve_verify_hotplug_features
{
1384 my ($value, $noerr) = @_;
1386 return $value if parse_hotplug_features
($value);
1390 die "unable to parse hotplug option\n";
1394 my($fh, $noerr) = @_;
1397 my $SG_GET_VERSION_NUM = 0x2282;
1399 my $versionbuf = "\x00" x
8;
1400 my $ret = ioctl($fh, $SG_GET_VERSION_NUM, $versionbuf);
1402 die "scsi ioctl SG_GET_VERSION_NUM failoed - $!\n" if !$noerr;
1405 my $version = unpack("I", $versionbuf);
1406 if ($version < 30000) {
1407 die "scsi generic interface too old\n" if !$noerr;
1411 my $buf = "\x00" x
36;
1412 my $sensebuf = "\x00" x
8;
1413 my $cmd = pack("C x3 C x1", 0x12, 36);
1415 # see /usr/include/scsi/sg.h
1416 my $sg_io_hdr_t = "i i C C s I P P P I I i P C C C C S S i I I";
1419 $sg_io_hdr_t, ord('S'), -3, length($cmd), length($sensebuf), 0, length($buf), $buf, $cmd, $sensebuf, 6000
1422 $ret = ioctl($fh, $SG_IO, $packet);
1424 die "scsi ioctl SG_IO failed - $!\n" if !$noerr;
1428 my @res = unpack($sg_io_hdr_t, $packet);
1429 if ($res[17] || $res[18]) {
1430 die "scsi ioctl SG_IO status error - $!\n" if !$noerr;
1435 $res->@{qw(type removable vendor product revision)} = unpack("C C x6 A8 A16 A4", $buf);
1437 $res->{removable
} = $res->{removable
} & 128 ?
1 : 0;
1438 $res->{type
} &= 0x1F;
1446 my $fh = IO
::File-
>new("+<$path") || return;
1447 my $res = scsi_inquiry
($fh, 1);
1453 sub print_tabletdevice_full
{
1454 my ($conf, $arch) = @_;
1456 my $q35 = PVE
::QemuServer
::Machine
::machine_type_is_q35
($conf);
1458 # we use uhci for old VMs because tablet driver was buggy in older qemu
1460 if ($q35 || $arch eq 'aarch64') {
1466 return "usb-tablet,id=tablet,bus=$usbbus.0,port=1";
1469 sub print_keyboarddevice_full
{
1470 my ($conf, $arch) = @_;
1472 return if $arch ne 'aarch64';
1474 return "usb-kbd,id=keyboard,bus=ehci.0,port=2";
1477 my sub get_drive_id
{
1479 return "$drive->{interface}$drive->{index}";
1482 sub print_drivedevice_full
{
1483 my ($storecfg, $conf, $vmid, $drive, $bridges, $arch, $machine_type) = @_;
1488 my $drive_id = get_drive_id
($drive);
1489 if ($drive->{interface
} eq 'virtio') {
1490 my $pciaddr = print_pci_addr
("$drive_id", $bridges, $arch, $machine_type);
1491 $device = "virtio-blk-pci,drive=drive-$drive_id,id=${drive_id}${pciaddr}";
1492 $device .= ",iothread=iothread-$drive_id" if $drive->{iothread
};
1493 } elsif ($drive->{interface
} eq 'scsi') {
1495 my ($maxdev, $controller, $controller_prefix) = scsihw_infos
($conf, $drive);
1496 my $unit = $drive->{index} % $maxdev;
1497 my $devicetype = 'hd';
1499 if (drive_is_cdrom
($drive)) {
1502 if ($drive->{file
} =~ m
|^/|) {
1503 $path = $drive->{file
};
1504 if (my $info = path_is_scsi
($path)) {
1505 if ($info->{type
} == 0 && $drive->{scsiblock
}) {
1506 $devicetype = 'block';
1507 } elsif ($info->{type
} == 1) { # tape
1508 $devicetype = 'generic';
1512 $path = PVE
::Storage
::path
($storecfg, $drive->{file
});
1515 # for compatibility only, we prefer scsi-hd (#2408, #2355, #2380)
1516 my $version = extract_version
($machine_type, kvm_user_version
());
1517 if ($path =~ m/^iscsi\:\/\
// &&
1518 !min_version
($version, 4, 1)) {
1519 $devicetype = 'generic';
1523 if (!$conf->{scsihw
} || $conf->{scsihw
} =~ m/^lsi/ || $conf->{scsihw
} eq 'pvscsi') {
1524 $device = "scsi-$devicetype,bus=$controller_prefix$controller.0,scsi-id=$unit";
1526 $device = "scsi-$devicetype,bus=$controller_prefix$controller.0,channel=0,scsi-id=0"
1527 .",lun=$drive->{index}";
1529 $device .= ",drive=drive-$drive_id,id=$drive_id";
1531 if ($drive->{ssd
} && ($devicetype eq 'block' || $devicetype eq 'hd')) {
1532 $device .= ",rotation_rate=1";
1534 $device .= ",wwn=$drive->{wwn}" if $drive->{wwn
};
1536 } elsif ($drive->{interface
} eq 'ide' || $drive->{interface
} eq 'sata') {
1537 my $maxdev = ($drive->{interface
} eq 'sata') ?
$PVE::QemuServer
::Drive
::MAX_SATA_DISKS
: 2;
1538 my $controller = int($drive->{index} / $maxdev);
1539 my $unit = $drive->{index} % $maxdev;
1540 my $devicetype = ($drive->{media
} && $drive->{media
} eq 'cdrom') ?
"cd" : "hd";
1542 $device = "ide-$devicetype";
1543 if ($drive->{interface
} eq 'ide') {
1544 $device .= ",bus=ide.$controller,unit=$unit";
1546 $device .= ",bus=ahci$controller.$unit";
1548 $device .= ",drive=drive-$drive_id,id=$drive_id";
1550 if ($devicetype eq 'hd') {
1551 if (my $model = $drive->{model
}) {
1552 $model = URI
::Escape
::uri_unescape
($model);
1553 $device .= ",model=$model";
1555 if ($drive->{ssd
}) {
1556 $device .= ",rotation_rate=1";
1559 $device .= ",wwn=$drive->{wwn}" if $drive->{wwn
};
1560 } elsif ($drive->{interface
} eq 'usb') {
1562 # -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0
1564 die "unsupported interface type";
1567 $device .= ",bootindex=$drive->{bootindex}" if $drive->{bootindex
};
1569 if (my $serial = $drive->{serial
}) {
1570 $serial = URI
::Escape
::uri_unescape
($serial);
1571 $device .= ",serial=$serial";
1578 sub get_initiator_name
{
1581 my $fh = IO
::File-
>new('/etc/iscsi/initiatorname.iscsi') || return;
1582 while (defined(my $line = <$fh>)) {
1583 next if $line !~ m/^\s*InitiatorName\s*=\s*([\.\-:\w]+)/;
1592 sub print_drive_commandline_full
{
1593 my ($storecfg, $vmid, $drive, $pbs_name, $io_uring) = @_;
1596 my $volid = $drive->{file
};
1597 my $format = $drive->{format
};
1598 my $drive_id = get_drive_id
($drive);
1600 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
1601 my $scfg = $storeid ? PVE
::Storage
::storage_config
($storecfg, $storeid) : undef;
1603 if (drive_is_cdrom
($drive)) {
1604 $path = get_iso_path
($storecfg, $vmid, $volid);
1605 die "$drive_id: cannot back cdrom drive with PBS snapshot\n" if $pbs_name;
1608 $path = PVE
::Storage
::path
($storecfg, $volid);
1609 $format //= qemu_img_format
($scfg, $volname);
1616 my $is_rbd = $path =~ m/^rbd:/;
1619 my @qemu_drive_options = qw(heads secs cyls trans media cache rerror werror aio discard);
1620 foreach my $o (@qemu_drive_options) {
1621 $opts .= ",$o=$drive->{$o}" if defined($drive->{$o});
1624 # snapshot only accepts on|off
1625 if (defined($drive->{snapshot
})) {
1626 my $v = $drive->{snapshot
} ?
'on' : 'off';
1627 $opts .= ",snapshot=$v";
1630 if (defined($drive->{ro
})) { # ro maps to QEMUs `readonly`, which accepts `on` or `off` only
1631 $opts .= ",readonly=" . ($drive->{ro
} ?
'on' : 'off');
1634 foreach my $type (['', '-total'], [_rd
=> '-read'], [_wr
=> '-write']) {
1635 my ($dir, $qmpname) = @$type;
1636 if (my $v = $drive->{"mbps$dir"}) {
1637 $opts .= ",throttling.bps$qmpname=".int($v*1024*1024);
1639 if (my $v = $drive->{"mbps${dir}_max"}) {
1640 $opts .= ",throttling.bps$qmpname-max=".int($v*1024*1024);
1642 if (my $v = $drive->{"bps${dir}_max_length"}) {
1643 $opts .= ",throttling.bps$qmpname-max-length=$v";
1645 if (my $v = $drive->{"iops${dir}"}) {
1646 $opts .= ",throttling.iops$qmpname=$v";
1648 if (my $v = $drive->{"iops${dir}_max"}) {
1649 $opts .= ",throttling.iops$qmpname-max=$v";
1651 if (my $v = $drive->{"iops${dir}_max_length"}) {
1652 $opts .= ",throttling.iops$qmpname-max-length=$v";
1657 $format = "rbd" if $is_rbd;
1658 die "$drive_id: Proxmox Backup Server backed drive cannot auto-detect the format\n"
1660 $opts .= ",format=alloc-track,file.driver=$format";
1662 $opts .= ",format=$format";
1665 my $cache_direct = 0;
1667 if (my $cache = $drive->{cache
}) {
1668 $cache_direct = $cache =~ /^(?:off|none|directsync)$/;
1669 } elsif (!drive_is_cdrom
($drive) && !($scfg && $scfg->{type
} eq 'btrfs' && !$scfg->{nocow
})) {
1670 $opts .= ",cache=none";
1674 # io_uring with cache mode writeback or writethrough on krbd will hang...
1675 my $rbd_no_io_uring = $scfg && $scfg->{type
} eq 'rbd' && $scfg->{krbd
} && !$cache_direct;
1677 # io_uring with cache mode writeback or writethrough on LVM will hang, without cache only
1678 # sometimes, just plain disable...
1679 my $lvm_no_io_uring = $scfg && $scfg->{type
} eq 'lvm';
1681 # io_uring causes problems when used with CIFS since kernel 5.15
1682 # Some discussion: https://www.spinics.net/lists/linux-cifs/msg26734.html
1683 my $cifs_no_io_uring = $scfg && $scfg->{type
} eq 'cifs';
1685 if (!$drive->{aio
}) {
1686 if ($io_uring && !$rbd_no_io_uring && !$lvm_no_io_uring && !$cifs_no_io_uring) {
1687 # io_uring supports all cache modes
1688 $opts .= ",aio=io_uring";
1690 # aio native works only with O_DIRECT
1692 $opts .= ",aio=native";
1694 $opts .= ",aio=threads";
1699 if (!drive_is_cdrom
($drive)) {
1701 if (defined($drive->{detect_zeroes
}) && !$drive->{detect_zeroes
}) {
1702 $detectzeroes = 'off';
1703 } elsif ($drive->{discard
}) {
1704 $detectzeroes = $drive->{discard
} eq 'on' ?
'unmap' : 'on';
1706 # This used to be our default with discard not being specified:
1707 $detectzeroes = 'on';
1710 # note: 'detect-zeroes' works per blockdev and we want it to persist
1711 # after the alloc-track is removed, so put it on 'file' directly
1712 my $dz_param = $pbs_name ?
"file.detect-zeroes" : "detect-zeroes";
1713 $opts .= ",$dz_param=$detectzeroes" if $detectzeroes;
1717 $opts .= ",backing=$pbs_name";
1718 $opts .= ",auto-remove=on";
1721 # my $file_param = $pbs_name ? "file.file.filename" : "file";
1722 my $file_param = "file";
1724 # non-rbd drivers require the underlying file to be a seperate block
1725 # node, so add a second .file indirection
1726 $file_param .= ".file" if !$is_rbd;
1727 $file_param .= ".filename";
1729 my $pathinfo = $path ?
"$file_param=$path," : '';
1731 return "${pathinfo}if=none,id=drive-$drive->{interface}$drive->{index}$opts";
1734 sub print_pbs_blockdev
{
1735 my ($pbs_conf, $pbs_name) = @_;
1736 my $blockdev = "driver=pbs,node-name=$pbs_name,read-only=on";
1737 $blockdev .= ",repository=$pbs_conf->{repository}";
1738 $blockdev .= ",namespace=$pbs_conf->{namespace}" if $pbs_conf->{namespace
};
1739 $blockdev .= ",snapshot=$pbs_conf->{snapshot}";
1740 $blockdev .= ",archive=$pbs_conf->{archive}";
1741 $blockdev .= ",keyfile=$pbs_conf->{keyfile}" if $pbs_conf->{keyfile
};
1745 sub print_netdevice_full
{
1746 my ($vmid, $conf, $net, $netid, $bridges, $use_old_bios_files, $arch, $machine_type) = @_;
1748 my $device = $net->{model
};
1749 if ($net->{model
} eq 'virtio') {
1750 $device = 'virtio-net-pci';
1753 my $pciaddr = print_pci_addr
("$netid", $bridges, $arch, $machine_type);
1754 my $tmpstr = "$device,mac=$net->{macaddr},netdev=$netid$pciaddr,id=$netid";
1755 if ($net->{queues
} && $net->{queues
} > 1 && $net->{model
} eq 'virtio'){
1756 # Consider we have N queues, the number of vectors needed is 2 * N + 2, i.e., one per in
1757 # and out of each queue plus one config interrupt and control vector queue
1758 my $vectors = $net->{queues
} * 2 + 2;
1759 $tmpstr .= ",vectors=$vectors,mq=on";
1761 $tmpstr .= ",bootindex=$net->{bootindex}" if $net->{bootindex
} ;
1763 if (my $mtu = $net->{mtu
}) {
1764 if ($net->{model
} eq 'virtio' && $net->{bridge
}) {
1765 my $bridge_mtu = PVE
::Network
::read_bridge_mtu
($net->{bridge
});
1768 } elsif ($mtu < 576) {
1769 die "netdev $netid: MTU '$mtu' is smaller than the IP minimum MTU '576'\n";
1770 } elsif ($mtu > $bridge_mtu) {
1771 die "netdev $netid: MTU '$mtu' is bigger than the bridge MTU '$bridge_mtu'\n";
1773 $tmpstr .= ",host_mtu=$mtu";
1775 warn "WARN: netdev $netid: ignoring MTU '$mtu', not using VirtIO or no bridge configured.\n";
1779 if ($use_old_bios_files) {
1781 if ($device eq 'virtio-net-pci') {
1782 $romfile = 'pxe-virtio.rom';
1783 } elsif ($device eq 'e1000') {
1784 $romfile = 'pxe-e1000.rom';
1785 } elsif ($device eq 'e1000e') {
1786 $romfile = 'pxe-e1000e.rom';
1787 } elsif ($device eq 'ne2k') {
1788 $romfile = 'pxe-ne2k_pci.rom';
1789 } elsif ($device eq 'pcnet') {
1790 $romfile = 'pxe-pcnet.rom';
1791 } elsif ($device eq 'rtl8139') {
1792 $romfile = 'pxe-rtl8139.rom';
1794 $tmpstr .= ",romfile=$romfile" if $romfile;
1800 sub print_netdev_full
{
1801 my ($vmid, $conf, $arch, $net, $netid, $hotplug) = @_;
1804 if ($netid =~ m/^net(\d+)$/) {
1808 die "got strange net id '$i'\n" if $i >= ${MAX_NETS
};
1810 my $ifname = "tap${vmid}i$i";
1812 # kvm uses TUNSETIFF ioctl, and that limits ifname length
1813 die "interface name '$ifname' is too long (max 15 character)\n"
1814 if length($ifname) >= 16;
1816 my $vhostparam = '';
1817 if (is_native
($arch)) {
1818 $vhostparam = ',vhost=on' if kernel_has_vhost_net
() && $net->{model
} eq 'virtio';
1821 my $vmname = $conf->{name
} || "vm$vmid";
1824 my $script = $hotplug ?
"pve-bridge-hotplug" : "pve-bridge";
1826 if ($net->{bridge
}) {
1827 $netdev = "type=tap,id=$netid,ifname=${ifname},script=/var/lib/qemu-server/$script"
1828 .",downscript=/var/lib/qemu-server/pve-bridgedown$vhostparam";
1830 $netdev = "type=user,id=$netid,hostname=$vmname";
1833 $netdev .= ",queues=$net->{queues}" if ($net->{queues
} && $net->{model
} eq 'virtio');
1839 'cirrus' => 'cirrus-vga',
1841 'vmware' => 'vmware-svga',
1842 'virtio' => 'virtio-vga',
1843 'virtio-gl' => 'virtio-vga-gl',
1846 sub print_vga_device
{
1847 my ($conf, $vga, $arch, $machine_version, $machine, $id, $qxlnum, $bridges) = @_;
1849 my $type = $vga_map->{$vga->{type
}};
1850 if ($arch eq 'aarch64' && defined($type) && $type eq 'virtio-vga') {
1851 $type = 'virtio-gpu';
1853 my $vgamem_mb = $vga->{memory
};
1855 my $max_outputs = '';
1857 $type = $id ?
'qxl' : 'qxl-vga';
1859 if (!$conf->{ostype
} || $conf->{ostype
} =~ m/^(?:l\d\d)|(?:other)$/) {
1860 # set max outputs so linux can have up to 4 qxl displays with one device
1861 if (min_version
($machine_version, 4, 1)) {
1862 $max_outputs = ",max_outputs=4";
1867 die "no devicetype for $vga->{type}\n" if !$type;
1871 if ($vga->{type
} =~ /^virtio/) {
1872 my $bytes = PVE
::Tools
::convert_size
($vgamem_mb, "mb" => "b");
1873 $memory = ",max_hostmem=$bytes";
1875 # from https://www.spice-space.org/multiple-monitors.html
1876 $memory = ",vgamem_mb=$vga->{memory}";
1877 my $ram = $vgamem_mb * 4;
1878 my $vram = $vgamem_mb * 2;
1879 $memory .= ",ram_size_mb=$ram,vram_size_mb=$vram";
1881 $memory = ",vgamem_mb=$vga->{memory}";
1883 } elsif ($qxlnum && $id) {
1884 $memory = ",ram_size=67108864,vram_size=33554432";
1888 if ($type eq 'VGA' && windows_version
($conf->{ostype
})) {
1889 $edidoff=",edid=off" if (!defined($conf->{bios
}) || $conf->{bios
} ne 'ovmf');
1892 my $q35 = PVE
::QemuServer
::Machine
::machine_type_is_q35
($conf);
1893 my $vgaid = "vga" . ($id // '');
1895 if ($q35 && $vgaid eq 'vga') {
1896 # the first display uses pcie.0 bus on q35 machines
1897 $pciaddr = print_pcie_addr
($vgaid, $bridges, $arch, $machine);
1899 $pciaddr = print_pci_addr
($vgaid, $bridges, $arch, $machine);
1902 if ($vga->{type
} eq 'virtio-gl') {
1903 my $base = '/usr/lib/x86_64-linux-gnu/lib';
1904 die "missing libraries for '$vga->{type}' detected! Please install 'libgl1' and 'libegl1'\n"
1905 if !-e
"${base}EGL.so.1" || !-e
"${base}GL.so.1";
1907 die "no DRM render node detected (/dev/dri/renderD*), no GPU? - needed for '$vga->{type}' display\n"
1908 if !PVE
::Tools
::dir_glob_regex
('/dev/dri/', "renderD.*");
1911 return "$type,id=${vgaid}${memory}${max_outputs}${pciaddr}${edidoff}";
1914 sub parse_number_sets
{
1917 foreach my $part (split(/;/, $set)) {
1918 if ($part =~ /^\s*(\d+)(?:-(\d+))?\s*$/) {
1919 die "invalid range: $part ($2 < $1)\n" if defined($2) && $2 < $1;
1920 push @$res, [ $1, $2 ];
1922 die "invalid range: $part\n";
1931 my $res = parse_property_string
($numa_fmt, $data);
1932 $res->{cpus
} = parse_number_sets
($res->{cpus
}) if defined($res->{cpus
});
1933 $res->{hostnodes
} = parse_number_sets
($res->{hostnodes
}) if defined($res->{hostnodes
});
1937 # netX: e1000=XX:XX:XX:XX:XX:XX,bridge=vmbr0,rate=<mbps>
1941 my $res = eval { parse_property_string
($net_fmt, $data) };
1946 if (!defined($res->{macaddr
})) {
1947 my $dc = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
1948 $res->{macaddr
} = PVE
::Tools
::random_ether_addr
($dc->{mac_prefix
});
1953 # ipconfigX ip=cidr,gw=ip,ip6=cidr,gw6=ip
1954 sub parse_ipconfig
{
1957 my $res = eval { parse_property_string
($ipconfig_fmt, $data) };
1963 if ($res->{gw
} && !$res->{ip
}) {
1964 warn 'gateway specified without specifying an IP address';
1967 if ($res->{gw6
} && !$res->{ip6
}) {
1968 warn 'IPv6 gateway specified without specifying an IPv6 address';
1971 if ($res->{gw
} && $res->{ip
} eq 'dhcp') {
1972 warn 'gateway specified together with DHCP';
1975 if ($res->{gw6
} && $res->{ip6
} !~ /^$IPV6RE/) {
1977 warn "IPv6 gateway specified together with $res->{ip6} address";
1981 if (!$res->{ip
} && !$res->{ip6
}) {
1982 return { ip
=> 'dhcp', ip6
=> 'dhcp' };
1991 return PVE
::JSONSchema
::print_property_string
($net, $net_fmt);
1994 sub add_random_macs
{
1995 my ($settings) = @_;
1997 foreach my $opt (keys %$settings) {
1998 next if $opt !~ m/^net(\d+)$/;
1999 my $net = parse_net
($settings->{$opt});
2001 $settings->{$opt} = print_net
($net);
2005 sub vm_is_volid_owner
{
2006 my ($storecfg, $vmid, $volid) = @_;
2008 if ($volid !~ m
|^/|) {
2010 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
2011 if ($owner && ($owner == $vmid)) {
2019 sub vmconfig_register_unused_drive
{
2020 my ($storecfg, $vmid, $conf, $drive) = @_;
2022 if (drive_is_cloudinit
($drive)) {
2023 eval { PVE
::Storage
::vdisk_free
($storecfg, $drive->{file
}) };
2025 delete $conf->{cloudinit
};
2026 } elsif (!drive_is_cdrom
($drive)) {
2027 my $volid = $drive->{file
};
2028 if (vm_is_volid_owner
($storecfg, $vmid, $volid)) {
2029 PVE
::QemuConfig-
>add_unused_volume($conf, $volid, $vmid);
2034 # smbios: [manufacturer=str][,product=str][,version=str][,serial=str][,uuid=uuid][,sku=str][,family=str][,base64=bool]
2038 pattern
=> '[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}',
2039 format_description
=> 'UUID',
2040 description
=> "Set SMBIOS1 UUID.",
2045 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2046 format_description
=> 'Base64 encoded string',
2047 description
=> "Set SMBIOS1 version.",
2052 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2053 format_description
=> 'Base64 encoded string',
2054 description
=> "Set SMBIOS1 serial number.",
2059 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2060 format_description
=> 'Base64 encoded string',
2061 description
=> "Set SMBIOS1 manufacturer.",
2066 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2067 format_description
=> 'Base64 encoded string',
2068 description
=> "Set SMBIOS1 product ID.",
2073 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2074 format_description
=> 'Base64 encoded string',
2075 description
=> "Set SMBIOS1 SKU string.",
2080 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2081 format_description
=> 'Base64 encoded string',
2082 description
=> "Set SMBIOS1 family string.",
2087 description
=> 'Flag to indicate that the SMBIOS values are base64 encoded',
2095 my $res = eval { parse_property_string
($smbios1_fmt, $data) };
2102 return PVE
::JSONSchema
::print_property_string
($smbios1, $smbios1_fmt);
2105 PVE
::JSONSchema
::register_format
('pve-qm-smbios1', $smbios1_fmt);
2107 sub parse_watchdog
{
2112 my $res = eval { parse_property_string
($watchdog_fmt, $value) };
2117 sub parse_guest_agent
{
2120 return {} if !defined($conf->{agent
});
2122 my $res = eval { parse_property_string
($agent_fmt, $conf->{agent
}) };
2125 # if the agent is disabled ignore the other potentially set properties
2126 return {} if !$res->{enabled
};
2131 my ($conf, $key) = @_;
2132 return undef if !defined($conf->{agent
});
2134 my $agent = parse_guest_agent
($conf);
2135 return $agent->{$key};
2141 return {} if !$value;
2142 my $res = eval { parse_property_string
($vga_fmt, $value) };
2152 my $res = eval { parse_property_string
($rng_fmt, $value) };
2157 sub parse_meta_info
{
2162 my $res = eval { parse_property_string
($meta_info_fmt, $value) };
2167 sub new_meta_info_string
{
2168 my () = @_; # for now do not allow to override any value
2170 return PVE
::JSONSchema
::print_property_string
(
2172 'creation-qemu' => kvm_user_version
(),
2173 ctime
=> "". int(time()),
2179 sub qemu_created_version_fixups
{
2180 my ($conf, $forcemachine, $kvmver) = @_;
2182 my $meta = parse_meta_info
($conf->{meta
}) // {};
2183 my $forced_vers = PVE
::QemuServer
::Machine
::extract_version
($forcemachine);
2185 # check if we need to apply some handling for VMs that always use the latest machine version but
2186 # had a machine version transition happen that affected HW such that, e.g., an OS config change
2187 # would be required (we do not want to pin machine version for non-windows OS type)
2189 (!defined($conf->{machine
}) || $conf->{machine
} =~ m/^(?:pc|q35|virt)$/) # non-versioned machine
2190 && (!defined($meta->{'creation-qemu'}) || !min_version
($meta->{'creation-qemu'}, 6, 1)) # created before 6.1
2191 && (!$forced_vers || min_version
($forced_vers, 6, 1)) # handle snapshot-rollback/migrations
2192 && min_version
($kvmver, 6, 1) # only need to apply the change since 6.1
2194 my $q35 = PVE
::QemuServer
::Machine
::machine_type_is_q35
($conf);
2195 if ($q35 && $conf->{ostype
} && $conf->{ostype
} eq 'l26') {
2196 # this changed to default-on in Q 6.1 for q35 machines, it will mess with PCI slot view
2197 # and thus with the predictable interface naming of systemd
2198 return ['-global', 'ICH9-LPC.acpi-pci-hotplug-with-bridge-support=off'];
2204 PVE
::JSONSchema
::register_format
('pve-qm-usb-device', \
&verify_usb_device
);
2205 sub verify_usb_device
{
2206 my ($value, $noerr) = @_;
2208 return $value if parse_usb_device
($value);
2212 die "unable to parse usb device\n";
2215 # add JSON properties for create and set function
2216 sub json_config_properties
{
2217 my ($prop, $with_disk_alloc) = @_;
2219 my $skip_json_config_opts = {
2223 runningmachine
=> 1,
2228 foreach my $opt (keys %$confdesc) {
2229 next if $skip_json_config_opts->{$opt};
2231 if ($with_disk_alloc && is_valid_drivename
($opt)) {
2232 $prop->{$opt} = $PVE::QemuServer
::Drive
::drivedesc_hash_with_alloc-
>{$opt};
2234 $prop->{$opt} = $confdesc->{$opt};
2241 # Properties that we can read from an OVF file
2242 sub json_ovf_properties
{
2245 for my $device (PVE
::QemuServer
::Drive
::valid_drive_names
()) {
2246 $prop->{$device} = {
2248 format
=> 'pve-volume-id-or-absolute-path',
2249 description
=> "Disk image that gets imported to $device",
2256 description
=> "The number of CPU cores.",
2261 description
=> "Amount of RAM for the VM in MB.",
2266 description
=> "Name of the VM.",
2273 # return copy of $confdesc_cloudinit to generate documentation
2274 sub cloudinit_config_properties
{
2276 return dclone
($confdesc_cloudinit);
2280 my ($key, $value) = @_;
2282 die "unknown setting '$key'\n" if !$confdesc->{$key};
2284 my $type = $confdesc->{$key}->{type
};
2286 if (!defined($value)) {
2287 die "got undefined value\n";
2290 if ($value =~ m/[\n\r]/) {
2291 die "property contains a line feed\n";
2294 if ($type eq 'boolean') {
2295 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
2296 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
2297 die "type check ('boolean') failed - got '$value'\n";
2298 } elsif ($type eq 'integer') {
2299 return int($1) if $value =~ m/^(\d+)$/;
2300 die "type check ('integer') failed - got '$value'\n";
2301 } elsif ($type eq 'number') {
2302 return $value if $value =~ m/^(\d+)(\.\d+)?$/;
2303 die "type check ('number') failed - got '$value'\n";
2304 } elsif ($type eq 'string') {
2305 if (my $fmt = $confdesc->{$key}->{format
}) {
2306 PVE
::JSONSchema
::check_format
($fmt, $value);
2309 $value =~ s/^\"(.*)\"$/$1/;
2312 die "internal error"
2317 my ($storecfg, $vmid, $skiplock, $replacement_conf, $purge_unreferenced) = @_;
2319 my $conf = PVE
::QemuConfig-
>load_config($vmid);
2321 PVE
::QemuConfig-
>check_lock($conf) if !$skiplock;
2323 if ($conf->{template
}) {
2324 # check if any base image is still used by a linked clone
2325 PVE
::QemuConfig-
>foreach_volume_full($conf, { include_unused
=> 1 }, sub {
2326 my ($ds, $drive) = @_;
2327 return if drive_is_cdrom
($drive);
2329 my $volid = $drive->{file
};
2330 return if !$volid || $volid =~ m
|^/|;
2332 die "base volume '$volid' is still in use by linked cloned\n"
2333 if PVE
::Storage
::volume_is_base_and_used
($storecfg, $volid);
2339 my $remove_owned_drive = sub {
2340 my ($ds, $drive) = @_;
2341 return if drive_is_cdrom
($drive, 1);
2343 my $volid = $drive->{file
};
2344 return if !$volid || $volid =~ m
|^/|;
2345 return if $volids->{$volid};
2347 my ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid);
2348 return if !$path || !$owner || ($owner != $vmid);
2350 $volids->{$volid} = 1;
2351 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid) };
2352 warn "Could not remove disk '$volid', check manually: $@" if $@;
2355 # only remove disks owned by this VM (referenced in the config)
2356 my $include_opts = {
2357 include_unused
=> 1,
2358 extra_keys
=> ['vmstate'],
2360 PVE
::QemuConfig-
>foreach_volume_full($conf, $include_opts, $remove_owned_drive);
2362 for my $snap (values %{$conf->{snapshots
}}) {
2363 next if !defined($snap->{vmstate
});
2364 my $drive = PVE
::QemuConfig-
>parse_volume('vmstate', $snap->{vmstate
}, 1);
2365 next if !defined($drive);
2366 $remove_owned_drive->('vmstate', $drive);
2369 PVE
::QemuConfig-
>foreach_volume_full($conf->{pending
}, $include_opts, $remove_owned_drive);
2371 if ($purge_unreferenced) { # also remove unreferenced disk
2372 my $vmdisks = PVE
::Storage
::vdisk_list
($storecfg, undef, $vmid, undef, 'images');
2373 PVE
::Storage
::foreach_volid
($vmdisks, sub {
2374 my ($volid, $sid, $volname, $d) = @_;
2375 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid) };
2380 if (defined $replacement_conf) {
2381 PVE
::QemuConfig-
>write_config($vmid, $replacement_conf);
2383 PVE
::QemuConfig-
>destroy_config($vmid);
2387 sub parse_vm_config
{
2388 my ($filename, $raw, $strict) = @_;
2390 return if !defined($raw);
2393 digest
=> Digest
::SHA
::sha1_hex
($raw),
2399 my $handle_error = sub {
2409 $filename =~ m
|/qemu-server/(\d
+)\
.conf
$|
2410 || die "got strange filename '$filename'";
2418 my @lines = split(/\n/, $raw);
2419 foreach my $line (@lines) {
2420 next if $line =~ m/^\s*$/;
2422 if ($line =~ m/^\[PENDING\]\s*$/i) {
2423 $section = 'pending';
2424 if (defined($descr)) {
2426 $conf->{description
} = $descr;
2429 $conf = $res->{$section} = {};
2431 } elsif ($line =~ m/^\[special:cloudinit\]\s*$/i) {
2432 $section = 'cloudinit';
2434 $conf = $res->{$section} = {};
2437 } elsif ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
2439 if (defined($descr)) {
2441 $conf->{description
} = $descr;
2444 $conf = $res->{snapshots
}->{$section} = {};
2448 if ($line =~ m/^\#(.*)$/) {
2449 $descr = '' if !defined($descr);
2450 $descr .= PVE
::Tools
::decode_text
($1) . "\n";
2454 if ($line =~ m/^(description):\s*(.*\S)\s*$/) {
2455 $descr = '' if !defined($descr);
2456 $descr .= PVE
::Tools
::decode_text
($2);
2457 } elsif ($line =~ m/snapstate:\s*(prepare|delete)\s*$/) {
2458 $conf->{snapstate
} = $1;
2459 } elsif ($line =~ m/^(args):\s*(.*\S)\s*$/) {
2462 $conf->{$key} = $value;
2463 } elsif ($line =~ m/^delete:\s*(.*\S)\s*$/) {
2465 if ($section eq 'pending') {
2466 $conf->{delete} = $value; # we parse this later
2468 $handle_error->("vm $vmid - property 'delete' is only allowed in [PENDING]\n");
2470 } elsif ($line =~ m/^([a-z][a-z_]*\d*):\s*(.+?)\s*$/) {
2473 eval { $value = check_type
($key, $value); };
2475 $handle_error->("vm $vmid - unable to parse value of '$key' - $@");
2477 $key = 'ide2' if $key eq 'cdrom';
2478 my $fmt = $confdesc->{$key}->{format
};
2479 if ($fmt && $fmt =~ /^pve-qm-(?:ide|scsi|virtio|sata)$/) {
2480 my $v = parse_drive
($key, $value);
2481 if (my $volid = filename_to_volume_id
($vmid, $v->{file
}, $v->{media
})) {
2482 $v->{file
} = $volid;
2483 $value = print_drive
($v);
2485 $handle_error->("vm $vmid - unable to parse value of '$key'\n");
2490 $conf->{$key} = $value;
2493 $handle_error->("vm $vmid - unable to parse config: $line\n");
2497 if (defined($descr)) {
2499 $conf->{description
} = $descr;
2501 delete $res->{snapstate
}; # just to be sure
2506 sub write_vm_config
{
2507 my ($filename, $conf) = @_;
2509 delete $conf->{snapstate
}; # just to be sure
2511 if ($conf->{cdrom
}) {
2512 die "option ide2 conflicts with cdrom\n" if $conf->{ide2
};
2513 $conf->{ide2
} = $conf->{cdrom
};
2514 delete $conf->{cdrom
};
2517 # we do not use 'smp' any longer
2518 if ($conf->{sockets
}) {
2519 delete $conf->{smp
};
2520 } elsif ($conf->{smp
}) {
2521 $conf->{sockets
} = $conf->{smp
};
2522 delete $conf->{cores
};
2523 delete $conf->{smp
};
2526 my $used_volids = {};
2528 my $cleanup_config = sub {
2529 my ($cref, $pending, $snapname) = @_;
2531 foreach my $key (keys %$cref) {
2532 next if $key eq 'digest' || $key eq 'description' || $key eq 'snapshots' ||
2533 $key eq 'snapstate' || $key eq 'pending' || $key eq 'cloudinit';
2534 my $value = $cref->{$key};
2535 if ($key eq 'delete') {
2536 die "propertry 'delete' is only allowed in [PENDING]\n"
2538 # fixme: check syntax?
2541 eval { $value = check_type
($key, $value); };
2542 die "unable to parse value of '$key' - $@" if $@;
2544 $cref->{$key} = $value;
2546 if (!$snapname && is_valid_drivename
($key)) {
2547 my $drive = parse_drive
($key, $value);
2548 $used_volids->{$drive->{file
}} = 1 if $drive && $drive->{file
};
2553 &$cleanup_config($conf);
2555 &$cleanup_config($conf->{pending
}, 1);
2557 &$cleanup_config($conf->{cloudinit
});
2559 foreach my $snapname (keys %{$conf->{snapshots
}}) {
2560 die "internal error: snapshot name '$snapname' is forbidden" if lc($snapname) eq 'pending';
2561 &$cleanup_config($conf->{snapshots
}->{$snapname}, undef, $snapname);
2564 # remove 'unusedX' settings if we re-add a volume
2565 foreach my $key (keys %$conf) {
2566 my $value = $conf->{$key};
2567 if ($key =~ m/^unused/ && $used_volids->{$value}) {
2568 delete $conf->{$key};
2572 my $generate_raw_config = sub {
2573 my ($conf, $pending) = @_;
2577 # add description as comment to top of file
2578 if (defined(my $descr = $conf->{description
})) {
2580 foreach my $cl (split(/\n/, $descr)) {
2581 $raw .= '#' . PVE
::Tools
::encode_text
($cl) . "\n";
2584 $raw .= "#\n" if $pending;
2588 foreach my $key (sort keys %$conf) {
2589 next if $key =~ /^(digest|description|pending|cloudinit|snapshots)$/;
2590 $raw .= "$key: $conf->{$key}\n";
2595 my $raw = &$generate_raw_config($conf);
2597 if (scalar(keys %{$conf->{pending
}})){
2598 $raw .= "\n[PENDING]\n";
2599 $raw .= &$generate_raw_config($conf->{pending
}, 1);
2602 if (scalar(keys %{$conf->{cloudinit
}})){
2603 $raw .= "\n[special:cloudinit]\n";
2604 $raw .= &$generate_raw_config($conf->{cloudinit
});
2607 foreach my $snapname (sort keys %{$conf->{snapshots
}}) {
2608 $raw .= "\n[$snapname]\n";
2609 $raw .= &$generate_raw_config($conf->{snapshots
}->{$snapname});
2619 # we use static defaults from our JSON schema configuration
2620 foreach my $key (keys %$confdesc) {
2621 if (defined(my $default = $confdesc->{$key}->{default})) {
2622 $res->{$key} = $default;
2630 my $vmlist = PVE
::Cluster
::get_vmlist
();
2632 return $res if !$vmlist || !$vmlist->{ids
};
2633 my $ids = $vmlist->{ids
};
2634 my $nodename = nodename
();
2636 foreach my $vmid (keys %$ids) {
2637 my $d = $ids->{$vmid};
2638 next if !$d->{node
} || $d->{node
} ne $nodename;
2639 next if !$d->{type
} || $d->{type
} ne 'qemu';
2640 $res->{$vmid}->{exists} = 1;
2645 # test if VM uses local resources (to prevent migration)
2646 sub check_local_resources
{
2647 my ($conf, $noerr) = @_;
2651 push @loc_res, "hostusb" if $conf->{hostusb
}; # old syntax
2652 push @loc_res, "hostpci" if $conf->{hostpci
}; # old syntax
2654 push @loc_res, "ivshmem" if $conf->{ivshmem
};
2656 foreach my $k (keys %$conf) {
2657 next if $k =~ m/^usb/ && ($conf->{$k} =~ m/^spice(?![^,])/);
2658 # sockets are safe: they will recreated be on the target side post-migrate
2659 next if $k =~ m/^serial/ && ($conf->{$k} eq 'socket');
2660 push @loc_res, $k if $k =~ m/^(usb|hostpci|serial|parallel)\d+$/;
2663 die "VM uses local resources\n" if scalar @loc_res && !$noerr;
2668 # check if used storages are available on all nodes (use by migrate)
2669 sub check_storage_availability
{
2670 my ($storecfg, $conf, $node) = @_;
2672 PVE
::QemuConfig-
>foreach_volume($conf, sub {
2673 my ($ds, $drive) = @_;
2675 my $volid = $drive->{file
};
2678 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
2681 # check if storage is available on both nodes
2682 my $scfg = PVE
::Storage
::storage_check_enabled
($storecfg, $sid);
2683 PVE
::Storage
::storage_check_enabled
($storecfg, $sid, $node);
2685 my ($vtype) = PVE
::Storage
::parse_volname
($storecfg, $volid);
2687 die "$volid: content type '$vtype' is not available on storage '$sid'\n"
2688 if !$scfg->{content
}->{$vtype};
2692 # list nodes where all VM images are available (used by has_feature API)
2694 my ($conf, $storecfg) = @_;
2696 my $nodelist = PVE
::Cluster
::get_nodelist
();
2697 my $nodehash = { map { $_ => 1 } @$nodelist };
2698 my $nodename = nodename
();
2700 PVE
::QemuConfig-
>foreach_volume($conf, sub {
2701 my ($ds, $drive) = @_;
2703 my $volid = $drive->{file
};
2706 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
2708 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
2709 if ($scfg->{disable
}) {
2711 } elsif (my $avail = $scfg->{nodes
}) {
2712 foreach my $node (keys %$nodehash) {
2713 delete $nodehash->{$node} if !$avail->{$node};
2715 } elsif (!$scfg->{shared
}) {
2716 foreach my $node (keys %$nodehash) {
2717 delete $nodehash->{$node} if $node ne $nodename
2726 sub check_local_storage_availability
{
2727 my ($conf, $storecfg) = @_;
2729 my $nodelist = PVE
::Cluster
::get_nodelist
();
2730 my $nodehash = { map { $_ => {} } @$nodelist };
2732 PVE
::QemuConfig-
>foreach_volume($conf, sub {
2733 my ($ds, $drive) = @_;
2735 my $volid = $drive->{file
};
2738 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
2740 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
2742 if ($scfg->{disable
}) {
2743 foreach my $node (keys %$nodehash) {
2744 $nodehash->{$node}->{unavailable_storages
}->{$storeid} = 1;
2746 } elsif (my $avail = $scfg->{nodes
}) {
2747 foreach my $node (keys %$nodehash) {
2748 if (!$avail->{$node}) {
2749 $nodehash->{$node}->{unavailable_storages
}->{$storeid} = 1;
2756 foreach my $node (values %$nodehash) {
2757 if (my $unavail = $node->{unavailable_storages
}) {
2758 $node->{unavailable_storages
} = [ sort keys %$unavail ];
2765 # Compat only, use assert_config_exists_on_node and vm_running_locally where possible
2767 my ($vmid, $nocheck, $node) = @_;
2769 PVE
::QemuConfig
::assert_config_exists_on_node
($vmid, $node) if !$nocheck;
2770 return PVE
::QemuServer
::Helpers
::vm_running_locally
($vmid);
2775 my $vzlist = config_list
();
2777 my $fd = IO
::Dir-
>new($PVE::QemuServer
::Helpers
::var_run_tmpdir
) || return $vzlist;
2779 while (defined(my $de = $fd->read)) {
2780 next if $de !~ m/^(\d+)\.pid$/;
2782 next if !defined($vzlist->{$vmid});
2783 if (my $pid = check_running
($vmid)) {
2784 $vzlist->{$vmid}->{pid
} = $pid;
2791 our $vmstatus_return_properties = {
2792 vmid
=> get_standard_option
('pve-vmid'),
2794 description
=> "Qemu process status.",
2796 enum
=> ['stopped', 'running'],
2799 description
=> "Maximum memory in bytes.",
2802 renderer
=> 'bytes',
2805 description
=> "Root disk size in bytes.",
2808 renderer
=> 'bytes',
2811 description
=> "VM name.",
2816 description
=> "Qemu QMP agent status.",
2821 description
=> "PID of running qemu process.",
2826 description
=> "Uptime.",
2829 renderer
=> 'duration',
2832 description
=> "Maximum usable CPUs.",
2837 description
=> "The current config lock, if any.",
2842 description
=> "The current configured tags, if any",
2846 'running-machine' => {
2847 description
=> "The currently running machine type (if running).",
2852 description
=> "The currently running QEMU version (if running).",
2858 my $last_proc_pid_stat;
2860 # get VM status information
2861 # This must be fast and should not block ($full == false)
2862 # We only query KVM using QMP if $full == true (this can be slow)
2864 my ($opt_vmid, $full) = @_;
2868 my $storecfg = PVE
::Storage
::config
();
2870 my $list = vzlist
();
2871 my $defaults = load_defaults
();
2873 my ($uptime) = PVE
::ProcFSTools
::read_proc_uptime
(1);
2875 my $cpucount = $cpuinfo->{cpus
} || 1;
2877 foreach my $vmid (keys %$list) {
2878 next if $opt_vmid && ($vmid ne $opt_vmid);
2880 my $conf = PVE
::QemuConfig-
>load_config($vmid);
2882 my $d = { vmid
=> int($vmid) };
2883 $d->{pid
} = int($list->{$vmid}->{pid
}) if $list->{$vmid}->{pid
};
2885 # fixme: better status?
2886 $d->{status
} = $list->{$vmid}->{pid
} ?
'running' : 'stopped';
2888 my $size = PVE
::QemuServer
::Drive
::bootdisk_size
($storecfg, $conf);
2889 if (defined($size)) {
2890 $d->{disk
} = 0; # no info available
2891 $d->{maxdisk
} = $size;
2897 $d->{cpus
} = ($conf->{sockets
} || $defaults->{sockets
})
2898 * ($conf->{cores
} || $defaults->{cores
});
2899 $d->{cpus
} = $cpucount if $d->{cpus
} > $cpucount;
2900 $d->{cpus
} = $conf->{vcpus
} if $conf->{vcpus
};
2902 $d->{name
} = $conf->{name
} || "VM $vmid";
2903 $d->{maxmem
} = $conf->{memory
} ?
$conf->{memory
}*(1024*1024)
2904 : $defaults->{memory
}*(1024*1024);
2906 if ($conf->{balloon
}) {
2907 $d->{balloon_min
} = $conf->{balloon
}*(1024*1024);
2908 $d->{shares
} = defined($conf->{shares
}) ?
$conf->{shares
}
2909 : $defaults->{shares
};
2920 $d->{diskwrite
} = 0;
2922 $d->{template
} = 1 if PVE
::QemuConfig-
>is_template($conf);
2924 $d->{serial
} = 1 if conf_has_serial
($conf);
2925 $d->{lock} = $conf->{lock} if $conf->{lock};
2926 $d->{tags
} = $conf->{tags
} if defined($conf->{tags
});
2931 my $netdev = PVE
::ProcFSTools
::read_proc_net_dev
();
2932 foreach my $dev (keys %$netdev) {
2933 next if $dev !~ m/^tap([1-9]\d*)i/;
2935 my $d = $res->{$vmid};
2938 $d->{netout
} += $netdev->{$dev}->{receive
};
2939 $d->{netin
} += $netdev->{$dev}->{transmit
};
2942 $d->{nics
}->{$dev}->{netout
} = int($netdev->{$dev}->{receive
});
2943 $d->{nics
}->{$dev}->{netin
} = int($netdev->{$dev}->{transmit
});
2948 my $ctime = gettimeofday
;
2950 foreach my $vmid (keys %$list) {
2952 my $d = $res->{$vmid};
2953 my $pid = $d->{pid
};
2956 my $pstat = PVE
::ProcFSTools
::read_proc_pid_stat
($pid);
2957 next if !$pstat; # not running
2959 my $used = $pstat->{utime} + $pstat->{stime
};
2961 $d->{uptime
} = int(($uptime - $pstat->{starttime
})/$cpuinfo->{user_hz
});
2963 if ($pstat->{vsize
}) {
2964 $d->{mem
} = int(($pstat->{rss
}/$pstat->{vsize
})*$d->{maxmem
});
2967 my $old = $last_proc_pid_stat->{$pid};
2969 $last_proc_pid_stat->{$pid} = {
2977 my $dtime = ($ctime - $old->{time}) * $cpucount * $cpuinfo->{user_hz
};
2979 if ($dtime > 1000) {
2980 my $dutime = $used - $old->{used
};
2982 $d->{cpu
} = (($dutime/$dtime)* $cpucount) / $d->{cpus
};
2983 $last_proc_pid_stat->{$pid} = {
2989 $d->{cpu
} = $old->{cpu
};
2993 return $res if !$full;
2995 my $qmpclient = PVE
::QMPClient-
>new();
2997 my $ballooncb = sub {
2998 my ($vmid, $resp) = @_;
3000 my $info = $resp->{'return'};
3001 return if !$info->{max_mem
};
3003 my $d = $res->{$vmid};
3005 # use memory assigned to VM
3006 $d->{maxmem
} = $info->{max_mem
};
3007 $d->{balloon
} = $info->{actual
};
3009 if (defined($info->{total_mem
}) && defined($info->{free_mem
})) {
3010 $d->{mem
} = $info->{total_mem
} - $info->{free_mem
};
3011 $d->{freemem
} = $info->{free_mem
};
3014 $d->{ballooninfo
} = $info;
3017 my $blockstatscb = sub {
3018 my ($vmid, $resp) = @_;
3019 my $data = $resp->{'return'} || [];
3020 my $totalrdbytes = 0;
3021 my $totalwrbytes = 0;
3023 for my $blockstat (@$data) {
3024 $totalrdbytes = $totalrdbytes + $blockstat->{stats
}->{rd_bytes
};
3025 $totalwrbytes = $totalwrbytes + $blockstat->{stats
}->{wr_bytes
};
3027 $blockstat->{device
} =~ s/drive-//;
3028 $res->{$vmid}->{blockstat
}->{$blockstat->{device
}} = $blockstat->{stats
};
3030 $res->{$vmid}->{diskread
} = $totalrdbytes;
3031 $res->{$vmid}->{diskwrite
} = $totalwrbytes;
3034 my $machinecb = sub {
3035 my ($vmid, $resp) = @_;
3036 my $data = $resp->{'return'} || [];
3038 $res->{$vmid}->{'running-machine'} =
3039 PVE
::QemuServer
::Machine
::current_from_query_machines
($data);
3042 my $versioncb = sub {
3043 my ($vmid, $resp) = @_;
3044 my $data = $resp->{'return'} // {};
3045 my $version = 'unknown';
3047 if (my $v = $data->{qemu
}) {
3048 $version = $v->{major
} . "." . $v->{minor
} . "." . $v->{micro
};
3051 $res->{$vmid}->{'running-qemu'} = $version;
3054 my $statuscb = sub {
3055 my ($vmid, $resp) = @_;
3057 $qmpclient->queue_cmd($vmid, $blockstatscb, 'query-blockstats');
3058 $qmpclient->queue_cmd($vmid, $machinecb, 'query-machines');
3059 $qmpclient->queue_cmd($vmid, $versioncb, 'query-version');
3060 # this fails if ballon driver is not loaded, so this must be
3061 # the last commnand (following command are aborted if this fails).
3062 $qmpclient->queue_cmd($vmid, $ballooncb, 'query-balloon');
3064 my $status = 'unknown';
3065 if (!defined($status = $resp->{'return'}->{status
})) {
3066 warn "unable to get VM status\n";
3070 $res->{$vmid}->{qmpstatus
} = $resp->{'return'}->{status
};
3073 foreach my $vmid (keys %$list) {
3074 next if $opt_vmid && ($vmid ne $opt_vmid);
3075 next if !$res->{$vmid}->{pid
}; # not running
3076 $qmpclient->queue_cmd($vmid, $statuscb, 'query-status');
3079 $qmpclient->queue_execute(undef, 2);
3081 foreach my $vmid (keys %$list) {
3082 next if $opt_vmid && ($vmid ne $opt_vmid);
3083 next if !$res->{$vmid}->{pid
}; #not running
3085 # we can't use the $qmpclient since it might have already aborted on
3086 # 'query-balloon', but this might also fail for older versions...
3087 my $qemu_support = eval { mon_cmd
($vmid, "query-proxmox-support") };
3088 $res->{$vmid}->{'proxmox-support'} = $qemu_support // {};
3091 foreach my $vmid (keys %$list) {
3092 next if $opt_vmid && ($vmid ne $opt_vmid);
3093 $res->{$vmid}->{qmpstatus
} = $res->{$vmid}->{status
} if !$res->{$vmid}->{qmpstatus
};
3099 sub conf_has_serial
{
3102 for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
3103 if ($conf->{"serial$i"}) {
3111 sub conf_has_audio
{
3112 my ($conf, $id) = @_;
3115 my $audio = $conf->{"audio$id"};
3116 return if !defined($audio);
3118 my $audioproperties = parse_property_string
($audio_fmt, $audio);
3119 my $audiodriver = $audioproperties->{driver
} // 'spice';
3122 dev
=> $audioproperties->{device
},
3123 dev_id
=> "audiodev$id",
3124 backend
=> $audiodriver,
3125 backend_id
=> "$audiodriver-backend${id}",
3130 my ($audio, $audiopciaddr, $machine_version) = @_;
3134 my $id = $audio->{dev_id
};
3136 if (min_version
($machine_version, 4, 2)) {
3137 $audiodev = ",audiodev=$audio->{backend_id}";
3140 if ($audio->{dev
} eq 'AC97') {
3141 push @$devs, '-device', "AC97,id=${id}${audiopciaddr}$audiodev";
3142 } elsif ($audio->{dev
} =~ /intel\-hda$/) {
3143 push @$devs, '-device', "$audio->{dev},id=${id}${audiopciaddr}";
3144 push @$devs, '-device', "hda-micro,id=${id}-codec0,bus=${id}.0,cad=0$audiodev";
3145 push @$devs, '-device', "hda-duplex,id=${id}-codec1,bus=${id}.0,cad=1$audiodev";
3147 die "unkown audio device '$audio->{dev}', implement me!";
3150 push @$devs, '-audiodev', "$audio->{backend},id=$audio->{backend_id}";
3158 socket => "/var/run/qemu-server/$vmid.swtpm",
3159 pid
=> "/var/run/qemu-server/$vmid.swtpm.pid",
3163 sub add_tpm_device
{
3164 my ($vmid, $devices, $conf) = @_;
3166 return if !$conf->{tpmstate0
};
3168 my $paths = get_tpm_paths
($vmid);
3170 push @$devices, "-chardev", "socket,id=tpmchar,path=$paths->{socket}";
3171 push @$devices, "-tpmdev", "emulator,id=tpmdev,chardev=tpmchar";
3172 push @$devices, "-device", "tpm-tis,tpmdev=tpmdev";
3176 my ($storecfg, $vmid, $tpmdrive, $migration) = @_;
3178 return if !$tpmdrive;
3181 my $tpm = parse_drive
("tpmstate0", $tpmdrive);
3182 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($tpm->{file
}, 1);
3184 $state = PVE
::Storage
::map_volume
($storecfg, $tpm->{file
});
3186 $state = $tpm->{file
};
3189 my $paths = get_tpm_paths
($vmid);
3191 # during migration, we will get state from remote
3194 # run swtpm_setup to create a new TPM state if it doesn't exist yet
3201 "--create-platform-cert",
3204 "/etc/swtpm_setup.conf", # do not use XDG configs
3206 "0", # force creation as root, error if not possible
3207 "--not-overwrite", # ignore existing state, do not modify
3210 push @$setup_cmd, "--tpm2" if $tpm->{version
} eq 'v2.0';
3211 # TPM 2.0 supports ECC crypto, use if possible
3212 push @$setup_cmd, "--ecc" if $tpm->{version
} eq 'v2.0';
3214 run_command
($setup_cmd, outfunc
=> sub {
3215 print "swtpm_setup: $1\n";
3219 my $emulator_cmd = [
3223 "backend-uri=file://$state,mode=0600",
3225 "type=unixio,path=$paths->{socket},mode=0600",
3227 "file=$paths->{pid}",
3228 "--terminate", # terminate on QEMU disconnect
3231 push @$emulator_cmd, "--tpm2" if $tpm->{version
} eq 'v2.0';
3232 run_command
($emulator_cmd, outfunc
=> sub { print $1; });
3234 my $tries = 100; # swtpm may take a bit to start before daemonizing, wait up to 5s for pid
3235 while (! -e
$paths->{pid
}) {
3236 die "failed to start swtpm: pid file '$paths->{pid}' wasn't created.\n" if --$tries == 0;
3240 # return untainted PID of swtpm daemon so it can be killed on error
3241 file_read_firstline
($paths->{pid
}) =~ m/(\d+)/;
3245 sub vga_conf_has_spice
{
3248 my $vgaconf = parse_vga
($vga);
3249 my $vgatype = $vgaconf->{type
};
3250 return 0 if !$vgatype || $vgatype !~ m/^qxl([234])?$/;
3257 return get_host_arch
() eq $arch;
3262 return $conf->{arch
} // get_host_arch
();
3265 my $default_machines = {
3270 sub get_installed_machine_version
{
3271 my ($kvmversion) = @_;
3272 $kvmversion = kvm_user_version
() if !defined($kvmversion);
3273 $kvmversion =~ m/^(\d+\.\d+)/;
3277 sub windows_get_pinned_machine_version
{
3278 my ($machine, $base_version, $kvmversion) = @_;
3280 my $pin_version = $base_version;
3281 if (!defined($base_version) ||
3282 !PVE
::QemuServer
::Machine
::can_run_pve_machine_version
($base_version, $kvmversion)
3284 $pin_version = get_installed_machine_version
($kvmversion);
3286 if (!$machine || $machine eq 'pc') {
3287 $machine = "pc-i440fx-$pin_version";
3288 } elsif ($machine eq 'q35') {
3289 $machine = "pc-q35-$pin_version";
3290 } elsif ($machine eq 'virt') {
3291 $machine = "virt-$pin_version";
3293 warn "unknown machine type '$machine', not touching that!\n";
3299 sub get_vm_machine
{
3300 my ($conf, $forcemachine, $arch, $add_pve_version, $kvmversion) = @_;
3302 my $machine = $forcemachine || $conf->{machine
};
3304 if (!$machine || $machine =~ m/^(?:pc|q35|virt)$/) {
3305 $kvmversion //= kvm_user_version
();
3306 # we must pin Windows VMs without a specific version to 5.1, as 5.2 fixed a bug in ACPI
3307 # layout which confuses windows quite a bit and may result in various regressions..
3308 # see: https://lists.gnu.org/archive/html/qemu-devel/2021-02/msg08484.html
3309 if (windows_version
($conf->{ostype
})) {
3310 $machine = windows_get_pinned_machine_version
($machine, '5.1', $kvmversion);
3313 $machine ||= $default_machines->{$arch};
3314 if ($add_pve_version) {
3315 my $pvever = PVE
::QemuServer
::Machine
::get_pve_version
($kvmversion);
3316 $machine .= "+pve$pvever";
3320 if ($add_pve_version && $machine !~ m/\+pve\d+?(?:\.pxe)?$/) {
3321 my $is_pxe = $machine =~ m/^(.*?)\.pxe$/;
3322 $machine = $1 if $is_pxe;
3324 # for version-pinned machines that do not include a pve-version (e.g.
3325 # pc-q35-4.1), we assume 0 to keep them stable in case we bump
3326 $machine .= '+pve0';
3328 $machine .= '.pxe' if $is_pxe;
3334 sub get_ovmf_files
($$$) {
3335 my ($arch, $efidisk, $smm) = @_;
3337 my $types = $OVMF->{$arch}
3338 or die "no OVMF images known for architecture '$arch'\n";
3340 my $type = 'default';
3341 if (defined($efidisk->{efitype
}) && $efidisk->{efitype
} eq '4m') {
3342 $type = $smm ?
"4m" : "4m-no-smm";
3343 $type .= '-ms' if $efidisk->{'pre-enrolled-keys'};
3346 return $types->{$type}->@*;
3350 aarch64
=> '/usr/bin/qemu-system-aarch64',
3351 x86_64
=> '/usr/bin/qemu-system-x86_64',
3353 sub get_command_for_arch
($) {
3355 return '/usr/bin/kvm' if is_native
($arch);
3357 my $cmd = $Arch2Qemu->{$arch}
3358 or die "don't know how to emulate architecture '$arch'\n";
3362 # To use query_supported_cpu_flags and query_understood_cpu_flags to get flags
3363 # to use in a QEMU command line (-cpu element), first array_intersect the result
3364 # of query_supported_ with query_understood_. This is necessary because:
3366 # a) query_understood_ returns flags the host cannot use and
3367 # b) query_supported_ (rather the QMP call) doesn't actually return CPU
3368 # flags, but CPU settings - with most of them being flags. Those settings
3369 # (and some flags, curiously) cannot be specified as a "-cpu" argument.
3371 # query_supported_ needs to start up to 2 temporary VMs and is therefore rather
3372 # expensive. If you need the value returned from this, you can get it much
3373 # cheaper from pmxcfs using PVE::Cluster::get_node_kv('cpuflags-$accel') with
3374 # $accel being 'kvm' or 'tcg'.
3376 # pvestatd calls this function on startup and whenever the QEMU/KVM version
3377 # changes, automatically populating pmxcfs.
3379 # Returns: { kvm => [ flagX, flagY, ... ], tcg => [ flag1, flag2, ... ] }
3380 # since kvm and tcg machines support different flags
3382 sub query_supported_cpu_flags
{
3385 $arch //= get_host_arch
();
3386 my $default_machine = $default_machines->{$arch};
3390 # FIXME: Once this is merged, the code below should work for ARM as well:
3391 # https://lists.nongnu.org/archive/html/qemu-devel/2019-06/msg04947.html
3392 die "QEMU/KVM cannot detect CPU flags on ARM (aarch64)\n" if
3395 my $kvm_supported = defined(kvm_version
());
3396 my $qemu_cmd = get_command_for_arch
($arch);
3398 my $pidfile = PVE
::QemuServer
::Helpers
::pidfile_name
($fakevmid);
3400 # Start a temporary (frozen) VM with vmid -1 to allow sending a QMP command
3401 my $query_supported_run_qemu = sub {
3407 '-machine', $default_machine,
3409 '-chardev', "socket,id=qmp,path=/var/run/qemu-server/$fakevmid.qmp,server=on,wait=off",
3410 '-mon', 'chardev=qmp,mode=control',
3411 '-pidfile', $pidfile,
3416 push @$cmd, '-accel', 'tcg';
3419 my $rc = run_command
($cmd, noerr
=> 1, quiet
=> 0);
3420 die "QEMU flag querying VM exited with code " . $rc if $rc;
3423 my $cmd_result = mon_cmd
(
3425 'query-cpu-model-expansion',
3427 model
=> { name
=> 'host' }
3430 my $props = $cmd_result->{model
}->{props
};
3431 foreach my $prop (keys %$props) {
3432 next if $props->{$prop} ne '1';
3433 # QEMU returns some flags multiple times, with '_', '.' or '-'
3434 # (e.g. lahf_lm and lahf-lm; sse4.2, sse4-2 and sse4_2; ...).
3435 # We only keep those with underscores, to match /proc/cpuinfo
3436 $prop =~ s/\.|-/_/g;
3437 $flags->{$prop} = 1;
3442 # force stop with 10 sec timeout and 'nocheck', always stop, even if QMP failed
3443 vm_stop
(undef, $fakevmid, 1, 1, 10, 0, 1);
3447 return [ sort keys %$flags ];
3450 # We need to query QEMU twice, since KVM and TCG have different supported flags
3451 PVE
::QemuConfig-
>lock_config($fakevmid, sub {
3452 $flags->{tcg
} = eval { $query_supported_run_qemu->(0) };
3453 warn "warning: failed querying supported tcg flags: $@\n" if $@;
3455 if ($kvm_supported) {
3456 $flags->{kvm
} = eval { $query_supported_run_qemu->(1) };
3457 warn "warning: failed querying supported kvm flags: $@\n" if $@;
3464 # Understood CPU flags are written to a file at 'pve-qemu' compile time
3465 my $understood_cpu_flag_dir = "/usr/share/kvm";
3466 sub query_understood_cpu_flags
{
3467 my $arch = get_host_arch
();
3468 my $filepath = "$understood_cpu_flag_dir/recognized-CPUID-flags-$arch";
3470 die "Cannot query understood QEMU CPU flags for architecture: $arch (file not found)\n"
3473 my $raw = file_get_contents
($filepath);
3474 $raw =~ s/^\s+|\s+$//g;
3475 my @flags = split(/\s+/, $raw);
3480 # Since commit 277d33454f77ec1d1e0bc04e37621e4dd2424b67 in pve-qemu, smm is not off by default
3481 # anymore. But smm=off seems to be required when using SeaBIOS and serial display.
3482 my sub should_disable_smm
{
3483 my ($conf, $vga) = @_;
3485 return (!defined($conf->{bios
}) || $conf->{bios
} eq 'seabios') &&
3486 $vga->{type
} && $vga->{type
} =~ m/^(serial\d+|none)$/;
3489 sub config_to_command
{
3490 my ($storecfg, $vmid, $conf, $defaults, $forcemachine, $forcecpu,
3494 my ($globalFlags, $machineFlags, $rtcFlags) = ([], [], []);
3497 my $ostype = $conf->{ostype
};
3498 my $winversion = windows_version
($ostype);
3499 my $kvm = $conf->{kvm
};
3500 my $nodename = nodename
();
3502 my $arch = get_vm_arch
($conf);
3503 my $kvm_binary = get_command_for_arch
($arch);
3504 my $kvmver = kvm_user_version
($kvm_binary);
3506 if (!$kvmver || $kvmver !~ m/^(\d+)\.(\d+)/ || $1 < 3) {
3507 $kvmver //= "undefined";
3508 die "Detected old QEMU binary ('$kvmver', at least 3.0 is required)\n";
3511 my $add_pve_version = min_version
($kvmver, 4, 1);
3513 my $machine_type = get_vm_machine
($conf, $forcemachine, $arch, $add_pve_version);
3514 my $machine_version = extract_version
($machine_type, $kvmver);
3515 $kvm //= 1 if is_native
($arch);
3517 $machine_version =~ m/(\d+)\.(\d+)/;
3518 my ($machine_major, $machine_minor) = ($1, $2);
3520 if ($kvmver =~ m/^\d+\.\d+\.(\d+)/ && $1 >= 90) {
3521 warn "warning: Installed QEMU version ($kvmver) is a release candidate, ignoring version checks\n";
3522 } elsif (!min_version
($kvmver, $machine_major, $machine_minor)) {
3523 die "Installed QEMU version '$kvmver' is too old to run machine type '$machine_type',"
3524 ." please upgrade node '$nodename'\n"
3525 } elsif (!PVE
::QemuServer
::Machine
::can_run_pve_machine_version
($machine_version, $kvmver)) {
3526 my $max_pve_version = PVE
::QemuServer
::Machine
::get_pve_version
($machine_version);
3527 die "Installed qemu-server (max feature level for $machine_major.$machine_minor is"
3528 ." pve$max_pve_version) is too old to run machine type '$machine_type', please upgrade"
3529 ." node '$nodename'\n";
3532 # if a specific +pve version is required for a feature, use $version_guard
3533 # instead of min_version to allow machines to be run with the minimum
3535 my $required_pve_version = 0;
3536 my $version_guard = sub {
3537 my ($major, $minor, $pve) = @_;
3538 return 0 if !min_version
($machine_version, $major, $minor, $pve);
3539 my $max_pve = PVE
::QemuServer
::Machine
::get_pve_version
("$major.$minor");
3540 return 1 if min_version
($machine_version, $major, $minor, $max_pve+1);
3541 $required_pve_version = $pve if $pve && $pve > $required_pve_version;
3545 if ($kvm && !defined kvm_version
()) {
3546 die "KVM virtualisation configured, but not available. Either disable in VM configuration"
3547 ." or enable in BIOS.\n";
3550 my $q35 = PVE
::QemuServer
::Machine
::machine_type_is_q35
($conf);
3551 my $hotplug_features = parse_hotplug_features
(defined($conf->{hotplug
}) ?
$conf->{hotplug
} : '1');
3552 my $use_old_bios_files = undef;
3553 ($use_old_bios_files, $machine_type) = qemu_use_old_bios_files
($machine_type);
3555 if ($conf->{affinity
}) {
3556 push @$cmd, "/usr/bin/taskset";
3557 push @$cmd, "--cpu-list";
3558 push @$cmd, "--all-tasks";
3559 push @$cmd, $conf->{affinity
};
3562 push @$cmd, $kvm_binary;
3564 push @$cmd, '-id', $vmid;
3566 my $vmname = $conf->{name
} || "vm$vmid";
3568 push @$cmd, '-name', "$vmname,debug-threads=on";
3570 push @$cmd, '-no-shutdown';
3574 my $qmpsocket = PVE
::QemuServer
::Helpers
::qmp_socket
($vmid);
3575 push @$cmd, '-chardev', "socket,id=qmp,path=$qmpsocket,server=on,wait=off";
3576 push @$cmd, '-mon', "chardev=qmp,mode=control";
3578 if (min_version
($machine_version, 2, 12)) {
3579 push @$cmd, '-chardev', "socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect=5";
3580 push @$cmd, '-mon', "chardev=qmp-event,mode=control";
3583 push @$cmd, '-pidfile' , PVE
::QemuServer
::Helpers
::pidfile_name
($vmid);
3585 push @$cmd, '-daemonize';
3587 if ($conf->{smbios1
}) {
3588 my $smbios_conf = parse_smbios1
($conf->{smbios1
});
3589 if ($smbios_conf->{base64
}) {
3590 # Do not pass base64 flag to qemu
3591 delete $smbios_conf->{base64
};
3592 my $smbios_string = "";
3593 foreach my $key (keys %$smbios_conf) {
3595 if ($key eq "uuid") {
3596 $value = $smbios_conf->{uuid
}
3598 $value = decode_base64
($smbios_conf->{$key});
3600 # qemu accepts any binary data, only commas need escaping by double comma
3602 $smbios_string .= "," . $key . "=" . $value if $value;
3604 push @$cmd, '-smbios', "type=1" . $smbios_string;
3606 push @$cmd, '-smbios', "type=1,$conf->{smbios1}";
3610 if ($conf->{bios
} && $conf->{bios
} eq 'ovmf') {
3612 if (my $efidisk = $conf->{efidisk0
}) {
3613 $d = parse_drive
('efidisk0', $efidisk);
3616 my ($ovmf_code, $ovmf_vars) = get_ovmf_files
($arch, $d, $q35);
3617 die "uefi base image '$ovmf_code' not found\n" if ! -f
$ovmf_code;
3619 my ($path, $format);
3620 my $read_only_str = '';
3622 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($d->{file
}, 1);
3623 $format = $d->{format
};
3625 $path = PVE
::Storage
::path
($storecfg, $d->{file
});
3626 if (!defined($format)) {
3627 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
3628 $format = qemu_img_format
($scfg, $volname);
3632 die "efidisk format must be specified\n"
3633 if !defined($format);
3636 $read_only_str = ',readonly=on' if drive_is_read_only
($conf, $d);
3638 log_warn
("no efidisk configured! Using temporary efivars disk.");
3639 $path = "/tmp/$vmid-ovmf.fd";
3640 PVE
::Tools
::file_copy
($ovmf_vars, $path, -s
$ovmf_vars);
3646 if ($format eq 'raw' && $version_guard->(4, 1, 2)) {
3647 $size_str = ",size=" . (-s
$ovmf_vars);
3650 # SPI flash does lots of read-modify-write OPs, without writeback this gets really slow #3329
3652 if ($path =~ m/^rbd:/) {
3653 $cache = ',cache=writeback';
3654 $path .= ':rbd_cache_policy=writeback'; # avoid write-around, we *need* to cache writes too
3657 push @$cmd, '-drive', "if=pflash,unit=0,format=raw,readonly=on,file=$ovmf_code";
3658 push @$cmd, '-drive', "if=pflash,unit=1$cache,format=$format,id=drive-efidisk0$size_str,file=${path}${read_only_str}";
3661 if ($q35) { # tell QEMU to load q35 config early
3662 # we use different pcie-port hardware for qemu >= 4.0 for passthrough
3663 if (min_version
($machine_version, 4, 0)) {
3664 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-q35-4.0.cfg';
3666 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-q35.cfg';
3670 if (defined(my $fixups = qemu_created_version_fixups
($conf, $forcemachine, $kvmver))) {
3671 push @$cmd, $fixups->@*;
3674 if ($conf->{vmgenid
}) {
3675 push @$devices, '-device', 'vmgenid,guid='.$conf->{vmgenid
};
3678 # add usb controllers
3679 my @usbcontrollers = PVE
::QemuServer
::USB
::get_usb_controllers
(
3680 $conf, $bridges, $arch, $machine_type, $usbdesc->{format
}, $MAX_USB_DEVICES);
3681 push @$devices, @usbcontrollers if @usbcontrollers;
3682 my $vga = parse_vga
($conf->{vga
});
3684 my $qxlnum = vga_conf_has_spice
($conf->{vga
});
3685 $vga->{type
} = 'qxl' if $qxlnum;
3687 if (!$vga->{type
}) {
3688 if ($arch eq 'aarch64') {
3689 $vga->{type
} = 'virtio';
3690 } elsif (min_version
($machine_version, 2, 9)) {
3691 $vga->{type
} = (!$winversion || $winversion >= 6) ?
'std' : 'cirrus';
3693 $vga->{type
} = ($winversion >= 6) ?
'std' : 'cirrus';
3697 # enable absolute mouse coordinates (needed by vnc)
3698 my $tablet = $conf->{tablet
};
3699 if (!defined($tablet)) {
3700 $tablet = $defaults->{tablet
};
3701 $tablet = 0 if $qxlnum; # disable for spice because it is not needed
3702 $tablet = 0 if $vga->{type
} =~ m/^serial\d+$/; # disable if we use serial terminal (no vga card)
3706 push @$devices, '-device', print_tabletdevice_full
($conf, $arch) if $tablet;
3707 my $kbd = print_keyboarddevice_full
($conf, $arch);
3708 push @$devices, '-device', $kbd if defined($kbd);
3711 my $bootorder = device_bootorder
($conf);
3713 # host pci device passthrough
3714 my ($kvm_off, $gpu_passthrough, $legacy_igd) = PVE
::QemuServer
::PCI
::print_hostpci_devices
(
3715 $vmid, $conf, $devices, $vga, $winversion, $q35, $bridges, $arch, $machine_type, $bootorder);
3718 my $usb_dev_features = {};
3719 $usb_dev_features->{spice_usb3
} = 1 if min_version
($machine_version, 4, 0);
3721 my @usbdevices = PVE
::QemuServer
::USB
::get_usb_devices
(
3722 $conf, $usbdesc->{format
}, $MAX_USB_DEVICES, $usb_dev_features, $bootorder);
3723 push @$devices, @usbdevices if @usbdevices;
3726 for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
3727 my $path = $conf->{"serial$i"} or next;
3728 if ($path eq 'socket') {
3729 my $socket = "/var/run/qemu-server/${vmid}.serial$i";
3730 push @$devices, '-chardev', "socket,id=serial$i,path=$socket,server=on,wait=off";
3731 # On aarch64, serial0 is the UART device. Qemu only allows
3732 # connecting UART devices via the '-serial' command line, as
3733 # the device has a fixed slot on the hardware...
3734 if ($arch eq 'aarch64' && $i == 0) {
3735 push @$devices, '-serial', "chardev:serial$i";
3737 push @$devices, '-device', "isa-serial,chardev=serial$i";
3740 die "no such serial device\n" if ! -c
$path;
3741 push @$devices, '-chardev', "tty,id=serial$i,path=$path";
3742 push @$devices, '-device', "isa-serial,chardev=serial$i";
3747 for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
3748 if (my $path = $conf->{"parallel$i"}) {
3749 die "no such parallel device\n" if ! -c
$path;
3750 my $devtype = $path =~ m!^/dev/usb/lp! ?
'tty' : 'parport';
3751 push @$devices, '-chardev', "$devtype,id=parallel$i,path=$path";
3752 push @$devices, '-device', "isa-parallel,chardev=parallel$i";
3756 if (min_version
($machine_version, 4, 0) && (my $audio = conf_has_audio
($conf))) {
3757 my $audiopciaddr = print_pci_addr
("audio0", $bridges, $arch, $machine_type);
3758 my $audio_devs = audio_devs
($audio, $audiopciaddr, $machine_version);
3759 push @$devices, @$audio_devs;
3762 add_tpm_device
($vmid, $devices, $conf);
3765 $sockets = $conf->{smp
} if $conf->{smp
}; # old style - no longer iused
3766 $sockets = $conf->{sockets
} if $conf->{sockets
};
3768 my $cores = $conf->{cores
} || 1;
3770 my $maxcpus = $sockets * $cores;
3772 my $vcpus = $conf->{vcpus
} ?
$conf->{vcpus
} : $maxcpus;
3774 my $allowed_vcpus = $cpuinfo->{cpus
};
3776 die "MAX $allowed_vcpus vcpus allowed per VM on this node\n" if ($allowed_vcpus < $maxcpus);
3778 if ($hotplug_features->{cpu
} && min_version
($machine_version, 2, 7)) {
3779 push @$cmd, '-smp', "1,sockets=$sockets,cores=$cores,maxcpus=$maxcpus";
3780 for (my $i = 2; $i <= $vcpus; $i++) {
3781 my $cpustr = print_cpu_device
($conf,$i);
3782 push @$cmd, '-device', $cpustr;
3787 push @$cmd, '-smp', "$vcpus,sockets=$sockets,cores=$cores,maxcpus=$maxcpus";
3789 push @$cmd, '-nodefaults';
3791 push @$cmd, '-boot', "menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg";
3793 push @$cmd, '-no-acpi' if defined($conf->{acpi
}) && $conf->{acpi
} == 0;
3795 push @$cmd, '-no-reboot' if defined($conf->{reboot
}) && $conf->{reboot
} == 0;
3797 if ($vga->{type
} && $vga->{type
} !~ m/^serial\d+$/ && $vga->{type
} ne 'none'){
3798 push @$devices, '-device', print_vga_device
(
3799 $conf, $vga, $arch, $machine_version, $machine_type, undef, $qxlnum, $bridges);
3801 push @$cmd, '-display', 'egl-headless,gl=core' if $vga->{type
} eq 'virtio-gl'; # VIRGL
3803 my $socket = PVE
::QemuServer
::Helpers
::vnc_socket
($vmid);
3804 push @$cmd, '-vnc', "unix:$socket,password=on";
3806 push @$cmd, '-vga', 'none' if $vga->{type
} eq 'none';
3807 push @$cmd, '-nographic';
3811 my $tdf = defined($conf->{tdf
}) ?
$conf->{tdf
} : $defaults->{tdf
};
3812 my $useLocaltime = $conf->{localtime};
3814 if ($winversion >= 5) { # windows
3815 $useLocaltime = 1 if !defined($conf->{localtime});
3817 # use time drift fix when acpi is enabled
3818 if (!(defined($conf->{acpi
}) && $conf->{acpi
} == 0)) {
3819 $tdf = 1 if !defined($conf->{tdf
});
3823 if ($winversion >= 6) {
3824 push @$globalFlags, 'kvm-pit.lost_tick_policy=discard';
3825 push @$cmd, '-no-hpet';
3828 push @$rtcFlags, 'driftfix=slew' if $tdf;
3830 if ($conf->{startdate
} && $conf->{startdate
} ne 'now') {
3831 push @$rtcFlags, "base=$conf->{startdate}";
3832 } elsif ($useLocaltime) {
3833 push @$rtcFlags, 'base=localtime';
3837 push @$cmd, '-cpu', $forcecpu;
3839 push @$cmd, get_cpu_options
($conf, $arch, $kvm, $kvm_off, $machine_version, $winversion, $gpu_passthrough);
3842 PVE
::QemuServer
::Memory
::config
($conf, $vmid, $sockets, $cores, $defaults, $hotplug_features, $cmd);
3844 push @$cmd, '-S' if $conf->{freeze
};
3846 push @$cmd, '-k', $conf->{keyboard
} if defined($conf->{keyboard
});
3848 my $guest_agent = parse_guest_agent
($conf);
3850 if ($guest_agent->{enabled
}) {
3851 my $qgasocket = PVE
::QemuServer
::Helpers
::qmp_socket
($vmid, 1);
3852 push @$devices, '-chardev', "socket,path=$qgasocket,server=on,wait=off,id=qga0";
3854 if (!$guest_agent->{type
} || $guest_agent->{type
} eq 'virtio') {
3855 my $pciaddr = print_pci_addr
("qga0", $bridges, $arch, $machine_type);
3856 push @$devices, '-device', "virtio-serial,id=qga0$pciaddr";
3857 push @$devices, '-device', 'virtserialport,chardev=qga0,name=org.qemu.guest_agent.0';
3858 } elsif ($guest_agent->{type
} eq 'isa') {
3859 push @$devices, '-device', "isa-serial,chardev=qga0";
3863 my $rng = $conf->{rng0
} ? parse_rng
($conf->{rng0
}) : undef;
3864 if ($rng && $version_guard->(4, 1, 2)) {
3865 check_rng_source
($rng->{source
});
3867 my $max_bytes = $rng->{max_bytes
} // $rng_fmt->{max_bytes
}->{default};
3868 my $period = $rng->{period
} // $rng_fmt->{period
}->{default};
3869 my $limiter_str = "";
3871 $limiter_str = ",max-bytes=$max_bytes,period=$period";
3874 my $rng_addr = print_pci_addr
("rng0", $bridges, $arch, $machine_type);
3875 push @$devices, '-object', "rng-random,filename=$rng->{source},id=rng0";
3876 push @$devices, '-device', "virtio-rng-pci,rng=rng0$limiter_str$rng_addr";
3881 if ($qxlnum || $vga->{type
} =~ /^virtio/) {
3884 for (my $i = 1; $i < $qxlnum; $i++){
3885 push @$devices, '-device', print_vga_device
(
3886 $conf, $vga, $arch, $machine_version, $machine_type, $i, $qxlnum, $bridges);
3889 # assume other OS works like Linux
3890 my ($ram, $vram) = ("134217728", "67108864");
3891 if ($vga->{memory
}) {
3892 $ram = PVE
::Tools
::convert_size
($qxlnum*4*$vga->{memory
}, 'mb' => 'b');
3893 $vram = PVE
::Tools
::convert_size
($qxlnum*2*$vga->{memory
}, 'mb' => 'b');
3895 push @$cmd, '-global', "qxl-vga.ram_size=$ram";
3896 push @$cmd, '-global', "qxl-vga.vram_size=$vram";
3900 my $pciaddr = print_pci_addr
("spice", $bridges, $arch, $machine_type);
3902 my $pfamily = PVE
::Tools
::get_host_address_family
($nodename);
3903 my @nodeaddrs = PVE
::Tools
::getaddrinfo_all
('localhost', family
=> $pfamily);
3904 die "failed to get an ip address of type $pfamily for 'localhost'\n" if !@nodeaddrs;
3906 push @$devices, '-device', "virtio-serial,id=spice$pciaddr";
3907 push @$devices, '-chardev', "spicevmc,id=vdagent,name=vdagent";
3908 push @$devices, '-device', "virtserialport,chardev=vdagent,name=com.redhat.spice.0";
3910 my $localhost = PVE
::Network
::addr_to_ip
($nodeaddrs[0]->{addr
});
3911 $spice_port = PVE
::Tools
::next_spice_port
($pfamily, $localhost);
3913 my $spice_enhancement_str = $conf->{spice_enhancements
} // '';
3914 my $spice_enhancement = parse_property_string
($spice_enhancements_fmt, $spice_enhancement_str);
3915 if ($spice_enhancement->{foldersharing
}) {
3916 push @$devices, '-chardev', "spiceport,id=foldershare,name=org.spice-space.webdav.0";
3917 push @$devices, '-device', "virtserialport,chardev=foldershare,name=org.spice-space.webdav.0";
3920 my $spice_opts = "tls-port=${spice_port},addr=$localhost,tls-ciphers=HIGH,seamless-migration=on";
3921 $spice_opts .= ",streaming-video=$spice_enhancement->{videostreaming}"
3922 if $spice_enhancement->{videostreaming
};
3924 push @$devices, '-spice', "$spice_opts";
3927 # enable balloon by default, unless explicitly disabled
3928 if (!defined($conf->{balloon
}) || $conf->{balloon
}) {
3929 my $pciaddr = print_pci_addr
("balloon0", $bridges, $arch, $machine_type);
3930 my $ballooncmd = "virtio-balloon-pci,id=balloon0$pciaddr";
3931 $ballooncmd .= ",free-page-reporting=on" if min_version
($machine_version, 6, 2);
3932 push @$devices, '-device', $ballooncmd;
3935 if ($conf->{watchdog
}) {
3936 my $wdopts = parse_watchdog
($conf->{watchdog
});
3937 my $pciaddr = print_pci_addr
("watchdog", $bridges, $arch, $machine_type);
3938 my $watchdog = $wdopts->{model
} || 'i6300esb';
3939 push @$devices, '-device', "$watchdog$pciaddr";
3940 push @$devices, '-watchdog-action', $wdopts->{action
} if $wdopts->{action
};
3944 my $scsicontroller = {};
3945 my $ahcicontroller = {};
3946 my $scsihw = defined($conf->{scsihw
}) ?
$conf->{scsihw
} : $defaults->{scsihw
};
3948 # Add iscsi initiator name if available
3949 if (my $initiator = get_initiator_name
()) {
3950 push @$devices, '-iscsi', "initiator-name=$initiator";
3953 PVE
::QemuConfig-
>foreach_volume($conf, sub {
3954 my ($ds, $drive) = @_;
3956 if (PVE
::Storage
::parse_volume_id
($drive->{file
}, 1)) {
3957 check_volume_storage_type
($storecfg, $drive->{file
});
3958 push @$vollist, $drive->{file
};
3961 # ignore efidisk here, already added in bios/fw handling code above
3962 return if $drive->{interface
} eq 'efidisk';
3964 return if $drive->{interface
} eq 'tpmstate';
3966 $use_virtio = 1 if $ds =~ m/^virtio/;
3968 $drive->{bootindex
} = $bootorder->{$ds} if $bootorder->{$ds};
3970 if ($drive->{interface
} eq 'virtio'){
3971 push @$cmd, '-object', "iothread,id=iothread-$ds" if $drive->{iothread
};
3974 if ($drive->{interface
} eq 'scsi') {
3976 my ($maxdev, $controller, $controller_prefix) = scsihw_infos
($conf, $drive);
3978 die "scsi$drive->{index}: machine version 4.1~pve2 or higher is required to use more than 14 SCSI disks\n"
3979 if $drive->{index} > 13 && !&$version_guard(4, 1, 2);
3981 my $pciaddr = print_pci_addr
("$controller_prefix$controller", $bridges, $arch, $machine_type);
3982 my $scsihw_type = $scsihw =~ m/^virtio-scsi-single/ ?
"virtio-scsi-pci" : $scsihw;
3985 if($conf->{scsihw
} && $conf->{scsihw
} eq "virtio-scsi-single" && $drive->{iothread
}){
3986 $iothread .= ",iothread=iothread-$controller_prefix$controller";
3987 push @$cmd, '-object', "iothread,id=iothread-$controller_prefix$controller";
3988 } elsif ($drive->{iothread
}) {
3990 "iothread is only valid with virtio disk or virtio-scsi-single controller, ignoring\n"
3995 if($conf->{scsihw
} && $conf->{scsihw
} eq "virtio-scsi-single" && $drive->{queues
}){
3996 $queues = ",num_queues=$drive->{queues}";
3999 push @$devices, '-device', "$scsihw_type,id=$controller_prefix$controller$pciaddr$iothread$queues"
4000 if !$scsicontroller->{$controller};
4001 $scsicontroller->{$controller}=1;
4004 if ($drive->{interface
} eq 'sata') {
4005 my $controller = int($drive->{index} / $PVE::QemuServer
::Drive
::MAX_SATA_DISKS
);
4006 my $pciaddr = print_pci_addr
("ahci$controller", $bridges, $arch, $machine_type);
4007 push @$devices, '-device', "ahci,id=ahci$controller,multifunction=on$pciaddr"
4008 if !$ahcicontroller->{$controller};
4009 $ahcicontroller->{$controller}=1;
4012 my $pbs_conf = $pbs_backing->{$ds};
4013 my $pbs_name = undef;
4015 $pbs_name = "drive-$ds-pbs";
4016 push @$devices, '-blockdev', print_pbs_blockdev
($pbs_conf, $pbs_name);
4019 my $drive_cmd = print_drive_commandline_full
(
4020 $storecfg, $vmid, $drive, $pbs_name, min_version
($kvmver, 6, 0));
4022 # extra protection for templates, but SATA and IDE don't support it..
4023 $drive_cmd .= ',readonly=on' if drive_is_read_only
($conf, $drive);
4025 push @$devices, '-drive',$drive_cmd;
4026 push @$devices, '-device', print_drivedevice_full
(
4027 $storecfg, $conf, $vmid, $drive, $bridges, $arch, $machine_type);
4030 for (my $i = 0; $i < $MAX_NETS; $i++) {
4031 my $netname = "net$i";
4033 next if !$conf->{$netname};
4034 my $d = parse_net
($conf->{$netname});
4037 $use_virtio = 1 if $d->{model
} eq 'virtio';
4039 $d->{bootindex
} = $bootorder->{$netname} if $bootorder->{$netname};
4041 my $netdevfull = print_netdev_full
($vmid, $conf, $arch, $d, $netname);
4042 push @$devices, '-netdev', $netdevfull;
4044 my $netdevicefull = print_netdevice_full
(
4045 $vmid, $conf, $d, $netname, $bridges, $use_old_bios_files, $arch, $machine_type);
4047 push @$devices, '-device', $netdevicefull;
4050 if ($conf->{ivshmem
}) {
4051 my $ivshmem = parse_property_string
($ivshmem_fmt, $conf->{ivshmem
});
4055 $bus = print_pcie_addr
("ivshmem");
4057 $bus = print_pci_addr
("ivshmem", $bridges, $arch, $machine_type);
4060 my $ivshmem_name = $ivshmem->{name
} // $vmid;
4061 my $path = '/dev/shm/pve-shm-' . $ivshmem_name;
4063 push @$devices, '-device', "ivshmem-plain,memdev=ivshmem$bus,";
4064 push @$devices, '-object', "memory-backend-file,id=ivshmem,share=on,mem-path=$path"
4065 .",size=$ivshmem->{size}M";
4068 # pci.4 is nested in pci.1
4069 $bridges->{1} = 1 if $bridges->{4};
4071 if (!$q35) { # add pci bridges
4072 if (min_version
($machine_version, 2, 3)) {
4076 $bridges->{3} = 1 if $scsihw =~ m/^virtio-scsi-single/;
4079 for my $k (sort {$b cmp $a} keys %$bridges) {
4080 next if $q35 && $k < 4; # q35.cfg already includes bridges up to 3
4083 if ($k == 2 && $legacy_igd) {
4086 my $pciaddr = print_pci_addr
("pci.$k_name", undef, $arch, $machine_type);
4087 my $devstr = "pci-bridge,id=pci.$k,chassis_nr=$k$pciaddr";
4089 if ($q35) { # add after -readconfig pve-q35.cfg
4090 splice @$devices, 2, 0, '-device', $devstr;
4092 unshift @$devices, '-device', $devstr if $k > 0;
4097 push @$machineFlags, 'accel=tcg';
4100 push @$machineFlags, 'smm=off' if should_disable_smm
($conf, $vga);
4102 my $machine_type_min = $machine_type;
4103 if ($add_pve_version) {
4104 $machine_type_min =~ s/\+pve\d+$//;
4105 $machine_type_min .= "+pve$required_pve_version";
4107 push @$machineFlags, "type=${machine_type_min}";
4109 push @$cmd, @$devices;
4110 push @$cmd, '-rtc', join(',', @$rtcFlags) if scalar(@$rtcFlags);
4111 push @$cmd, '-machine', join(',', @$machineFlags) if scalar(@$machineFlags);
4112 push @$cmd, '-global', join(',', @$globalFlags) if scalar(@$globalFlags);
4114 if (my $vmstate = $conf->{vmstate
}) {
4115 my $statepath = PVE
::Storage
::path
($storecfg, $vmstate);
4116 push @$vollist, $vmstate;
4117 push @$cmd, '-loadstate', $statepath;
4118 print "activating and using '$vmstate' as vmstate\n";
4121 if (PVE
::QemuConfig-
>is_template($conf)) {
4122 # needed to workaround base volumes being read-only
4123 push @$cmd, '-snapshot';
4127 if ($conf->{args
}) {
4128 my $aa = PVE
::Tools
::split_args
($conf->{args
});
4132 return wantarray ?
($cmd, $vollist, $spice_port) : $cmd;
4135 sub check_rng_source
{
4138 # mostly relevant for /dev/hwrng, but doesn't hurt to check others too
4139 die "cannot create VirtIO RNG device: source file '$source' doesn't exist\n"
4142 my $rng_current = '/sys/devices/virtual/misc/hw_random/rng_current';
4143 if ($source eq '/dev/hwrng' && file_read_firstline
($rng_current) eq 'none') {
4144 # Needs to abort, otherwise QEMU crashes on first rng access. Note that rng_current cannot
4145 # be changed to 'none' manually, so once the VM is past this point, it's no longer an issue.
4146 die "Cannot start VM with passed-through RNG device: '/dev/hwrng' exists, but"
4147 ." '$rng_current' is set to 'none'. Ensure that a compatible hardware-RNG is attached"
4155 my $res = mon_cmd
($vmid, 'query-spice');
4157 return $res->{'tls-port'} || $res->{'port'} || die "no spice port\n";
4160 sub vm_devices_list
{
4163 my $res = mon_cmd
($vmid, 'query-pci');
4164 my $devices_to_check = [];
4166 foreach my $pcibus (@$res) {
4167 push @$devices_to_check, @{$pcibus->{devices
}},
4170 while (@$devices_to_check) {
4172 for my $d (@$devices_to_check) {
4173 $devices->{$d->{'qdev_id'}} = 1 if $d->{'qdev_id'};
4174 next if !$d->{'pci_bridge'};
4176 $devices->{$d->{'qdev_id'}} += scalar(@{$d->{'pci_bridge'}->{devices
}});
4177 push @$to_check, @{$d->{'pci_bridge'}->{devices
}};
4179 $devices_to_check = $to_check;
4182 my $resblock = mon_cmd
($vmid, 'query-block');
4183 foreach my $block (@$resblock) {
4184 if($block->{device
} =~ m/^drive-(\S+)/){
4189 my $resmice = mon_cmd
($vmid, 'query-mice');
4190 foreach my $mice (@$resmice) {
4191 if ($mice->{name
} eq 'QEMU HID Tablet') {
4192 $devices->{tablet
} = 1;
4197 # for usb devices there is no query-usb
4198 # but we can iterate over the entries in
4199 # qom-list path=/machine/peripheral
4200 my $resperipheral = mon_cmd
($vmid, 'qom-list', path
=> '/machine/peripheral');
4201 foreach my $per (@$resperipheral) {
4202 if ($per->{name
} =~ m/^usb\d+$/) {
4203 $devices->{$per->{name
}} = 1;
4211 my ($storecfg, $conf, $vmid, $deviceid, $device, $arch, $machine_type) = @_;
4213 my $q35 = PVE
::QemuServer
::Machine
::machine_type_is_q35
($conf);
4215 my $devices_list = vm_devices_list
($vmid);
4216 return 1 if defined($devices_list->{$deviceid});
4218 # add PCI bridge if we need it for the device
4219 qemu_add_pci_bridge
($storecfg, $conf, $vmid, $deviceid, $arch, $machine_type);
4221 if ($deviceid eq 'tablet') {
4222 qemu_deviceadd
($vmid, print_tabletdevice_full
($conf, $arch));
4223 } elsif ($deviceid eq 'keyboard') {
4224 qemu_deviceadd
($vmid, print_keyboarddevice_full
($conf, $arch));
4225 } elsif ($deviceid =~ m/^usb(\d+)$/) {
4226 die "usb hotplug currently not reliable\n";
4227 # since we can't reliably hot unplug all added usb devices and usb
4228 # passthrough breaks live migration we disable usb hotplugging for now
4229 #qemu_deviceadd($vmid, PVE::QemuServer::USB::print_usbdevice_full($conf, $deviceid, $device));
4230 } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
4231 qemu_iothread_add
($vmid, $deviceid, $device);
4233 qemu_driveadd
($storecfg, $vmid, $device);
4234 my $devicefull = print_drivedevice_full
($storecfg, $conf, $vmid, $device, undef, $arch, $machine_type);
4236 qemu_deviceadd
($vmid, $devicefull);
4237 eval { qemu_deviceaddverify
($vmid, $deviceid); };
4239 eval { qemu_drivedel
($vmid, $deviceid); };
4243 } elsif ($deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/) {
4244 my $scsihw = defined($conf->{scsihw
}) ?
$conf->{scsihw
} : "lsi";
4245 my $pciaddr = print_pci_addr
($deviceid, undef, $arch, $machine_type);
4246 my $scsihw_type = $scsihw eq 'virtio-scsi-single' ?
"virtio-scsi-pci" : $scsihw;
4248 my $devicefull = "$scsihw_type,id=$deviceid$pciaddr";
4250 if($deviceid =~ m/^virtioscsi(\d+)$/ && $device->{iothread
}) {
4251 qemu_iothread_add
($vmid, $deviceid, $device);
4252 $devicefull .= ",iothread=iothread-$deviceid";
4255 if($deviceid =~ m/^virtioscsi(\d+)$/ && $device->{queues
}) {
4256 $devicefull .= ",num_queues=$device->{queues}";
4259 qemu_deviceadd
($vmid, $devicefull);
4260 qemu_deviceaddverify
($vmid, $deviceid);
4261 } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
4262 qemu_findorcreatescsihw
($storecfg,$conf, $vmid, $device, $arch, $machine_type);
4263 qemu_driveadd
($storecfg, $vmid, $device);
4265 my $devicefull = print_drivedevice_full
($storecfg, $conf, $vmid, $device, undef, $arch, $machine_type);
4266 eval { qemu_deviceadd
($vmid, $devicefull); };
4268 eval { qemu_drivedel
($vmid, $deviceid); };
4272 } elsif ($deviceid =~ m/^(net)(\d+)$/) {
4273 return if !qemu_netdevadd
($vmid, $conf, $arch, $device, $deviceid);
4275 my $machine_type = PVE
::QemuServer
::Machine
::qemu_machine_pxe
($vmid, $conf);
4276 my $use_old_bios_files = undef;
4277 ($use_old_bios_files, $machine_type) = qemu_use_old_bios_files
($machine_type);
4279 my $netdevicefull = print_netdevice_full
(
4280 $vmid, $conf, $device, $deviceid, undef, $use_old_bios_files, $arch, $machine_type);
4281 qemu_deviceadd
($vmid, $netdevicefull);
4283 qemu_deviceaddverify
($vmid, $deviceid);
4284 qemu_set_link_status
($vmid, $deviceid, !$device->{link_down
});
4287 eval { qemu_netdevdel
($vmid, $deviceid); };
4291 } elsif (!$q35 && $deviceid =~ m/^(pci\.)(\d+)$/) {
4293 my $pciaddr = print_pci_addr
($deviceid, undef, $arch, $machine_type);
4294 my $devicefull = "pci-bridge,id=pci.$bridgeid,chassis_nr=$bridgeid$pciaddr";
4296 qemu_deviceadd
($vmid, $devicefull);
4297 qemu_deviceaddverify
($vmid, $deviceid);
4299 die "can't hotplug device '$deviceid'\n";
4305 # fixme: this should raise exceptions on error!
4306 sub vm_deviceunplug
{
4307 my ($vmid, $conf, $deviceid) = @_;
4309 my $devices_list = vm_devices_list
($vmid);
4310 return 1 if !defined($devices_list->{$deviceid});
4312 my $bootdisks = PVE
::QemuServer
::Drive
::get_bootdisks
($conf);
4313 die "can't unplug bootdisk '$deviceid'\n" if grep {$_ eq $deviceid} @$bootdisks;
4315 if ($deviceid eq 'tablet' || $deviceid eq 'keyboard') {
4316 qemu_devicedel
($vmid, $deviceid);
4317 } elsif ($deviceid =~ m/^usb\d+$/) {
4318 die "usb hotplug currently not reliable\n";
4319 # when unplugging usb devices this way, there may be remaining usb
4320 # controllers/hubs so we disable it for now
4321 #qemu_devicedel($vmid, $deviceid);
4322 #qemu_devicedelverify($vmid, $deviceid);
4323 } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
4324 my $device = parse_drive
($deviceid, $conf->{$deviceid});
4326 qemu_devicedel
($vmid, $deviceid);
4327 qemu_devicedelverify
($vmid, $deviceid);
4328 qemu_drivedel
($vmid, $deviceid);
4329 qemu_iothread_del
($vmid, $deviceid, $device);
4330 } elsif ($deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/) {
4331 qemu_devicedel
($vmid, $deviceid);
4332 qemu_devicedelverify
($vmid, $deviceid);
4333 } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
4334 my $device = parse_drive
($deviceid, $conf->{$deviceid});
4336 qemu_devicedel
($vmid, $deviceid);
4337 qemu_devicedelverify
($vmid, $deviceid);
4338 qemu_drivedel
($vmid, $deviceid);
4339 qemu_deletescsihw
($conf, $vmid, $deviceid);
4341 qemu_iothread_del
($vmid, "virtioscsi$device->{index}", $device)
4342 if $conf->{scsihw
} && ($conf->{scsihw
} eq 'virtio-scsi-single');
4343 } elsif ($deviceid =~ m/^(net)(\d+)$/) {
4344 qemu_devicedel
($vmid, $deviceid);
4345 qemu_devicedelverify
($vmid, $deviceid);
4346 qemu_netdevdel
($vmid, $deviceid);
4348 die "can't unplug device '$deviceid'\n";
4354 sub qemu_deviceadd
{
4355 my ($vmid, $devicefull) = @_;
4357 $devicefull = "driver=".$devicefull;
4358 my %options = split(/[=,]/, $devicefull);
4360 mon_cmd
($vmid, "device_add" , %options);
4363 sub qemu_devicedel
{
4364 my ($vmid, $deviceid) = @_;
4366 my $ret = mon_cmd
($vmid, "device_del", id
=> $deviceid);
4369 sub qemu_iothread_add
{
4370 my ($vmid, $deviceid, $device) = @_;
4372 if ($device->{iothread
}) {
4373 my $iothreads = vm_iothreads_list
($vmid);
4374 qemu_objectadd
($vmid, "iothread-$deviceid", "iothread") if !$iothreads->{"iothread-$deviceid"};
4378 sub qemu_iothread_del
{
4379 my ($vmid, $deviceid, $device) = @_;
4381 if ($device->{iothread
}) {
4382 my $iothreads = vm_iothreads_list
($vmid);
4383 qemu_objectdel
($vmid, "iothread-$deviceid") if $iothreads->{"iothread-$deviceid"};
4387 sub qemu_objectadd
{
4388 my ($vmid, $objectid, $qomtype) = @_;
4390 mon_cmd
($vmid, "object-add", id
=> $objectid, "qom-type" => $qomtype);
4395 sub qemu_objectdel
{
4396 my ($vmid, $objectid) = @_;
4398 mon_cmd
($vmid, "object-del", id
=> $objectid);
4404 my ($storecfg, $vmid, $device) = @_;
4406 my $kvmver = get_running_qemu_version
($vmid);
4407 my $io_uring = min_version
($kvmver, 6, 0);
4408 my $drive = print_drive_commandline_full
($storecfg, $vmid, $device, undef, $io_uring);
4409 $drive =~ s/\\/\\\\/g;
4410 my $ret = PVE
::QemuServer
::Monitor
::hmp_cmd
($vmid, "drive_add auto \"$drive\"");
4412 # If the command succeeds qemu prints: "OK
"
4413 return 1 if $ret =~ m/OK/s;
4415 die "adding drive failed
: $ret\n";
4419 my ($vmid, $deviceid) = @_;
4421 my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_del drive-
$deviceid");
4424 return 1 if $ret eq "";
4426 # NB: device not found errors mean the drive was auto-deleted and we ignore the error
4427 return 1 if $ret =~ m/Device \'.*?\' not found/s;
4429 die "deleting drive
$deviceid failed
: $ret\n";
4432 sub qemu_deviceaddverify {
4433 my ($vmid, $deviceid) = @_;
4435 for (my $i = 0; $i <= 5; $i++) {
4436 my $devices_list = vm_devices_list($vmid);
4437 return 1 if defined($devices_list->{$deviceid});
4441 die "error on hotplug device
'$deviceid'\n";
4445 sub qemu_devicedelverify {
4446 my ($vmid, $deviceid) = @_;
4448 # need to verify that the device is correctly removed as device_del
4449 # is async and empty return is not reliable
4451 for (my $i = 0; $i <= 5; $i++) {
4452 my $devices_list = vm_devices_list($vmid);
4453 return 1 if !defined($devices_list->{$deviceid});
4457 die "error on hot-unplugging device
'$deviceid'\n";
4460 sub qemu_findorcreatescsihw {
4461 my ($storecfg, $conf, $vmid, $device, $arch, $machine_type) = @_;
4463 my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $device);
4465 my $scsihwid="$controller_prefix$controller";
4466 my $devices_list = vm_devices_list($vmid);
4468 if (!defined($devices_list->{$scsihwid})) {
4469 vm_deviceplug($storecfg, $conf, $vmid, $scsihwid, $device, $arch, $machine_type);
4475 sub qemu_deletescsihw {
4476 my ($conf, $vmid, $opt) = @_;
4478 my $device = parse_drive($opt, $conf->{$opt});
4480 if ($conf->{scsihw} && ($conf->{scsihw} eq 'virtio-scsi-single')) {
4481 vm_deviceunplug($vmid, $conf, "virtioscsi
$device->{index}");
4485 my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $device);
4487 my $devices_list = vm_devices_list($vmid);
4488 foreach my $opt (keys %{$devices_list}) {
4489 if (is_valid_drivename($opt)) {
4490 my $drive = parse_drive($opt, $conf->{$opt});
4491 if ($drive->{interface} eq 'scsi' && $drive->{index} < (($maxdev-1)*($controller+1))) {
4497 my $scsihwid="scsihw
$controller";
4499 vm_deviceunplug($vmid, $conf, $scsihwid);
4504 sub qemu_add_pci_bridge {
4505 my ($storecfg, $conf, $vmid, $device, $arch, $machine_type) = @_;
4511 print_pci_addr($device, $bridges, $arch, $machine_type);
4513 while (my ($k, $v) = each %$bridges) {
4516 return 1 if !defined($bridgeid) || $bridgeid < 1;
4518 my $bridge = "pci
.$bridgeid";
4519 my $devices_list = vm_devices_list($vmid);
4521 if (!defined($devices_list->{$bridge})) {
4522 vm_deviceplug($storecfg, $conf, $vmid, $bridge, $arch, $machine_type);
4528 sub qemu_set_link_status {
4529 my ($vmid, $device, $up) = @_;
4531 mon_cmd($vmid, "set_link
", name => $device,
4532 up => $up ? JSON::true : JSON::false);
4535 sub qemu_netdevadd {
4536 my ($vmid, $conf, $arch, $device, $deviceid) = @_;
4538 my $netdev = print_netdev_full($vmid, $conf, $arch, $device, $deviceid, 1);
4539 my %options = split(/[=,]/, $netdev);
4541 if (defined(my $vhost = $options{vhost})) {
4542 $options{vhost} = JSON::boolean(PVE::JSONSchema::parse_boolean($vhost));
4545 if (defined(my $queues = $options{queues})) {
4546 $options{queues} = $queues + 0;
4549 mon_cmd($vmid, "netdev_add
", %options);
4553 sub qemu_netdevdel {
4554 my ($vmid, $deviceid) = @_;
4556 mon_cmd($vmid, "netdev_del
", id => $deviceid);
4559 sub qemu_usb_hotplug {
4560 my ($storecfg, $conf, $vmid, $deviceid, $device, $arch, $machine_type) = @_;
4564 # remove the old one first
4565 vm_deviceunplug($vmid, $conf, $deviceid);
4567 # check if xhci controller is necessary and available
4568 if ($device->{usb3}) {
4570 my $devicelist = vm_devices_list($vmid);
4572 if (!$devicelist->{xhci}) {
4573 my $pciaddr = print_pci_addr("xhci
", undef, $arch, $machine_type);
4574 qemu_deviceadd($vmid, "nec-usb-xhci
,id
=xhci
$pciaddr");
4577 my $d = parse_usb_device($device->{host});
4578 $d->{usb3} = $device->{usb3};
4581 vm_deviceplug($storecfg, $conf, $vmid, $deviceid, $d, $arch, $machine_type);
4584 sub qemu_cpu_hotplug {
4585 my ($vmid, $conf, $vcpus) = @_;
4587 my $machine_type = PVE::QemuServer::Machine::get_current_qemu_machine($vmid);
4590 $sockets = $conf->{smp} if $conf->{smp}; # old style - no longer iused
4591 $sockets = $conf->{sockets} if $conf->{sockets};
4592 my $cores = $conf->{cores} || 1;
4593 my $maxcpus = $sockets * $cores;
4595 $vcpus = $maxcpus if !$vcpus;
4597 die "you can
't add more vcpus than maxcpus\n"
4598 if $vcpus > $maxcpus;
4600 my $currentvcpus = $conf->{vcpus} || $maxcpus;
4602 if ($vcpus < $currentvcpus) {
4604 if (PVE::QemuServer::Machine::machine_version($machine_type, 2, 7)) {
4606 for (my $i = $currentvcpus; $i > $vcpus; $i--) {
4607 qemu_devicedel($vmid, "cpu$i");
4609 my $currentrunningvcpus = undef;
4611 $currentrunningvcpus = mon_cmd($vmid, "query-cpus-fast");
4612 last if scalar(@{$currentrunningvcpus}) == $i-1;
4613 raise_param_exc({ vcpus => "error unplugging cpu$i" }) if $retry > 5;
4617 #update conf after each succesfull cpu unplug
4618 $conf->{vcpus} = scalar(@{$currentrunningvcpus});
4619 PVE::QemuConfig->write_config($vmid, $conf);
4622 die "cpu hot-unplugging requires qemu version 2.7 or higher\n";
4628 my $currentrunningvcpus = mon_cmd($vmid, "query-cpus-fast");
4629 die "vcpus in running vm does not match its configuration\n"
4630 if scalar(@{$currentrunningvcpus}) != $currentvcpus;
4632 if (PVE::QemuServer::Machine::machine_version($machine_type, 2, 7)) {
4634 for (my $i = $currentvcpus+1; $i <= $vcpus; $i++) {
4635 my $cpustr = print_cpu_device($conf, $i);
4636 qemu_deviceadd($vmid, $cpustr);
4639 my $currentrunningvcpus = undef;
4641 $currentrunningvcpus = mon_cmd($vmid, "query-cpus-fast");
4642 last if scalar(@{$currentrunningvcpus}) == $i;
4643 raise_param_exc({ vcpus => "error hotplugging cpu$i" }) if $retry > 10;
4647 #update conf after each succesfull cpu hotplug
4648 $conf->{vcpus} = scalar(@{$currentrunningvcpus});
4649 PVE::QemuConfig->write_config($vmid, $conf);
4653 for (my $i = $currentvcpus; $i < $vcpus; $i++) {
4654 mon_cmd($vmid, "cpu-add", id => int($i));
4659 sub qemu_block_set_io_throttle {
4660 my ($vmid, $deviceid,
4661 $bps, $bps_rd, $bps_wr, $iops, $iops_rd, $iops_wr,
4662 $bps_max, $bps_rd_max, $bps_wr_max, $iops_max, $iops_rd_max, $iops_wr_max,
4663 $bps_max_length, $bps_rd_max_length, $bps_wr_max_length,
4664 $iops_max_length, $iops_rd_max_length, $iops_wr_max_length) = @_;
4666 return if !check_running($vmid) ;
4668 mon_cmd($vmid, "block_set_io_throttle", device => $deviceid,
4670 bps_rd => int($bps_rd),
4671 bps_wr => int($bps_wr),
4673 iops_rd => int($iops_rd),
4674 iops_wr => int($iops_wr),
4675 bps_max => int($bps_max),
4676 bps_rd_max => int($bps_rd_max),
4677 bps_wr_max => int($bps_wr_max),
4678 iops_max => int($iops_max),
4679 iops_rd_max => int($iops_rd_max),
4680 iops_wr_max => int($iops_wr_max),
4681 bps_max_length => int($bps_max_length),
4682 bps_rd_max_length => int($bps_rd_max_length),
4683 bps_wr_max_length => int($bps_wr_max_length),
4684 iops_max_length => int($iops_max_length),
4685 iops_rd_max_length => int($iops_rd_max_length),
4686 iops_wr_max_length => int($iops_wr_max_length),
4691 sub qemu_block_resize {
4692 my ($vmid, $deviceid, $storecfg, $volid, $size) = @_;
4694 my $running = check_running($vmid);
4696 $size = 0 if !PVE::Storage::volume_resize($storecfg, $volid, $size, $running);
4698 return if !$running;
4700 my $padding = (1024 - $size % 1024) % 1024;
4701 $size = $size + $padding;
4706 device => $deviceid,
4712 sub qemu_volume_snapshot {
4713 my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
4715 my $running = check_running($vmid);
4717 if ($running && do_snapshots_with_qemu($storecfg, $volid, $deviceid)) {
4718 mon_cmd($vmid, 'blockdev-snapshot-internal-sync
', device => $deviceid, name => $snap);
4720 PVE::Storage::volume_snapshot($storecfg, $volid, $snap);
4724 sub qemu_volume_snapshot_delete {
4725 my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
4727 my $running = check_running($vmid);
4732 my $conf = PVE::QemuConfig->load_config($vmid);
4733 PVE::QemuConfig->foreach_volume($conf, sub {
4734 my ($ds, $drive) = @_;
4735 $running = 1 if $drive->{file} eq $volid;
4739 if ($running && do_snapshots_with_qemu($storecfg, $volid, $deviceid)) {
4740 mon_cmd($vmid, 'blockdev-snapshot-delete-internal-sync
', device => $deviceid, name => $snap);
4742 PVE::Storage::volume_snapshot_delete($storecfg, $volid, $snap, $running);
4746 sub set_migration_caps {
4747 my ($vmid, $savevm) = @_;
4749 my $qemu_support = eval { mon_cmd($vmid, "query-proxmox-support") };
4751 my $bitmap_prop = $savevm ? 'pbs-dirty-bitmap-savevm
' : 'pbs-dirty-bitmap-migration
';
4752 my $dirty_bitmaps = $qemu_support->{$bitmap_prop} ? 1 : 0;
4757 "auto-converge" => 1,
4759 "x-rdma-pin-all" => 0,
4762 "dirty-bitmaps" => $dirty_bitmaps,
4765 my $supported_capabilities = mon_cmd($vmid, "query-migrate-capabilities");
4767 for my $supported_capability (@$supported_capabilities) {
4769 capability => $supported_capability->{capability},
4770 state => $enabled_cap->{$supported_capability->{capability}} ? JSON::true : JSON::false,
4774 mon_cmd($vmid, "migrate-set-capabilities", capabilities => $cap_ref);
4778 my ($conf, $func, @param) = @_;
4782 my $test_volid = sub {
4783 my ($key, $drive, $snapname) = @_;
4785 my $volid = $drive->{file};
4788 $volhash->{$volid}->{cdrom} //= 1;
4789 $volhash->{$volid}->{cdrom} = 0 if !drive_is_cdrom($drive);
4791 my $replicate = $drive->{replicate} // 1;
4792 $volhash->{$volid}->{replicate} //= 0;
4793 $volhash->{$volid}->{replicate} = 1 if $replicate;
4795 $volhash->{$volid}->{shared} //= 0;
4796 $volhash->{$volid}->{shared} = 1 if $drive->{shared};
4798 $volhash->{$volid}->{referenced_in_config} //= 0;
4799 $volhash->{$volid}->{referenced_in_config} = 1 if !defined($snapname);
4801 $volhash->{$volid}->{referenced_in_snapshot}->{$snapname} = 1
4802 if defined($snapname);
4804 my $size = $drive->{size};
4805 $volhash->{$volid}->{size} //= $size if $size;
4807 $volhash->{$volid}->{is_vmstate} //= 0;
4808 $volhash->{$volid}->{is_vmstate} = 1 if $key eq 'vmstate
';
4810 $volhash->{$volid}->{is_tpmstate} //= 0;
4811 $volhash->{$volid}->{is_tpmstate} = 1 if $key eq 'tpmstate0
';
4813 $volhash->{$volid}->{is_unused} //= 0;
4814 $volhash->{$volid}->{is_unused} = 1 if $key =~ /^unused\d+$/;
4816 $volhash->{$volid}->{drivename} = $key if is_valid_drivename($key);
4819 my $include_opts = {
4820 extra_keys => ['vmstate
'],
4821 include_unused => 1,
4824 PVE::QemuConfig->foreach_volume_full($conf, $include_opts, $test_volid);
4825 foreach my $snapname (keys %{$conf->{snapshots}}) {
4826 my $snap = $conf->{snapshots}->{$snapname};
4827 PVE::QemuConfig->foreach_volume_full($snap, $include_opts, $test_volid, $snapname);
4830 foreach my $volid (keys %$volhash) {
4831 &$func($volid, $volhash->{$volid}, @param);
4835 my $fast_plug_option = {
4843 'vmstatestorage
' => 1,
4848 for my $opt (keys %$confdesc_cloudinit) {
4849 $fast_plug_option->{$opt} = 1;
4852 # hotplug changes in [PENDING]
4853 # $selection hash can be used to only apply specified options, for
4854 # example: { cores => 1 } (only apply changed 'cores
')
4855 # $errors ref is used to return error messages
4856 sub vmconfig_hotplug_pending {
4857 my ($vmid, $conf, $storecfg, $selection, $errors) = @_;
4859 my $defaults = load_defaults();
4860 my $arch = get_vm_arch($conf);
4861 my $machine_type = get_vm_machine($conf, undef, $arch);
4863 # commit values which do not have any impact on running VM first
4864 # Note: those option cannot raise errors, we we do not care about
4865 # $selection and always apply them.
4867 my $add_error = sub {
4868 my ($opt, $msg) = @_;
4869 $errors->{$opt} = "hotplug problem - $msg";
4873 foreach my $opt (keys %{$conf->{pending}}) { # add/change
4874 if ($fast_plug_option->{$opt}) {
4875 $conf->{$opt} = $conf->{pending}->{$opt};
4876 delete $conf->{pending}->{$opt};
4882 PVE::QemuConfig->write_config($vmid, $conf);
4885 my $hotplug_features = parse_hotplug_features(defined($conf->{hotplug}) ? $conf->{hotplug} : '1');
4887 my $cgroup = PVE::QemuServer::CGroup->new($vmid);
4888 my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
4889 foreach my $opt (sort keys %$pending_delete_hash) {
4890 next if $selection && !$selection->{$opt};
4891 my $force = $pending_delete_hash->{$opt}->{force};
4893 if ($opt eq 'hotplug
') {
4894 die "skip\n" if ($conf->{hotplug} =~ /memory/);
4895 } elsif ($opt eq 'tablet
') {
4896 die "skip\n" if !$hotplug_features->{usb};
4897 if ($defaults->{tablet}) {
4898 vm_deviceplug($storecfg, $conf, $vmid, 'tablet
', $arch, $machine_type);
4899 vm_deviceplug($storecfg, $conf, $vmid, 'keyboard
', $arch, $machine_type)
4900 if $arch eq 'aarch64
';
4902 vm_deviceunplug($vmid, $conf, 'tablet
');
4903 vm_deviceunplug($vmid, $conf, 'keyboard
') if $arch eq 'aarch64
';
4905 } elsif ($opt =~ m/^usb\d+/) {
4907 # since we cannot reliably hot unplug usb devices we are disabling it
4908 #die "skip\n" if !$hotplug_features->{usb} || $conf->{$opt} =~ m/spice/i;
4909 #vm_deviceunplug($vmid, $conf, $opt);
4910 } elsif ($opt eq 'vcpus
') {
4911 die "skip\n" if !$hotplug_features->{cpu};
4912 qemu_cpu_hotplug($vmid, $conf, undef);
4913 } elsif ($opt eq 'balloon
') {
4914 # enable balloon device is not hotpluggable
4915 die "skip\n" if defined($conf->{balloon}) && $conf->{balloon} == 0;
4916 # here we reset the ballooning value to memory
4917 my $balloon = $conf->{memory} || $defaults->{memory};
4918 mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
4919 } elsif ($fast_plug_option->{$opt}) {
4921 } elsif ($opt =~ m/^net(\d+)$/) {
4922 die "skip\n" if !$hotplug_features->{network};
4923 vm_deviceunplug($vmid, $conf, $opt);
4924 } elsif (is_valid_drivename($opt)) {
4925 die "skip\n" if !$hotplug_features->{disk} || $opt =~ m/(ide|sata)(\d+)/;
4926 vm_deviceunplug($vmid, $conf, $opt);
4927 vmconfig_delete_or_detach_drive($vmid, $storecfg, $conf, $opt, $force);
4928 } elsif ($opt =~ m/^memory$/) {
4929 die "skip\n" if !$hotplug_features->{memory};
4930 PVE::QemuServer::Memory::qemu_memory_hotplug($vmid, $conf, $defaults, $opt);
4931 } elsif ($opt eq 'cpuunits
') {
4932 $cgroup->change_cpu_shares(undef);
4933 } elsif ($opt eq 'cpulimit
') {
4934 $cgroup->change_cpu_quota(undef, undef); # reset, cgroup module can better decide values
4940 &$add_error($opt, $err) if $err ne "skip\n";
4942 delete $conf->{$opt};
4943 PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
4947 foreach my $opt (keys %{$conf->{pending}}) {
4948 next if $selection && !$selection->{$opt};
4949 my $value = $conf->{pending}->{$opt};
4951 if ($opt eq 'hotplug
') {
4952 die "skip\n" if ($value =~ /memory/) || ($value !~ /memory/ && $conf->{hotplug} =~ /memory/);
4953 } elsif ($opt eq 'tablet
') {
4954 die "skip\n" if !$hotplug_features->{usb};
4956 vm_deviceplug($storecfg, $conf, $vmid, 'tablet
', $arch, $machine_type);
4957 vm_deviceplug($storecfg, $conf, $vmid, 'keyboard
', $arch, $machine_type)
4958 if $arch eq 'aarch64
';
4959 } elsif ($value == 0) {
4960 vm_deviceunplug($vmid, $conf, 'tablet
');
4961 vm_deviceunplug($vmid, $conf, 'keyboard
') if $arch eq 'aarch64
';
4963 } elsif ($opt =~ m/^usb\d+$/) {
4965 # since we cannot reliably hot unplug usb devices we disable it for now
4966 #die "skip\n" if !$hotplug_features->{usb} || $value =~ m/spice/i;
4967 #my $d = eval { parse_property_string($usbdesc->{format}, $value) };
4968 #die "skip\n" if !$d;
4969 #qemu_usb_hotplug($storecfg, $conf, $vmid, $opt, $d, $arch, $machine_type);
4970 } elsif ($opt eq 'vcpus
') {
4971 die "skip\n" if !$hotplug_features->{cpu};
4972 qemu_cpu_hotplug($vmid, $conf, $value);
4973 } elsif ($opt eq 'balloon
') {
4974 # enable/disable balloning device is not hotpluggable
4975 my $old_balloon_enabled = !!(!defined($conf->{balloon}) || $conf->{balloon});
4976 my $new_balloon_enabled = !!(!defined($conf->{pending}->{balloon}) || $conf->{pending}->{balloon});
4977 die "skip\n" if $old_balloon_enabled != $new_balloon_enabled;
4979 # allow manual ballooning if shares is set to zero
4980 if ((defined($conf->{shares}) && ($conf->{shares} == 0))) {
4981 my $balloon = $conf->{pending}->{balloon} || $conf->{memory} || $defaults->{memory};
4982 mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
4984 } elsif ($opt =~ m/^net(\d+)$/) {
4985 # some changes can be done without hotplug
4986 vmconfig_update_net($storecfg, $conf, $hotplug_features->{network},
4987 $vmid, $opt, $value, $arch, $machine_type);
4988 } elsif (is_valid_drivename($opt)) {
4989 die "skip\n" if $opt eq 'efidisk0
' || $opt eq 'tpmstate0
';
4990 # some changes can be done without hotplug
4991 my $drive = parse_drive($opt, $value);
4992 if (drive_is_cloudinit($drive)) {
4993 PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid);
4995 vmconfig_update_disk($storecfg, $conf, $hotplug_features->{disk},
4996 $vmid, $opt, $value, $arch, $machine_type);
4997 } elsif ($opt =~ m/^memory$/) { #dimms
4998 die "skip\n" if !$hotplug_features->{memory};
4999 $value = PVE::QemuServer::Memory::qemu_memory_hotplug($vmid, $conf, $defaults, $opt, $value);
5000 } elsif ($opt eq 'cpuunits
') {
5001 my $new_cpuunits = PVE::CGroup::clamp_cpu_shares($conf->{pending}->{$opt}); #clamp
5002 $cgroup->change_cpu_shares($new_cpuunits);
5003 } elsif ($opt eq 'cpulimit
') {
5004 my $cpulimit = $conf->{pending}->{$opt} == 0 ? -1 : int($conf->{pending}->{$opt} * 100000);
5005 $cgroup->change_cpu_quota($cpulimit, 100000);
5006 } elsif ($opt eq 'agent
') {
5007 vmconfig_update_agent($conf, $opt, $value);
5009 die "skip\n"; # skip non-hot-pluggable options
5013 &$add_error($opt, $err) if $err ne "skip\n";
5015 $conf->{$opt} = $value;
5016 delete $conf->{pending}->{$opt};
5019 PVE::QemuConfig->write_config($vmid, $conf);
5021 if($hotplug_features->{cloudinit}) {
5022 my $pending = PVE::QemuServer::Cloudinit::get_pending_config($conf, $vmid);
5023 my $regenerate = undef;
5024 for my $item (@$pending) {
5025 $regenerate = 1 if defined($item->{delete}) or defined($item->{pending});
5027 PVE::QemuServer::vmconfig_update_cloudinit_drive($storecfg, $conf, $vmid) if $regenerate;
5031 sub try_deallocate_drive {
5032 my ($storecfg, $vmid, $conf, $key, $drive, $rpcenv, $authuser, $force) = @_;
5034 if (($force || $key =~ /^unused/) && !drive_is_cdrom($drive, 1)) {
5035 my $volid = $drive->{file};
5036 if (vm_is_volid_owner($storecfg, $vmid, $volid)) {
5037 my $sid = PVE::Storage::parse_volume_id($volid);
5038 $rpcenv->check($authuser, "/storage/$sid", ['Datastore
.AllocateSpace
']);
5040 # check if the disk is really unused
5041 die "unable to delete '$volid' - volume is still in use (snapshot?)\n"
5042 if PVE::QemuServer::Drive::is_volume_in_use($storecfg, $conf, $key, $volid);
5043 PVE::Storage::vdisk_free($storecfg, $volid);
5046 # If vm is not owner of this disk remove from config
5054 sub vmconfig_delete_or_detach_drive {
5055 my ($vmid, $storecfg, $conf, $opt, $force) = @_;
5057 my $drive = parse_drive($opt, $conf->{$opt});
5059 my $rpcenv = PVE::RPCEnvironment::get();
5060 my $authuser = $rpcenv->get_user();
5063 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM
.Config
.Disk
']);
5064 try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser, $force);
5066 vmconfig_register_unused_drive($storecfg, $vmid, $conf, $drive);
5072 sub vmconfig_apply_pending {
5073 my ($vmid, $conf, $storecfg, $errors) = @_;
5075 return if !scalar(keys %{$conf->{pending}});
5077 my $add_apply_error = sub {
5078 my ($opt, $msg) = @_;
5079 my $err_msg = "unable to apply pending change $opt : $msg";
5080 $errors->{$opt} = $err_msg;
5086 my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
5087 foreach my $opt (sort keys %$pending_delete_hash) {
5088 my $force = $pending_delete_hash->{$opt}->{force};
5090 if ($opt =~ m/^unused/) {
5091 die "internal error";
5092 } elsif (defined($conf->{$opt}) && is_valid_drivename($opt)) {
5093 vmconfig_delete_or_detach_drive($vmid, $storecfg, $conf, $opt, $force);
5097 $add_apply_error->($opt, $err);
5099 PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
5100 delete $conf->{$opt};
5104 PVE::QemuConfig->cleanup_pending($conf);
5106 my $generate_cloudnit = undef;
5108 foreach my $opt (keys %{$conf->{pending}}) { # add/change
5109 next if $opt eq 'delete'; # just to be sure
5111 if (defined($conf->{$opt}) && is_valid_drivename($opt)) {
5112 vmconfig_register_unused_drive($storecfg, $vmid, $conf, parse_drive($opt, $conf->{$opt}))
5116 $add_apply_error->($opt, $err);
5119 if (is_valid_drivename($opt)) {
5120 my $drive = parse_drive($opt, $conf->{pending}->{$opt});
5121 $generate_cloudnit = 1 if drive_is_cloudinit($drive);
5124 $conf->{$opt} = delete $conf->{pending}->{$opt};
5128 # write all changes at once to avoid unnecessary i/o
5129 PVE::QemuConfig->write_config($vmid, $conf);
5130 PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid) if $generate_cloudnit;
5133 sub vmconfig_update_net {
5134 my ($storecfg, $conf, $hotplug, $vmid, $opt, $value, $arch, $machine_type) = @_;
5136 my $newnet = parse_net($value);
5138 if ($conf->{$opt}) {
5139 my $oldnet = parse_net($conf->{$opt});
5141 if (safe_string_ne($oldnet->{model}, $newnet->{model}) ||
5142 safe_string_ne($oldnet->{macaddr}, $newnet->{macaddr}) ||
5143 safe_num_ne($oldnet->{queues}, $newnet->{queues}) ||
5144 !($newnet->{bridge} && $oldnet->{bridge})) { # bridge/nat mode change
5146 # for non online change, we try to hot-unplug
5147 die "skip\n" if !$hotplug;
5148 vm_deviceunplug($vmid, $conf, $opt);
5151 die "internal error" if $opt !~ m/net(\d+)/;
5152 my $iface = "tap${vmid}i$1";
5154 if (safe_string_ne($oldnet->{bridge}, $newnet->{bridge}) ||
5155 safe_num_ne($oldnet->{tag}, $newnet->{tag}) ||
5156 safe_string_ne($oldnet->{trunks}, $newnet->{trunks}) ||
5157 safe_num_ne($oldnet->{firewall}, $newnet->{firewall})) {
5158 PVE::Network::tap_unplug($iface);
5161 PVE::Network::SDN::Zones::tap_plug($iface, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
5163 PVE::Network::tap_plug($iface, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
5165 } elsif (safe_num_ne($oldnet->{rate}, $newnet->{rate})) {
5166 # Rate can be applied on its own but any change above needs to
5167 # include the rate in tap_plug since OVS resets everything.
5168 PVE::Network::tap_rate_limit($iface, $newnet->{rate});
5171 if (safe_string_ne($oldnet->{link_down}, $newnet->{link_down})) {
5172 qemu_set_link_status($vmid, $opt, !$newnet->{link_down});
5180 vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet, $arch, $machine_type);
5186 sub vmconfig_update_agent {
5187 my ($conf, $opt, $value) = @_;
5189 die "skip\n" if !$conf->{$opt};
5191 my $hotplug_options = { fstrim_cloned_disks => 1 };
5193 my $old_agent = parse_guest_agent($conf);
5194 my $agent = parse_guest_agent({$opt => $value});
5196 for my $option (keys %$agent) { # added/changed options
5197 next if defined($hotplug_options->{$option});
5198 die "skip\n" if safe_string_ne($agent->{$option}, $old_agent->{$option});
5201 for my $option (keys %$old_agent) { # removed options
5202 next if defined($hotplug_options->{$option});
5203 die "skip\n" if safe_string_ne($old_agent->{$option}, $agent->{$option});
5206 return; # either no actual change (e.g., format string reordered) or just hotpluggable changes
5209 sub vmconfig_update_disk {
5210 my ($storecfg, $conf, $hotplug, $vmid, $opt, $value, $arch, $machine_type) = @_;
5212 my $drive = parse_drive($opt, $value);
5214 if ($conf->{$opt} && (my $old_drive = parse_drive($opt, $conf->{$opt}))) {
5215 my $media = $drive->{media} || 'disk
';
5216 my $oldmedia = $old_drive->{media} || 'disk
';
5217 die "unable to change media type\n" if $media ne $oldmedia;
5219 if (!drive_is_cdrom($old_drive)) {
5221 if ($drive->{file} ne $old_drive->{file}) {
5223 die "skip\n" if !$hotplug;
5225 # unplug and register as unused
5226 vm_deviceunplug($vmid, $conf, $opt);
5227 vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive)
5230 # update existing disk
5232 # skip non hotpluggable value
5233 if (safe_string_ne($drive->{discard}, $old_drive->{discard}) ||
5234 safe_string_ne($drive->{iothread}, $old_drive->{iothread}) ||
5235 safe_string_ne($drive->{queues}, $old_drive->{queues}) ||
5236 safe_string_ne($drive->{cache}, $old_drive->{cache}) ||
5237 safe_string_ne($drive->{ssd}, $old_drive->{ssd})) {
5242 if (safe_num_ne($drive->{mbps}, $old_drive->{mbps}) ||
5243 safe_num_ne($drive->{mbps_rd}, $old_drive->{mbps_rd}) ||
5244 safe_num_ne($drive->{mbps_wr}, $old_drive->{mbps_wr}) ||
5245 safe_num_ne($drive->{iops}, $old_drive->{iops}) ||
5246 safe_num_ne($drive->{iops_rd}, $old_drive->{iops_rd}) ||
5247 safe_num_ne($drive->{iops_wr}, $old_drive->{iops_wr}) ||
5248 safe_num_ne($drive->{mbps_max}, $old_drive->{mbps_max}) ||
5249 safe_num_ne($drive->{mbps_rd_max}, $old_drive->{mbps_rd_max}) ||
5250 safe_num_ne($drive->{mbps_wr_max}, $old_drive->{mbps_wr_max}) ||
5251 safe_num_ne($drive->{iops_max}, $old_drive->{iops_max}) ||
5252 safe_num_ne($drive->{iops_rd_max}, $old_drive->{iops_rd_max}) ||
5253 safe_num_ne($drive->{iops_wr_max}, $old_drive->{iops_wr_max}) ||
5254 safe_num_ne($drive->{bps_max_length}, $old_drive->{bps_max_length}) ||
5255 safe_num_ne($drive->{bps_rd_max_length}, $old_drive->{bps_rd_max_length}) ||
5256 safe_num_ne($drive->{bps_wr_max_length}, $old_drive->{bps_wr_max_length}) ||
5257 safe_num_ne($drive->{iops_max_length}, $old_drive->{iops_max_length}) ||
5258 safe_num_ne($drive->{iops_rd_max_length}, $old_drive->{iops_rd_max_length}) ||
5259 safe_num_ne($drive->{iops_wr_max_length}, $old_drive->{iops_wr_max_length})) {
5261 qemu_block_set_io_throttle(
5263 ($drive->{mbps} || 0)*1024*1024,
5264 ($drive->{mbps_rd} || 0)*1024*1024,
5265 ($drive->{mbps_wr} || 0)*1024*1024,
5266 $drive->{iops} || 0,
5267 $drive->{iops_rd} || 0,
5268 $drive->{iops_wr} || 0,
5269 ($drive->{mbps_max} || 0)*1024*1024,
5270 ($drive->{mbps_rd_max} || 0)*1024*1024,
5271 ($drive->{mbps_wr_max} || 0)*1024*1024,
5272 $drive->{iops_max} || 0,
5273 $drive->{iops_rd_max} || 0,
5274 $drive->{iops_wr_max} || 0,
5275 $drive->{bps_max_length} || 1,
5276 $drive->{bps_rd_max_length} || 1,
5277 $drive->{bps_wr_max_length} || 1,
5278 $drive->{iops_max_length} || 1,
5279 $drive->{iops_rd_max_length} || 1,
5280 $drive->{iops_wr_max_length} || 1,
5290 if ($drive->{file} eq 'none
') {
5291 mon_cmd($vmid, "eject", force => JSON::true, id => "$opt");
5292 if (drive_is_cloudinit($old_drive)) {
5293 vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive);
5296 my $path = get_iso_path($storecfg, $vmid, $drive->{file});
5298 # force eject if locked
5299 mon_cmd($vmid, "eject", force => JSON::true, id => "$opt");
5302 mon_cmd($vmid, "blockdev-change-medium",
5303 id => "$opt", filename => "$path");
5311 die "skip\n" if !$hotplug || $opt =~ m/(ide|sata)(\d+)/;
5313 PVE::Storage::activate_volumes($storecfg, [$drive->{file}]) if $drive->{file} !~ m|^/dev/.+|;
5314 vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive, $arch, $machine_type);
5317 sub vmconfig_update_cloudinit_drive {
5318 my ($storecfg, $conf, $vmid) = @_;
5320 my $cloudinit_ds = undef;
5321 my $cloudinit_drive = undef;
5323 PVE::QemuConfig->foreach_volume($conf, sub {
5324 my ($ds, $drive) = @_;
5325 if (PVE::QemuServer::drive_is_cloudinit($drive)) {
5326 $cloudinit_ds = $ds;
5327 $cloudinit_drive = $drive;
5331 return if !$cloudinit_drive;
5333 PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid);
5334 my $running = PVE::QemuServer::check_running($vmid);
5337 my $path = PVE::Storage::path($storecfg, $cloudinit_drive->{file});
5339 mon_cmd($vmid, "eject", force => JSON::true, id => "$cloudinit_ds");
5340 mon_cmd($vmid, "blockdev-change-medium", id => "$cloudinit_ds", filename => "$path");
5345 # called in locked context by incoming migration
5346 sub vm_migrate_get_nbd_disks {
5347 my ($storecfg, $conf, $replicated_volumes) = @_;
5349 my $local_volumes = {};
5350 PVE::QemuConfig->foreach_volume($conf, sub {
5351 my ($ds, $drive) = @_;
5353 return if drive_is_cdrom($drive);
5354 return if $ds eq 'tpmstate0
';
5356 my $volid = $drive->{file};
5360 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
5362 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
5363 return if $scfg->{shared};
5365 # replicated disks re-use existing state via bitmap
5366 my $use_existing = $replicated_volumes->{$volid} ? 1 : 0;
5367 $local_volumes->{$ds} = [$volid, $storeid, $volname, $drive, $use_existing];
5369 return $local_volumes;
5372 # called in locked context by incoming migration
5373 sub vm_migrate_alloc_nbd_disks {
5374 my ($storecfg, $vmid, $source_volumes, $storagemap) = @_;
5377 foreach my $opt (sort keys %$source_volumes) {
5378 my ($volid, $storeid, $volname, $drive, $use_existing, $format) = @{$source_volumes->{$opt}};
5380 if ($use_existing) {
5381 $nbd->{$opt}->{drivestr} = print_drive($drive);
5382 $nbd->{$opt}->{volid} = $volid;
5383 $nbd->{$opt}->{replicated} = 1;
5387 # storage mapping + volname = regular migration
5388 # storage mapping + format = remote migration
5389 # order of precedence, filtered by whether storage supports it:
5390 # 1. explicit requested format
5391 # 2. format of current volume
5392 # 3. default format of storage
5393 if (!$storagemap->{identity}) {
5394 $storeid = PVE::JSONSchema::map_id($storagemap, $storeid);
5395 my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
5396 if (!$format || !grep { $format eq $_ } @$validFormats) {
5398 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
5399 my $fileFormat = qemu_img_format($scfg, $volname);
5400 $format = $fileFormat
5401 if grep { $fileFormat eq $_ } @$validFormats;
5403 $format //= $defFormat;
5406 # can't happen
for remote migration
, so
$volname is always
defined
5407 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
5408 $format = qemu_img_format
($scfg, $volname);
5411 my $size = $drive->{size
} / 1024;
5412 my $newvolid = PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid, $format, undef, $size);
5413 my $newdrive = $drive;
5414 $newdrive->{format
} = $format;
5415 $newdrive->{file
} = $newvolid;
5416 my $drivestr = print_drive
($newdrive);
5417 $nbd->{$opt}->{drivestr
} = $drivestr;
5418 $nbd->{$opt}->{volid
} = $newvolid;
5424 # see vm_start_nolock for parameters, additionally:
5426 # storagemap = parsed storage map for allocating NBD disks
5428 my ($storecfg, $vmid, $params, $migrate_opts) = @_;
5430 return PVE
::QemuConfig-
>lock_config($vmid, sub {
5431 my $conf = PVE
::QemuConfig-
>load_config($vmid, $migrate_opts->{migratedfrom
});
5433 die "you can't start a vm if it's a template\n"
5434 if !$params->{skiptemplate
} && PVE
::QemuConfig-
>is_template($conf);
5436 my $has_suspended_lock = PVE
::QemuConfig-
>has_lock($conf, 'suspended');
5437 my $has_backup_lock = PVE
::QemuConfig-
>has_lock($conf, 'backup');
5439 my $running = check_running
($vmid, undef, $migrate_opts->{migratedfrom
});
5441 if ($has_backup_lock && $running) {
5442 # a backup is currently running, attempt to start the guest in the
5443 # existing QEMU instance
5444 return vm_resume
($vmid);
5447 PVE
::QemuConfig-
>check_lock($conf)
5448 if !($params->{skiplock
} || $has_suspended_lock);
5450 $params->{resume
} = $has_suspended_lock || defined($conf->{vmstate
});
5452 die "VM $vmid already running\n" if $running;
5454 if (my $storagemap = $migrate_opts->{storagemap
}) {
5455 my $replicated = $migrate_opts->{replicated_volumes
};
5456 my $disks = vm_migrate_get_nbd_disks
($storecfg, $conf, $replicated);
5457 $migrate_opts->{nbd
} = vm_migrate_alloc_nbd_disks
($storecfg, $vmid, $disks, $storagemap);
5459 foreach my $opt (keys %{$migrate_opts->{nbd
}}) {
5460 $conf->{$opt} = $migrate_opts->{nbd
}->{$opt}->{drivestr
};
5464 return vm_start_nolock
($storecfg, $vmid, $conf, $params, $migrate_opts);
5470 # statefile => 'tcp', 'unix' for migration or path/volid for RAM state
5471 # skiplock => 0/1, skip checking for config lock
5472 # skiptemplate => 0/1, skip checking whether VM is template
5473 # forcemachine => to force Qemu machine (rollback/migration)
5474 # forcecpu => a QEMU '-cpu' argument string to override get_cpu_options
5475 # timeout => in seconds
5476 # paused => start VM in paused state (backup)
5477 # resume => resume from hibernation
5488 # nbd => volumes for NBD exports (vm_migrate_alloc_nbd_disks)
5489 # migratedfrom => source node
5490 # spice_ticket => used for spice migration, passed via tunnel/stdin
5491 # network => CIDR of migration network
5492 # type => secure/insecure - tunnel over encrypted connection or plain-text
5493 # nbd_proto_version => int, 0 for TCP, 1 for UNIX
5494 # replicated_volumes => which volids should be re-used with bitmaps for nbd migration
5495 # offline_volumes => new volids of offline migrated disks like tpmstate and cloudinit, not yet
5496 # contained in config
5497 sub vm_start_nolock
{
5498 my ($storecfg, $vmid, $conf, $params, $migrate_opts) = @_;
5500 my $statefile = $params->{statefile
};
5501 my $resume = $params->{resume
};
5503 my $migratedfrom = $migrate_opts->{migratedfrom
};
5504 my $migration_type = $migrate_opts->{type
};
5508 # clean up leftover reboot request files
5509 eval { clear_reboot_request
($vmid); };
5512 if (!$statefile && scalar(keys %{$conf->{pending
}})) {
5513 vmconfig_apply_pending
($vmid, $conf, $storecfg);
5514 $conf = PVE
::QemuConfig-
>load_config($vmid); # update/reload
5517 # don't regenerate the ISO if the VM is started as part of a live migration
5518 # this way we can reuse the old ISO with the correct config
5519 PVE
::QemuServer
::Cloudinit
::generate_cloudinitconfig
($conf, $vmid) if !$migratedfrom;
5521 # override offline migrated volumes, conf is out of date still
5522 if (my $offline_volumes = $migrate_opts->{offline_volumes
}) {
5523 for my $key (sort keys $offline_volumes->%*) {
5524 my $parsed = parse_drive
($key, $conf->{$key});
5525 $parsed->{file
} = $offline_volumes->{$key};
5526 $conf->{$key} = print_drive
($parsed);
5530 my $defaults = load_defaults
();
5532 # set environment variable useful inside network script
5533 $ENV{PVE_MIGRATED_FROM
} = $migratedfrom if $migratedfrom;
5535 PVE
::GuestHelpers
::exec_hookscript
($conf, $vmid, 'pre-start', 1);
5537 my $forcemachine = $params->{forcemachine
};
5538 my $forcecpu = $params->{forcecpu
};
5540 # enforce machine and CPU type on suspended vm to ensure HW compatibility
5541 $forcemachine = $conf->{runningmachine
};
5542 $forcecpu = $conf->{runningcpu
};
5543 print "Resuming suspended VM\n";
5546 my ($cmd, $vollist, $spice_port) = config_to_command
($storecfg, $vmid,
5547 $conf, $defaults, $forcemachine, $forcecpu, $params->{'pbs-backing'});
5550 my $get_migration_ip = sub {
5551 my ($nodename) = @_;
5553 return $migration_ip if defined($migration_ip);
5555 my $cidr = $migrate_opts->{network
};
5557 if (!defined($cidr)) {
5558 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
5559 $cidr = $dc_conf->{migration
}->{network
};
5562 if (defined($cidr)) {
5563 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
5565 die "could not get IP: no address configured on local " .
5566 "node for network '$cidr'\n" if scalar(@$ips) == 0;
5568 die "could not get IP: multiple addresses configured on local " .
5569 "node for network '$cidr'\n" if scalar(@$ips) > 1;
5571 $migration_ip = @$ips[0];
5574 $migration_ip = PVE
::Cluster
::remote_node_ip
($nodename, 1)
5575 if !defined($migration_ip);
5577 return $migration_ip;
5582 if ($statefile eq 'tcp') {
5583 my $localip = "localhost";
5584 my $datacenterconf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
5585 my $nodename = nodename
();
5587 if (!defined($migration_type)) {
5588 if (defined($datacenterconf->{migration
}->{type
})) {
5589 $migration_type = $datacenterconf->{migration
}->{type
};
5591 $migration_type = 'secure';
5595 if ($migration_type eq 'insecure') {
5596 $localip = $get_migration_ip->($nodename);
5597 $localip = "[$localip]" if Net
::IP
::ip_is_ipv6
($localip);
5600 my $pfamily = PVE
::Tools
::get_host_address_family
($nodename);
5601 my $migrate_port = PVE
::Tools
::next_migrate_port
($pfamily);
5602 $migrate_uri = "tcp:${localip}:${migrate_port}";
5603 push @$cmd, '-incoming', $migrate_uri;
5606 } elsif ($statefile eq 'unix') {
5607 # should be default for secure migrations as a ssh TCP forward
5608 # tunnel is not deterministic reliable ready and fails regurarly
5609 # to set up in time, so use UNIX socket forwards
5610 my $socket_addr = "/run/qemu-server/$vmid.migrate";
5611 unlink $socket_addr;
5613 $migrate_uri = "unix:$socket_addr";
5615 push @$cmd, '-incoming', $migrate_uri;
5618 } elsif (-e
$statefile) {
5619 push @$cmd, '-loadstate', $statefile;
5621 my $statepath = PVE
::Storage
::path
($storecfg, $statefile);
5622 push @$vollist, $statefile;
5623 push @$cmd, '-loadstate', $statepath;
5625 } elsif ($params->{paused
}) {
5629 my $start_timeout = $params->{timeout
} // config_aware_timeout
($conf, $resume);
5631 my $pci_devices = {}; # host pci devices
5632 for (my $i = 0; $i < $PVE::QemuServer
::PCI
::MAX_HOSTPCI_DEVICES
; $i++) {
5633 my $dev = $conf->{"hostpci$i"} or next;
5634 $pci_devices->{$i} = parse_hostpci
($dev);
5637 # do not reserve pciid for mediated devices, sysfs will error out for duplicate assignment
5638 my $real_pci_devices = [ grep { !(defined($_->{mdev
}) && scalar($_->{pciid
}->@*) == 1) } values $pci_devices->%* ];
5640 # map to a flat list of pci ids
5641 my $pci_id_list = [ map { $_->{id
} } map { $_->{pciid
}->@* } $real_pci_devices->@* ];
5643 # reserve all PCI IDs before actually doing anything with them
5644 PVE
::QemuServer
::PCI
::reserve_pci_usage
($pci_id_list, $vmid, $start_timeout);
5648 for my $id (sort keys %$pci_devices) {
5649 my $d = $pci_devices->{$id};
5650 for my $dev ($d->{pciid
}->@*) {
5651 my $info = PVE
::QemuServer
::PCI
::prepare_pci_device
($vmid, $dev->{id
}, $id, $d->{mdev
});
5653 # nvidia grid needs the uuid of the mdev as qemu parameter
5654 if ($d->{mdev
} && !defined($uuid) && $info->{vendor
} eq '10de') {
5655 $uuid = PVE
::QemuServer
::PCI
::generate_mdev_uuid
($vmid, $id);
5659 push @$cmd, '-uuid', $uuid if defined($uuid);
5662 eval { cleanup_pci_devices
($vmid, $conf) };
5667 PVE
::Storage
::activate_volumes
($storecfg, $vollist);
5670 run_command
(['/bin/systemctl', 'stop', "$vmid.scope"], outfunc
=> sub{}, errfunc
=> sub{});
5672 # Issues with the above 'stop' not being fully completed are extremely rare, a very low
5673 # timeout should be more than enough here...
5674 PVE
::Systemd
::wait_for_unit_removed
("$vmid.scope", 20);
5676 my $cpuunits = PVE
::CGroup
::clamp_cpu_shares
($conf->{cpuunits
});
5679 timeout
=> $statefile ?
undef : $start_timeout,
5684 # when migrating, prefix QEMU output so other side can pick up any
5685 # errors that might occur and show the user
5686 if ($migratedfrom) {
5687 $run_params{quiet
} = 1;
5688 $run_params{logfunc
} = sub { print "QEMU: $_[0]\n" };
5691 my %systemd_properties = (
5692 Slice
=> 'qemu.slice',
5693 KillMode
=> 'process',
5695 TimeoutStopUSec
=> ULONG_MAX
, # infinity
5698 if (PVE
::CGroup
::cgroup_mode
() == 2) {
5699 $systemd_properties{CPUWeight
} = $cpuunits;
5701 $systemd_properties{CPUShares
} = $cpuunits;
5704 if (my $cpulimit = $conf->{cpulimit
}) {
5705 $systemd_properties{CPUQuota
} = int($cpulimit * 100);
5707 $systemd_properties{timeout
} = 10 if $statefile; # setting up the scope shoul be quick
5709 my $run_qemu = sub {
5710 PVE
::Tools
::run_fork
sub {
5711 PVE
::Systemd
::enter_systemd_scope
($vmid, "Proxmox VE VM $vmid", %systemd_properties);
5714 if (my $tpm = $conf->{tpmstate0
}) {
5715 # start the TPM emulator so QEMU can connect on start
5716 $tpmpid = start_swtpm
($storecfg, $vmid, $tpm, $migratedfrom);
5719 my $exitcode = run_command
($cmd, %run_params);
5722 warn "stopping swtpm instance (pid $tpmpid) due to QEMU startup error\n";
5723 kill 'TERM', $tpmpid;
5725 die "QEMU exited with code $exitcode\n";
5730 if ($conf->{hugepages
}) {
5733 my $hugepages_topology = PVE
::QemuServer
::Memory
::hugepages_topology
($conf);
5734 my $hugepages_host_topology = PVE
::QemuServer
::Memory
::hugepages_host_topology
();
5736 PVE
::QemuServer
::Memory
::hugepages_mount
();
5737 PVE
::QemuServer
::Memory
::hugepages_allocate
($hugepages_topology, $hugepages_host_topology);
5739 eval { $run_qemu->() };
5741 PVE
::QemuServer
::Memory
::hugepages_reset
($hugepages_host_topology)
5742 if !$conf->{keephugepages
};
5746 PVE
::QemuServer
::Memory
::hugepages_pre_deallocate
($hugepages_topology)
5747 if !$conf->{keephugepages
};
5749 eval { PVE
::QemuServer
::Memory
::hugepages_update_locked
($code); };
5752 eval { $run_qemu->() };
5756 # deactivate volumes if start fails
5757 eval { PVE
::Storage
::deactivate_volumes
($storecfg, $vollist); };
5759 eval { cleanup_pci_devices
($vmid, $conf) };
5762 die "start failed: $err";
5765 # re-reserve all PCI IDs now that we can know the actual VM PID
5766 my $pid = PVE
::QemuServer
::Helpers
::vm_running_locally
($vmid);
5767 eval { PVE
::QemuServer
::PCI
::reserve_pci_usage
($pci_id_list, $vmid, undef, $pid) };
5770 print "migration listens on $migrate_uri\n" if $migrate_uri;
5771 $res->{migrate_uri
} = $migrate_uri;
5773 if ($statefile && $statefile ne 'tcp' && $statefile ne 'unix') {
5774 eval { mon_cmd
($vmid, "cont"); };
5778 #start nbd server for storage migration
5779 if (my $nbd = $migrate_opts->{nbd
}) {
5780 my $nbd_protocol_version = $migrate_opts->{nbd_proto_version
} // 0;
5782 my $migrate_storage_uri;
5783 # nbd_protocol_version > 0 for unix socket support
5784 if ($nbd_protocol_version > 0 && $migration_type eq 'secure') {
5785 my $socket_path = "/run/qemu-server/$vmid\_nbd.migrate";
5786 mon_cmd
($vmid, "nbd-server-start", addr
=> { type
=> 'unix', data
=> { path
=> $socket_path } } );
5787 $migrate_storage_uri = "nbd:unix:$socket_path";
5789 my $nodename = nodename
();
5790 my $localip = $get_migration_ip->($nodename);
5791 my $pfamily = PVE
::Tools
::get_host_address_family
($nodename);
5792 my $storage_migrate_port = PVE
::Tools
::next_migrate_port
($pfamily);
5794 mon_cmd
($vmid, "nbd-server-start", addr
=> {
5797 host
=> "${localip}",
5798 port
=> "${storage_migrate_port}",
5801 $localip = "[$localip]" if Net
::IP
::ip_is_ipv6
($localip);
5802 $migrate_storage_uri = "nbd:${localip}:${storage_migrate_port}";
5805 $res->{migrate_storage_uri
} = $migrate_storage_uri;
5807 foreach my $opt (sort keys %$nbd) {
5808 my $drivestr = $nbd->{$opt}->{drivestr
};
5809 my $volid = $nbd->{$opt}->{volid
};
5810 mon_cmd
($vmid, "nbd-server-add", device
=> "drive-$opt", writable
=> JSON
::true
);
5811 my $nbd_uri = "$migrate_storage_uri:exportname=drive-$opt";
5812 print "storage migration listens on $nbd_uri volume:$drivestr\n";
5813 print "re-using replicated volume: $opt - $volid\n"
5814 if $nbd->{$opt}->{replicated
};
5816 $res->{drives
}->{$opt} = $nbd->{$opt};
5817 $res->{drives
}->{$opt}->{nbd_uri
} = $nbd_uri;
5821 if ($migratedfrom) {
5823 set_migration_caps
($vmid);
5828 print "spice listens on port $spice_port\n";
5829 $res->{spice_port
} = $spice_port;
5830 if ($migrate_opts->{spice_ticket
}) {
5831 mon_cmd
($vmid, "set_password", protocol
=> 'spice', password
=>
5832 $migrate_opts->{spice_ticket
});
5833 mon_cmd
($vmid, "expire_password", protocol
=> 'spice', time => "+30");
5838 mon_cmd
($vmid, "balloon", value
=> $conf->{balloon
}*1024*1024)
5839 if !$statefile && $conf->{balloon
};
5841 foreach my $opt (keys %$conf) {
5842 next if $opt !~ m/^net\d+$/;
5843 my $nicconf = parse_net
($conf->{$opt});
5844 qemu_set_link_status
($vmid, $opt, 0) if $nicconf->{link_down
};
5848 mon_cmd
($vmid, 'qom-set',
5849 path
=> "machine/peripheral/balloon0",
5850 property
=> "guest-stats-polling-interval",
5851 value
=> 2) if (!defined($conf->{balloon
}) || $conf->{balloon
});
5854 print "Resumed VM, removing state\n";
5855 if (my $vmstate = $conf->{vmstate
}) {
5856 PVE
::Storage
::deactivate_volumes
($storecfg, [$vmstate]);
5857 PVE
::Storage
::vdisk_free
($storecfg, $vmstate);
5859 delete $conf->@{qw(lock vmstate runningmachine runningcpu)};
5860 PVE
::QemuConfig-
>write_config($vmid, $conf);
5863 PVE
::GuestHelpers
::exec_hookscript
($conf, $vmid, 'post-start');
5868 sub vm_commandline
{
5869 my ($storecfg, $vmid, $snapname) = @_;
5871 my $conf = PVE
::QemuConfig-
>load_config($vmid);
5873 my ($forcemachine, $forcecpu);
5875 my $snapshot = $conf->{snapshots
}->{$snapname};
5876 die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
5878 # check for machine or CPU overrides in snapshot
5879 $forcemachine = $snapshot->{runningmachine
};
5880 $forcecpu = $snapshot->{runningcpu
};
5882 $snapshot->{digest
} = $conf->{digest
}; # keep file digest for API
5887 my $defaults = load_defaults
();
5889 my $cmd = config_to_command
($storecfg, $vmid, $conf, $defaults, $forcemachine, $forcecpu);
5891 return PVE
::Tools
::cmd2string
($cmd);
5895 my ($vmid, $skiplock) = @_;
5897 PVE
::QemuConfig-
>lock_config($vmid, sub {
5899 my $conf = PVE
::QemuConfig-
>load_config($vmid);
5901 PVE
::QemuConfig-
>check_lock($conf) if !$skiplock;
5903 mon_cmd
($vmid, "system_reset");
5907 sub get_vm_volumes
{
5911 foreach_volid
($conf, sub {
5912 my ($volid, $attr) = @_;
5914 return if $volid =~ m
|^/|;
5916 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
5919 push @$vollist, $volid;
5925 sub cleanup_pci_devices
{
5926 my ($vmid, $conf) = @_;
5928 foreach my $key (keys %$conf) {
5929 next if $key !~ m/^hostpci(\d+)$/;
5930 my $hostpciindex = $1;
5931 my $uuid = PVE
::SysFSTools
::generate_mdev_uuid
($vmid, $hostpciindex);
5932 my $d = parse_hostpci
($conf->{$key});
5934 # NOTE: avoid PVE::SysFSTools::pci_cleanup_mdev_device as it requires PCI ID and we
5935 # don't want to break ABI just for this two liner
5936 my $dev_sysfs_dir = "/sys/bus/mdev/devices/$uuid";
5937 PVE
::SysFSTools
::file_write
("$dev_sysfs_dir/remove", "1") if -e
$dev_sysfs_dir;
5940 PVE
::QemuServer
::PCI
::remove_pci_reservation
($vmid);
5943 sub vm_stop_cleanup
{
5944 my ($storecfg, $vmid, $conf, $keepActive, $apply_pending_changes) = @_;
5949 my $vollist = get_vm_volumes
($conf);
5950 PVE
::Storage
::deactivate_volumes
($storecfg, $vollist);
5952 if (my $tpmdrive = $conf->{tpmstate0
}) {
5953 my $tpm = parse_drive
("tpmstate0", $tpmdrive);
5954 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($tpm->{file
}, 1);
5956 PVE
::Storage
::unmap_volume
($storecfg, $tpm->{file
});
5961 foreach my $ext (qw(mon qmp pid vnc qga)) {
5962 unlink "/var/run/qemu-server/${vmid}.$ext";
5965 if ($conf->{ivshmem
}) {
5966 my $ivshmem = parse_property_string
($ivshmem_fmt, $conf->{ivshmem
});
5967 # just delete it for now, VMs which have this already open do not
5968 # are affected, but new VMs will get a separated one. If this
5969 # becomes an issue we either add some sort of ref-counting or just
5970 # add a "don't delete on stop" flag to the ivshmem format.
5971 unlink '/dev/shm/pve-shm-' . ($ivshmem->{name
} // $vmid);
5974 cleanup_pci_devices
($vmid, $conf);
5976 vmconfig_apply_pending
($vmid, $conf, $storecfg) if $apply_pending_changes;
5978 warn $@ if $@; # avoid errors - just warn
5981 # call only in locked context
5983 my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive) = @_;
5985 my $pid = check_running
($vmid, $nocheck);
5990 $conf = PVE
::QemuConfig-
>load_config($vmid);
5991 PVE
::QemuConfig-
>check_lock($conf) if !$skiplock;
5992 if (!defined($timeout) && $shutdown && $conf->{startup
}) {
5993 my $opts = PVE
::JSONSchema
::pve_parse_startup_order
($conf->{startup
});
5994 $timeout = $opts->{down
} if $opts->{down
};
5996 PVE
::GuestHelpers
::exec_hookscript
($conf, $vmid, 'pre-stop');
6001 if (defined($conf) && get_qga_key
($conf, 'enabled')) {
6002 mon_cmd
($vmid, "guest-shutdown", timeout
=> $timeout);
6004 mon_cmd
($vmid, "system_powerdown");
6007 mon_cmd
($vmid, "quit");
6013 $timeout = 60 if !defined($timeout);
6016 while (($count < $timeout) && check_running
($vmid, $nocheck)) {
6021 if ($count >= $timeout) {
6023 warn "VM still running - terminating now with SIGTERM\n";
6026 die "VM quit/powerdown failed - got timeout\n";
6029 vm_stop_cleanup
($storecfg, $vmid, $conf, $keepActive, 1) if $conf;
6033 if (!check_running
($vmid, $nocheck)) {
6034 warn "Unexpected: VM shutdown command failed, but VM not running anymore..\n";
6038 warn "VM quit/powerdown failed - terminating now with SIGTERM\n";
6041 die "VM quit/powerdown failed\n";
6049 while (($count < $timeout) && check_running
($vmid, $nocheck)) {
6054 if ($count >= $timeout) {
6055 warn "VM still running - terminating now with SIGKILL\n";
6060 vm_stop_cleanup
($storecfg, $vmid, $conf, $keepActive, 1) if $conf;
6063 # Note: use $nocheck to skip tests if VM configuration file exists.
6064 # We need that when migration VMs to other nodes (files already moved)
6065 # Note: we set $keepActive in vzdump stop mode - volumes need to stay active
6067 my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive, $migratedfrom) = @_;
6069 $force = 1 if !defined($force) && !$shutdown;
6072 my $pid = check_running
($vmid, $nocheck, $migratedfrom);
6073 kill 15, $pid if $pid;
6074 my $conf = PVE
::QemuConfig-
>load_config($vmid, $migratedfrom);
6075 vm_stop_cleanup
($storecfg, $vmid, $conf, $keepActive, 0);
6079 PVE
::QemuConfig-
>lock_config($vmid, sub {
6080 _do_vm_stop
($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive);
6085 my ($vmid, $timeout) = @_;
6087 PVE
::QemuConfig-
>lock_config($vmid, sub {
6090 # only reboot if running, as qmeventd starts it again on a stop event
6091 return if !check_running
($vmid);
6093 create_reboot_request
($vmid);
6095 my $storecfg = PVE
::Storage
::config
();
6096 _do_vm_stop
($storecfg, $vmid, undef, undef, $timeout, 1);
6100 # avoid that the next normal shutdown will be confused for a reboot
6101 clear_reboot_request
($vmid);
6107 # note: if using the statestorage parameter, the caller has to check privileges
6109 my ($vmid, $skiplock, $includestate, $statestorage) = @_;
6116 PVE
::QemuConfig-
>lock_config($vmid, sub {
6118 $conf = PVE
::QemuConfig-
>load_config($vmid);
6120 my $is_backing_up = PVE
::QemuConfig-
>has_lock($conf, 'backup');
6121 PVE
::QemuConfig-
>check_lock($conf)
6122 if !($skiplock || $is_backing_up);
6124 die "cannot suspend to disk during backup\n"
6125 if $is_backing_up && $includestate;
6127 if ($includestate) {
6128 $conf->{lock} = 'suspending';
6129 my $date = strftime
("%Y-%m-%d", localtime(time()));
6130 $storecfg = PVE
::Storage
::config
();
6131 if (!$statestorage) {
6132 $statestorage = find_vmstate_storage
($conf, $storecfg);
6133 # check permissions for the storage
6134 my $rpcenv = PVE
::RPCEnvironment
::get
();
6135 if ($rpcenv->{type
} ne 'cli') {
6136 my $authuser = $rpcenv->get_user();
6137 $rpcenv->check($authuser, "/storage/$statestorage", ['Datastore.AllocateSpace']);
6142 $vmstate = PVE
::QemuConfig-
>__snapshot_save_vmstate(
6143 $vmid, $conf, "suspend-$date", $storecfg, $statestorage, 1);
6144 $path = PVE
::Storage
::path
($storecfg, $vmstate);
6145 PVE
::QemuConfig-
>write_config($vmid, $conf);
6147 mon_cmd
($vmid, "stop");
6151 if ($includestate) {
6153 PVE
::Storage
::activate_volumes
($storecfg, [$vmstate]);
6156 set_migration_caps
($vmid, 1);
6157 mon_cmd
($vmid, "savevm-start", statefile
=> $path);
6159 my $state = mon_cmd
($vmid, "query-savevm");
6160 if (!$state->{status
}) {
6161 die "savevm not active\n";
6162 } elsif ($state->{status
} eq 'active') {
6165 } elsif ($state->{status
} eq 'completed') {
6166 print "State saved, quitting\n";
6168 } elsif ($state->{status
} eq 'failed' && $state->{error
}) {
6169 die "query-savevm failed with error '$state->{error}'\n"
6171 die "query-savevm returned status '$state->{status}'\n";
6177 PVE
::QemuConfig-
>lock_config($vmid, sub {
6178 $conf = PVE
::QemuConfig-
>load_config($vmid);
6180 # cleanup, but leave suspending lock, to indicate something went wrong
6182 mon_cmd
($vmid, "savevm-end");
6183 PVE
::Storage
::deactivate_volumes
($storecfg, [$vmstate]);
6184 PVE
::Storage
::vdisk_free
($storecfg, $vmstate);
6185 delete $conf->@{qw(vmstate runningmachine runningcpu)};
6186 PVE
::QemuConfig-
>write_config($vmid, $conf);
6192 die "lock changed unexpectedly\n"
6193 if !PVE
::QemuConfig-
>has_lock($conf, 'suspending');
6195 mon_cmd
($vmid, "quit");
6196 $conf->{lock} = 'suspended';
6197 PVE
::QemuConfig-
>write_config($vmid, $conf);
6203 my ($vmid, $skiplock, $nocheck) = @_;
6205 PVE
::QemuConfig-
>lock_config($vmid, sub {
6206 my $res = mon_cmd
($vmid, 'query-status');
6207 my $resume_cmd = 'cont';
6210 if ($res->{status
}) {
6211 return if $res->{status
} eq 'running'; # job done, go home
6212 $resume_cmd = 'system_wakeup' if $res->{status
} eq 'suspended';
6213 $reset = 1 if $res->{status
} eq 'shutdown';
6218 my $conf = PVE
::QemuConfig-
>load_config($vmid);
6220 PVE
::QemuConfig-
>check_lock($conf)
6221 if !($skiplock || PVE
::QemuConfig-
>has_lock($conf, 'backup'));
6225 # required if a VM shuts down during a backup and we get a resume
6226 # request before the backup finishes for example
6227 mon_cmd
($vmid, "system_reset");
6229 mon_cmd
($vmid, $resume_cmd);
6234 my ($vmid, $skiplock, $key) = @_;
6236 PVE
::QemuConfig-
>lock_config($vmid, sub {
6238 my $conf = PVE
::QemuConfig-
>load_config($vmid);
6240 # there is no qmp command, so we use the human monitor command
6241 my $res = PVE
::QemuServer
::Monitor
::hmp_cmd
($vmid, "sendkey $key");
6242 die $res if $res ne '';
6246 # vzdump restore implementaion
6248 sub tar_archive_read_firstfile
{
6249 my $archive = shift;
6251 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
6253 # try to detect archive type first
6254 my $pid = open (my $fh, '-|', 'tar', 'tf', $archive) ||
6255 die "unable to open file '$archive'\n";
6256 my $firstfile = <$fh>;
6260 die "ERROR: archive contaions no data\n" if !$firstfile;
6266 sub tar_restore_cleanup
{
6267 my ($storecfg, $statfile) = @_;
6269 print STDERR
"starting cleanup\n";
6271 if (my $fd = IO
::File-
>new($statfile, "r")) {
6272 while (defined(my $line = <$fd>)) {
6273 if ($line =~ m/vzdump:([^\s:]*):(\S+)$/) {
6276 if ($volid =~ m
|^/|) {
6277 unlink $volid || die 'unlink failed\n';
6279 PVE
::Storage
::vdisk_free
($storecfg, $volid);
6281 print STDERR
"temporary volume '$volid' sucessfuly removed\n";
6283 print STDERR
"unable to cleanup '$volid' - $@" if $@;
6285 print STDERR
"unable to parse line in statfile - $line";
6292 sub restore_file_archive
{
6293 my ($archive, $vmid, $user, $opts) = @_;
6295 return restore_vma_archive
($archive, $vmid, $user, $opts)
6298 my $info = PVE
::Storage
::archive_info
($archive);
6299 my $format = $opts->{format
} // $info->{format
};
6300 my $comp = $info->{compression
};
6302 # try to detect archive format
6303 if ($format eq 'tar') {
6304 return restore_tar_archive
($archive, $vmid, $user, $opts);
6306 return restore_vma_archive
($archive, $vmid, $user, $opts, $comp);
6310 # hepler to remove disks that will not be used after restore
6311 my $restore_cleanup_oldconf = sub {
6312 my ($storecfg, $vmid, $oldconf, $virtdev_hash) = @_;
6314 my $kept_disks = {};
6316 PVE
::QemuConfig-
>foreach_volume($oldconf, sub {
6317 my ($ds, $drive) = @_;
6319 return if drive_is_cdrom
($drive, 1);
6321 my $volid = $drive->{file
};
6322 return if !$volid || $volid =~ m
|^/|;
6324 my ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid);
6325 return if !$path || !$owner || ($owner != $vmid);
6327 # Note: only delete disk we want to restore
6328 # other volumes will become unused
6329 if ($virtdev_hash->{$ds}) {
6330 eval { PVE
::Storage
::vdisk_free
($storecfg, $volid); };
6335 $kept_disks->{$volid} = 1;
6339 # after the restore we have no snapshots anymore
6340 for my $snapname (keys $oldconf->{snapshots
}->%*) {
6341 my $snap = $oldconf->{snapshots
}->{$snapname};
6342 if ($snap->{vmstate
}) {
6343 eval { PVE
::Storage
::vdisk_free
($storecfg, $snap->{vmstate
}); };
6349 for my $volid (keys $kept_disks->%*) {
6350 eval { PVE
::Storage
::volume_snapshot_delete
($storecfg, $volid, $snapname); };
6356 # Helper to parse vzdump backup device hints
6358 # $rpcenv: Environment, used to ckeck storage permissions
6359 # $user: User ID, to check storage permissions
6360 # $storecfg: Storage configuration
6361 # $fh: the file handle for reading the configuration
6362 # $devinfo: should contain device sizes for all backu-up'ed devices
6363 # $options: backup options (pool, default storage)
6365 # Return: $virtdev_hash, updates $devinfo (add devname, virtdev, format, storeid)
6366 my $parse_backup_hints = sub {
6367 my ($rpcenv, $user, $storecfg, $fh, $devinfo, $options) = @_;
6369 my $check_storage = sub { # assert if an image can be allocate
6370 my ($storeid, $scfg) = @_;
6371 die "Content type 'images' is not available on storage '$storeid'\n"
6372 if !$scfg->{content
}->{images
};
6373 $rpcenv->check($user, "/storage/$storeid", ['Datastore.AllocateSpace'])
6374 if $user ne 'root@pam';
6377 my $virtdev_hash = {};
6378 while (defined(my $line = <$fh>)) {
6379 if ($line =~ m/^\#qmdump\#map:(\S+):(\S+):(\S*):(\S*):$/) {
6380 my ($virtdev, $devname, $storeid, $format) = ($1, $2, $3, $4);
6381 die "archive does not contain data for drive '$virtdev'\n"
6382 if !$devinfo->{$devname};
6384 if (defined($options->{storage
})) {
6385 $storeid = $options->{storage
} || 'local';
6386 } elsif (!$storeid) {
6389 $format = 'raw' if !$format;
6390 $devinfo->{$devname}->{devname
} = $devname;
6391 $devinfo->{$devname}->{virtdev
} = $virtdev;
6392 $devinfo->{$devname}->{format
} = $format;
6393 $devinfo->{$devname}->{storeid
} = $storeid;
6395 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
6396 $check_storage->($storeid, $scfg); # permission and content type check
6398 $virtdev_hash->{$virtdev} = $devinfo->{$devname};
6399 } elsif ($line =~ m/^((?:ide|sata|scsi)\d+):\s*(.*)\s*$/) {
6401 my $drive = parse_drive
($virtdev, $2);
6403 if (drive_is_cloudinit
($drive)) {
6404 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($drive->{file
});
6405 $storeid = $options->{storage
} if defined ($options->{storage
});
6406 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
6407 my $format = qemu_img_format
($scfg, $volname); # has 'raw' fallback
6409 $check_storage->($storeid, $scfg); # permission and content type check
6411 $virtdev_hash->{$virtdev} = {
6413 storeid
=> $storeid,
6414 size
=> PVE
::QemuServer
::Cloudinit
::CLOUDINIT_DISK_SIZE
,
6421 return $virtdev_hash;
6424 # Helper to allocate and activate all volumes required for a restore
6426 # $storecfg: Storage configuration
6427 # $virtdev_hash: as returned by parse_backup_hints()
6429 # Returns: { $virtdev => $volid }
6430 my $restore_allocate_devices = sub {
6431 my ($storecfg, $virtdev_hash, $vmid) = @_;
6434 foreach my $virtdev (sort keys %$virtdev_hash) {
6435 my $d = $virtdev_hash->{$virtdev};
6436 my $alloc_size = int(($d->{size
} + 1024 - 1)/1024);
6437 my $storeid = $d->{storeid
};
6438 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
6440 # test if requested format is supported
6441 my ($defFormat, $validFormats) = PVE
::Storage
::storage_default_format
($storecfg, $storeid);
6442 my $supported = grep { $_ eq $d->{format
} } @$validFormats;
6443 $d->{format
} = $defFormat if !$supported;
6446 if ($d->{is_cloudinit
}) {
6447 $name = "vm-$vmid-cloudinit";
6448 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
6449 if ($scfg->{path
}) {
6450 $name .= ".$d->{format}";
6454 my $volid = PVE
::Storage
::vdisk_alloc
(
6455 $storecfg, $storeid, $vmid, $d->{format
}, $name, $alloc_size);
6457 print STDERR
"new volume ID is '$volid'\n";
6458 $d->{volid
} = $volid;
6460 PVE
::Storage
::activate_volumes
($storecfg, [$volid]);
6462 $map->{$virtdev} = $volid;
6468 sub restore_update_config_line
{
6469 my ($cookie, $map, $line, $unique) = @_;
6471 return '' if $line =~ m/^\#qmdump\#/;
6472 return '' if $line =~ m/^\#vzdump\#/;
6473 return '' if $line =~ m/^lock:/;
6474 return '' if $line =~ m/^unused\d+:/;
6475 return '' if $line =~ m/^parent:/;
6479 my $dc = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
6480 if (($line =~ m/^(vlan(\d+)):\s*(\S+)\s*$/)) {
6481 # try to convert old 1.X settings
6482 my ($id, $ind, $ethcfg) = ($1, $2, $3);
6483 foreach my $devconfig (PVE
::Tools
::split_list
($ethcfg)) {
6484 my ($model, $macaddr) = split(/\=/, $devconfig);
6485 $macaddr = PVE
::Tools
::random_ether_addr
($dc->{mac_prefix
}) if !$macaddr || $unique;
6488 bridge
=> "vmbr$ind",
6489 macaddr
=> $macaddr,
6491 my $netstr = print_net
($net);
6493 $res .= "net$cookie->{netcount}: $netstr\n";
6494 $cookie->{netcount
}++;
6496 } elsif (($line =~ m/^(net\d+):\s*(\S+)\s*$/) && $unique) {
6497 my ($id, $netstr) = ($1, $2);
6498 my $net = parse_net
($netstr);
6499 $net->{macaddr
} = PVE
::Tools
::random_ether_addr
($dc->{mac_prefix
}) if $net->{macaddr
};
6500 $netstr = print_net
($net);
6501 $res .= "$id: $netstr\n";
6502 } elsif ($line =~ m/^((ide|scsi|virtio|sata|efidisk|tpmstate)\d+):\s*(\S+)\s*$/) {
6505 my $di = parse_drive
($virtdev, $value);
6506 if (defined($di->{backup
}) && !$di->{backup
}) {
6508 } elsif ($map->{$virtdev}) {
6509 delete $di->{format
}; # format can change on restore
6510 $di->{file
} = $map->{$virtdev};
6511 $value = print_drive
($di);
6512 $res .= "$virtdev: $value\n";
6516 } elsif (($line =~ m/^vmgenid: (.*)/)) {
6518 if ($vmgenid ne '0') {
6519 # always generate a new vmgenid if there was a valid one setup
6520 $vmgenid = generate_uuid
();
6522 $res .= "vmgenid: $vmgenid\n";
6523 } elsif (($line =~ m/^(smbios1: )(.*)/) && $unique) {
6524 my ($uuid, $uuid_str);
6525 UUID
::generate
($uuid);
6526 UUID
::unparse
($uuid, $uuid_str);
6527 my $smbios1 = parse_smbios1
($2);
6528 $smbios1->{uuid
} = $uuid_str;
6529 $res .= $1.print_smbios1
($smbios1)."\n";
6537 my $restore_deactivate_volumes = sub {
6538 my ($storecfg, $virtdev_hash) = @_;
6541 for my $dev (values $virtdev_hash->%*) {
6542 push $vollist->@*, $dev->{volid
} if $dev->{volid
};
6545 eval { PVE
::Storage
::deactivate_volumes
($storecfg, $vollist); };
6546 print STDERR
$@ if $@;
6549 my $restore_destroy_volumes = sub {
6550 my ($storecfg, $virtdev_hash) = @_;
6552 for my $dev (values $virtdev_hash->%*) {
6553 my $volid = $dev->{volid
} or next;
6555 PVE
::Storage
::vdisk_free
($storecfg, $volid);
6556 print STDERR
"temporary volume '$volid' sucessfuly removed\n";
6558 print STDERR
"unable to cleanup '$volid' - $@" if $@;
6562 my $restore_merge_config = sub {
6563 my ($filename, $backup_conf_raw, $override_conf) = @_;
6565 my $backup_conf = parse_vm_config
($filename, $backup_conf_raw);
6566 for my $key (keys $override_conf->%*) {
6567 $backup_conf->{$key} = $override_conf->{$key};
6570 return $backup_conf;
6574 my ($cfg, $vmid) = @_;
6576 my $info = PVE
::Storage
::vdisk_list
($cfg, undef, $vmid, undef, 'images');
6578 my $volid_hash = {};
6579 foreach my $storeid (keys %$info) {
6580 foreach my $item (@{$info->{$storeid}}) {
6581 next if !($item->{volid
} && $item->{size
});
6582 $item->{path
} = PVE
::Storage
::path
($cfg, $item->{volid
});
6583 $volid_hash->{$item->{volid
}} = $item;
6590 sub update_disk_config
{
6591 my ($vmid, $conf, $volid_hash) = @_;
6594 my $prefix = "VM $vmid";
6596 # used and unused disks
6597 my $referenced = {};
6599 # Note: it is allowed to define multiple storages with same path (alias), so
6600 # we need to check both 'volid' and real 'path' (two different volid can point
6601 # to the same path).
6603 my $referencedpath = {};
6606 PVE
::QemuConfig-
>foreach_volume($conf, sub {
6607 my ($opt, $drive) = @_;
6609 my $volid = $drive->{file
};
6611 my $volume = $volid_hash->{$volid};
6613 # mark volid as "in-use" for next step
6614 $referenced->{$volid} = 1;
6615 if ($volume && (my $path = $volume->{path
})) {
6616 $referencedpath->{$path} = 1;
6619 return if drive_is_cdrom
($drive);
6622 my ($updated, $msg) = PVE
::QemuServer
::Drive
::update_disksize
($drive, $volume->{size
});
6623 if (defined($updated)) {
6625 $conf->{$opt} = print_drive
($updated);
6626 print "$prefix ($opt): $msg\n";
6630 # remove 'unusedX' entry if volume is used
6631 PVE
::QemuConfig-
>foreach_unused_volume($conf, sub {
6632 my ($opt, $drive) = @_;
6634 my $volid = $drive->{file
};
6638 $path = $volid_hash->{$volid}->{path
} if $volid_hash->{$volid};
6639 if ($referenced->{$volid} || ($path && $referencedpath->{$path})) {
6640 print "$prefix remove entry '$opt', its volume '$volid' is in use\n";
6642 delete $conf->{$opt};
6645 $referenced->{$volid} = 1;
6646 $referencedpath->{$path} = 1 if $path;
6649 foreach my $volid (sort keys %$volid_hash) {
6650 next if $volid =~ m/vm-$vmid-state-/;
6651 next if $referenced->{$volid};
6652 my $path = $volid_hash->{$volid}->{path
};
6653 next if !$path; # just to be sure
6654 next if $referencedpath->{$path};
6656 my $key = PVE
::QemuConfig-
>add_unused_volume($conf, $volid);
6657 print "$prefix add unreferenced volume '$volid' as '$key' to config\n";
6658 $referencedpath->{$path} = 1; # avoid to add more than once (aliases)
6665 my ($vmid, $nolock, $dryrun) = @_;
6667 my $cfg = PVE
::Storage
::config
();
6669 print "rescan volumes...\n";
6670 my $volid_hash = scan_volids
($cfg, $vmid);
6672 my $updatefn = sub {
6675 my $conf = PVE
::QemuConfig-
>load_config($vmid);
6677 PVE
::QemuConfig-
>check_lock($conf);
6680 foreach my $volid (keys %$volid_hash) {
6681 my $info = $volid_hash->{$volid};
6682 $vm_volids->{$volid} = $info if $info->{vmid
} && $info->{vmid
} == $vmid;
6685 my $changes = update_disk_config
($vmid, $conf, $vm_volids);
6687 PVE
::QemuConfig-
>write_config($vmid, $conf) if $changes && !$dryrun;
6690 if (defined($vmid)) {
6694 PVE
::QemuConfig-
>lock_config($vmid, $updatefn, $vmid);
6697 my $vmlist = config_list
();
6698 foreach my $vmid (keys %$vmlist) {
6702 PVE
::QemuConfig-
>lock_config($vmid, $updatefn, $vmid);
6708 sub restore_proxmox_backup_archive
{
6709 my ($archive, $vmid, $user, $options) = @_;
6711 my $storecfg = PVE
::Storage
::config
();
6713 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($archive);
6714 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
6716 my $fingerprint = $scfg->{fingerprint
};
6717 my $keyfile = PVE
::Storage
::PBSPlugin
::pbs_encryption_key_file_name
($storecfg, $storeid);
6719 my $repo = PVE
::PBSClient
::get_repository
($scfg);
6720 my $namespace = $scfg->{namespace
};
6722 # This is only used for `pbs-restore` and the QEMU PBS driver (live-restore)
6723 my $password = PVE
::Storage
::PBSPlugin
::pbs_get_password
($scfg, $storeid);
6724 local $ENV{PBS_PASSWORD
} = $password;
6725 local $ENV{PBS_FINGERPRINT
} = $fingerprint if defined($fingerprint);
6727 my ($vtype, $pbs_backup_name, undef, undef, undef, undef, $format) =
6728 PVE
::Storage
::parse_volname
($storecfg, $archive);
6730 die "got unexpected vtype '$vtype'\n" if $vtype ne 'backup';
6732 die "got unexpected backup format '$format'\n" if $format ne 'pbs-vm';
6734 my $tmpdir = "/var/tmp/vzdumptmp$$";
6738 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
6739 # disable interrupts (always do cleanups)
6743 local $SIG{HUP
} = sub { print STDERR
"got interrupt - ignored\n"; };
6745 # Note: $oldconf is undef if VM does not exists
6746 my $cfs_path = PVE
::QemuConfig-
>cfs_config_path($vmid);
6747 my $oldconf = PVE
::Cluster
::cfs_read_file
($cfs_path);
6748 my $new_conf_raw = '';
6750 my $rpcenv = PVE
::RPCEnvironment
::get
();
6751 my $devinfo = {}; # info about drives included in backup
6752 my $virtdev_hash = {}; # info about allocated drives
6760 local $SIG{PIPE
} = sub { die "interrupted by signal\n"; };
6762 my $cfgfn = "$tmpdir/qemu-server.conf";
6763 my $firewall_config_fn = "$tmpdir/fw.conf";
6764 my $index_fn = "$tmpdir/index.json";
6766 my $cmd = "restore";
6768 my $param = [$pbs_backup_name, "index.json", $index_fn];
6769 PVE
::Storage
::PBSPlugin
::run_raw_client_cmd
($scfg, $storeid, $cmd, $param);
6770 my $index = PVE
::Tools
::file_get_contents
($index_fn);
6771 $index = decode_json
($index);
6773 foreach my $info (@{$index->{files
}}) {
6774 if ($info->{filename
} =~ m/^(drive-\S+).img.fidx$/) {
6776 if ($info->{size
} =~ m/^(\d+)$/) { # untaint size
6777 $devinfo->{$devname}->{size
} = $1;
6779 die "unable to parse file size in 'index.json' - got '$info->{size}'\n";
6784 my $is_qemu_server_backup = scalar(
6785 grep { $_->{filename
} eq 'qemu-server.conf.blob' } @{$index->{files
}}
6787 if (!$is_qemu_server_backup) {
6788 die "backup does not look like a qemu-server backup (missing 'qemu-server.conf' file)\n";
6790 my $has_firewall_config = scalar(grep { $_->{filename
} eq 'fw.conf.blob' } @{$index->{files
}});
6792 $param = [$pbs_backup_name, "qemu-server.conf", $cfgfn];
6793 PVE
::Storage
::PBSPlugin
::run_raw_client_cmd
($scfg, $storeid, $cmd, $param);
6795 if ($has_firewall_config) {
6796 $param = [$pbs_backup_name, "fw.conf", $firewall_config_fn];
6797 PVE
::Storage
::PBSPlugin
::run_raw_client_cmd
($scfg, $storeid, $cmd, $param);
6799 my $pve_firewall_dir = '/etc/pve/firewall';
6800 mkdir $pve_firewall_dir; # make sure the dir exists
6801 PVE
::Tools
::file_copy
($firewall_config_fn, "${pve_firewall_dir}/$vmid.fw");
6804 my $fh = IO
::File-
>new($cfgfn, "r") ||
6805 die "unable to read qemu-server.conf - $!\n";
6807 $virtdev_hash = $parse_backup_hints->($rpcenv, $user, $storecfg, $fh, $devinfo, $options);
6809 # fixme: rate limit?
6811 # create empty/temp config
6812 PVE
::Tools
::file_set_contents
($conffile, "memory: 128\nlock: create");
6814 $restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash) if $oldconf;
6817 my $map = $restore_allocate_devices->($storecfg, $virtdev_hash, $vmid);
6819 foreach my $virtdev (sort keys %$virtdev_hash) {
6820 my $d = $virtdev_hash->{$virtdev};
6821 next if $d->{is_cloudinit
}; # no need to restore cloudinit
6823 # this fails if storage is unavailable
6824 my $volid = $d->{volid
};
6825 my $path = PVE
::Storage
::path
($storecfg, $volid);
6827 # for live-restore we only want to preload the efidisk and TPM state
6828 next if $options->{live
} && $virtdev ne 'efidisk0' && $virtdev ne 'tpmstate0';
6831 if (defined(my $ns = $scfg->{namespace
})) {
6832 @ns_arg = ('--ns', $ns);
6835 my $pbs_restore_cmd = [
6836 '/usr/bin/pbs-restore',
6837 '--repository', $repo,
6840 "$d->{devname}.img.fidx",
6845 push @$pbs_restore_cmd, '--format', $d->{format
} if $d->{format
};
6846 push @$pbs_restore_cmd, '--keyfile', $keyfile if -e
$keyfile;
6848 if (PVE
::Storage
::volume_has_feature
($storecfg, 'sparseinit', $volid)) {
6849 push @$pbs_restore_cmd, '--skip-zero';
6852 my $dbg_cmdstring = PVE
::Tools
::cmd2string
($pbs_restore_cmd);
6853 print "restore proxmox backup image: $dbg_cmdstring\n";
6854 run_command
($pbs_restore_cmd);
6857 $fh->seek(0, 0) || die "seek failed - $!\n";
6859 my $cookie = { netcount
=> 0 };
6860 while (defined(my $line = <$fh>)) {
6861 $new_conf_raw .= restore_update_config_line
(
6873 if ($err || !$options->{live
}) {
6874 $restore_deactivate_volumes->($storecfg, $virtdev_hash);
6880 $restore_destroy_volumes->($storecfg, $virtdev_hash);
6884 if ($options->{live
}) {
6885 # keep lock during live-restore
6886 $new_conf_raw .= "\nlock: create";
6889 my $new_conf = $restore_merge_config->($conffile, $new_conf_raw, $options->{override_conf
});
6890 PVE
::QemuConfig-
>write_config($vmid, $new_conf);
6892 eval { rescan
($vmid, 1); };
6895 PVE
::AccessControl
::add_vm_to_pool
($vmid, $options->{pool
}) if $options->{pool
};
6897 if ($options->{live
}) {
6903 local $SIG{PIPE
} = sub { die "got signal ($!) - abort\n"; };
6905 my $conf = PVE
::QemuConfig-
>load_config($vmid);
6906 die "cannot do live-restore for template\n" if PVE
::QemuConfig-
>is_template($conf);
6908 # these special drives are already restored before start
6909 delete $devinfo->{'drive-efidisk0'};
6910 delete $devinfo->{'drive-tpmstate0-backup'};
6914 keyfile
=> $keyfile,
6915 snapshot
=> $pbs_backup_name,
6916 namespace
=> $namespace,
6918 pbs_live_restore
($vmid, $conf, $storecfg, $devinfo, $pbs_opts);
6920 PVE
::QemuConfig-
>remove_lock($vmid, "create");
6924 sub pbs_live_restore
{
6925 my ($vmid, $conf, $storecfg, $restored_disks, $opts) = @_;
6927 print "starting VM for live-restore\n";
6928 print "repository: '$opts->{repo}', snapshot: '$opts->{snapshot}'\n";
6930 my $pbs_backing = {};
6931 for my $ds (keys %$restored_disks) {
6932 $ds =~ m/^drive-(.*)$/;
6934 $pbs_backing->{$confname} = {
6935 repository
=> $opts->{repo
},
6936 snapshot
=> $opts->{snapshot
},
6937 archive
=> "$ds.img.fidx",
6939 $pbs_backing->{$confname}->{keyfile
} = $opts->{keyfile
} if -e
$opts->{keyfile
};
6940 $pbs_backing->{$confname}->{namespace
} = $opts->{namespace
} if defined($opts->{namespace
});
6942 my $drive = parse_drive
($confname, $conf->{$confname});
6943 print "restoring '$ds' to '$drive->{file}'\n";
6946 my $drives_streamed = 0;
6948 # make sure HA doesn't interrupt our restore by stopping the VM
6949 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid)) {
6950 run_command
(['ha-manager', 'set', "vm:$vmid", '--state', 'started']);
6953 # start VM with backing chain pointing to PBS backup, environment vars for PBS driver
6954 # in QEMU (PBS_PASSWORD and PBS_FINGERPRINT) are already set by our caller
6955 vm_start_nolock
($storecfg, $vmid, $conf, {paused
=> 1, 'pbs-backing' => $pbs_backing}, {});
6957 my $qmeventd_fd = register_qmeventd_handle
($vmid);
6959 # begin streaming, i.e. data copy from PBS to target disk for every vol,
6960 # this will effectively collapse the backing image chain consisting of
6961 # [target <- alloc-track -> PBS snapshot] to just [target] (alloc-track
6962 # removes itself once all backing images vanish with 'auto-remove=on')
6964 for my $ds (sort keys %$restored_disks) {
6965 my $job_id = "restore-$ds";
6966 mon_cmd
($vmid, 'block-stream',
6967 'job-id' => $job_id,
6970 $jobs->{$job_id} = {};
6973 mon_cmd
($vmid, 'cont');
6974 qemu_drive_mirror_monitor
($vmid, undef, $jobs, 'auto', 0, 'stream');
6976 print "restore-drive jobs finished successfully, removing all tracking block devices"
6977 ." to disconnect from Proxmox Backup Server\n";
6979 for my $ds (sort keys %$restored_disks) {
6980 mon_cmd
($vmid, 'blockdev-del', 'node-name' => "$ds-pbs");
6983 close($qmeventd_fd);
6989 warn "An error occurred during live-restore: $err\n";
6990 _do_vm_stop
($storecfg, $vmid, 1, 1, 10, 0, 1);
6991 die "live-restore failed\n";
6995 sub restore_vma_archive
{
6996 my ($archive, $vmid, $user, $opts, $comp) = @_;
6998 my $readfrom = $archive;
7000 my $cfg = PVE
::Storage
::config
();
7002 my $bwlimit = $opts->{bwlimit
};
7004 my $dbg_cmdstring = '';
7005 my $add_pipe = sub {
7007 push @$commands, $cmd;
7008 $dbg_cmdstring .= ' | ' if length($dbg_cmdstring);
7009 $dbg_cmdstring .= PVE
::Tools
::cmd2string
($cmd);
7014 if ($archive eq '-') {
7017 # If we use a backup from a PVE defined storage we also consider that
7018 # storage's rate limit:
7019 my (undef, $volid) = PVE
::Storage
::path_to_volume_id
($cfg, $archive);
7020 if (defined($volid)) {
7021 my ($sid, undef) = PVE
::Storage
::parse_volume_id
($volid);
7022 my $readlimit = PVE
::Storage
::get_bandwidth_limit
('restore', [$sid], $bwlimit);
7024 print STDERR
"applying read rate limit: $readlimit\n";
7025 my $cstream = ['cstream', '-t', $readlimit*1024, '--', $readfrom];
7026 $add_pipe->($cstream);
7032 my $info = PVE
::Storage
::decompressor_info
('vma', $comp);
7033 my $cmd = $info->{decompressor
};
7034 push @$cmd, $readfrom;
7038 my $tmpdir = "/var/tmp/vzdumptmp$$";
7041 # disable interrupts (always do cleanups)
7045 local $SIG{HUP
} = sub { warn "got interrupt - ignored\n"; };
7047 my $mapfifo = "/var/tmp/vzdumptmp$$.fifo";
7048 POSIX
::mkfifo
($mapfifo, 0600);
7050 my $openfifo = sub { open($fifofh, '>', $mapfifo) or die $! };
7052 $add_pipe->(['vma', 'extract', '-v', '-r', $mapfifo, $readfrom, $tmpdir]);
7057 my $devinfo = {}; # info about drives included in backup
7058 my $virtdev_hash = {}; # info about allocated drives
7060 my $rpcenv = PVE
::RPCEnvironment
::get
();
7062 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
7064 # Note: $oldconf is undef if VM does not exist
7065 my $cfs_path = PVE
::QemuConfig-
>cfs_config_path($vmid);
7066 my $oldconf = PVE
::Cluster
::cfs_read_file
($cfs_path);
7067 my $new_conf_raw = '';
7071 my $print_devmap = sub {
7072 my $cfgfn = "$tmpdir/qemu-server.conf";
7074 # we can read the config - that is already extracted
7075 my $fh = IO
::File-
>new($cfgfn, "r") ||
7076 die "unable to read qemu-server.conf - $!\n";
7078 my $fwcfgfn = "$tmpdir/qemu-server.fw";
7080 my $pve_firewall_dir = '/etc/pve/firewall';
7081 mkdir $pve_firewall_dir; # make sure the dir exists
7082 PVE
::Tools
::file_copy
($fwcfgfn, "${pve_firewall_dir}/$vmid.fw");
7085 $virtdev_hash = $parse_backup_hints->($rpcenv, $user, $cfg, $fh, $devinfo, $opts);
7087 foreach my $info (values %{$virtdev_hash}) {
7088 my $storeid = $info->{storeid
};
7089 next if defined($storage_limits{$storeid});
7091 my $limit = PVE
::Storage
::get_bandwidth_limit
('restore', [$storeid], $bwlimit) // 0;
7092 print STDERR
"rate limit for storage $storeid: $limit KiB/s\n" if $limit;
7093 $storage_limits{$storeid} = $limit * 1024;
7096 foreach my $devname (keys %$devinfo) {
7097 die "found no device mapping information for device '$devname'\n"
7098 if !$devinfo->{$devname}->{virtdev
};
7101 # create empty/temp config
7103 PVE
::Tools
::file_set_contents
($conffile, "memory: 128\n");
7104 $restore_cleanup_oldconf->($cfg, $vmid, $oldconf, $virtdev_hash);
7108 my $map = $restore_allocate_devices->($cfg, $virtdev_hash, $vmid);
7110 # print restore information to $fifofh
7111 foreach my $virtdev (sort keys %$virtdev_hash) {
7112 my $d = $virtdev_hash->{$virtdev};
7113 next if $d->{is_cloudinit
}; # no need to restore cloudinit
7115 my $storeid = $d->{storeid
};
7116 my $volid = $d->{volid
};
7119 if (my $limit = $storage_limits{$storeid}) {
7120 $map_opts .= "throttling.bps=$limit:throttling.group=$storeid:";
7123 my $write_zeros = 1;
7124 if (PVE
::Storage
::volume_has_feature
($cfg, 'sparseinit', $volid)) {
7128 my $path = PVE
::Storage
::path
($cfg, $volid);
7130 print $fifofh "${map_opts}format=$d->{format}:${write_zeros}:$d->{devname}=$path\n";
7132 print "map '$d->{devname}' to '$path' (write zeros = ${write_zeros})\n";
7135 $fh->seek(0, 0) || die "seek failed - $!\n";
7137 my $cookie = { netcount
=> 0 };
7138 while (defined(my $line = <$fh>)) {
7139 $new_conf_raw .= restore_update_config_line
(
7156 local $SIG{PIPE
} = sub { die "interrupted by signal\n"; };
7157 local $SIG{ALRM
} = sub { die "got timeout\n"; };
7159 $oldtimeout = alarm($timeout);
7166 if ($line =~ m/^DEV:\sdev_id=(\d+)\ssize:\s(\d+)\sdevname:\s(\S+)$/) {
7167 my ($dev_id, $size, $devname) = ($1, $2, $3);
7168 $devinfo->{$devname} = { size
=> $size, dev_id
=> $dev_id };
7169 } elsif ($line =~ m/^CTIME: /) {
7170 # we correctly received the vma config, so we can disable
7171 # the timeout now for disk allocation (set to 10 minutes, so
7172 # that we always timeout if something goes wrong)
7175 print $fifofh "done\n";
7176 my $tmp = $oldtimeout || 0;
7177 $oldtimeout = undef;
7184 print "restore vma archive: $dbg_cmdstring\n";
7185 run_command
($commands, input
=> $input, outfunc
=> $parser, afterfork
=> $openfifo);
7189 alarm($oldtimeout) if $oldtimeout;
7191 $restore_deactivate_volumes->($cfg, $virtdev_hash);
7193 close($fifofh) if $fifofh;
7198 $restore_destroy_volumes->($cfg, $virtdev_hash);
7202 my $new_conf = $restore_merge_config->($conffile, $new_conf_raw, $opts->{override_conf
});
7203 PVE
::QemuConfig-
>write_config($vmid, $new_conf);
7205 eval { rescan
($vmid, 1); };
7208 PVE
::AccessControl
::add_vm_to_pool
($vmid, $opts->{pool
}) if $opts->{pool
};
7211 sub restore_tar_archive
{
7212 my ($archive, $vmid, $user, $opts) = @_;
7214 if (scalar(keys $opts->{override_conf
}->%*) > 0) {
7215 my $keystring = join(' ', keys $opts->{override_conf
}->%*);
7216 die "cannot pass along options ($keystring) when restoring from tar archive\n";
7219 if ($archive ne '-') {
7220 my $firstfile = tar_archive_read_firstfile
($archive);
7221 die "ERROR: file '$archive' does not look like a QemuServer vzdump backup\n"
7222 if $firstfile ne 'qemu-server.conf';
7225 my $storecfg = PVE
::Storage
::config
();
7227 # avoid zombie disks when restoring over an existing VM -> cleanup first
7228 # pass keep_empty_config=1 to keep the config (thus VMID) reserved for us
7229 # skiplock=1 because qmrestore has set the 'create' lock itself already
7230 my $vmcfgfn = PVE
::QemuConfig-
>config_file($vmid);
7231 destroy_vm
($storecfg, $vmid, 1, { lock => 'restore' }) if -f
$vmcfgfn;
7233 my $tocmd = "/usr/lib/qemu-server/qmextract";
7235 $tocmd .= " --storage " . PVE
::Tools
::shellquote
($opts->{storage
}) if $opts->{storage
};
7236 $tocmd .= " --pool " . PVE
::Tools
::shellquote
($opts->{pool
}) if $opts->{pool
};
7237 $tocmd .= ' --prealloc' if $opts->{prealloc
};
7238 $tocmd .= ' --info' if $opts->{info
};
7240 # tar option "xf" does not autodetect compression when read from STDIN,
7241 # so we pipe to zcat
7242 my $cmd = "zcat -f|tar xf " . PVE
::Tools
::shellquote
($archive) . " " .
7243 PVE
::Tools
::shellquote
("--to-command=$tocmd");
7245 my $tmpdir = "/var/tmp/vzdumptmp$$";
7248 local $ENV{VZDUMP_TMPDIR
} = $tmpdir;
7249 local $ENV{VZDUMP_VMID
} = $vmid;
7250 local $ENV{VZDUMP_USER
} = $user;
7252 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
7253 my $new_conf_raw = '';
7255 # disable interrupts (always do cleanups)
7259 local $SIG{HUP
} = sub { print STDERR
"got interrupt - ignored\n"; };
7267 local $SIG{PIPE
} = sub { die "interrupted by signal\n"; };
7269 if ($archive eq '-') {
7270 print "extracting archive from STDIN\n";
7271 run_command
($cmd, input
=> "<&STDIN");
7273 print "extracting archive '$archive'\n";
7277 return if $opts->{info
};
7281 my $statfile = "$tmpdir/qmrestore.stat";
7282 if (my $fd = IO
::File-
>new($statfile, "r")) {
7283 while (defined (my $line = <$fd>)) {
7284 if ($line =~ m/vzdump:([^\s:]*):(\S+)$/) {
7285 $map->{$1} = $2 if $1;
7287 print STDERR
"unable to parse line in statfile - $line\n";
7293 my $confsrc = "$tmpdir/qemu-server.conf";
7295 my $srcfd = IO
::File-
>new($confsrc, "r") || die "unable to open file '$confsrc'\n";
7297 my $cookie = { netcount
=> 0 };
7298 while (defined (my $line = <$srcfd>)) {
7299 $new_conf_raw .= restore_update_config_line
(
7310 tar_restore_cleanup
($storecfg, "$tmpdir/qmrestore.stat") if !$opts->{info
};
7316 PVE
::Tools
::file_set_contents
($conffile, $new_conf_raw);
7318 PVE
::Cluster
::cfs_update
(); # make sure we read new file
7320 eval { rescan
($vmid, 1); };
7324 sub foreach_storage_used_by_vm
{
7325 my ($conf, $func) = @_;
7329 PVE
::QemuConfig-
>foreach_volume($conf, sub {
7330 my ($ds, $drive) = @_;
7331 return if drive_is_cdrom
($drive);
7333 my $volid = $drive->{file
};
7335 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
7336 $sidhash->{$sid} = $sid if $sid;
7339 foreach my $sid (sort keys %$sidhash) {
7344 my $qemu_snap_storage = {
7347 sub do_snapshots_with_qemu
{
7348 my ($storecfg, $volid, $deviceid) = @_;
7350 return if $deviceid =~ m/tpmstate0/;
7352 my $storage_name = PVE
::Storage
::parse_volume_id
($volid);
7353 my $scfg = $storecfg->{ids
}->{$storage_name};
7354 die "could not find storage '$storage_name'\n" if !defined($scfg);
7356 if ($qemu_snap_storage->{$scfg->{type
}} && !$scfg->{krbd
}){
7360 if ($volid =~ m/\.(qcow2|qed)$/){
7367 sub qga_check_running
{
7368 my ($vmid, $nowarn) = @_;
7370 eval { mon_cmd
($vmid, "guest-ping", timeout
=> 3); };
7372 warn "Qemu Guest Agent is not running - $@" if !$nowarn;
7378 sub template_create
{
7379 my ($vmid, $conf, $disk) = @_;
7381 my $storecfg = PVE
::Storage
::config
();
7383 PVE
::QemuConfig-
>foreach_volume($conf, sub {
7384 my ($ds, $drive) = @_;
7386 return if drive_is_cdrom
($drive);
7387 return if $disk && $ds ne $disk;
7389 my $volid = $drive->{file
};
7390 return if !PVE
::Storage
::volume_has_feature
($storecfg, 'template', $volid);
7392 my $voliddst = PVE
::Storage
::vdisk_create_base
($storecfg, $volid);
7393 $drive->{file
} = $voliddst;
7394 $conf->{$ds} = print_drive
($drive);
7395 PVE
::QemuConfig-
>write_config($vmid, $conf);
7399 sub convert_iscsi_path
{
7402 if ($path =~ m
|^iscsi
://([^/]+)/([^/]+)/(.+)$|) {
7407 my $initiator_name = get_initiator_name
();
7409 return "file.driver=iscsi,file.transport=tcp,file.initiator-name=$initiator_name,".
7410 "file.portal=$portal,file.target=$target,file.lun=$lun,driver=raw";
7413 die "cannot convert iscsi path '$path', unkown format\n";
7416 sub qemu_img_convert
{
7417 my ($src_volid, $dst_volid, $size, $snapname, $is_zero_initialized) = @_;
7419 my $storecfg = PVE
::Storage
::config
();
7420 my ($src_storeid, $src_volname) = PVE
::Storage
::parse_volume_id
($src_volid, 1);
7421 my ($dst_storeid, $dst_volname) = PVE
::Storage
::parse_volume_id
($dst_volid, 1);
7423 die "destination '$dst_volid' is not a valid volid form qemu-img convert\n" if !$dst_storeid;
7427 my $src_is_iscsi = 0;
7431 PVE
::Storage
::activate_volumes
($storecfg, [$src_volid], $snapname);
7432 my $src_scfg = PVE
::Storage
::storage_config
($storecfg, $src_storeid);
7433 $src_format = qemu_img_format
($src_scfg, $src_volname);
7434 $src_path = PVE
::Storage
::path
($storecfg, $src_volid, $snapname);
7435 $src_is_iscsi = ($src_path =~ m
|^iscsi
://|);
7436 $cachemode = 'none' if $src_scfg->{type
} eq 'zfspool';
7437 } elsif (-f
$src_volid || -b
$src_volid) {
7438 $src_path = $src_volid;
7439 if ($src_path =~ m/\.($PVE::QemuServer::Drive::QEMU_FORMAT_RE)$/) {
7444 die "source '$src_volid' is not a valid volid nor path for qemu-img convert\n" if !$src_path;
7446 my $dst_scfg = PVE
::Storage
::storage_config
($storecfg, $dst_storeid);
7447 my $dst_format = qemu_img_format
($dst_scfg, $dst_volname);
7448 my $dst_path = PVE
::Storage
::path
($storecfg, $dst_volid);
7449 my $dst_is_iscsi = ($dst_path =~ m
|^iscsi
://|);
7452 push @$cmd, '/usr/bin/qemu-img', 'convert', '-p', '-n';
7453 push @$cmd, '-l', "snapshot.name=$snapname"
7454 if $snapname && $src_format && $src_format eq "qcow2";
7455 push @$cmd, '-t', 'none' if $dst_scfg->{type
} eq 'zfspool';
7456 push @$cmd, '-T', $cachemode if defined($cachemode);
7458 if ($src_is_iscsi) {
7459 push @$cmd, '--image-opts';
7460 $src_path = convert_iscsi_path
($src_path);
7461 } elsif ($src_format) {
7462 push @$cmd, '-f', $src_format;
7465 if ($dst_is_iscsi) {
7466 push @$cmd, '--target-image-opts';
7467 $dst_path = convert_iscsi_path
($dst_path);
7469 push @$cmd, '-O', $dst_format;
7472 push @$cmd, $src_path;
7474 if (!$dst_is_iscsi && $is_zero_initialized) {
7475 push @$cmd, "zeroinit:$dst_path";
7477 push @$cmd, $dst_path;
7482 if($line =~ m/\((\S+)\/100\
%\)/){
7484 my $transferred = int($size * $percent / 100);
7485 my $total_h = render_bytes
($size, 1);
7486 my $transferred_h = render_bytes
($transferred, 1);
7488 print "transferred $transferred_h of $total_h ($percent%)\n";
7493 eval { run_command
($cmd, timeout
=> undef, outfunc
=> $parser); };
7495 die "copy failed: $err" if $err;
7498 sub qemu_img_format
{
7499 my ($scfg, $volname) = @_;
7501 if ($scfg->{path
} && $volname =~ m/\.($PVE::QemuServer::Drive::QEMU_FORMAT_RE)$/) {
7508 sub qemu_drive_mirror
{
7509 my ($vmid, $drive, $dst_volid, $vmiddst, $is_zero_initialized, $jobs, $completion, $qga, $bwlimit, $src_bitmap) = @_;
7511 $jobs = {} if !$jobs;
7515 $jobs->{"drive-$drive"} = {};
7517 if ($dst_volid =~ /^nbd:/) {
7518 $qemu_target = $dst_volid;
7521 my $storecfg = PVE
::Storage
::config
();
7522 my ($dst_storeid, $dst_volname) = PVE
::Storage
::parse_volume_id
($dst_volid);
7524 my $dst_scfg = PVE
::Storage
::storage_config
($storecfg, $dst_storeid);
7526 $format = qemu_img_format
($dst_scfg, $dst_volname);
7528 my $dst_path = PVE
::Storage
::path
($storecfg, $dst_volid);
7530 $qemu_target = $is_zero_initialized ?
"zeroinit:$dst_path" : $dst_path;
7533 my $opts = { timeout
=> 10, device
=> "drive-$drive", mode
=> "existing", sync
=> "full", target
=> $qemu_target };
7534 $opts->{format
} = $format if $format;
7536 if (defined($src_bitmap)) {
7537 $opts->{sync
} = 'incremental';
7538 $opts->{bitmap
} = $src_bitmap;
7539 print "drive mirror re-using dirty bitmap '$src_bitmap'\n";
7542 if (defined($bwlimit)) {
7543 $opts->{speed
} = $bwlimit * 1024;
7544 print "drive mirror is starting for drive-$drive with bandwidth limit: ${bwlimit} KB/s\n";
7546 print "drive mirror is starting for drive-$drive\n";
7549 # if a job already runs for this device we get an error, catch it for cleanup
7550 eval { mon_cmd
($vmid, "drive-mirror", %$opts); };
7552 eval { PVE
::QemuServer
::qemu_blockjobs_cancel
($vmid, $jobs) };
7554 die "mirroring error: $err\n";
7557 qemu_drive_mirror_monitor
($vmid, $vmiddst, $jobs, $completion, $qga);
7560 # $completion can be either
7561 # 'complete': wait until all jobs are ready, block-job-complete them (default)
7562 # 'cancel': wait until all jobs are ready, block-job-cancel them
7563 # 'skip': wait until all jobs are ready, return with block jobs in ready state
7564 # 'auto': wait until all jobs disappear, only use for jobs which complete automatically
7565 sub qemu_drive_mirror_monitor
{
7566 my ($vmid, $vmiddst, $jobs, $completion, $qga, $op) = @_;
7568 $completion //= 'complete';
7572 my $err_complete = 0;
7574 my $starttime = time ();
7576 die "block job ('$op') timed out\n" if $err_complete > 300;
7578 my $stats = mon_cmd
($vmid, "query-block-jobs");
7581 my $running_jobs = {};
7582 for my $stat (@$stats) {
7583 next if $stat->{type
} ne $op;
7584 $running_jobs->{$stat->{device
}} = $stat;
7587 my $readycounter = 0;
7589 for my $job_id (sort keys %$jobs) {
7590 my $job = $running_jobs->{$job_id};
7592 my $vanished = !defined($job);
7593 my $complete = defined($jobs->{$job_id}->{complete
}) && $vanished;
7594 if($complete || ($vanished && $completion eq 'auto')) {
7595 print "$job_id: $op-job finished\n";
7596 delete $jobs->{$job_id};
7600 die "$job_id: '$op' has been cancelled\n" if !defined($job);
7602 my $busy = $job->{busy
};
7603 my $ready = $job->{ready
};
7604 if (my $total = $job->{len
}) {
7605 my $transferred = $job->{offset
} || 0;
7606 my $remaining = $total - $transferred;
7607 my $percent = sprintf "%.2f", ($transferred * 100 / $total);
7609 my $duration = $ctime - $starttime;
7610 my $total_h = render_bytes
($total, 1);
7611 my $transferred_h = render_bytes
($transferred, 1);
7613 my $status = sprintf(
7614 "transferred $transferred_h of $total_h ($percent%%) in %s",
7615 render_duration
($duration),
7620 $status .= ", still busy"; # shouldn't even happen? but mirror is weird
7622 $status .= ", ready";
7625 print "$job_id: $status\n" if !$jobs->{$job_id}->{ready
};
7626 $jobs->{$job_id}->{ready
} = $ready;
7629 $readycounter++ if $job->{ready
};
7632 last if scalar(keys %$jobs) == 0;
7634 if ($readycounter == scalar(keys %$jobs)) {
7635 print "all '$op' jobs are ready\n";
7637 # do the complete later (or has already been done)
7638 last if $completion eq 'skip' || $completion eq 'auto';
7640 if ($vmiddst && $vmiddst != $vmid) {
7641 my $agent_running = $qga && qga_check_running
($vmid);
7642 if ($agent_running) {
7643 print "freeze filesystem\n";
7644 eval { mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
7647 print "suspend vm\n";
7648 eval { PVE
::QemuServer
::vm_suspend
($vmid, 1); };
7652 # if we clone a disk for a new target vm, we don't switch the disk
7653 PVE
::QemuServer
::qemu_blockjobs_cancel
($vmid, $jobs);
7655 if ($agent_running) {
7656 print "unfreeze filesystem\n";
7657 eval { mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
7660 print "resume vm\n";
7661 eval { PVE
::QemuServer
::vm_resume
($vmid, 1, 1); };
7668 for my $job_id (sort keys %$jobs) {
7669 # try to switch the disk if source and destination are on the same guest
7670 print "$job_id: Completing block job_id...\n";
7673 if ($completion eq 'complete') {
7674 $op = 'block-job-complete';
7675 } elsif ($completion eq 'cancel') {
7676 $op = 'block-job-cancel';
7678 die "invalid completion value: $completion\n";
7680 eval { mon_cmd
($vmid, $op, device
=> $job_id) };
7681 if ($@ =~ m/cannot be completed/) {
7682 print "$job_id: block job cannot be completed, trying again.\n";
7685 print "$job_id: Completed successfully.\n";
7686 $jobs->{$job_id}->{complete
} = 1;
7697 eval { PVE
::QemuServer
::qemu_blockjobs_cancel
($vmid, $jobs) };
7698 die "block job ($op) error: $err";
7702 sub qemu_blockjobs_cancel
{
7703 my ($vmid, $jobs) = @_;
7705 foreach my $job (keys %$jobs) {
7706 print "$job: Cancelling block job\n";
7707 eval { mon_cmd
($vmid, "block-job-cancel", device
=> $job); };
7708 $jobs->{$job}->{cancel
} = 1;
7712 my $stats = mon_cmd
($vmid, "query-block-jobs");
7714 my $running_jobs = {};
7715 foreach my $stat (@$stats) {
7716 $running_jobs->{$stat->{device
}} = $stat;
7719 foreach my $job (keys %$jobs) {
7721 if (defined($jobs->{$job}->{cancel
}) && !defined($running_jobs->{$job})) {
7722 print "$job: Done.\n";
7723 delete $jobs->{$job};
7727 last if scalar(keys %$jobs) == 0;
7734 my ($storecfg, $source, $dest, $full, $newvollist, $jobs, $completion, $qga, $bwlimit) = @_;
7736 my ($vmid, $running) = $source->@{qw(vmid running)};
7737 my ($src_drivename, $drive, $snapname) = $source->@{qw(drivename drive snapname)};
7739 my ($newvmid, $dst_drivename, $efisize) = $dest->@{qw(vmid drivename efisize)};
7740 my ($storage, $format) = $dest->@{qw(storage format)};
7742 my $use_drive_mirror = $full && $running && $src_drivename && !$snapname;
7744 if ($src_drivename && $dst_drivename && $src_drivename ne $dst_drivename) {
7745 die "cloning from/to EFI disk requires EFI disk\n"
7746 if $src_drivename eq 'efidisk0' || $dst_drivename eq 'efidisk0';
7747 die "cloning from/to TPM state requires TPM state\n"
7748 if $src_drivename eq 'tpmstate0' || $dst_drivename eq 'tpmstate0';
7750 # This would lead to two device nodes in QEMU pointing to the same backing image!
7751 die "cannot change drive name when cloning disk from/to the same VM\n"
7752 if $use_drive_mirror && $vmid == $newvmid;
7755 die "cannot move TPM state while VM is running\n"
7756 if $use_drive_mirror && $src_drivename eq 'tpmstate0';
7760 print "create " . ($full ?
'full' : 'linked') . " clone of drive ";
7761 print "$src_drivename " if $src_drivename;
7762 print "($drive->{file})\n";
7765 $newvolid = PVE
::Storage
::vdisk_clone
($storecfg, $drive->{file
}, $newvmid, $snapname);
7766 push @$newvollist, $newvolid;
7769 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($drive->{file
});
7770 $storeid = $storage if $storage;
7772 my $dst_format = resolve_dst_disk_format
($storecfg, $storeid, $volname, $format);
7776 if (drive_is_cloudinit
($drive)) {
7777 $name = "vm-$newvmid-cloudinit";
7778 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
7779 if ($scfg->{path
}) {
7780 $name .= ".$dst_format";
7783 $size = PVE
::QemuServer
::Cloudinit
::CLOUDINIT_DISK_SIZE
;
7784 } elsif ($dst_drivename eq 'efidisk0') {
7785 $size = $efisize or die "internal error - need to specify EFI disk size\n";
7786 } elsif ($dst_drivename eq 'tpmstate0') {
7787 $dst_format = 'raw';
7788 $size = PVE
::QemuServer
::Drive
::TPMSTATE_DISK_SIZE
;
7790 ($size) = PVE
::Storage
::volume_size_info
($storecfg, $drive->{file
}, 10);
7792 $newvolid = PVE
::Storage
::vdisk_alloc
(
7793 $storecfg, $storeid, $newvmid, $dst_format, $name, ($size/1024)
7795 push @$newvollist, $newvolid;
7797 PVE
::Storage
::activate_volumes
($storecfg, [$newvolid]);
7799 if (drive_is_cloudinit
($drive)) {
7800 # when cloning multiple disks (e.g. during clone_vm) it might be the last disk
7801 # if this is the case, we have to complete any block-jobs still there from
7802 # previous drive-mirrors
7803 if (($completion eq 'complete') && (scalar(keys %$jobs) > 0)) {
7804 qemu_drive_mirror_monitor
($vmid, $newvmid, $jobs, $completion, $qga);
7809 my $sparseinit = PVE
::Storage
::volume_has_feature
($storecfg, 'sparseinit', $newvolid);
7810 if ($use_drive_mirror) {
7811 qemu_drive_mirror
($vmid, $src_drivename, $newvolid, $newvmid, $sparseinit, $jobs,
7812 $completion, $qga, $bwlimit);
7814 # TODO: handle bwlimits
7815 if ($dst_drivename eq 'efidisk0') {
7816 # the relevant data on the efidisk may be smaller than the source
7817 # e.g. on RBD/ZFS, so we use dd to copy only the amount
7818 # that is given by the OVMF_VARS.fd
7819 my $src_path = PVE
::Storage
::path
($storecfg, $drive->{file
}, $snapname);
7820 my $dst_path = PVE
::Storage
::path
($storecfg, $newvolid);
7822 my $src_format = (PVE
::Storage
::parse_volname
($storecfg, $drive->{file
}))[6];
7824 # better for Ceph if block size is not too small, see bug #3324
7827 my $cmd = ['qemu-img', 'dd', '-n', '-O', $dst_format];
7829 if ($src_format eq 'qcow2' && $snapname) {
7830 die "cannot clone qcow2 EFI disk snapshot - requires QEMU >= 6.2\n"
7831 if !min_version
(kvm_user_version
(), 6, 2);
7832 push $cmd->@*, '-l', $snapname;
7834 push $cmd->@*, "bs=$bs", "osize=$size", "if=$src_path", "of=$dst_path";
7837 qemu_img_convert
($drive->{file
}, $newvolid, $size, $snapname, $sparseinit);
7843 my ($size) = eval { PVE
::Storage
::volume_size_info
($storecfg, $newvolid, 10) };
7845 my $disk = dclone
($drive);
7846 delete $disk->{format
};
7847 $disk->{file
} = $newvolid;
7848 $disk->{size
} = $size if defined($size);
7853 sub get_running_qemu_version
{
7855 my $res = mon_cmd
($vmid, "query-version");
7856 return "$res->{qemu}->{major}.$res->{qemu}->{minor}";
7859 sub qemu_use_old_bios_files
{
7860 my ($machine_type) = @_;
7862 return if !$machine_type;
7864 my $use_old_bios_files = undef;
7866 if ($machine_type =~ m/^(\S+)\.pxe$/) {
7868 $use_old_bios_files = 1;
7870 my $version = extract_version
($machine_type, kvm_user_version
());
7871 # Note: kvm version < 2.4 use non-efi pxe files, and have problems when we
7872 # load new efi bios files on migration. So this hack is required to allow
7873 # live migration from qemu-2.2 to qemu-2.4, which is sometimes used when
7874 # updrading from proxmox-ve-3.X to proxmox-ve 4.0
7875 $use_old_bios_files = !min_version
($version, 2, 4);
7878 return ($use_old_bios_files, $machine_type);
7881 sub get_efivars_size
{
7882 my ($conf, $efidisk) = @_;
7884 my $arch = get_vm_arch
($conf);
7885 $efidisk //= $conf->{efidisk0
} ? parse_drive
('efidisk0', $conf->{efidisk0
}) : undef;
7886 my $smm = PVE
::QemuServer
::Machine
::machine_type_is_q35
($conf);
7887 my (undef, $ovmf_vars) = get_ovmf_files
($arch, $efidisk, $smm);
7888 die "uefi vars image '$ovmf_vars' not found\n" if ! -f
$ovmf_vars;
7889 return -s
$ovmf_vars;
7892 sub update_efidisk_size
{
7895 return if !defined($conf->{efidisk0
});
7897 my $disk = PVE
::QemuServer
::parse_drive
('efidisk0', $conf->{efidisk0
});
7898 $disk->{size
} = get_efivars_size
($conf);
7899 $conf->{efidisk0
} = print_drive
($disk);
7904 sub update_tpmstate_size
{
7907 my $disk = PVE
::QemuServer
::parse_drive
('tpmstate0', $conf->{tpmstate0
});
7908 $disk->{size
} = PVE
::QemuServer
::Drive
::TPMSTATE_DISK_SIZE
;
7909 $conf->{tpmstate0
} = print_drive
($disk);
7912 sub create_efidisk
($$$$$$$) {
7913 my ($storecfg, $storeid, $vmid, $fmt, $arch, $efidisk, $smm) = @_;
7915 my (undef, $ovmf_vars) = get_ovmf_files
($arch, $efidisk, $smm);
7916 die "EFI vars default image not found\n" if ! -f
$ovmf_vars;
7918 my $vars_size_b = -s
$ovmf_vars;
7919 my $vars_size = PVE
::Tools
::convert_size
($vars_size_b, 'b' => 'kb');
7920 my $volid = PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid, $fmt, undef, $vars_size);
7921 PVE
::Storage
::activate_volumes
($storecfg, [$volid]);
7923 qemu_img_convert
($ovmf_vars, $volid, $vars_size_b, undef, 0);
7924 my ($size) = PVE
::Storage
::volume_size_info
($storecfg, $volid, 3);
7926 return ($volid, $size/1024);
7929 sub vm_iothreads_list
{
7932 my $res = mon_cmd
($vmid, 'query-iothreads');
7935 foreach my $iothread (@$res) {
7936 $iothreads->{ $iothread->{id
} } = $iothread->{"thread-id"};
7943 my ($conf, $drive) = @_;
7947 if (!$conf->{scsihw
} || ($conf->{scsihw
} =~ m/^lsi/)) {
7949 } elsif ($conf->{scsihw
} && ($conf->{scsihw
} eq 'virtio-scsi-single')) {
7955 my $controller = int($drive->{index} / $maxdev);
7956 my $controller_prefix = ($conf->{scsihw
} && $conf->{scsihw
} eq 'virtio-scsi-single')
7960 return ($maxdev, $controller, $controller_prefix);
7963 sub resolve_dst_disk_format
{
7964 my ($storecfg, $storeid, $src_volname, $format) = @_;
7965 my ($defFormat, $validFormats) = PVE
::Storage
::storage_default_format
($storecfg, $storeid);
7968 # if no target format is specified, use the source disk format as hint
7970 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
7971 $format = qemu_img_format
($scfg, $src_volname);
7977 # test if requested format is supported - else use default
7978 my $supported = grep { $_ eq $format } @$validFormats;
7979 $format = $defFormat if !$supported;
7983 # NOTE: if this logic changes, please update docs & possibly gui logic
7984 sub find_vmstate_storage
{
7985 my ($conf, $storecfg) = @_;
7987 # first, return storage from conf if set
7988 return $conf->{vmstatestorage
} if $conf->{vmstatestorage
};
7990 my ($target, $shared, $local);
7992 foreach_storage_used_by_vm
($conf, sub {
7994 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
7995 my $dst = $scfg->{shared
} ? \
$shared : \
$local;
7996 $$dst = $sid if !$$dst || $scfg->{path
}; # prefer file based storage
7999 # second, use shared storage where VM has at least one disk
8000 # third, use local storage where VM has at least one disk
8001 # fall back to local storage
8002 $target = $shared // $local // 'local';
8008 my ($uuid, $uuid_str);
8009 UUID
::generate
($uuid);
8010 UUID
::unparse
($uuid, $uuid_str);
8014 sub generate_smbios1_uuid
{
8015 return "uuid=".generate_uuid
();
8021 mon_cmd
($vmid, 'nbd-server-stop');
8024 sub create_reboot_request
{
8026 open(my $fh, '>', "/run/qemu-server/$vmid.reboot")
8027 or die "failed to create reboot trigger file: $!\n";
8031 sub clear_reboot_request
{
8033 my $path = "/run/qemu-server/$vmid.reboot";
8036 $res = unlink($path);
8037 die "could not remove reboot request for $vmid: $!"
8038 if !$res && $! != POSIX
::ENOENT
;
8043 sub bootorder_from_legacy
{
8044 my ($conf, $bootcfg) = @_;
8046 my $boot = $bootcfg->{legacy
} || $boot_fmt->{legacy
}->{default};
8047 my $bootindex_hash = {};
8049 foreach my $o (split(//, $boot)) {
8050 $bootindex_hash->{$o} = $i*100;
8056 PVE
::QemuConfig-
>foreach_volume($conf, sub {
8057 my ($ds, $drive) = @_;
8059 if (drive_is_cdrom
($drive, 1)) {
8060 if ($bootindex_hash->{d
}) {
8061 $bootorder->{$ds} = $bootindex_hash->{d
};
8062 $bootindex_hash->{d
} += 1;
8064 } elsif ($bootindex_hash->{c
}) {
8065 $bootorder->{$ds} = $bootindex_hash->{c
}
8066 if $conf->{bootdisk
} && $conf->{bootdisk
} eq $ds;
8067 $bootindex_hash->{c
} += 1;
8071 if ($bootindex_hash->{n
}) {
8072 for (my $i = 0; $i < $MAX_NETS; $i++) {
8073 my $netname = "net$i";
8074 next if !$conf->{$netname};
8075 $bootorder->{$netname} = $bootindex_hash->{n
};
8076 $bootindex_hash->{n
} += 1;
8083 # Generate default device list for 'boot: order=' property. Matches legacy
8084 # default boot order, but with explicit device names. This is important, since
8085 # the fallback for when neither 'order' nor the old format is specified relies
8086 # on 'bootorder_from_legacy' above, and it would be confusing if this diverges.
8087 sub get_default_bootdevices
{
8093 my $first = PVE
::QemuServer
::Drive
::resolve_first_disk
($conf, 0);
8094 push @ret, $first if $first;
8097 $first = PVE
::QemuServer
::Drive
::resolve_first_disk
($conf, 1);
8098 push @ret, $first if $first;
8101 for (my $i = 0; $i < $MAX_NETS; $i++) {
8102 my $netname = "net$i";
8103 next if !$conf->{$netname};
8104 push @ret, $netname;
8111 sub device_bootorder
{
8114 return bootorder_from_legacy
($conf) if !defined($conf->{boot
});
8116 my $boot = parse_property_string
($boot_fmt, $conf->{boot
});
8119 if (!defined($boot) || $boot->{legacy
}) {
8120 $bootorder = bootorder_from_legacy
($conf, $boot);
8121 } elsif ($boot->{order
}) {
8122 my $i = 100; # start at 100 to allow user to insert devices before us with -args
8123 for my $dev (PVE
::Tools
::split_list
($boot->{order
})) {
8124 $bootorder->{$dev} = $i++;
8131 sub register_qmeventd_handle
{
8135 my $peer = "/var/run/qmeventd.sock";
8140 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $peer, Blocking
=> 0, Timeout
=> 1);
8142 if ($! != EINTR
&& $! != EAGAIN
) {
8143 die "unable to connect to qmeventd socket (vmid: $vmid) - $!\n";
8146 die "unable to connect to qmeventd socket (vmid: $vmid) - timeout "
8147 . "after $count retries\n";
8152 # send handshake to mark VM as backing up
8153 print $fh to_json
({vzdump
=> {vmid
=> "$vmid"}});
8155 # return handle to be closed later when inhibit is no longer required
8159 # bash completion helper
8161 sub complete_backup_archives
{
8162 my ($cmdname, $pname, $cvalue) = @_;
8164 my $cfg = PVE
::Storage
::config
();
8168 if ($cvalue =~ m/^([^:]+):/) {
8172 my $data = PVE
::Storage
::template_list
($cfg, $storeid, 'backup');
8175 foreach my $id (keys %$data) {
8176 foreach my $item (@{$data->{$id}}) {
8177 next if $item->{format
} !~ m/^vma\.(${\PVE::Storage::Plugin::COMPRESSOR_RE})$/;
8178 push @$res, $item->{volid
} if defined($item->{volid
});
8185 my $complete_vmid_full = sub {
8188 my $idlist = vmstatus
();
8192 foreach my $id (keys %$idlist) {
8193 my $d = $idlist->{$id};
8194 if (defined($running)) {
8195 next if $d->{template
};
8196 next if $running && $d->{status
} ne 'running';
8197 next if !$running && $d->{status
} eq 'running';
8206 return &$complete_vmid_full();
8209 sub complete_vmid_stopped
{
8210 return &$complete_vmid_full(0);
8213 sub complete_vmid_running
{
8214 return &$complete_vmid_full(1);
8217 sub complete_storage
{
8219 my $cfg = PVE
::Storage
::config
();
8220 my $ids = $cfg->{ids
};
8223 foreach my $sid (keys %$ids) {
8224 next if !PVE
::Storage
::storage_check_enabled
($cfg, $sid, undef, 1);
8225 next if !$ids->{$sid}->{content
}->{images
};
8232 sub complete_migration_storage
{
8233 my ($cmd, $param, $current_value, $all_args) = @_;
8235 my $targetnode = @$all_args[1];
8237 my $cfg = PVE
::Storage
::config
();
8238 my $ids = $cfg->{ids
};
8241 foreach my $sid (keys %$ids) {
8242 next if !PVE
::Storage
::storage_check_enabled
($cfg, $sid, $targetnode, 1);
8243 next if !$ids->{$sid}->{content
}->{images
};
8252 my $qmpstatus = eval {
8253 PVE
::QemuConfig
::assert_config_exists_on_node
($vmid);
8254 mon_cmd
($vmid, "query-status");
8257 return $qmpstatus && $qmpstatus->{status
} eq "paused";
8260 sub check_volume_storage_type
{
8261 my ($storecfg, $vol) = @_;
8263 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($vol);
8264 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
8265 my ($vtype) = PVE
::Storage
::parse_volname
($storecfg, $vol);
8267 die "storage '$storeid' does not support content-type '$vtype'\n"
8268 if !$scfg->{content
}->{$vtype};