82 Commits

Author SHA1 Message Date
Richard W.M. Jones
7e974b509c lib: Use guestfs_int_make_temp_path in a few more places.
There were various places in the library where we open coded making
temporary filenames.  This uses the utility function instead.
2017-09-21 18:05:07 +01:00
Richard W.M. Jones
0734afd41f lib: Fix guestfs_int_download_to_tmp.
Since commit 65cfecb0f5,
‘guestfs_int_download_to_tmp’ was buggy because it did not deal with
the case where a guest had multiple roots.  It cached the downloaded
file under a single name which was not distinguished by which root we
were looking at.  As a result, if you inspected (eg.) the icon on such
a guest, then in some circumstances the same icon could be returned
for all roots (incorrectly).

This changes the function in a few ways:

 - Don't cache downloaded files.  It wasn't a useful feature of the
   function in most scenarios.  Instead a new name is generated for
   every call.

 - Use guestfs_int_make_temp_path.

 - Allow an extension to be specified.
2017-09-21 18:05:07 +01:00
Richard W.M. Jones
0ec0af27c5 lib: Allow an extension to be specified in guestfs_int_make_temp_path.
In the function ‘guestfs_int_make_temp_path’, allow an optional
extension to be specified.  For example:

  r = guestfs_int_make_temp_path (g, "ls", "txt");

will create paths like ‘<tmpdir>/ls123.txt’.

NULL can also be passed for the extension, which means no extension.
2017-09-21 18:05:07 +01:00
Richard W.M. Jones
41df9aaf90 lib: Move guestfs_int_download_to_tmp and remove inspect.c.
Move the last remaining function ‘guestfs_int_download_to_tmp’ to
lib/inspect-icon.c (the main, but not only user of this function).
Then remove lib/inspect.c entirely.

This is not quite code motion because I updated the comment for the
function to reflect what it does in reality.
2017-09-21 18:05:07 +01:00
Richard W.M. Jones
26a5916a24 lib: Move guestfs_int_parse_unsigned_int to version.c
Just code motion, but this function seems to make more sense in
version.c since that is the main (but not only) user.
2017-09-21 18:05:07 +01:00
Richard W.M. Jones
439a5cf57b lib: Remove unused function guestfs_int_parse_unsigned_int_ignore_trailing.
Not used by any current code, removed.

Last use of the function was seen in
commit 65cfecb0f5.
2017-09-21 18:05:07 +01:00
Richard W.M. Jones
9679f608b2 threads: Update documentation in guestfs(3) to describe the new behaviour. 2017-09-16 23:06:25 +01:00
Richard W.M. Jones
b088eb5ef8 threads: Use thread-local storage for errors.
We permit the following constructs in libguestfs code:

  if (guestfs_some_call (g) == -1) {
    fprintf (stderr, "failed: error is %s\n", guestfs_last_error (g));
  }

and:

  guestfs_push_error_handler (g, NULL, NULL);
  guestfs_some_call (g);
  guestfs_pop_error_handler (g);

Neither of these would be safe if we allowed the handle to be used
from threads concurrently, since the error string or error handler
could be changed by another thread.

Solve this in approximately the same way that libvirt does: by making
the error, current error handler, and stack of error handlers use
thread-local storage (TLS).

The implementation is not entirely straightforward, mainly because
POSIX doesn't give us useful destructor behaviour, so effectively we
end up creating our own destructor using a linked list.

Note that you have to set the error handler in each thread separately,
which is an API change (eg: if you set the error handler in one
thread, then pass the handle 'g' to another thread, the error handler
in the second thread appears to have reset itself back to the default
error handler).  I haven't yet worked out a better way to solve this.
2017-09-16 23:06:25 +01:00
Richard W.M. Jones
b9847b404c threads: Acquire and release the lock around each public guestfs_* API.
Acquire the per-handle lock on entering each public API function.

The lock is released by a cleanup handler, so we only need to use the
ACQUIRE_LOCK_FOR_CURRENT_SCOPE macro at the top of each function.

Note this means we require __attribute__((cleanup)).  On platforms
where this is not supported, the code will probably hang whenever a
libguestfs function is called.

The only definitive list of public APIs is found indirectly in the
generator (in generator/c.ml : globals).
2017-09-16 23:06:25 +01:00
Richard W.M. Jones
d94a881d87 threads: Add a lock (a recursive mutex) to the handle.
Add a g->lock field.  This commit simply initializes and destroys the
lock on handle creation/free, and does nothing else.
2017-09-16 23:06:25 +01:00
Richard W.M. Jones
3a00c4d179 Remove inspection from the C library and switch to daemon/OCaml implementation. 2017-09-16 22:27:16 +01:00
Richard W.M. Jones
53ab995cf1 lib: inspect: Remove ‘fs’ parameter from ‘guestfs_int_download_to_tmp’.
After we move inspection code to the daemon, the library will no
longer have access to ‘struct inspect_fs’, and so we won't be able to
prefix downloads with the "root filesystem number".

Just remove this prefix (it's internal only).  However it does mean
that this function can no longer cache downloaded files.
2017-09-16 22:27:16 +01:00
Richard W.M. Jones
19940fc2bd New API: Deprecate hivex_value_utf8 and replace with hivex_value_string.
hivex has a function hivex_value_string.  We were not calling it under
the mistaken belief that because hivex implements this using iconv,
the function wouldn't work inside the daemon.  Instead we
reimplemented the functionality in the library.

This commit deprecates hivex_value_utf8 and removes the library side
code.  It replaces it with a plain wrapper around hivex_value_string.

Thanks: Pino Toscano
2017-09-16 22:27:16 +01:00
Richard W.M. Jones
65cfecb0f5 inspection: Deprecate APIs and remove support for inspecting installer CDs.
This just duplicated libosinfo information, and because it was never
tested it didn't work most of the time.
2017-09-16 22:27:16 +01:00
Pino Toscano
b06e7aa47d lib: qemu: ignore -Wnonnull also for GCC 4.8 and up
Versions older than 6 that have -Wnonnull need the flag too.

Fixes commit e2773e679f.
2017-09-15 17:49:57 +02:00
Richard W.M. Jones
e2773e679f lib: qemu: Also suppress -Wnonnull warning in yajl code.
Required by gcc-7.1.1.

Updates commit 3d2b84231f.
2017-09-14 19:29:59 +01:00
Richard W.M. Jones
9fe592808c launch: direct: Disable qemu locking when opening drives readonly (RHBZ#1417306). 2017-09-14 16:58:17 +01:00
Richard W.M. Jones
3d2b84231f lib: qemu: Add accessor to test if qemu does mandatory locking.
QEMU >= 2.10 started to do mandatory locking.  This checks the QMP
schema to see if we are using that version of qemu.  (Note it is
sometimes disabled in downstream builds, and it was also enabled in
upstream prereleases with version 2.9.9x, so we cannot just check the
version number).
2017-09-14 16:58:17 +01:00
Richard W.M. Jones
bf7d627305 lib: qemu: Allow parallel qemu binaries to be used with cache conflicts.
Rename the cache files like ‘qemu.stat’ etc so they include the qemu
binary "key" (ie. size and mtime) in the name.  This allows a single
user to use multiple qemu binaries in parallel without conflicts.
2017-09-14 16:58:16 +01:00
Richard W.M. Jones
a22eecbdb1 lib: qemu: Run QMP ‘query-qmp-schema’ against the qemu binary.
This adds an extra test using QMP (the QEMU Monitor Protocol).  This
allows us to get extra information about the qemu binary beyond what
is available from the version number or ‘qemu -help’.
2017-09-12 17:13:41 +01:00
Richard W.M. Jones
ea94f39e46 lib: qemu: Factor out common code for reading and writing cache files.
The previous code duplicated a lot of common code for reading and
writing the cache file per data field.  This change simply factors out
that common code.  This makes it simpler to add new tests in future.

This is just refactoring, it should have no effect.
2017-09-12 17:13:41 +01:00
Richard W.M. Jones
0eb1380ddc lib: qemu: Refactor guestfs_int_test_qemu so it doesn't return qemu version.
Rather unnecessarily this function returned the parsed qemu version.
This complicates further refactoring, so I have changed the function
not to return this, and instead there is a separate function you have
to call to get the version struct (‘guestfs_int_qemu_version’).

Apart from a tiny amount of extra copying this is simply refactoring
of the interface between the direct-mode backend and the qemu query
functions.
2017-09-12 10:57:32 +01:00
Richard W.M. Jones
410593e48b lib: command: Print command before running it with guestfs_int_cmd_pipe_run.
Unlike ordinary guestfs_int_cmd_run, the pipe version did not print
the command it was about to run when debugging was enabled.

Fixes commit e98bb86929.
2017-09-11 16:08:13 +01:00
Pino Toscano
5b60dd4eff launch: direct: limit kvm-pit.lost_tick_policy to x86
This QEMU property is specific to x86/x86_64, so add it only on these
architectures.
2017-09-01 14:05:38 +02:00
Nikos Skalkotos
c355b744bf New partition API: part_resize
This can be used to enlarge or shrink an existing partition.
2017-07-31 10:51:18 +02:00
Richard W.M. Jones
4052f613d2 daemon: Reimplement ‘list_filesystems’ API in the daemon, in OCaml.
Move the list_filesystems API into the daemon, reimplementing it in
OCaml.  Since this API makes many other API calls, it runs a lot
faster in the daemon.
2017-07-27 22:31:22 +01:00
Richard W.M. Jones
b48da89dd6 daemon: Reimplement ‘file_architecture’ API in OCaml.
The previously library-side ‘file_architecture’ API is reimplemented
in the daemon, in OCaml.

There are some significant differences compared to the C
implementation:

 - The C code used libmagic.  That is replaced by calling the ‘file’
   command (because that is simpler than using the library).

 - The C code had extra cases to deal with compressed files.  This is
   not necessary since the ‘file’ command supports the ‘-z’ option
   which transparently looks inside compressed content (this is a
   consequence of the change above).

This commit demonstrates a number of techniques which will be useful
for moving inspection code to the daemon:

 - Moving an API from the C library to the OCaml daemon.

 - Calling from one OCaml API inside the daemon to another (from
   ‘Filearch.file_architecture’ to ‘File.file’).  This can be done and
   is done with C daemon APIs but correct reply_with_error handling is
   more difficult in C.

 - Use of Str for regular expression matching within the appliance.
2017-07-27 22:31:22 +01:00
Richard W.M. Jones
98641e26c8 lib: Move guestfs_int_parse_unsigned_int* functions.
Just code motion, no functional change.
2017-07-18 13:22:00 +01:00
Richard W.M. Jones
f60f8c47cc lib: Move implementation of ‘hivex_value_utf8’ to new file ‘lib/hivex.c’.
Just a code movement, no change.
2017-07-14 14:35:37 +01:00
Richard W.M. Jones
f30b2065a2 gobject: Add outline guestfs-gobject(3) manual page.
Since we removed gtk doc, we might as well replace it with a
manual page explaining the basics of how to run gjs.
2017-07-10 17:03:19 +01:00
Richard W.M. Jones
e6c89f9631 utils: Rename ‘guestfs-internal-frontend.h’ to ‘guestfs-utils.h’.
The reason it's not just ‘utils.h’ is because Pino is worried that we
might pick up /usr/include/utils.h from a rogue library.
2017-07-10 17:01:59 +01:00
Richard W.M. Jones
5efebd8c7e utils: Split out structs cleanups and printing into common/structs.
These won't be used by the daemon, so interferes with us using
common/utils in the daemon, so they are moved to a different library.
2017-07-10 17:01:59 +01:00
Richard W.M. Jones
7489d22843 common/utils: Move ‘uefi.c’ to ‘lib/’.
This was only used inside the library, so move it there.
2017-07-10 17:01:59 +01:00
Richard W.M. Jones
d96209ef07 lib: libvirt: Pass copyonread flag through to the libvirt XML (RHBZ#1466563).
We were dropping the add_drive copyonread flag when using the libvirt
backend.  This resulted in significant performance degradation (2x-3x
slower) when running virt-v2v against VMware servers.

Thanks: Kun Wei.
2017-07-01 11:40:12 +01:00
Pavel Butsykin
40fcb3e4d2 launch: add support for autodetection of appliance image format
This feature allows you to use different image formats for the fixed
appliance. The raw format is used by default.

Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>
2017-06-29 12:13:12 +01:00
Richard W.M. Jones
db8fdfd8d6 lib: direct, uml: Refactor unblocking SIGTERM code.
A small refactoring of
commit 1f1cd11cf0.
2017-06-13 08:49:05 +01:00
Richard W.M. Jones
92995c5564 lib: Move close_file_descriptors into guestfs-internal.h.
This macro is only used by the library, so move it to
lib/guestfs-internal.h.
2017-06-13 08:42:29 +01:00
Richard W.M. Jones
1f1cd11cf0 lib: direct, uml: Unblock SIGTERM in the hypervisor and recovery processes (RHBZ#1460338).
If SIGTERM is blocked in the main program, then it ends up still being
blocked in the subprocess after we fork.  This means that we cannot
kill qemu by sending SIGTERM to it.  This commit fixes the problem by
unblocking SIGTERM unconditionally after fork.

Thanks: wtfuzz on IRC for reporting and analysis.
2017-06-12 13:24:26 +01:00
Richard W.M. Jones
5856323e6f lib: create: Allow any [[:alnum:]]+ string as a backingfmt parameter (RHBZ#1459979).
If you use the libguestfs tools which open disk images read-only
(eg. virt-df), with formats such as 'vdi', then you will see an error:

  error: invalid value for backingformat parameter 'vdi'

This is because opening a disk image read-only will try to create a
qcow2 file with the original image as a backing file.  However the
list of permitted backing formats was very restrictive and did not
include 'vdi' (nor many other uncommon formats).

Instead of using a whitelist for backing formats, just validate that
the string is alphanumeric and short.

Thanks: Mike Goodwin for reporting the bug.
2017-06-09 12:57:03 +01:00
Richard W.M. Jones
a5bd493e3f lib: Add VIRTIO_DEVICE_NAME macro to handle virtio-mmio vs CCW vs virtio-PCI.
Also move the macro into lib/launch-direct.c, since it is not used
anywhere else (libvirt handles the differences in the libvirt
backend).
2017-05-18 19:02:01 +01:00
Richard W.M. Jones
ddde6f9e2f s390x: launch: direct: Use virtio-*-ccw on this architecture.
PCI devices don't exist/work.  You would see errors such as:

qemu-system-s390x: -device virtio-rng-pci,rng=rng0: MSI-X support is mandatory in the S390 architecture
2017-05-18 19:02:01 +01:00
Richard W.M. Jones
5fc76d6d10 s390x: appliance: Use /dev/ttysclp0 for serial console. 2017-05-18 19:02:01 +01:00
Richard W.M. Jones
ade2652bda s390x: launch: direct: Use sclp as serial console on this architecture.
The same change to the direct backend as made to the libvirt backend
in the previous commit.
2017-05-18 19:02:01 +01:00
Richard W.M. Jones
6328f33236 s390x: launch: libvirt: Use <console> device sclp for appliance debug messages (RHBZ#1376547).
Thanks: Cole Robinson, Dan Horak, Thomas Huth.
2017-05-18 19:02:01 +01:00
Richard W.M. Jones
0071a6e146 launch: direct: Reimplement command line handling using qemuopts library. 2017-05-08 11:14:46 +01:00
Richard W.M. Jones
bb5ffd7497 configure: Drop --with-qemu-options / QEMU_OPTIONS.
In its current form this is very hard to implement because it requires
us to "unparse" the options, including removing any shell quoting.

It wasn't implemented at all for the libvirt backend.

Also contrary to the documentation, the configure script did not use
these options for testing, but constructed its own set of qemu test
options.
2017-05-08 11:14:46 +01:00
Richard W.M. Jones
9013a93d0e lib: Remove internal details of device name translation from API docs.
Remove much of the text detailing how device name translation
happened.  Since we removed support for virtio-blk
(commit 9e0294f88f) and deprecated the
‘iface’ parameter, only /dev/sdX device names should be visible
through the public APIs, both in parameters and in return values from
calls like guestfs_list_devices and guestfs_list_partitions.

Note the above is in fact not true for the UML backend, but UML is
broken in the kernel and in any case this will be fixed later.

(cherry picked from commit 2727e589db216bf0731385966889a4f66dbfe225)
2017-05-08 11:14:45 +01:00
Richard W.M. Jones
04f757a708 launch: Error if you try to launch with too many drives.
In particular the virt-rescue --scratch option makes it very easy to
add huge numbers of drives.  Since the per-backend max_disks limit was
never checked anywhere you could get peculiar failures.  Now you'll
get a clear error message:

$ virt-rescue --scratch=256
libguestfs: error: too many drives have been added, the current backend only supports 255 drives
2017-05-08 11:14:45 +01:00
Pavel Butsykin
a9e6fdefc3 appliance: search all types of appliances for each path separately
This patch changes appliance search using paths with multiple directories. Now
all appliance checks will be done separately for each directory. For example
if the path LIBGUESTFS_PATH=/a:/b:/c, then all applainces are searched first in
/a, then in /b and then in /c. It allows to flexibly configure the libguestfs
to interact with different appliances.

Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>
2017-05-05 14:12:03 +01:00
Pavel Butsykin
0f79400c7f build: add ./configure --with-guestfs-path option
Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>
2017-05-04 13:24:39 +01:00