Commit Graph

26 Commits

Author SHA1 Message Date
Pino Toscano
afa8111b75 OCaml tools: add output selection for --machine-readable
Add an optional argument for --machine-readable to select the output,
adding a new function to specifically write data to that output stream.
The possible choices are:
* --machine-readable: to stdout, like before
* --machine-readable=file:name-of-file: to the specified file
* --machine-readable=stream:stdout: explicitly to stdout
* --machine-readable=stream:stderr: explicitly to stderr

Adapt all the OCaml-based tools to use the new function, so the
--machine-readable choice is respected.
2018-08-23 18:03:39 +02:00
Pino Toscano
2fa78e6dce OCaml tools: simplify machine-readable handling
Make use of the helper provided by Tools_utils.create_standard_options,
so there is no need to implement the logic in each tool.

This affects all the OCaml tools with --machine-readable, namely:
virt-builder, virt-builder-repository, virt-dib, virt-get-kernel,
virt-resize, virt-sparsify, and virt-v2v.
2018-08-20 22:25:55 +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
0970bd0608 ocaml: Avoid Warning 52 for URI.parse_uri function.
This avoids warning 52 in OCaml code such as:

  try URI.parse_uri arg
  with Invalid_argument "URI.parse_uri" -> ...

which prints:

  Warning 52: Code should not depend on the actual values of
  this constructor's arguments. They are only for information
  and may change in future versions. (See manual section 8.5)

In the long term we need to change fish/uri.c so that we can throw
proper errors.
2017-10-05 11:32:54 +01:00
Richard W.M. Jones
c7651744da ocaml: Replace pattern matching { field = field } with { field }.
If you have a struct containing ‘field’, eg:

  type t = { field : int }

then previously to pattern-match on this type, eg. in function
parameters, you had to write:

  let f { field = field } =
    (* ... use field ... *)

In OCaml >= 3.12 it is possible to abbreviate cases where the field
being matched and the variable being bound have the same name, so now
you can just write:

  let f { field } =
    (* ... use field ... *)

(Similarly for a field prefixed by a Module name you can use
‘{ Module.field }’ instead of ‘{ Module.field = field }’).

This style is widely used inside the OCaml compiler sources, and is
briefer than the long form, so it makes sense to use it.  Furthermore
there was one place in virt-dib where we are already using this new
style, so the old code did not compile on OCaml < 3.12.

See also:
https://forge.ocamlcore.org/docman/view.php/77/112/leroy-cug2010.pdf
2017-10-05 11:32:54 +01:00
Richard W.M. Jones
cd304ad838 common/mltools: Rename Common_utils to Tools_utils.
Reflecting the purpose of this module now, which is to act as a place
for utility functions shared only by the OCaml virt tools.
2017-09-28 14:39:23 +01:00
Richard W.M. Jones
61d4891ef4 mllib: Split ‘Common_utils’ into ‘Std_utils’ + ‘Common_utils’.
The new module ‘Std_utils’ contains only functions which are pure
OCaml and depend only on the OCaml stdlib.  Therefore these functions
may be used by the generator.

The new module is moved to ‘common/mlstdutils’.

This also removes the "<stdlib>" hack, and the code which copied the
library around.

Also ‘Guestfs_config’, ‘Libdir’ and ‘StringMap’ modules are moved
since these are essentially the same.

The bulk of this change is just updating files which use
‘open Common_utils’ to add ‘open Std_utils’ where necessary.
2017-07-10 17:01:59 +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
55bf7de97c Update copyright dates for 2017
Run the following command over the source:

  perl -pi.bak -e 's/(20[01][0-9])-2016/$1-2017/g' `git ls-files`

(Thanks Rich for the perl snippet, as used in past years.)
2017-01-03 16:48:21 +01:00
Pino Toscano
6b26a0cce4 OCaml tools: add crypto support (RHBZ#1362649)
Make use of the additional command line arguments, and API needed to
decrypt LUKS partitions.

This affects only virt-customize, virt-get-kernel, virt-sparsify, and
virt-sysprep, as they are the main OCaml tools interacting with
user-provided images.
2016-09-19 19:49:51 +02:00
Richard W.M. Jones
ba2cf9a2eb mllib: Use L"..." and S '...' for long and short options.
There is also an M"..." variant which acts the same way as
L but handles the annoying virt-v2v two letter options.
2016-07-18 17:01:22 +01:00
Pino Toscano
0f7bf8f714 OCaml tools: add and use a Getopt module
Add a new Getopt module to mllib, to parse command line arguments with
handlers close to the ones used with Arg, but using getopt(3) (actually
getopt_long_only) to do the real parsing.  This allow us to provide
options for OCaml tools with a syntax similar to the C tools, and use
the additional features getopt offers and Arg does not.
Getopt now handles every part of the command line handling, including
the output of short & long options.

Do a single-step conversion of Common_utils and all the OCaml tools to
the syntax of Getopt.

Move a couple of utility functions from Common_utils to Getopt, since
they fit better there (and Common_utils cannot be used in Getopt, as
the former already uses the latter).

As side-change due to the conversion, extra arguments for sysprep
operation can have more keys for the same argument.
2016-07-13 18:43:32 +02:00
Pino Toscano
35fa141522 get-kernel: use "auto" as real autodetection marker (RHBZ#1341984) 2016-06-08 16:12:49 +02:00
Pino Toscano
82b0d25bb2 mllib: factor out mounting of guest root
Introduce and use a new inspect_mount_root function to mount all the
mountpoints of a root in the guest, replacing the same code doing that
in different tools.

inspect_mount_root_ro is inspect_mount_root with readonly mount option.
2016-03-03 14:52:39 +01:00
Pino Toscano
14b8e67f0b sysprep, get-kernel: explicit the Guestfs parameter
Help the OCaml compiler by expliciting Guestfs.guestfs as type for 'g'
in some functions.
2016-02-04 10:22:18 +01:00
Richard W.M. Jones
307c83177c Update copyright dates for 2016.
Run the following command over the source:

  perl -pi.bak -e 's/(20[01][0-9])-2015/$1-2016/g' `git ls-files`
2016-01-02 21:19:51 +00:00
Pino Toscano
4cc98d8f6e OCaml tools: use open_guestfs everywhere
Instead of creating Guestfs handles and manually apply common options
(e.g. debug and trace), use the open_guestfs in Common_utils.
This also applies the common options to handles which didn't set them
before, so we can inspect also their messages if needed.
2015-11-10 11:38:40 +01:00
Richard W.M. Jones
0944acf90c mllib: Replace various ad hoc string_* functions with String.*
This is just a straight refactoring.  Various ad hoc string_*
functions that appeared in Common_utils have been renamed and placed
in the String.* namespace.  The old vs "new" functions are:

string_prefix  -> String.is_prefix
string_suffix  -> String.is_suffix
string_find    -> String.find
replace_str    -> String.replace
string_nsplit  -> String.nsplit
string_split   -> String.split
string_lines_split -> String.lines_split
string_random8 -> String.random8
2015-10-06 13:40:34 +01:00
Pino Toscano
cf3d93c826 get-kernel: respect -q
Now that virt-get-kernel has -q/--quiet, support it by printing the
download messages only if not specified.
2015-09-02 00:25:55 +02:00
Pino Toscano
45f6cfe1a1 get-kernel: refactor list and download code
Split in an own function the code dong the mounting, the inspection of
the kernel files, and the downloading, including a per-OS function for
the actual kernel files.  This gives few advantages:
- the download phease is not repeated for all the files to fetch
- it is easier to eventually support multi-root disk images
- it is possible to support OSes different than Linux; virt-get-kernel
  now will just report the unsupported OS, instead of a generic
  "no kernel found" message

This is mostly code refactoring, with (on Linux) no actual behaviour
change.
2015-09-02 00:25:55 +02:00
Pino Toscano
997c59139e get-kernel: split command line handling in own function
Simple refactoring, no actual behaviour changes.
2015-09-02 00:25:55 +02:00
Pino Toscano
2af6e48ba3 mllib: add and use set_standard_options
Introduce a new common helper to add the common options for libguestfs
tools (short/long options, version, verbose, trace), and sort them.
All the OCaml-based tools had these options already, so there are no
functional changes in the interface they provide.

The only difference is that now the options are always sorted, while
some tools didn't had them like that previously: because of this, a
couple of ditto markers (as descriptions) don't match what's above them
anymore, and thus their full description is put instead.
2015-08-31 18:25:48 +02:00
Pino Toscano
1c43c0800f get-kernel: remove extra progname from error messages
error() prints it already
2015-08-06 15:15:40 +02:00
Pino Toscano
54c7d3f61e get-kernel: add --prefix
New --prefix option to specify a prefix for the extracted file names.
2015-06-15 18:07:52 +02:00
Pino Toscano
cdb1e6eeca get-kernel: add --unversioned-names
New --unversioned-names option to save extracted files just with their
base name (i.e. "vmlinux", "initrd.img").
2015-06-15 18:07:52 +02:00
Pino Toscano
1183809f4d (Almost) new tool: virt-get-kernel
Extract the guest kernel/ramdisk extraction from virt-builder into a
separate utility, so it can be used and improved without cluttering
virt-builder.

Currently it does what virt-builder --get-kernel did, adding also the
options for remote disks and libvirt access, much like other libguestfs
tools.

virt-builder --get-kernel now just spawns virt-get-kernel.
2015-06-15 18:07:03 +02:00