47 Commits

Author SHA1 Message Date
Richard W.M. Jones
503e9fde5a lib: direct: Remove assumptions that g->hv != NULL
In a following commit, g->hv may now be NULL if the user does not
explicitly either call guestfs_set_hv or set LIBGUESTFS_HV.

There were multiple places in the code that assumed g->hv was not
NULL, so they were all revised so that now g->hv is only not NULL if
set by the user and (in the normal case) is NULL meaning "use the
default hypervisor".

For the direct backend we call get_default_hv_direct early on and set
data->qemu in the handle.  We need to modify the KVM test so that the
current qemu binary is passed along.

We also have to change execve to execvpe since the path to qemu is not
absolute.
2026-03-17 19:07:32 +00:00
shivanayak
a47c923b05 lib: fix memory leak of query_kvm in guestfs_int_platform_has_kvm (#316)
The string allocated by generic_qmp_test via safe_strdup is passed to
parse_has_kvm but never freed afterwards, leaking memory on every KVM
capability check. Use CLEANUP_FREE to ensure automatic cleanup.

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-09 17:22:27 +00:00
Hilko Bengen
5a4977fc66 lib/qemu.c: Dump qemu's stderr if QMP test fails
Qemu will refuse to start on some (all?) architectures if firmware
files are missing, or for other reasons, and complain to stderr. Those
messages should be made visible to the user as part of the error message.
2026-02-17 07:05:56 +00:00
Richard W.M. Jones
f7a24b2ea8 lib/qemu.c: Use machine type none when inspecting QMP properties
Dan mentioned that there is a special machine type ("none") we can use
when querying for KVM.  It has no CPU, memory, etc and does not run,
but you can still enable KVM for it.

Note we have to remove the -cpu parameter, otherwise qemu prints this
error:

  qemu-kvm: apic-id property was not initialized properly

Updates: commit 5da8102f5f
Suggested-by: Daniel P. Berrangé <berrange@redhat.com>
2026-01-26 14:32:30 +00:00
Richard W.M. Jones
5da8102f5f lib/qemu.c: Don't start the guest when checking QMP properties
When checking for QMP properties, we run a qemu command and interact
with the QMP console.  However we also start the guest running (there
is no actual guest in this situation).  This occasionally causes this
line to be printed:

  libguestfs: generic_qmp_test: 3: {"timestamp": {"seconds": 1768823946, "microseconds": 898287}, "event": "GUEST_PANICKED", "data": {"action": "poweroff", "info": {"core": 0, "psw-addr": 0, "reason": "disabled-wait", "psw-mask": 562956395872256, "type": "s390"}}}\r\n

which confuses our parser.

As there is no reason to start the non-existent guest, add the -S
option which causes qemu to start up in a paused state.

For some reason this only happens on s390x but I think it could happen
on all architectures, so it may be a timing issue or something
particular about s390x firmware.
2026-01-19 12:03:59 +00:00
Richard W.M. Jones
bbbc982bf5 lib/qemu.c: Add debugging to generic_qmp_test()
This function fails sometimes on s390x, but it's hard to tell what is
going on because of insufficient debugging.
2026-01-19 10:27:24 +00:00
Richard W.M. Jones
0fe8c0492c lib/qemu.c: Turn debug messages which are really errors into error()
Commit 669eda1e24 ("lib/launch-direct.c: Simplify test for KVM, remove
qemu caching") made it so that failure of generic_qmp_test() will
cause launch to fail.  However we still used debug messages to print
the error, so unless you have debugging enabled you wouldn't see any
error message if this function fails.

Updates: commit 669eda1e24
2026-01-19 10:20:55 +00:00
Richard W.M. Jones
ac1cb3cd7c lib/qemu.c: Add -cpu max when testing for KVM via QMP
On RHEL 10.2 aarch64 (only) we see:

  $ echo '{ "execute": "qmp_capabilities" }' '{ "execute": "query-kvm" }' '{ "execute": "quit" }'  | QEMU_AUDIO_DRV=none "/usr/libexec/qemu-kvm" -display none -machine "virt,accel=kvm:hvf:tcg" -qmp stdio
  qemu-kvm: invalid accelerator hvf
  qemu-kvm: falling back to KVM
  {"QMP": {"version": {"qemu": {"micro": 0, "minor": 1, "major": 9}, "package": "qemu-kvm-9.1.0-15.el10_0.4"}, "capabilities": ["oob"]}}
  qemu-kvm: warning: CPU model cortex-a57-arm-cpu is deprecated -- use 'host' / 'max'
  qemu-kvm: kvm_init_vcpu: kvm_arch_init_vcpu failed (0): Invalid argument

Unfortunately we cannot use guestfs_int_get_cpu_model (as that
requires us to already know if KVM is supported), so we just have to
guess that -cpu max will work, at least enough for QMP to work.

Fixes: https://issues.redhat.com/browse/RHEL-121076
Reported-by: Xiang Hua Chen
2025-10-17 09:57:14 +01:00
Richard W.M. Jones
669eda1e24 lib/launch-direct.c: Simplify test for KVM, remove qemu caching
Previously we tested if KVM was available, and cached that for the
qemu binary.  I think this was actually wrong.  For example, if the
machine restarts, then the cache is still around, but KVM might be
enabled or disabled because of a new host kernel.

In any case, let's radically simplify this.

Test for KVM on each run.  Consequently we can remove all the qemu
test caching stuff as it is no longer used anywhere.

I also tightened up the code that runs the QMP query-kvm command, so
now any unexpected output will cause a runtime failure.  This command
ought to work, and if it breaks we ought to know about it and fix it.
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
cfbdf9bcc7 lib/qemu.c: Remove guestfs_int_qemu_supports_device
Previously we ran 'qemu -device ?' to list devices, using the output
to test if qemu supported particular devices.  This was extensively
used in the past, but after recent commits there are no further users
of this code, so we can remove it all completely.
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
1a45db7465 lib/qemu.c: Remove qemu -help invocation
Previously we parsed qemu -help output to get the version of qemu and
to test for other features.  After prior commits, this is no longer
done, so we can remove the qemu -help invocation and associated code
completely.
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
593a382840 lib: Remove guestfs_int_qemu_version
This function is now only used in one place, to print the version of
qemu in direct mode, when debugging is enabled.

Remove this function and replace with a direct command invocation
('qemu --version').  We only need to run this command when debugging
is enabled, and we copy all of the output to the debug channel.

I have made the assumption here that qemu -version does not try to
create a display device.  (The previous invocation of qemu -help
actually ran 'qemu -display none -help' indicating that this was not
always the case.)

This is actually an improvement on before, since now we get to see the
full output of 'qemu --version'.  The new output looks like:

  libguestfs: begin testing qemu features
  libguestfs: command: run: /usr/bin/qemu-kvm
  libguestfs: command: run: \ -version
  libguestfs: qemu: QEMU emulator version 10.1.0 (qemu-10.1.0-8.fc44)
  libguestfs: qemu: Copyright (c) 2003-2025 Fabrice Bellard and the QEMU Project developers
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
02d82a3c3a lib: Remove qemu version when testing for discard feature
We can assume much more recent qemu, so we don't need to have special
cases for qemu 1.5/1.6.
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
5df7da7e81 lib/qemu.c: Remove int_qemu_supports function
This function, which grepped the qemu -help output to see if a command
line option was supported, is no longer used and can be removed.
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
1f1ac69b11 lib/qemu.c: Remove qmp_schema
We used to read the QMP schema so we could see if the binary supported
qemu mandatory locking.  However after commit 47857751a7 ("lib:
direct: Remove test for qemu mandatory locking") this is dead code, so
remove it.

Updates: commit 47857751a7
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
72cfaff5c5 Update copyright dates for 2025
Automated using this command:

perl -pi.bak -e 's/(20[012][0-9])-20[12][01234]/$1-2025/g' `git ls-files`
2025-02-16 17:00:46 +00:00
Richard W.M. Jones
9e3c1f44ca lib/qemu.c: Replace jansson with json-c 2024-10-22 16:44:53 +01:00
Richard W.M. Jones
47857751a7 lib: direct: Remove test for qemu mandatory locking
We tested for QEMU >= 2.10 support for mandatory locking.  I believe
this is for all practical purposes always enabled now (and qemu 2.10
is ancient history) so simply assume it's true always.
2024-10-22 16:23:07 +01:00
Richard W.M. Jones
e37768d889 build: Assume __attribute__((cleanup)) always works
All recent compilers support this (except MS compilers which we don't
care about).  Assume it is supported.  We test it in ./configure and
hard fail if it doesn't work.

We still define HAVE_ATTRIBUTE_CLEANUP but you can now assume it is
always defined and don't have to check it.
2024-10-18 21:55:21 +01:00
Antonio Caggiano
ad3ccf42ab qemu: Add HVF to the accelerators list.
This speeds up greatly QEMU when running on MacOS.

Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com>
2024-08-12 11:28:50 +01:00
Richard W.M. Jones
8b3e8a9056 Remove tftp drive support
This was only theoretically supported, via curl.  It's unlikely that
it really worked as it was never tested.

If needed it's better to use nbdkit-curl-plugin instead (this applies
to http and ftp as well).
2024-06-27 16:27:06 +01:00
Richard W.M. Jones
b1db7847ee Remove sheepdog support
This was discontinued in qemu quite a long time ago.
2024-06-27 16:22:52 +01:00
Richard W.M. Jones
c080449511 Remove gluster support
Development on gluster has stopped upstream, see:

https://marc.info/?l=fedora-devel-list&m=171934833215726&w=2
2024-06-27 16:13:09 +01:00
Richard W.M. Jones
e2c7bddf10 Update copyright dates for 2023
Run this command across the source:

  perl -pi.bak -e 's/(20[012][0-9])-20[12][012]/$1-2023/g' `git ls-files`

and remove changes to po{,-docs}/*.po{,t} (these will be regenerated
later when we run 'make dist').
2023-02-07 10:50:48 +00:00
Richard W.M. Jones
347e875911 lib: Remove -Wanalyzer-file-leak ignores
These were added for GCC 11.  The problem has been fixed in GCC 12.
On macOS (clang) these produced errors like this:

tsk.c:75:32: error: unknown warning group '-Wanalyzer-file-leak', ignored [-Werror,-Wunknown-warning-option]
                               ^
2022-08-16 15:42:30 +01:00
Richard W.M. Jones
fdda111e0e lib/qemu.c: Suppress another bogus -fanalyser warning. 2021-01-28 12:27:41 +00:00
Richard W.M. Jones
0e17236d7d Update copyright dates to 2020. 2020-03-06 19:32:32 +00:00
Richard W.M. Jones
e047cc4da8 lib: qemu: rbd: Properly escape IPv6 addresses.
Each ':' character in the address must be escaped from qemu.

Thanks: Jonathan Wright
2019-04-01 17:31:25 +01:00
Richard W.M. Jones
ba7fa03923 lib: direct: Set QEMU_AUDIO_DRV=none when testing schema.
Schema parsing was failing with errors such as:

libguestfs: QMP parse error: '[' or '{' expected near end of file (ignored)

This happened because the QMP command was actually completely failing
and never printing a result at all.  This happens because the qemu
audio driver can't be set up without a console.  We can suppress this
by setting the environment variable QEMU_AUDIO_DRV=none, which is the
same thing that libvirt does, and also the same thing that we are
already doing when launching the real appliance subprocess.

See also: https://bugzilla.redhat.com/show_bug.cgi?id=1692047
2019-03-27 11:53:31 +00:00
Richard W.M. Jones
05d4fcb64d Update copyright dates for 2019.
This command run over the source:

perl -pi.bak -e 's/(20[01][0-9])-2018/$1-2019/g' `git ls-files`
2019-01-08 11:58:30 +00:00
Richard W.M. Jones
6f605a0804 lib: direct: Query qemu binary for availability of KVM (RHBZ#1605071).
When using the direct backend, you should see the result of testing
the qemu binary for the availability of KVM:

  libguestfs: qemu KVM: enabled

Thanks: Andrea Bolognani.
2018-09-21 10:56:25 +01:00
Richard W.M. Jones
e79286f717 qemu: Fix transcription error in conversion of yajl to jansson.
This broke qemu mandatory locking detection.

Fixes commit bd1c5c9f4d.
2018-03-29 20:19:53 +01:00
Pino Toscano
491970b82f lib: qemu: help GCC 8 by break'ing a case in a switch
GCC 8 thinks that the case drive_protocol_gluster may fall through, most
probably because the only code is a switch case that handles the
elements of an enum, and thus letting other fall through. In reality
this ought to not happen at all, so help GCC by break'ing the case,
which will then lead to the abort() at the end of
guestfs_int_drive_source_qemu_param.
2018-02-19 17:14:18 +01:00
Pino Toscano
bd1c5c9f4d Switch from YAJL to Jansson
While YAJL mostly works fine, it did not see any active development in
the last 3 years.  OTOH, Jansson is another JSON C implementation, with
a very liberal license, and a much nicer API.

Hence, switch all of libguestfs from YAJL to Jansson:
- configure checks, and buildsystem in general
- packages pulled in the appliance
- actual implementations
- contrib scripts
- documentation

This also makes use of the better APIs available (e.g. json_object_get,
json_array_foreach, and json_object_foreach).  This does not change the
API of our OCaml Yajl module.
2018-02-12 11:24:06 +01:00
Richard W.M. Jones
212762c593 Update copyright dates for 2018.
Run the following command over the source:

  perl -pi.bak -e 's/(20[01][0-9])-2017/$1-2018/g' `git ls-files`
2018-01-04 15:30:10 +00:00
Pino Toscano
b06e7aa47d lib: qemu: ignore -Wnonnull also for GCC 4.8 and up
Versions older than 6 that have -Wnonnull need the flag too.

Fixes commit e2773e679f.
2017-09-15 17:49:57 +02:00
Richard W.M. Jones
e2773e679f lib: qemu: Also suppress -Wnonnull warning in yajl code.
Required by gcc-7.1.1.

Updates commit 3d2b84231f.
2017-09-14 19:29:59 +01:00
Richard W.M. Jones
3d2b84231f lib: qemu: Add accessor to test if qemu does mandatory locking.
QEMU >= 2.10 started to do mandatory locking.  This checks the QMP
schema to see if we are using that version of qemu.  (Note it is
sometimes disabled in downstream builds, and it was also enabled in
upstream prereleases with version 2.9.9x, so we cannot just check the
version number).
2017-09-14 16:58:17 +01:00
Richard W.M. Jones
bf7d627305 lib: qemu: Allow parallel qemu binaries to be used with cache conflicts.
Rename the cache files like ‘qemu.stat’ etc so they include the qemu
binary "key" (ie. size and mtime) in the name.  This allows a single
user to use multiple qemu binaries in parallel without conflicts.
2017-09-14 16:58:16 +01:00
Richard W.M. Jones
a22eecbdb1 lib: qemu: Run QMP ‘query-qmp-schema’ against the qemu binary.
This adds an extra test using QMP (the QEMU Monitor Protocol).  This
allows us to get extra information about the qemu binary beyond what
is available from the version number or ‘qemu -help’.
2017-09-12 17:13:41 +01:00
Richard W.M. Jones
ea94f39e46 lib: qemu: Factor out common code for reading and writing cache files.
The previous code duplicated a lot of common code for reading and
writing the cache file per data field.  This change simply factors out
that common code.  This makes it simpler to add new tests in future.

This is just refactoring, it should have no effect.
2017-09-12 17:13:41 +01:00
Richard W.M. Jones
0eb1380ddc lib: qemu: Refactor guestfs_int_test_qemu so it doesn't return qemu version.
Rather unnecessarily this function returned the parsed qemu version.
This complicates further refactoring, so I have changed the function
not to return this, and instead there is a separate function you have
to call to get the version struct (‘guestfs_int_qemu_version’).

Apart from a tiny amount of extra copying this is simply refactoring
of the interface between the direct-mode backend and the qemu query
functions.
2017-09-12 10:57:32 +01:00
Richard W.M. Jones
0071a6e146 launch: direct: Reimplement command line handling using qemuopts library. 2017-05-08 11:14:46 +01:00
Richard W.M. Jones
9e0294f88f lib: direct: Remove support for virtio-blk as the default.
virtio-scsi has been supported in qemu since 2012, and it is superior
in every respect to virtio-blk.  There's no reason to still be using
virtio-blk.

virtio-scsi support was initially added in 2012
(commit 0c0a7d0d86).

You can still use virtio-blk using the (deprecated) iface parameter,
but don't do that in new code.
2017-04-20 14:38:08 +01:00
Richard W.M. Jones
ee206d7ba8 Use Unicode single quotes ‘’ in place of short single quoted strings throughout.
Only in end-user messages and documentation.  This change was done
mostly mechanically using the Perl script attached below.

I also changed don't -> don’t etc and made some other simple fixes.

See also: https://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html

----------
 #!/usr/bin/perl -w

use strict;
use Locale::PO;

my $re = qr{'([-\w%.,=?*/]+)'};

my %files = ();

foreach my $filename ("po/libguestfs.pot", "po-docs/libguestfs-docs.pot") {
    my $poref = Locale::PO->load_file_asarray($filename);

    foreach my $po (@$poref) {
        if ($po->msgid =~ $re) {
            my @refs = split /\s+/, $po->reference;
            foreach my $ref (@refs) {
                my ($file, $lineno) = split /:/, $ref, 2;
                $file =~ s{^\.\./}{};
                if (exists $files{$file}) {
                    push @{$files{$file}}, $lineno;
                } else {
                    $files{$file} = [$lineno];
                }
            }
        }
    }
}

foreach my $file (sort keys %files) {
    unless (-w $file) {
        warn "warning: $file is probably generated\n"; # have to edit generator
        next;
    }
    my @lines = sort { $a <=> $b } @{$files{$file}};

    #print "editing $file at lines ", join (", ", @lines), " ...\n";
    open FILE, "<$file" or die "$file: $!";
    my @all = ();
    push @all, $_ while <FILE>;
    close FILE;

    my $ext = $file;
    $ext =~ s/^.*\.//;

    foreach (@lines) {
        # Don't mess with verbatim sections in POD files.
        next if $ext eq "pod" && $all[$_-1] =~ m/^ /;

        unless ($all[$_-1] =~ $re) {
            # this can happen for multi-line strings, have to edit it
            # by hand
            warn "warning: $file:$_ does not contain expected content\n";
            next;
        }
        $all[$_-1] =~ s/$re/‘$1’/g;
    }

    rename "$file", "$file.bak";
    open FILE, ">$file" or die "$file: $!";
    print FILE $_ for @all;
    close FILE;
    my $mode = (stat ("$file.bak"))[2];
    chmod ($mode & 0777, "$file");
}
2017-04-04 18:47:37 +01:00
Pino Toscano
d36940992e lib: qemu: improve handling of FILE*
Create own blocks for all the parts dealing with FILE*: this way there
is no need to recycle the same FILE* variable for all the operations,
and have each block its own variable automatically cleaned up.

This also fixes a potential undefined behaviour on error: POSIX says
that after a call fclose(), a FILE* cannot be used anymore, not even
on fclose() failure. The previous behaviour for fclose == -1 was to jump
to the error label, which would then try to call fclose() again (since
the FILE* pointer was still non-null).
2017-03-06 15:04:25 +01:00
Richard W.M. Jones
f161c9ea57 Rename src/ to lib/ 2017-01-26 15:05:46 +00:00