24 Commits

Author SHA1 Message Date
Richard W.M. Jones
7833461af7 generator: Deprecate xfs_info (replaced by xfs_info2)
Deprecate this function, and suggest using xfs_info2 as its
replacement.
2026-01-26 14:40:39 +00:00
Richard W.M. Jones
02b64d5cec generator: Use quoted string literals in many places
This change was done almost entirely automatically using the script
below.  This uses the OCaml lexer to read the source files and extract
the strings and locations.  Strings which are "candidates" (in this
case, longer than 3 lines) are replaced in the output with quoted
string literals.

Since the OCaml lexer is used, it already substitutes all escape
sequences correctly.  I diffed the output of the generator and it is
identical after this change, except for UUIDs, which change because of
how Utils.stable_uuid is implemented.

Thanks: Nicolas Ojeda Bar

$ ocamlfind opt -package unix,compiler-libs.common find_strings.ml \
                -o find_strings.opt -linkpkg
$ for f in $( git ls-files -- \*.ml ) ; do ./find_strings.opt $f ; done

open Printf

let read_whole_file path =
  let buf = Buffer.create 16384 in
  let chan = open_in path in
  let maxlen = 16384 in
  let b = Bytes.create maxlen in
  let rec loop () =
    let r = input chan b 0 maxlen in
    if r > 0 then (
      Buffer.add_substring buf (Bytes.to_string b) 0 r;
      loop ()
    )
  in
  loop ();
  close_in chan;
  Buffer.contents buf

let count_chars c str =
  let count = ref 0 in
  for i = 0 to String.length str - 1 do
    if c = String.unsafe_get str i then incr count
  done;
  !count

let subs = ref []

let consider_string str loc =
  let nr_lines = count_chars '\n' str in
  if nr_lines > 3 then
    subs := (str, loc) :: !subs

let () =
  Lexer.init ();
  let filename = Sys.argv.(1) in
  let content = read_whole_file filename in
  let lexbuf = Lexing.from_string content in
  let rec loop () =
    let token = Lexer.token lexbuf in
    (match token with
     | Parser.EOF -> ();
     | STRING (s, loc, sopt) ->
        consider_string s loc; (* sopt? *)
        loop ();
     | token ->
        loop ();
    )
  in
  loop ();

  (* The list of subs is already reversed, which is convenient
   * because we must the file substitutions in reverse order.
   *)
  let subs = !subs in
  let new_content = ref content in
  List.iter (
    fun (str, loc) ->
      let { Location.loc_start = { pos_cnum = p1 };
            loc_end = { pos_cnum = p2 } } = loc in
      let len = String.length !new_content in
      let before = String.sub !new_content 0 (p1-1) in
      let after = String.sub !new_content (p2+1) (len - p2 - 1) in
      new_content := before ^ "{|" ^ str ^ "|}" ^ after
  ) subs;

  let new_content = !new_content in

  if content <> new_content then (
    (* Update the file in place. *)
    let new_filename = filename ^ ".new"
    and backup_filename = filename ^ ".bak" in
    let chan = open_out new_filename in
    fprintf chan "%s" new_content;
    close_out chan;
    Unix.rename filename backup_filename;
    Unix.rename new_filename filename
  )
2025-09-01 17:08:52 +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
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
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
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
Laszlo Ersek
f68eaee1d6 lib: drive_create_data, drive: remove field "iface"
Representing "iface" in the "drive_create_data" and "drive" structures is
now useless; the direct backend ignores "iface", while the libvirt one
rejects it unless it is empty. Unify both backends -- make them both
ignore "iface". (Which only relaxes the libvirt backend, so it cannot
cause compatibility problems.) This lets us remove the fields. Update the
documentation as well.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1844341
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20220504134155.11832-3-lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
2022-05-05 13:05:19 +02:00
Laszlo Ersek
3eb830dbae lib: launch-direct: ignore drive "iface" parameter
Rich said in <https://bugzilla.redhat.com/show_bug.cgi?id=1844341#c1>:

> The libvirt backend has never allowed the iface parameter.  We should
> probably ignore it in the direct backend since it's never been possible
> to use this parameter correctly.

Remove the handling of "iface" in the direct (QEMU) backend. Refresh the
documentation regarding both backends.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1844341
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20220504134155.11832-2-lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
2022-05-05 13:05:07 +02:00
Richard W.M. Jones
4256737227 lib: Remove drive hotplugging support
This was a feature that allowed you to add drives to the appliance
after launching it.  It was complicated to implement, and only worked
for the libvirt backend (not "direct", which is the default backend).

It also turned out to be a bad idea.  The original concept was that
appliance creation was slow, so to examine multiple guests you should
launch the handle once then hot-add the disks from each guest in turn
to manipulate them.  However this is terrible from a security point of
view, especially for multi-tenant, because the drives from one guest
might compromise the appliance and thus the filesystems/drives from
subsequent guests.

It also turns out that hotplugging is very slow.  Nowadays appliance
creation should be faster than hotplugging.

The main use case for this was virt-df, but virt-df no longer uses it
after we discovered the problems outlined above.
2022-03-09 09:28:02 +00:00
Richard W.M. Jones
55be87367d lib: Remove 9p APIs
These APIs were an experimental feature for passing through 9p
filesystems from the host to the libguestfs appliance.  It was never
possible to use this without hacking the qemu command line of the
appliance to add such drives by hand.  It also didn't fit the
libguestfs model very well.  And 9p is generally deprecated in
upstream qemu.

Note that for ABI reasons these APIs are not actually removed, they
have been changed so that they always return an error.  These APIs
were actually hard-removed from all versions of RHEL.

See-also: https://bugzilla.redhat.com/921710
2022-03-09 09:28:02 +00:00
Richard W.M. Jones
6d32773e81 tests: Run the tests in parallel.
Before this change the tests ran in about 12m34 and afterwards in
about 6m20, although the real change is more dramatic if you only run
tests from the tests/ subdirectory (as language tests still run serially).

This breaks valgrinding for now, which I intend to fix properly later.
2021-03-18 16:28:55 +00:00
Richard W.M. Jones
c456ea0332 New APIs: cryptsetup-open and cryptsetup-close.
This commit deprecates luks-open/luks-open-ro/luks-close for the more
generic sounding names cryptsetup-open/cryptsetup-close, which also
correspond directly to the cryptsetup commands.

The optional cryptsetup-open readonly flag is used to replace the
functionality of luks-open-ro.

The optional cryptsetup-open crypttype parameter can be used to select
the type (corresponding to cryptsetup open --type), which allows us to
open BitLocker-encrypted disks with no extra effort.  As a convenience
the crypttype parameter may be omitted, and libguestfs will use a
heuristic (based on vfs-type output) to try to determine the correct
type to use.

The deprecated functions and the new functions are all (re-)written in
OCaml.

There is no new test here, unfortunately.  It would be nice to test
Windows BitLocker support in this new API, however the Linux tools do
not support creating BitLocker disks, and while it is possible to
create one under Windows, the smallest compressed disk I could create
is 37M because of a mixture of the minimum support size for BitLocker
disks and the fact that encrypted parts of NTFS cannot be compressed.

Also synchronise with common module.
2020-10-12 10:44:08 +01:00
Richard W.M. Jones
0e17236d7d Update copyright dates to 2020. 2020-03-06 19:32:32 +00:00
Pino Toscano
0c261637f9 Fix small issues in documentations of APIs
- fix names of arguments & optional arguments in C<..> markers
- use https for URLs where possible
- fix links to other guestfs APIs
- use more C<..> markers for special tests, shell commands, values of
  arguments, and names of fields
- link to command man pages where an explicit command is mentioned
- fix few incorrect documentation bits
2019-08-13 07:43:42 +02: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
55dfcb2211 New API: lvm_scan, deprecate vgscan (RHBZ#1602353).
The old vgscan API literally ran vgscan.  When we switched to using
lvmetad (in commit dd162d2cd5) this
stopped working because lvmetad now ignores plain *scan commands
without the --cache option.

We documented that vgscan would rescan PVs, VGs and LVs, but without
activating them.

I have introduced a new API (lvm_scan) which scans or rescans PVs, VGs
and LVs.  It has an optional activate parameter allowing activation of
any new LVs that are found.

With lvmetad this nicely maps to the single command:

 pvscan --cache [--activate ay]
2018-07-26 12:02:59 +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
Richard W.M. Jones
b4728fd004 generator: Annotate returned strings which are devices or mountables.
Previously the generator did not change any string returned from the
daemon.  Thus guestfs_list_devices (for example) might return internal
device names like /dev/vda (if virtio-blk was in use).

This changes calls to the daemon so that returned strings are
annotated as plain strings, devices or mountables:

    old               --->     new
  RString "uuid"             RString (RPlainString "uuid")
  RString "device"           RString (RDevice "device")
  RString "fs"               RString (RMountable "fs")

For hash tables, keys and values must be annotated separately.  For
example a hash table of mountables (keys) -> plain strings (values)
would be annotated like this:

    old               --->     new
  RHashtable "fses"          RHashtable (RMountable, RPlainString, "fses")

The daemon calls reverse_device_name_translation (currently a no-op)
for devices and mountables.

Note that this has no effect for calls which are handled on the
library side.

(cherry picked from commit 6b77cc196ecb8d7e1d73592ef65a189a7412c97c)
2017-05-08 11:14:45 +01:00
Richard W.M. Jones
30411ef623 generator: Simplify the handling of string parameters.
Previously we had lots of types like String, Device, StringList,
DeviceList, etc. where Device was just a String with magical
properties (but only inside the daemon), and DeviceList was just a
list of Device strings.

Replace these with some simple top-level types:

  String
  StringList

and move the magic into a subtype.

The change is mechanical, for example:

    old                     --->    new
  FileIn "filename"               String (FileIn, "filename")
  DeviceList "devices"            StringList (Device, "devices")

Handling BufferIn is sufficiently different from a plain String
throughout all the bindings that it still uses a top-level type.
(Compare with FileIn/FileOut where the only difference is in the
protocol, but the bindings can uniformly treat it as a plain String.)

There is no semantic change, and the generated files are identical
except for a minor change in the (deprecated) Perl
%guestfs_introspection table.
2017-05-03 19:26:18 +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
Richard W.M. Jones
67ce5342c2 generator: Allow actions to be deprecated with no replacement.
There is precisely one such function at the moment
(guestfs_wait_ready).
2017-03-03 16:56:52 +00:00
Richard W.M. Jones
da6ea89371 generator: Move some deprecated functions to actions_core_deprecated.ml.
Fixes commit 97773d2bbe.
2017-03-03 15:03:48 +00:00
Richard W.M. Jones
3a4a491712 generator: Put all the daemon procedure numbers (proc_nr) into a single table.
Daemon 'proc_nr's have to be assigned monotonically and uniquely to
each daemon function.  However in practice it can be difficult to work
out which is the next free proc_nr.  Placing all of them into a single
table in a new file (proc_nr.ml) should make this easier.
2017-02-21 17:23:21 +00:00
Richard W.M. Jones
97773d2bbe generator: Group and move APIs from actions.ml into actions_*.ml.
Group the APIs logically and move them into new modules:

Actions_core:
  Core APIs and anything that doesn't fit into another group, eg. launch.
  (With some more effort this could be split further.)

Actions_augeas:
  Augeas APIs, eg. aug-init.

Actions_debug:
  Debug APIs.

Actions_hivex:
  Hivex APIs, eg. hivex-open.

Actions_inspection:
  Inspection APIs, eg. inspect-get-type.

Actions_properties:
  Handle properties, eg. set-hv, get-hv.

Actions_tsk:
  SleuthKit APIs, eg. filesystem-walk.

*_deprecated:
  All of the above modules have deprecated variants, where we
  place the deprecated actions.
2017-02-21 17:23:21 +00:00