Commit Graph

312 Commits

Author SHA1 Message Date
Susant Sahani
7d22402500 lib: Use bool type for allocated flag in disk creation
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-27 08:48:25 +00:00
Susant Sahani
3aaaf4f667 lib: Use compound initialization for buffer in disk creation
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-27 08:48:02 +00:00
Susant Sahani
cf6ac9aeba lib: Add automatic file descriptor cleanup using CLEANUP_CLOSE attrib
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-27 08:46:51 +00:00
Susant Sahani
40e549294e drives: Modernize guestfs_impl_add_drive_opts initialization
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-26 15:17:50 +00:00
Susant Sahani
b4d795c13d lib: Modernize guestfs_int_drive_protocol_to_string to use static array
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-26 15:17:50 +00:00
Susant Sahani
19b208777a fuse: Modernize mount_local_readdir with compound literal struct stat init
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-26 15:16:56 +00:00
Susant Sahani
257bfad817 appliance: Modernize guestfs_int_build_appliance with compound literal
No functional change.

Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-26 15:15:33 +00:00
Susant Sahani
6c8e3992fc fuse: Modernize mount_local_getattr with compound literal stat init
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-24 10:14:37 +00:00
Susant Sahani
b0f3d711cb lib: fuse - Replace strdup + safe_malloc with safe_strdup/safe_memdup in copy_xattr_list
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-24 10:14:37 +00:00
Susant Sahani
c3ccd2164f fuse: Modernize mount_local_statfs using compound literal statvfs init
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-24 10:14:37 +00:00
Susant Sahani
8ce7e08e1b drive-create: Modernize create_drive_file using compound literal
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-24 10:09:03 +00:00
Richard W.M. Jones
28bc6ca6b9 lib/match.c: Emit a debug message if pcre2_match returns an unexpected error
When iterating on commit 3c1554e7f2 ("lib: Add new app2_class field
for classifying applications") in early versions I was accidentally
passing NULL to match().  Unexpectedly this returned 1 (ie. matching).
This happened because we did not correctly handle other errors apart
from PCRE2_ERROR_NOMATCH from pcre2_match.

Emit a debug message when this happens, and return no match.

To test this I ran these commands, and there was no output:

  $ make && LIBGUESTFS_DEBUG=1 LIBGUESTFS_TRACE=1 make check
  $ find -name '*.log' | xargs grep 'match.*unexpected error'
2025-11-05 15:23:12 +00:00
Richard W.M. Jones
1db2b7837f daemon: inspect_get_windows_group_policy
Windows group policy objects (GPOs) are restrictions that can be added
by an administrator to Windows to lock down various operations.  From
our point of view the ones that matter involve restricting the ability
to inject device drivers.

Previously virt-v2v detected group policy here:

9bb2e7d470/convert/convert_windows.ml (L69)

We would like to report group policy through the libguestfs API and
tools such as virt-inspector, so move the code that is used to detect
group policy to libguestfs.  A new API is introduced that returns
whether group policy was found (only for Windows guests) during
inspection of the software registry.

Fixes: https://issues.redhat.com/browse/RHEL-125846
2025-11-04 15:56:33 +00:00
Richard W.M. Jones
344d96e158 lib/inspect-apps.c: Match name, display name and publisher across all regexs
For McAfee VirusScan, the name is bogus and the display name is the
actual name:

  <application>
    <name>{CE15D1B6-19B6-4D4D-8F43-CF5D2C3356FF}</name>
    <display_name>McAfee VirusScan Enterprise</display_name>
    <version>8.8.04001</version>
    <install_path>C:Program Files (x86)McAfeeVirusScan Enterprise\</install_path>
    <publisher>McAfee, Inc.</publisher>
    <url>http://www.mcafeesecurity.com/</url>
  </application>

Simplify the existing ad hoc code so we just match name, display name
and publisher (if present) against all the virus scanning strings.
2025-11-04 15:56:33 +00:00
Richard W.M. Jones
3c1554e7f2 lib: Add new app2_class field for classifying applications
Existing virt-v2v code uses some simple heuristics for detecting
Windows anti-virus software:

7520185504/convert/windows.ml

Replicate exactly this code as a new field in the struct returned by
guestfs_inspect_get_applications2.  Because of limitations with the
API, we must use one of the existing spare fields in the struct, and
it must have the same type (a string), so we are limited in the design
of this new API.  I chose to return a primary classification for the
application, with the only classification possible so far being
"antivirus" (or "" if not).  This allows the possibility of future
expansion of use of this field if we need to in future.

Fixes: https://issues.redhat.com/browse/RHEL-125846
2025-11-04 15:56:33 +00:00
Cole Robinson
1b791fdcd3 lib/launch-libvirt.c: avoid libvirt svirt validation error
If a user passes a custom hv, and uses add_libvirt_dom (like
in test820RHBZ912499.py) , this hits a bogus libvirt validation
check which rejects disabling svirt at both domain and disk level.

Work around it by skipping the redundant disk override when
svirt is disabled at the domain level.

Signed-off-by: Cole Robinson <crobinso@redhat.com>
2025-10-23 11:22:55 +01:00
Cole Robinson
acb785fede lib/launch-libvirt.c: check if g->hv symlink matches libvirt default
On fedora, ./configure will set QEMU=qemu-kvm, but libvirt
domcaps will default to qemu-system-x86_64. The former is a symlink
to the latter, but libguestfs doesn't know that, so determines
the user requested a non-default hv override and changes some
settings as a result (like disabling svirt).

Check for the symlink case and consider it a non-custom config
if realpath matches libvirt's default

Signed-off-by: Cole Robinson <crobinso@redhat.com>
2025-10-23 11:22:55 +01:00
Cole Robinson
55ea21d4b3 lib/launch-libvirt.c: debug is_custom_hv result
Add debugging about is_custom_hv result.
cache the initial call so we are only hitting the debug() once.

Signed-off-by: Cole Robinson <crobinso@redhat.com>
2025-10-23 11:22:55 +01:00
Cole Robinson
81e9f81a6e lib/launch-libvirt.c: simplify is_custom_hv
g->hv is always set in lib/handle.c

Signed-off-by: Cole Robinson <crobinso@redhat.com>
2025-10-23 11:22:55 +01:00
Cole Robinson
e051891347 lib/launch-libvirt.c: make model='selinux' disabling explicit
Using `<seclabel type='none'/>` implies `model=selinux`, so lets fill it
in to be explicit.

Signed-off-by: Cole Robinson <crobinso@redhat.com>
2025-10-23 11:22:55 +01:00
Cole Robinson
949679993e lib/launch-libvirt.c: Simplify root socket permissions
libvirt has DAC relabeled sockets for us for a decade, so we don't
need to do chowning here anymore

The chmod calls are still required, otherwise our created sockets
may be too permissive.

Update the comment to try and preserve the still relevant info,
though now it's a bit awkwardly placed.

Signed-off-by: Cole Robinson <crobinso@redhat.com>
2025-10-23 11:22:55 +01:00
Cole Robinson
62309ab40c lib/launch-libvirt.c: don't write <seclabel><imagelabel>
<imagelabel> is an output-only XML element. setting it when
defining XML does nothing

Signed-off-by: Cole Robinson <crobinso@redhat.com>
2025-10-23 11:22:55 +01:00
Richard W.M. Jones
7398fdc77c lib/launch-direct.c: Remove workaround that disabled 5-level page tables
https://bugzilla.redhat.com/2082806 is a private kernel bug (sigh) for
RHEL 8, where some missing backports caused 5-level page tables to
fail when qemu emulated them.  This was fixed back in 2023, so let's
remove this workaround now.
2025-10-17 09:57:53 +01: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
63a2ed92b4 fuse: Ignore extra options parameter on macOS
macOS macfuse has an extra options parameter for the setxattr and
getxattr FUSE callbacks.  The possible options are documented below.
However the underlying libguestfs APIs don't allow us to act on these
flags, so we ignore them for now.

(from https://manp.gs/mac/2/setxattr)

  XATTR_NOFOLLOW
    do not follow symbolic links. setxattr() normally sets attributes
    on the target of path if it is a symbolic link. With this option,
    setxattr() will act on the link itself.
  XATTR_NOFOLLOW_ANY
    do not follow any symbolic links encountered during pathname
    resolution. An error is returned if a symlink is encountered
    before the last component of path.
  XATTR_CREATE
    fail if the named attribute already exists.
  XATTR_REPLACE
    fail if the named attribute does not exist. Failure to specify
    XATTR_REPLACE or XATTR_CREATE allows creation and replacement.

Reported-by: Mohamed Akram
Fixes: https://github.com/libguestfs/libguestfs/issues/180
Related: https://github.com/macfuse/macfuse/issues/1065
2025-10-07 14:06:46 +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
4872c0cf09 lib/launch-direct.c: Assume virtio-rng
It's always a good idea to provide proper entropy to the libguestfs
appliance (eg. for any cryptographic operations).  We already assume
virtio, so we can assume virtio-rng is present.
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
d8b08eac38 lib/launch-libvirt.c: Don't store libvirt_version in handle
We no longer use the libvirt version anywhere, except when reporting
the version.  Remove this from the handle.

Simplify the remaining code.  In particular:

* don't bother parsing the libvirt version, just print what
  virGetVersion gives us

* guestfs_int_version_from_libvirt is dead code, so it can be removed
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
48bad58024 lib/launch-libvirt.c: Assume libvirt >= 9.0.0 and passt support
Libvirt 9.0.0 was released in January 2023, and it seems safe to
assume that if you're enabling the non-default backend, you can at
least use a new version of libvirt.

If you're using new libvirt, might as well also assume passt is
available.
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
d730fc8b7c lib/launch-libvirt.c: Remove a workaround for libvirt < 3.8.0
This version of libvirt was released in 2017.
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
a68f3eee15 lib/launch-libvirt.c: Add virtio-rng unconditionally
This tested for an ancient version of libvirt, but we can assume we
are always using something more recent.
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
788a1e4993 lib/launch-direct.c: Don't store qemu_version in handle
As we no longer use this field, no longer store it in the handle
struct.
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
99921de6e0 lib/launch-direct.c: Move baseline to qemu 7.2.0 and assume passt support
We already effectively assume that qemu is much newer than the 1.3.0
baseline previously documented.  As one example, commit 47857751a7
("lib: direct: Remove test for qemu mandatory locking") assumes that
qemu >= 2.10.  Because passt support is desirable in general, let's
assume that qemu is at least version 7.2.0, and document it.

qemu 7.2.0 was released in December 2022, nearly 3 years ago, and RHEL
9 is currently on qemu 9.1.0.

This keeps the SLIRP fallback path in case passt isn't installed, but
we should remove that fallback in future too.
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
dce5ce605d lib/launch-direct.c: Set lost_tick_policy=discard unconditionally
We already assume qemu version is must greater than 1.3.0 so we do not
need this qemu version test.
2025-09-29 16:49:03 +01:00
Richard W.M. Jones
ec47331b12 lib/launch-libvirt.c: Don't store qemu_version in handle
After previous commits, we no longer use the qemu version elsewhere
(after simply reporting it).  Remove all the code for storing the qemu
version.
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
fc243a36bd lib/launch-direct.c: Remove unnecessary tests for ancient qemu features
We can safely assume that qemu supports -nodefaults and
-no-user-config, since these have been supported since forever.

-no-hpet was deprecated in qemu 8.0 and the option removed in early
2024, replaced with -machine hpet=off.  HPET defaults to 'on' in
upstream qemu, and to 'off' in downstream RHEL rebuilds.

Since (for libguestfs) we can assume an up to date Linux kernel is
running inside the guest, and that the kernel will do the right thing
with regards to timers, we don't need to mess with qemu defaults.  In
practice, Linux chooses kvm-clock.

Thanks: Thomas Huth, Daniel Berrange
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
Cole Robinson
a2e7dfc73b New API: ntfs_chmod
Add an API to do the equivalent of `chmod [-r] MODE PATH` for
NTFS filesystems.

Files created on a linux ntfs-3g mount can not change permissions
directly. New files and directories are created with rough windows
equivalent of `chmod 777`. These wide open permissions can generate
security warnings on windows after virt-v2v installs bits into
`Program Files\Guestfs`.

Behind the scenes we use `ntfssecaudit(8)` from `ntfsprogs`
which is already part of the appliance. We only expose the chmod-style
feature; the rest of `ntfssecaudit` is concerned reporting and
managing fine grained windows security info which is way more than
we need.

Also note, `ntfssecaudit` needs to run on an unmounted partition
so using this is more complicated than a traditional `chmod` call.

Related: https://issues.redhat.com/browse/RHEL-104352

Signed-off-by: Cole Robinson <crobinso@redhat.com>
2025-09-09 16:29:13 +01:00
Richard W.M. Jones
1c0b56158a daemon: Deprecate guestfs_selinux_relabel, replace with guestfs_setfiles
The guestfs_selinux_relabel function was very hard to use.  In
particular it didn't just do an SELinux relabel as you might expect.
Instead you have to write a whole bunch of code around it (example[1])
to make it useful.

Another problem is that it doesn't let you pass multiple paths to the
setfiles command, but the command itself does permit that (and, as it
turns out, will require it).  There is no backwards compatible way to
extend the existing definition to allow a list parameter without
breaking API.

So deprecate guestfs_selinux_relabel.  Reimplement it as
guestfs_setfiles.  The new function is basically the same as the old
one, but allows you to pass a list of paths.  The old function calls
the new function with a single path parameter.

[1] https://github.com/libguestfs/libguestfs-common/blob/master/mlcustomize/SELinux_relabel.ml
2025-08-13 16:08:28 +01:00
Cole Robinson
701667b6f5 docs: Fix dead ntfs-3g doc links 2025-08-04 15:49:50 +01:00
Richard W.M. Jones
c7aaa89fba lib: libvirt: Sleep before retrying virDomainDestroyFlags
This saves us going into a loop if virDomainDestroyFlags keeps
returning -EBUSY quickly, which apparenrly can happen in containers.

The equivalent 'direct' backend code sleeps for 2 seconds in this case.
2025-07-25 09:42:39 +01:00
Richard W.M. Jones
0a91731356 lib: libvirt: Debug error from virDomainDestroyFlags
It's useful to see the error returned from virDomainDestroyFlags, so
make sure this gets written to debug output.
2025-07-25 09:41:27 +01:00
Richard W.M. Jones
f6fe0611a8 lib: Make libvirt ACPI feature flag conditional on x86 or Arm
On ppc64 and s390x it failed with an error like:

  unsupported configuration: machine type 'pseries-10.0' does not support ACPI

Updates: commit 7cf0ed750e
Related: https://bugzilla.redhat.com/show_bug.cgi?id=2372329
2025-06-12 08:11:56 +01:00
Richard W.M. Jones
7cf0ed750e lib: Enable ACPI for the libvirt backend
Many years ago we used to pass acpi=off on the Linux kernel command
line.  In commit db1f811b2 we stopped doing that (around 2016).
However unless you also use:

  <features>
    <acpi/>
  </features>

then it turns out that libvirt disables ACPI generation at the qemu
level.  None of this mattered until SeaBIOS 1.17 changed its
behaviour, causing ACPI to be required for virtio devices to work.

Updates: commit db1f811b29
Related: https://bugzilla.redhat.com/show_bug.cgi?id=2372329
Thanks: Gerd Hoffmann
2025-06-11 22:50:07 +01:00
Richard W.M. Jones
db46bcb535 lib/inspect-osinfo.c: Generate new osinfo shortname for SLES >= 15
libosinfo changes the naming scheme it uses for SUSE starting with
major version 15.  Previously it used names like "sles12" (or
"sles12sp1"), "sled12" for Server and Desktop variants.  In 15+ it
uses "sle15" as there are no variants any longer (instead the
installer asks you what variant you want to install).  We're only
interested in the Server variant.  Change the name that we return to
"sle15" or "sle15sp1".

See: b0fa386699
Fixes: https://issues.redhat.com/browse/RHEL-95791
Thanks: Ming Xie, Victor Toso
Related: https://issues.redhat.com/browse/RHEL-95540
2025-06-11 11:51:18 +01:00