]>
Commit | Line | Data |
---|---|---|
3392d6ca SR |
1 | package PVE::QemuServer::Machine; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
2ea5fb7e | 6 | use PVE::QemuServer::Helpers; |
3392d6ca SR |
7 | use PVE::QemuServer::Monitor; |
8 | ||
9471e48b TL |
9 | # Bump this for VM HW layout changes during a release (where the QEMU machine |
10 | # version stays the same) | |
11 | our $PVE_MACHINE_VERSION = 1; | |
12 | ||
3392d6ca SR |
13 | sub machine_type_is_q35 { |
14 | my ($conf) = @_; | |
15 | ||
16 | return $conf->{machine} && ($conf->{machine} =~ m/q35/) ? 1 : 0; | |
17 | } | |
18 | ||
19 | # this only works if VM is running | |
20 | sub get_current_qemu_machine { | |
21 | my ($vmid) = @_; | |
22 | ||
23 | my $res = PVE::QemuServer::Monitor::mon_cmd($vmid, 'query-machines'); | |
24 | ||
9471e48b | 25 | my ($current, $pve_version, $default); |
3392d6ca SR |
26 | foreach my $e (@$res) { |
27 | $default = $e->{name} if $e->{'is-default'}; | |
28 | $current = $e->{name} if $e->{'is-current'}; | |
9471e48b | 29 | $pve_version = $e->{'pve-version'} if $e->{'pve-version'}; |
3392d6ca SR |
30 | } |
31 | ||
9471e48b TL |
32 | $current .= "+$pve_version" if $current && $pve_version; |
33 | ||
3392d6ca SR |
34 | # fallback to the default machine if current is not supported by qemu |
35 | return $current || $default || 'pc'; | |
36 | } | |
37 | ||
9471e48b TL |
38 | # returns a string with major.minor+pve<VERSION>, patch version-part is ignored |
39 | # as it's seldom ressembling a real QEMU machine type, so it would be '0' 99% of | |
40 | # the time anyway.. This explicitly separates pveversion from the machine. | |
2ea5fb7e | 41 | sub extract_version { |
9471e48b TL |
42 | my ($machine_type, $kvmversion) = @_; |
43 | ||
44 | if (defined($machine_type) && $machine_type =~ m/^(?:pc(?:-i440fx|-q35)?|virt)-(\d+)\.(\d+)(?:\.(\d+))?(\+pve\d+)?/) { | |
45 | my $versionstr = "$1.$2"; | |
46 | $versionstr .= $4 if $4; | |
47 | return $versionstr; | |
48 | } elsif (defined($kvmversion)) { | |
49 | if ($kvmversion =~ m/^(\d+)\.(\d+)/) { | |
50 | return "$1.$2+pve$PVE_MACHINE_VERSION"; | |
51 | } | |
3392d6ca SR |
52 | } |
53 | ||
2ea5fb7e | 54 | return undef; |
3392d6ca SR |
55 | } |
56 | ||
2ea5fb7e | 57 | sub machine_version { |
9471e48b | 58 | my ($machine_type, $major, $minor, $pve) = @_; |
3392d6ca | 59 | |
2ea5fb7e | 60 | return PVE::QemuServer::Helpers::min_version( |
9471e48b | 61 | extract_version($machine_type), $major, $minor, $pve); |
3392d6ca SR |
62 | } |
63 | ||
64 | # dies if a) VM not running or not exisiting b) Version query failed | |
65 | # So, any defined return value is valid, any invalid state can be caught by eval | |
66 | sub runs_at_least_qemu_version { | |
67 | my ($vmid, $major, $minor, $extra) = @_; | |
68 | ||
69 | my $v = PVE::QemuServer::Monitor::mon_cmd($vmid, 'query-version'); | |
70 | die "could not query currently running version for VM $vmid\n" if !defined($v); | |
71 | $v = $v->{qemu}; | |
72 | ||
825ae5bc | 73 | return PVE::QemuServer::Helpers::version_cmp($v->{major}, $major, $v->{minor}, $minor, $v->{micro}, $extra) >= 0; |
3392d6ca SR |
74 | } |
75 | ||
76 | sub qemu_machine_pxe { | |
77 | my ($vmid, $conf) = @_; | |
78 | ||
79 | my $machine = get_current_qemu_machine($vmid); | |
80 | ||
81 | if ($conf->{machine} && $conf->{machine} =~ m/\.pxe$/) { | |
82 | $machine .= '.pxe'; | |
83 | } | |
84 | ||
85 | return $machine; | |
86 | } | |
87 | ||
88 | 1; |