guestfs-hacking man page

guestfs-hacking — extending and contributing to libguestfs


This manual page is for hackers who want to extend libguestfs itself.

Overview of the Source Code

Libguestfs source is located in the github repository https://github.com/libguestfs/libguestfs

Large amounts of boilerplate code in libguestfs (RPC, bindings, documentation) are generated. This means that many source files will appear to be missing from a straightforward git checkout. You have to run the generator ("./autogen.sh && make -C generator") in order to create those files.

Libguestfs uses an autotools-based build system, with the main files being configure.ac and Makefile.am. See "The Build System".

The generator subdirectory contains the generator, plus files describing the API. The src subdirectory contains source for the library. The appliance and daemon subdirectories contain the source for the code that builds the appliance, and the code that runs in the appliance respectively. Other directories are covered in the section "Source Code Subdirectories" below.

Apart from the fact that all API entry points go via some generated code, the library is straightforward. (In fact, even the generated code is designed to be readable, and should be read as ordinary code). Some actions run entirely in the library, and are written as C functions in files under src. Others are forwarded to the daemon where (after some generated RPC marshalling) they appear as C functions in files under daemon.

To build from source, first read the guestfs-building(1).

Source Code Subdirectories

There are a lot of subdirectories in the source tree! Which ones should you concentrate on first? src and daemon which contain the source code of the core library. generator is the code generator described above, so that is important. The Makefile.am in the root directory will tell you in which order the subdirectories get built. And then if you are looking at a particular tool (eg. v2v) or language binding (eg. python), go straight to that subdirectory, but remember that if you didn't run the generator yet, then you may find files which appear to be missing.

virt-alignment-scan(1) command and documentation.
The libguestfs appliance, build scripts and so on.
Bash tab-completion scripts.
Various build scripts used by autotools.
virt-builder(1) command and documentation.
The virt-cat(1), virt-filesystems(1), virt-log(1), virt-ls(1) and virt-tail(1) commands and documentation.
Outside contributions, experimental parts.
virt-customize(1) command and documentation.
The daemon that runs inside the libguestfs appliance and carries out actions.
virt-df(1) command and documentation.
virt-dib(1) command and documentation.
virt-diff(1) command and documentation.
Miscellaneous manual pages.
virt-edit(1) command and documentation.
C API example code.
guestfish(1), the command-line shell, and various shell scripts built on top such as virt-copy-in(1), virt-copy-out(1), virt-tar-in(1), virt-tar-out(1).
virt-format(1) command and documentation.
guestmount(1), FUSE (userspace filesystem) built on top of libguestfs.
The crucially important generator, used to automatically generate large amounts of boilerplate C code for things like RPC and bindings.
virt-get-kernel(1) command and documentation.
Gnulib is used as a portability library. A copy of gnulib is included under here.
virt-inspector(1), the virtual machine image inspector.
Logo used on the website. The fish is called Arthur by the way.
M4 macros used by autoconf. See "The Build System".
virt-make-fs(1) command and documentation.
Various libraries and common code used by virt-resize(1) and the other tools which are written in OCaml.
virt-p2v(1) command, documentation and scripts for building the virt-p2v ISO or disk image.
Translations of simple gettext strings.
The build infrastructure and PO files for translations of manpages and POD files. Eventually this will be combined with the po directory, but that is rather complicated.
virt-rescue(1) command and documentation.
virt-resize(1) command and documentation.
virt-sparsify(1) command and documentation.
Source code to the C library.
virt-sysprep(1) command and documentation.
Files and other test data used by the tests.
Test tool for end users to test if their qemu/kernel combination will work with libguestfs.
Used for temporary files when running the tests (instead of /tmp etc). The reason is so that you can run multiple parallel tests of libguestfs without having one set of tests overwriting the appliance created by another.
Command line tools written in Perl (virt-win-reg(1) and many others).
Miscellaneous utilities, such as "boot-benchmark".
virt-v2v(1) command and documentation.
The http://libguestfs.org website files.
Language bindings.

The Build System

Libguestfs uses the GNU autotools build system (autoconf, automake, libtool).

The ./configure script is generated from configure.ac and m4/guestfs_*.m4. Most of the configure script is split over many m4 macro files by topic, for example m4/guestfs_daemon.m4 deals with the dependencies of the daemon.

The job of the top level Makefile.am is mainly to list the subdirectories ("SUBDIRS") in the order they should be compiled.

common-rules.mk is included in every Makefile.am (top level and subdirectories). subdir-rules.mk is included only in subdirectory Makefile.am files.

There are many make targets. Use this command to list them all:

make help

Adding a New API

Because large amounts of boilerplate code in libguestfs are generated, this makes it easy to extend the libguestfs API.

To add a new API action there are two changes:

You need to add a description of the call (name, parameters, return type, tests, documentation) to generator/actions.ml.

There are two sorts of API action, depending on whether the call goes through to the daemon in the appliance, or is serviced entirely by the library (see "ARCHITECTURE" in guestfs-internals(3)). "guestfs_sync" in guestfs(3) is an example of the former, since the sync is done in the appliance. "guestfs_set_trace" in guestfs(3) is an example of the latter, since a trace flag is maintained in the handle and all tracing is done on the library side.

Most new actions are of the first type, and get added to the "daemon_functions" list. Each function has a unique procedure number used in the RPC protocol which is assigned to that action when we publish libguestfs and cannot be reused. Take the latest procedure number and increment it.

For library-only actions of the second type, add to the "non_daemon_functions" list. Since these functions are serviced by the library and do not travel over the RPC mechanism to the daemon, these functions do not need a procedure number, and so the procedure number is set to "-1".
Implement the action (in C):

For daemon actions, implement the function "do_<name>" in the "daemon/" directory.

For library actions, implement the function "guestfs_impl_<name>" in the "src/" directory.

In either case, use another function as an example of what to do.

After making these changes, use "make" to compile.

Note that you don't need to implement the RPC, language bindings, manual pages or anything else. It's all automatically generated from the OCaml description.

Adding tests for an API

You can supply zero or as many tests as you want per API call. The tests can either be added as part of the API description (generator/actions.ml), or in some rarer cases you may want to drop a script into "tests/*/". Note that adding a script to "tests/*/" is slower, so if possible use the first method.

The following describes the test environment used when you add an API test in actions.ml.

The test environment has 4 block devices:

/dev/sda 2 GB
General block device for testing.
/dev/sdb 2 GB
/dev/sdb1 is an ext2 filesystem used for testing filesystem write operations.
/dev/sdc 10 MB
Used in a few tests where two block devices are needed.
ISO with fixed content (see images/test.iso).

To be able to run the tests in a reasonable amount of time, the libguestfs appliance and block devices are reused between tests. So don't try testing "guestfs_kill_subprocess" in guestfs(3) :-x

Each test starts with an initial scenario, selected using one of the "Init*" expressions, described in generator/types.ml. These initialize the disks mentioned above in a particular way as documented in types.ml. You should not assume anything about the previous contents of other disks that are not initialized.

You can add a prerequisite clause to any individual test. This is a run-time check, which, if it fails, causes the test to be skipped. Useful if testing a command which might not work on all variations of libguestfs builds. A test that has prerequisite of "Always" means to run unconditionally.

In addition, packagers can skip individual tests by setting environment variables before running "make check".


eg: "SKIP_TEST_COMMAND_3=1" skips test #3 of "guestfs_command" in guestfs(3).



eg: "SKIP_TEST_ZEROFREE=1" skips all "guestfs_zerofree" in guestfs(3) tests.

Packagers can run only certain tests by setting for example:

TEST_ONLY="vfs_type zerofree"

See tests/c-api/tests.c for more details of how these environment variables work.

Debugging new APIs

Test new actions work before submitting them.

You can use guestfish to try out new commands.

Debugging the daemon is a problem because it runs inside a minimal environment. However you can fprintf messages in the daemon to stderr, and they will show up if you use "guestfish -v".

Adding a New Language Binding

All language bindings must be generated by the generator (see the generator subdirectory).

There is no documentation for this yet. We suggest you look at an existing binding, eg. generator/ocaml.ml or generator/perl.ml.

Adding tests for language bindings

Language bindings should come with tests. Previously testing of language bindings was rather ad-hoc, but we have been trying to formalize the set of tests that every language binding should use.

Currently only the OCaml and Perl bindings actually implement the full set of tests, and the OCaml bindings are canonical, so you should emulate what the OCaml tests do.

This is the numbering scheme used by the tests:

- 000+ basic tests:

  010  load the library
  020  create
  030  create-flags
  040  create multiple handles
  050  test setting and getting config properties
  060  explicit close
  065  implicit close (in GC'd languages)
  070  optargs
  080  version
  090  retvalues

- 100  launch, create partitions and LVs and filesystems

- 400+ events:

  410  close event
  420  log messages
  430  progress messages

- 800+ regression tests (specific to the language)

- 900+ any other custom tests for the language

To save time when running the tests, only 100, 430, 800+, 900+ should launch the handle.

Formatting Code

Our C source code generally adheres to some basic code-formatting conventions. The existing code base is not totally consistent on this front, but we do prefer that contributed code be formatted similarly. In short, use spaces-not-TABs for indentation, use 2 spaces for each indentation level, and other than that, follow the K&R style.

If you use Emacs, add the following to one of one of your start-up files (e.g., ~/.emacs), to help ensure that you get indentation right:

;;; In libguestfs, indent with spaces everywhere (not TABs).
;;; Exceptions: Makefile and ChangeLog modes.
(add-hook 'find-file-hook
    '(lambda () (if (and buffer-file-name
                         (string-match "/libguestfs\\>"
                         (not (string-equal mode-name "Change Log"))
                         (not (string-equal mode-name "Makefile")))
                    (setq indent-tabs-mode nil))))

;;; When editing C sources in libguestfs, use this style.
(defun libguestfs-c-mode ()
  "C mode with adjusted defaults for use with libguestfs."
  (c-set-style "K&R")
  (setq c-indent-level 2)
  (setq c-basic-offset 2))
(add-hook 'c-mode-hook
          '(lambda () (if (string-match "/libguestfs\\>"

Testing Your Changes

Turn warnings into errors when developing to make warnings hard to ignore:

./configure --enable-werror

Useful targets are:

"make check"
Runs the regular test suite.

This is implemented using the regular automake "TESTS" target. See the automake documentation for details.
"make check-valgrind"
Runs a subset of the test suite under valgrind.

See "Valgrind" below.
"make check-valgrind-local-guests"
Runs a subset of the test suite under valgrind using locally installed libvirt guests (read-only).
"make check-direct"
Runs all tests using default appliance back-end. This only has any effect if a non-default backend was selected using "./configure --with-default-backend=..."
"make check-valgrind-direct"
Run a subset of the test suite under valgrind using the default appliance back-end.
"make check-uml"

Runs all tests using the User-Mode Linux backend.

As there is no standard location for the User-Mode Linux kernel, you have to set "LIBGUESTFS_HV" to point to the kernel image, eg:

make check-uml LIBGUESTFS_HV=~/d/linux-um/vmlinux
"make check-valgrind-uml"
Runs all tests using the User-Mode Linux backend, under valgrind.

As above, you have to set "LIBGUESTFS_HV" to point to the kernel.
"make check-with-upstream-qemu"

Runs all tests using a local qemu binary. It looks for the qemu binary in QEMUDIR (defaults to $HOME/d/qemu), but you can set this to another directory on the command line, eg:

make check-with-upstream-qemu QEMUDIR=/usr/src/qemu
"make check-with-upstream-libvirt"

Runs all tests using a local libvirt. This only has any effect if the libvirt backend was selected using "./configure --with-default-backend=libvirt"

It looks for libvirt in LIBVIRTDIR (defaults to $HOME/d/libvirt), but you can set this to another directory on the command line, eg:

make check-with-upstream-libvirt LIBVIRTDIR=/usr/src/libvirt
"make check-slow"

Runs some slow/long-running tests which are not run by default.

To mark a test as slow/long-running:

Add it to the list of "TESTS" in the Makefile.am, just like a normal test.
Modify the test so it checks if the "SLOW=1" environment variable is set, and if not set it skips (ie. returns with exit code 77).
Add a variable "SLOW_TESTS" to the Makefile.am listing the slow tests.

Add a rule to the Makefile.am:

  $(MAKE) check TESTS="$(SLOW_TESTS)" SLOW=1
"make check-all"
Equivalent to running all "make check*" rules.
"make check-release"

Runs a subset of "make check*" rules that are required to pass before a tarball can be released. Currently this is:

"make installcheck"

Run "make check" on the installed copy of libguestfs.

The version of installed libguestfs being tested, and the version of the libguestfs source tree must be the same.


make clean ||:
make installcheck


When you do "make check-valgrind", it searches for any Makefile.am in the tree that has a "check-valgrind:" target and runs it.

Writing the Makefile.am and tests correctly to use valgrind and working with automake parallel tests is subtle.

If your tests are run via a shell script wrapper, then in the wrapper use:

$VG virt-foo

and in the Makefile.am use:

    make VG="@VG@" check

However, if your binaries run directly from the "TESTS" rule, you have to modify the Makefile.am like this:


    make VG="@VG@" check

In either case, check that the right program is being tested by examining the tmp/valgrind* log files carefully.

Submitting Patches

Submit patches to the mailing list: http://www.redhat.com/mailman/listinfo/… and CC to rjones@redhat.com.

You do not need to subscribe to the mailing list if you don't want to. There may be a short delay while your message is moderated.

Daemon Custom Printf Formatters

In the daemon code we have created custom printf formatters %Q and %R, which are used to do shell quoting.

Simple shell quoted string. Any spaces or other shell characters are escaped for you.
Same as %Q except the string is treated as a path which is prefixed by the sysroot.

For example:

asprintf (&cmd, "cat %R", path);

would produce "cat /sysroot/some\ path\ with\ spaces"

Note: Do not use these when you are passing parameters to the "command{,r,v,rv}()" functions. These parameters do NOT need to be quoted because they are not passed via the shell (instead, straight to exec). You probably want to use the "sysroot_path()" function however.

Internationalization (I18n) Support

We support i18n (gettext anyhow) in the library.

However many messages come from the daemon, and we don't translate those at the moment. One reason is that the appliance generally has all locale files removed from it, because they take up a lot of space. So we'd have to readd some of those, as well as copying our PO files into the appliance.

Debugging messages are never translated, since they are intended for the programmers.

How Ocaml Programs Are Compiled and Linked

Mostly this section is "how we make automake & ocamlopt work together" since OCaml programs themselves are easy to compile.

Automake has no native support for OCaml programs, ocamlc nor ocamlopt. What we do instead is to treat OCaml programs as C programs which happen to contain these "other objects" ("DEPENDENCIES" in automake-speak) that happen to be the OCaml objects. This works because OCaml programs usually have C files for native bindings etc.

So a typical program is described as just its C sources:

virt_v2v_SOURCES = ... utils-c.c xml-c.c

For programs that have no explicit C sources, we create an empty dummy.c file, and list that instead:

virt_resize_SOURCES = dummy.c

The OCaml objects which contain most of the code are listed as automake dependencies (other dependencies may also be listed):

virt_v2v_DEPENDENCIES = ... cmdline.cmx v2v.cmx

The only other special thing we need to do is to provide a custom link command. This is needed because automake won't assemble the ocamlopt command, the list of objects and the "-cclib" libraries in the correct order otherwise.

virt_v2v_LINK = \
    $(top_srcdir)/ocaml-link.sh -cclib '-lutils -lgnu' -- ...

The actual rules, which you can examine in v2v/Makefile.am, are a little bit more complicated than this because they have to handle:

Compiling for byte code or native code.
The pattern rules needed to compile the OCaml sources to objects.

These are now kept in subdir-rules.mk at the top level, which is included in every subdirectory Makefile.am.
Adding OCaml sources files to "EXTRA_DIST".

Automake isn't aware of the complete list of sources for a binary, so it will not add them all automatically.


First a little history. Virt-v2v has been through at least two complete rewrites, so this is probably about the third version (but we don't intend to rewrite it again). The previous version was written in Perl and can be found here: https://git.fedorahosted.org/git/virt-v…

The current version started out as almost a line-for-line rewrite of the Perl code in OCaml + C, and it still has a fairly similar structure. Therefore if there are details of this code that you don't understand (especially in the details of guest conversion), checking the Perl code may help.

The files to start with when reading this code are:


types.mli defines all the structures used and passed around when communicating between different bits of the program. v2v.ml controls how the program runs in stages.

After studying those files, you may want to branch out into the input modules (input_*), the output modules (output_*) or the conversion modules (convert_*). The input and output modules define -i and -o options (see the manual). The conversion modules define what guest types we can handle and the detailed steps involved in converting them.

Every other file in this directory is a support module / library of some sort. Some code is written in C, especially where we want to use an external C library such as libxml2.


Virt-p2v is a front end on virt-v2v. ie. All it does is act as a GUI front end, and it calls out to virt-v2v to perform the actual conversion. Therefore most of the C code in the p2v/ subdirectory is Gtk (GUI) code, or supporting code for talking to the remote conversion server. There is no special support for physical machines in virt-v2v. They are converted in the same way as foreign VMs.

Running virt-p2v

You can run the p2v/virt-p2v binary directly, but it will try to convert your machine's real /dev/sda which is unlikely to work well. However virt-p2v also has a test mode in which you can supply a test disk:

make -C p2v run-virt-p2v-directly

This is a wrapper around the virt-p2v(1) --test-disk option. You can control the "physical machine" disk by setting "PHYSICAL_MACHINE" to point to a disk image.

A more realistic test is to run virt-p2v inside a VM on the local machine. To do that, do:

make -C p2v run-virt-p2v-in-a-vm

This also runs qemu with the "physical machine" disk (which you can set by setting "PHYSICAL_MACHINE"), a virtual CD, and a variety of network cards for testing.

A third way to run virt-p2v simulates fairly accurately the program being downloaded over PXE and then doing an automatic conversion of the source physical machine (the non-GUI path -- see next section below):

make -C p2v run-virt-p2v-non-gui-conversion

Understanding the virt-p2v code

See also: "HOW Virt-P2v WORKS" in virt-p2v(1)

There are two paths through the code, GUI or non-GUI (parsing the kernel command line):

main.c ──────┬─────▶ gui.c ──────┬─────▶ conversion.c
             │                   │
             │                   │
             └────▶ kernel.c ────┘

but both paths call back to the conversion.c function "start_conversion" to run the remote virt-v2v.

The main task of gui.c/kernel.c is to populate the virt-v2v configuration (config.c).

During conversion, we need to establish ssh connections, and that is done using two libraries:

conversion.c ──────▶ ssh.c ──────▶ miniexpect.c

where ssh.c is responsible for managing ssh connections overall, and miniexpect.c implements "expect-like" functionality for talking interactively to the remote virt-v2v conversion server.

(Note that miniexpect is a separate library with its own upstream, so if you patch miniexpect.c, then please make sure the changes get reflected in miniexpect's upstream too: http://git.annexia.org/?p=miniexpect.gi…)

Making a Stable Release

When we make a stable release, there are several steps documented here. See "LIBGUESTFS VERSION NUMBERS" in guestfs(3) for general information about the stable branch policy.

Check "make && make check" works on at least Fedora, Debian and Ubuntu.
Check "./configure --without-libvirt" works.
Finalize guestfs-release-notes.pod

Push and pull from Zanata.


zanata push

to push the latest POT files to Zanata. Then run:


which is a wrapper to pull the latest translated *.po files.

Consider updating gnulib to latest upstream version.
Create new stable and development directories under http://libguestfs.org/download.
Edit website/index.html.in.

Set the version (in configure.ac) to the new stable version, ie. 1.XX.0, and commit it:

make distclean -k
make && make dist
make maintainer-commit
make maintainer-tag

Create the stable branch in git:

git branch stable-1.XX
git push origin stable-1.XX
Do a full release of the stable branch.
Set the version to the next development version and commit that. Optionally do a full release of the development branch.

Internal Documentation

This section documents internal functions inside libguestfs and various utilities. It is intended for libguestfs developers only.

This section is autogenerated from "/**" comments in source files, which are marked up in POD format.

These functions are not publicly exported, and may change or be removed at any time.

Subdirectory src

File src/actions-support.c

Helper functions for the actions code in src/actions-*.c.

File src/appliance.c

This file deals with building the libguestfs appliance.

Function "src/appliance.c:guestfs_int_build_appliance"

guestfs_int_build_appliance (guestfs_h *g,
                            char **kernel_rtn,
                            char **initrd_rtn,
                            char **appliance_rtn)

Locate or build the appliance.

This function locates or builds the appliance as necessary, handling the supermin appliance, caching of supermin-built appliances, or using either a fixed or old-style appliance.

The return value is 0 = good, "-1" = error. Returned in *kernel will be the name of the kernel to use, *initrd the name of the initrd, *appliance the name of the ext2 root filesystem. *appliance can be "NULL", meaning that we are using an old-style (non-ext2) appliance. All three strings must be freed by the caller. However the referenced files themselves must not be deleted.

The process is as follows:

Look for the first element of "g->path" which contains a supermin appliance skeleton. If no element has this, skip straight to step 3.
Call "supermin --build" to build the full appliance (if it needs to be rebuilt). If this is successful, return the full appliance.
Check each element of "g->path", looking for a fixed appliance. If one is found, return it.
Check each element of "g->path", looking for an old-style appliance. If one is found, return it.

The supermin appliance cache directory lives in $TMPDIR/.guestfs-$UID/ and consists of up to four files:

$TMPDIR/.guestfs-$UID/lock            - the supermin lock file
$TMPDIR/.guestfs-$UID/appliance.d/kernel - the kernel
$TMPDIR/.guestfs-$UID/appliance.d/initrd - the supermin initrd
$TMPDIR/.guestfs-$UID/appliance.d/root   - the appliance

Multiple instances of libguestfs with the same UID may be racing to create an appliance. However (since supermin ≥ 5) supermin provides a --lock flag and atomic update of the appliance.d subdirectory.

Function "src/appliance.c:build_supermin_appliance"

static int
build_supermin_appliance (guestfs_h *g,
                          const char *supermin_path,
                          char **kernel, char **initrd,
                          char **appliance)

Build supermin appliance from "supermin_path" to $TMPDIR/.guestfs-$UID.

Returns: 0 = built or "-1" = error (aborts launch).

Function "src/appliance.c:run_supermin_build"

static int
run_supermin_build (guestfs_h *g,
                    const char *lockfile,
                    const char *appliancedir,
                    const char *supermin_path)

Run "supermin --build" and tell it to generate the appliance.

Function "src/appliance.c:find_path"

static int
find_path (guestfs_h *g,
           int (*pred) (guestfs_h *g, const char *pelem, void *data),
           void *data,
           char **pelem_ret)

Search elements of "g->path", returning the first path element which matches the predicate function "pred".

Function "pred" must return a true or false value. If it returns "-1" then the entire search is aborted.

Return values:

 1 = a path element matched, it is returned in *pelem_ret and must be
     freed by the caller,
 0 = no path element matched, *pelem_ret is set to NULL, or
-1 = error which aborts the launch process

Function "src/appliance.c:dir_contains_file"

static int
dir_contains_file (guestfs_h *g, const char *dir, const char *file)

Returns true iff "file" is contained in "dir".

Function "src/appliance.c:dir_contains_files"

static int
dir_contains_files (guestfs_h *g, const char *dir, ...)

Returns true iff every listed file is contained in "dir".

Function "src/appliance.c:guestfs_int_get_uefi"

guestfs_int_get_uefi (guestfs_h *g, char **code, char **vars, int *flags)

Return the location of firmware needed to boot the appliance. This is aarch64 only currently, since that's the only architecture where UEFI is mandatory (and that only for RHEL).

*code is initialized with the path to the read-only UEFI code file. *vars is initialized with the path to a copy of the UEFI vars file (which is cleaned up automatically on exit).

If *code == *vars == "NULL" then no UEFI firmware is available.

*code and *vars should be freed by the caller.

If the function returns "-1" then there was a real error which should cause appliance building to fail (no UEFI firmware is not an error).

See also v2v/utils.ml:find_uefi_firmware

File src/cleanup.c

Libguestfs uses "CLEANUP_*" macros to simplify temporary allocations. They are implemented using the "__attribute__((cleanup))" feature of gcc and clang. Typical usage is:

fn ()
  CLEANUP_FREE char *str = NULL;
  str = safe_asprintf (g, "foo");
  // str is freed automatically when the function returns

There are a few catches to be aware of with the cleanup mechanism:

If a cleanup variable is not initialized, then you can end up calling free(3) with an undefined value, resulting in the program crashing. For this reason, you should usually initialize every cleanup variable with something, eg. "NULL"
Don't mark variables holding return values as cleanup variables.
The "main()" function shouldn't use cleanup variables since it is normally exited by calling exit(3), and that doesn't call the cleanup handlers.

The functions in this file are used internally by the "CLEANUP_*" macros. Don't call them directly.

File src/command.c

A wrapper for running external commands, loosely based on libvirt's "virCommand" interface.

In outline to use this interface you must:


Create a new command handle:

struct command *cmd;
cmd = guestfs_int_new_command (g);

Either add arguments:

guestfs_int_cmd_add_arg (cmd, "qemu-img");
guestfs_int_cmd_add_arg (cmd, "info");
guestfs_int_cmd_add_arg (cmd, filename);

(NB: You don't need to add a "NULL" argument at the end.)


Or construct a command using a mix of quoted and unquoted strings. (This is useful for system(3)/"popen("r")"-style shell commands, with the added safety of allowing args to be quoted properly).

guestfs_int_cmd_add_string_unquoted (cmd, "qemu-img info ");
guestfs_int_cmd_add_string_quoted (cmd, filename);
Set various flags, such as whether you want to capture errors in the regular libguestfs error log.

Run the command. This is what does the fork(2) call, optionally loops over the output, and then does a waitpid(3) and returns the exit status of the command.

r = guestfs_int_cmd_run (cmd);
if (r == -1)
  // error
// else test r using the WIF* functions

Close the handle:

guestfs_int_cmd_close (cmd);


Function "src/command.c:guestfs_int_new_command"

struct command *
guestfs_int_new_command (guestfs_h *g)

Create a new command handle.

Function "src/command.c:guestfs_int_cmd_add_arg"

guestfs_int_cmd_add_arg (struct command *cmd, const char *arg)

Add single arg (for "execv"-style command execution).

Function "src/command.c:guestfs_int_cmd_add_arg_format"

guestfs_int_cmd_add_arg_format (struct command *cmd, const char *fs, ...)

Add single arg (for "execv"-style command execution) using a printf(3)-style format string.

Function "src/command.c:guestfs_int_cmd_add_string_unquoted"

guestfs_int_cmd_add_string_unquoted (struct command *cmd, const char *str)

Add a string (for system(3)-style command execution).

This variant adds the strings without quoting them, which is dangerous if the string contains untrusted content.

Function "src/command.c:guestfs_int_cmd_add_string_quoted"

guestfs_int_cmd_add_string_quoted (struct command *cmd, const char *str)

Add a string (for system(3)-style command execution).

The string is enclosed in double quotes, with any special characters within the string which need escaping done. This is used to add a single argument to a system(3)-style command string.

Function "src/command.c:guestfs_int_cmd_set_stdout_callback"

guestfs_int_cmd_set_stdout_callback (struct command *cmd,
                                     cmd_stdout_callback stdout_callback,
                                     void *stdout_data, unsigned flags)

Set a callback which will capture stdout.

If flags contains "CMD_STDOUT_FLAG_LINE_BUFFER" (the default), then the callback is called line by line on the output. If there is a trailing "\n" then it is automatically removed before the callback is called. The line buffer is "\0"-terminated.

If flags contains "CMD_STDOUT_FLAG_UNBUFFERED", then buffers are passed to the callback as it is received from the command. Note in this case the buffer is not "\0"-terminated, so you need to may attention to the length field in the callback.

If flags contains "CMD_STDOUT_FLAG_WHOLE_BUFFER", then the callback is called exactly once, with the entire buffer. Note in this case the buffer is not "\0"-terminated, so you need to may attention to the length field in the callback.

Function "src/command.c:guestfs_int_cmd_set_stderr_to_stdout"

guestfs_int_cmd_set_stderr_to_stdout (struct command *cmd)

Equivalent to adding "2>&1" to the end of the command. This is incompatible with the "capture_errors" flag, because it doesn't make sense to combine them.

Function "src/command.c:guestfs_int_cmd_clear_capture_errors"

guestfs_int_cmd_clear_capture_errors (struct command *cmd)

Clear the "capture_errors" flag. This means that any errors will go to stderr, instead of being captured in the event log, and that is usually undesirable.

Function "src/command.c:guestfs_int_cmd_clear_close_files"

guestfs_int_cmd_clear_close_files (struct command *cmd)

Don't close file descriptors after the fork.

XXX Should allow single fds to be sent to child process.

Function "src/command.c:guestfs_int_cmd_set_child_callback"

guestfs_int_cmd_set_child_callback (struct command *cmd,
                                    cmd_child_callback child_callback,
                                    void *data)

Set a function to be executed in the child, right before the execution. Can be used to setup the child, for example changing its current directory.

Function "src/command.c:guestfs_int_cmd_set_child_rlimit"

guestfs_int_cmd_set_child_rlimit (struct command *cmd, int resource, long limit)

Set up child rlimits, in case the process we are running could consume lots of space or time.

Function "src/command.c:finish_command"

static void
finish_command (struct command *cmd)

Finish off the command by either "NULL"-terminating the argv array or adding a terminating "\0" to the string, or die with an internal error if no command has been added.

Function "src/command.c:loop"

static int
loop (struct command *cmd)

The loop which reads errors and output and directs it either to the log or to the stdout callback as appropriate.

Function "src/command.c:guestfs_int_cmd_run"

guestfs_int_cmd_run (struct command *cmd)

Fork, run the command, loop over the output, and waitpid.

Returns the exit status. Test it using "WIF*" macros.

On error: Calls "error" and returns "-1".

Function "src/command.c:guestfs_int_cmd_pipe_run"

guestfs_int_cmd_pipe_run (struct command *cmd, const char *mode)

Fork and run the command, but don't wait. Roughly equivalent to "popen (..., "r"|"w")".

Returns the file descriptor of the pipe, connected to stdout ("r") or stdin ("w") of the child process.

After reading/writing to this pipe, call "guestfs_int_cmd_pipe_wait" to wait for the status of the child.

Errors from the subcommand cannot be captured to the error log using this interface. Instead the caller should call "guestfs_int_cmd_get_pipe_errors" (after "guestfs_int_cmd_pipe_wait" returns an error).

Function "src/command.c:guestfs_int_cmd_pipe_wait"

guestfs_int_cmd_pipe_wait (struct command *cmd)

Wait for a subprocess created by "guestfs_int_cmd_pipe_run" to finish. On error (eg. failed syscall) this returns "-1" and sets the error. If the subcommand fails, then use "WIF*" macros to check this, and call "guestfs_int_cmd_get_pipe_errors" to read the error messages printed by the child.

Function "src/command.c:guestfs_int_cmd_get_pipe_errors"

char *
guestfs_int_cmd_get_pipe_errors (struct command *cmd)

Read the error messages printed by the child. The caller must free the returned buffer after use.

Function "src/command.c:guestfs_int_cmd_close"

guestfs_int_cmd_close (struct command *cmd)

Close the "cmd" object and free all resources.

Function "src/command.c:process_line_buffer"

static void
process_line_buffer (struct command *cmd, int closed)

Deal with buffering stdout for the callback.

File src/conn-socket.c

This file handles connections to the child process where this is done over regular POSIX sockets.

Function "src/conn-socket.c:handle_log_message"

static int
handle_log_message (guestfs_h *g,
                    struct connection_socket *conn)

This is called if "conn->console_sock" becomes ready to read while we are doing one of the connection operations above. It reads and deals with the log message.


log message(s) were handled successfully
connection to appliance closed

Function "src/conn-socket.c:guestfs_int_new_conn_socket_listening"

struct connection *
guestfs_int_new_conn_socket_listening (guestfs_h *g,
                                      int daemon_accept_sock,
                                      int console_sock)

Create a new socket connection, listening.

Note that it's OK for "console_sock" to be passed as "-1", meaning there's no console available for this appliance.

After calling this, "daemon_accept_sock" is owned by the connection, and will be closed properly either in "accept_connection" or "free_connection".

Function "src/conn-socket.c:guestfs_int_new_conn_socket_connected"

struct connection *
guestfs_int_new_conn_socket_connected (guestfs_h *g,
                                      int daemon_sock,
                                      int console_sock)

Create a new socket connection, connected.

As above, but the caller passes us a connected "daemon_sock" and promises not to call "accept_connection".

File src/create.c

APIs for creating empty disks.

Mostly this consists of wrappers around the qemu-img(1) program.

File src/drives.c

Drives added are stored in an array in the handle. Code here manages that array and the individual "struct drive" data.

Function "src/drives.c:create_overlay"

static int
create_overlay (guestfs_h *g, struct drive *drv)

For readonly drives, create an overlay to protect the original drive content. Note we never need to clean up these overlays since they are created in the temporary directory and deleted when the handle is closed.

Function "src/drives.c:create_drive_file"

static struct drive *
create_drive_file (guestfs_h *g,
                   const struct drive_create_data *data)

Create and free the "struct drive".

Function "src/drives.c:create_drive_dev_null"

static struct drive *
create_drive_dev_null (guestfs_h *g,
                       struct drive_create_data *data)

Create the special /dev/null drive.

Traditionally you have been able to use /dev/null as a filename, as many times as you like. Ancient KVM (RHEL 5) cannot handle adding /dev/null readonly. qemu 1.2 + virtio-scsi segfaults when you use any zero-sized file including /dev/null.

Because of these problems, we replace /dev/null with a non-zero sized temporary file. This shouldn't make any difference since users are not supposed to try and access a null drive.

Function "src/drives.c:drive_to_string"

static char *
drive_to_string (guestfs_h *g, const struct drive *drv)

Convert a "struct drive" to a string for debugging. The caller must free this string.

Function "src/drives.c:add_drive_to_handle_at"

static void
add_drive_to_handle_at (guestfs_h *g, struct drive *d, size_t drv_index)

Add "struct drive" to the "g->drives" vector at the given index "drv_index". If the array isn't large enough it is reallocated. The index must not contain a drive already.

Function "src/drives.c:add_drive_to_handle"

static void
add_drive_to_handle (guestfs_h *g, struct drive *d)

Add struct drive to the end of the "g->drives" vector in the handle.

Function "src/drives.c:guestfs_int_add_dummy_appliance_drive"

guestfs_int_add_dummy_appliance_drive (guestfs_h *g)

Called during launch to add a dummy slot to "g->drives".

Function "src/drives.c:guestfs_int_free_drives"

guestfs_int_free_drives (guestfs_h *g)

Free up all the drives in the handle.

Function "src/drives.c:valid_format_iface"

static int
valid_format_iface (const char *str)

Check string parameter matches regular expression "^[-_[:alnum:]]+$" (in C locale).

Function "src/drives.c:valid_disk_label"

static int
valid_disk_label (const char *str)

Check the disk label is reasonable. It can't contain certain characters, eg. '/', ','. However be stricter here and ensure it's just alphabetic and ≤ 20 characters in length.

Function "src/drives.c:valid_hostname"

static int
valid_hostname (const char *str)

Check the server hostname is reasonable.

Function "src/drives.c:valid_port"

static int
valid_port (int port)

Check the port number is reasonable.

Function "src/drives.c:guestfs_impl_remove_drive"

guestfs_impl_remove_drive (guestfs_h *g, const char *label)

This function implements "guestfs_remove_drive" in guestfs(3).

Depending on whether we are hotplugging or not, this function does slightly different things: If not hotplugging, then the drive just disappears as if it had never been added. The later drives "move up" to fill the space. When hotplugging we have to do some complex stuff, and we usually end up leaving an empty ("NULL") slot in the "g->drives" vector.

Function "src/drives.c:guestfs_int_checkpoint_drives"

guestfs_int_checkpoint_drives (guestfs_h *g)

Checkpoint and roll back drives, so that groups of drives can be added atomicly. Only used by "guestfs_add_domain" in guestfs(3).

Function "src/drives.c:guestfs_impl_debug_drives"

char **
guestfs_impl_debug_drives (guestfs_h *g)

Internal function to return the list of drives.

File src/errors.c

This file handles errors, and also debug, trace and warning messages.

Errors in libguestfs API calls are handled by setting an error message and optional errno in the handle. The caller has the choice of testing API calls to find out if they failed and then querying the last error from the handle, and/or getting a callback.

From the point of view of the library source, generally you should use the "error" or "perrorf" macros along error paths, eg:

if (something_bad) {
  error (g, "something bad happened");
  return -1;

Make sure to call the "error" or "perrorf" macro exactly once along each error path, since the handle can only store a single error and the previous error will be overwritten.

Function "src/errors.c:guestfs_int_warning"

guestfs_int_warning (guestfs_h *g, const char *fs, ...)

Print a warning.

Code should not call this function directly. Use the "warning (g, fs, ...)" macro.

Warnings are printed unconditionally. We try to make these rare: Generally speaking, a warning should either be an error, or if it's not important for end users then it should be a debug message.

Function "src/errors.c:guestfs_int_debug"

guestfs_int_debug (guestfs_h *g, const char *fs, ...)

Print a debug message.

Code should not call this function directly. To add debug messages in the library, use the "debug (g, fs, ...)" macro. The macro checks if "g->verbose" is false and avoids the function call, meaning the macro is more efficient.

Function "src/errors.c:guestfs_int_trace"

guestfs_int_trace (guestfs_h *g, const char *fs, ...)

Print a trace message.

Do not call this function. All calls are generated automatically.

Function "src/errors.c:guestfs_int_error_errno"

guestfs_int_error_errno (guestfs_h *g, int errnum, const char *fs, ...)

Set the last error and errno in the handle, and optionally raise the error callback if one is defined.

If you don't need to set errno, use the "error (g, fs, ...)" macro instead of calling this directly. If you need to set errno then there is no macro wrapper, so calling this function directly is fine.

Function "src/errors.c:guestfs_int_perrorf"

guestfs_int_perrorf (guestfs_h *g, const char *fs, ...)

Similar to perror(3), but it sets the last error in the handle, raises the error callback if one is defined, and supports format strings.

You should probably use the "perrorf (g, fs, ...)" macro instead of calling this directly.

Function "src/errors.c:guestfs_int_launch_failed_error"

guestfs_int_launch_failed_error (guestfs_h *g)

Raise a launch failed error in a standard format.

Since this is the most common error seen by people who have installation problems, buggy qemu, etc, and since no one reads the FAQ, describe in this error message what resources are available to debug launch problems.

Function "src/errors.c:guestfs_int_unexpected_close_error"

guestfs_int_unexpected_close_error (guestfs_h *g)

Raise an error if the appliance unexpectedly crashes after launch.

Function "src/errors.c:guestfs_int_launch_timeout"

guestfs_int_launch_timeout (guestfs_h *g)

Raise an error if the appliance hangs during launch.

Function "src/errors.c:guestfs_int_external_command_failed"

guestfs_int_external_command_failed (guestfs_h *g, int status,
                                    const char *cmd_name, const char *extra)

Raise an error if an external command fails.

"status" is the status code of the command (eg. returned from waitpid(2) or system(3)). This function turns the status code into an explanatory string.

File src/events.c

Function "src/events.c:replace_old_style_event_callback"

static void
replace_old_style_event_callback (guestfs_h *g,
                                  guestfs_event_callback cb,
                                  uint64_t event_bitmask,
                                  void *opaque,
                                  void *opaque2)

Emulate old-style callback API.

There were no event handles, so multiple callbacks per event were not supported. Calling the same "guestfs_set_*_callback" function would replace the existing event. Calling it with "cb == NULL" meant that the caller wanted to remove the callback.

File src/guid.c

Function "src/guid.c:guestfs_int_validate_guid"

guestfs_int_validate_guid (const char *str)

Check whether a string supposed to contain a GUID actually contains it. It can recognize strings either as "{21EC2020-3AEA-1069-A2DD-08002B30309D}" or "21EC2020-3AEA-1069-A2DD-08002B30309D".

File src/handle.c

This file deals with the "guestfs_h" handle, creating it, closing it, and initializing/setting/getting fields.

Function "src/handle.c:init_libguestfs"

static void
init_libguestfs (void)

No initialization is required by libguestfs, but libvirt and libxml2 require initialization if they might be called from multiple threads. Hence this constructor function which is called when libguestfs is first loaded.

Function "src/handle.c:shutdown_backend"

static int
shutdown_backend (guestfs_h *g, int check_for_errors)

This function is the common path for shutting down the backend qemu process.

"guestfs_shutdown" calls "shutdown_backend" with "check_for_errors=1". "guestfs_close" calls "shutdown_backend" with "check_for_errors=0".

"check_for_errors" is a hint to the backend about whether we care about errors or not. In the libvirt case it can be used to optimize the shutdown for speed when we don't care.

Function "src/handle.c:close_handles"

static void
close_handles (void)

Close all open handles (called from atexit(3)).

Function "src/handle.c:guestfs_int_get_backend_setting_bool"

guestfs_int_get_backend_setting_bool (guestfs_h *g, const char *name)

This is a convenience function, but we might consider exporting it as an API in future.

File src/inspect.c

This file, and the other "src/inspect*.c" files, handle inspection. See "INSPECTION" in guestfs(3).

Function "src/inspect.c:guestfs_impl_inspect_os"

char **
guestfs_impl_inspect_os (guestfs_h *g)

The main inspection API.

Function "src/inspect.c:collect_coreos_inspection_info"

static void
collect_coreos_inspection_info (guestfs_h *g)

Traverse through the filesystem list and find out if it contains the "/" and "/usr" filesystems of a CoreOS image. If this is the case, sum up all the collected information on the root fs.

Function "src/inspect.c:check_for_duplicated_bsd_root"

static void
check_for_duplicated_bsd_root (guestfs_h *g)

On *BSD systems, sometimes /dev/sda[1234] is a shadow of the real root filesystem that is probably /dev/sda5 (see: http://www.freebsd.org/doc/handbook/dis…)

Function "src/inspect.c:guestfs_int_download_to_tmp"

char *
guestfs_int_download_to_tmp (guestfs_h *g, struct inspect_fs *fs,
                            const char *filename,
                            const char *basename, uint64_t max_size)

Download a guest file to a local temporary file. The file is cached in the temporary directory, and is not downloaded again.

The name of the temporary (downloaded) file is returned. The caller must free the pointer, but does not need to delete the temporary file. It will be deleted when the handle is closed.

Refuse to download the guest file if it is larger than "max_size". On this and other errors, "NULL" is returned.

There is actually one cache per "struct inspect_fs *" in order to handle the case of multiple roots.

File src/launch-direct.c

Implementation of the "direct" backend.

For more details see "BACKENDS" in guestfs(3).

File src/launch-libvirt.c

Function "src/launch-libvirt.c:get_source_format_or_autodetect"

static char *
get_source_format_or_autodetect (guestfs_h *g, struct drive *drv)

Return "drv->src.format", but if it is "NULL", autodetect the format.

libvirt has disabled the feature of detecting the disk format, unless the administrator sets "allow_disk_format_probing=1" in /etc/libvirt/qemu.conf. There is no way to detect if this option is set, so we have to do format detection here using "qemu-img" and pass that to libvirt.

This can still be a security issue, so in most cases it is recommended the users pass the format to libguestfs which will faithfully pass that straight through to libvirt without doing autodetection.

Caller must free the returned string. On error this function sets the error in the handle and returns "NULL".

Function "src/launch-libvirt.c:make_qcow2_overlay"

static char *
make_qcow2_overlay (guestfs_h *g, const char *backing_drive,
                    const char *format)

Create a qcow2 format overlay, with the given "backing_drive" (file). The "format" parameter, which must be non-NULL, is the backing file format. This is used to create the appliance overlay, and also for read-only drives.

File src/launch.c

This file implements "guestfs_launch" in guestfs(3).

Most of the work is done by the backends (see "BACKEND" in guestfs(3)), which are implemented in src/launch-direct.c, src/launch-libvirt.c etc, so this file mostly passes calls through to the current backend.

Function "src/launch.c:guestfs_int_launch_send_progress"

guestfs_int_launch_send_progress (guestfs_h *g, int perdozen)

This function sends a launch progress message.

Launching the appliance generates approximate progress messages. Currently these are defined as follows:

 0 / 12: launch clock starts
 3 / 12: appliance created
 6 / 12: detected that guest kernel started
 9 / 12: detected that /init script is running
12 / 12: launch completed successfully


This is not a documented ABI and the behaviour may be changed or removed in future.
Messages are only sent if more than 5 seconds has elapsed since the launch clock started.
There is a hack in src/proto.c to make this work.

Function "src/launch.c:guestfs_int_timeval_diff"

guestfs_int_timeval_diff (const struct timeval *x, const struct timeval *y)

Compute "y - x" and return the result in milliseconds.

Approximately the same as this code: http://www.mpp.mpg.de/~huber/util/timev…

Function "src/launch.c:guestfs_impl_max_disks"

guestfs_impl_max_disks (guestfs_h *g)

Returns the maximum number of disks allowed to be added to the backend (backend dependent).

Function "src/launch.c:guestfs_impl_wait_ready"

guestfs_impl_wait_ready (guestfs_h *g)

Implementation of "guestfs_wait_ready" in guestfs(3). You had to call this function after launch in versions ≤ 1.0.70, but it is now an (almost) no-op.

Function "src/launch.c:guestfs_int_appliance_command_line"

char *
guestfs_int_appliance_command_line (guestfs_h *g, const char *appliance_dev,
                                   int flags)

Construct the Linux command line passed to the appliance. This is used by the "direct" and "libvirt" backends, and is simply located in this file because it's a convenient place for this common code.

The "appliance_dev" parameter must be the full device name of the appliance disk and must have already been adjusted to take into account virtio-blk or virtio-scsi; eg "/dev/sdb".

The "flags" parameter can contain the following flags logically or'd together (or 0):

If we are launching a qemu TCG guest (ie. KVM is known to be disabled or unavailable). If you don't know, don't pass this flag.

Note that this function returns a newly allocated buffer which must be freed by the caller.

Function "src/launch.c:guestfs_int_get_cpu_model"

const char *
guestfs_int_get_cpu_model (int kvm)

Return the right CPU model to use as the qemu "-cpu" parameter or its equivalent in libvirt. This returns:

The literal string "host" means use "-cpu host".
some string
Some string such as "cortex-a57" means use "-cpu cortex-a57".
"NULL" means no "-cpu" option at all. Note returning "NULL" does not indicate an error.

This is made unnecessarily hard and fragile because of two stupid choices in QEMU:

The default for "qemu-system-aarch64 -M virt" is to emulate a "cortex-a15" (WTF?).
We don't know for sure if KVM will work, but "-cpu host" is broken with TCG, so we almost always pass a broken "-cpu" flag if KVM is semi-broken in any way.

Function "src/launch.c:guestfs_int_create_socketname"

guestfs_int_create_socketname (guestfs_h *g, const char *filename,
                               char (*sockpath)[UNIX_PATH_MAX])

Create the path for a socket with the selected filename in the tmpdir.

Function "src/launch.c:guestfs_int_register_backend"

guestfs_int_register_backend (const char *name, const struct backend_ops *ops)

When the library is loaded, each backend calls this function to register itself in a global list.

Function "src/launch.c:guestfs_int_set_backend"

guestfs_int_set_backend (guestfs_h *g, const char *method)

Implementation of "guestfs_set_backend" in guestfs(3).

Callers must ensure this is only called in the config state.
This shouldn't call "error" since it may be called early in handle initialization. It can return an error code however.

File src/proto.c

This is the code used to send and receive RPC messages and (for certain types of message) to perform file transfers. This code is driven from the generated actions (src/actions-*.c). There are five different cases to consider:

A non-daemon function (eg. "guestfs_set_verbose" in guestfs(3)). There is no RPC involved at all, it's all handled inside the library.

A simple RPC (eg. "guestfs_mount" in guestfs(3)). We write the request, then read the reply. The sequence of calls is:


An RPC with "FileIn" parameters (eg. "guestfs_upload" in guestfs(3)). We write the request, then write the file(s), then read the reply. The sequence of calls is:

guestfs_int_send_file  (possibly multiple times)

An RPC with "FileOut" parameters (eg. "guestfs_download" in guestfs(3)). We write the request, then read the reply, then read the file(s). The sequence of calls is:

guestfs_int_recv_file  (possibly multiple times)
Both "FileIn" and "FileOut" parameters. There are no calls like this in the current API, but they would be implemented as a combination of cases 3 and 4.

All read/write/etc operations are performed using the current connection module ("g->conn"). During operations the connection module transparently handles log messages that appear on the console.

Function "src/proto.c:child_cleanup"

static void
child_cleanup (guestfs_h *g)

This is called if we detect EOF, ie. qemu died.

Function "src/proto.c:guestfs_int_progress_message_callback"

guestfs_int_progress_message_callback (guestfs_h *g,
                                      const guestfs_progress *message)

Convenient wrapper to generate a progress message callback.

Function "src/proto.c:guestfs_int_log_message_callback"

guestfs_int_log_message_callback (guestfs_h *g, const char *buf, size_t len)

Connection modules call us back here when they get a log message.

Function "src/proto.c:check_daemon_socket"

static ssize_t
check_daemon_socket (guestfs_h *g)

Before writing to the daemon socket, check the read side of the daemon socket for any of these conditions:

return -1
daemon cancellation message
return -2
progress message
handle it here
end of input or appliance exited unexpectedly
return 0
anything else
return 1

Function "src/proto.c:guestfs_int_send_file"

guestfs_int_send_file (guestfs_h *g, const char *filename)

Send a file.

Returns 0 on success, "-1" for error, "-2" if the daemon cancelled (we must read the error message).

Function "src/proto.c:send_file_data"

static int
send_file_data (guestfs_h *g, const char *buf, size_t len)

Send a chunk of file data.

Function "src/proto.c:send_file_cancellation"

static int
send_file_cancellation (guestfs_h *g)

Send a cancellation message.

Function "src/proto.c:send_file_complete"

static int
send_file_complete (guestfs_h *g)

Send a file complete chunk.

Function "src/proto.c:recv_from_daemon"

static int
recv_from_daemon (guestfs_h *g, uint32_t *size_rtn, void **buf_rtn)

This function reads a single message, file chunk, launch flag or cancellation flag from the daemon. If something was read, it returns 0, otherwise "-1".

Both "size_rtn" and "buf_rtn" must be passed by the caller as non-NULL.

*size_rtn returns the size of the returned message or it may be "GUESTFS_LAUNCH_FLAG" or "GUESTFS_CANCEL_FLAG".

*buf_rtn is returned containing the message (if any) or will be set to "NULL". *buf_rtn must be freed by the caller.

This checks for EOF (appliance died) and passes that up through the child_cleanup function above.

Log message, progress messages are handled transparently here.

Function "src/proto.c:guestfs_int_recv"

guestfs_int_recv (guestfs_h *g, const char *fn,
                 guestfs_message_header *hdr,
                 guestfs_message_error *err,
                 xdrproc_t xdrp, char *ret)

Receive a reply.

Function "src/proto.c:guestfs_int_recv_discard"

guestfs_int_recv_discard (guestfs_h *g, const char *fn)

Same as "guestfs_int_recv", but it discards the reply message.

Notes (XXX):

This returns an int, but all current callers ignore it.
The error string may end up being set twice on error paths.

Function "src/proto.c:guestfs_int_recv_file"

guestfs_int_recv_file (guestfs_h *g, const char *filename)

Returns "-1" = error, 0 = EOF, ">0" = more data

Function "src/proto.c:receive_file_data"

static ssize_t
receive_file_data (guestfs_h *g, void **buf_r)

Receive a chunk of file data.

Returns "-1" = error, 0 = EOF, ">0" = more data

File src/qemu.c

Functions to handle qemu versions and features.

Function "src/qemu.c:guestfs_int_test_qemu"

struct qemu_data *
guestfs_int_test_qemu (guestfs_h *g, struct version *qemu_version)

Test qemu binary (or wrapper) runs, and do "qemu -help" so we know the version of qemu what options this qemu supports, and "qemu -device ?" so we know what devices are available.

The version number of qemu (from the "-help" output) is saved in &qemu_version.

This caches the results in the cachedir so that as long as the qemu binary does not change, calling this is effectively free.

Function "src/qemu.c:parse_qemu_version"

static void
parse_qemu_version (guestfs_h *g, const char *qemu_help,
                    struct version *qemu_version)

Parse the first line of "qemu_help" into the major and minor version of qemu, but don't fail if parsing is not possible.

Function "src/qemu.c:guestfs_int_qemu_supports"

guestfs_int_qemu_supports (guestfs_h *g, const struct qemu_data *data,
                           const char *option)

Test if option is supported by qemu command line (just by grepping the help text).

Function "src/qemu.c:guestfs_int_qemu_supports_device"

guestfs_int_qemu_supports_device (guestfs_h *g,
                                  const struct qemu_data *data,
                                  const char *device_name)

Test if device is supported by qemu (currently just greps the "qemu -device ?" output).

Function "src/qemu.c:guestfs_int_qemu_supports_virtio_scsi"

guestfs_int_qemu_supports_virtio_scsi (guestfs_h *g, struct qemu_data *data,
                                       const struct version *qemu_version)

Test if qemu supports virtio-scsi.

Returns 1 = use virtio-scsi, or 0 = use virtio-blk.

Function "src/qemu.c:guestfs_int_qemu_escape_param"

char *
guestfs_int_qemu_escape_param (guestfs_h *g, const char *param)

Escape a qemu parameter.

Every "," becomes ",,". The caller must free the returned string.

Function "src/qemu.c:guestfs_int_drive_source_qemu_param"

char *
guestfs_int_drive_source_qemu_param (guestfs_h *g,
                                     const struct drive_source *src)

Useful function to format a drive + protocol for qemu.

Note that the qemu parameter is the bit after "file=". It is not escaped here, but would usually be escaped if passed to qemu as part of a full -drive parameter (but not for qemu-img(1)).

Function "src/qemu.c:guestfs_int_discard_possible"

guestfs_int_discard_possible (guestfs_h *g, struct drive *drv,
                             const struct version *qemu_version)

Test if discard is both supported by qemu AND possible with the underlying file or device. This returns 1 if discard is possible. It returns 0 if not possible and sets the error to the reason why.

This function is called when the user set "discard == "enable"".

Function "src/qemu.c:guestfs_int_free_qemu_data"

guestfs_int_free_qemu_data (struct qemu_data *data)

Free the "struct qemu_data".

File src/stringsbuf.c

An expandable NULL-terminated vector of strings (like "argv").

Use the "DECLARE_STRINGSBUF" macro to declare the stringsbuf.

Note: Don't confuse this with stringsbuf in the daemon which is a different type with different methods.

Function "src/stringsbuf.c:guestfs_int_add_string_nodup"

guestfs_int_add_string_nodup (guestfs_h *g, struct stringsbuf *sb, char *str)

Add a string to the end of the list.

This doesn't call strdup(3) on the string, so the string itself is stored inside the vector.

Function "src/stringsbuf.c:guestfs_int_add_string"

guestfs_int_add_string (guestfs_h *g, struct stringsbuf *sb, const char *str)

Add a string to the end of the list.

This makes a copy of the string.

Function "src/stringsbuf.c:guestfs_int_add_sprintf"

guestfs_int_add_sprintf (guestfs_h *g, struct stringsbuf *sb,
                        const char *fs, ...)

Add a string to the end of the list.

Uses an sprintf-like format string when creating the string.

Function "src/stringsbuf.c:guestfs_int_end_stringsbuf"

guestfs_int_end_stringsbuf (guestfs_h *g, struct stringsbuf *sb)

Finish the string buffer.

This adds the terminating NULL to the end of the vector.

Function "src/stringsbuf.c:guestfs_int_free_stringsbuf"

guestfs_int_free_stringsbuf (struct stringsbuf *sb)

Free the string buffer and the strings.

File src/tmpdirs.c

Handle temporary directories.

Function "src/tmpdirs.c:set_abs_path"

static int
set_abs_path (guestfs_h *g, const char *tmpdir, char **tmpdir_ret)

We need to make all tmpdir paths absolute because lots of places in the code assume this. Do it at the time we set the path or read the environment variable (https://bugzilla.redhat.com/882417).

Function "src/tmpdirs.c:guestfs_impl_get_tmpdir"

char *
guestfs_impl_get_tmpdir (guestfs_h *g)

Implements the "guestfs_get_tmpdir" API.

Note this actually calculates the tmpdir, so it never returns "NULL".

Function "src/tmpdirs.c:guestfs_impl_get_cachedir"

char *
guestfs_impl_get_cachedir (guestfs_h *g)

Implements the "guestfs_get_cachedir" API.

Note this actually calculates the cachedir, so it never returns "NULL".

Function "src/tmpdirs.c:guestfs_impl_get_sockdir"

char *
guestfs_impl_get_sockdir (guestfs_h *g)

Implements the "guestfs_get_sockdir" API.

Note this actually calculates the sockdir, so it never returns "NULL".

Function "src/tmpdirs.c:guestfs_int_lazy_make_tmpdir"

guestfs_int_lazy_make_tmpdir (guestfs_h *g)

The "g->tmpdir" (per-handle temporary directory) is not created when the handle is created. Instead we create it lazily before the first time it is used, or during launch.

Function "src/tmpdirs.c:guestfs_int_lazy_make_supermin_appliance_dir"

char *
guestfs_int_lazy_make_supermin_appliance_dir (guestfs_h *g)

Create the supermin appliance directory under cachedir, if it does not exist.

Sanity-check that the permissions on the cachedir are safe, in case it has been pre-created maliciously or tampered with.

Returns the directory name which the caller must free.

Function "src/tmpdirs.c:guestfs_int_recursive_remove_dir"

guestfs_int_recursive_remove_dir (guestfs_h *g, const char *dir)

Recursively remove a temporary directory. If removal fails, just return (it's a temporary directory so it'll eventually be cleaned up by a temp cleaner).

This is implemented using "rm -rf" because that's simpler and safer.

File src/umask.c

Return current umask in a thread-safe way.

glibc documents, but does not actually implement, a "getumask(3)" call.

We use "Umask" from /proc/self/status for Linux ≥ 4.7. For older Linux and other Unix, this file implements an expensive but thread-safe way to get the current process's umask.

Thanks to: Josh Stone, Jiri Jaburek, Eric Blake.

Function "src/umask.c:guestfs_int_getumask"

guestfs_int_getumask (guestfs_h *g)

Returns the current process's umask. On failure, returns "-1" and sets the error in the guestfs handle.

Function "src/umask.c:get_umask_from_proc"

static int
get_umask_from_proc (guestfs_h *g)

For Linux ≥ 4.7 get the umask from /proc/self/status.

On failure this returns "-1". However if we could not open the /proc file or find the "Umask" entry in it, return "-2" which causes the fallback path to run.

Function "src/umask.c:get_umask_from_fork"

static int
get_umask_from_fork (guestfs_h *g)

Fallback method of getting the umask using fork.

File src/unit-tests.c

Unit tests of internal functions.

These tests may use a libguestfs handle, but must not launch the handle. Also, avoid long-running tests.

Function "src/unit-tests.c:test_split"

static void
test_split (void)

Test "guestfs_int_split_string".

Function "src/unit-tests.c:test_concat"

static void
test_concat (void)

Test "guestfs_int_concat_strings".

Function "src/unit-tests.c:test_join"

static void
test_join (void)

Test "guestfs_int_join_strings".

Function "src/unit-tests.c:test_validate_guid"

static void
test_validate_guid (void)

Test "guestfs_int_validate_guid".

Function "src/unit-tests.c:test_drive_name"

static void
test_drive_name (void)

Test "guestfs_int_drive_name".

Function "src/unit-tests.c:test_drive_index"

static void
test_drive_index (void)

Test "guestfs_int_drive_index".

Function "src/unit-tests.c:test_getumask"

static void
test_getumask (void)

Test "guestfs_int_getumask".

Function "src/unit-tests.c:test_command"

static void
test_command (void)

Test "guestfs_int_new_command" etc.

XXX These tests could be made much more thorough. So far we simply test that it's not obviously broken.

Function "src/unit-tests.c:test_qemu_escape_param"

static void
test_qemu_escape_param (void)

Test "guestfs_int_qemu_escape_param"

XXX I wanted to make this test run qemu, passing some parameters which need to be escaped, but I cannot think of a way to do that without launching a VM.

Function "src/unit-tests.c:test_timeval_diff"

static void
test_timeval_diff (void)

Test "guestfs_int_timeval_diff".

File src/utils.c

Utility functions used by the library, tools and language bindings.

These functions these must not call internal library functions such as "safe_*", "error" or "perrorf", or any "guestfs_int_*".

Function "src/utils.c:guestfs_int_split_string"

char **
guestfs_int_split_string (char sep, const char *str)

Split string at separator character "sep", returning the list of strings. Returns "NULL" on memory allocation failure.

Note (assuming "sep" is ":"):

"str == NULL"
"str == """
returns "[]"
"str == "abc""
returns "["abc"]"
"str == ":""
returns "["", ""]"

Function "src/utils.c:guestfs_int_exit_status_to_string"

char *
guestfs_int_exit_status_to_string (int status, const char *cmd_name,
                                  char *buffer, size_t buflen)

Translate a wait/system exit status into a printable string.

Function "src/utils.c:guestfs_int_random_string"

guestfs_int_random_string (char *ret, size_t len)

Return a random string of characters.


The "ret" buffer must have length "len+1" in order to store the final "\0" character.
There is about 5 bits of randomness per output character (so about "5*len" bits of randomness in the resulting string).

Function "src/utils.c:guestfs_int_drive_name"

char *
guestfs_int_drive_name (size_t index, char *ret)

This turns a drive index (eg. 27) into a drive name (eg. "ab").

Drive indexes count from 0. The return buffer has to be large enough for the resulting string, and the returned pointer points to the *end* of the string.


Function "src/utils.c:guestfs_int_drive_index"

guestfs_int_drive_index (const char *name)

The opposite of "guestfs_int_drive_name". Take a string like "ab" and return the index (eg 27).

Note that you must remove any prefix such as "hd", "sd" etc, or any partition number before calling the function.

Function "src/utils.c:guestfs_int_is_true"

guestfs_int_is_true (const char *str)

Similar to "Tcl_GetBoolean".

Function "src/utils.c:guestfs_int_fadvise_normal"

guestfs_int_fadvise_normal (int fd)

Hint that we will read or write the file descriptor normally.

On Linux, this clears the "FMODE_RANDOM" flag on the file [see below] and sets the per-file number of readahead pages to equal the block device readahead setting.

It's OK to call this on a non-file since we ignore failure as it is only a hint.

Function "src/utils.c:guestfs_int_fadvise_sequential"

guestfs_int_fadvise_sequential (int fd)

Hint that we will read or write the file descriptor sequentially.

On Linux, this clears the "FMODE_RANDOM" flag on the file [see below] and sets the per-file number of readahead pages to twice the block device readahead setting.

It's OK to call this on a non-file since we ignore failure as it is only a hint.

Function "src/utils.c:guestfs_int_fadvise_random"

guestfs_int_fadvise_random (int fd)

Hint that we will read or write the file descriptor randomly.

On Linux, this sets the "FMODE_RANDOM" flag on the file. The effect of this flag is to:

Disable normal sequential file readahead.
If any read of the file is done which misses in the page cache, 2MB are read into the page cache. [I think - I'm not sure I totally understand what this is doing]

It's OK to call this on a non-file since we ignore failure as it is only a hint.

Function "src/utils.c:guestfs_int_fadvise_noreuse"

guestfs_int_fadvise_noreuse (int fd)

Hint that we will access the data only once.

On Linux, this does nothing.

It's OK to call this on a non-file since we ignore failure as it is only a hint.

Function "src/utils.c:guestfs_int_fadvise_dontneed"

guestfs_int_fadvise_dontneed (int fd)

Hint that we will not access the data in the near future.

On Linux, this immediately writes out any dirty pages in the page cache and then invalidates (drops) all pages associated with this file from the page cache. Apparently it does this even if the file is opened or being used by other processes. This setting is not persistent; if you subsequently read the file it will be cached in the page cache as normal.

It's OK to call this on a non-file since we ignore failure as it is only a hint.

Function "src/utils.c:guestfs_int_fadvise_willneed"

guestfs_int_fadvise_willneed (int fd)

Hint that we will access the data in the near future.

On Linux, this immediately reads the whole file into the page cache. This setting is not persistent; subsequently pages may be dropped from the page cache as normal.

It's OK to call this on a non-file since we ignore failure as it is only a hint.

Function "src/utils.c:guestfs_int_shell_unquote"

char *
guestfs_int_shell_unquote (const char *str)

Unquote a shell-quoted string.

Augeas passes strings to us which may be quoted, eg. if they come from files in /etc/sysconfig. This function can do simple unquoting of these strings.

Note this function does not do variable substitution, since that is impossible without knowing the file context and indeed the environment under which the shell script is run. Configuration files should not use complex quoting.

"str" is the input string from Augeas, a string that may be single- or double-quoted or may not be quoted. The returned string is unquoted, and must be freed by the caller. "NULL" is returned on error and "errno" is set accordingly.

For information on double-quoting in bash, see https://www.gnu.org/software/bash/manua…

File src/version.c

This file provides simple version number management.

Function "src/version.c:guestfs_int_version_from_x_y"

guestfs_int_version_from_x_y (guestfs_h *g, struct version *v, const char *str)

Parses a version from a string, looking for a "X.Y" pattern.

Returns "-1" on failure (like failed integer parsing), 0 on missing match, and 1 on match and successful parsing. "v" is changed only on successful match.

Function "src/version.c:guestfs_int_version_from_x_y_re"

guestfs_int_version_from_x_y_re (guestfs_h *g, struct version *v,
                                 const char *str, const pcre *re)

Parses a version from a string, using the specified "re" as regular expression which must provide (at least) two matches.

Returns "-1" on failure (like failed integer parsing), 0 on missing match, and 1 on match and successful parsing. "v" is changed only on successful match.

Function "src/version.c:guestfs_int_version_from_x_y_or_x"

guestfs_int_version_from_x_y_or_x (guestfs_h *g, struct version *v,
                                   const char *str)

Parses a version from a string, either looking for a "X.Y" pattern or considering it as whole integer.

Returns "-1" on failure (like failed integer parsing), 0 on missing match, and 1 on match and successful parsing. "v" is changed only on successful match.

File src/wait.c

Function "src/wait.c:guestfs_int_waitpid"

guestfs_int_waitpid (guestfs_h *g, pid_t pid, int *status, const char *errmsg)

A safe version of waitpid(3) which retries if "EINTR" is returned.

Note: this only needs to be used in the library, or in programs that install a non-restartable "SIGCHLD" handler (which is not the case for any current libguestfs virt tools).

If the main program installs a SIGCHLD handler and sets it to be non-restartable, then what can happen is the library is waiting in a wait syscall, the child exits, "SIGCHLD" is sent to the process, and the wait syscall returns "EINTR". Since the library cannot control the signal handler, we have to instead restart the wait syscall, which is the purpose of this wrapper.

Function "src/wait.c:guestfs_int_waitpid_noerror"

guestfs_int_waitpid_noerror (pid_t pid)

Like "guestfs_int_waitpid", but ignore errors.

Function "src/wait.c:guestfs_int_wait4"

guestfs_int_wait4 (guestfs_h *g, pid_t pid, int *status,
                   struct rusage *rusage, const char *errmsg)

A safe version of wait4(2) which retries if "EINTR" is returned.

File src/whole-file.c

Function "src/whole-file.c:guestfs_int_read_whole_file"

guestfs_int_read_whole_file (guestfs_h *g, const char *filename,
                             char **data_r, size_t *size_r)

Read the whole file "filename" into a memory buffer.

The memory buffer is initialized and returned in "data_r". The size of the file in bytes is returned in "size_r". The return buffer must be freed by the caller.

On error this sets the error in the handle and returns "-1".

For the convenience of callers, the returned buffer is NUL-terminated (the NUL is not included in the size).

The file must be a regular, local, trusted file. In particular, do not use this function to read files that might be under control of an untrusted user since that will lead to a denial-of-service attack.

Subdirectory builder

File builder/index-parser-c.c

This file handles the interface between the C/lex/yacc index file parser, and the OCaml world. See builder/index_parser.ml for the OCaml type definition.

Subdirectory cat

File cat/visit.c

This file contains a recursive function for visiting all files and directories in a guestfs filesystem.

Adapted from https://rwmj.wordpress.com/2010/12/15/t…

Function "cat/visit.c:visit"

visit (guestfs_h *g, const char *dir, visitor_function f, void *opaque)

Visit every file and directory in a guestfs filesystem, starting at "dir".

"dir" may be "/" to visit the entire filesystem, or may be some subdirectory. Symbolic links are not followed.

The visitor function "f" is called once for every directory and every file. The parameters passed to "f" include the current directory name, the current file name (or "NULL" when we're visiting a directory), the "guestfs_statns" (file permissions etc), and the list of extended attributes of the file. The visitor function may return "-1" which causes the whole recursion to stop with an error.

Also passed to this function is an "opaque" pointer which is passed through to the visitor function.

Returns 0 if everything went OK, or "-1" if there was an error. Error handling is not particularly well defined. It will either set an error in the libguestfs handle or print an error on stderr, but there is no way for the caller to tell the difference.

Subdirectory daemon

File daemon/command.c

This file contains a number of useful functions for running external commands and capturing their output.

Function "daemon/command.c:commandf"

commandf (char **stdoutput, char **stderror, unsigned flags,
          const char *name, ...)

Run a command. Optionally capture stdout and stderr as strings.

Returns 0 if the command ran successfully, or "-1" if there was any error.

For a description of the "flags" see "commandrvf".

There is also a macro "command(out,err,name,...)" which calls "commandf" with "flags=0".

Function "daemon/command.c:commandrf"

commandrf (char **stdoutput, char **stderror, unsigned flags,
           const char *name, ...)

Same as "command", but we allow the status code from the subcommand to be non-zero, and return that status code.

We still return "-1" if there was some other error.

There is also a macro "commandr(out,err,name,...)" which calls "commandrf" with "flags=0".

Function "daemon/command.c:commandvf"

commandvf (char **stdoutput, char **stderror, unsigned flags,
           char const *const *argv)

Same as "command", but passing in an argv array.

There is also a macro "commandv(out,err,argv)" which calls "commandvf" with "flags=0".

Function "daemon/command.c:commandrvf"

commandrvf (char **stdoutput, char **stderror, unsigned flags,
            char const* const *argv)

This is a more sane version of system(3) for running external commands. It uses fork/execvp, so we don't need to worry about quoting of parameters, and it allows us to capture any error messages in a buffer.

If "stdoutput" is not "NULL", then *stdoutput will return the stdout of the command as a string.

If "stderror" is not "NULL", then *stderror will return the stderr of the command. If there is a final \n character, it is removed so you can use the error string directly in a call to "reply_with_error".

Flags are:

For broken external commands that send error messages to stdout (hello, parted) but that don't have any useful stdout information, use this flag to capture the error messages in the *stderror buffer. If using this flag, you should pass "stdoutput=NULL" because nothing could ever be captured in that buffer.
For running external commands on chrooted files correctly (see https://bugzilla.redhat.com/579608) specifying this flag causes another process to be forked which chroots into sysroot and just copies the input file to stdin of the specified command. The file descriptor is ORed with the flags, and that file descriptor is always closed by this function. See daemon/hexdump.c for an example of usage.

There is also a macro "commandrv(out,err,argv)" which calls "commandrvf" with "flags=0".

File daemon/guestfsd.c

This is the guestfs daemon which runs inside the guestfs appliance. This file handles start up, connecting back to the library, and has several utility functions.

Function "daemon/guestfsd.c:is_root_device_stat"

static int
is_root_device_stat (struct stat *statbuf)

Return true iff device is the root device (and therefore should be ignored from the point of view of user calls).

Function "daemon/guestfsd.c:sysroot_path"

char *
sysroot_path (const char *path)

Turn "/path" into "/sysroot/path".

Returns "NULL" on failure. The caller must check for this and call "reply_with_perror ("malloc")". The caller must also free the returned string.

See also the custom %R printf formatter which does shell quoting too.

Function "daemon/guestfsd.c:sysroot_realpath"

char *
sysroot_realpath (const char *path)

Resolve path within sysroot, calling "sysroot_path" on the resolved path.

Returns "NULL" on failure. The caller must check for this and call "reply_with_perror ("malloc")". The caller must also free the returned string.

See also the custom %R printf formatter which does shell quoting too.

Function "daemon/guestfsd.c:is_power_of_2"

is_power_of_2 (unsigned long v)

Returns true if "v" is a power of 2.

Uses the algorithm described at http://graphics.stanford.edu/~seander/b…

Function "daemon/guestfsd.c:compare_device_names"

compare_device_names (const char *a, const char *b)

Compare device names (including partition numbers if present).


Function "daemon/guestfsd.c:split_lines_sb"

struct stringsbuf
split_lines_sb (char *str)

Split an output string into a NULL-terminated list of lines, wrapped into a stringsbuf.

Typically this is used where we have run an external command which has printed out a list of things, and we want to return an actual list.

The corner cases here are quite tricky. Note in particular:

returns "[]"
returns "[""]"
returns "["a"; "b"]"
returns "["a"; "b"]"
returns "["a"; "b"; ""]"

The original string is written over and destroyed by this function (which is usually OK because it's the 'out' string from "command*()"). You can free the original string, because "add_string()" strdups the strings.

"argv" in the "struct stringsbuf" will be "NULL" in case of errors.

Function "daemon/guestfsd.c:trim"

trim (char *str)

Skip leading and trailing whitespace, updating the original string in-place.

Function "daemon/guestfsd.c:print_shell_quote"

static int
print_shell_quote (FILE *stream,
                   const struct printf_info *info ATTRIBUTE_UNUSED,
                   const void *const *args)

printf helper function so we can use %Q ("quoted") and %R to print shell-quoted strings. See guestfs-hacking(1) for more details.

Function "daemon/guestfsd.c:device_name_translation"

char *
device_name_translation (const char *device)

Perform device name translation. See guestfs(3) for the algorithm. Usually you should not call this directly.

It returns a newly allocated string which the caller must free.

It returns "NULL" on error. Note it does not call "reply_with_*".

We have to open the device and test for "ENXIO", because the device nodes may exist in the appliance.

Function "daemon/guestfsd.c:parse_btrfsvol"

parse_btrfsvol (const char *desc_orig, mountable_t *mountable)

Parse the mountable descriptor for a btrfs subvolume. Don't call this directly; it is only used from the stubs.

A btrfs subvolume is given as:


where /dev/sda3 is a block device containing a btrfs filesystem, and root is the name of a subvolume on it. This function is passed the string following "btrfsvol:".

On success, "mountable->device" and "mountable->volume" must be freed by the caller.

Function "daemon/guestfsd.c:mountable_to_string"

char *
mountable_to_string (const mountable_t *mountable)

Convert a "mountable_t" back to its string representation

This function can be used in an error path, so must not call "reply_with_error".

Function "daemon/guestfsd.c:prog_exists"

prog_exists (const char *prog)

Check program exists and is executable on $PATH.

Function "daemon/guestfsd.c:random_name"

random_name (char *template)

Pass a template such as "/sysroot/XXXXXXXX.XXX". This updates the template to contain a randomly named file. Any 'X' characters after the final '/' in the template are replaced with random characters.

Notes: You should probably use an 8.3 path, so it's compatible with all filesystems including basic FAT. Also this only substitutes lowercase ASCII letters and numbers, again for compatibility with lowest common denominator filesystems.

This doesn't create a file or check whether or not the file exists (it would be extremely unlikely to exist as long as the RNG is working).

If there is an error, "-1" is returned.

Function "daemon/guestfsd.c:udev_settle"

udev_settle (void)

LVM and other commands aren't synchronous, especially when udev is involved. eg. You can create or remove some device, but the "/dev" device node won't appear until some time later. This means that you get an error if you run one command followed by another.

Use "udevadm settle" after certain commands, but don't be too fussed if it fails.

File daemon/internal.c

Internal functions that are not part of the public API.

Subdirectory df

File df/domains.c

This file is used by "virt-df" and some of the other tools when they are implicitly asked to operate over all libvirt domains (VMs), for example when "virt-df" is called without specifying any particular disk image.

It hides the complexity of querying the list of domains from libvirt.

Function "df/domains.c:free_domains"

free_domains (void)

Frees up everything allocated by "get_all_libvirt_domains".

Function "df/domains.c:get_all_libvirt_domains"

get_all_libvirt_domains (const char *libvirt_uri)

Read all libguest guests into the global variables "domains" and "nr_domains". The guests are ordered by name. This exits on any error.

File df/estimate-max-threads.c

Function "df/estimate-max-threads.c:estimate_max_threads"

estimate_max_threads (void)

This function uses the output of "free -m" to estimate how many libguestfs appliances could be safely started in parallel. Note that it always returns ≥ 1.

Function "df/estimate-max-threads.c:read_line_from"

static char *
read_line_from (const char *cmd)

Run external command and read the first line of output.

File df/parallel.c

This file is used by "virt-df" and some of the other tools when they need to run multiple parallel libguestfs instances to operate on a large number of libvirt domains efficiently.

It implements a multithreaded work queue. In addition it reorders the output so the output still appears in the same order as the input (ie. still ordered alphabetically).

Function "df/parallel.c:start_threads"

start_threads (size_t option_P, guestfs_h *options_handle, work_fn work)

Run the threads and work through the global list of libvirt domains.

"option_P" is whatever the user passed in the -P option, or 0 if the user didn't use the -P option (in which case the number of threads is chosen heuristically).

"options_handle" (which may be "NULL") is the global guestfs handle created by the options mini-library.

The work function ("work") should do the work (inspecting the domain, etc.) on domain index "i". However it must not print out any result directly. Instead it prints anything it needs to the supplied "FILE *". The work function should return 0 on success or "-1" on error.

The "start_threads" function returns 0 if all work items completed successfully, or "-1" if there was an error.

Subdirectory fish

File fish/alloc.c

This file implements the guestfish "alloc" and "sparse" commands.

Function "fish/alloc.c:alloc_disk"

alloc_disk (const char *filename, const char *size_str, int add, int sparse)

This is the underlying allocation function. It's called from a few other places in guestfish.

File fish/config.c

This file parses the guestfish configuration file, usually ~/.libguestfs-tools.rc or /etc/libguestfs-tools.conf.

Note that "parse_config" is called very early, before command line parsing, before the "verbose" flag has been set, even before the global handle "g" is opened.

File fish/copy.c

This file implements the guestfish commands "copy-in" and "copy-out".

File fish/decrypt.c

This file implements the decryption of disk images, usually done before mounting their partitions.

Function "fish/decrypt.c:make_mapname"

static void
make_mapname (const char *device, char *mapname, size_t len)

Make a LUKS map name from the partition name, eg. "/dev/vda2" => "luksvda2"

Function "fish/decrypt.c:inspect_do_decrypt"

inspect_do_decrypt (guestfs_h *g)

Simple implementation of decryption: look for any "crypto_LUKS" partitions and decrypt them, then rescan for VGs. This only works for Fedora whole-disk encryption. WIP to make this work for other encryption schemes.

File fish/destpaths.c

The file handles tab-completion of filesystem paths in guestfish.

File fish/display-options.c

This file contains common code used to implement --short-options and --long-options in C virt tools. (The equivalent for OCaml virt tools is implemented by mllib/getopt.ml).

These "hidden" options are used to implement bash tab completion.

Function "fish/display-options.c:display_short_options"

display_short_options (const char *format)

Implements the internal "tool --short-options" flag, which just lists out the short options available. Used by bash completion.

Function "fish/display-options.c:display_long_options"

display_long_options (const struct option *long_options)

Implements the internal "tool --long-options" flag, which just lists out the long options available. Used by bash completion.

File fish/display.c

The file implements the guestfish "display" command, for displaying graphical files (icons, images) in disk images.

File fish/domain.c

Implements the guestfish (and other tools) -d option.

Function "fish/domain.c:add_libvirt_drives"

add_libvirt_drives (guestfs_h *g, const char *guest)

This function is called when a user invokes "guestfish -d guest".

Returns the number of drives added ("> 0"), or "-1" for failure.

File fish/echo.c

The file implements the guestfish "echo" command.

File fish/edit.c

guestfish "edit" command, suggested by Ján Ondrej.

File fish/events.c

This file implements the guestfish event-related commands, "event", "delete-event" and "list-events".

File fish/file-edit.c

This file implements common file editing in a range of utilities including guestfish(1), virt-edit(1), virt-customize(1) and virt-builder(1).

It contains the code for both interactive-(editor-)based editing and non-interactive editing using Perl snippets.

File fish/fish.c

guestfish, the guest filesystem shell. This file contains the main loop and utilities.

Function "fish/fish.c:parse_command_line"

static struct parsed_command
parse_command_line (char *buf, int *exit_on_error_rtn)

Parse a command string, splitting at whitespace, handling '!', '#' etc. This destructively updates "buf".

"exit_on_error_rtn" is used to pass in the global "exit_on_error" setting and to return the local setting (eg. if the command begins with '-').

Returns in "parsed_command.status":

got a guestfish command (returned in "cmd_rtn"/"argv_rtn"/"pipe_rtn")
no guestfish command, but otherwise OK
an error

Function "fish/fish.c:parse_quoted_string"

static ssize_t
parse_quoted_string (char *p)

Parse double-quoted strings, replacing backslash escape sequences with the true character. Since the string is returned in place, the escapes must make the string shorter.

Function "fish/fish.c:execute_and_inline"

static int
execute_and_inline (const char *cmd, int global_exit_on_error)

Used to handle "<!" (execute command and inline result).

Function "fish/fish.c:issue_command"

issue_command (const char *cmd, char *argv[], const char *pipecmd,
               int rc_exit_on_error_flag)

Run a command.

"rc_exit_on_error_flag" is the "exit_on_error" flag that we pass to the remote server (when issuing --remote commands). It does not cause "issue_command" itself to exit on error.

Function "fish/fish.c:extended_help_message"

extended_help_message (void)

Print an extended help message when the user types in an unknown command for the first command issued. A common case is the user doing:

guestfish disk.img

expecting guestfish to open disk.img (in fact, this tried to run a non-existent command "disk.img").

Function "fish/fish.c:error_cb"

static void
error_cb (guestfs_h *g, void *data, const char *msg)

Error callback. This replaces the standard libguestfs error handler.

Function "fish/fish.c:free_n_strings"

static void
free_n_strings (char **str, size_t len)

Free strings from a non-NULL terminated "char**".

Function "fish/fish.c:decode_ps1"

static char *
decode_ps1 (const char *str)

Decode "str" into the final printable prompt string.

Function "fish/fish.c:win_prefix"

char *
win_prefix (const char *path)

Resolve the special "win:..." form for Windows-specific paths. The generated code calls this for all device or path arguments.

The function returns a newly allocated string, and the caller must free this string; else display an error and return "NULL".

Function "fish/fish.c:file_in"

char *
file_in (const char *arg)

Resolve the special "FileIn" paths ("-" or "-<<END" or filename).

The caller (fish/cmds.c) will call "free_file_in" after the command has run which should clean up resources.

Function "fish/fish.c:file_out"

char *
file_out (const char *arg)

Resolve the special "FileOut" paths ("-" or filename).

The caller (fish/cmds.c) will call "free (str)" after the command has run.

Function "fish/fish.c:progress_callback"

progress_callback (guestfs_h *g, void *data,
                   uint64_t event, int event_handle, int flags,
                   const char *buf, size_t buf_len,
                   const uint64_t *array, size_t array_len)

Callback which displays a progress bar.

File fish/glob.c

This file implements the guestfish "glob" command.

Function "fish/glob.c:expand_devicename"

static char **
expand_devicename (guestfs_h *g, const char *device)

Glob-expand device patterns, such as "/dev/sd*" (https://bugzilla.redhat.com/635971).

There is no "guestfs_glob_expand_device" function because the equivalent can be implemented using functions like "guestfs_list_devices".

It's not immediately clear what it means to expand a pattern like "/dev/sd*". Should that include device name translation? Should the result include partitions as well as devices?

Should "/dev/" + "*" return every possible device and filesystem? How about VGs? LVs?

To solve this what we do is build up a list of every device, partition, etc., then glob against that list.

Notes for future work (XXX):

This doesn't handle device name translation. It wouldn't be too hard to add.
Could have an API function for returning all device-like things.

Function "fish/glob.c:add_strings_matching"

static int
add_strings_matching (char **pp, const char *glob,
                      char ***ret, size_t *size_r)

Using POSIX fnmatch(3), find strings in the list "pp" which match pattern "glob". Add strings which match to the "ret" array. *size_r is the current size of the "ret" array, which is updated with the new size.

Function "fish/glob.c:single_element_list"

static char **
single_element_list (const char *element)

Return a single element list containing "element".

File fish/help.c

The file implements the guestfish "help" command.

Function "fish/help.c:display_help"

display_help (void)

The "help" command.

This used to just list all commands, but that's not very useful. Instead display some useful context-sensitive help. This could be improved if we knew how many drives had been added already, and whether anything was mounted.

File fish/hexedit.c

This file implements the guestfish "hexedit" command.

File fish/inspect.c

This file implements inspecting the guest and mounting the filesystems found in the right places. It is used by the guestfish(1) -i option and some utilities such as virt-cat(1).

Function "fish/inspect.c:inspect_mount_handle"

inspect_mount_handle (guestfs_h *g)

This function implements the -i option.

Function "fish/inspect.c:print_inspect_prompt"

print_inspect_prompt (void)

This function is called only if "inspect_mount_root" was called, and only after we've printed the prompt in interactive mode.

File fish/keys.c

Function "fish/keys.c:read_key"

char *
read_key (const char *param)

Read a passphrase ('Key') from /dev/tty with echo off.

The caller (fish/cmds.c) will call free on the string afterwards. Based on the code in cryptsetup file lib/utils.c.

File fish/lcd.c

Function "fish/lcd.c:run_lcd"

run_lcd (const char *cmd, size_t argc, char *argv[])

guestfish "lcd" command (similar to the "lcd" command in BSD ftp).

File fish/man.c

Function "fish/man.c:run_man"

run_man (const char *cmd, size_t argc, char *argv[])

guestfish "man" command

File fish/more.c

This file implements the guestfish "more" command.

File fish/options.c

This file contains common options parsing code used by guestfish and many other tools which share a common options syntax.

For example, guestfish, virt-cat, virt-ls etc all support the -a option, and that is handled in all of those tools using a macro "OPTION_a" defined in fish/options.h.

There are a lot of common global variables used, "drvs" accumulates the list of drives, "verbose" for the -v flag, and many more.

Function "fish/options.c:option_a"

option_a (const char *arg, const char *format, struct drv **drvsp)

Handle the guestfish -a option on the command line.

Function "fish/options.c:option_d"

option_d (const char *arg, struct drv **drvsp)

Handle the -d option when passed on the command line.

Function "fish/options.c:display_mountpoints_on_failure"

static void
display_mountpoints_on_failure (const char *mp_device,
                                const char *user_supplied_options)

If the -m option fails on any command, display a useful error message listing the mountpoints.

File fish/prep.c

This file implements the guestfish -N option for creating pre-prepared disk layouts.

File fish/progress.c

This file implements the progress bar in guestfish(1), virt-resize(1) and virt-sparsify(1).

Function "fish/progress.c:progress_bar_init"

struct progress_bar *
progress_bar_init (unsigned flags)

Initialize a progress bar struct.

It is intended that you can reuse the same struct for multiple commands (but only in a single thread). Call "progress_bar_reset" before each new command.

Function "fish/progress.c:progress_bar_free"

progress_bar_free (struct progress_bar *bar)

Free a progress bar struct.

Function "fish/progress.c:progress_bar_reset"

progress_bar_reset (struct progress_bar *bar)

This function should be called just before you issue any command.

Function "fish/progress.c:estimate_remaining_time"

static double
estimate_remaining_time (struct progress_bar *bar, double ratio)

Return remaining time estimate (in seconds) for current call.

This returns the running mean estimate of remaining time, but if the latest estimate of total time is greater than two s.d.'s from the running mean then we don't print anything because we're not confident that the estimate is meaningful. (Returned value is <0.0 when nothing should be printed).

Function "fish/progress.c:progress_bar_set"

progress_bar_set (struct progress_bar *bar,
                  uint64_t position, uint64_t total)

Set the position of the progress bar.

This should be called from a "GUESTFS_EVENT_PROGRESS" event callback.

File fish/rc.c

This file implements guestfish remote (command) support.

Function "fish/rc.c:rc_listen"

rc_listen (void)

The remote control server (ie. "guestfish --listen").

Function "fish/rc.c:rc_remote"

rc_remote (int pid, const char *cmd, size_t argc, char *argv[],
           int exit_on_error)

The remote control client (ie. "guestfish --remote").

File fish/reopen.c

This file implements the guestfish "reopen" command.

File fish/setenv.c

This file implements the guestfish "setenv" and "unsetenv" commands.

File fish/supported.c

This file implements the guestfish "supported" command.

File fish/tilde.c

This file implements tilde ("~") expansion of home directories in guestfish(1).

Function "fish/tilde.c:try_tilde_expansion"

char *
try_tilde_expansion (char *str)

This is called from the script loop if we find a candidate for "~username" (tilde-expansion).

Function "fish/tilde.c:expand_home"

static char *
expand_home (char *orig, const char *append)

Return $HOME + append string.

Function "fish/tilde.c:find_home_for_username"

static const char *
find_home_for_username (const char *username, size_t ulen)

Lookup "username" (of length "ulen"), return home directory if found, or "NULL" if not found.

File fish/time.c

This file implements the guestfish "time" command.

File fish/uri.c

This file implements URI parsing for the -a option, in many utilities including guestfish(1), virt-cat(1), virt-builder(1), virt-customize(1), etc.

File fish/windows.c

This file implements "win:" Windows file path support in guestfish(1).

Subdirectory p2v

File p2v/config.c

Function "p2v/config.c:print_config"

print_config (struct config *config, FILE *fp)

Print the conversion parameters and other important information.

File p2v/conversion.c

This file manages the p2v conversion.

The conversion is actually done by virt-v2v(1) running on the remote conversion server. This file manages running the remote command and provides callbacks for displaying the output.

When virt-p2v operates in GUI mode, this code runs in a separate thread. When virt-p2v operates in kernel mode, this runs synchronously in the main thread.

Function "p2v/conversion.c:start_qemu_nbd"

static pid_t
start_qemu_nbd (int port, const char *device)

Start a local qemu-nbd(1) process.

Returns the process ID (> 0) or 0 if there is an error.

Function "p2v/conversion.c:connect_with_source_port"

static int
connect_with_source_port (const char *hostname, int dest_port, int source_port)

Connect to "hostname:dest_port", resolving the address using getaddrinfo(3).

This also sets the source port of the connection to the first free port number ≥ "source_port".

This may involve multiple connections - to IPv4 and IPv6 for instance.

Function "p2v/conversion.c:generate_libvirt_xml"

static void
generate_libvirt_xml (struct config *config, struct data_conn *data_conns,
                      const char *filename)

Write the libvirt XML for this physical machine.

Note this is not actually input for libvirt. It's input for virt-v2v on the conversion server. Virt-v2v will (if necessary) generate the final libvirt XML.

Function "p2v/conversion.c:map_interface_to_network"

static const char *
map_interface_to_network (struct config *config, const char *interface)

Using "config->network_map", map the interface to a target network name. If no map is found, return "default". See virt-p2v(1) documentation of "p2v.network" for how the network map works.

Note this returns a static string which is only valid as long as "config->network_map" is not freed.

Function "p2v/conversion.c:generate_name"

static void
generate_name (struct config *config, const char *filename)

Write the guest name into "filename".

Function "p2v/conversion.c:generate_wrapper_script"

static void
generate_wrapper_script (struct config *config, const char *remote_dir,
                         const char *filename)

Construct the virt-v2v wrapper script.

This will be sent to the remote server, and is easier than trying to "type" a long and complex single command line into the ssh connection when we start the conversion.

Function "p2v/conversion.c:print_quoted"

static void
print_quoted (FILE *fp, const char *s)

Print a shell-quoted string on "fp".

Function "p2v/conversion.c:generate_dmesg_file"

static void
generate_dmesg_file (const char *filename)

Put the output of the "dmesg" command into "filename".

If the command fails, this is non-fatal.

File p2v/gui.c

This file implements almost all of the virt-p2v graphical user interface (GUI).

The GUI has three main dialogs:

Connection dialog
The connection dialog is the one shown initially. It asks the user to type in the login details for the remote conversion server and invites the user to test the ssh connection.
Conversion dialog
The conversion dialog asks for information about the target VM (eg. the number of vCPUs required), and about what to convert (eg. which network interfaces should be copied and which should be ignored).
Running dialog
The running dialog is displayed when the P2V process is underway. It mainly displays the virt-v2v debug messages.

Note that the other major dialog ("Configure network ...") is handled entirely by NetworkManager's nm-connection-editor(1) program and has nothing to do with this code.

Function "p2v/gui.c:gui_conversion"

gui_conversion (struct config *config)

The entry point from the main program.

Note that "gtk_init" etc have already been called in "main".

Function "p2v/gui.c:create_connection_dialog"

static void
create_connection_dialog (struct config *config)

Create the connection dialog.

This creates the dialog, but it is not displayed. See "show_connection_dialog".

Function "p2v/gui.c:username_changed_callback"

static void
username_changed_callback (GtkWidget *w, gpointer data)

If the username is "root", disable the sudo button.

Function "p2v/gui.c:password_or_identity_changed_callback"

static void
password_or_identity_changed_callback (GtkWidget *w, gpointer data)

The password or SSH identity URL entries are mutually exclusive, so if one contains text then disable the other. This function is called when the "changed" signal is received on either.

Function "p2v/gui.c:show_connection_dialog"

static void
show_connection_dialog (void)

Hide all other dialogs and show the connection dialog.

Function "p2v/gui.c:test_connection_clicked"

static void
test_connection_clicked (GtkWidget *w, gpointer data)

Callback from the "Test connection" button.

This initiates a background thread which actually does the ssh to the conversion server and the rest of the testing (see "test_connection_thread").

Function "p2v/gui.c:test_connection_thread"

static void *
test_connection_thread (void *data)

Run "test_connection" (in a detached background thread). Once it finishes stop the spinner and set the spinner message appropriately. If the test is successful then we enable the "Next" button. If unsuccessful, an error is shown in the connection dialog.

Function "p2v/gui.c:start_spinner"

static gboolean
start_spinner (gpointer user_data)

Idle task called from "test_connection_thread" (but run on the main thread) to start the spinner in the connection dialog.

Function "p2v/gui.c:stop_spinner"

static gboolean
stop_spinner (gpointer user_data)

Idle task called from "test_connection_thread" (but run on the main thread) to stop the spinner in the connection dialog.

Function "p2v/gui.c:test_connection_error"

static gboolean
test_connection_error (gpointer user_data)

Idle task called from "test_connection_thread" (but run on the main thread) when there is an error. Display the error message and disable the "Next" button so the user is forced to correct it.

Function "p2v/gui.c:test_connection_ok"

static gboolean
test_connection_ok (gpointer user_data)

Idle task called from "test_connection_thread" (but run on the main thread) when the connection test was successful.

Function "p2v/gui.c:configure_network_button_clicked"

static void
configure_network_button_clicked (GtkWidget *w, gpointer data)

Callback from the "Configure network ..." button. This dialog is handled entirely by an external program which is part of NetworkManager.

Function "p2v/gui.c:xterm_button_clicked"

static void
xterm_button_clicked (GtkWidget *w, gpointer data)

Callback from the "XTerm ..." button.

Function "p2v/gui.c:about_button_clicked"

static void
about_button_clicked (GtkWidget *w, gpointer data)

Callback from the "About virt-p2v ..." button.

See also p2v/about-authors.c and p2v/about-license.c.

Function "p2v/gui.c:connection_next_clicked"

static void
connection_next_clicked (GtkWidget *w, gpointer data)

Callback when the connection dialog "Next" button has been clicked.

Function "p2v/gui.c:create_conversion_dialog"

static void
create_conversion_dialog (struct config *config)

Create the conversion dialog.

This creates the dialog, but it is not displayed. See "show_conversion_dialog".

Function "p2v/gui.c:show_conversion_dialog"

static void
show_conversion_dialog (void)

Hide all other dialogs and show the conversion dialog.

Function "p2v/gui.c:set_info_label"

static void
set_info_label (void)

Update the "Information" section in the conversion dialog.

Note that "v2v_version" (the remote virt-v2v version) is read from the remote virt-v2v in the "test_connection" function.

Function "p2v/gui.c:repopulate_output_combo"

static void
repopulate_output_combo (struct config *config)

Repopulate the list of output drivers in the "Output to (-o)" combo. The list of drivers is read from the remote virt-v2v instance in "test_connection".

Function "p2v/gui.c:populate_disks"

static void
populate_disks (GtkTreeView *disks_list)

Populate the "Fixed hard disks" treeview.

Function "p2v/gui.c:populate_removable"

static void
populate_removable (GtkTreeView *removable_list)

Populate the "Removable media" treeview.

Function "p2v/gui.c:populate_interfaces"

static void
populate_interfaces (GtkTreeView *interfaces_list)

Populate the "Network interfaces" treeview.

Function "p2v/gui.c:maybe_identify_click"

static gboolean
maybe_identify_click (GtkWidget *interfaces_list, GdkEventButton *event,
                      gpointer data)

When the user clicks on the interface name on the list of interfaces, we want to run "ethtool --identify", which usually makes some lights flash on the physical interface.

We cannot catch clicks on the cell itself, so we have to go via a more obscure route. See http://stackoverflow.com/a/27207433 and https://en.wikibooks.org/wiki/GTK%2B_By…

Function "p2v/gui.c:conversion_back_clicked"

static void
conversion_back_clicked (GtkWidget *w, gpointer data)

The conversion dialog "Back" button has been clicked.

Function "p2v/gui.c:vcpus_or_memory_check_callback"

static void
vcpus_or_memory_check_callback (GtkWidget *w, gpointer data)

Display a warning if the vCPUs or memory is outside the supported range (https://bugzilla.redhat.com/823758).

Function "p2v/gui.c:create_running_dialog"

static void
create_running_dialog (void)

Create the running dialog.

This creates the dialog, but it is not displayed. See "show_running_dialog".

Function "p2v/gui.c:show_running_dialog"

static void
show_running_dialog (void)

Hide all other dialogs and show the running dialog.

Function "p2v/gui.c:set_log_dir"

static gboolean
set_log_dir (gpointer user_data)

Display the remote log directory in the running dialog.

If this isn't called from the main thread, then you must only call it via an idle task ("g_idle_add").

NB: This frees the remote_dir ("user_data" pointer) which was strdup'd in "notify_ui_callback".

Function "p2v/gui.c:set_status"

static gboolean
set_status (gpointer user_data)

Display the conversion status in the running dialog.

If this isn't called from the main thread, then you must only call it via an idle task ("g_idle_add").

NB: This frees the message ("user_data" pointer) which was strdup'd in "notify_ui_callback".

Function "p2v/gui.c:add_v2v_output"

static gboolean
add_v2v_output (gpointer user_data)

Append output from the virt-v2v process to the buffer, and scroll to ensure it is visible.

This function is able to parse ANSI colour sequences and more.

If this isn't called from the main thread, then you must only call it via an idle task ("g_idle_add").

NB: This frees the message ("user_data" pointer) which was strdup'd in "notify_ui_callback".

Function "p2v/gui.c:start_conversion_clicked"

static void
start_conversion_clicked (GtkWidget *w, gpointer data)

Callback when the "Start conversion" button is clicked.

Function "p2v/gui.c:start_conversion_thread"

static void *
start_conversion_thread (void *data)

This is the background thread which performs the conversion.

Function "p2v/gui.c:conversion_error"

static gboolean
conversion_error (gpointer user_data)

Idle task called from "start_conversion_thread" (but run on the main thread) when there was an error during the conversion.

Function "p2v/gui.c:conversion_finished"

static gboolean
conversion_finished (gpointer user_data)

Idle task called from "start_conversion_thread" (but run on the main thread) when the conversion completed without errors.

Function "p2v/gui.c:notify_ui_callback"

static void
notify_ui_callback (int type, const char *data)

This is called from conversion.c:"start_conversion" when there is a status change or a log message.

Function "p2v/gui.c:cancel_conversion_dialog"

static void
cancel_conversion_dialog (GtkWidget *w, gpointer data)

This is called when the user clicks on the "Cancel conversion" button. Since conversions can run for a long time, and cancelling the conversion is non-recoverable, this function displays a confirmation dialog before cancelling the conversion.

File p2v/kernel-cmdline.c

Mini library to read and parse "/proc/cmdline".

Function "p2v/kernel-cmdline.c:parse_cmdline_string"

char **
parse_cmdline_string (const char *cmdline)

Read and parse "/proc/cmdline".

We only support double quoting, consistent with the Linux documentation. https://www.kernel.org/doc/Documentatio…

systemd supports single and double quoting and single character escaping, but we don't support all that.

Returns a list of key, value pairs, terminated by "NULL".

File p2v/kernel.c

Kernel-driven, non-interactive configuration of virt-p2v.

File p2v/main.c

Function "p2v/main.c:partition_parent"

static dev_t
partition_parent (dev_t part_dev)

Get parent device of a partition.

Returns 0 if no parent device could be found.

Function "p2v/main.c:device_contains"

static int
device_contains (const char *dev, dev_t root_device)

Return true if the named device (eg. "dev == "sda"") contains the root filesystem. "root_device" is the major:minor of the root filesystem (eg. "8:1" if the root filesystem was /dev/sda1).

This doesn't work for LVs and so on. However we only really care if this test works on the P2V ISO where the root device is a regular partition.

Function "p2v/main.c:find_all_disks"

static void
find_all_disks (void)

Enumerate all disks in /sys/block and add them to the global "all_disks" and "all_removable" arrays.

Function "p2v/main.c:find_all_interfaces"

static void
find_all_interfaces (void)

Enumerate all network interfaces in /sys/class/net and add them to the global "all_interfaces" array.

Function "p2v/main.c:cpuinfo_flags"

static int
cpuinfo_flags (void)

Read the list of flags from /proc/cpuinfo.

File p2v/ssh.c

This file handles the ssh connections to the conversion server.

virt-p2v will open several connections over the lifetime of the conversion process.

In "test_connection", it will first open a connection (to check it is possible) and query virt-v2v on the server to ensure it exists, it is the right version, and so on. This connection is then closed, because in the GUI case we don't want to deal with keeping it alive in case the administrator has set up an autologout.

Once we start conversion, we will open a control connection to send the libvirt configuration data and to start up virt-v2v, and we will open up one data connection per local hard disk. The data connection(s) have a reverse port forward to the local qemu-nbd(8) server which is serving the content of that hard disk. The remote port for each data connection is assigned by ssh. See "open_data_connection" and "start_remote_conversion".

Function "p2v/ssh.c:curl_download"

static int
curl_download (const char *url, const char *local_file)

Download URL to local file using the external 'curl' command.

Function "p2v/ssh.c:cache_ssh_identity"

static int
cache_ssh_identity (struct config *config)

Re-cache the "config->identity_url" if needed.

Function "p2v/ssh.c:start_ssh"

static mexp_h *
start_ssh (unsigned spawn_flags, struct config *config,
           char **extra_args, int wait_prompt)

Start ssh subprocess with the standard arguments and possibly some optional arguments. Also handles authentication.

Function "p2v/ssh.c:scp_file"

scp_file (struct config *config, const char *localfile, const char *remotefile)

Upload a file to remote using scp(1).

This is a simplified version of "start_ssh" above.

File p2v/utils.c

Function "p2v/utils.c:get_blockdev_size"

get_blockdev_size (const char *dev)

Return size of a block device, from /sys/block/dev/size.

This function always succeeds, or else exits (since we expect "dev" to always be valid and the "size" file to always exist).

Function "p2v/utils.c:get_blockdev_model"

char *
get_blockdev_model (const char *dev)

Return model of a block device, from /sys/block/dev/device/model.

Returns "NULL" if the file was not found. The caller must free the returned string.

Function "p2v/utils.c:get_blockdev_serial"

char *
get_blockdev_serial (const char *dev)

Return the serial number of a block device.

This is found using the lsblk command.

Returns "NULL" if we could not get the serial number. The caller must free the returned string.

Function "p2v/utils.c:get_if_addr"

char *
get_if_addr (const char *if_name)

Return contents of /sys/class/net/if_name/address (if found).

Function "p2v/utils.c:get_if_vendor"

char *
get_if_vendor (const char *if_name, int truncate)

Return contents of /sys/class/net/if_name/device/vendor (if found), mapped to the PCI vendor. See: http://pjwelsh.blogspot.co.uk/2011/11/h…

Function "p2v/utils.c:wait_network_online"

wait_network_online (const struct config *config)

Wait for the network to come online, but don't error out if that fails. The caller will call "test_connection" immediately after this which will fail if the network didn't come online.

File p2v/whole-file.c

Function "p2v/whole-file.c:read_whole_file"

read_whole_file (const char *filename, char **data_r, size_t *size_r)

Read the whole file into a memory buffer and return it. The file should be a regular, local, trusted file.

Subdirectory python

File python/handle.c

This file contains a small number of functions that are written by hand. The majority of the bindings are generated (see python/actions-*.c).

Subdirectory v2v

File v2v/domainxml-c.c

This module implements various "virsh"-like commands, but with non-broken authentication handling.

File v2v/xml-c.c

Mini interface to libxml2.

See Also

guestfs(3), guestfs-building(1), guestfs-examples(3), guestfs-internals(3), guestfs-performance(1), guestfs-release-notes(1), guestfs-testing(1), libguestfs-test-tool(1), libguestfs-make-fixed-appliance(1), http://libguestfs.org/.


Richard W.M. Jones ("rjones at redhat dot com")


This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


To get a list of bugs against libguestfs, use this link: https://bugzilla.redhat.com/buglist.cgi…

To report a new bug against libguestfs, use this link: https://bugzilla.redhat.com/enter_bug.c…

When reporting a bug, please supply:

The version of libguestfs.
Where you got libguestfs (eg. which Linux distro, compiled from source, etc)
Describe the bug accurately and give a way to reproduce it.
Run libguestfs-test-tool(1) and paste the complete, unedited output into the bug report.

Referenced By

guestfs(3), guestfs-building(1), guestfs-release-notes(1).

Explore man page connections for guestfs-hacking(1).

libguestfs 1.35.9 2016-10-20