1 package PVE
::QemuServer
;
23 use Storable
qw(dclone);
25 use PVE
::Exception
qw(raise raise_param_exc);
27 use PVE
::Tools
qw(run_command lock_file lock_file_full file_read_firstline dir_glob_foreach $IPV6RE);
28 use PVE
::JSONSchema
qw(get_standard_option);
29 use PVE
::Cluster
qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
34 use PVE
::RPCEnvironment
;
35 use PVE
::GuestHelpers
;
36 use PVE
::QemuServer
::PCI
qw(print_pci_addr print_pcie_addr print_pcie_root_port);
37 use PVE
::QemuServer
::Memory
;
38 use PVE
::QemuServer
::USB
qw(parse_usb_device);
39 use PVE
::QemuServer
::Cloudinit
;
42 use Time
::HiRes
qw(gettimeofday);
43 use File
::Copy
qw(copy);
46 my $EDK2_FW_BASE = '/usr/share/pve-edk2-firmware/';
49 "$EDK2_FW_BASE/OVMF_CODE.fd",
50 "$EDK2_FW_BASE/OVMF_VARS.fd"
53 "$EDK2_FW_BASE/AAVMF_CODE.fd",
54 "$EDK2_FW_BASE/AAVMF_VARS.fd"
58 my $qemu_snap_storage = { rbd
=> 1 };
60 my $cpuinfo = PVE
::ProcFSTools
::read_cpuinfo
();
62 my $QEMU_FORMAT_RE = qr/raw|cow|qcow|qcow2|qed|vmdk|cloop/;
64 # Note about locking: we use flock on the config file protect
65 # against concurent actions.
66 # Aditionaly, we have a 'lock' setting in the config file. This
67 # can be set to 'migrate', 'backup', 'snapshot' or 'rollback'. Most actions are not
68 # allowed when such lock is set. But you can ignore this kind of
69 # lock with the --skiplock flag.
71 cfs_register_file
('/qemu-server/',
75 PVE
::JSONSchema
::register_standard_option
('pve-qm-stateuri', {
76 description
=> "Some command save/restore state from this location.",
82 PVE
::JSONSchema
::register_standard_option
('pve-qm-image-format', {
84 enum
=> [qw(raw cow qcow qed qcow2 vmdk cloop)],
85 description
=> "The drive's backing file's data format.",
89 PVE
::JSONSchema
::register_standard_option
('pve-qemu-machine', {
90 description
=> "Specifies the Qemu machine type.",
92 pattern
=> '(pc|pc(-i440fx)?-\d+\.\d+(\.pxe)?|q35|pc-q35-\d+\.\d+(\.pxe)?|virt(?:-\d+\.\d+)?)',
97 #no warnings 'redefine';
100 my ($controller, $vmid, $option, $value) = @_;
102 my $path = "/sys/fs/cgroup/$controller/qemu.slice/$vmid.scope/$option";
103 PVE
::ProcFSTools
::write_proc_entry
($path, $value);
107 my $nodename = PVE
::INotify
::nodename
();
109 mkdir "/etc/pve/nodes/$nodename";
110 my $confdir = "/etc/pve/nodes/$nodename/qemu-server";
113 my $var_run_tmpdir = "/var/run/qemu-server";
114 mkdir $var_run_tmpdir;
116 my $lock_dir = "/var/lock/qemu-server";
119 my $cpu_vendor_list = {
121 486 => 'GenuineIntel',
122 pentium
=> 'GenuineIntel',
123 pentium2
=> 'GenuineIntel',
124 pentium3
=> 'GenuineIntel',
125 coreduo
=> 'GenuineIntel',
126 core2duo
=> 'GenuineIntel',
127 Conroe
=> 'GenuineIntel',
128 Penryn
=> 'GenuineIntel',
129 Nehalem
=> 'GenuineIntel',
130 'Nehalem-IBRS' => 'GenuineIntel',
131 Westmere
=> 'GenuineIntel',
132 'Westmere-IBRS' => 'GenuineIntel',
133 SandyBridge
=> 'GenuineIntel',
134 'SandyBridge-IBRS' => 'GenuineIntel',
135 IvyBridge
=> 'GenuineIntel',
136 'IvyBridge-IBRS' => 'GenuineIntel',
137 Haswell
=> 'GenuineIntel',
138 'Haswell-IBRS' => 'GenuineIntel',
139 'Haswell-noTSX' => 'GenuineIntel',
140 'Haswell-noTSX-IBRS' => 'GenuineIntel',
141 Broadwell
=> 'GenuineIntel',
142 'Broadwell-IBRS' => 'GenuineIntel',
143 'Broadwell-noTSX' => 'GenuineIntel',
144 'Broadwell-noTSX-IBRS' => 'GenuineIntel',
145 'Skylake-Client' => 'GenuineIntel',
146 'Skylake-Client-IBRS' => 'GenuineIntel',
147 'Skylake-Server' => 'GenuineIntel',
148 'Skylake-Server-IBRS' => 'GenuineIntel',
149 'Cascadelake-Server' => 'GenuineIntel',
150 KnightsMill
=> 'GenuineIntel',
154 athlon
=> 'AuthenticAMD',
155 phenom
=> 'AuthenticAMD',
156 Opteron_G1
=> 'AuthenticAMD',
157 Opteron_G2
=> 'AuthenticAMD',
158 Opteron_G3
=> 'AuthenticAMD',
159 Opteron_G4
=> 'AuthenticAMD',
160 Opteron_G5
=> 'AuthenticAMD',
161 EPYC
=> 'AuthenticAMD',
162 'EPYC-IBPB' => 'AuthenticAMD',
164 # generic types, use vendor from host node
173 my @supported_cpu_flags = (
187 my $cpu_flag = qr/[+-](@{[join('|', @supported_cpu_flags)]})/;
191 description
=> "Emulated CPU type.",
193 enum
=> [ sort { "\L$a" cmp "\L$b" } keys %$cpu_vendor_list ],
198 description
=> "Do not identify as a KVM virtual machine.",
205 pattern
=> qr/[a-zA-Z0-9]{1,12}/,
206 format_description
=> 'vendor-id',
207 description
=> 'The Hyper-V vendor ID. Some drivers or programs inside Windows guests need a specific ID.',
211 description
=> "List of additional CPU flags separated by ';'."
212 . " Use '+FLAG' to enable, '-FLAG' to disable a flag."
213 . " Currently supported flags: @{[join(', ', @supported_cpu_flags)]}.",
214 format_description
=> '+FLAG[;-FLAG...]',
216 pattern
=> qr/$cpu_flag(;$cpu_flag)*/,
225 enum
=> [qw(i6300esb ib700)],
226 description
=> "Watchdog type to emulate.",
227 default => 'i6300esb',
232 enum
=> [qw(reset shutdown poweroff pause debug none)],
233 description
=> "The action to perform if after activation the guest fails to poll the watchdog in time.",
237 PVE
::JSONSchema
::register_format
('pve-qm-watchdog', $watchdog_fmt);
241 description
=> "Enable/disable Qemu GuestAgent.",
246 fstrim_cloned_disks
=> {
247 description
=> "Run fstrim after cloning/moving a disk.",
256 description
=> "Select the VGA type.",
261 enum
=> [qw(cirrus qxl qxl2 qxl3 qxl4 none serial0 serial1 serial2 serial3 std virtio vmware)],
264 description
=> "Sets the VGA memory (in MiB). Has no effect with serial display.",
276 description
=> "The size of the file in MB.",
280 pattern
=> '[a-zA-Z0-9\-]+',
282 format_description
=> 'string',
283 description
=> "The name of the file. Will be prefixed with 'pve-shm-'. Default is the VMID. Will be deleted when the VM is stopped.",
290 enum
=> [qw(ich9-intel-hda intel-hda AC97)],
291 description
=> "Configure an audio device."
298 description
=> "Driver backend for the audio device."
302 my $spice_enhancements_fmt = {
307 description
=> "Enable folder sharing via SPICE. Needs Spice-WebDAV daemon installed in the VM."
311 enum
=> ['off', 'all', 'filter'],
314 description
=> "Enable video streaming. Uses compression for detected video streams."
322 description
=> "Specifies whether a VM will be started during system bootup.",
328 description
=> "Automatic restart after crash (currently ignored).",
333 type
=> 'string', format
=> 'pve-hotplug-features',
334 description
=> "Selectively enable hotplug features. This is a comma separated list of hotplug features: 'network', 'disk', 'cpu', 'memory' and 'usb'. Use '0' to disable hotplug completely. Value '1' is an alias for the default 'network,disk,usb'.",
335 default => 'network,disk,usb',
340 description
=> "Allow reboot. If set to '0' the VM exit on reboot.",
346 description
=> "Lock/unlock the VM.",
347 enum
=> [qw(backup clone create migrate rollback snapshot snapshot-delete suspending suspended)],
352 description
=> "Limit of CPU usage.",
353 verbose_description
=> "Limit of CPU usage.\n\nNOTE: If the computer has 2 CPUs, it has total of '2' CPU time. Value '0' indicates no CPU limit.",
361 description
=> "CPU weight for a VM.",
362 verbose_description
=> "CPU weight for a VM. Argument is used in the kernel fair scheduler. The larger the number is, the more CPU time this VM gets. Number is relative to weights of all the other running VMs.",
370 description
=> "Amount of RAM for the VM in MB. This is the maximum available memory when you use the balloon device.",
377 description
=> "Amount of target RAM for the VM in MB. Using zero disables the ballon driver.",
383 description
=> "Amount of memory shares for auto-ballooning. The larger the number is, the more memory this VM gets. Number is relative to weights of all other running VMs. Using zero disables auto-ballooning. Auto-ballooning is done by pvestatd.",
391 description
=> "Keybord layout for vnc server. Default is read from the '/etc/pve/datacenter.cfg' configuration file.".
392 "It should not be necessary to set it.",
393 enum
=> PVE
::Tools
::kvmkeymaplist
(),
398 type
=> 'string', format
=> 'dns-name',
399 description
=> "Set a name for the VM. Only used on the configuration web interface.",
404 description
=> "SCSI controller model",
405 enum
=> [qw(lsi lsi53c810 virtio-scsi-pci virtio-scsi-single megasas pvscsi)],
411 description
=> "Description for the VM. Only used on the configuration web interface. This is saved as comment inside the configuration file.",
416 enum
=> [qw(other wxp w2k w2k3 w2k8 wvista win7 win8 win10 l24 l26 solaris)],
417 description
=> "Specify guest operating system.",
418 verbose_description
=> <<EODESC,
419 Specify guest operating system. This is used to enable special
420 optimization/features for specific operating systems:
423 other;; unspecified OS
424 wxp;; Microsoft Windows XP
425 w2k;; Microsoft Windows 2000
426 w2k3;; Microsoft Windows 2003
427 w2k8;; Microsoft Windows 2008
428 wvista;; Microsoft Windows Vista
429 win7;; Microsoft Windows 7
430 win8;; Microsoft Windows 8/2012/2012r2
431 win10;; Microsoft Windows 10/2016
432 l24;; Linux 2.4 Kernel
433 l26;; Linux 2.6/3.X Kernel
434 solaris;; Solaris/OpenSolaris/OpenIndiania kernel
440 description
=> "Boot on floppy (a), hard disk (c), CD-ROM (d), or network (n).",
441 pattern
=> '[acdn]{1,4}',
446 type
=> 'string', format
=> 'pve-qm-bootdisk',
447 description
=> "Enable booting from specified disk.",
448 pattern
=> '(ide|sata|scsi|virtio)\d+',
453 description
=> "The number of CPUs. Please use option -sockets instead.",
460 description
=> "The number of CPU sockets.",
467 description
=> "The number of cores per socket.",
474 description
=> "Enable/disable NUMA.",
480 description
=> "Enable/disable hugepages memory.",
481 enum
=> [qw(any 2 1024)],
486 description
=> "Number of hotplugged vcpus.",
493 description
=> "Enable/disable ACPI.",
498 description
=> "Enable/disable Qemu GuestAgent and its properties.",
500 format
=> $agent_fmt,
505 description
=> "Enable/disable KVM hardware virtualization.",
511 description
=> "Enable/disable time drift fix.",
517 description
=> "Set the real time clock to local time. This is enabled by default if ostype indicates a Microsoft OS.",
522 description
=> "Freeze CPU at startup (use 'c' monitor command to start execution).",
526 type
=> 'string', format
=> $vga_fmt,
527 description
=> "Configure the VGA hardware.",
528 verbose_description
=> "Configure the VGA Hardware. If you want to use ".
529 "high resolution modes (>= 1280x1024x16) you may need to increase " .
530 "the vga memory option. Since QEMU 2.9 the default VGA display type " .
531 "is 'std' for all OS types besides some Windows versions (XP and " .
532 "older) which use 'cirrus'. The 'qxl' option enables the SPICE " .
533 "display server. For win* OS you can select how many independent " .
534 "displays you want, Linux guests can add displays them self.\n".
535 "You can also run without any graphic card, using a serial device as terminal.",
539 type
=> 'string', format
=> 'pve-qm-watchdog',
540 description
=> "Create a virtual hardware watchdog device.",
541 verbose_description
=> "Create a virtual hardware watchdog device. Once enabled" .
542 " (by a guest action), the watchdog must be periodically polled " .
543 "by an agent inside the guest or else the watchdog will reset " .
544 "the guest (or execute the respective action specified)",
549 typetext
=> "(now | YYYY-MM-DD | YYYY-MM-DDTHH:MM:SS)",
550 description
=> "Set the initial date of the real time clock. Valid format for date are: 'now' or '2006-06-17T16:01:21' or '2006-06-17'.",
551 pattern
=> '(now|\d{4}-\d{1,2}-\d{1,2}(T\d{1,2}:\d{1,2}:\d{1,2})?)',
554 startup
=> get_standard_option
('pve-startup-order'),
558 description
=> "Enable/disable Template.",
564 description
=> "Arbitrary arguments passed to kvm.",
565 verbose_description
=> <<EODESCR,
566 Arbitrary arguments passed to kvm, for example:
568 args: -no-reboot -no-hpet
570 NOTE: this option is for experts only.
577 description
=> "Enable/disable the USB tablet device.",
578 verbose_description
=> "Enable/disable the USB tablet device. This device is " .
579 "usually needed to allow absolute mouse positioning with VNC. " .
580 "Else the mouse runs out of sync with normal VNC clients. " .
581 "If you're running lots of console-only guests on one host, " .
582 "you may consider disabling this to save some context switches. " .
583 "This is turned off by default if you use spice (-vga=qxl).",
588 description
=> "Set maximum speed (in MB/s) for migrations. Value 0 is no limit.",
592 migrate_downtime
=> {
595 description
=> "Set maximum tolerated downtime (in seconds) for migrations.",
601 type
=> 'string', format
=> 'pve-qm-ide',
602 typetext
=> '<volume>',
603 description
=> "This is an alias for option -ide2",
607 description
=> "Emulated CPU type.",
611 parent
=> get_standard_option
('pve-snapshot-name', {
613 description
=> "Parent snapshot name. This is used internally, and should not be modified.",
617 description
=> "Timestamp for snapshots.",
623 type
=> 'string', format
=> 'pve-volume-id',
624 description
=> "Reference to a volume which stores the VM state. This is used internally for snapshots.",
626 vmstatestorage
=> get_standard_option
('pve-storage-id', {
627 description
=> "Default storage for VM state volumes/files.",
630 runningmachine
=> get_standard_option
('pve-qemu-machine', {
631 description
=> "Specifies the Qemu machine type of the running vm. This is used internally for snapshots.",
633 machine
=> get_standard_option
('pve-qemu-machine'),
635 description
=> "Virtual processor architecture. Defaults to the host.",
638 enum
=> [qw(x86_64 aarch64)],
641 description
=> "Specify SMBIOS type 1 fields.",
642 type
=> 'string', format
=> 'pve-qm-smbios1',
649 description
=> "Sets the protection flag of the VM. This will disable the remove VM and remove disk operations.",
655 enum
=> [ qw(seabios ovmf) ],
656 description
=> "Select BIOS implementation.",
657 default => 'seabios',
661 pattern
=> '(?:[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}|[01])',
662 format_description
=> 'UUID',
663 description
=> "Set VM Generation ID. Use '1' to autogenerate on create or update, pass '0' to disable explicitly.",
664 verbose_description
=> "The VM generation ID (vmgenid) device exposes a".
665 " 128-bit integer value identifier to the guest OS. This allows to".
666 " notify the guest operating system when the virtual machine is".
667 " executed with a different configuration (e.g. snapshot execution".
668 " or creation from a template). The guest operating system notices".
669 " the change, and is then able to react as appropriate by marking".
670 " its copies of distributed databases as dirty, re-initializing its".
671 " random number generator, etc.\n".
672 "Note that auto-creation only works when done throug API/CLI create".
673 " or update methods, but not when manually editing the config file.",
674 default => "1 (autogenerated)",
679 format
=> 'pve-volume-id',
681 description
=> "Script that will be executed during various steps in the vms lifetime.",
685 format
=> $ivshmem_fmt,
686 description
=> "Inter-VM shared memory. Useful for direct communication between VMs, or to the host.",
691 format
=> $audio_fmt,
692 description
=> "Configure a audio device, useful in combination with QXL/Spice.",
695 spice_enhancements
=> {
697 format
=> $spice_enhancements_fmt,
698 description
=> "Configure additional enhancements for SPICE.",
707 description
=> 'Specify a custom file containing all meta data passed to the VM via cloud-init. This is provider specific meaning configdrive2 and nocloud differ.',
708 format
=> 'pve-volume-id',
709 format_description
=> 'volume',
714 description
=> 'Specify a custom file containing all network data passed to the VM via cloud-init.',
715 format
=> 'pve-volume-id',
716 format_description
=> 'volume',
721 description
=> 'Specify a custom file containing all user data passed to the VM via cloud-init.',
722 format
=> 'pve-volume-id',
723 format_description
=> 'volume',
726 PVE
::JSONSchema
::register_format
('pve-qm-cicustom', $cicustom_fmt);
728 my $confdesc_cloudinit = {
732 description
=> 'Specifies the cloud-init configuration format. The default depends on the configured operating system type (`ostype`. We use the `nocloud` format for Linux, and `configdrive2` for windows.',
733 enum
=> ['configdrive2', 'nocloud'],
738 description
=> "cloud-init: User name to change ssh keys and password for instead of the image's configured default user.",
743 description
=> 'cloud-init: Password to assign the user. Using this is generally not recommended. Use ssh keys instead. Also note that older cloud-init versions do not support hashed passwords.',
748 description
=> 'cloud-init: Specify custom files to replace the automatically generated ones at start.',
749 format
=> 'pve-qm-cicustom',
754 description
=> "cloud-init: Sets DNS search domains for a container. Create will automatically use the setting from the host if neither searchdomain nor nameserver are set.",
758 type
=> 'string', format
=> 'address-list',
759 description
=> "cloud-init: Sets DNS server IP address for a container. Create will automatically use the setting from the host if neither searchdomain nor nameserver are set.",
764 format
=> 'urlencoded',
765 description
=> "cloud-init: Setup public SSH keys (one key per line, OpenSSH format).",
769 # what about other qemu settings ?
771 #machine => 'string',
784 ##soundhw => 'string',
786 while (my ($k, $v) = each %$confdesc) {
787 PVE
::JSONSchema
::register_standard_option
("pve-qm-$k", $v);
790 my $MAX_IDE_DISKS = 4;
791 my $MAX_SCSI_DISKS = 14;
792 my $MAX_VIRTIO_DISKS = 16;
793 my $MAX_SATA_DISKS = 6;
794 my $MAX_USB_DEVICES = 5;
796 my $MAX_UNUSED_DISKS = 256;
797 my $MAX_HOSTPCI_DEVICES = 16;
798 my $MAX_SERIAL_PORTS = 4;
799 my $MAX_PARALLEL_PORTS = 3;
805 pattern
=> qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
806 description
=> "CPUs accessing this NUMA node.",
807 format_description
=> "id[-id];...",
811 description
=> "Amount of memory this NUMA node provides.",
816 pattern
=> qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
817 description
=> "Host NUMA nodes to use.",
818 format_description
=> "id[-id];...",
823 enum
=> [qw(preferred bind interleave)],
824 description
=> "NUMA allocation policy.",
828 PVE
::JSONSchema
::register_format
('pve-qm-numanode', $numa_fmt);
831 type
=> 'string', format
=> $numa_fmt,
832 description
=> "NUMA topology.",
834 PVE
::JSONSchema
::register_standard_option
("pve-qm-numanode", $numadesc);
836 for (my $i = 0; $i < $MAX_NUMA; $i++) {
837 $confdesc->{"numa$i"} = $numadesc;
840 my $nic_model_list = ['rtl8139', 'ne2k_pci', 'e1000', 'pcnet', 'virtio',
841 'ne2k_isa', 'i82551', 'i82557b', 'i82559er', 'vmxnet3',
842 'e1000-82540em', 'e1000-82544gc', 'e1000-82545em'];
843 my $nic_model_list_txt = join(' ', sort @$nic_model_list);
845 my $net_fmt_bridge_descr = <<__EOD__;
846 Bridge to attach the network device to. The Proxmox VE standard bridge
849 If you do not specify a bridge, we create a kvm user (NATed) network
850 device, which provides DHCP and DNS services. The following addresses
857 The DHCP server assign addresses to the guest starting from 10.0.2.15.
861 macaddr
=> get_standard_option
('mac-addr', {
862 description
=> "MAC address. That address must be unique withing your network. This is automatically generated if not specified.",
866 description
=> "Network Card Model. The 'virtio' model provides the best performance with very low CPU overhead. If your guest does not support this driver, it is usually best to use 'e1000'.",
867 enum
=> $nic_model_list,
870 (map { $_ => { keyAlias
=> 'model', alias
=> 'macaddr' }} @$nic_model_list),
873 description
=> $net_fmt_bridge_descr,
874 format_description
=> 'bridge',
879 minimum
=> 0, maximum
=> 16,
880 description
=> 'Number of packet queues to be used on the device.',
886 description
=> "Rate limit in mbps (megabytes per second) as floating point number.",
891 minimum
=> 1, maximum
=> 4094,
892 description
=> 'VLAN tag to apply to packets on this interface.',
897 pattern
=> qr/\d+(?:-\d+)?(?:;\d+(?:-\d+)?)*/,
898 description
=> 'VLAN trunks to pass through this interface.',
899 format_description
=> 'vlanid[;vlanid...]',
904 description
=> 'Whether this interface should be protected by the firewall.',
909 description
=> 'Whether this interface should be disconnected (like pulling the plug).',
916 type
=> 'string', format
=> $net_fmt,
917 description
=> "Specify network devices.",
920 PVE
::JSONSchema
::register_standard_option
("pve-qm-net", $netdesc);
925 format
=> 'pve-ipv4-config',
926 format_description
=> 'IPv4Format/CIDR',
927 description
=> 'IPv4 address in CIDR format.',
934 format_description
=> 'GatewayIPv4',
935 description
=> 'Default gateway for IPv4 traffic.',
941 format
=> 'pve-ipv6-config',
942 format_description
=> 'IPv6Format/CIDR',
943 description
=> 'IPv6 address in CIDR format.',
950 format_description
=> 'GatewayIPv6',
951 description
=> 'Default gateway for IPv6 traffic.',
956 PVE
::JSONSchema
::register_format
('pve-qm-ipconfig', $ipconfig_fmt);
959 type
=> 'string', format
=> 'pve-qm-ipconfig',
960 description
=> <<'EODESCR',
961 cloud-init: Specify IP addresses and gateways for the corresponding interface.
963 IP addresses use CIDR notation, gateways are optional but need an IP of the same type specified.
965 The special string 'dhcp' can be used for IP addresses to use DHCP, in which case no explicit gateway should be provided.
966 For IPv6 the special string 'auto' can be used to use stateless autoconfiguration.
968 If cloud-init is enabled and neither an IPv4 nor an IPv6 address is specified, it defaults to using dhcp on IPv4.
971 PVE
::JSONSchema
::register_standard_option
("pve-qm-ipconfig", $netdesc);
973 for (my $i = 0; $i < $MAX_NETS; $i++) {
974 $confdesc->{"net$i"} = $netdesc;
975 $confdesc_cloudinit->{"ipconfig$i"} = $ipconfigdesc;
978 foreach my $key (keys %$confdesc_cloudinit) {
979 $confdesc->{$key} = $confdesc_cloudinit->{$key};
982 PVE
::JSONSchema
::register_format
('pve-volume-id-or-qm-path', \
&verify_volume_id_or_qm_path
);
983 sub verify_volume_id_or_qm_path
{
984 my ($volid, $noerr) = @_;
986 if ($volid eq 'none' || $volid eq 'cdrom' || $volid =~ m
|^/|) {
990 # if its neither 'none' nor 'cdrom' nor a path, check if its a volume-id
991 $volid = eval { PVE
::JSONSchema
::check_format
('pve-volume-id', $volid, '') };
993 return undef if $noerr;
1001 my %drivedesc_base = (
1002 volume
=> { alias
=> 'file' },
1005 format
=> 'pve-volume-id-or-qm-path',
1007 format_description
=> 'volume',
1008 description
=> "The drive's backing volume.",
1012 enum
=> [qw(cdrom disk)],
1013 description
=> "The drive's media type.",
1019 description
=> "Force the drive's physical geometry to have a specific cylinder count.",
1024 description
=> "Force the drive's physical geometry to have a specific head count.",
1029 description
=> "Force the drive's physical geometry to have a specific sector count.",
1034 enum
=> [qw(none lba auto)],
1035 description
=> "Force disk geometry bios translation mode.",
1040 description
=> "Controls qemu's snapshot mode feature."
1041 . " If activated, changes made to the disk are temporary and will"
1042 . " be discarded when the VM is shutdown.",
1047 enum
=> [qw(none writethrough writeback unsafe directsync)],
1048 description
=> "The drive's cache mode",
1051 format
=> get_standard_option
('pve-qm-image-format'),
1054 format
=> 'disk-size',
1055 format_description
=> 'DiskSize',
1056 description
=> "Disk size. This is purely informational and has no effect.",
1061 description
=> "Whether the drive should be included when making backups.",
1066 description
=> 'Whether the drive should considered for replication jobs.',
1072 enum
=> [qw(ignore report stop)],
1073 description
=> 'Read error action.',
1078 enum
=> [qw(enospc ignore report stop)],
1079 description
=> 'Write error action.',
1084 enum
=> [qw(native threads)],
1085 description
=> 'AIO type to use.',
1090 enum
=> [qw(ignore on)],
1091 description
=> 'Controls whether to pass discard/trim requests to the underlying storage.',
1096 description
=> 'Controls whether to detect and try to optimize writes of zeroes.',
1101 format
=> 'urlencoded',
1102 format_description
=> 'serial',
1103 maxLength
=> 20*3, # *3 since it's %xx url enoded
1104 description
=> "The drive's reported serial number, url-encoded, up to 20 bytes long.",
1109 description
=> 'Mark this locally-managed volume as available on all nodes',
1110 verbose_description
=> "Mark this locally-managed volume as available on all nodes.\n\nWARNING: This option does not share the volume automatically, it assumes it is shared already!",
1116 my %iothread_fmt = ( iothread
=> {
1118 description
=> "Whether to use iothreads for this drive",
1125 format
=> 'urlencoded',
1126 format_description
=> 'model',
1127 maxLength
=> 40*3, # *3 since it's %xx url enoded
1128 description
=> "The drive's reported model name, url-encoded, up to 40 bytes long.",
1136 description
=> "Number of queues.",
1142 my %scsiblock_fmt = (
1145 description
=> "whether to use scsi-block for full passthrough of host block device\n\nWARNING: can lead to I/O errors in combination with low memory or high memory fragmentation on host",
1154 description
=> "Whether to expose this drive as an SSD, rather than a rotational hard disk.",
1162 pattern
=> qr/^(0x)[0-9a-fA-F]{16}/,
1163 format_description
=> 'wwn',
1164 description
=> "The drive's worldwide name, encoded as 16 bytes hex string, prefixed by '0x'.",
1169 my $add_throttle_desc = sub {
1170 my ($key, $type, $what, $unit, $longunit, $minimum) = @_;
1173 format_description
=> $unit,
1174 description
=> "Maximum $what in $longunit.",
1177 $d->{minimum
} = $minimum if defined($minimum);
1178 $drivedesc_base{$key} = $d;
1180 # throughput: (leaky bucket)
1181 $add_throttle_desc->('bps', 'integer', 'r/w speed', 'bps', 'bytes per second');
1182 $add_throttle_desc->('bps_rd', 'integer', 'read speed', 'bps', 'bytes per second');
1183 $add_throttle_desc->('bps_wr', 'integer', 'write speed', 'bps', 'bytes per second');
1184 $add_throttle_desc->('mbps', 'number', 'r/w speed', 'mbps', 'megabytes per second');
1185 $add_throttle_desc->('mbps_rd', 'number', 'read speed', 'mbps', 'megabytes per second');
1186 $add_throttle_desc->('mbps_wr', 'number', 'write speed', 'mbps', 'megabytes per second');
1187 $add_throttle_desc->('iops', 'integer', 'r/w I/O', 'iops', 'operations per second');
1188 $add_throttle_desc->('iops_rd', 'integer', 'read I/O', 'iops', 'operations per second');
1189 $add_throttle_desc->('iops_wr', 'integer', 'write I/O', 'iops', 'operations per second');
1191 # pools: (pool of IO before throttling starts taking effect)
1192 $add_throttle_desc->('mbps_max', 'number', 'unthrottled r/w pool', 'mbps', 'megabytes per second');
1193 $add_throttle_desc->('mbps_rd_max', 'number', 'unthrottled read pool', 'mbps', 'megabytes per second');
1194 $add_throttle_desc->('mbps_wr_max', 'number', 'unthrottled write pool', 'mbps', 'megabytes per second');
1195 $add_throttle_desc->('iops_max', 'integer', 'unthrottled r/w I/O pool', 'iops', 'operations per second');
1196 $add_throttle_desc->('iops_rd_max', 'integer', 'unthrottled read I/O pool', 'iops', 'operations per second');
1197 $add_throttle_desc->('iops_wr_max', 'integer', 'unthrottled write I/O pool', 'iops', 'operations per second');
1200 $add_throttle_desc->('bps_max_length', 'integer', 'length of I/O bursts', 'seconds', 'seconds', 1);
1201 $add_throttle_desc->('bps_rd_max_length', 'integer', 'length of read I/O bursts', 'seconds', 'seconds', 1);
1202 $add_throttle_desc->('bps_wr_max_length', 'integer', 'length of write I/O bursts', 'seconds', 'seconds', 1);
1203 $add_throttle_desc->('iops_max_length', 'integer', 'length of I/O bursts', 'seconds', 'seconds', 1);
1204 $add_throttle_desc->('iops_rd_max_length', 'integer', 'length of read I/O bursts', 'seconds', 'seconds', 1);
1205 $add_throttle_desc->('iops_wr_max_length', 'integer', 'length of write I/O bursts', 'seconds', 'seconds', 1);
1208 $drivedesc_base{'bps_rd_length'} = { alias
=> 'bps_rd_max_length' };
1209 $drivedesc_base{'bps_wr_length'} = { alias
=> 'bps_wr_max_length' };
1210 $drivedesc_base{'iops_rd_length'} = { alias
=> 'iops_rd_max_length' };
1211 $drivedesc_base{'iops_wr_length'} = { alias
=> 'iops_wr_max_length' };
1219 PVE
::JSONSchema
::register_format
("pve-qm-ide", $ide_fmt);
1223 type
=> 'string', format
=> $ide_fmt,
1224 description
=> "Use volume as IDE hard disk or CD-ROM (n is 0 to " .($MAX_IDE_DISKS -1) . ").",
1226 PVE
::JSONSchema
::register_standard_option
("pve-qm-ide", $idedesc);
1238 type
=> 'string', format
=> $scsi_fmt,
1239 description
=> "Use volume as SCSI hard disk or CD-ROM (n is 0 to " . ($MAX_SCSI_DISKS - 1) . ").",
1241 PVE
::JSONSchema
::register_standard_option
("pve-qm-scsi", $scsidesc);
1250 type
=> 'string', format
=> $sata_fmt,
1251 description
=> "Use volume as SATA hard disk or CD-ROM (n is 0 to " . ($MAX_SATA_DISKS - 1). ").",
1253 PVE
::JSONSchema
::register_standard_option
("pve-qm-sata", $satadesc);
1261 type
=> 'string', format
=> $virtio_fmt,
1262 description
=> "Use volume as VIRTIO hard disk (n is 0 to " . ($MAX_VIRTIO_DISKS - 1) . ").",
1264 PVE
::JSONSchema
::register_standard_option
("pve-qm-virtio", $virtiodesc);
1266 my $alldrive_fmt = {
1277 volume
=> { alias
=> 'file' },
1280 format
=> 'pve-volume-id-or-qm-path',
1282 format_description
=> 'volume',
1283 description
=> "The drive's backing volume.",
1285 format
=> get_standard_option
('pve-qm-image-format'),
1288 format
=> 'disk-size',
1289 format_description
=> 'DiskSize',
1290 description
=> "Disk size. This is purely informational and has no effect.",
1295 my $efidisk_desc = {
1297 type
=> 'string', format
=> $efidisk_fmt,
1298 description
=> "Configure a Disk for storing EFI vars",
1301 PVE
::JSONSchema
::register_standard_option
("pve-qm-efidisk", $efidisk_desc);
1306 type
=> 'string', format
=> 'pve-qm-usb-device',
1307 format_description
=> 'HOSTUSBDEVICE|spice',
1308 description
=> <<EODESCR,
1309 The Host USB device or port or the value 'spice'. HOSTUSBDEVICE syntax is:
1311 'bus-port(.port)*' (decimal numbers) or
1312 'vendor_id:product_id' (hexadeciaml numbers) or
1315 You can use the 'lsusb -t' command to list existing usb devices.
1317 NOTE: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
1319 The value 'spice' can be used to add a usb redirection devices for spice.
1325 description
=> "Specifies whether if given host option is a USB3 device or port.",
1332 type
=> 'string', format
=> $usb_fmt,
1333 description
=> "Configure an USB device (n is 0 to 4).",
1335 PVE
::JSONSchema
::register_standard_option
("pve-qm-usb", $usbdesc);
1337 my $PCIRE = qr/[a-f0-9]{2}:[a-f0-9]{2}(?:\.[a-f0-9])?/;
1342 pattern
=> qr/$PCIRE(;$PCIRE)*/,
1343 format_description
=> 'HOSTPCIID[;HOSTPCIID2...]',
1344 description
=> <<EODESCR,
1345 Host PCI device pass through. The PCI ID of a host's PCI device or a list
1346 of PCI virtual functions of the host. HOSTPCIID syntax is:
1348 'bus:dev.func' (hexadecimal numbers)
1350 You can us the 'lspci' command to list existing PCI devices.
1355 description
=> "Specify whether or not the device's ROM will be visible in the guest's memory map.",
1361 pattern
=> '[^,;]+',
1362 format_description
=> 'string',
1363 description
=> "Custom pci device rom filename (must be located in /usr/share/kvm/).",
1368 description
=> "Choose the PCI-express bus (needs the 'q35' machine model).",
1374 description
=> "Enable vfio-vga device support.",
1380 format_description
=> 'string',
1381 pattern
=> '[^/\.:]+',
1383 description
=> <<EODESCR
1384 The type of mediated device to use.
1385 An instance of this type will be created on startup of the VM and
1386 will be cleaned up when the VM stops.
1390 PVE
::JSONSchema
::register_format
('pve-qm-hostpci', $hostpci_fmt);
1394 type
=> 'string', format
=> 'pve-qm-hostpci',
1395 description
=> "Map host PCI devices into guest.",
1396 verbose_description
=> <<EODESCR,
1397 Map host PCI devices into guest.
1399 NOTE: This option allows direct access to host hardware. So it is no longer
1400 possible to migrate such machines - use with special care.
1402 CAUTION: Experimental! User reported problems with this option.
1405 PVE
::JSONSchema
::register_standard_option
("pve-qm-hostpci", $hostpcidesc);
1410 pattern
=> '(/dev/.+|socket)',
1411 description
=> "Create a serial device inside the VM (n is 0 to 3)",
1412 verbose_description
=> <<EODESCR,
1413 Create a serial device inside the VM (n is 0 to 3), and pass through a
1414 host serial device (i.e. /dev/ttyS0), or create a unix socket on the
1415 host side (use 'qm terminal' to open a terminal connection).
1417 NOTE: If you pass through a host serial device, it is no longer possible to migrate such machines - use with special care.
1419 CAUTION: Experimental! User reported problems with this option.
1426 pattern
=> '/dev/parport\d+|/dev/usb/lp\d+',
1427 description
=> "Map host parallel devices (n is 0 to 2).",
1428 verbose_description
=> <<EODESCR,
1429 Map host parallel devices (n is 0 to 2).
1431 NOTE: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
1433 CAUTION: Experimental! User reported problems with this option.
1437 for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
1438 $confdesc->{"parallel$i"} = $paralleldesc;
1441 for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
1442 $confdesc->{"serial$i"} = $serialdesc;
1445 for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
1446 $confdesc->{"hostpci$i"} = $hostpcidesc;
1449 for (my $i = 0; $i < $MAX_IDE_DISKS; $i++) {
1450 $drivename_hash->{"ide$i"} = 1;
1451 $confdesc->{"ide$i"} = $idedesc;
1454 for (my $i = 0; $i < $MAX_SATA_DISKS; $i++) {
1455 $drivename_hash->{"sata$i"} = 1;
1456 $confdesc->{"sata$i"} = $satadesc;
1459 for (my $i = 0; $i < $MAX_SCSI_DISKS; $i++) {
1460 $drivename_hash->{"scsi$i"} = 1;
1461 $confdesc->{"scsi$i"} = $scsidesc ;
1464 for (my $i = 0; $i < $MAX_VIRTIO_DISKS; $i++) {
1465 $drivename_hash->{"virtio$i"} = 1;
1466 $confdesc->{"virtio$i"} = $virtiodesc;
1469 $drivename_hash->{efidisk0
} = 1;
1470 $confdesc->{efidisk0
} = $efidisk_desc;
1472 for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) {
1473 $confdesc->{"usb$i"} = $usbdesc;
1478 type
=> 'string', format
=> 'pve-volume-id',
1479 description
=> "Reference to unused volumes. This is used internally, and should not be modified manually.",
1482 for (my $i = 0; $i < $MAX_UNUSED_DISKS; $i++) {
1483 $confdesc->{"unused$i"} = $unuseddesc;
1486 my $kvm_api_version = 0;
1489 return $kvm_api_version if $kvm_api_version;
1491 open my $fh, '<', '/dev/kvm'
1494 # 0xae00 => KVM_GET_API_VERSION
1495 $kvm_api_version = ioctl($fh, 0xae00, 0);
1497 return $kvm_api_version;
1500 my $kvm_user_version = {};
1503 sub kvm_user_version
{
1506 $binary //= get_command_for_arch
(get_host_arch
()); # get the native arch by default
1507 my $st = stat($binary);
1509 my $cachedmtime = $kvm_mtime->{$binary} // -1;
1510 return $kvm_user_version->{$binary} if $kvm_user_version->{$binary} &&
1511 $cachedmtime == $st->mtime;
1513 $kvm_user_version->{$binary} = 'unknown';
1514 $kvm_mtime->{$binary} = $st->mtime;
1518 if ($line =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?)(\.\d+)?[,\s]/) {
1519 $kvm_user_version->{$binary} = $2;
1523 eval { run_command
([$binary, '--version'], outfunc
=> $code); };
1526 return $kvm_user_version->{$binary};
1530 sub kernel_has_vhost_net
{
1531 return -c
'/dev/vhost-net';
1534 sub valid_drive_names
{
1535 # order is important - used to autoselect boot disk
1536 return ((map { "ide$_" } (0 .. ($MAX_IDE_DISKS - 1))),
1537 (map { "scsi$_" } (0 .. ($MAX_SCSI_DISKS - 1))),
1538 (map { "virtio$_" } (0 .. ($MAX_VIRTIO_DISKS - 1))),
1539 (map { "sata$_" } (0 .. ($MAX_SATA_DISKS - 1))),
1543 sub is_valid_drivename
{
1546 return defined($drivename_hash->{$dev});
1551 return defined($confdesc->{$key});
1555 return $nic_model_list;
1558 sub os_list_description
{
1562 wxp
=> 'Windows XP',
1563 w2k
=> 'Windows 2000',
1564 w2k3
=>, 'Windows 2003',
1565 w2k8
=> 'Windows 2008',
1566 wvista
=> 'Windows Vista',
1567 win7
=> 'Windows 7',
1568 win8
=> 'Windows 8/2012',
1569 win10
=> 'Windows 10/2016',
1577 sub get_cdrom_path
{
1579 return $cdrom_path if $cdrom_path;
1581 return $cdrom_path = "/dev/cdrom" if -l
"/dev/cdrom";
1582 return $cdrom_path = "/dev/cdrom1" if -l
"/dev/cdrom1";
1583 return $cdrom_path = "/dev/cdrom2" if -l
"/dev/cdrom2";
1587 my ($storecfg, $vmid, $cdrom) = @_;
1589 if ($cdrom eq 'cdrom') {
1590 return get_cdrom_path
();
1591 } elsif ($cdrom eq 'none') {
1593 } elsif ($cdrom =~ m
|^/|) {
1596 return PVE
::Storage
::path
($storecfg, $cdrom);
1600 # try to convert old style file names to volume IDs
1601 sub filename_to_volume_id
{
1602 my ($vmid, $file, $media) = @_;
1604 if (!($file eq 'none' || $file eq 'cdrom' ||
1605 $file =~ m
|^/dev/.+| || $file =~ m/^([^:]+):(.+)$/)) {
1607 return undef if $file =~ m
|/|;
1609 if ($media && $media eq 'cdrom') {
1610 $file = "local:iso/$file";
1612 $file = "local:$vmid/$file";
1619 sub verify_media_type
{
1620 my ($opt, $vtype, $media) = @_;
1625 if ($media eq 'disk') {
1627 } elsif ($media eq 'cdrom') {
1630 die "internal error";
1633 return if ($vtype eq $etype);
1635 raise_param_exc
({ $opt => "unexpected media type ($vtype != $etype)" });
1638 sub cleanup_drive_path
{
1639 my ($opt, $storecfg, $drive) = @_;
1641 # try to convert filesystem paths to volume IDs
1643 if (($drive->{file
} !~ m/^(cdrom|none)$/) &&
1644 ($drive->{file
} !~ m
|^/dev/.+|) &&
1645 ($drive->{file
} !~ m/^([^:]+):(.+)$/) &&
1646 ($drive->{file
} !~ m/^\d+$/)) {
1647 my ($vtype, $volid) = PVE
::Storage
::path_to_volume_id
($storecfg, $drive->{file
});
1648 raise_param_exc
({ $opt => "unable to associate path '$drive->{file}' to any storage"}) if !$vtype;
1649 $drive->{media
} = 'cdrom' if !$drive->{media
} && $vtype eq 'iso';
1650 verify_media_type
($opt, $vtype, $drive->{media
});
1651 $drive->{file
} = $volid;
1654 $drive->{media
} = 'cdrom' if !$drive->{media
} && $drive->{file
} =~ m/^(cdrom|none)$/;
1657 sub parse_hotplug_features
{
1662 return $res if $data eq '0';
1664 $data = $confdesc->{hotplug
}->{default} if $data eq '1';
1666 foreach my $feature (PVE
::Tools
::split_list
($data)) {
1667 if ($feature =~ m/^(network|disk|cpu|memory|usb)$/) {
1670 die "invalid hotplug feature '$feature'\n";
1676 PVE
::JSONSchema
::register_format
('pve-hotplug-features', \
&pve_verify_hotplug_features
);
1677 sub pve_verify_hotplug_features
{
1678 my ($value, $noerr) = @_;
1680 return $value if parse_hotplug_features
($value);
1682 return undef if $noerr;
1684 die "unable to parse hotplug option\n";
1687 # ideX = [volume=]volume-id[,media=d][,cyls=c,heads=h,secs=s[,trans=t]]
1688 # [,snapshot=on|off][,cache=on|off][,format=f][,backup=yes|no]
1689 # [,rerror=ignore|report|stop][,werror=enospc|ignore|report|stop]
1690 # [,aio=native|threads][,discard=ignore|on][,detect_zeroes=on|off]
1691 # [,iothread=on][,serial=serial][,model=model]
1694 my ($key, $data) = @_;
1696 my ($interface, $index);
1698 if ($key =~ m/^([^\d]+)(\d+)$/) {
1705 my $desc = $key =~ /^unused\d+$/ ?
$alldrive_fmt
1706 : $confdesc->{$key}->{format
};
1708 warn "invalid drive key: $key\n";
1711 my $res = eval { PVE
::JSONSchema
::parse_property_string
($desc, $data) };
1712 return undef if !$res;
1713 $res->{interface
} = $interface;
1714 $res->{index} = $index;
1717 foreach my $opt (qw(bps bps_rd bps_wr)) {
1718 if (my $bps = defined(delete $res->{$opt})) {
1719 if (defined($res->{"m$opt"})) {
1720 warn "both $opt and m$opt specified\n";
1724 $res->{"m$opt"} = sprintf("%.3f", $bps / (1024*1024.0));
1728 # can't use the schema's 'requires' because of the mbps* => bps* "transforming aliases"
1729 for my $requirement (
1730 [mbps_max
=> 'mbps'],
1731 [mbps_rd_max
=> 'mbps_rd'],
1732 [mbps_wr_max
=> 'mbps_wr'],
1733 [miops_max
=> 'miops'],
1734 [miops_rd_max
=> 'miops_rd'],
1735 [miops_wr_max
=> 'miops_wr'],
1736 [bps_max_length
=> 'mbps_max'],
1737 [bps_rd_max_length
=> 'mbps_rd_max'],
1738 [bps_wr_max_length
=> 'mbps_wr_max'],
1739 [iops_max_length
=> 'iops_max'],
1740 [iops_rd_max_length
=> 'iops_rd_max'],
1741 [iops_wr_max_length
=> 'iops_wr_max']) {
1742 my ($option, $requires) = @$requirement;
1743 if ($res->{$option} && !$res->{$requires}) {
1744 warn "$option requires $requires\n";
1749 return undef if $error;
1751 return undef if $res->{mbps_rd
} && $res->{mbps
};
1752 return undef if $res->{mbps_wr
} && $res->{mbps
};
1753 return undef if $res->{iops_rd
} && $res->{iops
};
1754 return undef if $res->{iops_wr
} && $res->{iops
};
1756 if ($res->{media
} && ($res->{media
} eq 'cdrom')) {
1757 return undef if $res->{snapshot
} || $res->{trans
} || $res->{format
};
1758 return undef if $res->{heads
} || $res->{secs
} || $res->{cyls
};
1759 return undef if $res->{interface
} eq 'virtio';
1762 if (my $size = $res->{size
}) {
1763 return undef if !defined($res->{size
} = PVE
::JSONSchema
::parse_size
($size));
1770 my ($vmid, $drive) = @_;
1771 my $data = { %$drive };
1772 delete $data->{$_} for qw(index interface);
1773 return PVE
::JSONSchema
::print_property_string
($data, $alldrive_fmt);
1777 my($fh, $noerr) = @_;
1780 my $SG_GET_VERSION_NUM = 0x2282;
1782 my $versionbuf = "\x00" x
8;
1783 my $ret = ioctl($fh, $SG_GET_VERSION_NUM, $versionbuf);
1785 die "scsi ioctl SG_GET_VERSION_NUM failoed - $!\n" if !$noerr;
1788 my $version = unpack("I", $versionbuf);
1789 if ($version < 30000) {
1790 die "scsi generic interface too old\n" if !$noerr;
1794 my $buf = "\x00" x
36;
1795 my $sensebuf = "\x00" x
8;
1796 my $cmd = pack("C x3 C x1", 0x12, 36);
1798 # see /usr/include/scsi/sg.h
1799 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";
1801 my $packet = pack($sg_io_hdr_t, ord('S'), -3, length($cmd),
1802 length($sensebuf), 0, length($buf), $buf,
1803 $cmd, $sensebuf, 6000);
1805 $ret = ioctl($fh, $SG_IO, $packet);
1807 die "scsi ioctl SG_IO failed - $!\n" if !$noerr;
1811 my @res = unpack($sg_io_hdr_t, $packet);
1812 if ($res[17] || $res[18]) {
1813 die "scsi ioctl SG_IO status error - $!\n" if !$noerr;
1818 (my $byte0, my $byte1, $res->{vendor
},
1819 $res->{product
}, $res->{revision
}) = unpack("C C x6 A8 A16 A4", $buf);
1821 $res->{removable
} = $byte1 & 128 ?
1 : 0;
1822 $res->{type
} = $byte0 & 31;
1830 my $fh = IO
::File-
>new("+<$path") || return undef;
1831 my $res = scsi_inquiry
($fh, 1);
1837 sub machine_type_is_q35
{
1840 return $conf->{machine
} && ($conf->{machine
} =~ m/q35/) ?
1 : 0;
1843 sub print_tabletdevice_full
{
1844 my ($conf, $arch) = @_;
1846 my $q35 = machine_type_is_q35
($conf);
1848 # we use uhci for old VMs because tablet driver was buggy in older qemu
1850 if (machine_type_is_q35
($conf) || $arch eq 'aarch64') {
1856 return "usb-tablet,id=tablet,bus=$usbbus.0,port=1";
1859 sub print_keyboarddevice_full
{
1860 my ($conf, $arch, $machine) = @_;
1862 return undef if $arch ne 'aarch64';
1864 return "usb-kbd,id=keyboard,bus=ehci.0,port=2";
1867 sub print_drivedevice_full
{
1868 my ($storecfg, $conf, $vmid, $drive, $bridges, $arch, $machine_type) = @_;
1873 if ($drive->{interface
} eq 'virtio') {
1874 my $pciaddr = print_pci_addr
("$drive->{interface}$drive->{index}", $bridges, $arch, $machine_type);
1875 $device = "virtio-blk-pci,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}$pciaddr";
1876 $device .= ",iothread=iothread-$drive->{interface}$drive->{index}" if $drive->{iothread
};
1877 } elsif ($drive->{interface
} eq 'scsi') {
1879 my ($maxdev, $controller, $controller_prefix) = scsihw_infos
($conf, $drive);
1880 my $unit = $drive->{index} % $maxdev;
1881 my $devicetype = 'hd';
1883 if (drive_is_cdrom
($drive)) {
1886 if ($drive->{file
} =~ m
|^/|) {
1887 $path = $drive->{file
};
1888 if (my $info = path_is_scsi
($path)) {
1889 if ($info->{type
} == 0 && $drive->{scsiblock
}) {
1890 $devicetype = 'block';
1891 } elsif ($info->{type
} == 1) { # tape
1892 $devicetype = 'generic';
1896 $path = PVE
::Storage
::path
($storecfg, $drive->{file
});
1899 if($path =~ m/^iscsi\:\/\
//){
1900 $devicetype = 'generic';
1904 if (!$conf->{scsihw
} || ($conf->{scsihw
} =~ m/^lsi/)){
1905 $device = "scsi-$devicetype,bus=$controller_prefix$controller.0,scsi-id=$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
1907 $device = "scsi-$devicetype,bus=$controller_prefix$controller.0,channel=0,scsi-id=0,lun=$drive->{index},drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
1910 if ($drive->{ssd
} && ($devicetype eq 'block' || $devicetype eq 'hd')) {
1911 $device .= ",rotation_rate=1";
1913 $device .= ",wwn=$drive->{wwn}" if $drive->{wwn
};
1915 } elsif ($drive->{interface
} eq 'ide' || $drive->{interface
} eq 'sata') {
1916 my $maxdev = ($drive->{interface
} eq 'sata') ?
$MAX_SATA_DISKS : 2;
1917 my $controller = int($drive->{index} / $maxdev);
1918 my $unit = $drive->{index} % $maxdev;
1919 my $devicetype = ($drive->{media
} && $drive->{media
} eq 'cdrom') ?
"cd" : "hd";
1921 $device = "ide-$devicetype";
1922 if ($drive->{interface
} eq 'ide') {
1923 $device .= ",bus=ide.$controller,unit=$unit";
1925 $device .= ",bus=ahci$controller.$unit";
1927 $device .= ",drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
1929 if ($devicetype eq 'hd') {
1930 if (my $model = $drive->{model
}) {
1931 $model = URI
::Escape
::uri_unescape
($model);
1932 $device .= ",model=$model";
1934 if ($drive->{ssd
}) {
1935 $device .= ",rotation_rate=1";
1938 $device .= ",wwn=$drive->{wwn}" if $drive->{wwn
};
1939 } elsif ($drive->{interface
} eq 'usb') {
1941 # -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0
1943 die "unsupported interface type";
1946 $device .= ",bootindex=$drive->{bootindex}" if $drive->{bootindex
};
1948 if (my $serial = $drive->{serial
}) {
1949 $serial = URI
::Escape
::uri_unescape
($serial);
1950 $device .= ",serial=$serial";
1957 sub get_initiator_name
{
1960 my $fh = IO
::File-
>new('/etc/iscsi/initiatorname.iscsi') || return undef;
1961 while (defined(my $line = <$fh>)) {
1962 next if $line !~ m/^\s*InitiatorName\s*=\s*([\.\-:\w]+)/;
1971 sub print_drive_full
{
1972 my ($storecfg, $vmid, $drive) = @_;
1975 my $volid = $drive->{file
};
1978 if (drive_is_cdrom
($drive)) {
1979 $path = get_iso_path
($storecfg, $vmid, $volid);
1981 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
1983 $path = PVE
::Storage
::path
($storecfg, $volid);
1984 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
1985 $format = qemu_img_format
($scfg, $volname);
1993 my @qemu_drive_options = qw(heads secs cyls trans media format cache rerror werror aio discard);
1994 foreach my $o (@qemu_drive_options) {
1995 $opts .= ",$o=$drive->{$o}" if defined($drive->{$o});
1998 # snapshot only accepts on|off
1999 if (defined($drive->{snapshot
})) {
2000 my $v = $drive->{snapshot
} ?
'on' : 'off';
2001 $opts .= ",snapshot=$v";
2004 foreach my $type (['', '-total'], [_rd
=> '-read'], [_wr
=> '-write']) {
2005 my ($dir, $qmpname) = @$type;
2006 if (my $v = $drive->{"mbps$dir"}) {
2007 $opts .= ",throttling.bps$qmpname=".int($v*1024*1024);
2009 if (my $v = $drive->{"mbps${dir}_max"}) {
2010 $opts .= ",throttling.bps$qmpname-max=".int($v*1024*1024);
2012 if (my $v = $drive->{"bps${dir}_max_length"}) {
2013 $opts .= ",throttling.bps$qmpname-max-length=$v";
2015 if (my $v = $drive->{"iops${dir}"}) {
2016 $opts .= ",throttling.iops$qmpname=$v";
2018 if (my $v = $drive->{"iops${dir}_max"}) {
2019 $opts .= ",throttling.iops$qmpname-max=$v";
2021 if (my $v = $drive->{"iops${dir}_max_length"}) {
2022 $opts .= ",throttling.iops$qmpname-max-length=$v";
2026 $opts .= ",format=$format" if $format && !$drive->{format
};
2028 my $cache_direct = 0;
2030 if (my $cache = $drive->{cache
}) {
2031 $cache_direct = $cache =~ /^(?:off|none|directsync)$/;
2032 } elsif (!drive_is_cdrom
($drive)) {
2033 $opts .= ",cache=none";
2037 # aio native works only with O_DIRECT
2038 if (!$drive->{aio
}) {
2040 $opts .= ",aio=native";
2042 $opts .= ",aio=threads";
2046 if (!drive_is_cdrom
($drive)) {
2048 if (defined($drive->{detect_zeroes
}) && !$drive->{detect_zeroes
}) {
2049 $detectzeroes = 'off';
2050 } elsif ($drive->{discard
}) {
2051 $detectzeroes = $drive->{discard
} eq 'on' ?
'unmap' : 'on';
2053 # This used to be our default with discard not being specified:
2054 $detectzeroes = 'on';
2056 $opts .= ",detect-zeroes=$detectzeroes" if $detectzeroes;
2059 my $pathinfo = $path ?
"file=$path," : '';
2061 return "${pathinfo}if=none,id=drive-$drive->{interface}$drive->{index}$opts";
2064 sub print_netdevice_full
{
2065 my ($vmid, $conf, $net, $netid, $bridges, $use_old_bios_files, $arch, $machine_type) = @_;
2067 my $bootorder = $conf->{boot
} || $confdesc->{boot
}->{default};
2069 my $device = $net->{model
};
2070 if ($net->{model
} eq 'virtio') {
2071 $device = 'virtio-net-pci';
2074 my $pciaddr = print_pci_addr
("$netid", $bridges, $arch, $machine_type);
2075 my $tmpstr = "$device,mac=$net->{macaddr},netdev=$netid$pciaddr,id=$netid";
2076 if ($net->{queues
} && $net->{queues
} > 1 && $net->{model
} eq 'virtio'){
2077 #Consider we have N queues, the number of vectors needed is 2*N + 2 (plus one config interrupt and control vq)
2078 my $vectors = $net->{queues
} * 2 + 2;
2079 $tmpstr .= ",vectors=$vectors,mq=on";
2081 $tmpstr .= ",bootindex=$net->{bootindex}" if $net->{bootindex
} ;
2083 if ($use_old_bios_files) {
2085 if ($device eq 'virtio-net-pci') {
2086 $romfile = 'pxe-virtio.rom';
2087 } elsif ($device eq 'e1000') {
2088 $romfile = 'pxe-e1000.rom';
2089 } elsif ($device eq 'ne2k') {
2090 $romfile = 'pxe-ne2k_pci.rom';
2091 } elsif ($device eq 'pcnet') {
2092 $romfile = 'pxe-pcnet.rom';
2093 } elsif ($device eq 'rtl8139') {
2094 $romfile = 'pxe-rtl8139.rom';
2096 $tmpstr .= ",romfile=$romfile" if $romfile;
2102 sub print_netdev_full
{
2103 my ($vmid, $conf, $arch, $net, $netid, $hotplug) = @_;
2106 if ($netid =~ m/^net(\d+)$/) {
2110 die "got strange net id '$i'\n" if $i >= ${MAX_NETS
};
2112 my $ifname = "tap${vmid}i$i";
2114 # kvm uses TUNSETIFF ioctl, and that limits ifname length
2115 die "interface name '$ifname' is too long (max 15 character)\n"
2116 if length($ifname) >= 16;
2118 my $vhostparam = '';
2119 if (is_native
($arch)) {
2120 $vhostparam = ',vhost=on' if kernel_has_vhost_net
() && $net->{model
} eq 'virtio';
2123 my $vmname = $conf->{name
} || "vm$vmid";
2126 my $script = $hotplug ?
"pve-bridge-hotplug" : "pve-bridge";
2128 if ($net->{bridge
}) {
2129 $netdev = "type=tap,id=$netid,ifname=${ifname},script=/var/lib/qemu-server/$script,downscript=/var/lib/qemu-server/pve-bridgedown$vhostparam";
2131 $netdev = "type=user,id=$netid,hostname=$vmname";
2134 $netdev .= ",queues=$net->{queues}" if ($net->{queues
} && $net->{model
} eq 'virtio');
2140 sub print_cpu_device
{
2141 my ($conf, $id) = @_;
2143 my $kvm = $conf->{kvm
} // 1;
2144 my $cpu = $kvm ?
"kvm64" : "qemu64";
2145 if (my $cputype = $conf->{cpu
}) {
2146 my $cpuconf = PVE
::JSONSchema
::parse_property_string
($cpu_fmt, $cputype)
2147 or die "Cannot parse cpu description: $cputype\n";
2148 $cpu = $cpuconf->{cputype
};
2151 my $cores = $conf->{cores
} || 1;
2153 my $current_core = ($id - 1) % $cores;
2154 my $current_socket = int(($id - 1 - $current_core)/$cores);
2156 return "$cpu-x86_64-cpu,id=cpu$id,socket-id=$current_socket,core-id=$current_core,thread-id=0";
2160 'cirrus' => 'cirrus-vga',
2162 'vmware' => 'vmware-svga',
2163 'virtio' => 'virtio-vga',
2166 sub print_vga_device
{
2167 my ($conf, $vga, $arch, $machine, $id, $qxlnum, $bridges) = @_;
2169 my $type = $vga_map->{$vga->{type
}};
2170 if ($arch eq 'aarch64' && defined($type) && $type eq 'virtio-vga') {
2171 $type = 'virtio-gpu';
2173 my $vgamem_mb = $vga->{memory
};
2175 $type = $id ?
'qxl' : 'qxl-vga';
2177 die "no devicetype for $vga->{type}\n" if !$type;
2181 if ($vga->{type
} eq 'virtio') {
2182 my $bytes = PVE
::Tools
::convert_size
($vgamem_mb, "mb" => "b");
2183 $memory = ",max_hostmem=$bytes";
2185 # from https://www.spice-space.org/multiple-monitors.html
2186 $memory = ",vgamem_mb=$vga->{memory}";
2187 my $ram = $vgamem_mb * 4;
2188 my $vram = $vgamem_mb * 2;
2189 $memory .= ",ram_size_mb=$ram,vram_size_mb=$vram";
2191 $memory = ",vgamem_mb=$vga->{memory}";
2193 } elsif ($qxlnum && $id) {
2194 $memory = ",ram_size=67108864,vram_size=33554432";
2197 my $q35 = machine_type_is_q35
($conf);
2198 my $vgaid = "vga" . ($id // '');
2201 if ($q35 && $vgaid eq 'vga') {
2202 # the first display uses pcie.0 bus on q35 machines
2203 $pciaddr = print_pcie_addr
($vgaid, $bridges, $arch, $machine);
2205 $pciaddr = print_pci_addr
($vgaid, $bridges, $arch, $machine);
2208 return "$type,id=${vgaid}${memory}${pciaddr}";
2211 sub drive_is_cloudinit
{
2213 return $drive->{file
} =~ m
@[:/]vm-\d
+-cloudinit
(?
:\
.$QEMU_FORMAT_RE)?
$@;
2216 sub drive_is_cdrom
{
2217 my ($drive, $exclude_cloudinit) = @_;
2219 return 0 if $exclude_cloudinit && drive_is_cloudinit
($drive);
2221 return $drive && $drive->{media
} && ($drive->{media
} eq 'cdrom');
2225 sub parse_number_sets
{
2228 foreach my $part (split(/;/, $set)) {
2229 if ($part =~ /^\s*(\d+)(?:-(\d+))?\s*$/) {
2230 die "invalid range: $part ($2 < $1)\n" if defined($2) && $2 < $1;
2231 push @$res, [ $1, $2 ];
2233 die "invalid range: $part\n";
2242 my $res = PVE
::JSONSchema
::parse_property_string
($numa_fmt, $data);
2243 $res->{cpus
} = parse_number_sets
($res->{cpus
}) if defined($res->{cpus
});
2244 $res->{hostnodes
} = parse_number_sets
($res->{hostnodes
}) if defined($res->{hostnodes
});
2251 return undef if !$value;
2253 my $res = PVE
::JSONSchema
::parse_property_string
($hostpci_fmt, $value);
2255 my @idlist = split(/;/, $res->{host
});
2256 delete $res->{host
};
2257 foreach my $id (@idlist) {
2258 if ($id =~ m/\./) { # full id 00:00.1
2259 push @{$res->{pciid
}}, {
2262 } else { # partial id 00:00
2263 $res->{pciid
} = PVE
::SysFSTools
::lspci
($id);
2269 # netX: e1000=XX:XX:XX:XX:XX:XX,bridge=vmbr0,rate=<mbps>
2273 my $res = eval { PVE
::JSONSchema
::parse_property_string
($net_fmt, $data) };
2278 if (!defined($res->{macaddr
})) {
2279 my $dc = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
2280 $res->{macaddr
} = PVE
::Tools
::random_ether_addr
($dc->{mac_prefix
});
2285 # ipconfigX ip=cidr,gw=ip,ip6=cidr,gw6=ip
2286 sub parse_ipconfig
{
2289 my $res = eval { PVE
::JSONSchema
::parse_property_string
($ipconfig_fmt, $data) };
2295 if ($res->{gw
} && !$res->{ip
}) {
2296 warn 'gateway specified without specifying an IP address';
2299 if ($res->{gw6
} && !$res->{ip6
}) {
2300 warn 'IPv6 gateway specified without specifying an IPv6 address';
2303 if ($res->{gw
} && $res->{ip
} eq 'dhcp') {
2304 warn 'gateway specified together with DHCP';
2307 if ($res->{gw6
} && $res->{ip6
} !~ /^$IPV6RE/) {
2309 warn "IPv6 gateway specified together with $res->{ip6} address";
2313 if (!$res->{ip
} && !$res->{ip6
}) {
2314 return { ip
=> 'dhcp', ip6
=> 'dhcp' };
2323 return PVE
::JSONSchema
::print_property_string
($net, $net_fmt);
2326 sub add_random_macs
{
2327 my ($settings) = @_;
2329 foreach my $opt (keys %$settings) {
2330 next if $opt !~ m/^net(\d+)$/;
2331 my $net = parse_net
($settings->{$opt});
2333 $settings->{$opt} = print_net
($net);
2337 sub vm_is_volid_owner
{
2338 my ($storecfg, $vmid, $volid) = @_;
2340 if ($volid !~ m
|^/|) {
2342 eval { ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid); };
2343 if ($owner && ($owner == $vmid)) {
2351 sub split_flagged_list
{
2352 my $text = shift || '';
2353 $text =~ s/[,;]/ /g;
2355 return { map { /^(!?)(.*)$/ && ($2, $1) } ($text =~ /\S+/g) };
2358 sub join_flagged_list
{
2359 my ($how, $lst) = @_;
2360 join $how, map { $lst->{$_} . $_ } keys %$lst;
2363 sub vmconfig_delete_pending_option
{
2364 my ($conf, $key, $force) = @_;
2366 delete $conf->{pending
}->{$key};
2367 my $pending_delete_hash = split_flagged_list
($conf->{pending
}->{delete});
2368 $pending_delete_hash->{$key} = $force ?
'!' : '';
2369 $conf->{pending
}->{delete} = join_flagged_list
(',', $pending_delete_hash);
2372 sub vmconfig_undelete_pending_option
{
2373 my ($conf, $key) = @_;
2375 my $pending_delete_hash = split_flagged_list
($conf->{pending
}->{delete});
2376 delete $pending_delete_hash->{$key};
2378 if (%$pending_delete_hash) {
2379 $conf->{pending
}->{delete} = join_flagged_list
(',', $pending_delete_hash);
2381 delete $conf->{pending
}->{delete};
2385 sub vmconfig_register_unused_drive
{
2386 my ($storecfg, $vmid, $conf, $drive) = @_;
2388 if (drive_is_cloudinit
($drive)) {
2389 eval { PVE
::Storage
::vdisk_free
($storecfg, $drive->{file
}) };
2391 } elsif (!drive_is_cdrom
($drive)) {
2392 my $volid = $drive->{file
};
2393 if (vm_is_volid_owner
($storecfg, $vmid, $volid)) {
2394 PVE
::QemuConfig-
>add_unused_volume($conf, $volid, $vmid);
2399 sub vmconfig_cleanup_pending
{
2402 # remove pending changes when nothing changed
2404 foreach my $opt (keys %{$conf->{pending
}}) {
2405 if (defined($conf->{$opt}) && ($conf->{pending
}->{$opt} eq $conf->{$opt})) {
2407 delete $conf->{pending
}->{$opt};
2411 my $current_delete_hash = split_flagged_list
($conf->{pending
}->{delete});
2412 my $pending_delete_hash = {};
2413 while (my ($opt, $force) = each %$current_delete_hash) {
2414 if (defined($conf->{$opt})) {
2415 $pending_delete_hash->{$opt} = $force;
2421 if (%$pending_delete_hash) {
2422 $conf->{pending
}->{delete} = join_flagged_list
(',', $pending_delete_hash);
2424 delete $conf->{pending
}->{delete};
2430 # smbios: [manufacturer=str][,product=str][,version=str][,serial=str][,uuid=uuid][,sku=str][,family=str][,base64=bool]
2434 pattern
=> '[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}',
2435 format_description
=> 'UUID',
2436 description
=> "Set SMBIOS1 UUID.",
2441 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2442 format_description
=> 'Base64 encoded string',
2443 description
=> "Set SMBIOS1 version.",
2448 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2449 format_description
=> 'Base64 encoded string',
2450 description
=> "Set SMBIOS1 serial number.",
2455 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2456 format_description
=> 'Base64 encoded string',
2457 description
=> "Set SMBIOS1 manufacturer.",
2462 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2463 format_description
=> 'Base64 encoded string',
2464 description
=> "Set SMBIOS1 product ID.",
2469 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2470 format_description
=> 'Base64 encoded string',
2471 description
=> "Set SMBIOS1 SKU string.",
2476 pattern
=> '[A-Za-z0-9+\/]+={0,2}',
2477 format_description
=> 'Base64 encoded string',
2478 description
=> "Set SMBIOS1 family string.",
2483 description
=> 'Flag to indicate that the SMBIOS values are base64 encoded',
2491 my $res = eval { PVE
::JSONSchema
::parse_property_string
($smbios1_fmt, $data) };
2498 return PVE
::JSONSchema
::print_property_string
($smbios1, $smbios1_fmt);
2501 PVE
::JSONSchema
::register_format
('pve-qm-smbios1', $smbios1_fmt);
2503 PVE
::JSONSchema
::register_format
('pve-qm-bootdisk', \
&verify_bootdisk
);
2504 sub verify_bootdisk
{
2505 my ($value, $noerr) = @_;
2507 return $value if is_valid_drivename
($value);
2509 return undef if $noerr;
2511 die "invalid boot disk '$value'\n";
2514 sub parse_watchdog
{
2517 return undef if !$value;
2519 my $res = eval { PVE
::JSONSchema
::parse_property_string
($watchdog_fmt, $value) };
2524 sub parse_guest_agent
{
2527 return {} if !defined($value->{agent
});
2529 my $res = eval { PVE
::JSONSchema
::parse_property_string
($agent_fmt, $value->{agent
}) };
2532 # if the agent is disabled ignore the other potentially set properties
2533 return {} if !$res->{enabled
};
2540 return {} if !$value;
2541 my $res = eval { PVE
::JSONSchema
::parse_property_string
($vga_fmt, $value) };
2546 PVE
::JSONSchema
::register_format
('pve-qm-usb-device', \
&verify_usb_device
);
2547 sub verify_usb_device
{
2548 my ($value, $noerr) = @_;
2550 return $value if parse_usb_device
($value);
2552 return undef if $noerr;
2554 die "unable to parse usb device\n";
2557 # add JSON properties for create and set function
2558 sub json_config_properties
{
2561 foreach my $opt (keys %$confdesc) {
2562 next if $opt eq 'parent' || $opt eq 'snaptime' || $opt eq 'vmstate' || $opt eq 'runningmachine';
2563 $prop->{$opt} = $confdesc->{$opt};
2569 # return copy of $confdesc_cloudinit to generate documentation
2570 sub cloudinit_config_properties
{
2572 return dclone
($confdesc_cloudinit);
2576 my ($key, $value) = @_;
2578 die "unknown setting '$key'\n" if !$confdesc->{$key};
2580 my $type = $confdesc->{$key}->{type
};
2582 if (!defined($value)) {
2583 die "got undefined value\n";
2586 if ($value =~ m/[\n\r]/) {
2587 die "property contains a line feed\n";
2590 if ($type eq 'boolean') {
2591 return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
2592 return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
2593 die "type check ('boolean') failed - got '$value'\n";
2594 } elsif ($type eq 'integer') {
2595 return int($1) if $value =~ m/^(\d+)$/;
2596 die "type check ('integer') failed - got '$value'\n";
2597 } elsif ($type eq 'number') {
2598 return $value if $value =~ m/^(\d+)(\.\d+)?$/;
2599 die "type check ('number') failed - got '$value'\n";
2600 } elsif ($type eq 'string') {
2601 if (my $fmt = $confdesc->{$key}->{format
}) {
2602 PVE
::JSONSchema
::check_format
($fmt, $value);
2605 $value =~ s/^\"(.*)\"$/$1/;
2608 die "internal error"
2615 my $conf = PVE
::QemuConfig-
>config_file($vmid);
2616 utime undef, undef, $conf;
2620 my ($storecfg, $vmid, $keep_empty_config, $skiplock) = @_;
2622 my $conf = PVE
::QemuConfig-
>load_config($vmid);
2624 PVE
::QemuConfig-
>check_lock($conf) if !$skiplock;
2626 if ($conf->{template
}) {
2627 # check if any base image is still used by a linked clone
2628 foreach_drive
($conf, sub {
2629 my ($ds, $drive) = @_;
2631 return if drive_is_cdrom
($drive);
2633 my $volid = $drive->{file
};
2635 return if !$volid || $volid =~ m
|^/|;
2637 die "base volume '$volid' is still in use by linked cloned\n"
2638 if PVE
::Storage
::volume_is_base_and_used
($storecfg, $volid);
2643 # only remove disks owned by this VM
2644 foreach_drive
($conf, sub {
2645 my ($ds, $drive) = @_;
2647 return if drive_is_cdrom
($drive, 1);
2649 my $volid = $drive->{file
};
2651 return if !$volid || $volid =~ m
|^/|;
2653 my ($path, $owner) = PVE
::Storage
::path
($storecfg, $volid);
2654 return if !$path || !$owner || ($owner != $vmid);
2657 PVE
::Storage
::vdisk_free
($storecfg, $volid);
2659 warn "Could not remove disk '$volid', check manually: $@" if $@;
2663 if ($keep_empty_config) {
2664 PVE
::QemuConfig-
>write_config($vmid, "memory: 128\n");
2666 PVE
::QemuConfig-
>destroy_config($vmid);
2669 # also remove unused disk
2671 my $dl = PVE
::Storage
::vdisk_list
($storecfg, undef, $vmid);
2674 PVE
::Storage
::foreach_volid
($dl, sub {
2675 my ($volid, $sid, $volname, $d) = @_;
2676 PVE
::Storage
::vdisk_free
($storecfg, $volid);
2685 sub parse_vm_config
{
2686 my ($filename, $raw) = @_;
2688 return undef if !defined($raw);
2691 digest
=> Digest
::SHA
::sha1_hex
($raw),
2696 $filename =~ m
|/qemu-server/(\d
+)\
.conf
$|
2697 || die "got strange filename '$filename'";
2705 my @lines = split(/\n/, $raw);
2706 foreach my $line (@lines) {
2707 next if $line =~ m/^\s*$/;
2709 if ($line =~ m/^\[PENDING\]\s*$/i) {
2710 $section = 'pending';
2711 if (defined($descr)) {
2713 $conf->{description
} = $descr;
2716 $conf = $res->{$section} = {};
2719 } elsif ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
2721 if (defined($descr)) {
2723 $conf->{description
} = $descr;
2726 $conf = $res->{snapshots
}->{$section} = {};
2730 if ($line =~ m/^\#(.*)\s*$/) {
2731 $descr = '' if !defined($descr);
2732 $descr .= PVE
::Tools
::decode_text
($1) . "\n";
2736 if ($line =~ m/^(description):\s*(.*\S)\s*$/) {
2737 $descr = '' if !defined($descr);
2738 $descr .= PVE
::Tools
::decode_text
($2);
2739 } elsif ($line =~ m/snapstate:\s*(prepare|delete)\s*$/) {
2740 $conf->{snapstate
} = $1;
2741 } elsif ($line =~ m/^(args):\s*(.*\S)\s*$/) {
2744 $conf->{$key} = $value;
2745 } elsif ($line =~ m/^delete:\s*(.*\S)\s*$/) {
2747 if ($section eq 'pending') {
2748 $conf->{delete} = $value; # we parse this later
2750 warn "vm $vmid - propertry 'delete' is only allowed in [PENDING]\n";
2752 } elsif ($line =~ m/^([a-z][a-z_]*\d*):\s*(.+?)\s*$/) {
2755 eval { $value = check_type
($key, $value); };
2757 warn "vm $vmid - unable to parse value of '$key' - $@";
2759 $key = 'ide2' if $key eq 'cdrom';
2760 my $fmt = $confdesc->{$key}->{format
};
2761 if ($fmt && $fmt =~ /^pve-qm-(?:ide|scsi|virtio|sata)$/) {
2762 my $v = parse_drive
($key, $value);
2763 if (my $volid = filename_to_volume_id
($vmid, $v->{file
}, $v->{media
})) {
2764 $v->{file
} = $volid;
2765 $value = print_drive
($vmid, $v);
2767 warn "vm $vmid - unable to parse value of '$key'\n";
2772 $conf->{$key} = $value;
2777 if (defined($descr)) {
2779 $conf->{description
} = $descr;
2781 delete $res->{snapstate
}; # just to be sure
2786 sub write_vm_config
{
2787 my ($filename, $conf) = @_;
2789 delete $conf->{snapstate
}; # just to be sure
2791 if ($conf->{cdrom
}) {
2792 die "option ide2 conflicts with cdrom\n" if $conf->{ide2
};
2793 $conf->{ide2
} = $conf->{cdrom
};
2794 delete $conf->{cdrom
};
2797 # we do not use 'smp' any longer
2798 if ($conf->{sockets
}) {
2799 delete $conf->{smp
};
2800 } elsif ($conf->{smp
}) {
2801 $conf->{sockets
} = $conf->{smp
};
2802 delete $conf->{cores
};
2803 delete $conf->{smp
};
2806 my $used_volids = {};
2808 my $cleanup_config = sub {
2809 my ($cref, $pending, $snapname) = @_;
2811 foreach my $key (keys %$cref) {
2812 next if $key eq 'digest' || $key eq 'description' || $key eq 'snapshots' ||
2813 $key eq 'snapstate' || $key eq 'pending';
2814 my $value = $cref->{$key};
2815 if ($key eq 'delete') {
2816 die "propertry 'delete' is only allowed in [PENDING]\n"
2818 # fixme: check syntax?
2821 eval { $value = check_type
($key, $value); };
2822 die "unable to parse value of '$key' - $@" if $@;
2824 $cref->{$key} = $value;
2826 if (!$snapname && is_valid_drivename
($key)) {
2827 my $drive = parse_drive
($key, $value);
2828 $used_volids->{$drive->{file
}} = 1 if $drive && $drive->{file
};
2833 &$cleanup_config($conf);
2835 &$cleanup_config($conf->{pending
}, 1);
2837 foreach my $snapname (keys %{$conf->{snapshots
}}) {
2838 die "internal error" if $snapname eq 'pending';
2839 &$cleanup_config($conf->{snapshots
}->{$snapname}, undef, $snapname);
2842 # remove 'unusedX' settings if we re-add a volume
2843 foreach my $key (keys %$conf) {
2844 my $value = $conf->{$key};
2845 if ($key =~ m/^unused/ && $used_volids->{$value}) {
2846 delete $conf->{$key};
2850 my $generate_raw_config = sub {
2851 my ($conf, $pending) = @_;
2855 # add description as comment to top of file
2856 if (defined(my $descr = $conf->{description
})) {
2858 foreach my $cl (split(/\n/, $descr)) {
2859 $raw .= '#' . PVE
::Tools
::encode_text
($cl) . "\n";
2862 $raw .= "#\n" if $pending;
2866 foreach my $key (sort keys %$conf) {
2867 next if $key eq 'digest' || $key eq 'description' || $key eq 'pending' || $key eq 'snapshots';
2868 $raw .= "$key: $conf->{$key}\n";
2873 my $raw = &$generate_raw_config($conf);
2875 if (scalar(keys %{$conf->{pending
}})){
2876 $raw .= "\n[PENDING]\n";
2877 $raw .= &$generate_raw_config($conf->{pending
}, 1);
2880 foreach my $snapname (sort keys %{$conf->{snapshots
}}) {
2881 $raw .= "\n[$snapname]\n";
2882 $raw .= &$generate_raw_config($conf->{snapshots
}->{$snapname});
2892 # we use static defaults from our JSON schema configuration
2893 foreach my $key (keys %$confdesc) {
2894 if (defined(my $default = $confdesc->{$key}->{default})) {
2895 $res->{$key} = $default;
2903 my $vmlist = PVE
::Cluster
::get_vmlist
();
2905 return $res if !$vmlist || !$vmlist->{ids
};
2906 my $ids = $vmlist->{ids
};
2908 foreach my $vmid (keys %$ids) {
2909 my $d = $ids->{$vmid};
2910 next if !$d->{node
} || $d->{node
} ne $nodename;
2911 next if !$d->{type
} || $d->{type
} ne 'qemu';
2912 $res->{$vmid}->{exists} = 1;
2917 # test if VM uses local resources (to prevent migration)
2918 sub check_local_resources
{
2919 my ($conf, $noerr) = @_;
2923 push @loc_res, "hostusb" if $conf->{hostusb
}; # old syntax
2924 push @loc_res, "hostpci" if $conf->{hostpci
}; # old syntax
2926 push @loc_res, "ivshmem" if $conf->{ivshmem
};
2928 foreach my $k (keys %$conf) {
2929 next if $k =~ m/^usb/ && ($conf->{$k} =~ m/^spice(?![^,])/);
2930 # sockets are safe: they will recreated be on the target side post-migrate
2931 next if $k =~ m/^serial/ && ($conf->{$k} eq 'socket');
2932 push @loc_res, $k if $k =~ m/^(usb|hostpci|serial|parallel)\d+$/;
2935 die "VM uses local resources\n" if scalar @loc_res && !$noerr;
2940 # check if used storages are available on all nodes (use by migrate)
2941 sub check_storage_availability
{
2942 my ($storecfg, $conf, $node) = @_;
2944 foreach_drive
($conf, sub {
2945 my ($ds, $drive) = @_;
2947 my $volid = $drive->{file
};
2950 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
2953 # check if storage is available on both nodes
2954 my $scfg = PVE
::Storage
::storage_check_node
($storecfg, $sid);
2955 PVE
::Storage
::storage_check_node
($storecfg, $sid, $node);
2959 # list nodes where all VM images are available (used by has_feature API)
2961 my ($conf, $storecfg) = @_;
2963 my $nodelist = PVE
::Cluster
::get_nodelist
();
2964 my $nodehash = { map { $_ => 1 } @$nodelist };
2965 my $nodename = PVE
::INotify
::nodename
();
2967 foreach_drive
($conf, sub {
2968 my ($ds, $drive) = @_;
2970 my $volid = $drive->{file
};
2973 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
2975 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
2976 if ($scfg->{disable
}) {
2978 } elsif (my $avail = $scfg->{nodes
}) {
2979 foreach my $node (keys %$nodehash) {
2980 delete $nodehash->{$node} if !$avail->{$node};
2982 } elsif (!$scfg->{shared
}) {
2983 foreach my $node (keys %$nodehash) {
2984 delete $nodehash->{$node} if $node ne $nodename
2993 sub check_local_storage_availability
{
2994 my ($conf, $storecfg) = @_;
2996 my $nodelist = PVE
::Cluster
::get_nodelist
();
2997 my $nodehash = { map { $_ => {} } @$nodelist };
2999 foreach_drive
($conf, sub {
3000 my ($ds, $drive) = @_;
3002 my $volid = $drive->{file
};
3005 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
3007 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
3009 if ($scfg->{disable
}) {
3010 foreach my $node (keys %$nodehash) {
3011 $nodehash->{$node}->{unavailable_storages
}->{$storeid} = 1;
3013 } elsif (my $avail = $scfg->{nodes
}) {
3014 foreach my $node (keys %$nodehash) {
3015 if (!$avail->{$node}) {
3016 $nodehash->{$node}->{unavailable_storages
}->{$storeid} = 1;
3023 foreach my $node (values %$nodehash) {
3024 if (my $unavail = $node->{unavailable_storages
}) {
3025 $node->{unavailable_storages
} = [ sort keys %$unavail ];
3033 my ($pidfile, $pid) = @_;
3035 my $fh = IO
::File-
>new("/proc/$pid/cmdline", "r");
3039 return undef if !$line;
3040 my @param = split(/\0/, $line);
3042 my $cmd = $param[0];
3043 return if !$cmd || ($cmd !~ m
|kvm
$| && $cmd !~ m
@(?
:^|/)qemu-system-[^/]+$@);
3045 for (my $i = 0; $i < scalar (@param); $i++) {
3048 if (($p eq '-pidfile') || ($p eq '--pidfile')) {
3049 my $p = $param[$i+1];
3050 return 1 if $p && ($p eq $pidfile);
3059 my ($vmid, $nocheck, $node) = @_;
3061 my $filename = PVE
::QemuConfig-
>config_file($vmid, $node);
3063 die "unable to find configuration file for VM $vmid - no such machine\n"
3064 if !$nocheck && ! -f
$filename;
3066 my $pidfile = pidfile_name
($vmid);
3068 if (my $fd = IO
::File-
>new("<$pidfile")) {
3073 my $mtime = $st->mtime;
3074 if ($mtime > time()) {
3075 warn "file '$filename' modified in future\n";
3078 if ($line =~ m/^(\d+)$/) {
3080 if (check_cmdline
($pidfile, $pid)) {
3081 if (my $pinfo = PVE
::ProcFSTools
::check_process_running
($pid)) {
3093 my $vzlist = config_list
();
3095 my $fd = IO
::Dir-
>new($var_run_tmpdir) || return $vzlist;
3097 while (defined(my $de = $fd->read)) {
3098 next if $de !~ m/^(\d+)\.pid$/;
3100 next if !defined($vzlist->{$vmid});
3101 if (my $pid = check_running
($vmid)) {
3102 $vzlist->{$vmid}->{pid
} = $pid;
3110 my ($storecfg, $conf) = @_;
3112 my $bootdisk = $conf->{bootdisk
};
3113 return undef if !$bootdisk;
3114 return undef if !is_valid_drivename
($bootdisk);
3116 return undef if !$conf->{$bootdisk};
3118 my $drive = parse_drive
($bootdisk, $conf->{$bootdisk});
3119 return undef if !defined($drive);
3121 return undef if drive_is_cdrom
($drive);
3123 my $volid = $drive->{file
};
3124 return undef if !$volid;
3126 return $drive->{size
};
3129 our $vmstatus_return_properties = {
3130 vmid
=> get_standard_option
('pve-vmid'),
3132 description
=> "Qemu process status.",
3134 enum
=> ['stopped', 'running'],
3137 description
=> "Maximum memory in bytes.",
3140 renderer
=> 'bytes',
3143 description
=> "Root disk size in bytes.",
3146 renderer
=> 'bytes',
3149 description
=> "VM name.",
3154 description
=> "Qemu QMP agent status.",
3159 description
=> "PID of running qemu process.",
3164 description
=> "Uptime.",
3167 renderer
=> 'duration',
3170 description
=> "Maximum usable CPUs.",
3175 description
=> "The current config lock, if any.",
3181 my $last_proc_pid_stat;
3183 # get VM status information
3184 # This must be fast and should not block ($full == false)
3185 # We only query KVM using QMP if $full == true (this can be slow)
3187 my ($opt_vmid, $full) = @_;
3191 my $storecfg = PVE
::Storage
::config
();
3193 my $list = vzlist
();
3194 my $defaults = load_defaults
();
3196 my ($uptime) = PVE
::ProcFSTools
::read_proc_uptime
(1);
3198 my $cpucount = $cpuinfo->{cpus
} || 1;
3200 foreach my $vmid (keys %$list) {
3201 next if $opt_vmid && ($vmid ne $opt_vmid);
3203 my $cfspath = PVE
::QemuConfig-
>cfs_config_path($vmid);
3204 my $conf = PVE
::Cluster
::cfs_read_file
($cfspath) || {};
3206 my $d = { vmid
=> $vmid };
3207 $d->{pid
} = $list->{$vmid}->{pid
};
3209 # fixme: better status?
3210 $d->{status
} = $list->{$vmid}->{pid
} ?
'running' : 'stopped';
3212 my $size = disksize
($storecfg, $conf);
3213 if (defined($size)) {
3214 $d->{disk
} = 0; # no info available
3215 $d->{maxdisk
} = $size;
3221 $d->{cpus
} = ($conf->{sockets
} || $defaults->{sockets
})
3222 * ($conf->{cores
} || $defaults->{cores
});
3223 $d->{cpus
} = $cpucount if $d->{cpus
} > $cpucount;
3224 $d->{cpus
} = $conf->{vcpus
} if $conf->{vcpus
};
3226 $d->{name
} = $conf->{name
} || "VM $vmid";
3227 $d->{maxmem
} = $conf->{memory
} ?
$conf->{memory
}*(1024*1024)
3228 : $defaults->{memory
}*(1024*1024);
3230 if ($conf->{balloon
}) {
3231 $d->{balloon_min
} = $conf->{balloon
}*(1024*1024);
3232 $d->{shares
} = defined($conf->{shares
}) ?
$conf->{shares
}
3233 : $defaults->{shares
};
3244 $d->{diskwrite
} = 0;
3246 $d->{template
} = PVE
::QemuConfig-
>is_template($conf);
3248 $d->{serial
} = 1 if conf_has_serial
($conf);
3249 $d->{lock} = $conf->{lock} if $conf->{lock};
3254 my $netdev = PVE
::ProcFSTools
::read_proc_net_dev
();
3255 foreach my $dev (keys %$netdev) {
3256 next if $dev !~ m/^tap([1-9]\d*)i/;
3258 my $d = $res->{$vmid};
3261 $d->{netout
} += $netdev->{$dev}->{receive
};
3262 $d->{netin
} += $netdev->{$dev}->{transmit
};
3265 $d->{nics
}->{$dev}->{netout
} = $netdev->{$dev}->{receive
};
3266 $d->{nics
}->{$dev}->{netin
} = $netdev->{$dev}->{transmit
};
3271 my $ctime = gettimeofday
;
3273 foreach my $vmid (keys %$list) {
3275 my $d = $res->{$vmid};
3276 my $pid = $d->{pid
};
3279 my $pstat = PVE
::ProcFSTools
::read_proc_pid_stat
($pid);
3280 next if !$pstat; # not running
3282 my $used = $pstat->{utime} + $pstat->{stime
};
3284 $d->{uptime
} = int(($uptime - $pstat->{starttime
})/$cpuinfo->{user_hz
});
3286 if ($pstat->{vsize
}) {
3287 $d->{mem
} = int(($pstat->{rss
}/$pstat->{vsize
})*$d->{maxmem
});
3290 my $old = $last_proc_pid_stat->{$pid};
3292 $last_proc_pid_stat->{$pid} = {
3300 my $dtime = ($ctime - $old->{time}) * $cpucount * $cpuinfo->{user_hz
};
3302 if ($dtime > 1000) {
3303 my $dutime = $used - $old->{used
};
3305 $d->{cpu
} = (($dutime/$dtime)* $cpucount) / $d->{cpus
};
3306 $last_proc_pid_stat->{$pid} = {
3312 $d->{cpu
} = $old->{cpu
};
3316 return $res if !$full;
3318 my $qmpclient = PVE
::QMPClient-
>new();
3320 my $ballooncb = sub {
3321 my ($vmid, $resp) = @_;
3323 my $info = $resp->{'return'};
3324 return if !$info->{max_mem
};
3326 my $d = $res->{$vmid};
3328 # use memory assigned to VM
3329 $d->{maxmem
} = $info->{max_mem
};
3330 $d->{balloon
} = $info->{actual
};
3332 if (defined($info->{total_mem
}) && defined($info->{free_mem
})) {
3333 $d->{mem
} = $info->{total_mem
} - $info->{free_mem
};
3334 $d->{freemem
} = $info->{free_mem
};
3337 $d->{ballooninfo
} = $info;
3340 my $blockstatscb = sub {
3341 my ($vmid, $resp) = @_;
3342 my $data = $resp->{'return'} || [];
3343 my $totalrdbytes = 0;
3344 my $totalwrbytes = 0;
3346 for my $blockstat (@$data) {
3347 $totalrdbytes = $totalrdbytes + $blockstat->{stats
}->{rd_bytes
};
3348 $totalwrbytes = $totalwrbytes + $blockstat->{stats
}->{wr_bytes
};
3350 $blockstat->{device
} =~ s/drive-//;
3351 $res->{$vmid}->{blockstat
}->{$blockstat->{device
}} = $blockstat->{stats
};
3353 $res->{$vmid}->{diskread
} = $totalrdbytes;
3354 $res->{$vmid}->{diskwrite
} = $totalwrbytes;
3357 my $statuscb = sub {
3358 my ($vmid, $resp) = @_;
3360 $qmpclient->queue_cmd($vmid, $blockstatscb, 'query-blockstats');
3361 # this fails if ballon driver is not loaded, so this must be
3362 # the last commnand (following command are aborted if this fails).
3363 $qmpclient->queue_cmd($vmid, $ballooncb, 'query-balloon');
3365 my $status = 'unknown';
3366 if (!defined($status = $resp->{'return'}->{status
})) {
3367 warn "unable to get VM status\n";
3371 $res->{$vmid}->{qmpstatus
} = $resp->{'return'}->{status
};
3374 foreach my $vmid (keys %$list) {
3375 next if $opt_vmid && ($vmid ne $opt_vmid);
3376 next if !$res->{$vmid}->{pid
}; # not running
3377 $qmpclient->queue_cmd($vmid, $statuscb, 'query-status');
3380 $qmpclient->queue_execute(undef, 2);
3382 foreach my $vmid (keys %$list) {
3383 next if $opt_vmid && ($vmid ne $opt_vmid);
3384 $res->{$vmid}->{qmpstatus
} = $res->{$vmid}->{status
} if !$res->{$vmid}->{qmpstatus
};
3391 my ($conf, $func, @param) = @_;
3393 foreach my $ds (valid_drive_names
()) {
3394 next if !defined($conf->{$ds});
3396 my $drive = parse_drive
($ds, $conf->{$ds});
3399 &$func($ds, $drive, @param);
3404 my ($conf, $func, @param) = @_;
3408 my $test_volid = sub {
3409 my ($volid, $is_cdrom, $replicate, $shared, $snapname, $size) = @_;
3413 $volhash->{$volid}->{cdrom
} //= 1;
3414 $volhash->{$volid}->{cdrom
} = 0 if !$is_cdrom;
3416 $volhash->{$volid}->{replicate
} //= 0;
3417 $volhash->{$volid}->{replicate
} = 1 if $replicate;
3419 $volhash->{$volid}->{shared
} //= 0;
3420 $volhash->{$volid}->{shared
} = 1 if $shared;
3422 $volhash->{$volid}->{referenced_in_config
} //= 0;
3423 $volhash->{$volid}->{referenced_in_config
} = 1 if !defined($snapname);
3425 $volhash->{$volid}->{referenced_in_snapshot
}->{$snapname} = 1
3426 if defined($snapname);
3427 $volhash->{$volid}->{size
} = $size if $size;
3430 foreach_drive
($conf, sub {
3431 my ($ds, $drive) = @_;
3432 $test_volid->($drive->{file
}, drive_is_cdrom
($drive), $drive->{replicate
} // 1, $drive->{shared
}, undef, $drive->{size
});
3435 foreach my $snapname (keys %{$conf->{snapshots
}}) {
3436 my $snap = $conf->{snapshots
}->{$snapname};
3437 $test_volid->($snap->{vmstate
}, 0, 1, $snapname);
3438 foreach_drive
($snap, sub {
3439 my ($ds, $drive) = @_;
3440 $test_volid->($drive->{file
}, drive_is_cdrom
($drive), $drive->{replicate
} // 1, $drive->{shared
}, $snapname);
3444 foreach my $volid (keys %$volhash) {
3445 &$func($volid, $volhash->{$volid}, @param);
3449 sub conf_has_serial
{
3452 for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
3453 if ($conf->{"serial$i"}) {
3461 sub conf_has_audio
{
3462 my ($conf, $id) = @_;
3465 my $audio = $conf->{"audio$id"};
3466 return undef if !defined($audio);
3468 my $audioproperties = PVE
::JSONSchema
::parse_property_string
($audio_fmt, $audio);
3469 my $audiodriver = $audioproperties->{driver
} // 'spice';
3472 dev
=> $audioproperties->{device
},
3473 dev_id
=> "audiodev$id",
3474 backend
=> $audiodriver,
3475 backend_id
=> "$audiodriver-backend${id}",
3479 sub vga_conf_has_spice
{
3482 my $vgaconf = parse_vga
($vga);
3483 my $vgatype = $vgaconf->{type
};
3484 return 0 if !$vgatype || $vgatype !~ m/^qxl([234])?$/;
3489 my $host_arch; # FIXME: fix PVE::Tools::get_host_arch
3490 sub get_host_arch
() {
3491 $host_arch = (POSIX
::uname
())[4] if !$host_arch;
3497 return get_host_arch
() eq $arch;
3500 my $default_machines = {
3505 sub get_basic_machine_info
{
3506 my ($conf, $forcemachine) = @_;
3508 my $arch = $conf->{arch
} // get_host_arch
();
3509 my $machine = $forcemachine || $conf->{machine
} || $default_machines->{$arch};
3510 return ($arch, $machine);
3513 sub get_ovmf_files
($) {
3516 my $ovmf = $OVMF->{$arch}
3517 or die "no OVMF images known for architecture '$arch'\n";
3523 aarch64
=> '/usr/bin/qemu-system-aarch64',
3524 x86_64
=> '/usr/bin/qemu-system-x86_64',
3526 sub get_command_for_arch
($) {
3528 return '/usr/bin/kvm' if is_native
($arch);
3530 my $cmd = $Arch2Qemu->{$arch}
3531 or die "don't know how to emulate architecture '$arch'\n";
3535 sub get_cpu_options
{
3536 my ($conf, $arch, $kvm, $machine_type, $kvm_off, $kvmver, $winversion, $gpu_passthrough) = @_;
3539 my $ostype = $conf->{ostype
};
3541 my $cpu = $kvm ?
"kvm64" : "qemu64";
3542 if ($arch eq 'aarch64') {
3543 $cpu = 'cortex-a57';
3546 if (my $cputype = $conf->{cpu
}) {
3547 my $cpuconf = PVE
::JSONSchema
::parse_property_string
($cpu_fmt, $cputype)
3548 or die "Cannot parse cpu description: $cputype\n";
3549 $cpu = $cpuconf->{cputype
};
3550 $kvm_off = 1 if $cpuconf->{hidden
};
3551 $hv_vendor_id = $cpuconf->{'hv-vendor-id'};
3553 if (defined(my $flags = $cpuconf->{flags
})) {
3554 push @$cpuFlags, split(";", $flags);
3558 push @$cpuFlags , '+lahf_lm' if $cpu eq 'kvm64' && $arch eq 'x86_64';
3560 push @$cpuFlags , '-x2apic'
3561 if $conf->{ostype
} && $conf->{ostype
} eq 'solaris';
3563 push @$cpuFlags, '+sep' if $cpu eq 'kvm64' || $cpu eq 'kvm32';
3565 push @$cpuFlags, '-rdtscp' if $cpu =~ m/^Opteron/;
3567 if (qemu_machine_feature_enabled
($machine_type, $kvmver, 2, 3) && $arch eq 'x86_64') {
3569 push @$cpuFlags , '+kvm_pv_unhalt' if $kvm;
3570 push @$cpuFlags , '+kvm_pv_eoi' if $kvm;
3573 add_hyperv_enlightenments
($cpuFlags, $winversion, $machine_type, $kvmver, $conf->{bios
}, $gpu_passthrough, $hv_vendor_id) if $kvm;
3575 push @$cpuFlags, 'enforce' if $cpu ne 'host' && $kvm && $arch eq 'x86_64';
3577 push @$cpuFlags, 'kvm=off' if $kvm_off;
3579 if (my $cpu_vendor = $cpu_vendor_list->{$cpu}) {
3580 push @$cpuFlags, "vendor=${cpu_vendor}"
3581 if $cpu_vendor ne 'default';
3582 } elsif ($arch ne 'aarch64') {
3583 die "internal error"; # should not happen
3586 $cpu .= "," . join(',', @$cpuFlags) if scalar(@$cpuFlags);
3588 return ('-cpu', $cpu);
3591 sub config_to_command
{
3592 my ($storecfg, $vmid, $conf, $defaults, $forcemachine) = @_;
3595 my $globalFlags = [];
3596 my $machineFlags = [];
3601 my $vernum = 0; # unknown
3602 my $ostype = $conf->{ostype
};
3603 my $winversion = windows_version
($ostype);
3604 my $kvm = $conf->{kvm
};
3606 my ($arch, $machine_type) = get_basic_machine_info
($conf, $forcemachine);
3607 my $kvm_binary = get_command_for_arch
($arch);
3608 my $kvmver = kvm_user_version
($kvm_binary);
3609 $kvm //= 1 if is_native
($arch);
3612 die "KVM virtualisation configured, but not available. Either disable in VM configuration or enable in BIOS.\n"
3613 if !defined kvm_version
();
3616 if ($kvmver =~ m/^(\d+)\.(\d+)$/) {
3617 $vernum = $1*1000000+$2*1000;
3618 } elsif ($kvmver =~ m/^(\d+)\.(\d+)\.(\d+)$/) {
3619 $vernum = $1*1000000+$2*1000+$3;
3622 die "detected old qemu-kvm binary ($kvmver)\n" if $vernum < 15000;
3624 my $q35 = machine_type_is_q35
($conf);
3625 my $hotplug_features = parse_hotplug_features
(defined($conf->{hotplug
}) ?
$conf->{hotplug
} : '1');
3626 my $use_old_bios_files = undef;
3627 ($use_old_bios_files, $machine_type) = qemu_use_old_bios_files
($machine_type);
3629 my $cpuunits = defined($conf->{cpuunits
}) ?
3630 $conf->{cpuunits
} : $defaults->{cpuunits
};
3632 push @$cmd, $kvm_binary;
3634 push @$cmd, '-id', $vmid;
3636 my $vmname = $conf->{name
} || "vm$vmid";
3638 push @$cmd, '-name', $vmname;
3642 my $qmpsocket = qmp_socket
($vmid);
3643 push @$cmd, '-chardev', "socket,id=qmp,path=$qmpsocket,server,nowait";
3644 push @$cmd, '-mon', "chardev=qmp,mode=control";
3646 if (qemu_machine_feature_enabled
($machine_type, $kvmver, 2, 12)) {
3647 push @$cmd, '-chardev', "socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect=5";
3648 push @$cmd, '-mon', "chardev=qmp-event,mode=control";
3651 push @$cmd, '-pidfile' , pidfile_name
($vmid);
3653 push @$cmd, '-daemonize';
3655 if ($conf->{smbios1
}) {
3656 my $smbios_conf = parse_smbios1
($conf->{smbios1
});
3657 if ($smbios_conf->{base64
}) {
3658 # Do not pass base64 flag to qemu
3659 delete $smbios_conf->{base64
};
3660 my $smbios_string = "";
3661 foreach my $key (keys %$smbios_conf) {
3663 if ($key eq "uuid") {
3664 $value = $smbios_conf->{uuid
}
3666 $value = decode_base64
($smbios_conf->{$key});
3668 # qemu accepts any binary data, only commas need escaping by double comma
3670 $smbios_string .= "," . $key . "=" . $value if $value;
3672 push @$cmd, '-smbios', "type=1" . $smbios_string;
3674 push @$cmd, '-smbios', "type=1,$conf->{smbios1}";
3678 if ($conf->{vmgenid
}) {
3679 push @$devices, '-device', 'vmgenid,guid='.$conf->{vmgenid
};
3682 my ($ovmf_code, $ovmf_vars) = get_ovmf_files
($arch);
3683 if ($conf->{bios
} && $conf->{bios
} eq 'ovmf') {
3684 die "uefi base image not found\n" if ! -f
$ovmf_code;
3688 if (my $efidisk = $conf->{efidisk0
}) {
3689 my $d = PVE
::JSONSchema
::parse_property_string
($efidisk_fmt, $efidisk);
3690 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($d->{file
}, 1);
3691 $format = $d->{format
};
3693 $path = PVE
::Storage
::path
($storecfg, $d->{file
});
3694 if (!defined($format)) {
3695 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
3696 $format = qemu_img_format
($scfg, $volname);
3700 die "efidisk format must be specified\n"
3701 if !defined($format);
3704 warn "no efidisk configured! Using temporary efivars disk.\n";
3705 $path = "/tmp/$vmid-ovmf.fd";
3706 PVE
::Tools
::file_copy
($ovmf_vars, $path, -s
$ovmf_vars);
3710 push @$cmd, '-drive', "if=pflash,unit=0,format=raw,readonly,file=$ovmf_code";
3711 push @$cmd, '-drive', "if=pflash,unit=1,format=$format,id=drive-efidisk0,file=$path";
3716 # we use different pcie-port hardware for qemu >= 4.0 for passthrough
3717 if (qemu_machine_feature_enabled
($machine_type, $kvmver, 4, 0)) {
3718 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-q35-4.0.cfg';
3720 push @$devices, '-readconfig', '/usr/share/qemu-server/pve-q35.cfg';
3724 # add usb controllers
3725 my @usbcontrollers = PVE
::QemuServer
::USB
::get_usb_controllers
($conf, $bridges, $arch, $machine_type, $usbdesc->{format
}, $MAX_USB_DEVICES);
3726 push @$devices, @usbcontrollers if @usbcontrollers;
3727 my $vga = parse_vga
($conf->{vga
});
3729 my $qxlnum = vga_conf_has_spice
($conf->{vga
});
3730 $vga->{type
} = 'qxl' if $qxlnum;
3732 if (!$vga->{type
}) {
3733 if ($arch eq 'aarch64') {
3734 $vga->{type
} = 'virtio';
3735 } elsif (qemu_machine_feature_enabled
($machine_type, $kvmver, 2, 9)) {
3736 $vga->{type
} = (!$winversion || $winversion >= 6) ?
'std' : 'cirrus';
3738 $vga->{type
} = ($winversion >= 6) ?
'std' : 'cirrus';
3742 # enable absolute mouse coordinates (needed by vnc)
3744 if (defined($conf->{tablet
})) {
3745 $tablet = $conf->{tablet
};
3747 $tablet = $defaults->{tablet
};
3748 $tablet = 0 if $qxlnum; # disable for spice because it is not needed
3749 $tablet = 0 if $vga->{type
} =~ m/^serial\d+$/; # disable if we use serial terminal (no vga card)
3753 push @$devices, '-device', print_tabletdevice_full
($conf, $arch) if $tablet;
3754 my $kbd = print_keyboarddevice_full
($conf, $arch);
3755 push @$devices, '-device', $kbd if defined($kbd);
3759 my $gpu_passthrough;
3762 for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
3763 my $id = "hostpci$i";
3764 my $d = parse_hostpci
($conf->{$id});
3767 if (my $pcie = $d->{pcie
}) {
3768 die "q35 machine model is not enabled" if !$q35;
3769 # win7 wants to have the pcie devices directly on the pcie bus
3770 # instead of in the root port
3771 if ($winversion == 7) {
3772 $pciaddr = print_pcie_addr
("${id}bus0");
3774 # add more root ports if needed, 4 are present by default
3775 # by pve-q35 cfgs, rest added here on demand.
3777 push @$devices, '-device', print_pcie_root_port
($i);
3779 $pciaddr = print_pcie_addr
($id);
3782 $pciaddr = print_pci_addr
($id, $bridges, $arch, $machine_type);
3786 if ($d->{'x-vga'}) {
3787 $xvga = ',x-vga=on' if !($conf->{bios
} && $conf->{bios
} eq 'ovmf');
3789 $vga->{type
} = 'none' if !defined($conf->{vga
});
3790 $gpu_passthrough = 1;
3793 my $pcidevices = $d->{pciid
};
3794 my $multifunction = 1 if @$pcidevices > 1;
3797 if ($d->{mdev
} && scalar(@$pcidevices) == 1) {
3798 my $pci_id = $pcidevices->[0]->{id
};
3799 my $uuid = PVE
::SysFSTools
::generate_mdev_uuid
($vmid, $i);
3800 $sysfspath = "/sys/bus/pci/devices/0000:$pci_id/$uuid";
3801 } elsif ($d->{mdev
}) {
3802 warn "ignoring mediated device '$id' with multifunction device\n";
3806 foreach my $pcidevice (@$pcidevices) {
3807 my $devicestr = "vfio-pci";
3810 $devicestr .= ",sysfsdev=$sysfspath";
3812 $devicestr .= ",host=$pcidevice->{id}";
3815 my $mf_addr = $multifunction ?
".$j" : '';
3816 $devicestr .= ",id=${id}${mf_addr}${pciaddr}${mf_addr}";
3819 $devicestr .= ',rombar=0' if defined($d->{rombar
}) && !$d->{rombar
};
3820 $devicestr .= "$xvga";
3821 $devicestr .= ",multifunction=on" if $multifunction;
3822 $devicestr .= ",romfile=/usr/share/kvm/$d->{romfile}" if $d->{romfile
};
3825 push @$devices, '-device', $devicestr;
3831 my $usb_dev_features = {};
3832 $usb_dev_features->{spice_usb3
} = 1 if qemu_machine_feature_enabled
($machine_type, $kvmver, 4, 0);
3834 my @usbdevices = PVE
::QemuServer
::USB
::get_usb_devices
($conf, $usbdesc->{format
}, $MAX_USB_DEVICES, $usb_dev_features);
3835 push @$devices, @usbdevices if @usbdevices;
3837 for (my $i = 0; $i < $MAX_SERIAL_PORTS; $i++) {
3838 if (my $path = $conf->{"serial$i"}) {
3839 if ($path eq 'socket') {
3840 my $socket = "/var/run/qemu-server/${vmid}.serial$i";
3841 push @$devices, '-chardev', "socket,id=serial$i,path=$socket,server,nowait";
3842 # On aarch64, serial0 is the UART device. Qemu only allows
3843 # connecting UART devices via the '-serial' command line, as
3844 # the device has a fixed slot on the hardware...
3845 if ($arch eq 'aarch64' && $i == 0) {
3846 push @$devices, '-serial', "chardev:serial$i";
3848 push @$devices, '-device', "isa-serial,chardev=serial$i";
3851 die "no such serial device\n" if ! -c
$path;
3852 push @$devices, '-chardev', "tty,id=serial$i,path=$path";
3853 push @$devices, '-device', "isa-serial,chardev=serial$i";
3859 for (my $i = 0; $i < $MAX_PARALLEL_PORTS; $i++) {
3860 if (my $path = $conf->{"parallel$i"}) {
3861 die "no such parallel device\n" if ! -c
$path;
3862 my $devtype = $path =~ m!^/dev/usb/lp! ?
'tty' : 'parport';
3863 push @$devices, '-chardev', "$devtype,id=parallel$i,path=$path";
3864 push @$devices, '-device', "isa-parallel,chardev=parallel$i";
3868 if (my $audio = conf_has_audio
($conf)) {
3870 my $audiopciaddr = print_pci_addr
("audio0", $bridges, $arch, $machine_type);
3872 my $id = $audio->{dev_id
};
3873 if ($audio->{dev
} eq 'AC97') {
3874 push @$devices, '-device', "AC97,id=${id}${audiopciaddr}";
3875 } elsif ($audio->{dev
} =~ /intel\-hda$/) {
3876 push @$devices, '-device', "$audio->{dev},id=${id}${audiopciaddr}";
3877 push @$devices, '-device', "hda-micro,id=${id}-codec0,bus=${id}.0,cad=0";
3878 push @$devices, '-device', "hda-duplex,id=${id}-codec1,bus=${id}.0,cad=1";
3880 die "unkown audio device '$audio->{dev}', implement me!";
3883 push @$devices, '-audiodev', "$audio->{backend},id=$audio->{backend_id}";
3887 $sockets = $conf->{smp
} if $conf->{smp
}; # old style - no longer iused
3888 $sockets = $conf->{sockets
} if $conf->{sockets
};
3890 my $cores = $conf->{cores
} || 1;
3892 my $maxcpus = $sockets * $cores;
3894 my $vcpus = $conf->{vcpus
} ?
$conf->{vcpus
} : $maxcpus;
3896 my $allowed_vcpus = $cpuinfo->{cpus
};
3898 die "MAX $allowed_vcpus vcpus allowed per VM on this node\n"
3899 if ($allowed_vcpus < $maxcpus);
3901 if($hotplug_features->{cpu
} && qemu_machine_feature_enabled
($machine_type, $kvmver, 2, 7)) {
3903 push @$cmd, '-smp', "1,sockets=$sockets,cores=$cores,maxcpus=$maxcpus";
3904 for (my $i = 2; $i <= $vcpus; $i++) {
3905 my $cpustr = print_cpu_device
($conf,$i);
3906 push @$cmd, '-device', $cpustr;
3911 push @$cmd, '-smp', "$vcpus,sockets=$sockets,cores=$cores,maxcpus=$maxcpus";
3913 push @$cmd, '-nodefaults';
3915 my $bootorder = $conf->{boot
} || $confdesc->{boot
}->{default};
3917 my $bootindex_hash = {};
3919 foreach my $o (split(//, $bootorder)) {
3920 $bootindex_hash->{$o} = $i*100;
3924 push @$cmd, '-boot', "menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg";
3926 push @$cmd, '-no-acpi' if defined($conf->{acpi
}) && $conf->{acpi
} == 0;
3928 push @$cmd, '-no-reboot' if defined($conf->{reboot
}) && $conf->{reboot
} == 0;
3930 if ($vga->{type
} && $vga->{type
} !~ m/^serial\d+$/ && $vga->{type
} ne 'none'){
3931 push @$devices, '-device', print_vga_device
($conf, $vga, $arch, $machine_type, undef, $qxlnum, $bridges);
3932 my $socket = vnc_socket
($vmid);
3933 push @$cmd, '-vnc', "unix:$socket,password";
3935 push @$cmd, '-vga', 'none' if $vga->{type
} eq 'none';
3936 push @$cmd, '-nographic';
3940 my $tdf = defined($conf->{tdf
}) ?
$conf->{tdf
} : $defaults->{tdf
};
3942 my $useLocaltime = $conf->{localtime};
3944 if ($winversion >= 5) { # windows
3945 $useLocaltime = 1 if !defined($conf->{localtime});
3947 # use time drift fix when acpi is enabled
3948 if (!(defined($conf->{acpi
}) && $conf->{acpi
} == 0)) {
3949 $tdf = 1 if !defined($conf->{tdf
});
3953 if ($winversion >= 6) {
3954 push @$globalFlags, 'kvm-pit.lost_tick_policy=discard';
3955 push @$cmd, '-no-hpet';
3958 push @$rtcFlags, 'driftfix=slew' if $tdf;
3961 push @$machineFlags, 'accel=tcg';
3964 if ($machine_type) {
3965 push @$machineFlags, "type=${machine_type}";
3968 if (($conf->{startdate
}) && ($conf->{startdate
} ne 'now')) {
3969 push @$rtcFlags, "base=$conf->{startdate}";
3970 } elsif ($useLocaltime) {
3971 push @$rtcFlags, 'base=localtime';
3974 push @$cmd, get_cpu_options
($conf, $arch, $kvm, $machine_type, $kvm_off, $kvmver, $winversion, $gpu_passthrough);
3976 PVE
::QemuServer
::Memory
::config
($conf, $vmid, $sockets, $cores, $defaults, $hotplug_features, $cmd);
3978 push @$cmd, '-S' if $conf->{freeze
};
3980 push @$cmd, '-k', $conf->{keyboard
} if defined($conf->{keyboard
});
3982 if (parse_guest_agent
($conf)->{enabled
}) {
3983 my $qgasocket = qmp_socket
($vmid, 1);
3984 my $pciaddr = print_pci_addr
("qga0", $bridges, $arch, $machine_type);
3985 push @$devices, '-chardev', "socket,path=$qgasocket,server,nowait,id=qga0";
3986 push @$devices, '-device', "virtio-serial,id=qga0$pciaddr";
3987 push @$devices, '-device', 'virtserialport,chardev=qga0,name=org.qemu.guest_agent.0';
3995 for(my $i = 1; $i < $qxlnum; $i++){
3996 push @$devices, '-device', print_vga_device
($conf, $vga, $arch, $machine_type, $i, $qxlnum, $bridges);
3999 # assume other OS works like Linux
4000 my ($ram, $vram) = ("134217728", "67108864");
4001 if ($vga->{memory
}) {
4002 $ram = PVE
::Tools
::convert_size
($qxlnum*4*$vga->{memory
}, 'mb' => 'b');
4003 $vram = PVE
::Tools
::convert_size
($qxlnum*2*$vga->{memory
}, 'mb' => 'b');
4005 push @$cmd, '-global', "qxl-vga.ram_size=$ram";
4006 push @$cmd, '-global', "qxl-vga.vram_size=$vram";
4010 my $pciaddr = print_pci_addr
("spice", $bridges, $arch, $machine_type);
4012 my $nodename = PVE
::INotify
::nodename
();
4013 my $pfamily = PVE
::Tools
::get_host_address_family
($nodename);
4014 my @nodeaddrs = PVE
::Tools
::getaddrinfo_all
('localhost', family
=> $pfamily);
4015 die "failed to get an ip address of type $pfamily for 'localhost'\n" if !@nodeaddrs;
4017 push @$devices, '-device', "virtio-serial,id=spice$pciaddr";
4018 push @$devices, '-chardev', "spicevmc,id=vdagent,name=vdagent";
4019 push @$devices, '-device', "virtserialport,chardev=vdagent,name=com.redhat.spice.0";
4021 my $localhost = PVE
::Network
::addr_to_ip
($nodeaddrs[0]->{addr
});
4022 $spice_port = PVE
::Tools
::next_spice_port
($pfamily, $localhost);
4024 my $spice_enhancement = PVE
::JSONSchema
::parse_property_string
($spice_enhancements_fmt, $conf->{spice_enhancements
} // '');
4025 if ($spice_enhancement->{foldersharing
}) {
4026 push @$devices, '-chardev', "spiceport,id=foldershare,name=org.spice-space.webdav.0";
4027 push @$devices, '-device', "virtserialport,chardev=foldershare,name=org.spice-space.webdav.0";
4030 my $spice_opts = "tls-port=${spice_port},addr=$localhost,tls-ciphers=HIGH,seamless-migration=on";
4031 $spice_opts .= ",streaming-video=$spice_enhancement->{videostreaming}" if $spice_enhancement->{videostreaming
};
4032 push @$devices, '-spice', "$spice_opts";
4035 # enable balloon by default, unless explicitly disabled
4036 if (!defined($conf->{balloon
}) || $conf->{balloon
}) {
4037 $pciaddr = print_pci_addr
("balloon0", $bridges, $arch, $machine_type);
4038 push @$devices, '-device', "virtio-balloon-pci,id=balloon0$pciaddr";
4041 if ($conf->{watchdog
}) {
4042 my $wdopts = parse_watchdog
($conf->{watchdog
});
4043 $pciaddr = print_pci_addr
("watchdog", $bridges, $arch, $machine_type);
4044 my $watchdog = $wdopts->{model
} || 'i6300esb';
4045 push @$devices, '-device', "$watchdog$pciaddr";
4046 push @$devices, '-watchdog-action', $wdopts->{action
} if $wdopts->{action
};
4050 my $scsicontroller = {};
4051 my $ahcicontroller = {};
4052 my $scsihw = defined($conf->{scsihw
}) ?
$conf->{scsihw
} : $defaults->{scsihw
};
4054 # Add iscsi initiator name if available
4055 if (my $initiator = get_initiator_name
()) {
4056 push @$devices, '-iscsi', "initiator-name=$initiator";
4059 foreach_drive
($conf, sub {
4060 my ($ds, $drive) = @_;
4062 if (PVE
::Storage
::parse_volume_id
($drive->{file
}, 1)) {
4063 push @$vollist, $drive->{file
};
4066 # ignore efidisk here, already added in bios/fw handling code above
4067 return if $drive->{interface
} eq 'efidisk';
4069 $use_virtio = 1 if $ds =~ m/^virtio/;
4071 if (drive_is_cdrom
($drive)) {
4072 if ($bootindex_hash->{d
}) {
4073 $drive->{bootindex
} = $bootindex_hash->{d
};
4074 $bootindex_hash->{d
} += 1;
4077 if ($bootindex_hash->{c
}) {
4078 $drive->{bootindex
} = $bootindex_hash->{c
} if $conf->{bootdisk
} && ($conf->{bootdisk
} eq $ds);
4079 $bootindex_hash->{c
} += 1;
4083 if($drive->{interface
} eq 'virtio'){
4084 push @$cmd, '-object', "iothread,id=iothread-$ds" if $drive->{iothread
};
4087 if ($drive->{interface
} eq 'scsi') {
4089 my ($maxdev, $controller, $controller_prefix) = scsihw_infos
($conf, $drive);
4091 $pciaddr = print_pci_addr
("$controller_prefix$controller", $bridges, $arch, $machine_type);
4092 my $scsihw_type = $scsihw =~ m/^virtio-scsi-single/ ?
"virtio-scsi-pci" : $scsihw;
4095 if($conf->{scsihw
} && $conf->{scsihw
} eq "virtio-scsi-single" && $drive->{iothread
}){
4096 $iothread .= ",iothread=iothread-$controller_prefix$controller";
4097 push @$cmd, '-object', "iothread,id=iothread-$controller_prefix$controller";
4098 } elsif ($drive->{iothread
}) {
4099 warn "iothread is only valid with virtio disk or virtio-scsi-single controller, ignoring\n";
4103 if($conf->{scsihw
} && $conf->{scsihw
} eq "virtio-scsi-single" && $drive->{queues
}){
4104 $queues = ",num_queues=$drive->{queues}";
4107 push @$devices, '-device', "$scsihw_type,id=$controller_prefix$controller$pciaddr$iothread$queues" if !$scsicontroller->{$controller};
4108 $scsicontroller->{$controller}=1;
4111 if ($drive->{interface
} eq 'sata') {
4112 my $controller = int($drive->{index} / $MAX_SATA_DISKS);
4113 $pciaddr = print_pci_addr
("ahci$controller", $bridges, $arch, $machine_type);
4114 push @$devices, '-device', "ahci,id=ahci$controller,multifunction=on$pciaddr" if !$ahcicontroller->{$controller};
4115 $ahcicontroller->{$controller}=1;
4118 my $drive_cmd = print_drive_full
($storecfg, $vmid, $drive);
4119 push @$devices, '-drive',$drive_cmd;
4120 push @$devices, '-device', print_drivedevice_full
($storecfg, $conf, $vmid, $drive, $bridges, $arch, $machine_type);
4123 for (my $i = 0; $i < $MAX_NETS; $i++) {
4124 next if !$conf->{"net$i"};
4125 my $d = parse_net
($conf->{"net$i"});
4128 $use_virtio = 1 if $d->{model
} eq 'virtio';
4130 if ($bootindex_hash->{n
}) {
4131 $d->{bootindex
} = $bootindex_hash->{n
};
4132 $bootindex_hash->{n
} += 1;
4135 my $netdevfull = print_netdev_full
($vmid, $conf, $arch, $d, "net$i");
4136 push @$devices, '-netdev', $netdevfull;
4138 my $netdevicefull = print_netdevice_full
($vmid, $conf, $d, "net$i", $bridges, $use_old_bios_files, $arch, $machine_type);
4139 push @$devices, '-device', $netdevicefull;
4142 if ($conf->{ivshmem
}) {
4143 my $ivshmem = PVE
::JSONSchema
::parse_property_string
($ivshmem_fmt, $conf->{ivshmem
});
4147 $bus = print_pcie_addr
("ivshmem");
4149 $bus = print_pci_addr
("ivshmem", $bridges, $arch, $machine_type);
4152 my $ivshmem_name = $ivshmem->{name
} // $vmid;
4153 my $path = '/dev/shm/pve-shm-' . $ivshmem_name;
4155 push @$devices, '-device', "ivshmem-plain,memdev=ivshmem$bus,";
4156 push @$devices, '-object', "memory-backend-file,id=ivshmem,share=on,mem-path=$path,size=$ivshmem->{size}M";
4161 if (qemu_machine_feature_enabled
($machine_type, $kvmver, 2, 3)) {
4166 $bridges->{3} = 1 if $scsihw =~ m/^virtio-scsi-single/;
4168 for my $k (sort {$b cmp $a} keys %$bridges) {
4169 $pciaddr = print_pci_addr
("pci.$k", undef, $arch, $machine_type);
4170 unshift @$devices, '-device', "pci-bridge,id=pci.$k,chassis_nr=$k$pciaddr" if $k > 0;
4174 push @$cmd, @$devices;
4175 push @$cmd, '-rtc', join(',', @$rtcFlags)
4176 if scalar(@$rtcFlags);
4177 push @$cmd, '-machine', join(',', @$machineFlags)
4178 if scalar(@$machineFlags);
4179 push @$cmd, '-global', join(',', @$globalFlags)
4180 if scalar(@$globalFlags);
4182 if (my $vmstate = $conf->{vmstate
}) {
4183 my $statepath = PVE
::Storage
::path
($storecfg, $vmstate);
4184 push @$vollist, $statepath;
4185 push @$cmd, '-loadstate', $statepath;
4189 if ($conf->{args
}) {
4190 my $aa = PVE
::Tools
::split_args
($conf->{args
});
4194 return wantarray ?
($cmd, $vollist, $spice_port) : $cmd;
4199 return "${var_run_tmpdir}/$vmid.vnc";
4205 my $res = vm_mon_cmd
($vmid, 'query-spice');
4207 return $res->{'tls-port'} || $res->{'port'} || die "no spice port\n";
4211 my ($vmid, $qga, $name) = @_;
4212 my $sockettype = $qga ?
'qga' : 'qmp';
4213 my $ext = $name ?
'-'.$name : '';
4214 return "${var_run_tmpdir}/$vmid$ext.$sockettype";
4219 return "${var_run_tmpdir}/$vmid.pid";
4222 sub vm_devices_list
{
4225 my $res = vm_mon_cmd
($vmid, 'query-pci');
4226 my $devices_to_check = [];
4228 foreach my $pcibus (@$res) {
4229 push @$devices_to_check, @{$pcibus->{devices
}},
4232 while (@$devices_to_check) {
4234 for my $d (@$devices_to_check) {
4235 $devices->{$d->{'qdev_id'}} = 1 if $d->{'qdev_id'};
4236 next if !$d->{'pci_bridge'};
4238 $devices->{$d->{'qdev_id'}} += scalar(@{$d->{'pci_bridge'}->{devices
}});
4239 push @$to_check, @{$d->{'pci_bridge'}->{devices
}};
4241 $devices_to_check = $to_check;
4244 my $resblock = vm_mon_cmd
($vmid, 'query-block');
4245 foreach my $block (@$resblock) {
4246 if($block->{device
} =~ m/^drive-(\S+)/){
4251 my $resmice = vm_mon_cmd
($vmid, 'query-mice');
4252 foreach my $mice (@$resmice) {
4253 if ($mice->{name
} eq 'QEMU HID Tablet') {
4254 $devices->{tablet
} = 1;
4259 # for usb devices there is no query-usb
4260 # but we can iterate over the entries in
4261 # qom-list path=/machine/peripheral
4262 my $resperipheral = vm_mon_cmd
($vmid, 'qom-list', path
=> '/machine/peripheral');
4263 foreach my $per (@$resperipheral) {
4264 if ($per->{name
} =~ m/^usb\d+$/) {
4265 $devices->{$per->{name
}} = 1;
4273 my ($storecfg, $conf, $vmid, $deviceid, $device, $arch, $machine_type) = @_;
4275 my $q35 = machine_type_is_q35
($conf);
4277 my $devices_list = vm_devices_list
($vmid);
4278 return 1 if defined($devices_list->{$deviceid});
4280 qemu_add_pci_bridge
($storecfg, $conf, $vmid, $deviceid, $arch, $machine_type); # add PCI bridge if we need it for the device
4282 if ($deviceid eq 'tablet') {
4284 qemu_deviceadd
($vmid, print_tabletdevice_full
($conf, $arch));
4286 } elsif ($deviceid eq 'keyboard') {
4288 qemu_deviceadd
($vmid, print_keyboarddevice_full
($conf, $arch));
4290 } elsif ($deviceid =~ m/^usb(\d+)$/) {
4292 die "usb hotplug currently not reliable\n";
4293 # since we can't reliably hot unplug all added usb devices
4294 # and usb passthrough disables live migration
4295 # we disable usb hotplugging for now
4296 qemu_deviceadd
($vmid, PVE
::QemuServer
::USB
::print_usbdevice_full
($conf, $deviceid, $device));
4298 } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
4300 qemu_iothread_add
($vmid, $deviceid, $device);
4302 qemu_driveadd
($storecfg, $vmid, $device);
4303 my $devicefull = print_drivedevice_full
($storecfg, $conf, $vmid, $device, $arch, $machine_type);
4305 qemu_deviceadd
($vmid, $devicefull);
4306 eval { qemu_deviceaddverify
($vmid, $deviceid); };
4308 eval { qemu_drivedel
($vmid, $deviceid); };
4313 } elsif ($deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/) {
4316 my $scsihw = defined($conf->{scsihw
}) ?
$conf->{scsihw
} : "lsi";
4317 my $pciaddr = print_pci_addr
($deviceid, undef, $arch, $machine_type);
4318 my $scsihw_type = $scsihw eq 'virtio-scsi-single' ?
"virtio-scsi-pci" : $scsihw;
4320 my $devicefull = "$scsihw_type,id=$deviceid$pciaddr";
4322 if($deviceid =~ m/^virtioscsi(\d+)$/ && $device->{iothread
}) {
4323 qemu_iothread_add
($vmid, $deviceid, $device);
4324 $devicefull .= ",iothread=iothread-$deviceid";
4327 if($deviceid =~ m/^virtioscsi(\d+)$/ && $device->{queues
}) {
4328 $devicefull .= ",num_queues=$device->{queues}";
4331 qemu_deviceadd
($vmid, $devicefull);
4332 qemu_deviceaddverify
($vmid, $deviceid);
4334 } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
4336 qemu_findorcreatescsihw
($storecfg,$conf, $vmid, $device, $arch, $machine_type);
4337 qemu_driveadd
($storecfg, $vmid, $device);
4339 my $devicefull = print_drivedevice_full
($storecfg, $conf, $vmid, $device, $arch, $machine_type);
4340 eval { qemu_deviceadd
($vmid, $devicefull); };
4342 eval { qemu_drivedel
($vmid, $deviceid); };
4347 } elsif ($deviceid =~ m/^(net)(\d+)$/) {
4349 return undef if !qemu_netdevadd
($vmid, $conf, $arch, $device, $deviceid);
4351 my $machine_type = PVE
::QemuServer
::qemu_machine_pxe
($vmid, $conf);
4352 my $use_old_bios_files = undef;
4353 ($use_old_bios_files, $machine_type) = qemu_use_old_bios_files
($machine_type);
4355 my $netdevicefull = print_netdevice_full
($vmid, $conf, $device, $deviceid, undef, $use_old_bios_files, $arch, $machine_type);
4356 qemu_deviceadd
($vmid, $netdevicefull);
4358 qemu_deviceaddverify
($vmid, $deviceid);
4359 qemu_set_link_status
($vmid, $deviceid, !$device->{link_down
});
4362 eval { qemu_netdevdel
($vmid, $deviceid); };
4367 } elsif (!$q35 && $deviceid =~ m/^(pci\.)(\d+)$/) {
4370 my $pciaddr = print_pci_addr
($deviceid, undef, $arch, $machine_type);
4371 my $devicefull = "pci-bridge,id=pci.$bridgeid,chassis_nr=$bridgeid$pciaddr";
4373 qemu_deviceadd
($vmid, $devicefull);
4374 qemu_deviceaddverify
($vmid, $deviceid);
4377 die "can't hotplug device '$deviceid'\n";
4383 # fixme: this should raise exceptions on error!
4384 sub vm_deviceunplug
{
4385 my ($vmid, $conf, $deviceid) = @_;
4387 my $devices_list = vm_devices_list
($vmid);
4388 return 1 if !defined($devices_list->{$deviceid});
4390 die "can't unplug bootdisk" if $conf->{bootdisk
} && $conf->{bootdisk
} eq $deviceid;
4392 if ($deviceid eq 'tablet' || $deviceid eq 'keyboard') {
4394 qemu_devicedel
($vmid, $deviceid);
4396 } elsif ($deviceid =~ m/^usb\d+$/) {
4398 die "usb hotplug currently not reliable\n";
4399 # when unplugging usb devices this way,
4400 # there may be remaining usb controllers/hubs
4401 # so we disable it for now
4402 qemu_devicedel
($vmid, $deviceid);
4403 qemu_devicedelverify
($vmid, $deviceid);
4405 } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
4407 qemu_devicedel
($vmid, $deviceid);
4408 qemu_devicedelverify
($vmid, $deviceid);
4409 qemu_drivedel
($vmid, $deviceid);
4410 qemu_iothread_del
($conf, $vmid, $deviceid);
4412 } elsif ($deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/) {
4414 qemu_devicedel
($vmid, $deviceid);
4415 qemu_devicedelverify
($vmid, $deviceid);
4416 qemu_iothread_del
($conf, $vmid, $deviceid);
4418 } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
4420 qemu_devicedel
($vmid, $deviceid);
4421 qemu_drivedel
($vmid, $deviceid);
4422 qemu_deletescsihw
($conf, $vmid, $deviceid);
4424 } elsif ($deviceid =~ m/^(net)(\d+)$/) {
4426 qemu_devicedel
($vmid, $deviceid);
4427 qemu_devicedelverify
($vmid, $deviceid);
4428 qemu_netdevdel
($vmid, $deviceid);
4431 die "can't unplug device '$deviceid'\n";
4437 sub qemu_deviceadd
{
4438 my ($vmid, $devicefull) = @_;
4440 $devicefull = "driver=".$devicefull;
4441 my %options = split(/[=,]/, $devicefull);
4443 vm_mon_cmd
($vmid, "device_add" , %options);
4446 sub qemu_devicedel
{
4447 my ($vmid, $deviceid) = @_;
4449 my $ret = vm_mon_cmd
($vmid, "device_del", id
=> $deviceid);
4452 sub qemu_iothread_add
{
4453 my($vmid, $deviceid, $device) = @_;
4455 if ($device->{iothread
}) {
4456 my $iothreads = vm_iothreads_list
($vmid);
4457 qemu_objectadd
($vmid, "iothread-$deviceid", "iothread") if !$iothreads->{"iothread-$deviceid"};
4461 sub qemu_iothread_del
{
4462 my($conf, $vmid, $deviceid) = @_;
4464 my $confid = $deviceid;
4465 if ($deviceid =~ m/^(?:virtioscsi|scsihw)(\d+)$/) {
4466 $confid = 'scsi' . $1;
4468 my $device = parse_drive
($confid, $conf->{$confid});
4469 if ($device->{iothread
}) {
4470 my $iothreads = vm_iothreads_list
($vmid);
4471 qemu_objectdel
($vmid, "iothread-$deviceid") if $iothreads->{"iothread-$deviceid"};
4475 sub qemu_objectadd
{
4476 my($vmid, $objectid, $qomtype) = @_;
4478 vm_mon_cmd
($vmid, "object-add", id
=> $objectid, "qom-type" => $qomtype);
4483 sub qemu_objectdel
{
4484 my($vmid, $objectid) = @_;
4486 vm_mon_cmd
($vmid, "object-del", id
=> $objectid);
4492 my ($storecfg, $vmid, $device) = @_;
4494 my $drive = print_drive_full
($storecfg, $vmid, $device);
4495 $drive =~ s/\\/\\\\/g;
4496 my $ret = vm_human_monitor_command
($vmid, "drive_add auto \"$drive\"");
4498 # If the command succeeds qemu prints: "OK
"
4499 return 1 if $ret =~ m/OK/s;
4501 die "adding drive failed
: $ret\n";
4505 my($vmid, $deviceid) = @_;
4507 my $ret = vm_human_monitor_command($vmid, "drive_del drive-
$deviceid");
4510 return 1 if $ret eq "";
4512 # NB: device not found errors mean the drive was auto-deleted and we ignore the error
4513 return 1 if $ret =~ m/Device \'.*?\' not found/s;
4515 die "deleting drive
$deviceid failed
: $ret\n";
4518 sub qemu_deviceaddverify {
4519 my ($vmid, $deviceid) = @_;
4521 for (my $i = 0; $i <= 5; $i++) {
4522 my $devices_list = vm_devices_list($vmid);
4523 return 1 if defined($devices_list->{$deviceid});
4527 die "error on hotplug device
'$deviceid'\n";
4531 sub qemu_devicedelverify {
4532 my ($vmid, $deviceid) = @_;
4534 # need to verify that the device is correctly removed as device_del
4535 # is async and empty return is not reliable
4537 for (my $i = 0; $i <= 5; $i++) {
4538 my $devices_list = vm_devices_list($vmid);
4539 return 1 if !defined($devices_list->{$deviceid});
4543 die "error on hot-unplugging device
'$deviceid'\n";
4546 sub qemu_findorcreatescsihw {
4547 my ($storecfg, $conf, $vmid, $device, $arch, $machine_type) = @_;
4549 my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $device);
4551 my $scsihwid="$controller_prefix$controller";
4552 my $devices_list = vm_devices_list($vmid);
4554 if(!defined($devices_list->{$scsihwid})) {
4555 vm_deviceplug($storecfg, $conf, $vmid, $scsihwid, $device, $arch, $machine_type);
4561 sub qemu_deletescsihw {
4562 my ($conf, $vmid, $opt) = @_;
4564 my $device = parse_drive($opt, $conf->{$opt});
4566 if ($conf->{scsihw} && ($conf->{scsihw} eq 'virtio-scsi-single')) {
4567 vm_deviceunplug($vmid, $conf, "virtioscsi
$device->{index}");
4571 my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $device);
4573 my $devices_list = vm_devices_list($vmid);
4574 foreach my $opt (keys %{$devices_list}) {
4575 if (PVE::QemuServer::is_valid_drivename($opt)) {
4576 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
4577 if($drive->{interface} eq 'scsi' && $drive->{index} < (($maxdev-1)*($controller+1))) {
4583 my $scsihwid="scsihw
$controller";
4585 vm_deviceunplug($vmid, $conf, $scsihwid);
4590 sub qemu_add_pci_bridge {
4591 my ($storecfg, $conf, $vmid, $device, $arch, $machine_type) = @_;
4597 print_pci_addr($device, $bridges, $arch, $machine_type);
4599 while (my ($k, $v) = each %$bridges) {
4602 return 1 if !defined($bridgeid) || $bridgeid < 1;
4604 my $bridge = "pci
.$bridgeid";
4605 my $devices_list = vm_devices_list($vmid);
4607 if (!defined($devices_list->{$bridge})) {
4608 vm_deviceplug($storecfg, $conf, $vmid, $bridge, $arch, $machine_type);
4614 sub qemu_set_link_status {
4615 my ($vmid, $device, $up) = @_;
4617 vm_mon_cmd($vmid, "set_link
", name => $device,
4618 up => $up ? JSON::true : JSON::false);
4621 sub qemu_netdevadd {
4622 my ($vmid, $conf, $arch, $device, $deviceid) = @_;
4624 my $netdev = print_netdev_full($vmid, $conf, $arch, $device, $deviceid, 1);
4625 my %options = split(/[=,]/, $netdev);
4627 vm_mon_cmd($vmid, "netdev_add
", %options);
4631 sub qemu_netdevdel {
4632 my ($vmid, $deviceid) = @_;
4634 vm_mon_cmd($vmid, "netdev_del
", id => $deviceid);
4637 sub qemu_usb_hotplug {
4638 my ($storecfg, $conf, $vmid, $deviceid, $device, $arch, $machine_type) = @_;
4642 # remove the old one first
4643 vm_deviceunplug($vmid, $conf, $deviceid);
4645 # check if xhci controller is necessary and available
4646 if ($device->{usb3}) {
4648 my $devicelist = vm_devices_list($vmid);
4650 if (!$devicelist->{xhci}) {
4651 my $pciaddr = print_pci_addr("xhci
", undef, $arch, $machine_type);
4652 qemu_deviceadd($vmid, "nec-usb-xhci
,id
=xhci
$pciaddr");
4655 my $d = parse_usb_device($device->{host});
4656 $d->{usb3} = $device->{usb3};
4659 vm_deviceplug($storecfg, $conf, $vmid, $deviceid, $d, $arch, $machine_type);
4662 sub qemu_cpu_hotplug {
4663 my ($vmid, $conf, $vcpus) = @_;
4665 my $machine_type = PVE::QemuServer::get_current_qemu_machine($vmid);
4668 $sockets = $conf->{smp} if $conf->{smp}; # old style - no longer iused
4669 $sockets = $conf->{sockets} if $conf->{sockets};
4670 my $cores = $conf->{cores} || 1;
4671 my $maxcpus = $sockets * $cores;
4673 $vcpus = $maxcpus if !$vcpus;
4675 die "you can
't add more vcpus than maxcpus\n"
4676 if $vcpus > $maxcpus;
4678 my $currentvcpus = $conf->{vcpus} || $maxcpus;
4680 if ($vcpus < $currentvcpus) {
4682 if (qemu_machine_feature_enabled ($machine_type, undef, 2, 7)) {
4684 for (my $i = $currentvcpus; $i > $vcpus; $i--) {
4685 qemu_devicedel($vmid, "cpu$i");
4687 my $currentrunningvcpus = undef;
4689 $currentrunningvcpus = vm_mon_cmd($vmid, "query-cpus");
4690 last if scalar(@{$currentrunningvcpus}) == $i-1;
4691 raise_param_exc({ vcpus => "error unplugging cpu$i" }) if $retry > 5;
4695 #update conf after each succesfull cpu unplug
4696 $conf->{vcpus} = scalar(@{$currentrunningvcpus});
4697 PVE::QemuConfig->write_config($vmid, $conf);
4700 die "cpu hot-unplugging requires qemu version 2.7 or higher\n";
4706 my $currentrunningvcpus = vm_mon_cmd($vmid, "query-cpus");
4707 die "vcpus in running vm does not match its configuration\n"
4708 if scalar(@{$currentrunningvcpus}) != $currentvcpus;
4710 if (qemu_machine_feature_enabled ($machine_type, undef, 2, 7)) {
4712 for (my $i = $currentvcpus+1; $i <= $vcpus; $i++) {
4713 my $cpustr = print_cpu_device($conf, $i);
4714 qemu_deviceadd($vmid, $cpustr);
4717 my $currentrunningvcpus = undef;
4719 $currentrunningvcpus = vm_mon_cmd($vmid, "query-cpus");
4720 last if scalar(@{$currentrunningvcpus}) == $i;
4721 raise_param_exc({ vcpus => "error hotplugging cpu$i" }) if $retry > 10;
4725 #update conf after each succesfull cpu hotplug
4726 $conf->{vcpus} = scalar(@{$currentrunningvcpus});
4727 PVE::QemuConfig->write_config($vmid, $conf);
4731 for (my $i = $currentvcpus; $i < $vcpus; $i++) {
4732 vm_mon_cmd($vmid, "cpu-add", id => int($i));
4737 sub qemu_block_set_io_throttle {
4738 my ($vmid, $deviceid,
4739 $bps, $bps_rd, $bps_wr, $iops, $iops_rd, $iops_wr,
4740 $bps_max, $bps_rd_max, $bps_wr_max, $iops_max, $iops_rd_max, $iops_wr_max,
4741 $bps_max_length, $bps_rd_max_length, $bps_wr_max_length,
4742 $iops_max_length, $iops_rd_max_length, $iops_wr_max_length) = @_;
4744 return if !check_running($vmid) ;
4746 vm_mon_cmd($vmid, "block_set_io_throttle", device => $deviceid,
4748 bps_rd => int($bps_rd),
4749 bps_wr => int($bps_wr),
4751 iops_rd => int($iops_rd),
4752 iops_wr => int($iops_wr),
4753 bps_max => int($bps_max),
4754 bps_rd_max => int($bps_rd_max),
4755 bps_wr_max => int($bps_wr_max),
4756 iops_max => int($iops_max),
4757 iops_rd_max => int($iops_rd_max),
4758 iops_wr_max => int($iops_wr_max),
4759 bps_max_length => int($bps_max_length),
4760 bps_rd_max_length => int($bps_rd_max_length),
4761 bps_wr_max_length => int($bps_wr_max_length),
4762 iops_max_length => int($iops_max_length),
4763 iops_rd_max_length => int($iops_rd_max_length),
4764 iops_wr_max_length => int($iops_wr_max_length),
4769 # old code, only used to shutdown old VM after update
4771 my ($fh, $timeout) = @_;
4773 my $sel = new IO::Select;
4780 while (scalar (@ready = $sel->can_read($timeout))) {
4782 if ($count = $fh->sysread($buf, 8192)) {
4783 if ($buf =~ /^(.*)\(qemu\) $/s) {
4790 if (!defined($count)) {
4797 die "monitor read timeout\n" if !scalar(@ready);
4802 sub qemu_block_resize {
4803 my ($vmid, $deviceid, $storecfg, $volid, $size) = @_;
4805 my $running = check_running($vmid);
4807 $size = 0 if !PVE::Storage::volume_resize($storecfg, $volid, $size, $running);
4809 return if !$running;
4811 vm_mon_cmd($vmid, "block_resize", device => $deviceid, size => int($size));
4815 sub qemu_volume_snapshot {
4816 my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
4818 my $running = check_running($vmid);
4820 if ($running && do_snapshots_with_qemu($storecfg, $volid)){
4821 vm_mon_cmd($vmid, 'blockdev-snapshot-internal-sync
', device => $deviceid, name => $snap);
4823 PVE::Storage::volume_snapshot($storecfg, $volid, $snap);
4827 sub qemu_volume_snapshot_delete {
4828 my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
4830 my $running = check_running($vmid);
4835 my $conf = PVE::QemuConfig->load_config($vmid);
4836 foreach_drive($conf, sub {
4837 my ($ds, $drive) = @_;
4838 $running = 1 if $drive->{file} eq $volid;
4842 if ($running && do_snapshots_with_qemu($storecfg, $volid)){
4843 vm_mon_cmd($vmid, 'blockdev-snapshot-delete-internal-sync
', device => $deviceid, name => $snap);
4845 PVE::Storage::volume_snapshot_delete($storecfg, $volid, $snap, $running);
4849 sub set_migration_caps {
4855 "auto-converge" => 1,
4857 "x-rdma-pin-all" => 0,
4862 my $supported_capabilities = vm_mon_cmd_nocheck($vmid, "query-migrate-capabilities");
4864 for my $supported_capability (@$supported_capabilities) {
4866 capability => $supported_capability->{capability},
4867 state => $enabled_cap->{$supported_capability->{capability}} ? JSON::true : JSON::false,
4871 vm_mon_cmd_nocheck($vmid, "migrate-set-capabilities", capabilities => $cap_ref);
4874 my $fast_plug_option = {
4882 'vmstatestorage
' => 1,
4886 # hotplug changes in [PENDING]
4887 # $selection hash can be used to only apply specified options, for
4888 # example: { cores => 1 } (only apply changed 'cores
')
4889 # $errors ref is used to return error messages
4890 sub vmconfig_hotplug_pending {
4891 my ($vmid, $conf, $storecfg, $selection, $errors) = @_;
4893 my $defaults = load_defaults();
4894 my ($arch, $machine_type) = get_basic_machine_info($conf, undef);
4896 # commit values which do not have any impact on running VM first
4897 # Note: those option cannot raise errors, we we do not care about
4898 # $selection and always apply them.
4900 my $add_error = sub {
4901 my ($opt, $msg) = @_;
4902 $errors->{$opt} = "hotplug problem - $msg";
4906 foreach my $opt (keys %{$conf->{pending}}) { # add/change
4907 if ($fast_plug_option->{$opt}) {
4908 $conf->{$opt} = $conf->{pending}->{$opt};
4909 delete $conf->{pending}->{$opt};
4915 PVE::QemuConfig->write_config($vmid, $conf);
4916 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
4919 my $hotplug_features = parse_hotplug_features(defined($conf->{hotplug}) ? $conf->{hotplug} : '1');
4921 my $pending_delete_hash = split_flagged_list($conf->{pending}->{delete});
4922 while (my ($opt, $force) = each %$pending_delete_hash) {
4923 next if $selection && !$selection->{$opt};
4925 if ($opt eq 'hotplug
') {
4926 die "skip\n" if ($conf->{hotplug} =~ /memory/);
4927 } elsif ($opt eq 'tablet
') {
4928 die "skip\n" if !$hotplug_features->{usb};
4929 if ($defaults->{tablet}) {
4930 vm_deviceplug($storecfg, $conf, $vmid, 'tablet
', $arch, $machine_type);
4931 vm_deviceplug($storecfg, $conf, $vmid, 'keyboard
', $arch, $machine_type)
4932 if $arch eq 'aarch64
';
4934 vm_deviceunplug($vmid, $conf, 'tablet
');
4935 vm_deviceunplug($vmid, $conf, 'keyboard
') if $arch eq 'aarch64
';
4937 } elsif ($opt =~ m/^usb\d+/) {
4939 # since we cannot reliably hot unplug usb devices
4940 # we are disabling it
4941 die "skip\n" if !$hotplug_features->{usb} || $conf->{$opt} =~ m/spice/i;
4942 vm_deviceunplug($vmid, $conf, $opt);
4943 } elsif ($opt eq 'vcpus
') {
4944 die "skip\n" if !$hotplug_features->{cpu};
4945 qemu_cpu_hotplug($vmid, $conf, undef);
4946 } elsif ($opt eq 'balloon
') {
4947 # enable balloon device is not hotpluggable
4948 die "skip\n" if defined($conf->{balloon}) && $conf->{balloon} == 0;
4949 # here we reset the ballooning value to memory
4950 my $balloon = $conf->{memory} || $defaults->{memory};
4951 vm_mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
4952 } elsif ($fast_plug_option->{$opt}) {
4954 } elsif ($opt =~ m/^net(\d+)$/) {
4955 die "skip\n" if !$hotplug_features->{network};
4956 vm_deviceunplug($vmid, $conf, $opt);
4957 } elsif (is_valid_drivename($opt)) {
4958 die "skip\n" if !$hotplug_features->{disk} || $opt =~ m/(ide|sata)(\d+)/;
4959 vm_deviceunplug($vmid, $conf, $opt);
4960 vmconfig_delete_or_detach_drive($vmid, $storecfg, $conf, $opt, $force);
4961 } elsif ($opt =~ m/^memory$/) {
4962 die "skip\n" if !$hotplug_features->{memory};
4963 PVE::QemuServer::Memory::qemu_memory_hotplug($vmid, $conf, $defaults, $opt);
4964 } elsif ($opt eq 'cpuunits
') {
4965 cgroups_write("cpu", $vmid, "cpu.shares", $defaults->{cpuunits});
4966 } elsif ($opt eq 'cpulimit
') {
4967 cgroups_write("cpu", $vmid, "cpu.cfs_quota_us", -1);
4973 &$add_error($opt, $err) if $err ne "skip\n";
4975 # save new config if hotplug was successful
4976 delete $conf->{$opt};
4977 vmconfig_undelete_pending_option($conf, $opt);
4978 PVE::QemuConfig->write_config($vmid, $conf);
4979 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
4983 my $apply_pending_cloudinit;
4984 $apply_pending_cloudinit = sub {
4985 my ($key, $value) = @_;
4986 $apply_pending_cloudinit = sub {}; # once is enough
4988 my @cloudinit_opts = keys %$confdesc_cloudinit;
4989 foreach my $opt (keys %{$conf->{pending}}) {
4990 next if !grep { $_ eq $opt } @cloudinit_opts;
4991 $conf->{$opt} = delete $conf->{pending}->{$opt};
4994 my $new_conf = { %$conf };
4995 $new_conf->{$key} = $value;
4996 PVE::QemuServer::Cloudinit::generate_cloudinitconfig($new_conf, $vmid);
4999 foreach my $opt (keys %{$conf->{pending}}) {
5000 next if $selection && !$selection->{$opt};
5001 my $value = $conf->{pending}->{$opt};
5003 if ($opt eq 'hotplug
') {
5004 die "skip\n" if ($value =~ /memory/) || ($value !~ /memory/ && $conf->{hotplug} =~ /memory/);
5005 } elsif ($opt eq 'tablet
') {
5006 die "skip\n" if !$hotplug_features->{usb};
5008 vm_deviceplug($storecfg, $conf, $vmid, 'tablet
', $arch, $machine_type);
5009 vm_deviceplug($storecfg, $conf, $vmid, 'keyboard
', $arch, $machine_type)
5010 if $arch eq 'aarch64
';
5011 } elsif ($value == 0) {
5012 vm_deviceunplug($vmid, $conf, 'tablet
');
5013 vm_deviceunplug($vmid, $conf, 'keyboard
') if $arch eq 'aarch64
';
5015 } elsif ($opt =~ m/^usb\d+$/) {
5017 # since we cannot reliably hot unplug usb devices
5018 # we are disabling it
5019 die "skip\n" if !$hotplug_features->{usb} || $value =~ m/spice/i;
5020 my $d = eval { PVE::JSONSchema::parse_property_string($usbdesc->{format}, $value) };
5021 die "skip\n" if !$d;
5022 qemu_usb_hotplug($storecfg, $conf, $vmid, $opt, $d, $arch, $machine_type);
5023 } elsif ($opt eq 'vcpus
') {
5024 die "skip\n" if !$hotplug_features->{cpu};
5025 qemu_cpu_hotplug($vmid, $conf, $value);
5026 } elsif ($opt eq 'balloon
') {
5027 # enable/disable balloning device is not hotpluggable
5028 my $old_balloon_enabled = !!(!defined($conf->{balloon}) || $conf->{balloon});
5029 my $new_balloon_enabled = !!(!defined($conf->{pending}->{balloon}) || $conf->{pending}->{balloon});
5030 die "skip\n" if $old_balloon_enabled != $new_balloon_enabled;
5032 # allow manual ballooning if shares is set to zero
5033 if ((defined($conf->{shares}) && ($conf->{shares} == 0))) {
5034 my $balloon = $conf->{pending}->{balloon} || $conf->{memory} || $defaults->{memory};
5035 vm_mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
5037 } elsif ($opt =~ m/^net(\d+)$/) {
5038 # some changes can be done without hotplug
5039 vmconfig_update_net($storecfg, $conf, $hotplug_features->{network},
5040 $vmid, $opt, $value, $arch, $machine_type);
5041 } elsif (is_valid_drivename($opt)) {
5042 # some changes can be done without hotplug
5043 my $drive = parse_drive($opt, $value);
5044 if (drive_is_cloudinit($drive)) {
5045 &$apply_pending_cloudinit($opt, $value);
5047 vmconfig_update_disk($storecfg, $conf, $hotplug_features->{disk},
5048 $vmid, $opt, $value, 1, $arch, $machine_type);
5049 } elsif ($opt =~ m/^memory$/) { #dimms
5050 die "skip\n" if !$hotplug_features->{memory};
5051 $value = PVE::QemuServer::Memory::qemu_memory_hotplug($vmid, $conf, $defaults, $opt, $value);
5052 } elsif ($opt eq 'cpuunits
') {
5053 cgroups_write("cpu", $vmid, "cpu.shares", $conf->{pending}->{$opt});
5054 } elsif ($opt eq 'cpulimit
') {
5055 my $cpulimit = $conf->{pending}->{$opt} == 0 ? -1 : int($conf->{pending}->{$opt} * 100000);
5056 cgroups_write("cpu", $vmid, "cpu.cfs_quota_us", $cpulimit);
5058 die "skip\n"; # skip non-hot-pluggable options
5062 &$add_error($opt, $err) if $err ne "skip\n";
5064 # save new config if hotplug was successful
5065 $conf->{$opt} = $value;
5066 delete $conf->{pending}->{$opt};
5067 PVE::QemuConfig->write_config($vmid, $conf);
5068 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
5073 sub try_deallocate_drive {
5074 my ($storecfg, $vmid, $conf, $key, $drive, $rpcenv, $authuser, $force) = @_;
5076 if (($force || $key =~ /^unused/) && !drive_is_cdrom($drive, 1)) {
5077 my $volid = $drive->{file};
5078 if (vm_is_volid_owner($storecfg, $vmid, $volid)) {
5079 my $sid = PVE::Storage::parse_volume_id($volid);
5080 $rpcenv->check($authuser, "/storage/$sid", ['Datastore
.AllocateSpace
']);
5082 # check if the disk is really unused
5083 die "unable to delete '$volid' - volume is still in use (snapshot?)\n"
5084 if is_volume_in_use($storecfg, $conf, $key, $volid);
5085 PVE::Storage::vdisk_free($storecfg, $volid);
5088 # If vm is not owner of this disk remove from config
5096 sub vmconfig_delete_or_detach_drive {
5097 my ($vmid, $storecfg, $conf, $opt, $force) = @_;
5099 my $drive = parse_drive($opt, $conf->{$opt});
5101 my $rpcenv = PVE::RPCEnvironment::get();
5102 my $authuser = $rpcenv->get_user();
5105 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM
.Config
.Disk
']);
5106 try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser, $force);
5108 vmconfig_register_unused_drive($storecfg, $vmid, $conf, $drive);
5112 sub vmconfig_apply_pending {
5113 my ($vmid, $conf, $storecfg) = @_;
5117 my $pending_delete_hash = split_flagged_list($conf->{pending}->{delete});
5118 while (my ($opt, $force) = each %$pending_delete_hash) {
5119 die "internal error" if $opt =~ m/^unused/;
5120 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
5121 if (!defined($conf->{$opt})) {
5122 vmconfig_undelete_pending_option($conf, $opt);
5123 PVE::QemuConfig->write_config($vmid, $conf);
5124 } elsif (is_valid_drivename($opt)) {
5125 vmconfig_delete_or_detach_drive($vmid, $storecfg, $conf, $opt, $force);
5126 vmconfig_undelete_pending_option($conf, $opt);
5127 delete $conf->{$opt};
5128 PVE::QemuConfig->write_config($vmid, $conf);
5130 vmconfig_undelete_pending_option($conf, $opt);
5131 delete $conf->{$opt};
5132 PVE::QemuConfig->write_config($vmid, $conf);
5136 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
5138 foreach my $opt (keys %{$conf->{pending}}) { # add/change
5139 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
5141 if (defined($conf->{$opt}) && ($conf->{$opt} eq $conf->{pending}->{$opt})) {
5142 # skip if nothing changed
5143 } elsif (is_valid_drivename($opt)) {
5144 vmconfig_register_unused_drive($storecfg, $vmid, $conf, parse_drive($opt, $conf->{$opt}))
5145 if defined($conf->{$opt});
5146 $conf->{$opt} = $conf->{pending}->{$opt};
5148 $conf->{$opt} = $conf->{pending}->{$opt};
5151 delete $conf->{pending}->{$opt};
5152 PVE::QemuConfig->write_config($vmid, $conf);
5156 my $safe_num_ne = sub {
5159 return 0 if !defined($a) && !defined($b);
5160 return 1 if !defined($a);
5161 return 1 if !defined($b);
5166 my $safe_string_ne = sub {
5169 return 0 if !defined($a) && !defined($b);
5170 return 1 if !defined($a);
5171 return 1 if !defined($b);
5176 sub vmconfig_update_net {
5177 my ($storecfg, $conf, $hotplug, $vmid, $opt, $value, $arch, $machine_type) = @_;
5179 my $newnet = parse_net($value);
5181 if ($conf->{$opt}) {
5182 my $oldnet = parse_net($conf->{$opt});
5184 if (&$safe_string_ne($oldnet->{model}, $newnet->{model}) ||
5185 &$safe_string_ne($oldnet->{macaddr}, $newnet->{macaddr}) ||
5186 &$safe_num_ne($oldnet->{queues}, $newnet->{queues}) ||
5187 !($newnet->{bridge} && $oldnet->{bridge})) { # bridge/nat mode change
5189 # for non online change, we try to hot-unplug
5190 die "skip\n" if !$hotplug;
5191 vm_deviceunplug($vmid, $conf, $opt);
5194 die "internal error" if $opt !~ m/net(\d+)/;
5195 my $iface = "tap${vmid}i$1";
5197 if (&$safe_string_ne($oldnet->{bridge}, $newnet->{bridge}) ||
5198 &$safe_num_ne($oldnet->{tag}, $newnet->{tag}) ||
5199 &$safe_string_ne($oldnet->{trunks}, $newnet->{trunks}) ||
5200 &$safe_num_ne($oldnet->{firewall}, $newnet->{firewall})) {
5201 PVE::Network::tap_unplug($iface);
5202 PVE::Network::tap_plug($iface, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
5203 } elsif (&$safe_num_ne($oldnet->{rate}, $newnet->{rate})) {
5204 # Rate can be applied on its own but any change above needs to
5205 # include the rate in tap_plug since OVS resets everything.
5206 PVE::Network::tap_rate_limit($iface, $newnet->{rate});
5209 if (&$safe_string_ne($oldnet->{link_down}, $newnet->{link_down})) {
5210 qemu_set_link_status($vmid, $opt, !$newnet->{link_down});
5218 vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet, $arch, $machine_type);
5224 sub vmconfig_update_disk {
5225 my ($storecfg, $conf, $hotplug, $vmid, $opt, $value, $force, $arch, $machine_type) = @_;
5227 # fixme: do we need force?
5229 my $drive = parse_drive($opt, $value);
5231 if ($conf->{$opt}) {
5233 if (my $old_drive = parse_drive($opt, $conf->{$opt})) {
5235 my $media = $drive->{media} || 'disk
';
5236 my $oldmedia = $old_drive->{media} || 'disk
';
5237 die "unable to change media type\n" if $media ne $oldmedia;
5239 if (!drive_is_cdrom($old_drive)) {
5241 if ($drive->{file} ne $old_drive->{file}) {
5243 die "skip\n" if !$hotplug;
5245 # unplug and register as unused
5246 vm_deviceunplug($vmid, $conf, $opt);
5247 vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive)
5250 # update existing disk
5252 # skip non hotpluggable value
5253 if (&$safe_string_ne($drive->{discard}, $old_drive->{discard}) ||
5254 &$safe_string_ne($drive->{iothread}, $old_drive->{iothread}) ||
5255 &$safe_string_ne($drive->{queues}, $old_drive->{queues}) ||
5256 &$safe_string_ne($drive->{cache}, $old_drive->{cache})) {
5261 if (&$safe_num_ne($drive->{mbps}, $old_drive->{mbps}) ||
5262 &$safe_num_ne($drive->{mbps_rd}, $old_drive->{mbps_rd}) ||
5263 &$safe_num_ne($drive->{mbps_wr}, $old_drive->{mbps_wr}) ||
5264 &$safe_num_ne($drive->{iops}, $old_drive->{iops}) ||
5265 &$safe_num_ne($drive->{iops_rd}, $old_drive->{iops_rd}) ||
5266 &$safe_num_ne($drive->{iops_wr}, $old_drive->{iops_wr}) ||
5267 &$safe_num_ne($drive->{mbps_max}, $old_drive->{mbps_max}) ||
5268 &$safe_num_ne($drive->{mbps_rd_max}, $old_drive->{mbps_rd_max}) ||
5269 &$safe_num_ne($drive->{mbps_wr_max}, $old_drive->{mbps_wr_max}) ||
5270 &$safe_num_ne($drive->{iops_max}, $old_drive->{iops_max}) ||
5271 &$safe_num_ne($drive->{iops_rd_max}, $old_drive->{iops_rd_max}) ||
5272 &$safe_num_ne($drive->{iops_wr_max}, $old_drive->{iops_wr_max}) ||
5273 &$safe_num_ne($drive->{bps_max_length}, $old_drive->{bps_max_length}) ||
5274 &$safe_num_ne($drive->{bps_rd_max_length}, $old_drive->{bps_rd_max_length}) ||
5275 &$safe_num_ne($drive->{bps_wr_max_length}, $old_drive->{bps_wr_max_length}) ||
5276 &$safe_num_ne($drive->{iops_max_length}, $old_drive->{iops_max_length}) ||
5277 &$safe_num_ne($drive->{iops_rd_max_length}, $old_drive->{iops_rd_max_length}) ||
5278 &$safe_num_ne($drive->{iops_wr_max_length}, $old_drive->{iops_wr_max_length})) {
5280 qemu_block_set_io_throttle($vmid,"drive-$opt",
5281 ($drive->{mbps} || 0)*1024*1024,
5282 ($drive->{mbps_rd} || 0)*1024*1024,
5283 ($drive->{mbps_wr} || 0)*1024*1024,
5284 $drive->{iops} || 0,
5285 $drive->{iops_rd} || 0,
5286 $drive->{iops_wr} || 0,
5287 ($drive->{mbps_max} || 0)*1024*1024,
5288 ($drive->{mbps_rd_max} || 0)*1024*1024,
5289 ($drive->{mbps_wr_max} || 0)*1024*1024,
5290 $drive->{iops_max} || 0,
5291 $drive->{iops_rd_max} || 0,
5292 $drive->{iops_wr_max} || 0,
5293 $drive->{bps_max_length} || 1,
5294 $drive->{bps_rd_max_length} || 1,
5295 $drive->{bps_wr_max_length} || 1,
5296 $drive->{iops_max_length} || 1,
5297 $drive->{iops_rd_max_length} || 1,
5298 $drive->{iops_wr_max_length} || 1);
5307 if ($drive->{file} eq 'none
') {
5308 vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt");
5309 if (drive_is_cloudinit($old_drive)) {
5310 vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive);
5313 my $path = get_iso_path($storecfg, $vmid, $drive->{file});
5314 vm_mon_cmd($vmid, "eject", force => JSON::true,device => "drive-$opt"); # force eject if locked
5315 vm_mon_cmd($vmid, "change", device => "drive-$opt",target => "$path") if $path;
5323 die "skip\n" if !$hotplug || $opt =~ m/(ide|sata)(\d+)/;
5325 PVE::Storage::activate_volumes($storecfg, [$drive->{file}]) if $drive->{file} !~ m|^/dev/.+|;
5326 vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive, $arch, $machine_type);
5330 my ($storecfg, $vmid, $statefile, $skiplock, $migratedfrom, $paused,
5331 $forcemachine, $spice_ticket, $migration_network, $migration_type, $targetstorage) = @_;
5333 PVE::QemuConfig->lock_config($vmid, sub {
5334 my $conf = PVE::QemuConfig->load_config($vmid, $migratedfrom);
5336 die "you can't start a vm
if it
's a template\n" if PVE::QemuConfig->is_template($conf);
5338 my $is_suspended = PVE::QemuConfig->has_lock($conf, 'suspended
');
5340 PVE::QemuConfig->check_lock($conf)
5341 if !($skiplock || $is_suspended);
5343 die "VM $vmid already running\n" if check_running($vmid, undef, $migratedfrom);
5345 # clean up leftover reboot request files
5346 eval { clear_reboot_request($vmid); };
5349 if (!$statefile && scalar(keys %{$conf->{pending}})) {
5350 vmconfig_apply_pending($vmid, $conf, $storecfg);
5351 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
5354 PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid);
5356 my $defaults = load_defaults();
5358 # set environment variable useful inside network script
5359 $ENV{PVE_MIGRATED_FROM} = $migratedfrom if $migratedfrom;
5361 my $local_volumes = {};
5363 if ($targetstorage) {
5364 foreach_drive($conf, sub {
5365 my ($ds, $drive) = @_;
5367 return if drive_is_cdrom($drive);
5369 my $volid = $drive->{file};
5373 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
5375 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
5376 return if $scfg->{shared};
5377 $local_volumes->{$ds} = [$volid, $storeid, $volname];
5382 foreach my $opt (sort keys %$local_volumes) {
5384 my ($volid, $storeid, $volname) = @{$local_volumes->{$opt}};
5385 my $drive = parse_drive($opt, $conf->{$opt});
5387 #if remote storage is specified, use default format
5388 if ($targetstorage && $targetstorage ne "1") {
5389 $storeid = $targetstorage;
5390 my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
5391 $format = $defFormat;
5393 #else we use same format than original
5394 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
5395 $format = qemu_img_format($scfg, $volid);
5398 my $newvolid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, undef, ($drive->{size}/1024));
5399 my $newdrive = $drive;
5400 $newdrive->{format} = $format;
5401 $newdrive->{file} = $newvolid;
5402 my $drivestr = PVE::QemuServer::print_drive($vmid, $newdrive);
5403 $local_volumes->{$opt} = $drivestr;
5404 #pass drive to conf for command line
5405 $conf->{$opt} = $drivestr;
5409 PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'pre-start
', 1);
5411 if ($is_suspended) {
5412 # enforce machine type on suspended vm to ensure HW compatibility
5413 $forcemachine = $conf->{runningmachine};
5414 print "Resuming suspended VM\n";
5417 my ($cmd, $vollist, $spice_port) = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine);
5421 if ($statefile eq 'tcp
') {
5422 my $localip = "localhost";
5423 my $datacenterconf = PVE::Cluster::cfs_read_file('datacenter
.cfg
');
5424 my $nodename = PVE::INotify::nodename();
5426 if (!defined($migration_type)) {
5427 if (defined($datacenterconf->{migration}->{type})) {
5428 $migration_type = $datacenterconf->{migration}->{type};
5430 $migration_type = 'secure
';
5434 if ($migration_type eq 'insecure
') {
5435 my $migrate_network_addr = PVE::Cluster::get_local_migration_ip($migration_network);
5436 if ($migrate_network_addr) {
5437 $localip = $migrate_network_addr;
5439 $localip = PVE::Cluster::remote_node_ip($nodename, 1);
5442 $localip = "[$localip]" if Net::IP::ip_is_ipv6($localip);
5445 my $pfamily = PVE::Tools::get_host_address_family($nodename);
5446 my $migrate_port = PVE::Tools::next_migrate_port($pfamily);
5447 $migrate_uri = "tcp:${localip}:${migrate_port}";
5448 push @$cmd, '-incoming
', $migrate_uri;
5451 } elsif ($statefile eq 'unix
') {
5452 # should be default for secure migrations as a ssh TCP forward
5453 # tunnel is not deterministic reliable ready and fails regurarly
5454 # to set up in time, so use UNIX socket forwards
5455 my $socket_addr = "/run/qemu-server/$vmid.migrate";
5456 unlink $socket_addr;
5458 $migrate_uri = "unix:$socket_addr";
5460 push @$cmd, '-incoming
', $migrate_uri;
5463 } elsif (-e $statefile) {
5464 push @$cmd, '-loadstate
', $statefile;
5466 my $statepath = PVE::Storage::path($storecfg, $statefile);
5467 push @$vollist, $statepath;
5468 push @$cmd, '-loadstate
', $statepath;
5475 for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
5476 my $d = parse_hostpci($conf->{"hostpci$i"});
5478 my $pcidevices = $d->{pciid};
5479 foreach my $pcidevice (@$pcidevices) {
5480 my $pciid = $pcidevice->{id};
5482 my $info = PVE::SysFSTools::pci_device_info("0000:$pciid");
5483 die "IOMMU not present\n" if !PVE::SysFSTools::check_iommu_support();
5484 die "no pci device info for device '$pciid'\n" if !$info;
5487 my $uuid = PVE::SysFSTools::generate_mdev_uuid($vmid, $i);
5488 PVE::SysFSTools::pci_create_mdev_device($pciid, $uuid, $d->{mdev});
5490 die "can't unbind
/bind pci group to vfio
'$pciid'\n"
5491 if !PVE::SysFSTools::pci_dev_group_bind_to_vfio($pciid);
5492 die "can
't reset pci device '$pciid'\n"
5493 if $info->{has_fl_reset} and !PVE::SysFSTools::pci_dev_reset($info);
5498 PVE::Storage::activate_volumes($storecfg, $vollist);
5501 run_command(['/bin/systemctl
', 'stop
', "$vmid.scope"],
5502 outfunc => sub {}, errfunc => sub {});
5504 # Issues with the above 'stop
' not being fully completed are extremely rare, a very low
5505 # timeout should be more than enough here...
5506 PVE::Systemd::wait_for_unit_removed("$vmid.scope", 5);
5508 my $cpuunits = defined($conf->{cpuunits}) ? $conf->{cpuunits}
5509 : $defaults->{cpuunits};
5511 my $start_timeout = ($conf->{hugepages} || $is_suspended) ? 300 : 30;
5512 my %run_params = (timeout => $statefile ? undef : $start_timeout, umask => 0077);
5515 Slice => 'qemu
.slice
',
5517 CPUShares => $cpuunits
5520 if (my $cpulimit = $conf->{cpulimit}) {
5521 $properties{CPUQuota} = int($cpulimit * 100);
5523 $properties{timeout} = 10 if $statefile; # setting up the scope shoul be quick
5525 my $run_qemu = sub {
5526 PVE::Tools::run_fork sub {
5527 PVE::Systemd::enter_systemd_scope($vmid, "Proxmox VE VM $vmid", %properties);
5528 run_command($cmd, %run_params);
5532 if ($conf->{hugepages}) {
5535 my $hugepages_topology = PVE::QemuServer::Memory::hugepages_topology($conf);
5536 my $hugepages_host_topology = PVE::QemuServer::Memory::hugepages_host_topology();
5538 PVE::QemuServer::Memory::hugepages_mount();
5539 PVE::QemuServer::Memory::hugepages_allocate($hugepages_topology, $hugepages_host_topology);
5541 eval { $run_qemu->() };
5543 PVE::QemuServer::Memory::hugepages_reset($hugepages_host_topology);
5547 PVE::QemuServer::Memory::hugepages_pre_deallocate($hugepages_topology);
5549 eval { PVE::QemuServer::Memory::hugepages_update_locked($code); };
5552 eval { $run_qemu->() };
5556 # deactivate volumes if start fails
5557 eval { PVE::Storage::deactivate_volumes($storecfg, $vollist); };
5558 die "start failed: $err";
5561 print "migration listens on $migrate_uri\n" if $migrate_uri;
5563 if ($statefile && $statefile ne 'tcp
' && $statefile ne 'unix
') {
5564 eval { vm_mon_cmd_nocheck($vmid, "cont"); };
5568 #start nbd server for storage migration
5569 if ($targetstorage) {
5570 my $nodename = PVE::INotify::nodename();
5571 my $migrate_network_addr = PVE::Cluster::get_local_migration_ip($migration_network);
5572 my $localip = $migrate_network_addr ? $migrate_network_addr : PVE::Cluster::remote_node_ip($nodename, 1);
5573 my $pfamily = PVE::Tools::get_host_address_family($nodename);
5574 my $storage_migrate_port = PVE::Tools::next_migrate_port($pfamily);
5576 vm_mon_cmd_nocheck($vmid, "nbd-server-start", addr => { type => 'inet
', data => { host => "${localip}", port => "${storage_migrate_port}" } } );
5578 $localip = "[$localip]" if Net::IP::ip_is_ipv6($localip);
5580 foreach my $opt (sort keys %$local_volumes) {
5581 my $volid = $local_volumes->{$opt};
5582 vm_mon_cmd_nocheck($vmid, "nbd-server-add", device => "drive-$opt", writable => JSON::true );
5583 my $migrate_storage_uri = "nbd:${localip}:${storage_migrate_port}:exportname=drive-$opt";
5584 print "storage migration listens on $migrate_storage_uri volume:$volid\n";
5588 if ($migratedfrom) {
5590 set_migration_caps($vmid);
5595 print "spice listens on port $spice_port\n";
5596 if ($spice_ticket) {
5597 vm_mon_cmd_nocheck($vmid, "set_password", protocol => 'spice
', password => $spice_ticket);
5598 vm_mon_cmd_nocheck($vmid, "expire_password", protocol => 'spice
', time => "+30");
5603 vm_mon_cmd_nocheck($vmid, "balloon", value => $conf->{balloon}*1024*1024)
5604 if !$statefile && $conf->{balloon};
5606 foreach my $opt (keys %$conf) {
5607 next if $opt !~ m/^net\d+$/;
5608 my $nicconf = parse_net($conf->{$opt});
5609 qemu_set_link_status($vmid, $opt, 0) if $nicconf->{link_down};
5613 vm_mon_cmd_nocheck($vmid, 'qom-set
',
5614 path => "machine/peripheral/balloon0",
5615 property => "guest-stats-polling-interval",
5616 value => 2) if (!defined($conf->{balloon}) || $conf->{balloon});
5618 if ($is_suspended && (my $vmstate = $conf->{vmstate})) {
5619 print "Resumed VM, removing state\n";
5620 delete $conf->@{qw(lock vmstate runningmachine)};
5621 PVE
::Storage
::deactivate_volumes
($storecfg, [$vmstate]);
5622 PVE
::Storage
::vdisk_free
($storecfg, $vmstate);
5623 PVE
::QemuConfig-
>write_config($vmid, $conf);
5626 PVE
::GuestHelpers
::exec_hookscript
($conf, $vmid, 'post-start');
5631 my ($vmid, $execute, %params) = @_;
5633 my $cmd = { execute
=> $execute, arguments
=> \
%params };
5634 vm_qmp_command
($vmid, $cmd);
5637 sub vm_mon_cmd_nocheck
{
5638 my ($vmid, $execute, %params) = @_;
5640 my $cmd = { execute
=> $execute, arguments
=> \
%params };
5641 vm_qmp_command
($vmid, $cmd, 1);
5644 sub vm_qmp_command
{
5645 my ($vmid, $cmd, $nocheck) = @_;
5650 if ($cmd->{arguments
}) {
5651 $timeout = delete $cmd->{arguments
}->{timeout
};
5655 die "VM $vmid not running\n" if !check_running
($vmid, $nocheck);
5656 my $sname = qmp_socket
($vmid);
5657 if (-e
$sname) { # test if VM is reasonambe new and supports qmp/qga
5658 my $qmpclient = PVE
::QMPClient-
>new();
5660 $res = $qmpclient->cmd($vmid, $cmd, $timeout);
5662 die "unable to open monitor socket\n";
5666 syslog
("err", "VM $vmid qmp command failed - $err");
5673 sub vm_human_monitor_command
{
5674 my ($vmid, $cmdline) = @_;
5677 execute
=> 'human-monitor-command',
5678 arguments
=> { 'command-line' => $cmdline},
5681 return vm_qmp_command
($vmid, $cmd);
5684 sub vm_commandline
{
5685 my ($storecfg, $vmid, $snapname) = @_;
5687 my $conf = PVE
::QemuConfig-
>load_config($vmid);
5690 my $snapshot = $conf->{snapshots
}->{$snapname};
5691 die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
5693 $snapshot->{digest
} = $conf->{digest
}; # keep file digest for API
5698 my $defaults = load_defaults
();
5700 my $cmd = config_to_command
($storecfg, $vmid, $conf, $defaults);
5702 return PVE
::Tools
::cmd2string
($cmd);
5706 my ($vmid, $skiplock) = @_;
5708 PVE
::QemuConfig-
>lock_config($vmid, sub {
5710 my $conf = PVE
::QemuConfig-
>load_config($vmid);
5712 PVE
::QemuConfig-
>check_lock($conf) if !$skiplock;
5714 vm_mon_cmd
($vmid, "system_reset");
5718 sub get_vm_volumes
{
5722 foreach_volid
($conf, sub {
5723 my ($volid, $attr) = @_;
5725 return if $volid =~ m
|^/|;
5727 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
5730 push @$vollist, $volid;
5736 sub vm_stop_cleanup
{
5737 my ($storecfg, $vmid, $conf, $keepActive, $apply_pending_changes) = @_;
5742 my $vollist = get_vm_volumes
($conf);
5743 PVE
::Storage
::deactivate_volumes
($storecfg, $vollist);
5746 foreach my $ext (qw(mon qmp pid vnc qga)) {
5747 unlink "/var/run/qemu-server/${vmid}.$ext";
5750 if ($conf->{ivshmem
}) {
5751 my $ivshmem = PVE
::JSONSchema
::parse_property_string
($ivshmem_fmt, $conf->{ivshmem
});
5752 # just delete it for now, VMs which have this already open do not
5753 # are affected, but new VMs will get a separated one. If this
5754 # becomes an issue we either add some sort of ref-counting or just
5755 # add a "don't delete on stop" flag to the ivshmem format.
5756 unlink '/dev/shm/pve-shm-' . ($ivshmem->{name
} // $vmid);
5759 foreach my $key (keys %$conf) {
5760 next if $key !~ m/^hostpci(\d+)$/;
5761 my $hostpciindex = $1;
5762 my $d = parse_hostpci
($conf->{$key});
5763 my $uuid = PVE
::SysFSTools
::generate_mdev_uuid
($vmid, $hostpciindex);
5765 foreach my $pci (@{$d->{pciid
}}) {
5766 my $pciid = $pci->{id
};
5767 PVE
::SysFSTools
::pci_cleanup_mdev_device
($pciid, $uuid);
5771 vmconfig_apply_pending
($vmid, $conf, $storecfg) if $apply_pending_changes;
5773 warn $@ if $@; # avoid errors - just warn
5776 # call only in locked context
5778 my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive) = @_;
5780 my $pid = check_running
($vmid, $nocheck);
5785 $conf = PVE
::QemuConfig-
>load_config($vmid);
5786 PVE
::QemuConfig-
>check_lock($conf) if !$skiplock;
5787 if (!defined($timeout) && $shutdown && $conf->{startup
}) {
5788 my $opts = PVE
::JSONSchema
::pve_parse_startup_order
($conf->{startup
});
5789 $timeout = $opts->{down
} if $opts->{down
};
5791 PVE
::GuestHelpers
::exec_hookscript
($conf, $vmid, 'pre-stop');
5796 if (defined($conf) && parse_guest_agent
($conf)->{enabled
}) {
5797 vm_qmp_command
($vmid, {
5798 execute
=> "guest-shutdown",
5799 arguments
=> { timeout
=> $timeout }
5802 vm_qmp_command
($vmid, { execute
=> "system_powerdown" }, $nocheck);
5805 vm_qmp_command
($vmid, { execute
=> "quit" }, $nocheck);
5811 $timeout = 60 if !defined($timeout);
5814 while (($count < $timeout) && check_running
($vmid, $nocheck)) {
5819 if ($count >= $timeout) {
5821 warn "VM still running - terminating now with SIGTERM\n";
5824 die "VM quit/powerdown failed - got timeout\n";
5827 vm_stop_cleanup
($storecfg, $vmid, $conf, $keepActive, 1) if $conf;
5832 warn "VM quit/powerdown failed - terminating now with SIGTERM\n";
5835 die "VM quit/powerdown failed\n";
5843 while (($count < $timeout) && check_running
($vmid, $nocheck)) {
5848 if ($count >= $timeout) {
5849 warn "VM still running - terminating now with SIGKILL\n";
5854 vm_stop_cleanup
($storecfg, $vmid, $conf, $keepActive, 1) if $conf;
5857 # Note: use $nocheck to skip tests if VM configuration file exists.
5858 # We need that when migration VMs to other nodes (files already moved)
5859 # Note: we set $keepActive in vzdump stop mode - volumes need to stay active
5861 my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive, $migratedfrom) = @_;
5863 $force = 1 if !defined($force) && !$shutdown;
5866 my $pid = check_running
($vmid, $nocheck, $migratedfrom);
5867 kill 15, $pid if $pid;
5868 my $conf = PVE
::QemuConfig-
>load_config($vmid, $migratedfrom);
5869 vm_stop_cleanup
($storecfg, $vmid, $conf, $keepActive, 0);
5873 PVE
::QemuConfig-
>lock_config($vmid, sub {
5874 _do_vm_stop
($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive);
5879 my ($vmid, $timeout) = @_;
5881 PVE
::QemuConfig-
>lock_config($vmid, sub {
5883 # only reboot if running, as qmeventd starts it again on a stop event
5884 return if !check_running
($vmid);
5886 create_reboot_request
($vmid);
5888 my $storecfg = PVE
::Storage
::config
();
5889 _do_vm_stop
($storecfg, $vmid, undef, undef, $timeout, 1);
5895 my ($vmid, $skiplock, $includestate, $statestorage) = @_;
5902 PVE
::QemuConfig-
>lock_config($vmid, sub {
5904 $conf = PVE
::QemuConfig-
>load_config($vmid);
5906 my $is_backing_up = PVE
::QemuConfig-
>has_lock($conf, 'backup');
5907 PVE
::QemuConfig-
>check_lock($conf)
5908 if !($skiplock || $is_backing_up);
5910 die "cannot suspend to disk during backup\n"
5911 if $is_backing_up && $includestate;
5913 if ($includestate) {
5914 $conf->{lock} = 'suspending';
5915 my $date = strftime
("%Y-%m-%d", localtime(time()));
5916 $storecfg = PVE
::Storage
::config
();
5917 $vmstate = PVE
::QemuConfig-
>__snapshot_save_vmstate($vmid, $conf, "suspend-$date", $storecfg, $statestorage, 1);
5918 $path = PVE
::Storage
::path
($storecfg, $vmstate);
5919 PVE
::QemuConfig-
>write_config($vmid, $conf);
5921 vm_mon_cmd
($vmid, "stop");
5925 if ($includestate) {
5927 PVE
::Storage
::activate_volumes
($storecfg, [$vmstate]);
5930 vm_mon_cmd
($vmid, "savevm-start", statefile
=> $path);
5932 my $state = vm_mon_cmd_nocheck
($vmid, "query-savevm");
5933 if (!$state->{status
}) {
5934 die "savevm not active\n";
5935 } elsif ($state->{status
} eq 'active') {
5938 } elsif ($state->{status
} eq 'completed') {
5939 print "State saved, quitting\n";
5941 } elsif ($state->{status
} eq 'failed' && $state->{error
}) {
5942 die "query-savevm failed with error '$state->{error}'\n"
5944 die "query-savevm returned status '$state->{status}'\n";
5950 PVE
::QemuConfig-
>lock_config($vmid, sub {
5951 $conf = PVE
::QemuConfig-
>load_config($vmid);
5953 # cleanup, but leave suspending lock, to indicate something went wrong
5955 vm_mon_cmd
($vmid, "savevm-end");
5956 PVE
::Storage
::deactivate_volumes
($storecfg, [$vmstate]);
5957 PVE
::Storage
::vdisk_free
($storecfg, $vmstate);
5958 delete $conf->@{qw(vmstate runningmachine)};
5959 PVE
::QemuConfig-
>write_config($vmid, $conf);
5965 die "lock changed unexpectedly\n"
5966 if !PVE
::QemuConfig-
>has_lock($conf, 'suspending');
5968 vm_qmp_command
($vmid, { execute
=> "quit" });
5969 $conf->{lock} = 'suspended';
5970 PVE
::QemuConfig-
>write_config($vmid, $conf);
5976 my ($vmid, $skiplock, $nocheck) = @_;
5978 PVE
::QemuConfig-
>lock_config($vmid, sub {
5979 my $vm_mon_cmd = $nocheck ? \
&vm_mon_cmd_nocheck
: \
&vm_mon_cmd
;
5980 my $res = $vm_mon_cmd->($vmid, 'query-status');
5981 my $resume_cmd = 'cont';
5983 if ($res->{status
} && $res->{status
} eq 'suspended') {
5984 $resume_cmd = 'system_wakeup';
5989 my $conf = PVE
::QemuConfig-
>load_config($vmid);
5991 PVE
::QemuConfig-
>check_lock($conf)
5992 if !($skiplock || PVE
::QemuConfig-
>has_lock($conf, 'backup'));
5995 $vm_mon_cmd->($vmid, $resume_cmd);
6000 my ($vmid, $skiplock, $key) = @_;
6002 PVE
::QemuConfig-
>lock_config($vmid, sub {
6004 my $conf = PVE
::QemuConfig-
>load_config($vmid);
6006 # there is no qmp command, so we use the human monitor command
6007 my $res = vm_human_monitor_command
($vmid, "sendkey $key");
6008 die $res if $res ne '';
6013 my ($storecfg, $vmid, $skiplock) = @_;
6015 PVE
::QemuConfig-
>lock_config($vmid, sub {
6017 my $conf = PVE
::QemuConfig-
>load_config($vmid);
6019 if (!check_running
($vmid)) {
6020 destroy_vm
($storecfg, $vmid, undef, $skiplock);
6022 die "VM $vmid is running - destroy failed\n";
6027 # vzdump restore implementaion
6029 sub tar_archive_read_firstfile
{
6030 my $archive = shift;
6032 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
6034 # try to detect archive type first
6035 my $pid = open (my $fh, '-|', 'tar', 'tf', $archive) ||
6036 die "unable to open file '$archive'\n";
6037 my $firstfile = <$fh>;
6041 die "ERROR: archive contaions no data\n" if !$firstfile;
6047 sub tar_restore_cleanup
{
6048 my ($storecfg, $statfile) = @_;
6050 print STDERR
"starting cleanup\n";
6052 if (my $fd = IO
::File-
>new($statfile, "r")) {
6053 while (defined(my $line = <$fd>)) {
6054 if ($line =~ m/vzdump:([^\s:]*):(\S+)$/) {
6057 if ($volid =~ m
|^/|) {
6058 unlink $volid || die 'unlink failed\n';
6060 PVE
::Storage
::vdisk_free
($storecfg, $volid);
6062 print STDERR
"temporary volume '$volid' sucessfuly removed\n";
6064 print STDERR
"unable to cleanup '$volid' - $@" if $@;
6066 print STDERR
"unable to parse line in statfile - $line";
6073 sub restore_archive
{
6074 my ($archive, $vmid, $user, $opts) = @_;
6076 my $format = $opts->{format
};
6079 if ($archive =~ m/\.tgz$/ || $archive =~ m/\.tar\.gz$/) {
6080 $format = 'tar' if !$format;
6082 } elsif ($archive =~ m/\.tar$/) {
6083 $format = 'tar' if !$format;
6084 } elsif ($archive =~ m/.tar.lzo$/) {
6085 $format = 'tar' if !$format;
6087 } elsif ($archive =~ m/\.vma$/) {
6088 $format = 'vma' if !$format;
6089 } elsif ($archive =~ m/\.vma\.gz$/) {
6090 $format = 'vma' if !$format;
6092 } elsif ($archive =~ m/\.vma\.lzo$/) {
6093 $format = 'vma' if !$format;
6096 $format = 'vma' if !$format; # default
6099 # try to detect archive format
6100 if ($format eq 'tar') {
6101 return restore_tar_archive
($archive, $vmid, $user, $opts);
6103 return restore_vma_archive
($archive, $vmid, $user, $opts, $comp);
6107 sub restore_update_config_line
{
6108 my ($outfd, $cookie, $vmid, $map, $line, $unique) = @_;
6110 return if $line =~ m/^\#qmdump\#/;
6111 return if $line =~ m/^\#vzdump\#/;
6112 return if $line =~ m/^lock:/;
6113 return if $line =~ m/^unused\d+:/;
6114 return if $line =~ m/^parent:/;
6116 my $dc = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
6117 if (($line =~ m/^(vlan(\d+)):\s*(\S+)\s*$/)) {
6118 # try to convert old 1.X settings
6119 my ($id, $ind, $ethcfg) = ($1, $2, $3);
6120 foreach my $devconfig (PVE
::Tools
::split_list
($ethcfg)) {
6121 my ($model, $macaddr) = split(/\=/, $devconfig);
6122 $macaddr = PVE
::Tools
::random_ether_addr
($dc->{mac_prefix
}) if !$macaddr || $unique;
6125 bridge
=> "vmbr$ind",
6126 macaddr
=> $macaddr,
6128 my $netstr = print_net
($net);
6130 print $outfd "net$cookie->{netcount}: $netstr\n";
6131 $cookie->{netcount
}++;
6133 } elsif (($line =~ m/^(net\d+):\s*(\S+)\s*$/) && $unique) {
6134 my ($id, $netstr) = ($1, $2);
6135 my $net = parse_net
($netstr);
6136 $net->{macaddr
} = PVE
::Tools
::random_ether_addr
($dc->{mac_prefix
}) if $net->{macaddr
};
6137 $netstr = print_net
($net);
6138 print $outfd "$id: $netstr\n";
6139 } elsif ($line =~ m/^((ide|scsi|virtio|sata|efidisk)\d+):\s*(\S+)\s*$/) {
6142 my $di = parse_drive
($virtdev, $value);
6143 if (defined($di->{backup
}) && !$di->{backup
}) {
6144 print $outfd "#$line";
6145 } elsif ($map->{$virtdev}) {
6146 delete $di->{format
}; # format can change on restore
6147 $di->{file
} = $map->{$virtdev};
6148 $value = print_drive
($vmid, $di);
6149 print $outfd "$virtdev: $value\n";
6153 } elsif (($line =~ m/^vmgenid: (.*)/)) {
6155 if ($vmgenid ne '0') {
6156 # always generate a new vmgenid if there was a valid one setup
6157 $vmgenid = generate_uuid
();
6159 print $outfd "vmgenid: $vmgenid\n";
6160 } elsif (($line =~ m/^(smbios1: )(.*)/) && $unique) {
6161 my ($uuid, $uuid_str);
6162 UUID
::generate
($uuid);
6163 UUID
::unparse
($uuid, $uuid_str);
6164 my $smbios1 = parse_smbios1
($2);
6165 $smbios1->{uuid
} = $uuid_str;
6166 print $outfd $1.print_smbios1
($smbios1)."\n";
6173 my ($cfg, $vmid) = @_;
6175 my $info = PVE
::Storage
::vdisk_list
($cfg, undef, $vmid);
6177 my $volid_hash = {};
6178 foreach my $storeid (keys %$info) {
6179 foreach my $item (@{$info->{$storeid}}) {
6180 next if !($item->{volid
} && $item->{size
});
6181 $item->{path
} = PVE
::Storage
::path
($cfg, $item->{volid
});
6182 $volid_hash->{$item->{volid
}} = $item;
6189 sub is_volume_in_use
{
6190 my ($storecfg, $conf, $skip_drive, $volid) = @_;
6192 my $path = PVE
::Storage
::path
($storecfg, $volid);
6194 my $scan_config = sub {
6195 my ($cref, $snapname) = @_;
6197 foreach my $key (keys %$cref) {
6198 my $value = $cref->{$key};
6199 if (is_valid_drivename
($key)) {
6200 next if $skip_drive && $key eq $skip_drive;
6201 my $drive = parse_drive
($key, $value);
6202 next if !$drive || !$drive->{file
} || drive_is_cdrom
($drive);
6203 return 1 if $volid eq $drive->{file
};
6204 if ($drive->{file
} =~ m!^/!) {
6205 return 1 if $drive->{file
} eq $path;
6207 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($drive->{file
}, 1);
6209 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid, 1);
6211 return 1 if $path eq PVE
::Storage
::path
($storecfg, $drive->{file
}, $snapname);
6219 return 1 if &$scan_config($conf);
6223 foreach my $snapname (keys %{$conf->{snapshots
}}) {
6224 return 1 if &$scan_config($conf->{snapshots
}->{$snapname}, $snapname);
6230 sub update_disksize
{
6231 my ($vmid, $conf, $volid_hash) = @_;
6234 my $prefix = "VM $vmid:";
6236 # used and unused disks
6237 my $referenced = {};
6239 # Note: it is allowed to define multiple storages with same path (alias), so
6240 # we need to check both 'volid' and real 'path' (two different volid can point
6241 # to the same path).
6243 my $referencedpath = {};
6246 foreach my $opt (keys %$conf) {
6247 if (is_valid_drivename
($opt)) {
6248 my $drive = parse_drive
($opt, $conf->{$opt});
6249 my $volid = $drive->{file
};
6252 $referenced->{$volid} = 1;
6253 if ($volid_hash->{$volid} &&
6254 (my $path = $volid_hash->{$volid}->{path
})) {
6255 $referencedpath->{$path} = 1;
6258 next if drive_is_cdrom
($drive);
6259 next if !$volid_hash->{$volid};
6261 $drive->{size
} = $volid_hash->{$volid}->{size
};
6262 my $new = print_drive
($vmid, $drive);
6263 if ($new ne $conf->{$opt}) {
6265 $conf->{$opt} = $new;
6266 print "$prefix update disk '$opt' information.\n";
6271 # remove 'unusedX' entry if volume is used
6272 foreach my $opt (keys %$conf) {
6273 next if $opt !~ m/^unused\d+$/;
6274 my $volid = $conf->{$opt};
6275 my $path = $volid_hash->{$volid}->{path
} if $volid_hash->{$volid};
6276 if ($referenced->{$volid} || ($path && $referencedpath->{$path})) {
6277 print "$prefix remove entry '$opt', its volume '$volid' is in use.\n";
6279 delete $conf->{$opt};
6282 $referenced->{$volid} = 1;
6283 $referencedpath->{$path} = 1 if $path;
6286 foreach my $volid (sort keys %$volid_hash) {
6287 next if $volid =~ m/vm-$vmid-state-/;
6288 next if $referenced->{$volid};
6289 my $path = $volid_hash->{$volid}->{path
};
6290 next if !$path; # just to be sure
6291 next if $referencedpath->{$path};
6293 my $key = PVE
::QemuConfig-
>add_unused_volume($conf, $volid);
6294 print "$prefix add unreferenced volume '$volid' as '$key' to config.\n";
6295 $referencedpath->{$path} = 1; # avoid to add more than once (aliases)
6302 my ($vmid, $nolock, $dryrun) = @_;
6304 my $cfg = PVE
::Storage
::config
();
6306 # FIXME: Remove once our RBD plugin can handle CT and VM on a single storage
6307 # see: https://pve.proxmox.com/pipermail/pve-devel/2018-July/032900.html
6308 foreach my $stor (keys %{$cfg->{ids
}}) {
6309 delete($cfg->{ids
}->{$stor}) if ! $cfg->{ids
}->{$stor}->{content
}->{images
};
6312 print "rescan volumes...\n";
6313 my $volid_hash = scan_volids
($cfg, $vmid);
6315 my $updatefn = sub {
6318 my $conf = PVE
::QemuConfig-
>load_config($vmid);
6320 PVE
::QemuConfig-
>check_lock($conf);
6323 foreach my $volid (keys %$volid_hash) {
6324 my $info = $volid_hash->{$volid};
6325 $vm_volids->{$volid} = $info if $info->{vmid
} && $info->{vmid
} == $vmid;
6328 my $changes = update_disksize
($vmid, $conf, $vm_volids);
6330 PVE
::QemuConfig-
>write_config($vmid, $conf) if $changes && !$dryrun;
6333 if (defined($vmid)) {
6337 PVE
::QemuConfig-
>lock_config($vmid, $updatefn, $vmid);
6340 my $vmlist = config_list
();
6341 foreach my $vmid (keys %$vmlist) {
6345 PVE
::QemuConfig-
>lock_config($vmid, $updatefn, $vmid);
6351 sub restore_vma_archive
{
6352 my ($archive, $vmid, $user, $opts, $comp) = @_;
6354 my $readfrom = $archive;
6356 my $cfg = PVE
::Storage
::config
();
6358 my $bwlimit = $opts->{bwlimit
};
6360 my $dbg_cmdstring = '';
6361 my $add_pipe = sub {
6363 push @$commands, $cmd;
6364 $dbg_cmdstring .= ' | ' if length($dbg_cmdstring);
6365 $dbg_cmdstring .= PVE
::Tools
::cmd2string
($cmd);
6370 if ($archive eq '-') {
6373 # If we use a backup from a PVE defined storage we also consider that
6374 # storage's rate limit:
6375 my (undef, $volid) = PVE
::Storage
::path_to_volume_id
($cfg, $archive);
6376 if (defined($volid)) {
6377 my ($sid, undef) = PVE
::Storage
::parse_volume_id
($volid);
6378 my $readlimit = PVE
::Storage
::get_bandwidth_limit
('restore', [$sid], $bwlimit);
6380 print STDERR
"applying read rate limit: $readlimit\n";
6381 my $cstream = ['cstream', '-t', $readlimit*1024, '--', $readfrom];
6382 $add_pipe->($cstream);
6389 if ($comp eq 'gzip') {
6390 $cmd = ['zcat', $readfrom];
6391 } elsif ($comp eq 'lzop') {
6392 $cmd = ['lzop', '-d', '-c', $readfrom];
6394 die "unknown compression method '$comp'\n";
6399 my $tmpdir = "/var/tmp/vzdumptmp$$";
6402 # disable interrupts (always do cleanups)
6406 local $SIG{HUP
} = sub { warn "got interrupt - ignored\n"; };
6408 my $mapfifo = "/var/tmp/vzdumptmp$$.fifo";
6409 POSIX
::mkfifo
($mapfifo, 0600);
6412 my $openfifo = sub {
6413 open($fifofh, '>', $mapfifo) || die $!;
6416 $add_pipe->(['vma', 'extract', '-v', '-r', $mapfifo, $readfrom, $tmpdir]);
6423 my $rpcenv = PVE
::RPCEnvironment
::get
();
6425 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
6426 my $tmpfn = "$conffile.$$.tmp";
6428 # Note: $oldconf is undef if VM does not exists
6429 my $cfs_path = PVE
::QemuConfig-
>cfs_config_path($vmid);
6430 my $oldconf = PVE
::Cluster
::cfs_read_file
($cfs_path);
6434 my $print_devmap = sub {
6435 my $virtdev_hash = {};
6437 my $cfgfn = "$tmpdir/qemu-server.conf";
6439 # we can read the config - that is already extracted
6440 my $fh = IO
::File-
>new($cfgfn, "r") ||
6441 "unable to read qemu-server.conf - $!\n";
6443 my $fwcfgfn = "$tmpdir/qemu-server.fw";
6445 my $pve_firewall_dir = '/etc/pve/firewall';
6446 mkdir $pve_firewall_dir; # make sure the dir exists
6447 PVE
::Tools
::file_copy
($fwcfgfn, "${pve_firewall_dir}/$vmid.fw");
6450 while (defined(my $line = <$fh>)) {
6451 if ($line =~ m/^\#qmdump\#map:(\S+):(\S+):(\S*):(\S*):$/) {
6452 my ($virtdev, $devname, $storeid, $format) = ($1, $2, $3, $4);
6453 die "archive does not contain data for drive '$virtdev'\n"
6454 if !$devinfo->{$devname};
6455 if (defined($opts->{storage
})) {
6456 $storeid = $opts->{storage
} || 'local';
6457 } elsif (!$storeid) {
6460 $format = 'raw' if !$format;
6461 $devinfo->{$devname}->{devname
} = $devname;
6462 $devinfo->{$devname}->{virtdev
} = $virtdev;
6463 $devinfo->{$devname}->{format
} = $format;
6464 $devinfo->{$devname}->{storeid
} = $storeid;
6466 # check permission on storage
6467 my $pool = $opts->{pool
}; # todo: do we need that?
6468 if ($user ne 'root@pam') {
6469 $rpcenv->check($user, "/storage/$storeid", ['Datastore.AllocateSpace']);
6472 $storage_limits{$storeid} = $bwlimit;
6474 $virtdev_hash->{$virtdev} = $devinfo->{$devname};
6475 } elsif ($line =~ m/^((?:ide|sata|scsi)\d+):\s*(.*)\s*$/) {
6477 my $drive = parse_drive
($virtdev, $2);
6478 if (drive_is_cloudinit
($drive)) {
6479 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($drive->{file
});
6480 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
6481 my $format = qemu_img_format
($scfg, $volname); # has 'raw' fallback
6485 storeid
=> $opts->{storage
} // $storeid,
6486 size
=> PVE
::QemuServer
::Cloudinit
::CLOUDINIT_DISK_SIZE
,
6487 file
=> $drive->{file
}, # to make drive_is_cloudinit check possible
6488 name
=> "vm-$vmid-cloudinit",
6491 $virtdev_hash->{$virtdev} = $d;
6496 foreach my $key (keys %storage_limits) {
6497 my $limit = PVE
::Storage
::get_bandwidth_limit
('restore', [$key], $bwlimit);
6499 print STDERR
"rate limit for storage $key: $limit KiB/s\n";
6500 $storage_limits{$key} = $limit * 1024;
6503 foreach my $devname (keys %$devinfo) {
6504 die "found no device mapping information for device '$devname'\n"
6505 if !$devinfo->{$devname}->{virtdev
};
6508 # create empty/temp config
6510 PVE
::Tools
::file_set_contents
($conffile, "memory: 128\n");
6511 foreach_drive
($oldconf, sub {
6512 my ($ds, $drive) = @_;
6514 return if drive_is_cdrom
($drive, 1);
6516 my $volid = $drive->{file
};
6517 return if !$volid || $volid =~ m
|^/|;
6519 my ($path, $owner) = PVE
::Storage
::path
($cfg, $volid);
6520 return if !$path || !$owner || ($owner != $vmid);
6522 # Note: only delete disk we want to restore
6523 # other volumes will become unused
6524 if ($virtdev_hash->{$ds}) {
6525 eval { PVE
::Storage
::vdisk_free
($cfg, $volid); };
6532 # delete vmstate files, after the restore we have no snapshots anymore
6533 foreach my $snapname (keys %{$oldconf->{snapshots
}}) {
6534 my $snap = $oldconf->{snapshots
}->{$snapname};
6535 if ($snap->{vmstate
}) {
6536 eval { PVE
::Storage
::vdisk_free
($cfg, $snap->{vmstate
}); };
6545 foreach my $virtdev (sort keys %$virtdev_hash) {
6546 my $d = $virtdev_hash->{$virtdev};
6547 my $alloc_size = int(($d->{size
} + 1024 - 1)/1024);
6548 my $storeid = $d->{storeid
};
6549 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
6552 if (my $limit = $storage_limits{$storeid}) {
6553 $map_opts .= "throttling.bps=$limit:throttling.group=$storeid:";
6556 # test if requested format is supported
6557 my ($defFormat, $validFormats) = PVE
::Storage
::storage_default_format
($cfg, $storeid);
6558 my $supported = grep { $_ eq $d->{format
} } @$validFormats;
6559 $d->{format
} = $defFormat if !$supported;
6562 if ($d->{is_cloudinit
}) {
6564 $name .= ".$d->{format}" if $d->{format
} ne 'raw';
6567 my $volid = PVE
::Storage
::vdisk_alloc
($cfg, $storeid, $vmid, $d->{format
}, $name, $alloc_size);
6568 print STDERR
"new volume ID is '$volid'\n";
6569 $d->{volid
} = $volid;
6571 PVE
::Storage
::activate_volumes
($cfg, [$volid]);
6573 my $write_zeros = 1;
6574 if (PVE
::Storage
::volume_has_feature
($cfg, 'sparseinit', $volid)) {
6578 if (!$d->{is_cloudinit
}) {
6579 my $path = PVE
::Storage
::path
($cfg, $volid);
6581 print $fifofh "${map_opts}format=$d->{format}:${write_zeros}:$d->{devname}=$path\n";
6583 print "map '$d->{devname}' to '$path' (write zeros = ${write_zeros})\n";
6585 $map->{$virtdev} = $volid;
6588 $fh->seek(0, 0) || die "seek failed - $!\n";
6590 my $outfd = new IO
::File
($tmpfn, "w") ||
6591 die "unable to write config for VM $vmid\n";
6593 my $cookie = { netcount
=> 0 };
6594 while (defined(my $line = <$fh>)) {
6595 restore_update_config_line
($outfd, $cookie, $vmid, $map, $line, $opts->{unique
});
6608 local $SIG{PIPE
} = sub { die "interrupted by signal\n"; };
6609 local $SIG{ALRM
} = sub { die "got timeout\n"; };
6611 $oldtimeout = alarm($timeout);
6618 if ($line =~ m/^DEV:\sdev_id=(\d+)\ssize:\s(\d+)\sdevname:\s(\S+)$/) {
6619 my ($dev_id, $size, $devname) = ($1, $2, $3);
6620 $devinfo->{$devname} = { size
=> $size, dev_id
=> $dev_id };
6621 } elsif ($line =~ m/^CTIME: /) {
6622 # we correctly received the vma config, so we can disable
6623 # the timeout now for disk allocation (set to 10 minutes, so
6624 # that we always timeout if something goes wrong)
6627 print $fifofh "done\n";
6628 my $tmp = $oldtimeout || 0;
6629 $oldtimeout = undef;
6635 print "restore vma archive: $dbg_cmdstring\n";
6636 run_command
($commands, input
=> $input, outfunc
=> $parser, afterfork
=> $openfifo);
6640 alarm($oldtimeout) if $oldtimeout;
6643 foreach my $devname (keys %$devinfo) {
6644 my $volid = $devinfo->{$devname}->{volid
};
6645 push @$vollist, $volid if $volid;
6648 PVE
::Storage
::deactivate_volumes
($cfg, $vollist);
6656 foreach my $devname (keys %$devinfo) {
6657 my $volid = $devinfo->{$devname}->{volid
};
6660 if ($volid =~ m
|^/|) {
6661 unlink $volid || die 'unlink failed\n';
6663 PVE
::Storage
::vdisk_free
($cfg, $volid);
6665 print STDERR
"temporary volume '$volid' sucessfuly removed\n";
6667 print STDERR
"unable to cleanup '$volid' - $@" if $@;
6674 rename($tmpfn, $conffile) ||
6675 die "unable to commit configuration file '$conffile'\n";
6677 PVE
::Cluster
::cfs_update
(); # make sure we read new file
6679 eval { rescan
($vmid, 1); };
6683 sub restore_tar_archive
{
6684 my ($archive, $vmid, $user, $opts) = @_;
6686 if ($archive ne '-') {
6687 my $firstfile = tar_archive_read_firstfile
($archive);
6688 die "ERROR: file '$archive' dos not lock like a QemuServer vzdump backup\n"
6689 if $firstfile ne 'qemu-server.conf';
6692 my $storecfg = PVE
::Storage
::config
();
6694 # destroy existing data - keep empty config
6695 my $vmcfgfn = PVE
::QemuConfig-
>config_file($vmid);
6696 destroy_vm
($storecfg, $vmid, 1) if -f
$vmcfgfn;
6698 my $tocmd = "/usr/lib/qemu-server/qmextract";
6700 $tocmd .= " --storage " . PVE
::Tools
::shellquote
($opts->{storage
}) if $opts->{storage
};
6701 $tocmd .= " --pool " . PVE
::Tools
::shellquote
($opts->{pool
}) if $opts->{pool
};
6702 $tocmd .= ' --prealloc' if $opts->{prealloc
};
6703 $tocmd .= ' --info' if $opts->{info
};
6705 # tar option "xf" does not autodetect compression when read from STDIN,
6706 # so we pipe to zcat
6707 my $cmd = "zcat -f|tar xf " . PVE
::Tools
::shellquote
($archive) . " " .
6708 PVE
::Tools
::shellquote
("--to-command=$tocmd");
6710 my $tmpdir = "/var/tmp/vzdumptmp$$";
6713 local $ENV{VZDUMP_TMPDIR
} = $tmpdir;
6714 local $ENV{VZDUMP_VMID
} = $vmid;
6715 local $ENV{VZDUMP_USER
} = $user;
6717 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
6718 my $tmpfn = "$conffile.$$.tmp";
6720 # disable interrupts (always do cleanups)
6724 local $SIG{HUP
} = sub { print STDERR
"got interrupt - ignored\n"; };
6732 local $SIG{PIPE
} = sub { die "interrupted by signal\n"; };
6734 if ($archive eq '-') {
6735 print "extracting archive from STDIN\n";
6736 run_command
($cmd, input
=> "<&STDIN");
6738 print "extracting archive '$archive'\n";
6742 return if $opts->{info
};
6746 my $statfile = "$tmpdir/qmrestore.stat";
6747 if (my $fd = IO
::File-
>new($statfile, "r")) {
6748 while (defined (my $line = <$fd>)) {
6749 if ($line =~ m/vzdump:([^\s:]*):(\S+)$/) {
6750 $map->{$1} = $2 if $1;
6752 print STDERR
"unable to parse line in statfile - $line\n";
6758 my $confsrc = "$tmpdir/qemu-server.conf";
6760 my $srcfd = new IO
::File
($confsrc, "r") ||
6761 die "unable to open file '$confsrc'\n";
6763 my $outfd = new IO
::File
($tmpfn, "w") ||
6764 die "unable to write config for VM $vmid\n";
6766 my $cookie = { netcount
=> 0 };
6767 while (defined (my $line = <$srcfd>)) {
6768 restore_update_config_line
($outfd, $cookie, $vmid, $map, $line, $opts->{unique
});
6780 tar_restore_cleanup
($storecfg, "$tmpdir/qmrestore.stat") if !$opts->{info
};
6787 rename $tmpfn, $conffile ||
6788 die "unable to commit configuration file '$conffile'\n";
6790 PVE
::Cluster
::cfs_update
(); # make sure we read new file
6792 eval { rescan
($vmid, 1); };
6796 sub foreach_storage_used_by_vm
{
6797 my ($conf, $func) = @_;
6801 foreach_drive
($conf, sub {
6802 my ($ds, $drive) = @_;
6803 return if drive_is_cdrom
($drive);
6805 my $volid = $drive->{file
};
6807 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
6808 $sidhash->{$sid} = $sid if $sid;
6811 foreach my $sid (sort keys %$sidhash) {
6816 sub do_snapshots_with_qemu
{
6817 my ($storecfg, $volid) = @_;
6819 my $storage_name = PVE
::Storage
::parse_volume_id
($volid);
6820 my $scfg = $storecfg->{ids
}->{$storage_name};
6822 if ($qemu_snap_storage->{$scfg->{type
}} && !$scfg->{krbd
}){
6826 if ($volid =~ m/\.(qcow2|qed)$/){
6833 sub qga_check_running
{
6834 my ($vmid, $nowarn) = @_;
6836 eval { vm_mon_cmd
($vmid, "guest-ping", timeout
=> 3); };
6838 warn "Qemu Guest Agent is not running - $@" if !$nowarn;
6844 sub template_create
{
6845 my ($vmid, $conf, $disk) = @_;
6847 my $storecfg = PVE
::Storage
::config
();
6849 foreach_drive
($conf, sub {
6850 my ($ds, $drive) = @_;
6852 return if drive_is_cdrom
($drive);
6853 return if $disk && $ds ne $disk;
6855 my $volid = $drive->{file
};
6856 return if !PVE
::Storage
::volume_has_feature
($storecfg, 'template', $volid);
6858 my $voliddst = PVE
::Storage
::vdisk_create_base
($storecfg, $volid);
6859 $drive->{file
} = $voliddst;
6860 $conf->{$ds} = print_drive
($vmid, $drive);
6861 PVE
::QemuConfig-
>write_config($vmid, $conf);
6865 sub convert_iscsi_path
{
6868 if ($path =~ m
|^iscsi
://([^/]+)/([^/]+)/(.+)$|) {
6873 my $initiator_name = get_initiator_name
();
6875 return "file.driver=iscsi,file.transport=tcp,file.initiator-name=$initiator_name,".
6876 "file.portal=$portal,file.target=$target,file.lun=$lun,driver=raw";
6879 die "cannot convert iscsi path '$path', unkown format\n";
6882 sub qemu_img_convert
{
6883 my ($src_volid, $dst_volid, $size, $snapname, $is_zero_initialized) = @_;
6885 my $storecfg = PVE
::Storage
::config
();
6886 my ($src_storeid, $src_volname) = PVE
::Storage
::parse_volume_id
($src_volid, 1);
6887 my ($dst_storeid, $dst_volname) = PVE
::Storage
::parse_volume_id
($dst_volid, 1);
6889 die "destination '$dst_volid' is not a valid volid form qemu-img convert\n" if !$dst_storeid;
6893 my $src_is_iscsi = 0;
6894 my $src_format = 'raw';
6897 PVE
::Storage
::activate_volumes
($storecfg, [$src_volid], $snapname);
6898 my $src_scfg = PVE
::Storage
::storage_config
($storecfg, $src_storeid);
6899 $src_format = qemu_img_format
($src_scfg, $src_volname);
6900 $src_path = PVE
::Storage
::path
($storecfg, $src_volid, $snapname);
6901 $src_is_iscsi = ($src_path =~ m
|^iscsi
://|);
6902 $cachemode = 'none' if $src_scfg->{type
} eq 'zfspool';
6903 } elsif (-f
$src_volid) {
6904 $src_path = $src_volid;
6905 if ($src_path =~ m/\.($QEMU_FORMAT_RE)$/) {
6910 die "source '$src_volid' is not a valid volid nor path for qemu-img convert\n" if !$src_path;
6912 my $dst_scfg = PVE
::Storage
::storage_config
($storecfg, $dst_storeid);
6913 my $dst_format = qemu_img_format
($dst_scfg, $dst_volname);
6914 my $dst_path = PVE
::Storage
::path
($storecfg, $dst_volid);
6915 my $dst_is_iscsi = ($dst_path =~ m
|^iscsi
://|);
6918 push @$cmd, '/usr/bin/qemu-img', 'convert', '-p', '-n';
6919 push @$cmd, '-l', "snapshot.name=$snapname" if($snapname && $src_format eq "qcow2");
6920 push @$cmd, '-t', 'none' if $dst_scfg->{type
} eq 'zfspool';
6921 push @$cmd, '-T', $cachemode if defined($cachemode);
6923 if ($src_is_iscsi) {
6924 push @$cmd, '--image-opts';
6925 $src_path = convert_iscsi_path
($src_path);
6927 push @$cmd, '-f', $src_format;
6930 if ($dst_is_iscsi) {
6931 push @$cmd, '--target-image-opts';
6932 $dst_path = convert_iscsi_path
($dst_path);
6934 push @$cmd, '-O', $dst_format;
6937 push @$cmd, $src_path;
6939 if (!$dst_is_iscsi && $is_zero_initialized) {
6940 push @$cmd, "zeroinit:$dst_path";
6942 push @$cmd, $dst_path;
6947 if($line =~ m/\((\S+)\/100\
%\)/){
6949 my $transferred = int($size * $percent / 100);
6950 my $remaining = $size - $transferred;
6952 print "transferred: $transferred bytes remaining: $remaining bytes total: $size bytes progression: $percent %\n";
6957 eval { run_command
($cmd, timeout
=> undef, outfunc
=> $parser); };
6959 die "copy failed: $err" if $err;
6962 sub qemu_img_format
{
6963 my ($scfg, $volname) = @_;
6965 if ($scfg->{path
} && $volname =~ m/\.($QEMU_FORMAT_RE)$/) {
6972 sub qemu_drive_mirror
{
6973 my ($vmid, $drive, $dst_volid, $vmiddst, $is_zero_initialized, $jobs, $skipcomplete, $qga, $bwlimit) = @_;
6975 $jobs = {} if !$jobs;
6979 $jobs->{"drive-$drive"} = {};
6981 if ($dst_volid =~ /^nbd:/) {
6982 $qemu_target = $dst_volid;
6985 my $storecfg = PVE
::Storage
::config
();
6986 my ($dst_storeid, $dst_volname) = PVE
::Storage
::parse_volume_id
($dst_volid);
6988 my $dst_scfg = PVE
::Storage
::storage_config
($storecfg, $dst_storeid);
6990 $format = qemu_img_format
($dst_scfg, $dst_volname);
6992 my $dst_path = PVE
::Storage
::path
($storecfg, $dst_volid);
6994 $qemu_target = $is_zero_initialized ?
"zeroinit:$dst_path" : $dst_path;
6997 my $opts = { timeout
=> 10, device
=> "drive-$drive", mode
=> "existing", sync
=> "full", target
=> $qemu_target };
6998 $opts->{format
} = $format if $format;
7000 if (defined($bwlimit)) {
7001 $opts->{speed
} = $bwlimit * 1024;
7002 print "drive mirror is starting for drive-$drive with bandwidth limit: ${bwlimit} KB/s\n";
7004 print "drive mirror is starting for drive-$drive\n";
7007 # if a job already runs for this device we get an error, catch it for cleanup
7008 eval { vm_mon_cmd
($vmid, "drive-mirror", %$opts); };
7010 eval { PVE
::QemuServer
::qemu_blockjobs_cancel
($vmid, $jobs) };
7012 die "mirroring error: $err\n";
7015 qemu_drive_mirror_monitor
($vmid, $vmiddst, $jobs, $skipcomplete, $qga);
7018 sub qemu_drive_mirror_monitor
{
7019 my ($vmid, $vmiddst, $jobs, $skipcomplete, $qga) = @_;
7022 my $err_complete = 0;
7025 die "storage migration timed out\n" if $err_complete > 300;
7027 my $stats = vm_mon_cmd
($vmid, "query-block-jobs");
7029 my $running_mirror_jobs = {};
7030 foreach my $stat (@$stats) {
7031 next if $stat->{type
} ne 'mirror';
7032 $running_mirror_jobs->{$stat->{device
}} = $stat;
7035 my $readycounter = 0;
7037 foreach my $job (keys %$jobs) {
7039 if(defined($jobs->{$job}->{complete
}) && !defined($running_mirror_jobs->{$job})) {
7040 print "$job : finished\n";
7041 delete $jobs->{$job};
7045 die "$job: mirroring has been cancelled\n" if !defined($running_mirror_jobs->{$job});
7047 my $busy = $running_mirror_jobs->{$job}->{busy
};
7048 my $ready = $running_mirror_jobs->{$job}->{ready
};
7049 if (my $total = $running_mirror_jobs->{$job}->{len
}) {
7050 my $transferred = $running_mirror_jobs->{$job}->{offset
} || 0;
7051 my $remaining = $total - $transferred;
7052 my $percent = sprintf "%.2f", ($transferred * 100 / $total);
7054 print "$job: transferred: $transferred bytes remaining: $remaining bytes total: $total bytes progression: $percent % busy: $busy ready: $ready \n";
7057 $readycounter++ if $running_mirror_jobs->{$job}->{ready
};
7060 last if scalar(keys %$jobs) == 0;
7062 if ($readycounter == scalar(keys %$jobs)) {
7063 print "all mirroring jobs are ready \n";
7064 last if $skipcomplete; #do the complete later
7066 if ($vmiddst && $vmiddst != $vmid) {
7067 my $agent_running = $qga && qga_check_running
($vmid);
7068 if ($agent_running) {
7069 print "freeze filesystem\n";
7070 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
7072 print "suspend vm\n";
7073 eval { PVE
::QemuServer
::vm_suspend
($vmid, 1); };
7076 # if we clone a disk for a new target vm, we don't switch the disk
7077 PVE
::QemuServer
::qemu_blockjobs_cancel
($vmid, $jobs);
7079 if ($agent_running) {
7080 print "unfreeze filesystem\n";
7081 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
7083 print "resume vm\n";
7084 eval { PVE
::QemuServer
::vm_resume
($vmid, 1, 1); };
7090 foreach my $job (keys %$jobs) {
7091 # try to switch the disk if source and destination are on the same guest
7092 print "$job: Completing block job...\n";
7094 eval { vm_mon_cmd
($vmid, "block-job-complete", device
=> $job) };
7095 if ($@ =~ m/cannot be completed/) {
7096 print "$job: Block job cannot be completed, try again.\n";
7099 print "$job: Completed successfully.\n";
7100 $jobs->{$job}->{complete
} = 1;
7111 eval { PVE
::QemuServer
::qemu_blockjobs_cancel
($vmid, $jobs) };
7112 die "mirroring error: $err";
7117 sub qemu_blockjobs_cancel
{
7118 my ($vmid, $jobs) = @_;
7120 foreach my $job (keys %$jobs) {
7121 print "$job: Cancelling block job\n";
7122 eval { vm_mon_cmd
($vmid, "block-job-cancel", device
=> $job); };
7123 $jobs->{$job}->{cancel
} = 1;
7127 my $stats = vm_mon_cmd
($vmid, "query-block-jobs");
7129 my $running_jobs = {};
7130 foreach my $stat (@$stats) {
7131 $running_jobs->{$stat->{device
}} = $stat;
7134 foreach my $job (keys %$jobs) {
7136 if (defined($jobs->{$job}->{cancel
}) && !defined($running_jobs->{$job})) {
7137 print "$job: Done.\n";
7138 delete $jobs->{$job};
7142 last if scalar(keys %$jobs) == 0;
7149 my ($storecfg, $vmid, $running, $drivename, $drive, $snapname,
7150 $newvmid, $storage, $format, $full, $newvollist, $jobs, $skipcomplete, $qga, $bwlimit) = @_;
7155 print "create linked clone of drive $drivename ($drive->{file})\n";
7156 $newvolid = PVE
::Storage
::vdisk_clone
($storecfg, $drive->{file
}, $newvmid, $snapname);
7157 push @$newvollist, $newvolid;
7160 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($drive->{file
});
7161 $storeid = $storage if $storage;
7163 my $dst_format = resolve_dst_disk_format
($storecfg, $storeid, $volname, $format);
7164 my ($size) = PVE
::Storage
::volume_size_info
($storecfg, $drive->{file
}, 3);
7166 print "create full clone of drive $drivename ($drive->{file})\n";
7168 $newvolid = PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $newvmid, $dst_format, $name, ($size/1024));
7169 push @$newvollist, $newvolid;
7171 PVE
::Storage
::activate_volumes
($storecfg, [$newvolid]);
7173 my $sparseinit = PVE
::Storage
::volume_has_feature
($storecfg, 'sparseinit', $newvolid);
7174 if (!$running || $snapname) {
7175 # TODO: handle bwlimits
7176 qemu_img_convert
($drive->{file
}, $newvolid, $size, $snapname, $sparseinit);
7179 my $kvmver = get_running_qemu_version
($vmid);
7180 if (!qemu_machine_feature_enabled
(undef, $kvmver, 2, 7)) {
7181 die "drive-mirror with iothread requires qemu version 2.7 or higher\n"
7182 if $drive->{iothread
};
7185 qemu_drive_mirror
($vmid, $drivename, $newvolid, $newvmid, $sparseinit, $jobs, $skipcomplete, $qga, $bwlimit);
7189 my ($size) = PVE
::Storage
::volume_size_info
($storecfg, $newvolid, 3);
7192 $disk->{format
} = undef;
7193 $disk->{file
} = $newvolid;
7194 $disk->{size
} = $size;
7199 # this only works if VM is running
7200 sub get_current_qemu_machine
{
7203 my $cmd = { execute
=> 'query-machines', arguments
=> {} };
7204 my $res = vm_qmp_command
($vmid, $cmd);
7206 my ($current, $default);
7207 foreach my $e (@$res) {
7208 $default = $e->{name
} if $e->{'is-default'};
7209 $current = $e->{name
} if $e->{'is-current'};
7212 # fallback to the default machine if current is not supported by qemu
7213 return $current || $default || 'pc';
7216 sub get_running_qemu_version
{
7218 my $cmd = { execute
=> 'query-version', arguments
=> {} };
7219 my $res = vm_qmp_command
($vmid, $cmd);
7220 return "$res->{qemu}->{major}.$res->{qemu}->{minor}";
7223 sub qemu_machine_feature_enabled
{
7224 my ($machine, $kvmver, $version_major, $version_minor) = @_;
7229 if ($machine && $machine =~ m/^((?:pc(-i440fx|-q35)?|virt)-(\d+)\.(\d+))/) {
7231 $current_major = $3;
7232 $current_minor = $4;
7234 } elsif ($kvmver =~ m/^(\d+)\.(\d+)/) {
7236 $current_major = $1;
7237 $current_minor = $2;
7240 return 1 if $current_major > $version_major ||
7241 ($current_major == $version_major &&
7242 $current_minor >= $version_minor);
7245 sub qemu_machine_pxe
{
7246 my ($vmid, $conf) = @_;
7248 my $machine = PVE
::QemuServer
::get_current_qemu_machine
($vmid);
7250 if ($conf->{machine
} && $conf->{machine
} =~ m/\.pxe$/) {
7257 sub qemu_use_old_bios_files
{
7258 my ($machine_type) = @_;
7260 return if !$machine_type;
7262 my $use_old_bios_files = undef;
7264 if ($machine_type =~ m/^(\S+)\.pxe$/) {
7266 $use_old_bios_files = 1;
7268 my $kvmver = kvm_user_version
();
7269 # Note: kvm version < 2.4 use non-efi pxe files, and have problems when we
7270 # load new efi bios files on migration. So this hack is required to allow
7271 # live migration from qemu-2.2 to qemu-2.4, which is sometimes used when
7272 # updrading from proxmox-ve-3.X to proxmox-ve 4.0
7273 $use_old_bios_files = !qemu_machine_feature_enabled
($machine_type, $kvmver, 2, 4);
7276 return ($use_old_bios_files, $machine_type);
7279 sub create_efidisk
($$$$$) {
7280 my ($storecfg, $storeid, $vmid, $fmt, $arch) = @_;
7282 my (undef, $ovmf_vars) = get_ovmf_files
($arch);
7283 die "EFI vars default image not found\n" if ! -f
$ovmf_vars;
7285 my $vars_size_b = -s
$ovmf_vars;
7286 my $vars_size = PVE
::Tools
::convert_size
($vars_size_b, 'b' => 'kb');
7287 my $volid = PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid, $fmt, undef, $vars_size);
7288 PVE
::Storage
::activate_volumes
($storecfg, [$volid]);
7290 qemu_img_convert
($ovmf_vars, $volid, $vars_size_b, undef, 0);
7292 return ($volid, $vars_size);
7295 sub vm_iothreads_list
{
7298 my $res = vm_mon_cmd
($vmid, 'query-iothreads');
7301 foreach my $iothread (@$res) {
7302 $iothreads->{ $iothread->{id
} } = $iothread->{"thread-id"};
7309 my ($conf, $drive) = @_;
7313 if (!$conf->{scsihw
} || ($conf->{scsihw
} =~ m/^lsi/)) {
7315 } elsif ($conf->{scsihw
} && ($conf->{scsihw
} eq 'virtio-scsi-single')) {
7321 my $controller = int($drive->{index} / $maxdev);
7322 my $controller_prefix = ($conf->{scsihw
} && $conf->{scsihw
} eq 'virtio-scsi-single') ?
"virtioscsi" : "scsihw";
7324 return ($maxdev, $controller, $controller_prefix);
7327 sub add_hyperv_enlightenments
{
7328 my ($cpuFlags, $winversion, $machine_type, $kvmver, $bios, $gpu_passthrough, $hv_vendor_id) = @_;
7330 return if $winversion < 6;
7331 return if $bios && $bios eq 'ovmf' && $winversion < 8;
7333 if ($gpu_passthrough || defined($hv_vendor_id)) {
7334 $hv_vendor_id //= 'proxmox';
7335 push @$cpuFlags , "hv_vendor_id=$hv_vendor_id";
7338 if (qemu_machine_feature_enabled
($machine_type, $kvmver, 2, 3)) {
7339 push @$cpuFlags , 'hv_spinlocks=0x1fff';
7340 push @$cpuFlags , 'hv_vapic';
7341 push @$cpuFlags , 'hv_time';
7343 push @$cpuFlags , 'hv_spinlocks=0xffff';
7346 if (qemu_machine_feature_enabled
($machine_type, $kvmver, 2, 6)) {
7347 push @$cpuFlags , 'hv_reset';
7348 push @$cpuFlags , 'hv_vpindex';
7349 push @$cpuFlags , 'hv_runtime';
7352 if ($winversion >= 7) {
7353 push @$cpuFlags , 'hv_relaxed';
7355 if (qemu_machine_feature_enabled
($machine_type, $kvmver, 2, 12)) {
7356 push @$cpuFlags , 'hv_synic';
7357 push @$cpuFlags , 'hv_stimer';
7360 if (qemu_machine_feature_enabled
($machine_type, $kvmver, 3, 1)) {
7361 push @$cpuFlags , 'hv_ipi';
7366 sub windows_version
{
7369 return 0 if !$ostype;
7373 if($ostype eq 'wxp' || $ostype eq 'w2k3' || $ostype eq 'w2k') {
7375 } elsif($ostype eq 'w2k8' || $ostype eq 'wvista') {
7377 } elsif ($ostype =~ m/^win(\d+)$/) {
7384 sub resolve_dst_disk_format
{
7385 my ($storecfg, $storeid, $src_volname, $format) = @_;
7386 my ($defFormat, $validFormats) = PVE
::Storage
::storage_default_format
($storecfg, $storeid);
7389 # if no target format is specified, use the source disk format as hint
7391 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
7392 $format = qemu_img_format
($scfg, $src_volname);
7398 # test if requested format is supported - else use default
7399 my $supported = grep { $_ eq $format } @$validFormats;
7400 $format = $defFormat if !$supported;
7404 sub resolve_first_disk
{
7406 my @disks = PVE
::QemuServer
::valid_drive_names
();
7408 foreach my $ds (reverse @disks) {
7409 next if !$conf->{$ds};
7410 my $disk = PVE
::QemuServer
::parse_drive
($ds, $conf->{$ds});
7411 next if PVE
::QemuServer
::drive_is_cdrom
($disk);
7418 my ($uuid, $uuid_str);
7419 UUID
::generate
($uuid);
7420 UUID
::unparse
($uuid, $uuid_str);
7424 sub generate_smbios1_uuid
{
7425 return "uuid=".generate_uuid
();
7431 vm_mon_cmd
($vmid, 'nbd-server-stop');
7434 sub create_reboot_request
{
7436 open(my $fh, '>', "/run/qemu-server/$vmid.reboot")
7437 or die "failed to create reboot trigger file: $!\n";
7441 sub clear_reboot_request
{
7443 my $path = "/run/qemu-server/$vmid.reboot";
7446 $res = unlink($path);
7447 die "could not remove reboot request for $vmid: $!"
7448 if !$res && $! != POSIX
::ENOENT
;
7453 # bash completion helper
7455 sub complete_backup_archives
{
7456 my ($cmdname, $pname, $cvalue) = @_;
7458 my $cfg = PVE
::Storage
::config
();
7462 if ($cvalue =~ m/^([^:]+):/) {
7466 my $data = PVE
::Storage
::template_list
($cfg, $storeid, 'backup');
7469 foreach my $id (keys %$data) {
7470 foreach my $item (@{$data->{$id}}) {
7471 next if $item->{format
} !~ m/^vma\.(gz|lzo)$/;
7472 push @$res, $item->{volid
} if defined($item->{volid
});
7479 my $complete_vmid_full = sub {
7482 my $idlist = vmstatus
();
7486 foreach my $id (keys %$idlist) {
7487 my $d = $idlist->{$id};
7488 if (defined($running)) {
7489 next if $d->{template
};
7490 next if $running && $d->{status
} ne 'running';
7491 next if !$running && $d->{status
} eq 'running';
7500 return &$complete_vmid_full();
7503 sub complete_vmid_stopped
{
7504 return &$complete_vmid_full(0);
7507 sub complete_vmid_running
{
7508 return &$complete_vmid_full(1);
7511 sub complete_storage
{
7513 my $cfg = PVE
::Storage
::config
();
7514 my $ids = $cfg->{ids
};
7517 foreach my $sid (keys %$ids) {
7518 next if !PVE
::Storage
::storage_check_enabled
($cfg, $sid, undef, 1);
7519 next if !$ids->{$sid}->{content
}->{images
};