Commit Graph

1488 Commits

Author SHA1 Message Date
Richard W.M. Jones
47c2233026 daemon/blkid.c: Fix compilation error introduced by earlier change
blkid.c: In function ‘test_blkid_p_i_opt’:
  blkid.c:105:3: error: label ‘command_failed’ defined but not used [-Werror=unused-label]
    105 |   command_failed:
        |   ^~~~~~~~~~~~~~

Fixes: https://github.com/libguestfs/libguestfs/pull/315
Fixes: commit ff4467a1a4
2026-03-10 08:31:27 +00:00
shivanayak
91578639c6 daemon: fix fd leak in do_inotify_files on error paths (#319)
When popen or do_inotify_read fails, the file descriptor created by
mkstemp is never closed before returning. Add close(fd) to both
error paths to prevent file descriptor leaks.

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-09 18:15:38 +00:00
shivanayak
ff4467a1a4 daemon: fix low severity bugs in debug, file, and blkid (#315)
- debug.c: fix memory leak of out buffer on opendir failure in
  debug_fds. After fclose on open_memstream, the out buffer is
  allocated and must be freed.

- file.c: add missing reply_with_perror on strdup failure in
  do_zfile, so callers get a proper error message instead of
  silent NULL return.

- blkid.c: fix wrong error variable used at command_failed label
  in test_blkid_p_i_opt. The second commandr stores its error in
  err2, but goto command_failed would report err from the first
  command. Inline the error reporting with the correct variable.

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-09 18:14:32 +00:00
shivanayak
25c321a8fd daemon: fix temp file leak in do_vgmeta on error paths (#314)
When vgcfgbackup fails or subsequent file operations fail, the
temporary file created by mkstemp is never unlinked. Add unlink(tmp)
to all error paths after mkstemp to prevent temp files accumulating
in /tmp.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-09 16:48:56 +00:00
shivanayak
cb233a73c6 daemon: fix memory leak in do_aug_match on realloc failure (#313)
When realloc fails, vp is NULL so free(vp) is a no-op. The original
matches array (and all r strings allocated by aug_match) is leaked.
Use free_stringslen to properly free the array and its contents.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-09 16:46:00 +00:00
Richard W.M. Jones
8ded6e8858 Revert "daemon: send_dirent_info: Use designated compound literal for tsk_dirent"
This breaks compilation:

tsk.c: In function ‘send_dirent_info’:
tsk.c:209:32: error: passing argument 2 of ‘file_metadata’ discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
  209 |   file_metadata (fsfile->meta, &dirent);
      |                                ^~~~~~~
tsk.c:51:43: note: expected ‘guestfs_int_tsk_dirent *’ but argument is of type ‘const struct guestfs_int_tsk_dirent *’
   51 | static void file_metadata (TSK_FS_META *, guestfs_int_tsk_dirent *);
      |                                           ^~~~~~~~~~~~~~~~~~~~~~~~
tsk.c:211:43: error: passing argument 2 of ‘xdr_guestfs_int_tsk_dirent’ discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
  211 |   ret = xdr_guestfs_int_tsk_dirent (&xdr, &dirent);
      |                                           ^~~~~~~
In file included from tsk.c:29:
../common/protocol/guestfs_protocol.h:5215:51: note: expected ‘guestfs_int_tsk_dirent *’ but argument is of type ‘const struct guestfs_int_tsk_dirent *’
 5215 | extern  bool_t xdr_guestfs_int_tsk_dirent (XDR *, guestfs_int_tsk_dirent*);
      |                                                   ^~~~~~~~~~~~~~~~~~~~~~~

This reverts commit b5000cf547.
2026-02-11 12:07:22 +00:00
Richard W.M. Jones
e1deb358ce daemon: Reimplement xfs_info using xfs_info2
Remove all the complicated old C parsing code and reimplement xfs_info
using xfs_info2.  Note that this function will be deprecated.
2026-01-26 14:40:39 +00:00
Richard W.M. Jones
dfd2700616 New API: xfs_info2
Reimplement xfs_info by returning a hash table of values (rather than
a limited struct), and by writing it in OCaml with PCRE which makes
string parsing a lot simpler.  This will now flexibly return all the
fields from the underlying xfs_info command, even (hopefully) future
fields.

Note the field values are returned as strings, because the actual
fields in xfs_info output are fairly random and free-form.  There is a
trade off here between returning as much information as we can, and
requiring the user to do a bit of (simple) field parsing.

Fixes: https://issues.redhat.com/browse/RHEL-143673
2026-01-26 14:40:39 +00:00
Richard W.M. Jones
548af2929e daemon/selinux.c: Fix compilation when no libselinux
/usr/bin/ld: /tmp/ccvSGq6E.ltrans7.ltrans.o:(.data.rel.ro+0x1f8): undefined reference to `optgroup_selinuxrelabel_available'

The reason is that we didn't include optgroup_selinuxrelabel_available
on the fallback / no libselinux code path.

Reported-by: David Runge
Thanks: Toolybird
Fixes: https://github.com/libguestfs/libguestfs/issues/290
Fixes: commit ed40333a23
2026-01-19 14:53:12 +00:00
Anders Roxell
7fee08e53f tar_in: add keepdirlink option for --keep-directory-symlink
Add a new optional boolean argument 'keepdirlink' to tar_in that passes
--keep-directory-symlink to tar. This preserves existing symlinks to
directories when extracting, which is important for usrmerge systems
where /lib is a symlink to /usr/lib.

Without this option, extracting a tarball containing lib/modules/...
to / would replace the /lib symlink with a real directory, breaking
the system.

Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
2026-01-19 12:39:59 +00:00
Richard W.M. Jones
c7b204bce3 daemon/device-name-translation.c: Fix btrfs volume reverse translation
Devices associated with btrfs volumes are not reverse-translated
(e.g., btrfsvol:/dev/sdX to sdY).

Forward translation occurs, creating a path mismatch.  This causes
errors in subsequent btrfs commands.

Thanks: Arye Yurkovsky
2025-12-10 12:31:15 +00:00
Arye Yurkovsky
62b0e2ab64 daemon: lvm_utils: Further narrow stat failure to ENOENT
Missed the 2nd stat
2025-12-08 17:40:59 +00:00
Arye Yurkovsky
6aebf70e18 daemon: lvm_utils: Narrow stat failure to ENOENT
And remove redundant parantheses.
To only catch the failure we want to catch.
2025-12-08 17:40:59 +00:00
Arye Yurkovsky
c7228cd1ce daemon: lvm_utils: Handle stat errors
lvm returns logical volumes even if they're broken, for instance when a
physical volume is missing in their volume group.
In such cases, stat would fail to resolve the provided path.
Handle such cases by skipping such failures when finding the matching
lvm device.
2025-12-08 17:40:59 +00:00
Susant Sahani
0a8d1aefef daemon: tsk - Fix memory leak
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-24 11:15:23 +00:00
Susant Sahani
b5000cf547 daemon: send_dirent_info: Use designated compound literal for tsk_dirent
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-24 11:15:23 +00:00
Richard W.M. Jones
56da6b36d3 daemon: btrfs: Simplify snapshot code and fix invalid memory access
The existing code had a bug which you can demonstrate by doing:

  $ guestfish -N fs:btrfs:10G -m /dev/sda1 \
  btrfs-subvolume-create /sub :
  btrfs-subvolume-snapshot /sub /snap1 : \
  btrfs-subvolume-snapshot /sub /snap123 : \
  btrfs-subvolume-snapshot /sub /snap123456 : \
  btrfs-subvolume-show /sub
  ...
  libguestfs: error: appliance closed the connection unexpectedly.
  This usually means the libguestfs appliance crashed.

As the code for parsing the output and creating the comma-separated
list of snapshots was unncessarily complicated in the first place,
simplify it.  This also fixes the bug.

This also adds a regression test.

Thanks: Arye Yurkovsky
Link: https://lists.libguestfs.org/archives/list/guestfs@lists.libguestfs.org/thread/QV5VDHIH7WRUNAE54K6OEOKJMWL6M7EM/
2025-11-24 10:44:44 +00:00
Susant Sahani
0bd33f87ce daemon: stat_to_statns: Modernize with designated initializer
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-24 09:58:22 +00:00
Susant Sahani
0c437e52d6 daemon: bind_mount: Initialize struct bind_state with compound literal
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-24 09:58:05 +00:00
Susant Sahani
799b04fe3e daemon: Convert do_set_uuid_random to table-driven dispatch
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-24 09:49:03 +00:00
Susant Sahani
174014933a daemon: use table-driven dispatch in do_set_uuid
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-24 09:49:03 +00:00
Susant Sahani
301b9bc481 daemon: set-label: Convert to table-driven dispatch
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-24 09:47:21 +00:00
Susant Sahani
276897c4df daemon: Use modern compound literal for SIGPIPE ignore
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-21 18:58:00 +00:00
Susant Sahani
4f19d68565 daemon: Use designated initializer for struct sockaddr_un
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-21 14:02:14 +00:00
Susant Sahani
3940b79041 parted: Modernize check_parttype() with table-driven mapping
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-21 13:57:26 +00:00
Susant Sahani
e04a450ec7 daemon: modernize program_of_csum() using table lookup
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-20 13:11:47 +00:00
Susant Sahani
3c56167615 daemon: use compound literal in send_error()
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-20 11:22:56 +00:00
Susant Sahani
4b81c6f2b1 daemon: use compound literal for reply header
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-20 11:22:56 +00:00
Susant Sahani
b983586eb3 daemon: modernize send_file_write() with compound literal
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-20 11:22:56 +00:00
Susant Sahani
bad58301fb daemon: use compound literal for zero timeval in check_for_library_cancellation
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-20 11:22:56 +00:00
Susant Sahani
60c06f3287 daemon: modernize send_file_end() with compound literal
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-20 11:22:56 +00:00
Susant Sahani
a0c9b41d35 daemon: modernize notify_progress_no_ratelimit: compound literal
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-20 10:36:57 +00:00
Susant Sahani
23257770a8 daemon: modernize pulse_mode_start with compound literals
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-20 08:47:44 +00:00
Susant Sahani
6c8763dcb5 daemon: modernize pulse_mode_cancel with compound literals
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-20 08:47:44 +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
355f8a5413 daemon/inspect_fs_windows.ml: Debug number of children
Just adds a useful debugging statement.
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
Richard W.M. Jones
495f71affc daemon, generator: Use power of 2 for initial size of Hashtbl.create
Before 2011 it was recommended to use a prime number for the initial
size.  In 2011 the OCaml hash table was reimplemented using a hash
function based on Murmur 3.  Hashtbl.create now adjusts the initial
size to the next power of 2 (minimum 16).  So replace obsolete
'Hashtbl.create 13' with 'Hashtbl.create 16'.
2025-09-12 08:37:58 +01:00
Richard W.M. Jones
30ccb9a3e6 daemon/selinux.ml: Use setfiles -A option to reduce memory usage
In filesystems that have many millions of files, the default behaviour
of setfiles is to build a huge hash table containing every filename.
This uses up lots of memory which prevents relabelling from happening
in the reduced memory environment of the libguestfs appliance.

I added the setfiles -A option to change this default behaviour.  If
setfiles has the option then use it.

Fixes: https://issues.redhat.com/browse/RHEL-114292
Related: https://issues.redhat.com/browse/RHEL-111165
Related: https://issues.redhat.com/browse/RHEL-111505
2025-09-11 21:03:37 +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
32b911a061 daemon/inspect_fs_windows.ml: Use {|..|} string literal 2025-09-01 13:59:56 +01:00
Richard W.M. Jones
f857f39a2d daemon/selinux.ml: Simplify setfiles option testing
Updates: commit 1c0b56158a
2025-08-30 14:05:34 +01:00
Richard W.M. Jones
91ee98523e daemon/selinux.ml: Fix typo in comment
Fixes: commit d0d8e67384
2025-08-27 10:43:00 +01:00
Richard W.M. Jones
1c00248ac1 daemon/inspect_fs_windows.ml: Ignore blank disks in drive mapping
If HKLM\System\MountedDevices references a blank disk, then when we
try to search for the actual backing device we will get an error from
parted:

  parted: /dev/sdb: parted exited with status 1: Error: /dev/sdb: unrecognised disk label: Invalid argument

Just ignore these errors instead of failing inspection.

Fixes: https://issues.redhat.com/browse/RHEL-108803
Reported-by: Ameen Barakat
Thanks: Ming Xie
2025-08-14 15:45:59 +01:00
Richard W.M. Jones
5c7e15cfae daemon/inspect_fs_windows.ml: Add debugging when we start registry analysis
Add some debugging when we begin the process of analyzing the Windows
registry of a guest.
2025-08-14 15:45:59 +01:00
Richard W.M. Jones
e18bd72c8e daemon/inspect_fs_windows.ml: Add debugging for MBR drive mappings
The function 'map_registry_disk_blob_gpt' immediately below this one
has a debugging statement.  Add the equivalent to the function
'map_registry_disk_blob_mbr'.

The output looks like:

  map_registry_disk_blob_mbr: searching for MBR disk ID 31 32 33 34
  map_registry_disk_blob_mbr: searching for MBR partition offset 00 00 00 10 00 00 00 00
2025-08-14 15:45:59 +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
Richard W.M. Jones
fd4db60cff generator: Implement StringList for OCaml functions
No existing OCaml functions have a StringList parameter, but we would
like to add one.

The original plan seems to have been to map these to 'string array'
types, but 'string list' is more natural, albeit marginally less
efficient.  The implementation here just has to convert the 'char **'
into the OCaml linked list of values.
2025-08-13 16:08:28 +01:00
Richard W.M. Jones
ed40333a23 daemon: Reimplement guestfs_selinux_relabel in OCaml
No change, just reimplement the existing C implementation in OCaml.
2025-08-13 16:08:28 +01:00
Richard W.M. Jones
c931ab3bc8 daemon: sysroot: Avoid copying the path every time we call sysroot ()
This path never changes once the daemon has started up, so we don't
need to call into C code and copy the string every time.
2025-08-13 16:08:28 +01:00