Commit Graph

1448 Commits

Author SHA1 Message Date
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
Richard W.M. Jones
1e0099671a daemon: sysroot: Avoid double-/ when creating sysroot paths in OCaml
Previously calling 'sysroot_path "/dev"' for example would return the
string "/sysroot//dev".  While this is not wrong, it confuses some
external programs (hello, setfiles), and it's not very "clean".  Be a
bit more careful to avoid doubling the '/' character in the common case.
2025-08-13 16:08:28 +01:00
Cole Robinson
06db19c56c daemon: inspect: check /etc/crypttab for /dev/mapper/*
Encrypted root fs on SUSE distros will present itself like so:

```
/dev/mapper/cr_root                         /                   btrfs   defaults                0 0
UUID=588905f9-bfa4-47b5-9fe8-893cb8ad4a0b   /var                btrfs   subvol=/@/var           0 0
... more subvols here ...
UUID=8a278363-3042-4dea-a878-592f5e1b7381   swap                btrfs   defaults                0 0
/dev/mapper/cr_root                         /.snapshots         btrfs   subvol=/@/.snapshots    0 0

cr_root  UUID=5289379a-a707-41b5-994c-c383f7ed54cc  none  x-initrd.attach
```

This breaks `-i` inspection, since libguestfs doesn't know what
/dev/mapper/cr_root is supposed to be, and nothing in the appliance
will autopopulate that path. This isn't a problem on Fedora, where
it uses UUID= instead of a /dev/mapper path.

Currently when we see /dev/mapper as a mount prefix, we only attempt
to do some LVM name mapping. This extends libguestfs to check
/etc/crypttab first. If we find an entry for the mapper path, and it
points to the encrypted luks UUID, we use that UUID to build the
associated /dev/disk/by-id/dm-uuid-CRYPT-* path, which is a symlink
to the unencrypted /dev/dm-X path

Resolves: https://issues.redhat.com/browse/RHEL-93584

Signed-off-by: Cole Robinson <crobinso@redhat.com>
2025-08-12 17:55:06 +01:00
Richard W.M. Jones
f4f84a8824 daemon: Add contents of /etc/fstab to verbose log
Also some mdadm configuration files.  This is useful for debugging.

The output looks like this:

  info: /etc/fstab in /dev/VG/Root
  LABEL=BOOT /boot ext2 default 0 0$
  LABEL=ROOT / ext2 default 0 0$

Fixes: https://issues.redhat.com/browse/RHEL-106490
2025-07-30 11:01:03 +01:00
Cole Robinson
bcf204ad68 daemon: inspect: factor out resolve_dev_mapper function
This is just code movement.

Signed-off-by: Cole Robinson <crobinso@redhat.com>

RWMJ: Renamed and moved the function for consistency with surrounding
code.
2025-06-13 09:10:30 +01:00
Richard W.M. Jones
8f5e4f07ba inspection: Ignore btrfs snapshots of roots
In SLES guests in particular, btrfs snapshots seem to be used to allow
rollback of changes made to the filesystem.  Dozens of snapshots may
be present.  Technically therefore these are multi-boot guests.  The
libguestfs concept of "root" of an operating system does not map well
to this, causing problems in virt-inspector and virt-v2v.

In this commit we ignore these duplicates.  The test is quite narrow
to avoid false positives: We only remove a duplicate if it is a member
of a parent device, both are btrfs, both the snapshot and parent have
a root role, and the roles are otherwise very similar.

There may be a case for reporting this information separately in
future, although it's also easy to find this out now.  For example,
when you see a btrfs root device returned by inspect_os, you could
call btrfs_subvolume_list on the root device to list the snapshots.

Fixes: https://issues.redhat.com/browse/RHEL-93109
2025-05-27 17:01:09 +01:00
Richard W.M. Jones
ebaba43221 daemon/inspect.ml: Fix comment
Back in commit 8289aa1ad6 ("New APIs for guest inspection.", 2010)
when inspection was first added, we did inspection in the library, so
it was accurate to say that inspection information was stored "in the
handle".  Much later, in commit 394d11be49 and commit 3a00c4d179
(2017) we moved inspection to the daemon, but left the comment the
same.

Fixes: commit 3a00c4d179
2025-05-27 17:01:09 +01:00
Richard W.M. Jones
b2ec671abd daemon/inspect.ml: Pipeline style when mapping and filtering filesystems
No actual change in the functionality, just make it clear that this is
a pipeline of transformations on the list of filesystems.
2025-05-27 17:01:09 +01:00
Richard W.M. Jones
7ac190ed20 daemon/listfs.ml: Add more debugging to list_filesystems
This function is used from other parts of the daemon, especially for
example with inspection.  However it was difficult to follow exactly
what filesystems it was returning because of insufficient debugging
information.
2025-05-27 17:01:09 +01:00
Richard W.M. Jones
b98cc96129 daemon: Implement e2fsck -n flag (as FORCENO option)
Fixes: https://issues.redhat.com/browse/RHEL-92599
2025-05-20 14:40:58 +01:00
Richard W.M. Jones
ea3dd97f1d New API: Replace btrfs-fsck with btrfs-scrub-full
The old btrfs-fsck API used "btrfs check" which appears to be broken
or deprecated.  The real tool you should use is "btrfs scrub".  We
have already implemented that API, but it is very awkward to use from
libguestfs.  In particular there's no existing way to run the scrub
and wait for it to finish.

Fix this by deprecating btrfs-fsck.  Implement a new API
btrfs-scrub-full which runs btrfs scrub in the foreground, waits for
it to finish, and handles errors.  It's much more like fsck tools in
other filesystems.

Thanks: Eric Sandeen
Fixes: https://issues.redhat.com/browse/RHEL-91936
2025-05-19 13:42:44 +01:00
Richard W.M. Jones
f9edfc9a18 Update common submodule
This pulls in the commits below, requiring us to replace all uses of
String.is_prefix and String.is_suffix.

Mostly done with Perl like this, and carefully checked by hand
afterwards since this doesn't get everything right:

  $ perl -pi.bak -e 's/String.is_prefix ([^[:space:]\)]+) ([^[:space:]\)]+)/String.starts_with \2 \1/g' -- `git ls-files`

  Richard W.M. Jones (3):
      mlstdutils: Fix comment that still referred to the old function names
      mldrivers: Link to gettext-stub if ocaml-gettext is enabled
      mlstdutils: Rename String.is_prefix -> starts_with, is_suffix -> ends_with
2025-05-11 21:29:23 +01:00
Richard W.M. Jones
c4ebeee505 Update common submodule
Pull in these commits which require minor changes:

  Richard W.M. Jones (3):
      mlstdutils: Remove Std_utils.identity
      mlstdutils: Remove Std_utils.protect
      mlstdutils: Remove List.filter_map
2025-05-11 12:37:23 +01:00
Richard W.M. Jones
5441d3dd0c daemon: inspect: Remove duplicate root mountpoints in /etc/fstab
A customer case was found where /etc/fstab contained multiple root
mountpoints, something like:

  LABEL=System / xfs ...
  LABEL=Boot /boot ext2 ...
  LABEL=System / xfs ...

This causes libguestfs and virt-v2v to fail.  Either (on RHEL 9) we
try to mount the second instance of / which gives an error.  Or (on
upstream kernels) we are able to mount the second instance but then
libguestfs gets confused when trying to unmount them.

In this case as the mounted devices are the same we can just delete
the duplicate.  It's also possible that there could be multiple
non-identical root mountpoints, in which case we have to pick one, and
this code arbitrarily picks the first[*] (but emits a warning).

We don't do anything for non-root mountpoints.

Update common submodule to add 'List.same' function from mlstdutils.

[*] Which one is "the first" depends on what version of ocaml-augeas
we are using.  ocaml-augeas version 0.6 Augeas.matches function
returns entries in reverse order (compared to augeas itself).  This is
fixed in version 0.7:
http://git.annexia.org/?p=ocaml-augeas.git;a=commitdiff;h=b703b92e3d26690aa6f7b822132049ce5435983e

Fixes: https://issues.redhat.com/browse/RHEL-90168
2025-05-08 21:14:23 +01:00
Richard W.M. Jones
9b32056061 Fix miscellaneous spelling mistakes
$ git ls-files | xargs codespell
2025-04-29 19:05:07 +01:00
Richard W.M. Jones
ca87485eea daemon/fstrim.c: Run the fstrim command twice
In upstream kernels, for XFS, you may have to run fstrim twice for it
to be effective.

Fixes: https://issues.redhat.com/browse/RHEL-88508
Related: https://issues.redhat.com/browse/RHEL-88450
Thanks: Eric Sandeen
2025-04-28 10:07:34 +01:00
Richard W.M. Jones
e127edcafc daemon/fstrim.c: Issue sync_disks after fstrim
Thanks: Eric Sandeen
2025-04-25 15:24:43 +01:00
Richard W.M. Jones
166395914f daemon: Use $(OCAMLLIB) instead of ocamlc -where
OCAMLLIB is already set to the value of ocamlc -where that was found
at configure time, so there is no need to run that command again at
build time.
2025-04-22 12:37:40 +01:00
Richard W.M. Jones
e43ca19129 daemon: inspect: Resolve Ubuntu 22+ /dev/disk/by-id/dm-uuid-LVM-... in fstab
Linux + LVM supports device names like /dev/disk/by-id/dm-uuid-LVM-
followed by two concatenated UUIDs, firstly for the volume group and
secondly for the logical volume.  We can reverse those to get the
device name (/dev/VG/LV).

fstab entries look like:

  # / was on /dev/vg0/lv-0 during curtin installation
  /dev/disk/by-id/dm-uuid-LVM-OzFWT6NHkstr1hcmrWRRMDGPn9xdZj1YOOycQ533186x288FdU6UubU3OlnWJz6D / ext4 defaults 0 1
  # /usr was on /dev/vg0/lv-1 during curtin installation
  /dev/disk/by-id/dm-uuid-LVM-OzFWT6NHkstr1hcmrWRRMDGPn9xdZj1YZu53m4ZssZ8Jeb3I14RAJwIj5YlHIb9P /usr ext4 defaults 0 1

The upshot of this fix is that we are now able to correctly inspect
and run virt-v2v on Ubuntu 22+ guests with split /usr.  In particular,
we correctly map /etc/fstab entries like the above to LV device names,
which means that /usr merging now works correctly.

Reported-by: Jaroslav Spanko
Thanks: Daniel Berrange
Fixes: https://issues.redhat.com/browse/RHEL-87493
2025-04-16 21:12:49 +01:00
Richard W.M. Jones
a73f248369 daemon: Rewrite {pvs,vgs,lvs}-full APIs in OCaml
These were previously written in very convoluted C which had to deal
with parsing the crazy output of the "lvm" command.  In fact the
parsing was so complex that it was generated by the generator.  It's
easier to do this in OCaml.

These are basically legacy APIs.  They cannot be expanded and LVM
already supports many more fields.  We should replace these with APIs
for getting single named fields from LVM.
2025-04-16 21:12:49 +01:00
Richard W.M. Jones
7a1ffd744b daemon: inspect: Resolve Ubuntu 22+ /dev/disk/by-uuid/ in fstab
Ubuntu 22= uses /dev/disk/by-uuid/ followed by a filesystem UUID in
fstab entries.  Resolve these to mountables.

A typical fstab entry looks like this:

  # /boot was on /dev/vda2 during curtin installation
  /dev/disk/by-uuid/b4e56462-5a64-4272-b76d-f5e58bd8f128 /boot ext4 defaults 0 1

The comment is generated by the installer and appears in the fstab.
This entry would be translated to /dev/sda2.
2025-04-16 21:12:49 +01:00
Richard W.M. Jones
5a16d1120f generator: Use new FDevice type for the pvs-full pv_name field
Remove the code which did explicit reverse device name translation,
and use the generator's code instead.
2025-04-16 12:27:07 +01:00
Richard W.M. Jones
2d1e894130 daemon: inspect: Add some debugging of /usr merging 2025-04-16 10:28:41 +01:00
Richard W.M. Jones
4176b2043f daemon: Fix loongarch64 detection on RHEL 9
$ rpm -q file
file-5.39-16.el9.x86_64
$ file ./test-data/binaries/bin-loongarch64-dynamic
./test-data/binaries/bin-loongarch64-dynamic: ELF 64-bit LSB pie executable, *unknown arch 0x102* version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-loongarch-lp64d.so.1, BuildID[sha1]=7622a1a70bf6e697851ac3790557e1ca686459b5, for GNU/Linux 5.19.0, stripped

Updates: commit 729d6d55ea
2025-03-11 13:40:14 +00:00
denisecheng
0c70675aae support TencentOS
Signed-off-by: denisecheng <denisecheng@tencent.com>
Fixes: https://github.com/libguestfs/libguestfs/pull/172
Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
2025-03-03 14:22:37 +00:00
Richard W.M. Jones
47ac4871b2 daemon: New command_out and sh_out APIs
These APIs allow you to capture output from guest commands that
generate more output than the protocol limit allows.

Thanks: Nijin Ashok
Fixes: https://issues.redhat.com/browse/RHEL-80159
2025-02-19 12:01:10 +00: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
ed21af3b0a daemon: Translate file 5.46 "Intel i386" to "i386"
Fixes: https://github.com/libguestfs/libguestfs/issues/162
Reported-by: Toolybird
2025-01-02 16:13:35 +00:00
Richard W.M. Jones
996dd8774f build: Move baseline OCaml to 4.08
OCaml 4.08.0 was released on 2019-06-14, over 5 years ago.  By
requiring a slightly later OCaml version, we can drop more
compatibility code which was only used by older versions.

Consistent with qemu & libvirt, this drops support for compiling
upstream libguestfs on RHEL 8 (ocaml-4.07.0-4.el8.x86_64).

Qemu policy:
https://www.qemu.org/docs/master/about/build-platforms.html

Libvirt policy:
https://libvirt.org/platforms.html

Update the common submodule, pulling in:

  Richard W.M. Jones (4):
      qemuopts: Add ability to add raw, unquoted output to qemu scripts
      qemuopts: Fix missing break statement
      mlstdutils: Remove Option module
      Remove test for caml_alloc_initialized_string
2024-11-21 14:30:44 +00:00
Richard W.M. Jones
5ea1e899e0 daemon/ldm.c: Replace jansson with json-c 2024-10-22 11:02:38 +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
Richard W.M. Jones
e55eac12d4 docs: Update libguestfs email address in a couple more places
Reported-by: Julian Euler
2024-09-12 12:45:54 +01:00
Wang Guoquan
1d8924c343 libguestfs: Support openEuler
openEuler is simliar to CentOS, but the ID is not lower-case string,
as below:

    NAME="openEuler"
    VERSION="24.03 (LTS)"
    ID="openEuler"
    VERSION_ID="24.03"
    PRETTY_NAME="openEuler 24.03 (LTS)"
    ANSI_COLOR="0;31"

Signed-off-by: Wang Guoquan <wangguoquan03@foxmail.com>
2024-09-10 11:33:20 +01:00
Richard W.M. Jones
3d2061b83c daemon/inspect_fs.ml: Sort lists of distros
No change, just tidying up the lists.
2024-07-11 13:12:41 +01:00
grass-lu
a429ab3319 Kylin is centos derivative
Acked-by: Richard W.M. Jones <rjones@redhat.com>
2024-07-11 13:12:24 +01:00
Richard W.M. Jones
e616c8f286 inspection: Resolve PARTUUID= and PARTLABEL= in /etc/fstab
Fixes: https://issues.redhat.com/browse/RHEL-46596
2024-07-08 14:44:01 +01:00
Richard W.M. Jones
1816651f3c New APIs: findfs_partuuid and findfs_partlabel
These search for partitions by UUID or label (name).  They only work
for GPT.
2024-07-08 14:44:01 +01:00
Richard W.M. Jones
4c5c0782af daemon/findfs.ml: Fix whitespace 2024-07-08 14:37:06 +01:00
Richard W.M. Jones
24c1f7b03a daemon: Fix parsing in part_get_gpt_attributes
The actual output of sfdisk --part-attrs is bizarre and doesn't match
the documentation.  After looking at the source from util-linux, fix
the parsing to match what sfdisk produces.

Reported-by: Yongkui Guo
Fixes: commit c6c266a85d
Fixes: https://issues.redhat.com/browse/RHEL-35998
2024-06-28 09:42:20 +01:00
Jonatan Pålsson
465be22d9b daemon: cryptsetup_open: Add --cipher
This allows passing the --cipher argument to cryptsetup as an optional
parameter.
2024-06-20 07:42:55 +02:00
Richard W.M. Jones
c6c266a85d daemon: Reimplement partition GPT functions using sfdisk
sfdisk can now do everything with GPT that sgdisk was needed for
before.  In particular we are able to reimplement the following
functions using sfdisk:

- part_set_disk_guid   (replace with sfdisk --disk-id)
- part_get_disk_guid
- part_set_disk_guid_random
- part_set_gpt_attributes           (sfdisk --part-attrs)
- part_get_gpt_attributes
- part_set_gpt_guid                 (sfdisk --part-uuid)
- part_get_gpt_guid
- part_set_gpt_type                 (sfdisk --part-type)
- part_get_gpt_type

This allows us to drop the requirement for gdisk in many cases.

There is only one API remaining which requires gdisk, part_expand_gpt,
which we do not use in our tools.  In a prior commit I already moved
this solitary function to a new source file (daemon/gdisk.c).

Fixes: https://issues.redhat.com/browse/RHEL-35998
2024-05-10 16:25:13 +01:00