.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Podwrapper::Man 1.52.0 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "guestfs-hacking 1" .TH guestfs-hacking 1 2024-01-05 libguestfs-1.52.0 "Virtualization Support" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME guestfs\-hacking \- extending and contributing to libguestfs .SH DESCRIPTION .IX Header "DESCRIPTION" This manual page is for hackers who want to extend libguestfs itself. .SH "THE SOURCE CODE" .IX Header "THE SOURCE CODE" Libguestfs source is located in the github repository https://github.com/libguestfs/libguestfs .PP 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 (\f(CW\*(C`./configure && make \-C generator\*(C'\fR) in order to create those files. .PP Libguestfs uses an autotools-based build system, with the main files being \fIconfigure.ac\fR and \fIMakefile.am\fR. See "THE BUILD SYSTEM". .PP The \fIgenerator\fR subdirectory contains the generator, plus files describing the API. The \fIlib\fR subdirectory contains source for the library. The \fIappliance\fR and \fIdaemon\fR 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. .PP 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 \fIlib\fR. Others are forwarded to the daemon where (after some generated RPC marshalling) they appear as C functions in files under \fIdaemon\fR. .PP To build from source, first read the \fBguestfs\-building\fR\|(1). .SS "SOURCE CODE SUBDIRECTORIES" .IX Subsection "SOURCE CODE SUBDIRECTORIES" There are a lot of subdirectories in the source tree! Which ones should you concentrate on first? \fIlib\fR and \fIdaemon\fR which contain the source code of the core library. \fIgenerator\fR is the code generator described above, so that is important. The \fIMakefile.am\fR 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. \fIcustomize\fR) or language binding (eg. \fIpython\fR), 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. .IP \fIalign\fR 4 .IX Item "align" \&\fBvirt\-alignment\-scan\fR\|(1) command and documentation. .IP \fIappliance\fR 4 .IX Item "appliance" The libguestfs appliance, build scripts and so on. .IP \fIbash\fR 4 .IX Item "bash" Bash tab-completion scripts. .IP \fIbuild-aux\fR 4 .IX Item "build-aux" Various build scripts used by autotools. .IP \fIbuilder\fR 4 .IX Item "builder" \&\fBvirt\-builder\fR\|(1) command and documentation. .IP \fIcat\fR 4 .IX Item "cat" The \fBvirt\-cat\fR\|(1), \fBvirt\-filesystems\fR\|(1), \fBvirt\-log\fR\|(1), \&\fBvirt\-ls\fR\|(1) and \fBvirt\-tail\fR\|(1) commands and documentation. .IP \fIcommon\fR 4 .IX Item "common" Various libraries of internal code can be found in the \fIcommon\fR subdirectory: .RS 4 .IP \fIcommon/edit\fR 4 .IX Item "common/edit" Common code for interactively and non-interactively editing files within a libguestfs filesystem. .IP \fIcommon/errnostring\fR 4 .IX Item "common/errnostring" The communication protocol used between the library and the daemon running inside the appliance has to encode errnos as strings, which is handled by this library. .IP \fIcommon/mlcustomize\fR 4 .IX Item "common/mlcustomize" Library code associated with \f(CW\*(C`virt\-customize\*(C'\fR but also used in other tools. .IP \fIcommon/mlgettext\fR 4 .IX Item "common/mlgettext" Small, generated wrapper which allows libguestfs to be compiled with or without ocaml-gettext. This is generated by \fI./configure\fR. .IP \fIcommon/mlpcre\fR 4 .IX Item "common/mlpcre" Lightweight OCaml bindings for Perl Compatible Regular Expressions (PCRE). Note this is not related in any way to Markus Mottl's ocaml-pcre library. .IP \fIcommon/mlprogress\fR 4 .IX Item "common/mlprogress" OCaml bindings for the progress bar functions (see \fIcommon/progress\fR). .IP \fIcommon/mlstdutils\fR 4 .IX Item "common/mlstdutils" A library of pure OCaml utility functions used in many places. .IP \fIcommon/mltools\fR 4 .IX Item "common/mltools" OCaml utility functions only used by the OCaml virt tools (like \&\f(CW\*(C`virt\-sysprep\*(C'\fR, \f(CW\*(C`virt\-customize\*(C'\fR etc.) .IP \fIcommon/mlutils\fR 4 .IX Item "common/mlutils" OCaml bindings for C functions in \f(CW\*(C`common/utils\*(C'\fR, and some POSIX bindings which are missing from the OCaml stdlib. .IP \fIcommon/mlvisit\fR 4 .IX Item "common/mlvisit" OCaml bindings for the visit functions (see \fIcommon/visit\fR). .IP \fIcommon/mlxml\fR 4 .IX Item "common/mlxml" OCaml bindings for the libxml2 library. .IP \fIcommon/options\fR 4 .IX Item "common/options" Common options parsing for guestfish, guestmount and some virt tools. .IP \fIcommon/parallel\fR 4 .IX Item "common/parallel" A framework used for processing multiple libvirt domains in parallel. .IP \fIcommon/progress\fR 4 .IX Item "common/progress" Common code for printing progress bars. .IP \fIcommon/protocol\fR 4 .IX Item "common/protocol" The XDR-based communication protocol used between the library and the daemon running inside the appliance is defined here. .IP \fIcommon/qemuopts\fR 4 .IX Item "common/qemuopts" Mini-library for writing qemu command lines and qemu config files. .IP \fIcommon/structs\fR 4 .IX Item "common/structs" Common code for printing and freeing libguestfs structs, used by the library and some tools. .IP \fIcommon/utils\fR 4 .IX Item "common/utils" Various utility functions used throughout the library and tools. .IP \fIcommon/visit\fR 4 .IX Item "common/visit" Recursively visit a guestfs filesystem hierarchy. .IP \fIcommon/windows\fR 4 .IX Item "common/windows" Utility functions for handling Windows drive letters. .RE .RS 4 .RE .IP \fIcontrib\fR 4 .IX Item "contrib" Outside contributions, experimental parts. .IP \fIcustomize\fR 4 .IX Item "customize" \&\fBvirt\-customize\fR\|(1) command and documentation. .IP \fIdaemon\fR 4 .IX Item "daemon" The daemon that runs inside the libguestfs appliance and carries out actions. .IP \fIdf\fR 4 .IX Item "df" \&\fBvirt\-df\fR\|(1) command and documentation. .IP \fIdib\fR 4 .IX Item "dib" \&\fBvirt\-dib\fR\|(1) command and documentation. .IP \fIdiff\fR 4 .IX Item "diff" \&\fBvirt\-diff\fR\|(1) command and documentation. .IP \fIdocs\fR 4 .IX Item "docs" Miscellaneous manual pages. .IP \fIedit\fR 4 .IX Item "edit" \&\fBvirt\-edit\fR\|(1) command and documentation. .IP \fIexamples\fR 4 .IX Item "examples" C API example code. .IP \fIfish\fR 4 .IX Item "fish" \&\fBguestfish\fR\|(1), the command-line shell, and various shell scripts built on top such as \fBvirt\-copy\-in\fR\|(1), \fBvirt\-copy\-out\fR\|(1), \&\fBvirt\-tar\-in\fR\|(1), \fBvirt\-tar\-out\fR\|(1). .IP \fIformat\fR 4 .IX Item "format" \&\fBvirt\-format\fR\|(1) command and documentation. .IP \fIfuse\fR 4 .IX Item "fuse" \&\fBguestmount\fR\|(1), FUSE (userspace filesystem) built on top of libguestfs. .IP \fIgenerator\fR 4 .IX Item "generator" The crucially important generator, used to automatically generate large amounts of boilerplate C code for things like RPC and bindings. .IP \fIget-kernel\fR 4 .IX Item "get-kernel" \&\fBvirt\-get\-kernel\fR\|(1) command and documentation. .IP \fIinspector\fR 4 .IX Item "inspector" \&\fBvirt\-inspector\fR\|(1), the virtual machine image inspector. .IP \fIlib\fR 4 .IX Item "lib" Source code to the C library. .IP \fIlogo\fR 4 .IX Item "logo" Logo used on the website. The fish is called Arthur by the way. .IP \fIm4\fR 4 .IX Item "m4" M4 macros used by autoconf. See "THE BUILD SYSTEM". .IP \fImake-fs\fR 4 .IX Item "make-fs" \&\fBvirt\-make\-fs\fR\|(1) command and documentation. .IP \fIpo\fR 4 .IX Item "po" Translations of simple gettext strings. .IP \fIpo-docs\fR 4 .IX Item "po-docs" The build infrastructure and PO files for translations of manpages and POD files. Eventually this will be combined with the \fIpo\fR directory, but that is rather complicated. .IP \fIrescue\fR 4 .IX Item "rescue" \&\fBvirt\-rescue\fR\|(1) command and documentation. .IP \fIresize\fR 4 .IX Item "resize" \&\fBvirt\-resize\fR\|(1) command and documentation. .IP \fIsparsify\fR 4 .IX Item "sparsify" \&\fBvirt\-sparsify\fR\|(1) command and documentation. .IP \fIsysprep\fR 4 .IX Item "sysprep" \&\fBvirt\-sysprep\fR\|(1) command and documentation. .IP \fItests\fR 4 .IX Item "tests" Tests. .IP \fItest-data\fR 4 .IX Item "test-data" Files and other test data used by the tests. .IP \fItest-tool\fR 4 .IX Item "test-tool" Test tool for end users to test if their qemu/kernel combination will work with libguestfs. .IP \fItmp\fR 4 .IX Item "tmp" Used for temporary files when running the tests (instead of \fI/tmp\fR 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. .IP \fItools\fR 4 .IX Item "tools" Command line tools written in Perl (\fBvirt\-win\-reg\fR\|(1) and many others). .IP \fIutils\fR 4 .IX Item "utils" Miscellaneous utilities, such as \f(CW\*(C`boot\-benchmark\*(C'\fR. .IP \fIv2v\fR 4 .IX Item "v2v" Up to libguestfs > 1.42 this contained the \fBvirt\-v2v\fR\|(1) tool, but this has now moved into a separate repository: https://github.com/libguestfs/virt\-v2v .IP \fIwebsite\fR 4 .IX Item "website" The http://libguestfs.org website files. .IP \fIcsharp\fR 4 .IX Item "csharp" .PD 0 .IP \fIerlang\fR 4 .IX Item "erlang" .IP \fIgobject\fR 4 .IX Item "gobject" .IP \fIgolang\fR 4 .IX Item "golang" .IP \fIhaskell\fR 4 .IX Item "haskell" .IP \fIjava\fR 4 .IX Item "java" .IP \fIlua\fR 4 .IX Item "lua" .IP \fIocaml\fR 4 .IX Item "ocaml" .IP \fIphp\fR 4 .IX Item "php" .IP \fIperl\fR 4 .IX Item "perl" .IP \fIpython\fR 4 .IX Item "python" .IP \fIruby\fR 4 .IX Item "ruby" .PD Language bindings. .SS "THE BUILD SYSTEM" .IX Subsection "THE BUILD SYSTEM" Libguestfs uses the GNU autotools build system (autoconf, automake, libtool). .PP The \fI./configure\fR script is generated from \fIconfigure.ac\fR and \&\fIm4/guestfs\-*.m4\fR. Most of the configure script is split over many m4 macro files by topic, for example \fIm4/guestfs\-daemon.m4\fR deals with the dependencies of the daemon. .PP The job of the top level \fIMakefile.am\fR is mainly to list the subdirectories (\f(CW\*(C`SUBDIRS\*(C'\fR) in the order they should be compiled. .PP \&\fIcommon\-rules.mk\fR is included in every \fIMakefile.am\fR (top level and subdirectories). \fIsubdir\-rules.mk\fR is included only in subdirectory \&\fIMakefile.am\fR files. .PP There are many make targets. Use this command to list them all: .PP .Vb 1 \& make help .Ve .SH "EXTENDING LIBGUESTFS" .IX Header "EXTENDING LIBGUESTFS" .SS "ADDING A NEW API" .IX Subsection "ADDING A NEW API" Because large amounts of boilerplate code in libguestfs are generated, this makes it easy to extend the libguestfs API. .PP To add a new API action there are two changes: .IP 1. 4 You need to add a description of the call (name, parameters, return type, tests, documentation) to \fIgenerator/actions_*.ml\fR and possibly \fIgenerator/proc_nr.ml\fR. .Sp 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 \fBguestfs\-internals\fR\|(1)). "guestfs_sync" in \fBguestfs\fR\|(3) is an example of the former, since the sync is done in the appliance. "guestfs_set_trace" in \fBguestfs\fR\|(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. .Sp Most new actions are of the first type, and get added to the \&\f(CW\*(C`daemon_functions\*(C'\fR 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. .Sp For library-only actions of the second type, add to the \&\f(CW\*(C`non_daemon_functions\*(C'\fR 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 \f(CW\-1\fR. .IP 2. 4 Implement the action (in C): .Sp For daemon actions, implement the function \f(CW\*(C`do_\*(C'\fR in the \&\f(CW\*(C`daemon/\*(C'\fR directory. .Sp For library actions, implement the function \f(CW\*(C`guestfs_impl_\*(C'\fR in the \f(CW\*(C`lib/\*(C'\fR directory. .Sp In either case, use another function as an example of what to do. .IP 3. 4 As an alternative to step 2: Since libguestfs 1.38, daemon actions can be implemented in OCaml. You have to set the \f(CW\*(C`impl = OCaml ...\*(C'\fR flag in the generator. Take a look at \fIdaemon/file.ml\fR for an example. .PP After making these changes, use \f(CW\*(C`make\*(C'\fR to compile. .PP 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. .PP \fIAdding tests for an API\fR .IX Subsection "Adding tests for an API" .PP 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 (\fIgenerator/actions_*.ml\fR), or in some rarer cases you may want to drop a script into \f(CW\*(C`tests/*/\*(C'\fR. Note that adding a script to \f(CW\*(C`tests/*/\*(C'\fR is slower, so if possible use the first method. .PP The following describes the test environment used when you add an API test in \fIactions_*.ml\fR. .PP The test environment has 4 block devices: .IP "\fI/dev/sda\fR 2 GB" 4 .IX Item "/dev/sda 2 GB" General block device for testing. .IP "\fI/dev/sdb\fR 2 GB" 4 .IX Item "/dev/sdb 2 GB" \&\fI/dev/sdb1\fR is an ext2 filesystem used for testing filesystem write operations. .IP "\fI/dev/sdc\fR 10 MB" 4 .IX Item "/dev/sdc 10 MB" Used in a few tests where two block devices are needed. .IP \fI/dev/sdd\fR 4 .IX Item "/dev/sdd" ISO with fixed content (see \fIimages/test.iso\fR). .PP 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 \fBguestfs\fR\|(3) :\-x .PP Each test starts with an initial scenario, selected using one of the \&\f(CW\*(C`Init*\*(C'\fR expressions, described in \fIgenerator/types.ml\fR. These initialize the disks mentioned above in a particular way as documented in \fItypes.ml\fR. You should not assume anything about the previous contents of other disks that are not initialized. .PP 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 \f(CW\*(C`Always\*(C'\fR means to run unconditionally. .PP In addition, packagers can skip individual tests by setting environment variables before running \f(CW\*(C`make check\*(C'\fR. .PP .Vb 1 \& SKIP_TEST__=1 .Ve .PP eg: \f(CW\*(C`SKIP_TEST_COMMAND_3=1\*(C'\fR skips test #3 of "guestfs_command" in \fBguestfs\fR\|(3). .PP or: .PP .Vb 1 \& SKIP_TEST_=1 .Ve .PP eg: \f(CW\*(C`SKIP_TEST_ZEROFREE=1\*(C'\fR skips all "guestfs_zerofree" in \fBguestfs\fR\|(3) tests. .PP Packagers can run only certain tests by setting for example: .PP .Vb 1 \& TEST_ONLY="vfs_type zerofree" .Ve .PP See \fItests/c\-api/tests.c\fR for more details of how these environment variables work. .PP \fIDebugging new APIs\fR .IX Subsection "Debugging new APIs" .PP Test new actions work before submitting them. .PP You can use guestfish to try out new commands. .PP 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 \f(CW\*(C`guestfish \-v\*(C'\fR. .SS "ADDING A NEW LANGUAGE BINDING" .IX Subsection "ADDING A NEW LANGUAGE BINDING" All language bindings must be generated by the generator (see the \fIgenerator\fR subdirectory). .PP There is no documentation for this yet. We suggest you look at an existing binding, eg. \fIgenerator/ocaml.ml\fR or \&\fIgenerator/perl.ml\fR. .PP \fIAdding tests for language bindings\fR .IX Subsection "Adding tests for language bindings" .PP 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. .PP 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. .PP This is the numbering scheme used by the tests: .PP .Vb 1 \& \- 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\*(Aqd 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 .Ve .PP To save time when running the tests, only 100, 430, 800+, 900+ should launch the handle. .SS "FORMATTING CODE" .IX Subsection "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. .PP If you use Emacs, add the following to one of your start-up files (e.g., ~/.emacs), to help ensure that you get indentation right: .PP .Vb 9 \& ;;; In libguestfs, indent with spaces everywhere (not TABs). \& ;;; Exceptions: Makefile and ChangeLog modes. \& (add\-hook \*(Aqfind\-file\-hook \& \*(Aq(lambda () (if (and buffer\-file\-name \& (string\-match "/libguestfs\e\e>" \& (buffer\-file\-name)) \& (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." \& (interactive) \& (c\-set\-style "K&R") \& (setq c\-indent\-level 2) \& (setq c\-basic\-offset 2)) \& (add\-hook \*(Aqc\-mode\-hook \& \*(Aq(lambda () (if (string\-match "/libguestfs\e\e>" \& (buffer\-file\-name)) \& (libguestfs\-c\-mode)))) .Ve .SS "TESTING YOUR CHANGES" .IX Subsection "TESTING YOUR CHANGES" Turn warnings into errors when developing to make warnings hard to ignore: .PP .Vb 1 \& ./configure \-\-enable\-werror .Ve .PP Useful targets are: .ie n .IP """make check""" 4 .el .IP "\f(CWmake check\fR" 4 .IX Item "make check" Runs the regular test suite. .Sp This is implemented using the regular automake \f(CW\*(C`TESTS\*(C'\fR target. See the automake documentation for details. .ie n .IP """make check\-valgrind""" 4 .el .IP "\f(CWmake check\-valgrind\fR" 4 .IX Item "make check-valgrind" Runs a subset of the test suite under valgrind. .Sp See "VALGRIND" below. .ie n .IP """make check\-valgrind\-local\-guests""" 4 .el .IP "\f(CWmake check\-valgrind\-local\-guests\fR" 4 .IX Item "make check-valgrind-local-guests" Runs a subset of the test suite under valgrind using locally installed libvirt guests (read-only). .ie n .IP """make check\-direct""" 4 .el .IP "\f(CWmake check\-direct\fR" 4 .IX Item "make check-direct" Runs all tests using default appliance back-end. This only has any effect if a non-default backend was selected using \f(CW\*(C`./configure \-\-with\-default\-backend=...\*(C'\fR .ie n .IP """make check\-valgrind\-direct""" 4 .el .IP "\f(CWmake check\-valgrind\-direct\fR" 4 .IX Item "make check-valgrind-direct" Run a subset of the test suite under valgrind using the default appliance back-end. .ie n .IP """make check\-with\-upstream\-qemu""" 4 .el .IP "\f(CWmake check\-with\-upstream\-qemu\fR" 4 .IX Item "make check-with-upstream-qemu" Runs all tests using a local qemu binary. It looks for the qemu binary in QEMUDIR (defaults to \fR\f(CI$HOME\fR\fI/d/qemu\fR), but you can set this to another directory on the command line, eg: .Sp .Vb 1 \& make check\-with\-upstream\-qemu QEMUDIR=/usr/src/qemu .Ve .ie n .IP """make check\-with\-upstream\-libvirt""" 4 .el .IP "\f(CWmake check\-with\-upstream\-libvirt\fR" 4 .IX Item "make check-with-upstream-libvirt" Runs all tests using a local libvirt. This only has any effect if the libvirt backend was selected using \&\f(CW\*(C`./configure \-\-with\-default\-backend=libvirt\*(C'\fR .Sp It looks for libvirt in LIBVIRTDIR (defaults to \fR\f(CI$HOME\fR\fI/d/libvirt\fR), but you can set this to another directory on the command line, eg: .Sp .Vb 1 \& make check\-with\-upstream\-libvirt LIBVIRTDIR=/usr/src/libvirt .Ve .ie n .IP """make check\-slow""" 4 .el .IP "\f(CWmake check\-slow\fR" 4 .IX Item "make check-slow" Runs some slow/long\-running tests which are not run by default. .Sp To mark a test as slow/long\-running: .RS 4 .IP \(bu 4 Add it to the list of \f(CW\*(C`TESTS\*(C'\fR in the \fIMakefile.am\fR, just like a normal test. .IP \(bu 4 Modify the test so it checks if the \f(CW\*(C`SLOW=1\*(C'\fR environment variable is set, and if \fInot\fR set it skips (ie. returns with exit code 77). If using \f(CW$TEST_FUNCTIONS\fR, you can call the function \f(CW\*(C`slow_test\*(C'\fR for this. .IP \(bu 4 Add a variable \f(CW\*(C`SLOW_TESTS\*(C'\fR to the \fIMakefile.am\fR listing the slow tests. .IP \(bu 4 Add a rule to the \fIMakefile.am\fR: .Sp .Vb 2 \& check\-slow: \& $(MAKE) check TESTS="$(SLOW_TESTS)" SLOW=1 .Ve .RE .RS 4 .RE .ie n .IP """sudo make check\-root""" 4 .el .IP "\f(CWsudo make check\-root\fR" 4 .IX Item "sudo make check-root" Runs some tests which require root privileges. These are supposed to be safe, but take care. You have to run this as root (eg. using \&\fBsudo\fR\|(8) explicitly). .Sp To mark a test as requiring root: .RS 4 .IP \(bu 4 Add it to the list of \f(CW\*(C`TESTS\*(C'\fR in the \fIMakefile.am\fR, just like a normal test. .IP \(bu 4 Modify the test so it checks if euid == 0, and if \fInot\fR set it skips (ie. returns with exit code 77). If using \f(CW$TEST_FUNCTIONS\fR, you can call the function \f(CW\*(C`root_test\*(C'\fR for this. .IP \(bu 4 Add a variable \f(CW\*(C`ROOT_TESTS\*(C'\fR to the \fIMakefile.am\fR listing the root tests. .IP \(bu 4 Add a rule to the \fIMakefile.am\fR: .Sp .Vb 2 \& check\-root: \& $(MAKE) check TESTS="$(ROOT_TESTS)" .Ve .RE .RS 4 .RE .ie n .IP """make check\-all""" 4 .el .IP "\f(CWmake check\-all\fR" 4 .IX Item "make check-all" Equivalent to running all \f(CW\*(C`make check*\*(C'\fR rules except \f(CW\*(C`check\-root\*(C'\fR. .ie n .IP """make check\-release""" 4 .el .IP "\f(CWmake check\-release\fR" 4 .IX Item "make check-release" Runs a subset of \f(CW\*(C`make check*\*(C'\fR rules that are required to pass before a tarball can be released. Currently this is: .RS 4 .IP \(bu 4 check .IP \(bu 4 check-valgrind .IP \(bu 4 check-direct .IP \(bu 4 check-valgrind-direct .IP \(bu 4 check-slow .RE .RS 4 .RE .ie n .IP """make installcheck""" 4 .el .IP "\f(CWmake installcheck\fR" 4 .IX Item "make installcheck" Run \f(CW\*(C`make check\*(C'\fR on the installed copy of libguestfs. .Sp The version of installed libguestfs being tested, and the version of the libguestfs source tree must be the same. .Sp Do: .Sp .Vb 4 \& ./configure \& make clean ||: \& make \& make installcheck .Ve .SS VALGRIND .IX Subsection "VALGRIND" When you do \f(CW\*(C`make check\-valgrind\*(C'\fR, it searches for any \fIMakefile.am\fR in the tree that has a \f(CW\*(C`check\-valgrind:\*(C'\fR target and runs it. .PP Writing the \fIMakefile.am\fR and tests correctly to use valgrind and working with automake parallel tests is subtle. .PP If your tests are run via a shell script wrapper, then in the wrapper use: .PP .Vb 1 \& $VG virt\-foo .Ve .PP and in the \fIMakefile.am\fR use: .PP .Vb 2 \& check\-valgrind: \& make VG="@VG@" check .Ve .PP However, if your binaries run directly from the \f(CW\*(C`TESTS\*(C'\fR rule, you have to modify the \fIMakefile.am\fR like this: .PP .Vb 1 \& LOG_COMPILER = $(VG) \& \& check\-valgrind: \& make VG="@VG@" check .Ve .PP In either case, check that the right program is being tested by examining the \fItmp/valgrind*\fR log files carefully. .SS "SUBMITTING PATCHES" .IX Subsection "SUBMITTING PATCHES" Submit patches to the mailing list: https://lists.libguestfs.org and CC to rjones@redhat.com. .PP 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. .SS "INTERNATIONALIZATION (I18N) SUPPORT" .IX Subsection "INTERNATIONALIZATION (I18N) SUPPORT" We support i18n (gettext anyhow) in the library. .PP 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. .PP Debugging messages are never translated, since they are intended for the programmers. .SH "MISCELLANEOUS TOPICS" .IX Header "MISCELLANEOUS TOPICS" .SS "HOW OCAML PROGRAMS ARE COMPILED AND LINKED" .IX Subsection "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. .PP 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" (\f(CW"DEPENDENCIES"\fR in automake-speak) that happen to be the OCaml objects. This works because OCaml programs usually have C files for native bindings etc. .PP So a typical program is described as just its C sources: .PP .Vb 1 \& virt_customize_SOURCES = ... crypt\-c.c perl_edit\-c.c .Ve .PP For programs that have no explicit C sources, we create an empty \&\fIdummy.c\fR file, and list that instead: .PP .Vb 1 \& virt_resize_SOURCES = dummy.c .Ve .PP The OCaml objects which contain most of the code are listed as automake dependencies (other dependencies may also be listed): .PP .Vb 1 \& virt_customize_DEPENDENCIES = ... customize_main.cmx .Ve .PP 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 \f(CW\*(C`\-cclib\*(C'\fR libraries in the correct order otherwise. .PP .Vb 2 \& virt_customize_LINK = \e \& $(top_builddir)/ocaml\-link.sh \-cclib \*(Aq\-lutils\*(Aq \-\- ... .Ve .PP The actual rules, which you can examine in \fIcustomize/Makefile.am\fR, are a little bit more complicated than this because they have to handle: .IP \(bu 4 Compiling for byte code or native code. .IP \(bu 4 The pattern rules needed to compile the OCaml sources to objects. .Sp These are now kept in \fIsubdir\-rules.mk\fR at the top level, which is included in every subdirectory \fIMakefile.am\fR. .IP \(bu 4 Adding OCaml sources files to \f(CW\*(C`EXTRA_DIST\*(C'\fR. .Sp Automake isn't aware of the complete list of sources for a binary, so it will not add them all automatically. .SH "MAINTAINER TASKS" .IX Header "MAINTAINER TASKS" .SS "MAINTAINER MAKEFILE TARGETS" .IX Subsection "MAINTAINER MAKEFILE TARGETS" These \f(CW\*(C`make\*(C'\fR targets probably won’t work and aren't useful unless you are a libguestfs maintainer. .PP \fImake maintainer-commit\fR .IX Subsection "make maintainer-commit" .PP This commits everything in the working directory with the commit message \f(CW\*(C`Version $(VERSION).\*(C'\fR. You must update \fIconfigure.ac\fR, clean and rebuild first. .PP \fImake maintainer-tag\fR .IX Subsection "make maintainer-tag" .PP This tags the current HEAD commit with the tag \f(CW\*(C`v$(VERSION)\*(C'\fR and one of the messages: .PP .Vb 1 \& Version $(VERSION) stable \& \& Version $(VERSION) development .Ve .PP (See "LIBGUESTFS VERSION NUMBERS" in \fBguestfs\fR\|(3) for the difference between a stable and development release.) .PP \fImake maintainer-check-authors\fR .IX Subsection "make maintainer-check-authors" .PP Check that all authors (found in git commit messages) are included in the \fIgenerator/authors.ml\fR file. .PP \fImake maintainer-check-extra-dist\fR .IX Subsection "make maintainer-check-extra-dist" .PP This rule must be run after \f(CW\*(C`make dist\*(C'\fR (so there is a tarball in the working directory). It compares the contents of the tarball with the contents of git to ensure that no files have been missed from \&\fIMakefile.am\fR \f(CW\*(C`EXTRA_DIST\*(C'\fR rules. .PP \fImake maintainer-upload-website\fR .IX Subsection "make maintainer-upload-website" .PP This is used by the software used to automate libguestfs releases to copy the libguestfs website to another git repository before it is uploaded to the web server. .SS "MAKING A STABLE RELEASE" .IX Subsection "MAKING A STABLE RELEASE" When we make a stable release, there are several steps documented here. See "LIBGUESTFS VERSION NUMBERS" in \fBguestfs\fR\|(3) for general information about the stable branch policy. .IP \(bu 4 Check \f(CW\*(C`make && make check\*(C'\fR works on at least: .RS 4 .IP "Fedora (x86\-64)" 4 .IX Item "Fedora (x86-64)" .PD 0 .IP "Debian (x86\-64)" 4 .IX Item "Debian (x86-64)" .IP "Ubuntu (x86\-64)" 4 .IX Item "Ubuntu (x86-64)" .IP "Fedora (aarch64)" 4 .IX Item "Fedora (aarch64)" .IP "Fedora (ppc64)" 4 .IX Item "Fedora (ppc64)" .IP "Fedora (ppc64le)" 4 .IX Item "Fedora (ppc64le)" .RE .RS 4 .RE .IP \(bu 4 .PD Check \f(CW\*(C`./configure \-\-without\-libvirt\*(C'\fR works. .IP \(bu 4 Finalize \fIguestfs\-release\-notes.pod\fR .IP \(bu 4 Create new stable and development directories under http://libguestfs.org/download. .IP \(bu 4 Edit \fIwebsite/index.html.in\fR. .IP \(bu 4 Set the version (in \fIconfigure.ac\fR) to the new \fIstable\fR version, ie. 1.XX.0, and commit it: .Sp .Vb 6 \& ./localconfigure \& make distclean \-k \& ./localconfigure \& make && make dist \& make maintainer\-commit \& make maintainer\-tag .Ve .IP \(bu 4 Create the stable branch in git: .Sp .Vb 2 \& git branch stable\-1.XX \& git push origin stable\-1.XX .Ve .IP \(bu 4 Do a full release of the stable branch. .IP \(bu 4 Set the version to the next development version and commit that. Optionally do a full release of the development branch. .SH "INTERNAL DOCUMENTATION" .IX Header "INTERNAL DOCUMENTATION" This section documents internal functions inside libguestfs and various utilities. It is intended for libguestfs developers only. .PP This section is autogenerated from \f(CW\*(C`/**\*(C'\fR comments in source files, which are marked up in POD format. .PP \&\fBThese functions are not publicly exported, and may change or be removed at any time.\fR .SS "Subdirectory \fIlib\fP" .IX Subsection "Subdirectory lib" \fIFile lib/actions\-support.c\fR .IX Subsection "File lib/actions-support.c" .PP Helper functions for the actions code in \fIlib/actions\-*.c\fR. .PP \fIFile lib/appliance\-cpu.c\fR .IX Subsection "File lib/appliance-cpu.c" .PP The appliance choice of CPU model. .PP Function \f(CW\*(C`lib/appliance\-cpu.c:guestfs_int_get_cpu_model\*(C'\fR .IX Subsection "Function lib/appliance-cpu.c:guestfs_int_get_cpu_model" .PP .Vb 2 \& const char * \& guestfs_int_get_cpu_model (int kvm) .Ve .PP Return the right CPU model to use as the qemu \f(CW\*(C`\-cpu\*(C'\fR parameter or its equivalent in libvirt. This returns: .ie n .IP """host""" 4 .el .IP "\f(CW""host""\fR" 4 .IX Item """host""" The literal string \f(CW"host"\fR means use \f(CW\*(C`\-cpu host\*(C'\fR. .ie n .IP """max""" 4 .el .IP "\f(CW""max""\fR" 4 .IX Item """max""" The literal string \f(CW"max"\fR means use \f(CW\*(C`\-cpu max\*(C'\fR (the best possible). This requires awkward translation for libvirt. .IP "some string" 4 .IX Item "some string" Some string such as \f(CW"cortex\-a57"\fR means use \f(CW\*(C`\-cpu cortex\-a57\*(C'\fR. .ie n .IP """NULL""" 4 .el .IP \f(CWNULL\fR 4 .IX Item "NULL" \&\f(CW\*(C`NULL\*(C'\fR means no \f(CW\*(C`\-cpu\*(C'\fR option at all. Note returning \f(CW\*(C`NULL\*(C'\fR does not indicate an error. .PP This is made unnecessarily hard and fragile because of two stupid choices in QEMU: .IP \(bu 4 The default for \f(CW\*(C`qemu\-system\-aarch64 \-M virt\*(C'\fR is to emulate a \&\f(CW\*(C`cortex\-a15\*(C'\fR (WTF?). .IP \(bu 4 We don't know for sure if KVM will work, but \f(CW\*(C`\-cpu host\*(C'\fR is broken with TCG, so we almost always pass a broken \f(CW\*(C`\-cpu\*(C'\fR flag if KVM is semi-broken in any way. .PP \fIFile lib/appliance\-kcmdline.c\fR .IX Subsection "File lib/appliance-kcmdline.c" .PP The appliance kernel command line. .PP Definition \f(CW\*(C`lib/appliance\-kcmdline.c:VALID_TERM\*(C'\fR .IX Subsection "Definition lib/appliance-kcmdline.c:VALID_TERM" .PP .Vb 1 \& #define VALID_TERM .Ve .PP Check that the \f(CW$TERM\fR environment variable is reasonable before we pass it through to the appliance. .PP Function \f(CW\*(C`lib/appliance\-kcmdline.c:get_root_uuid_with_file\*(C'\fR .IX Subsection "Function lib/appliance-kcmdline.c:get_root_uuid_with_file" .PP .Vb 2 \& static char * \& get_root_uuid_with_file (guestfs_h *g, const char *appliance) .Ve .PP Given a disk image containing an extX filesystem, return the UUID. .PP Function \f(CW\*(C`lib/appliance\-kcmdline.c:run_qemu_img_dd\*(C'\fR .IX Subsection "Function lib/appliance-kcmdline.c:run_qemu_img_dd" .PP .Vb 2 \& static int \& run_qemu_img_dd (guestfs_h *g, const char *in_file, char *out_file) .Ve .PP Read the first 256k bytes of the in_file with \fBqemu\-img\fR\|(1) command and write them into the out_file. That may be useful to get UUID of the QCOW2 disk image with \f(CW\*(C`get_root_uuid_with_file\*(C'\fR. .PP The function returns zero if successful, otherwise \-1. .PP Function \f(CW\*(C`lib/appliance\-kcmdline.c:get_root_uuid\*(C'\fR .IX Subsection "Function lib/appliance-kcmdline.c:get_root_uuid" .PP .Vb 2 \& static char * \& get_root_uuid (guestfs_h *g, const char *appliance) .Ve .PP Get the UUID from the appliance disk image. .PP Function \f(CW\*(C`lib/appliance\-kcmdline.c:guestfs_int_appliance_command_line\*(C'\fR .IX Subsection "Function lib/appliance-kcmdline.c:guestfs_int_appliance_command_line" .PP .Vb 4 \& char * \& guestfs_int_appliance_command_line (guestfs_h *g, \& const char *appliance, \& int flags) .Ve .PP Construct the Linux command line passed to the appliance. This is used by the \f(CW\*(C`direct\*(C'\fR and \f(CW\*(C`libvirt\*(C'\fR backends, and is simply located in this file because it's a convenient place for this common code. .PP The \f(CW\*(C`appliance\*(C'\fR parameter is the filename of the appliance (could be NULL) from which we obtain the root UUID. .PP The \f(CW\*(C`flags\*(C'\fR parameter can contain the following flags logically or'd together (or 0): .ie n .IP """APPLIANCE_COMMAND_LINE_IS_TCG""" 4 .el .IP \f(CWAPPLIANCE_COMMAND_LINE_IS_TCG\fR 4 .IX Item "APPLIANCE_COMMAND_LINE_IS_TCG" 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. .PP Note that this function returns a newly allocated buffer which must be freed by the caller. .PP \fIFile lib/appliance\-uefi.c\fR .IX Subsection "File lib/appliance-uefi.c" .PP Find the UEFI firmware needed to boot the appliance. .PP See also \fIlib/uefi.c\fR (autogenerated file) containing the firmware file locations. .PP Function \f(CW\*(C`lib/appliance\-uefi.c:guestfs_int_get_uefi\*(C'\fR .IX Subsection "Function lib/appliance-uefi.c:guestfs_int_get_uefi" .PP .Vb 4 \& int \& guestfs_int_get_uefi (guestfs_h *g, char *const *firmwares, \& const char **firmware, char **code, char **vars, \& int *flags) .Ve .PP 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). .PP \&\f(CW\*(C`firmwares\*(C'\fR is an optional list of allowed values for the firmware autoselection of libvirt. It is \f(CW\*(C`NULL\*(C'\fR to indicate it is not supported. \f(CW*firmware\fR is set to one of the strings in \&\f(CW\*(C`firmwares\*(C'\fR in case one can be used. .PP \&\f(CW*code\fR is initialized with the path to the read-only UEFI code file. \f(CW*vars\fR is initialized with the path to a copy of the UEFI vars file (which is cleaned up automatically on exit). .PP In case a UEFI firmware is available, either \f(CW*firmware\fR is set to a non\-\f(CW\*(C`NULL\*(C'\fR value, or \f(CW*code\fR and \f(CW*vars\fR are. .PP \&\f(CW*code\fR and \f(CW*vars\fR should be freed by the caller, and \&\f(CW*firmware\fR \fBmust\fR not. .PP If the function returns \f(CW\-1\fR then there was a real error which should cause appliance building to fail (no UEFI firmware is not an error). .PP See also \fIvirt\-v2v.git/v2v/utils.ml\fR:find_uefi_firmware .PP \fIFile lib/appliance.c\fR .IX Subsection "File lib/appliance.c" .PP This file deals with building the libguestfs appliance. .PP Function \f(CW\*(C`lib/appliance.c:guestfs_int_build_appliance\*(C'\fR .IX Subsection "Function lib/appliance.c:guestfs_int_build_appliance" .PP .Vb 5 \& int \& guestfs_int_build_appliance (guestfs_h *g, \& char **kernel_rtn, \& char **initrd_rtn, \& char **appliance_rtn) .Ve .PP Locate or build the appliance. .PP 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. .PP The return value is \f(CW0\fR = good, \f(CW\-1\fR = error. Returned in \&\f(CW\*(C`appliance.kernel\*(C'\fR will be the name of the kernel to use, \&\f(CW\*(C`appliance.initrd\*(C'\fR the name of the initrd, \f(CW\*(C`appliance.image\*(C'\fR the name of the ext2 root filesystem. \f(CW\*(C`appliance.image\*(C'\fR can be \f(CW\*(C`NULL\*(C'\fR, 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 \fInot\fR be deleted. .PP The process is as follows: .IP 1. 4 Look in \f(CW\*(C`path\*(C'\fR which contains a supermin appliance skeleton. If no element has this, skip straight to step 3. .IP 2. 4 Call \f(CW\*(C`supermin \-\-build\*(C'\fR to build the full appliance (if it needs to be rebuilt). If this is successful, return the full appliance. .IP 3. 4 Check \f(CW\*(C`path\*(C'\fR, looking for a fixed appliance. If one is found, return it. .IP 4. 4 Check \f(CW\*(C`path\*(C'\fR, looking for an old-style appliance. If one is found, return it. .PP The supermin appliance cache directory lives in \&\fR\f(CI$TMPDIR\fR\fI/.guestfs\-$UID/\fR and consists of up to four files: .PP .Vb 4 \& $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 .Ve .PP Multiple instances of libguestfs with the same UID may be racing to create an appliance. However (since supermin ≥ 5) supermin provides a \fI\-\-lock\fR flag and atomic update of the \fIappliance.d\fR subdirectory. .PP Function \f(CW\*(C`lib/appliance.c:locate_or_build_appliance\*(C'\fR .IX Subsection "Function lib/appliance.c:locate_or_build_appliance" .PP .Vb 4 \& static int \& locate_or_build_appliance (guestfs_h *g, \& struct appliance_files *appliance, \& const char *path) .Ve .PP Check \f(CW\*(C`path\*(C'\fR, looking for one of appliances: supermin appliance, fixed appliance or old-style appliance. If one of the fixed appliances is found, return it. If the supermin appliance skeleton is found, build and return appliance. .PP Return values: .PP .Vb 3 \& 1 = appliance is found, returns C, \& 0 = appliance not found, \& \-1 = error which aborts the launch process. .Ve .PP Function \f(CW\*(C`lib/appliance.c:search_appliance\*(C'\fR .IX Subsection "Function lib/appliance.c:search_appliance" .PP .Vb 2 \& static int \& search_appliance (guestfs_h *g, struct appliance_files *appliance) .Ve .PP Search elements of \f(CW\*(C`g\->path\*(C'\fR, returning the first \f(CW\*(C`appliance\*(C'\fR element which matches the predicate function \f(CW\*(C`locate_or_build_appliance\*(C'\fR. .PP Return values: .PP .Vb 3 \& 1 = a path element matched, returns C, \& 0 = no path element matched, \& \-1 = error which aborts the launch process. .Ve .PP Function \f(CW\*(C`lib/appliance.c:build_supermin_appliance\*(C'\fR .IX Subsection "Function lib/appliance.c:build_supermin_appliance" .PP .Vb 4 \& static int \& build_supermin_appliance (guestfs_h *g, \& const char *supermin_path, \& struct appliance_files *appliance) .Ve .PP Build supermin appliance from \f(CW\*(C`supermin_path\*(C'\fR to \&\fR\f(CI$TMPDIR\fR\fI/.guestfs\-$UID\fR. .PP Returns: \f(CW0\fR = built or \f(CW\-1\fR = error (aborts launch). .PP Function \f(CW\*(C`lib/appliance.c:run_supermin_build\*(C'\fR .IX Subsection "Function lib/appliance.c:run_supermin_build" .PP .Vb 5 \& static int \& run_supermin_build (guestfs_h *g, \& const char *lockfile, \& const char *appliancedir, \& const char *supermin_path) .Ve .PP Run \f(CW\*(C`supermin \-\-build\*(C'\fR and tell it to generate the appliance. .PP Function \f(CW\*(C`lib/appliance.c:dir_contains_file\*(C'\fR .IX Subsection "Function lib/appliance.c:dir_contains_file" .PP .Vb 2 \& static int \& dir_contains_file (guestfs_h *g, const char *dir, const char *file) .Ve .PP Returns true iff \f(CW\*(C`file\*(C'\fR is contained in \f(CW\*(C`dir\*(C'\fR. .PP Function \f(CW\*(C`lib/appliance.c:dir_contains_files\*(C'\fR .IX Subsection "Function lib/appliance.c:dir_contains_files" .PP .Vb 2 \& static int \& dir_contains_files (guestfs_h *g, const char *dir, ...) .Ve .PP Returns true iff every listed file is contained in \f(CW\*(C`dir\*(C'\fR. .PP \fIFile lib/command.c\fR .IX Subsection "File lib/command.c" .PP A wrapper for running external commands, loosely based on libvirt's \&\f(CW\*(C`virCommand\*(C'\fR interface. .PP In outline to use this interface you must: .IP 1. 4 Create a new command handle: .Sp .Vb 2 \& struct command *cmd; \& cmd = guestfs_int_new_command (g); .Ve .IP 2. 4 \&\fIEither\fR add arguments: .Sp .Vb 3 \& guestfs_int_cmd_add_arg (cmd, "qemu\-img"); \& guestfs_int_cmd_add_arg (cmd, "info"); \& guestfs_int_cmd_add_arg (cmd, filename); .Ve .Sp (\fBNB:\fR You don't need to add a \f(CW\*(C`NULL\*(C'\fR argument at the end.) .IP 3. 4 \&\fIOr\fR construct a command using a mix of quoted and unquoted strings. (This is useful for \fBsystem\fR\|(3)/\f(CWpopen("r")\fR\-style shell commands, with the added safety of allowing args to be quoted properly). .Sp .Vb 2 \& guestfs_int_cmd_add_string_unquoted (cmd, "qemu\-img info "); \& guestfs_int_cmd_add_string_quoted (cmd, filename); .Ve .IP 4. 4 Set various flags, such as whether you want to capture errors in the regular libguestfs error log. .IP 5. 4 Run the command. This is what does the \fBfork\fR\|(2) call, optionally loops over the output, and then does a \fBwaitpid\fR\|(3) and returns the exit status of the command. .Sp .Vb 4 \& r = guestfs_int_cmd_run (cmd); \& if (r == \-1) \& // error \& // else test r using the WIF* functions .Ve .IP 6. 4 Close the handle: .Sp .Vb 1 \& guestfs_int_cmd_close (cmd); .Ve .Sp (or use \f(CW\*(C`CLEANUP_CMD_CLOSE\*(C'\fR). .PP Function \f(CW\*(C`lib/command.c:guestfs_int_new_command\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_new_command" .PP .Vb 2 \& struct command * \& guestfs_int_new_command (guestfs_h *g) .Ve .PP Create a new command handle. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_add_arg\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_add_arg" .PP .Vb 2 \& void \& guestfs_int_cmd_add_arg (struct command *cmd, const char *arg) .Ve .PP Add single arg (for \f(CW\*(C`execv\*(C'\fR\-style command execution). .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_add_arg_format\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_add_arg_format" .PP .Vb 2 \& void \& guestfs_int_cmd_add_arg_format (struct command *cmd, const char *fs, ...) .Ve .PP Add single arg (for \f(CW\*(C`execv\*(C'\fR\-style command execution) using a \fBprintf\fR\|(3)\-style format string. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_add_string_unquoted\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_add_string_unquoted" .PP .Vb 2 \& void \& guestfs_int_cmd_add_string_unquoted (struct command *cmd, const char *str) .Ve .PP Add a string (for \fBsystem\fR\|(3)\-style command execution). .PP This variant adds the strings without quoting them, which is dangerous if the string contains untrusted content. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_add_string_quoted\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_add_string_quoted" .PP .Vb 2 \& void \& guestfs_int_cmd_add_string_quoted (struct command *cmd, const char *str) .Ve .PP Add a string (for \fBsystem\fR\|(3)\-style command execution). .PP 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 \fBsystem\fR\|(3)\-style command string. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_set_stdout_callback\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_set_stdout_callback" .PP .Vb 4 \& void \& guestfs_int_cmd_set_stdout_callback (struct command *cmd, \& cmd_stdout_callback stdout_callback, \& void *stdout_data, unsigned flags) .Ve .PP Set a callback which will capture stdout. .PP If flags contains \f(CW\*(C`CMD_STDOUT_FLAG_LINE_BUFFER\*(C'\fR (the default), then the callback is called line by line on the output. If there is a trailing \f(CW\*(C`\en\*(C'\fR then it is automatically removed before the callback is called. The line buffer is \f(CW\*(C`\e0\*(C'\fR\-terminated. .PP If flags contains \f(CW\*(C`CMD_STDOUT_FLAG_UNBUFFERED\*(C'\fR, then buffers are passed to the callback as it is received from the command. Note in this case the buffer is \fInot\fR \f(CW\*(C`\e0\*(C'\fR\-terminated, so you need to may attention to the length field in the callback. .PP If flags contains \f(CW\*(C`CMD_STDOUT_FLAG_WHOLE_BUFFER\*(C'\fR, then the callback is called exactly once, with the entire buffer. Note in this case the buffer is \fInot\fR \f(CW\*(C`\e0\*(C'\fR\-terminated, so you need to may attention to the length field in the callback. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_set_stderr_to_stdout\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_set_stderr_to_stdout" .PP .Vb 2 \& void \& guestfs_int_cmd_set_stderr_to_stdout (struct command *cmd) .Ve .PP Equivalent to adding \f(CW\*(C`2>&1\*(C'\fR to the end of the command. This is incompatible with the \f(CW\*(C`capture_errors\*(C'\fR flag, because it doesn't make sense to combine them. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_clear_capture_errors\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_clear_capture_errors" .PP .Vb 2 \& void \& guestfs_int_cmd_clear_capture_errors (struct command *cmd) .Ve .PP Clear the \f(CW\*(C`capture_errors\*(C'\fR flag. This means that any errors will go to stderr, instead of being captured in the event log, and that is usually undesirable. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_set_child_callback\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_set_child_callback" .PP .Vb 4 \& void \& guestfs_int_cmd_set_child_callback (struct command *cmd, \& cmd_child_callback child_callback, \& void *data) .Ve .PP 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. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_set_child_rlimit\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_set_child_rlimit" .PP .Vb 2 \& void \& guestfs_int_cmd_set_child_rlimit (struct command *cmd, int resource, long limit) .Ve .PP Set up child rlimits, in case the process we are running could consume lots of space or time. .PP Function \f(CW\*(C`lib/command.c:finish_command\*(C'\fR .IX Subsection "Function lib/command.c:finish_command" .PP .Vb 2 \& static void \& finish_command (struct command *cmd) .Ve .PP Finish off the command by either \f(CW\*(C`NULL\*(C'\fR\-terminating the argv array or adding a terminating \f(CW\*(C`\e0\*(C'\fR to the string, or die with an internal error if no command has been added. .PP Function \f(CW\*(C`lib/command.c:loop\*(C'\fR .IX Subsection "Function lib/command.c:loop" .PP .Vb 2 \& static int \& loop (struct command *cmd) .Ve .PP The loop which reads errors and output and directs it either to the log or to the stdout callback as appropriate. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_run\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_run" .PP .Vb 2 \& int \& guestfs_int_cmd_run (struct command *cmd) .Ve .PP Fork, run the command, loop over the output, and waitpid. .PP Returns the exit status. Test it using \f(CW\*(C`WIF*\*(C'\fR macros. .PP On error: Calls \f(CW\*(C`error\*(C'\fR and returns \f(CW\-1\fR. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_pipe_run\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_pipe_run" .PP .Vb 2 \& int \& guestfs_int_cmd_pipe_run (struct command *cmd, const char *mode) .Ve .PP Fork and run the command, but don't wait. Roughly equivalent to \&\f(CW\*(C`popen\ (...,\ "r"|"w")\*(C'\fR. .PP Returns the file descriptor of the pipe, connected to stdout (\f(CW"r"\fR) or stdin (\f(CW"w"\fR) of the child process. .PP After reading/writing to this pipe, call \&\f(CW\*(C`guestfs_int_cmd_pipe_wait\*(C'\fR to wait for the status of the child. .PP Errors from the subcommand cannot be captured to the error log using this interface. Instead the caller should call \&\f(CW\*(C`guestfs_int_cmd_get_pipe_errors\*(C'\fR (after \&\f(CW\*(C`guestfs_int_cmd_pipe_wait\*(C'\fR returns an error). .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_pipe_wait\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_pipe_wait" .PP .Vb 2 \& int \& guestfs_int_cmd_pipe_wait (struct command *cmd) .Ve .PP Wait for a subprocess created by \f(CW\*(C`guestfs_int_cmd_pipe_run\*(C'\fR to finish. On error (eg. failed syscall) this returns \f(CW\-1\fR and sets the error. If the subcommand fails, then use \f(CW\*(C`WIF*\*(C'\fR macros to check this, and call \f(CW\*(C`guestfs_int_cmd_get_pipe_errors\*(C'\fR to read the error messages printed by the child. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_get_pipe_errors\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_get_pipe_errors" .PP .Vb 2 \& char * \& guestfs_int_cmd_get_pipe_errors (struct command *cmd) .Ve .PP Read the error messages printed by the child. The caller must free the returned buffer after use. .PP Function \f(CW\*(C`lib/command.c:guestfs_int_cmd_close\*(C'\fR .IX Subsection "Function lib/command.c:guestfs_int_cmd_close" .PP .Vb 2 \& void \& guestfs_int_cmd_close (struct command *cmd) .Ve .PP Close the \f(CW\*(C`cmd\*(C'\fR object and free all resources. .PP Function \f(CW\*(C`lib/command.c:process_line_buffer\*(C'\fR .IX Subsection "Function lib/command.c:process_line_buffer" .PP .Vb 2 \& static void \& process_line_buffer (struct command *cmd, int closed) .Ve .PP Deal with buffering stdout for the callback. .PP \fIFile lib/conn\-socket.c\fR .IX Subsection "File lib/conn-socket.c" .PP This file handles connections to the child process where this is done over regular POSIX sockets. .PP Function \f(CW\*(C`lib/conn\-socket.c:handle_log_message\*(C'\fR .IX Subsection "Function lib/conn-socket.c:handle_log_message" .PP .Vb 3 \& static int \& handle_log_message (guestfs_h *g, \& struct connection_socket *conn) .Ve .PP This is called if \f(CW\*(C`conn\->console_sock\*(C'\fR becomes ready to read while we are doing one of the connection operations above. It reads and deals with the log message. .PP Returns: .ie n .IP 1 4 .el .IP \f(CW1\fR 4 .IX Item "1" log message(s) were handled successfully .ie n .IP 0 4 .el .IP \f(CW0\fR 4 .IX Item "0" connection to appliance closed .ie n .IP \-1 4 .el .IP \f(CW\-1\fR 4 .IX Item "-1" error .PP Function \f(CW\*(C`lib/conn\-socket.c:guestfs_int_new_conn_socket_listening\*(C'\fR .IX Subsection "Function lib/conn-socket.c:guestfs_int_new_conn_socket_listening" .PP .Vb 4 \& struct connection * \& guestfs_int_new_conn_socket_listening (guestfs_h *g, \& int daemon_accept_sock, \& int console_sock) .Ve .PP Create a new socket connection, listening. .PP Note that it's OK for \f(CW\*(C`console_sock\*(C'\fR to be passed as \f(CW\-1\fR, meaning there's no console available for this appliance. .PP After calling this, \f(CW\*(C`daemon_accept_sock\*(C'\fR is owned by the connection, and will be closed properly either in \&\f(CW\*(C`accept_connection\*(C'\fR or \f(CW\*(C`free_connection\*(C'\fR. .PP Function \f(CW\*(C`lib/conn\-socket.c:guestfs_int_new_conn_socket_connected\*(C'\fR .IX Subsection "Function lib/conn-socket.c:guestfs_int_new_conn_socket_connected" .PP .Vb 4 \& struct connection * \& guestfs_int_new_conn_socket_connected (guestfs_h *g, \& int daemon_sock, \& int console_sock) .Ve .PP Create a new socket connection, connected. .PP As above, but the caller passes us a connected \f(CW\*(C`daemon_sock\*(C'\fR and promises not to call \f(CW\*(C`accept_connection\*(C'\fR. .PP \fIFile lib/create.c\fR .IX Subsection "File lib/create.c" .PP APIs for creating empty disks. .PP Mostly this consists of wrappers around the \fBqemu\-img\fR\|(1) program. .PP Definition \f(CW\*(C`lib/create.c:VALID_FORMAT\*(C'\fR .IX Subsection "Definition lib/create.c:VALID_FORMAT" .PP .Vb 1 \& #define VALID_FORMAT .Ve .PP Check for valid backing format. Allow any \f(CW\*(C`^[[:alnum]]+$\*(C'\fR (in C locale), but limit the length to something reasonable. .PP \fIFile lib/drives.c\fR .IX Subsection "File lib/drives.c" .PP Drives added are stored in an array in the handle. Code here manages that array and the individual \f(CW\*(C`struct drive\*(C'\fR data. .PP Function \f(CW\*(C`lib/drives.c:create_overlay\*(C'\fR .IX Subsection "Function lib/drives.c:create_overlay" .PP .Vb 2 \& static int \& create_overlay (guestfs_h *g, struct drive *drv) .Ve .PP 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. .PP Function \f(CW\*(C`lib/drives.c:create_drive_file\*(C'\fR .IX Subsection "Function lib/drives.c:create_drive_file" .PP .Vb 3 \& static struct drive * \& create_drive_file (guestfs_h *g, \& const struct drive_create_data *data) .Ve .PP Create and free the \f(CW\*(C`struct drive\*(C'\fR. .PP Function \f(CW\*(C`lib/drives.c:create_drive_dev_null\*(C'\fR .IX Subsection "Function lib/drives.c:create_drive_dev_null" .PP .Vb 3 \& static struct drive * \& create_drive_dev_null (guestfs_h *g, \& struct drive_create_data *data) .Ve .PP Create the special \fI/dev/null\fR drive. .PP Traditionally you have been able to use \fI/dev/null\fR as a filename, as many times as you like. Ancient KVM (RHEL 5) cannot handle adding \fI/dev/null\fR readonly. qemu 1.2 + virtio-scsi segfaults when you use any zero-sized file including \fI/dev/null\fR. .PP Because of these problems, we replace \fI/dev/null\fR 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. .PP Function \f(CW\*(C`lib/drives.c:drive_to_string\*(C'\fR .IX Subsection "Function lib/drives.c:drive_to_string" .PP .Vb 2 \& static char * \& drive_to_string (guestfs_h *g, const struct drive *drv) .Ve .PP Convert a \f(CW\*(C`struct drive\*(C'\fR to a string for debugging. The caller must free this string. .PP Function \f(CW\*(C`lib/drives.c:add_drive_to_handle_at\*(C'\fR .IX Subsection "Function lib/drives.c:add_drive_to_handle_at" .PP .Vb 2 \& static void \& add_drive_to_handle_at (guestfs_h *g, struct drive *d, size_t drv_index) .Ve .PP Add \f(CW\*(C`struct drive\*(C'\fR to the \f(CW\*(C`g\->drives\*(C'\fR vector at the given index \f(CW\*(C`drv_index\*(C'\fR. If the array isn't large enough it is reallocated. The index must not contain a drive already. .PP Function \f(CW\*(C`lib/drives.c:add_drive_to_handle\*(C'\fR .IX Subsection "Function lib/drives.c:add_drive_to_handle" .PP .Vb 2 \& static void \& add_drive_to_handle (guestfs_h *g, struct drive *d) .Ve .PP Add struct drive to the end of the \f(CW\*(C`g\->drives\*(C'\fR vector in the handle. .PP Function \f(CW\*(C`lib/drives.c:guestfs_int_add_dummy_appliance_drive\*(C'\fR .IX Subsection "Function lib/drives.c:guestfs_int_add_dummy_appliance_drive" .PP .Vb 2 \& void \& guestfs_int_add_dummy_appliance_drive (guestfs_h *g) .Ve .PP Called during launch to add a dummy slot to \f(CW\*(C`g\->drives\*(C'\fR. .PP Function \f(CW\*(C`lib/drives.c:guestfs_int_free_drives\*(C'\fR .IX Subsection "Function lib/drives.c:guestfs_int_free_drives" .PP .Vb 2 \& void \& guestfs_int_free_drives (guestfs_h *g) .Ve .PP Free up all the drives in the handle. .PP Definition \f(CW\*(C`lib/drives.c:VALID_FORMAT\*(C'\fR .IX Subsection "Definition lib/drives.c:VALID_FORMAT" .PP .Vb 1 \& #define VALID_FORMAT .Ve .PP Check string parameter matches regular expression \&\f(CW\*(C`^[\-_[:alnum:]]+$\*(C'\fR (in C locale). .PP Definition \f(CW\*(C`lib/drives.c:VALID_DISK_LABEL\*(C'\fR .IX Subsection "Definition lib/drives.c:VALID_DISK_LABEL" .PP .Vb 1 \& #define VALID_DISK_LABEL .Ve .PP Check the disk label is reasonable. It can't contain certain characters, eg. \f(CW\*(Aq/\*(Aq\fR, \f(CW\*(Aq,\*(Aq\fR. However be stricter here and ensure it's just alphabetic and ≤ 20 characters in length. .PP Definition \f(CW\*(C`lib/drives.c:VALID_HOSTNAME\*(C'\fR .IX Subsection "Definition lib/drives.c:VALID_HOSTNAME" .PP .Vb 1 \& #define VALID_HOSTNAME .Ve .PP Check the server hostname is reasonable. .PP Function \f(CW\*(C`lib/drives.c:valid_port\*(C'\fR .IX Subsection "Function lib/drives.c:valid_port" .PP .Vb 2 \& static int \& valid_port (int port) .Ve .PP Check the port number is reasonable. .PP Function \f(CW\*(C`lib/drives.c:valid_blocksize\*(C'\fR .IX Subsection "Function lib/drives.c:valid_blocksize" .PP .Vb 2 \& static int \& valid_blocksize (int blocksize) .Ve .PP Check the block size is reasonable. It can't be other then 512 or 4096. .PP Function \f(CW\*(C`lib/drives.c:guestfs_int_checkpoint_drives\*(C'\fR .IX Subsection "Function lib/drives.c:guestfs_int_checkpoint_drives" .PP .Vb 2 \& size_t \& guestfs_int_checkpoint_drives (guestfs_h *g) .Ve .PP Checkpoint and roll back drives, so that groups of drives can be added atomically. Only used by "guestfs_add_domain" in \fBguestfs\fR\|(3). .PP Function \f(CW\*(C`lib/drives.c:guestfs_impl_debug_drives\*(C'\fR .IX Subsection "Function lib/drives.c:guestfs_impl_debug_drives" .PP .Vb 2 \& char ** \& guestfs_impl_debug_drives (guestfs_h *g) .Ve .PP Internal function to return the list of drives. .PP \fIFile lib/errors.c\fR .IX Subsection "File lib/errors.c" .PP This file handles errors, and also debug, trace and warning messages. .PP 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. .PP From the point of view of the library source, generally you should use the \f(CW\*(C`error\*(C'\fR or \f(CW\*(C`perrorf\*(C'\fR macros along error paths, eg: .PP .Vb 4 \& if (something_bad) { \& error (g, "something bad happened"); \& return \-1; \& } .Ve .PP Make sure to call the \f(CW\*(C`error\*(C'\fR or \f(CW\*(C`perrorf\*(C'\fR macro exactly once along each error path, since the handle can only store a single error and the previous error will be overwritten. .PP Function \f(CW\*(C`lib/errors.c:guestfs_int_warning\*(C'\fR .IX Subsection "Function lib/errors.c:guestfs_int_warning" .PP .Vb 2 \& void \& guestfs_int_warning (guestfs_h *g, const char *fs, ...) .Ve .PP Print a warning. .PP Code should \fInot\fR call this function directly. Use the \&\f(CW\*(C`warning\ (g,\ fs,\ ...)\*(C'\fR macro. .PP 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. .PP Function \f(CW\*(C`lib/errors.c:guestfs_int_debug\*(C'\fR .IX Subsection "Function lib/errors.c:guestfs_int_debug" .PP .Vb 2 \& void \& guestfs_int_debug (guestfs_h *g, const char *fs, ...) .Ve .PP Print a debug message. .PP Code should \fInot\fR call this function directly. To add debug messages in the library, use the \f(CW\*(C`debug\ (g,\ fs,\ ...)\*(C'\fR macro. The macro checks if \f(CW\*(C`g\->verbose\*(C'\fR is false and avoids the function call, meaning the macro is more efficient. .PP Function \f(CW\*(C`lib/errors.c:guestfs_int_trace\*(C'\fR .IX Subsection "Function lib/errors.c:guestfs_int_trace" .PP .Vb 2 \& void \& guestfs_int_trace (guestfs_h *g, const char *fs, ...) .Ve .PP Print a trace message. .PP Do not call this function. All calls are generated automatically. .PP Function \f(CW\*(C`lib/errors.c:guestfs_int_error_errno\*(C'\fR .IX Subsection "Function lib/errors.c:guestfs_int_error_errno" .PP .Vb 2 \& void \& guestfs_int_error_errno (guestfs_h *g, int errnum, const char *fs, ...) .Ve .PP Set the last error and errno in the handle, and optionally raise the error callback if one is defined. .PP If you don't need to set errno, use the \f(CW\*(C`error\ (g,\ fs,\ ...)\*(C'\fR 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. .PP Function \f(CW\*(C`lib/errors.c:guestfs_int_perrorf\*(C'\fR .IX Subsection "Function lib/errors.c:guestfs_int_perrorf" .PP .Vb 2 \& void \& guestfs_int_perrorf (guestfs_h *g, const char *fs, ...) .Ve .PP Similar to \fBperror\fR\|(3), but it sets the last error in the handle, raises the error callback if one is defined, and supports format strings. .PP You should probably use the \f(CW\*(C`perrorf\ (g,\ fs,\ ...)\*(C'\fR macro instead of calling this directly. .PP Function \f(CW\*(C`lib/errors.c:guestfs_int_launch_failed_error\*(C'\fR .IX Subsection "Function lib/errors.c:guestfs_int_launch_failed_error" .PP .Vb 2 \& void \& guestfs_int_launch_failed_error (guestfs_h *g) .Ve .PP Raise a launch failed error in a standard format. .PP 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. .PP Function \f(CW\*(C`lib/errors.c:guestfs_int_unexpected_close_error\*(C'\fR .IX Subsection "Function lib/errors.c:guestfs_int_unexpected_close_error" .PP .Vb 2 \& void \& guestfs_int_unexpected_close_error (guestfs_h *g) .Ve .PP Raise an error if the appliance unexpectedly crashes after launch. .PP Function \f(CW\*(C`lib/errors.c:guestfs_int_launch_timeout\*(C'\fR .IX Subsection "Function lib/errors.c:guestfs_int_launch_timeout" .PP .Vb 2 \& void \& guestfs_int_launch_timeout (guestfs_h *g) .Ve .PP Raise an error if the appliance hangs during launch. .PP Function \f(CW\*(C`lib/errors.c:guestfs_int_external_command_failed\*(C'\fR .IX Subsection "Function lib/errors.c:guestfs_int_external_command_failed" .PP .Vb 3 \& void \& guestfs_int_external_command_failed (guestfs_h *g, int status, \& const char *cmd_name, const char *extra) .Ve .PP Raise an error if an external command fails. .PP \&\f(CW\*(C`status\*(C'\fR is the status code of the command (eg. returned from \&\fBwaitpid\fR\|(2) or \fBsystem\fR\|(3)). This function turns the status code into an explanatory string. .PP \fIFile lib/events.c\fR .IX Subsection "File lib/events.c" .PP Function \f(CW\*(C`lib/events.c:replace_old_style_event_callback\*(C'\fR .IX Subsection "Function lib/events.c:replace_old_style_event_callback" .PP .Vb 6 \& static void \& replace_old_style_event_callback (guestfs_h *g, \& guestfs_event_callback cb, \& uint64_t event_bitmask, \& void *opaque, \& void *opaque2) .Ve .PP Emulate old-style callback API. .PP There were no event handles, so multiple callbacks per event were not supported. Calling the same \f(CW\*(C`guestfs_set_*_callback\*(C'\fR function would replace the existing event. Calling it with \f(CW\*(C`cb == NULL\*(C'\fR meant that the caller wanted to remove the callback. .PP \fIFile lib/guestfs\-internal\-all.h\fR .IX Subsection "File lib/guestfs-internal-all.h" .PP This header contains definitions which are shared by all parts of libguestfs, ie. the daemon, the library, language bindings and virt tools (ie. \fIall\fR C code). .PP If you need a definition used by only the library, put it in \&\fIlib/guestfs\-internal.h\fR instead. .PP If a definition is used by only a single tool, it should not be in any shared header file at all. .PP \fIFile lib/guestfs\-internal.h\fR .IX Subsection "File lib/guestfs-internal.h" .PP This header file is included in the libguestfs library (\fIlib/\fR) only. .PP See also \fIlib/guestfs\-internal\-all.h\fR. .PP Structure \f(CW\*(C`lib/guestfs\-internal.h:event\*(C'\fR .IX Subsection "Structure lib/guestfs-internal.h:event" .PP .Vb 4 \& struct event { \& uint64_t event_bitmask; \& guestfs_event_callback cb; \& void *opaque; \& \& /* opaque2 is not exposed through the API, but is used internally to \& * emulate the old\-style callback API. \& */ \& void *opaque2; \& }; .Ve .PP This struct is used to maintain a list of events registered against the handle. See \f(CW\*(C`g\->events\*(C'\fR in the handle. .PP Structure \f(CW\*(C`lib/guestfs\-internal.h:drive\*(C'\fR .IX Subsection "Structure lib/guestfs-internal.h:drive" .PP .Vb 3 \& struct drive { \& /* Original source of the drive, eg. file:..., http:... */ \& struct drive_source src; \& \& /* If the drive is readonly, then an overlay [a local file] is \& * created before launch to protect the original drive content, and \& * the filename is stored here. Backends should open this file if \& * it is non\-NULL, else consult the original source above. \& * \& * Note that the overlay is in a backend\-specific format, probably \& * different from the source format. eg. qcow2 \& */ \& char *overlay; \& \& /* Various per\-drive flags. */ \& bool readonly; \& char *name; \& char *disk_label; \& char *cachemode; \& enum discard discard; \& bool copyonread; \& int blocksize; \& }; .Ve .PP There is one \f(CW\*(C`struct drive\*(C'\fR per drive. .PP Structure \f(CW\*(C`lib/guestfs\-internal.h:backend_ops\*(C'\fR .IX Subsection "Structure lib/guestfs-internal.h:backend_ops" .PP .Vb 9 \& struct backend_ops { \& /* Size (in bytes) of the per\-handle data structure needed by this \& * backend. The data pointer is allocated and freed by libguestfs \& * and passed to the functions in the \*(Aqvoid *data\*(Aq parameter. \& * Inside the data structure is opaque to libguestfs. Any strings \& * etc pointed to by it must be freed by the backend during \& * shutdown. \& */ \& size_t data_size; \& \& /* Create a COW overlay on top of a drive. This must be a local \& * file, created in the temporary directory. This is called when \& * the drive is added to the handle. \& */ \& char *(*create_cow_overlay) (guestfs_h *g, void *data, struct drive *drv); \& \& /* Launch and shut down. */ \& int (*launch) (guestfs_h *g, void *data, const char *arg); \& int (*shutdown) (guestfs_h *g, void *data, int check_for_errors); \& \& /* Miscellaneous. */ \& int (*get_pid) (guestfs_h *g, void *data); \& int (*max_disks) (guestfs_h *g, void *data); \& }; .Ve .PP Backend operations. .PP Each backend (eg. libvirt, direct) defines some functions which get run at various places in the handle lifecycle (eg. at launch, shutdown). The backend defines this struct pointing to those functions. .PP Structure \f(CW\*(C`lib/guestfs\-internal.h:connection\*(C'\fR .IX Subsection "Structure lib/guestfs-internal.h:connection" .PP .Vb 2 \& struct connection { \& const struct connection_ops *ops; \& \& /* In the real struct, private data used by each connection module \& * follows here. \& */ \& }; .Ve .PP Connection module. .PP A \f(CW\*(C`connection\*(C'\fR represents the appliance console connection plus the daemon connection. It hides the underlying representation (POSIX sockets, \f(CW\*(C`virStreamPtr\*(C'\fR). .PP Structure \f(CW\*(C`lib/guestfs\-internal.h:cached_feature\*(C'\fR .IX Subsection "Structure lib/guestfs-internal.h:cached_feature" .PP .Vb 4 \& struct cached_feature { \& char *group; \& int result; \& }; .Ve .PP Cache of queried features. .PP Used to cache the appliance features (see \fIlib/available.c\fR). .PP Structure \f(CW\*(C`lib/guestfs\-internal.h:guestfs_h\*(C'\fR .IX Subsection "Structure lib/guestfs-internal.h:guestfs_h" .PP .Vb 3 \& struct guestfs_h { \& struct guestfs_h *next; /* Linked list of open handles. */ \& enum state state; /* See the state machine diagram in guestfs(3)*/ \& \& /* Lock acquired when entering any public guestfs_* function to \& * protect the handle. \& */ \& pthread_mutex_t lock; \& \& /**** Configuration of the handle. ****/ \& bool verbose; /* Debugging. */ \& bool trace; /* Trace calls. */ \& bool autosync; /* Autosync. */ \& bool direct_mode; /* Direct mode. */ \& bool recovery_proc; /* Create a recovery process. */ \& bool enable_network; /* Enable the network. */ \& bool selinux; /* selinux enabled? */ \& bool pgroup; /* Create process group for children? */ \& bool close_on_exit; /* Is this handle on the atexit list? */ \& \& int smp; /* If > 1, \-smp flag passed to hv. */ \& int memsize; /* Size of RAM (megabytes). */ \& \& char *path; /* Path to the appliance. */ \& char *hv; /* Hypervisor (HV) binary. */ \& char *append; /* Append to kernel command line. */ \& \& struct hv_param *hv_params; /* Extra hv parameters. */ \& \& char *program; /* Program name. */ \& char *identifier; /* Handle identifier. */ \& \& /* Array of drives added by add\-drive* APIs. \& * \& * Before launch this list can be empty or contain some drives. \& * \& * During launch, a dummy slot may be added which represents the \& * slot taken up by the appliance drive. \& * \& * During shutdown, this list is deleted, so that each launch gets a \& * fresh set of drives (however callers: don\*(Aqt do this, create a new \& * handle each time). \& * \& * Always use ITER_DRIVES macro to iterate over this list! \& */ \& struct drive **drives; \& size_t nr_drives; \& \& #define ITER_DRIVES(g,i,drv) \e \& for (i = 0; i < (g)\->nr_drives; ++i) \e \& if (((drv) = (g)\->drives[i]) != NULL) \& \& /* Backend. NB: Use guestfs_int_set_backend to change the backend. */ \& char *backend; /* The full string, always non\-NULL. */ \& char *backend_arg; /* Pointer to the argument part. */ \& const struct backend_ops *backend_ops; \& void *backend_data; /* Per\-handle data. */ \& char **backend_settings; /* Backend settings (can be NULL). */ \& \& /**** Runtime information. ****/ \& /* Temporary and cache directories. */ \& /* The actual temporary directory \- this is not created with the \& * handle, you have to call guestfs_int_lazy_make_tmpdir. \& */ \& char *tmpdir; \& char *sockdir; \& /* Environment variables that affect tmpdir/cachedir/sockdir locations. */ \& char *env_tmpdir; /* $TMPDIR (NULL if not set) */ \& char *env_runtimedir; /* $XDG_RUNTIME_DIR (NULL if not set)*/ \& char *int_tmpdir; /* $LIBGUESTFS_TMPDIR or guestfs_set_tmpdir or NULL */ \& char *int_cachedir; /* $LIBGUESTFS_CACHEDIR or guestfs_set_cachedir or NULL */ \& \& /* Error handler, plus stack of old error handlers. */ \& pthread_key_t error_data; \& \& /* Linked list of error_data structures allocated for this handle, \& * plus a mutex to protect the linked list. \& */ \& pthread_mutex_t error_data_list_lock; \& struct error_data *error_data_list; \& \& /* Out of memory error handler. */ \& guestfs_abort_cb abort_cb; \& \& /* Events. */ \& struct event *events; \& size_t nr_events; \& \& /* Private data area. */ \& struct hash_table *pda; \& struct pda_entry *pda_next; \& \& /* User cancelled transfer. Not signal\-atomic, but it doesn\*(Aqt \& * matter for this case because we only care if it is != 0. \& */ \& int user_cancel; \& \& struct timeval launch_t; /* The time that we called guestfs_launch. */ \& \& /* Used by bindtests. */ \& FILE *test_fp; \& \& /* Used to generate unique numbers, eg for temp files. To use this, \& * \*(Aq++g\->unique\*(Aq. Note these are only unique per\-handle, not \& * globally unique. \& */ \& int unique; \& \& /*** Protocol. ***/ \& struct connection *conn; /* Connection to appliance. */ \& int msg_next_serial; \& \& #if HAVE_FUSE \& /**** Used by the mount\-local APIs. ****/ \& char *localmountpoint; \& struct fuse *fuse; /* FUSE handle. */ \& int ml_dir_cache_timeout; /* Directory cache timeout. */ \& Hash_table *lsc_ht, *xac_ht, *rlc_ht; /* Directory cache. */ \& int ml_read_only; /* If mounted read\-only. */ \& int ml_debug_calls; /* Extra debug info on each FUSE call. */ \& #endif \& \& #ifdef HAVE_LIBVIRT \& /* Used by lib/libvirt\-auth.c. */ \& #define NR_CREDENTIAL_TYPES 9 \& unsigned int nr_supported_credentials; \& int supported_credentials[NR_CREDENTIAL_TYPES]; \& const char *saved_libvirt_uri; /* Doesn\*(Aqt need to be freed. */ \& bool wrapper_warning_done; \& unsigned int nr_requested_credentials; \& virConnectCredentialPtr requested_credentials; \& #endif \& \& /* Cached features. */ \& struct cached_feature *features; \& size_t nr_features; \& \& /* Used by lib/info.c. \-1 = not tested or error; else 0 or 1. */ \& int qemu_img_supports_U_option; \& }; .Ve .PP The libguestfs handle. .PP Structure \f(CW\*(C`lib/guestfs\-internal.h:version\*(C'\fR .IX Subsection "Structure lib/guestfs-internal.h:version" .PP .Vb 5 \& struct version { \& int v_major; \& int v_minor; \& int v_micro; \& }; .Ve .PP Used for storing major.minor.micro version numbers. See \fIlib/version.c\fR for more information. .PP \fIFile lib/guid.c\fR .IX Subsection "File lib/guid.c" .PP Function \f(CW\*(C`lib/guid.c:guestfs_int_validate_guid\*(C'\fR .IX Subsection "Function lib/guid.c:guestfs_int_validate_guid" .PP .Vb 2 \& int \& guestfs_int_validate_guid (const char *str) .Ve .PP Check whether a string supposed to contain a GUID actually contains it. It can recognize strings either as \&\f(CW\*(C`{21EC2020\-3AEA\-1069\-A2DD\-08002B30309D}\*(C'\fR or \&\f(CW\*(C`21EC2020\-3AEA\-1069\-A2DD\-08002B30309D\*(C'\fR. .PP \fIFile lib/handle.c\fR .IX Subsection "File lib/handle.c" .PP This file deals with the \f(CW\*(C`guestfs_h\*(C'\fR handle, creating it, closing it, and initializing/setting/getting fields. .PP Function \f(CW\*(C`lib/handle.c:init_libguestfs\*(C'\fR .IX Subsection "Function lib/handle.c:init_libguestfs" .PP .Vb 2 \& static void \& init_libguestfs (void) .Ve .PP 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. .PP Function \f(CW\*(C`lib/handle.c:shutdown_backend\*(C'\fR .IX Subsection "Function lib/handle.c:shutdown_backend" .PP .Vb 2 \& static int \& shutdown_backend (guestfs_h *g, int check_for_errors) .Ve .PP This function is the common path for shutting down the backend qemu process. .PP \&\f(CW\*(C`guestfs_shutdown\*(C'\fR calls \f(CW\*(C`shutdown_backend\*(C'\fR with \&\f(CW\*(C`check_for_errors=1\*(C'\fR. \f(CW\*(C`guestfs_close\*(C'\fR calls \f(CW\*(C`shutdown_backend\*(C'\fR with \f(CW\*(C`check_for_errors=0\*(C'\fR. .PP \&\f(CW\*(C`check_for_errors\*(C'\fR 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. .PP Function \f(CW\*(C`lib/handle.c:close_handles\*(C'\fR .IX Subsection "Function lib/handle.c:close_handles" .PP .Vb 2 \& static void \& close_handles (void) .Ve .PP Close all open handles (called from \fBatexit\fR\|(3)). .PP Function \f(CW\*(C`lib/handle.c:guestfs_int_get_backend_setting_bool\*(C'\fR .IX Subsection "Function lib/handle.c:guestfs_int_get_backend_setting_bool" .PP .Vb 2 \& int \& guestfs_int_get_backend_setting_bool (guestfs_h *g, const char *name) .Ve .PP This is a convenience function, but we might consider exporting it as an API in future. .PP \fIFile lib/info.c\fR .IX Subsection "File lib/info.c" .PP Function \f(CW\*(C`lib/info.c:qemu_img_supports_U_option\*(C'\fR .IX Subsection "Function lib/info.c:qemu_img_supports_U_option" .PP .Vb 2 \& static int \& qemu_img_supports_U_option (guestfs_h *g) .Ve .PP Test if the qemu-img info command supports the \f(CW\*(C`\-U\*(C'\fR option to disable locking. The result is memoized in the handle. .PP Note this option was added in qemu 2.11. We can remove this test when we can assume everyone is using qemu >= 2.11. .PP \fIFile lib/inspect\-icon.c\fR .IX Subsection "File lib/inspect-icon.c" .PP Function \f(CW\*(C`lib/inspect\-icon.c:guestfs_int_download_to_tmp\*(C'\fR .IX Subsection "Function lib/inspect-icon.c:guestfs_int_download_to_tmp" .PP .Vb 4 \& char * \& guestfs_int_download_to_tmp (guestfs_h *g, const char *filename, \& const char *extension, \& uint64_t max_size) .Ve .PP Download a guest file to a local temporary file. .PP The name of the temporary (downloaded) file is returned. The caller must free the pointer, but does \fInot\fR need to delete the temporary file. It will be deleted when the handle is closed. .PP The name of the temporary file is randomly generated, but an extension can be specified using \f(CW\*(C`extension\*(C'\fR (or pass \f(CW\*(C`NULL\*(C'\fR for none). .PP Refuse to download the guest file if it is larger than \f(CW\*(C`max_size\*(C'\fR. On this and other errors, \f(CW\*(C`NULL\*(C'\fR is returned. .PP \fIFile lib/launch\-direct.c\fR .IX Subsection "File lib/launch-direct.c" .PP Implementation of the \f(CW\*(C`direct\*(C'\fR backend. .PP For more details see "BACKENDS" in \fBguestfs\fR\|(3). .PP Function \f(CW\*(C`lib/launch\-direct.c:add_drive_standard_params\*(C'\fR .IX Subsection "Function lib/launch-direct.c:add_drive_standard_params" .PP .Vb 4 \& static int \& add_drive_standard_params (guestfs_h *g, struct backend_direct_data *data, \& struct qemuopts *qopts, \& size_t i, struct drive *drv) .Ve .PP Add the standard elements of the \f(CW\*(C`\-drive\*(C'\fR parameter. .PP Function \f(CW\*(C`lib/launch\-direct.c:add_device_blocksize_params\*(C'\fR .IX Subsection "Function lib/launch-direct.c:add_device_blocksize_params" .PP .Vb 3 \& static int \& add_device_blocksize_params (guestfs_h *g, struct qemuopts *qopts, \& struct drive *drv) .Ve .PP Add the physical_block_size and logical_block_size elements of the \f(CW\*(C`\-device\*(C'\fR parameter. .PP Function \f(CW\*(C`lib/launch\-direct.c:launch_passt\*(C'\fR .IX Subsection "Function lib/launch-direct.c:launch_passt" .PP .Vb 2 \& static int \& launch_passt (guestfs_h *g, long *passt_pid, char (*sockpath)[UNIX_PATH_MAX]) .Ve .PP Launch passt such that it daemonizes. .PP On error, \f(CW\-1\fR is returned; \f(CW\*(C`passt_pid\*(C'\fR and \f(CW\*(C`sockpath\*(C'\fR are not modified. .PP On success, \f(CW0\fR is returned. \f(CW\*(C`passt_pid\*(C'\fR contains the PID of the passt background process. \f(CW\*(C`sockpath\*(C'\fR contains the pathname of the unix domain socket where passt will accept a single connection. .PP \fIFile lib/launch\-libvirt.c\fR .IX Subsection "File lib/launch-libvirt.c" .PP Function \f(CW\*(C`lib/launch\-libvirt.c:get_source_format_or_autodetect\*(C'\fR .IX Subsection "Function lib/launch-libvirt.c:get_source_format_or_autodetect" .PP .Vb 2 \& static char * \& get_source_format_or_autodetect (guestfs_h *g, struct drive *drv) .Ve .PP Return \f(CW\*(C`drv\->src.format\*(C'\fR, but if it is \f(CW\*(C`NULL\*(C'\fR, autodetect the format. .PP libvirt has disabled the feature of detecting the disk format, unless the administrator sets \f(CW\*(C`allow_disk_format_probing=1\*(C'\fR in \&\fI/etc/libvirt/qemu.conf\fR. There is no way to detect if this option is set, so we have to do format detection here using \&\f(CW\*(C`qemu\-img\*(C'\fR and pass that to libvirt. .PP 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. .PP Caller must free the returned string. On error this function sets the error in the handle and returns \f(CW\*(C`NULL\*(C'\fR. .PP Function \f(CW\*(C`lib/launch\-libvirt.c:make_qcow2_overlay\*(C'\fR .IX Subsection "Function lib/launch-libvirt.c:make_qcow2_overlay" .PP .Vb 3 \& static char * \& make_qcow2_overlay (guestfs_h *g, const char *backing_drive, \& const char *format) .Ve .PP Create a qcow2 format overlay, with the given \f(CW\*(C`backing_drive\*(C'\fR (file). The \f(CW\*(C`format\*(C'\fR parameter is the backing file format. The \f(CW\*(C`format\*(C'\fR parameter can be NULL, in this case the backing format will be determined automatically. This is used to create the appliance overlay, and also for read-only drives. .PP \fIFile lib/launch.c\fR .IX Subsection "File lib/launch.c" .PP This file implements "guestfs_launch" in \fBguestfs\fR\|(3). .PP Most of the work is done by the backends (see "BACKEND" in \fBguestfs\fR\|(3)), which are implemented in \&\fIlib/launch\-direct.c\fR, \fIlib/launch\-libvirt.c\fR etc, so this file mostly passes calls through to the current backend. .PP Function \f(CW\*(C`lib/launch.c:guestfs_int_launch_send_progress\*(C'\fR .IX Subsection "Function lib/launch.c:guestfs_int_launch_send_progress" .PP .Vb 2 \& void \& guestfs_int_launch_send_progress (guestfs_h *g, int perdozen) .Ve .PP This function sends a launch progress message. .PP Launching the appliance generates approximate progress messages. Currently these are defined as follows: .PP .Vb 5 \& 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 .Ve .PP Notes: .IP 1. 4 This is not a documented ABI and the behaviour may be changed or removed in future. .IP 2. 4 Messages are only sent if more than 5 seconds has elapsed since the launch clock started. .IP 3. 4 There is a hack in \fIlib/proto.c\fR to make this work. .PP Function \f(CW\*(C`lib/launch.c:guestfs_int_timeval_diff\*(C'\fR .IX Subsection "Function lib/launch.c:guestfs_int_timeval_diff" .PP .Vb 2 \& int64_t \& guestfs_int_timeval_diff (const struct timeval *x, const struct timeval *y) .Ve .PP Compute \f(CW\*(C`y \- x\*(C'\fR and return the result in milliseconds. .PP Approximately the same as this code: http://www.mpp.mpg.de/~huber/util/timevaldiff.c .PP Function \f(CW\*(C`lib/launch.c:guestfs_int_unblock_sigterm\*(C'\fR .IX Subsection "Function lib/launch.c:guestfs_int_unblock_sigterm" .PP .Vb 2 \& void \& guestfs_int_unblock_sigterm (void) .Ve .PP Unblock the \f(CW\*(C`SIGTERM\*(C'\fR signal. Call this after \fBfork\fR\|(2) so that the parent process can send \f(CW\*(C`SIGTERM\*(C'\fR to the child process in case \&\f(CW\*(C`SIGTERM\*(C'\fR is blocked. See https://bugzilla.redhat.com/1460338. .PP Function \f(CW\*(C`lib/launch.c:guestfs_impl_max_disks\*(C'\fR .IX Subsection "Function lib/launch.c:guestfs_impl_max_disks" .PP .Vb 2 \& int \& guestfs_impl_max_disks (guestfs_h *g) .Ve .PP Returns the maximum number of disks allowed to be added to the backend (backend dependent). .PP Function \f(CW\*(C`lib/launch.c:guestfs_impl_wait_ready\*(C'\fR .IX Subsection "Function lib/launch.c:guestfs_impl_wait_ready" .PP .Vb 2 \& int \& guestfs_impl_wait_ready (guestfs_h *g) .Ve .PP Implementation of "guestfs_wait_ready" in \fBguestfs\fR\|(3). You had to call this function after launch in versions ≤ 1.0.70, but it is now an (almost) no-op. .PP Function \f(CW\*(C`lib/launch.c:guestfs_int_register_backend\*(C'\fR .IX Subsection "Function lib/launch.c:guestfs_int_register_backend" .PP .Vb 2 \& void \& guestfs_int_register_backend (const char *name, const struct backend_ops *ops) .Ve .PP When the library is loaded, each backend calls this function to register itself in a global list. .PP Function \f(CW\*(C`lib/launch.c:guestfs_int_set_backend\*(C'\fR .IX Subsection "Function lib/launch.c:guestfs_int_set_backend" .PP .Vb 2 \& int \& guestfs_int_set_backend (guestfs_h *g, const char *method) .Ve .PP Implementation of "guestfs_set_backend" in \fBguestfs\fR\|(3). .IP \(bu 4 Callers must ensure this is only called in the config state. .IP \(bu 4 This shouldn't call \f(CW\*(C`error\*(C'\fR since it may be called early in handle initialization. It can return an error code however. .PP Function \f(CW\*(C`lib/launch.c:guestfs_int_passt_runnable\*(C'\fR .IX Subsection "Function lib/launch.c:guestfs_int_passt_runnable" .PP .Vb 2 \& bool \& guestfs_int_passt_runnable (guestfs_h *g) .Ve .PP Return \f(CW\*(C`true\*(C'\fR if we can call \f(CW\*(C`passt\ \-\-help\*(C'\fR, and it exits with status \&\f(CW0\fR or \f(CW1\fR. .PP (At least \f(CW\*(C`passt\-0^20230222.g4ddbcb9\-2.el9_2.x86_64\*(C'\fR terminates with status \&\f(CW1\fR in response to "\-\-help", which is arguably wrong, and potentially subject to change, but it doesn't really matter.) .PP \fIFile lib/private\-data.c\fR .IX Subsection "File lib/private-data.c" .PP Implement a private data area where libguestfs C API users can attach arbitrary pieces of data to a \f(CW\*(C`guestfs_h\*(C'\fR handle. .PP For more information see "PRIVATE DATA AREA" in \fBguestfs\fR\|(3). .PP Language bindings do not generally expose this, largely because in non-C languages it is easy to associate data with handles in other ways (using hash tables or maps). .PP Structure \f(CW\*(C`lib/private\-data.c:pda_entry\*(C'\fR .IX Subsection "Structure lib/private-data.c:pda_entry" .PP .Vb 4 \& struct pda_entry { \& char *key; /* key */ \& void *data; /* opaque user data pointer */ \& }; .Ve .PP The private data area is internally stored as a gnulib hash table containing \f(CW\*(C`pda_entry\*(C'\fR structures. .PP Note the private data area is allocated lazily, since the vast majority of callers will never use it. This means \f(CW\*(C`g\->pda\*(C'\fR is likely to be \f(CW\*(C`NULL\*(C'\fR. .PP \fIFile lib/proto.c\fR .IX Subsection "File lib/proto.c" .PP 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 (\fIlib/actions\-*.c\fR). There are five different cases to consider: .IP 1. 4 A non-daemon function (eg. "guestfs_set_verbose" in \fBguestfs\fR\|(3)). There is no RPC involved at all, it's all handled inside the library. .IP 2. 4 A simple RPC (eg. "guestfs_mount" in \fBguestfs\fR\|(3)). We write the request, then read the reply. The sequence of calls is: .Sp .Vb 2 \& guestfs_int_send \& guestfs_int_recv .Ve .IP 3. 4 An RPC with \f(CW\*(C`FileIn\*(C'\fR parameters (eg. "guestfs_upload" in \fBguestfs\fR\|(3)). We write the request, then write the file(s), then read the reply. The sequence of calls is: .Sp .Vb 3 \& guestfs_int_send \& guestfs_int_send_file (possibly multiple times) \& guestfs_int_recv .Ve .IP 4. 4 An RPC with \f(CW\*(C`FileOut\*(C'\fR parameters (eg. "guestfs_download" in \fBguestfs\fR\|(3)). We write the request, then read the reply, then read the file(s). The sequence of calls is: .Sp .Vb 3 \& guestfs_int_send \& guestfs_int_recv \& guestfs_int_recv_file (possibly multiple times) .Ve .IP 5. 4 Both \f(CW\*(C`FileIn\*(C'\fR and \f(CW\*(C`FileOut\*(C'\fR parameters. There are no calls like this in the current API, but they would be implemented as a combination of cases 3 and 4. .PP All read/write/etc operations are performed using the current connection module (\f(CW\*(C`g\->conn\*(C'\fR). During operations the connection module transparently handles log messages that appear on the console. .PP Function \f(CW\*(C`lib/proto.c:child_cleanup\*(C'\fR .IX Subsection "Function lib/proto.c:child_cleanup" .PP .Vb 2 \& static void \& child_cleanup (guestfs_h *g) .Ve .PP This is called if we detect EOF, ie. qemu died. .PP Function \f(CW\*(C`lib/proto.c:guestfs_int_progress_message_callback\*(C'\fR .IX Subsection "Function lib/proto.c:guestfs_int_progress_message_callback" .PP .Vb 3 \& void \& guestfs_int_progress_message_callback (guestfs_h *g, \& const guestfs_progress *message) .Ve .PP Convenient wrapper to generate a progress message callback. .PP Function \f(CW\*(C`lib/proto.c:guestfs_int_log_message_callback\*(C'\fR .IX Subsection "Function lib/proto.c:guestfs_int_log_message_callback" .PP .Vb 2 \& void \& guestfs_int_log_message_callback (guestfs_h *g, const char *buf, size_t len) .Ve .PP Connection modules call us back here when they get a log message. .PP Function \f(CW\*(C`lib/proto.c:check_daemon_socket\*(C'\fR .IX Subsection "Function lib/proto.c:check_daemon_socket" .PP .Vb 2 \& static ssize_t \& check_daemon_socket (guestfs_h *g) .Ve .PP Before writing to the daemon socket, check the read side of the daemon socket for any of these conditions: .IP error 4 .IX Item "error" return \-1 .IP "daemon cancellation message" 4 .IX Item "daemon cancellation message" return \-2 .IP "progress message" 4 .IX Item "progress message" handle it here .IP "end of input or appliance exited unexpectedly" 4 .IX Item "end of input or appliance exited unexpectedly" return 0 .IP "anything else" 4 .IX Item "anything else" return 1 .PP Function \f(CW\*(C`lib/proto.c:guestfs_int_send_file\*(C'\fR .IX Subsection "Function lib/proto.c:guestfs_int_send_file" .PP .Vb 2 \& int \& guestfs_int_send_file (guestfs_h *g, const char *filename) .Ve .PP Send a file. .PP Returns \f(CW0\fR on success, \f(CW\-1\fR for error, \f(CW\-2\fR if the daemon cancelled (we must read the error message). .PP Function \f(CW\*(C`lib/proto.c:send_file_data\*(C'\fR .IX Subsection "Function lib/proto.c:send_file_data" .PP .Vb 2 \& static int \& send_file_data (guestfs_h *g, const char *buf, size_t len) .Ve .PP Send a chunk of file data. .PP Function \f(CW\*(C`lib/proto.c:send_file_cancellation\*(C'\fR .IX Subsection "Function lib/proto.c:send_file_cancellation" .PP .Vb 2 \& static int \& send_file_cancellation (guestfs_h *g) .Ve .PP Send a cancellation message. .PP Function \f(CW\*(C`lib/proto.c:send_file_complete\*(C'\fR .IX Subsection "Function lib/proto.c:send_file_complete" .PP .Vb 2 \& static int \& send_file_complete (guestfs_h *g) .Ve .PP Send a file complete chunk. .PP Function \f(CW\*(C`lib/proto.c:recv_from_daemon\*(C'\fR .IX Subsection "Function lib/proto.c:recv_from_daemon" .PP .Vb 2 \& static int \& recv_from_daemon (guestfs_h *g, uint32_t *size_rtn, void **buf_rtn) .Ve .PP This function reads a single message, file chunk, launch flag or cancellation flag from the daemon. If something was read, it returns \f(CW0\fR, otherwise \f(CW\-1\fR. .PP Both \f(CW\*(C`size_rtn\*(C'\fR and \f(CW\*(C`buf_rtn\*(C'\fR must be passed by the caller as non-NULL. .PP \&\f(CW*size_rtn\fR returns the size of the returned message or it may be \&\f(CW\*(C`GUESTFS_LAUNCH_FLAG\*(C'\fR or \f(CW\*(C`GUESTFS_CANCEL_FLAG\*(C'\fR. .PP \&\f(CW*buf_rtn\fR is returned containing the message (if any) or will be set to \f(CW\*(C`NULL\*(C'\fR. \f(CW*buf_rtn\fR must be freed by the caller. .PP This checks for EOF (appliance died) and passes that up through the child_cleanup function above. .PP Log message, progress messages are handled transparently here. .PP Function \f(CW\*(C`lib/proto.c:guestfs_int_recv\*(C'\fR .IX Subsection "Function lib/proto.c:guestfs_int_recv" .PP .Vb 5 \& int \& guestfs_int_recv (guestfs_h *g, const char *fn, \& guestfs_message_header *hdr, \& guestfs_message_error *err, \& xdrproc_t xdrp, char *ret) .Ve .PP Receive a reply. .PP Function \f(CW\*(C`lib/proto.c:guestfs_int_recv_discard\*(C'\fR .IX Subsection "Function lib/proto.c:guestfs_int_recv_discard" .PP .Vb 2 \& int \& guestfs_int_recv_discard (guestfs_h *g, const char *fn) .Ve .PP Same as \f(CW\*(C`guestfs_int_recv\*(C'\fR, but it discards the reply message. .PP Notes (XXX): .IP \(bu 4 This returns an int, but all current callers ignore it. .IP \(bu 4 The error string may end up being set twice on error paths. .PP Function \f(CW\*(C`lib/proto.c:guestfs_int_recv_file\*(C'\fR .IX Subsection "Function lib/proto.c:guestfs_int_recv_file" .PP .Vb 2 \& int \& guestfs_int_recv_file (guestfs_h *g, const char *filename) .Ve .PP Returns \f(CW\-1\fR = error, \f(CW0\fR = EOF, \f(CW\*(C`>0\*(C'\fR = more data .PP Function \f(CW\*(C`lib/proto.c:receive_file_data\*(C'\fR .IX Subsection "Function lib/proto.c:receive_file_data" .PP .Vb 2 \& static ssize_t \& receive_file_data (guestfs_h *g, void **buf_r) .Ve .PP Receive a chunk of file data. .PP Returns \f(CW\-1\fR = error, \f(CW0\fR = EOF, \f(CW\*(C`>0\*(C'\fR = more data .PP \fIFile lib/qemu.c\fR .IX Subsection "File lib/qemu.c" .PP Functions to handle qemu versions and features. .PP Function \f(CW\*(C`lib/qemu.c:guestfs_int_test_qemu\*(C'\fR .IX Subsection "Function lib/qemu.c:guestfs_int_test_qemu" .PP .Vb 2 \& struct qemu_data * \& guestfs_int_test_qemu (guestfs_h *g) .Ve .PP Test that the qemu binary (or wrapper) runs, and do \f(CW\*(C`qemu \-help\*(C'\fR and other commands so we can find out the version of qemu and what options this qemu supports. .PP This caches the results in the cachedir so that as long as the qemu binary does not change, calling this is effectively free. .PP Function \f(CW\*(C`lib/qemu.c:cache_filename\*(C'\fR .IX Subsection "Function lib/qemu.c:cache_filename" .PP .Vb 3 \& static char * \& cache_filename (guestfs_h *g, const char *cachedir, \& const struct stat *statbuf, const char *suffix) .Ve .PP Generate the filenames, for the stat file and the other cache files. .PP By including the size and mtime in the filename we also ensure that the same user can use multiple versions of qemu without conflicts. .PP Function \f(CW\*(C`lib/qemu.c:parse_qemu_version\*(C'\fR .IX Subsection "Function lib/qemu.c:parse_qemu_version" .PP .Vb 3 \& static void \& parse_qemu_version (guestfs_h *g, const char *qemu_help, \& struct version *qemu_version) .Ve .PP Parse the first line of \f(CW\*(C`qemu_help\*(C'\fR into the major and minor version of qemu, but don't fail if parsing is not possible. .PP Function \f(CW\*(C`lib/qemu.c:parse_json\*(C'\fR .IX Subsection "Function lib/qemu.c:parse_json" .PP .Vb 2 \& static void \& parse_json (guestfs_h *g, const char *json, json_t **treep) .Ve .PP Parse the json output from QMP. But don't fail if parsing is not possible. .PP Function \f(CW\*(C`lib/qemu.c:parse_has_kvm\*(C'\fR .IX Subsection "Function lib/qemu.c:parse_has_kvm" .PP .Vb 2 \& static void \& parse_has_kvm (guestfs_h *g, const char *json, bool *ret) .Ve .PP Parse the json output from QMP query-kvm to find out if KVM is enabled on this machine. Don't fail if parsing is not possible, assume KVM is available. .PP The JSON output looks like: {"return": {"enabled": true, "present": true}} .PP Function \f(CW\*(C`lib/qemu.c:generic_read_cache\*(C'\fR .IX Subsection "Function lib/qemu.c:generic_read_cache" .PP .Vb 2 \& static int \& generic_read_cache (guestfs_h *g, const char *filename, char **strp) .Ve .PP Generic functions for reading and writing the cache files, used where we are just reading and writing plain text strings. .PP Function \f(CW\*(C`lib/qemu.c:generic_qmp_test\*(C'\fR .IX Subsection "Function lib/qemu.c:generic_qmp_test" .PP .Vb 4 \& static int \& generic_qmp_test (guestfs_h *g, struct qemu_data *data, \& const char *qmp_command, \& char **outp) .Ve .PP Run a generic QMP test on the QEMU binary. .PP Function \f(CW\*(C`lib/qemu.c:guestfs_int_qemu_version\*(C'\fR .IX Subsection "Function lib/qemu.c:guestfs_int_qemu_version" .PP .Vb 2 \& struct version \& guestfs_int_qemu_version (guestfs_h *g, struct qemu_data *data) .Ve .PP Return the parsed version of qemu. .PP Function \f(CW\*(C`lib/qemu.c:guestfs_int_qemu_supports\*(C'\fR .IX Subsection "Function lib/qemu.c:guestfs_int_qemu_supports" .PP .Vb 3 \& int \& guestfs_int_qemu_supports (guestfs_h *g, const struct qemu_data *data, \& const char *option) .Ve .PP Test if option is supported by qemu command line (just by grepping the help text). .PP Function \f(CW\*(C`lib/qemu.c:guestfs_int_qemu_supports_device\*(C'\fR .IX Subsection "Function lib/qemu.c:guestfs_int_qemu_supports_device" .PP .Vb 4 \& int \& guestfs_int_qemu_supports_device (guestfs_h *g, \& const struct qemu_data *data, \& const char *device_name) .Ve .PP Test if device is supported by qemu (currently just greps the \&\f(CW\*(C`qemu \-device ?\*(C'\fR output). .PP Function \f(CW\*(C`lib/qemu.c:guestfs_int_qemu_mandatory_locking\*(C'\fR .IX Subsection "Function lib/qemu.c:guestfs_int_qemu_mandatory_locking" .PP .Vb 3 \& int \& guestfs_int_qemu_mandatory_locking (guestfs_h *g, \& const struct qemu_data *data) .Ve .PP Test if the qemu binary uses mandatory file locking, added in QEMU >= 2.10 (but sometimes disabled). .PP Function \f(CW\*(C`lib/qemu.c:guestfs_int_qemu_escape_param\*(C'\fR .IX Subsection "Function lib/qemu.c:guestfs_int_qemu_escape_param" .PP .Vb 2 \& char * \& guestfs_int_qemu_escape_param (guestfs_h *g, const char *param) .Ve .PP Escape a qemu parameter. .PP Every \f(CW\*(C`,\*(C'\fR becomes \f(CW\*(C`,,\*(C'\fR. The caller must free the returned string. .PP XXX This functionality is now only used when constructing a qemu-img command in \fIlib/create.c\fR. We should extend the qemuopts library to cover this use case. .PP Function \f(CW\*(C`lib/qemu.c:guestfs_int_drive_source_qemu_param\*(C'\fR .IX Subsection "Function lib/qemu.c:guestfs_int_drive_source_qemu_param" .PP .Vb 3 \& char * \& guestfs_int_drive_source_qemu_param (guestfs_h *g, \& const struct drive_source *src) .Ve .PP Useful function to format a drive + protocol for qemu. .PP Note that the qemu parameter is the bit after \f(CW"file="\fR. It is not escaped here, but would usually be escaped if passed to qemu as part of a full \-drive parameter (but not for \fBqemu\-img\fR\|(1)). .PP Function \f(CW\*(C`lib/qemu.c:guestfs_int_discard_possible\*(C'\fR .IX Subsection "Function lib/qemu.c:guestfs_int_discard_possible" .PP .Vb 3 \& bool \& guestfs_int_discard_possible (guestfs_h *g, struct drive *drv, \& const struct version *qemu_version) .Ve .PP Test if discard is both supported by qemu AND possible with the underlying file or device. This returns \f(CW1\fR if discard is possible. It returns \f(CW0\fR if not possible and sets the error to the reason why. .PP This function is called when the user set \f(CW\*(C`discard == "enable"\*(C'\fR. .PP Function \f(CW\*(C`lib/qemu.c:guestfs_int_free_qemu_data\*(C'\fR .IX Subsection "Function lib/qemu.c:guestfs_int_free_qemu_data" .PP .Vb 2 \& void \& guestfs_int_free_qemu_data (struct qemu_data *data) .Ve .PP Free the \f(CW\*(C`struct qemu_data\*(C'\fR. .PP \fIFile lib/rescue.c\fR .IX Subsection "File lib/rescue.c" .PP Support for \fBvirt\-rescue\fR\|(1). .PP \fIFile lib/stringsbuf.c\fR .IX Subsection "File lib/stringsbuf.c" .PP An expandable NULL-terminated vector of strings (like \f(CW\*(C`argv\*(C'\fR). .PP Use the \f(CW\*(C`DECLARE_STRINGSBUF\*(C'\fR macro to declare the stringsbuf. .PP Note: Don't confuse this with stringsbuf in the daemon which is a different type with different methods. .PP Function \f(CW\*(C`lib/stringsbuf.c:guestfs_int_add_string_nodup\*(C'\fR .IX Subsection "Function lib/stringsbuf.c:guestfs_int_add_string_nodup" .PP .Vb 2 \& void \& guestfs_int_add_string_nodup (guestfs_h *g, struct stringsbuf *sb, char *str) .Ve .PP Add a string to the end of the list. .PP This doesn't call \fBstrdup\fR\|(3) on the string, so the string itself is stored inside the vector. .PP Function \f(CW\*(C`lib/stringsbuf.c:guestfs_int_add_string\*(C'\fR .IX Subsection "Function lib/stringsbuf.c:guestfs_int_add_string" .PP .Vb 2 \& void \& guestfs_int_add_string (guestfs_h *g, struct stringsbuf *sb, const char *str) .Ve .PP Add a string to the end of the list. .PP This makes a copy of the string. .PP Function \f(CW\*(C`lib/stringsbuf.c:guestfs_int_add_sprintf\*(C'\fR .IX Subsection "Function lib/stringsbuf.c:guestfs_int_add_sprintf" .PP .Vb 3 \& void \& guestfs_int_add_sprintf (guestfs_h *g, struct stringsbuf *sb, \& const char *fs, ...) .Ve .PP Add a string to the end of the list. .PP Uses an sprintf-like format string when creating the string. .PP Function \f(CW\*(C`lib/stringsbuf.c:guestfs_int_end_stringsbuf\*(C'\fR .IX Subsection "Function lib/stringsbuf.c:guestfs_int_end_stringsbuf" .PP .Vb 2 \& void \& guestfs_int_end_stringsbuf (guestfs_h *g, struct stringsbuf *sb) .Ve .PP Finish the string buffer. .PP This adds the terminating NULL to the end of the vector. .PP Function \f(CW\*(C`lib/stringsbuf.c:guestfs_int_free_stringsbuf\*(C'\fR .IX Subsection "Function lib/stringsbuf.c:guestfs_int_free_stringsbuf" .PP .Vb 2 \& void \& guestfs_int_free_stringsbuf (struct stringsbuf *sb) .Ve .PP Free the string buffer and the strings. .PP \fIFile lib/tmpdirs.c\fR .IX Subsection "File lib/tmpdirs.c" .PP Handle temporary directories. .PP Function \f(CW\*(C`lib/tmpdirs.c:set_abs_path\*(C'\fR .IX Subsection "Function lib/tmpdirs.c:set_abs_path" .PP .Vb 3 \& static int \& set_abs_path (guestfs_h *g, const char *ctxstr, \& const char *tmpdir, char **tmpdir_ret) .Ve .PP 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). .PP The \f(CW\*(C`ctxstr\*(C'\fR parameter is a string displayed in error messages giving the context of the operation (eg. name of environment variable being used, or API function being called). .PP Function \f(CW\*(C`lib/tmpdirs.c:guestfs_impl_get_tmpdir\*(C'\fR .IX Subsection "Function lib/tmpdirs.c:guestfs_impl_get_tmpdir" .PP .Vb 2 \& char * \& guestfs_impl_get_tmpdir (guestfs_h *g) .Ve .PP Implements the \f(CW\*(C`guestfs_get_tmpdir\*(C'\fR API. .PP Note this actually calculates the tmpdir, so it never returns \&\f(CW\*(C`NULL\*(C'\fR. .PP Function \f(CW\*(C`lib/tmpdirs.c:guestfs_impl_get_cachedir\*(C'\fR .IX Subsection "Function lib/tmpdirs.c:guestfs_impl_get_cachedir" .PP .Vb 2 \& char * \& guestfs_impl_get_cachedir (guestfs_h *g) .Ve .PP Implements the \f(CW\*(C`guestfs_get_cachedir\*(C'\fR API. .PP Note this actually calculates the cachedir, so it never returns \f(CW\*(C`NULL\*(C'\fR. .PP Function \f(CW\*(C`lib/tmpdirs.c:guestfs_impl_get_sockdir\*(C'\fR .IX Subsection "Function lib/tmpdirs.c:guestfs_impl_get_sockdir" .PP .Vb 2 \& char * \& guestfs_impl_get_sockdir (guestfs_h *g) .Ve .PP Implements the \f(CW\*(C`guestfs_get_sockdir\*(C'\fR API. .PP Note this actually calculates the sockdir, so it never returns \&\f(CW\*(C`NULL\*(C'\fR. .PP Function \f(CW\*(C`lib/tmpdirs.c:guestfs_int_lazy_make_tmpdir\*(C'\fR .IX Subsection "Function lib/tmpdirs.c:guestfs_int_lazy_make_tmpdir" .PP .Vb 2 \& int \& guestfs_int_lazy_make_tmpdir (guestfs_h *g) .Ve .PP The \f(CW\*(C`g\->tmpdir\*(C'\fR (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. .PP Function \f(CW\*(C`lib/tmpdirs.c:guestfs_int_make_temp_path\*(C'\fR .IX Subsection "Function lib/tmpdirs.c:guestfs_int_make_temp_path" .PP .Vb 3 \& char * \& guestfs_int_make_temp_path (guestfs_h *g, \& const char *name, const char *extension) .Ve .PP Generate unique temporary paths for temporary files. .PP Returns a unique path or NULL on error. .PP Function \f(CW\*(C`lib/tmpdirs.c:guestfs_int_create_socketname\*(C'\fR .IX Subsection "Function lib/tmpdirs.c:guestfs_int_create_socketname" .PP .Vb 3 \& int \& guestfs_int_create_socketname (guestfs_h *g, const char *filename, \& char (*sockpath)[UNIX_PATH_MAX]) .Ve .PP Create the path for a socket with the selected filename in the sockdir. .PP Function \f(CW\*(C`lib/tmpdirs.c:guestfs_int_make_pid_path\*(C'\fR .IX Subsection "Function lib/tmpdirs.c:guestfs_int_make_pid_path" .PP .Vb 2 \& char * \& guestfs_int_make_pid_path (guestfs_h *g, const char *name) .Ve .PP Generate unique paths for PID files. .PP Returns a unique path or NULL on error. On success, the pathname points under sockdir and not tmpdir; daemons that write PID files after dropping privileges may not have access to tmpdir. .PP Function \f(CW\*(C`lib/tmpdirs.c:guestfs_int_lazy_make_supermin_appliance_dir\*(C'\fR .IX Subsection "Function lib/tmpdirs.c:guestfs_int_lazy_make_supermin_appliance_dir" .PP .Vb 2 \& char * \& guestfs_int_lazy_make_supermin_appliance_dir (guestfs_h *g) .Ve .PP Create the supermin appliance directory under cachedir, if it does not exist. .PP Sanity-check that the permissions on the cachedir are safe, in case it has been pre-created maliciously or tampered with. .PP Returns the directory name which the caller must free. .PP Function \f(CW\*(C`lib/tmpdirs.c:guestfs_int_recursive_remove_dir\*(C'\fR .IX Subsection "Function lib/tmpdirs.c:guestfs_int_recursive_remove_dir" .PP .Vb 2 \& void \& guestfs_int_recursive_remove_dir (guestfs_h *g, const char *dir) .Ve .PP 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). .PP This is implemented using \f(CW\*(C`rm \-rf\*(C'\fR because that's simpler and safer. .PP \fIFile lib/umask.c\fR .IX Subsection "File lib/umask.c" .PP Return current umask in a thread-safe way. .PP glibc documents, but does not actually implement, a "\fBgetumask\fR\|(3)" call. .PP We use \f(CW\*(C`Umask\*(C'\fR from \fI/proc/self/status\fR 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. .PP Thanks to: Josh Stone, Jiri Jaburek, Eric Blake. .PP Function \f(CW\*(C`lib/umask.c:guestfs_int_getumask\*(C'\fR .IX Subsection "Function lib/umask.c:guestfs_int_getumask" .PP .Vb 2 \& int \& guestfs_int_getumask (guestfs_h *g) .Ve .PP Returns the current process's umask. On failure, returns \f(CW\-1\fR and sets the error in the guestfs handle. .PP Function \f(CW\*(C`lib/umask.c:get_umask_from_proc\*(C'\fR .IX Subsection "Function lib/umask.c:get_umask_from_proc" .PP .Vb 2 \& static int \& get_umask_from_proc (guestfs_h *g) .Ve .PP For Linux ≥ 4.7 get the umask from \fI/proc/self/status\fR. .PP On failure this returns \f(CW\-1\fR. However if we could not open the \&\fI/proc\fR file or find the \f(CW\*(C`Umask\*(C'\fR entry in it, return \f(CW\-2\fR which causes the fallback path to run. .PP Function \f(CW\*(C`lib/umask.c:get_umask_from_fork\*(C'\fR .IX Subsection "Function lib/umask.c:get_umask_from_fork" .PP .Vb 2 \& static int \& get_umask_from_fork (guestfs_h *g) .Ve .PP Fallback method of getting the umask using fork. .PP \fIFile lib/unit\-tests.c\fR .IX Subsection "File lib/unit-tests.c" .PP Unit tests of internal functions. .PP These tests may use a libguestfs handle, but must not launch the handle. Also, avoid long-running tests. .PP Function \f(CW\*(C`lib/unit\-tests.c:test_split\*(C'\fR .IX Subsection "Function lib/unit-tests.c:test_split" .PP .Vb 2 \& static void \& test_split (void) .Ve .PP Test \f(CW\*(C`guestfs_int_split_string\*(C'\fR. .PP Function \f(CW\*(C`lib/unit\-tests.c:test_concat\*(C'\fR .IX Subsection "Function lib/unit-tests.c:test_concat" .PP .Vb 2 \& static void \& test_concat (void) .Ve .PP Test \f(CW\*(C`guestfs_int_concat_strings\*(C'\fR. .PP Function \f(CW\*(C`lib/unit\-tests.c:test_join\*(C'\fR .IX Subsection "Function lib/unit-tests.c:test_join" .PP .Vb 2 \& static void \& test_join (void) .Ve .PP Test \f(CW\*(C`guestfs_int_join_strings\*(C'\fR. .PP Function \f(CW\*(C`lib/unit\-tests.c:test_validate_guid\*(C'\fR .IX Subsection "Function lib/unit-tests.c:test_validate_guid" .PP .Vb 2 \& static void \& test_validate_guid (void) .Ve .PP Test \f(CW\*(C`guestfs_int_validate_guid\*(C'\fR. .PP Function \f(CW\*(C`lib/unit\-tests.c:test_drive_name\*(C'\fR .IX Subsection "Function lib/unit-tests.c:test_drive_name" .PP .Vb 2 \& static void \& test_drive_name (void) .Ve .PP Test \f(CW\*(C`guestfs_int_drive_name\*(C'\fR. .PP Function \f(CW\*(C`lib/unit\-tests.c:test_drive_index\*(C'\fR .IX Subsection "Function lib/unit-tests.c:test_drive_index" .PP .Vb 2 \& static void \& test_drive_index (void) .Ve .PP Test \f(CW\*(C`guestfs_int_drive_index\*(C'\fR. .PP Function \f(CW\*(C`lib/unit\-tests.c:test_getumask\*(C'\fR .IX Subsection "Function lib/unit-tests.c:test_getumask" .PP .Vb 2 \& static void \& test_getumask (void) .Ve .PP Test \f(CW\*(C`guestfs_int_getumask\*(C'\fR. .PP Function \f(CW\*(C`lib/unit\-tests.c:test_command\*(C'\fR .IX Subsection "Function lib/unit-tests.c:test_command" .PP .Vb 2 \& static void \& test_command (void) .Ve .PP Test \f(CW\*(C`guestfs_int_new_command\*(C'\fR etc. .PP XXX These tests could be made much more thorough. So far we simply test that it's not obviously broken. .PP Function \f(CW\*(C`lib/unit\-tests.c:test_qemu_escape_param\*(C'\fR .IX Subsection "Function lib/unit-tests.c:test_qemu_escape_param" .PP .Vb 2 \& static void \& test_qemu_escape_param (void) .Ve .PP Test \f(CW\*(C`guestfs_int_qemu_escape_param\*(C'\fR .PP 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. .PP Function \f(CW\*(C`lib/unit\-tests.c:test_timeval_diff\*(C'\fR .IX Subsection "Function lib/unit-tests.c:test_timeval_diff" .PP .Vb 2 \& static void \& test_timeval_diff (void) .Ve .PP Test \f(CW\*(C`guestfs_int_timeval_diff\*(C'\fR. .PP \fIFile lib/version.c\fR .IX Subsection "File lib/version.c" .PP This file provides simple version number management. .PP Function \f(CW\*(C`lib/version.c:guestfs_int_version_from_x_y\*(C'\fR .IX Subsection "Function lib/version.c:guestfs_int_version_from_x_y" .PP .Vb 2 \& int \& guestfs_int_version_from_x_y (guestfs_h *g, struct version *v, const char *str) .Ve .PP Parses a version from a string, looking for a \f(CW\*(C`X.Y\*(C'\fR pattern. .PP Returns \f(CW\-1\fR on failure (like failed integer parsing), \f(CW0\fR on missing match, and \f(CW1\fR on match and successful parsing. \f(CW\*(C`v\*(C'\fR is changed only on successful match. .PP Function \f(CW\*(C`lib/version.c:guestfs_int_version_from_x_y_re\*(C'\fR .IX Subsection "Function lib/version.c:guestfs_int_version_from_x_y_re" .PP .Vb 3 \& int \& guestfs_int_version_from_x_y_re (guestfs_h *g, struct version *v, \& const char *str, const pcre2_code *re) .Ve .PP Parses a version from a string, using the specified \f(CW\*(C`re\*(C'\fR as regular expression which \fImust\fR provide (at least) two matches. .PP Returns \f(CW\-1\fR on failure (like failed integer parsing), \f(CW0\fR on missing match, and \f(CW1\fR on match and successful parsing. \f(CW\*(C`v\*(C'\fR is changed only on successful match. .PP Function \f(CW\*(C`lib/version.c:guestfs_int_version_from_x_y_or_x\*(C'\fR .IX Subsection "Function lib/version.c:guestfs_int_version_from_x_y_or_x" .PP .Vb 3 \& int \& guestfs_int_version_from_x_y_or_x (guestfs_h *g, struct version *v, \& const char *str) .Ve .PP Parses a version from a string, either looking for a \f(CW\*(C`X.Y\*(C'\fR pattern or considering it as whole integer. .PP Returns \f(CW\-1\fR on failure (like failed integer parsing), \f(CW0\fR on missing match, and \f(CW1\fR on match and successful parsing. \f(CW\*(C`v\*(C'\fR is changed only on successful match. .PP Function \f(CW\*(C`lib/version.c:guestfs_int_parse_unsigned_int\*(C'\fR .IX Subsection "Function lib/version.c:guestfs_int_parse_unsigned_int" .PP .Vb 2 \& int \& guestfs_int_parse_unsigned_int (guestfs_h *g, const char *str) .Ve .PP Parse small, unsigned ints, as used in version numbers. .PP This will fail with an error if trailing characters are found after the integer. .PP Returns ≥ \f(CW0\fR on success, or \f(CW\-1\fR on failure. .PP \fIFile lib/wait.c\fR .IX Subsection "File lib/wait.c" .PP Function \f(CW\*(C`lib/wait.c:guestfs_int_waitpid\*(C'\fR .IX Subsection "Function lib/wait.c:guestfs_int_waitpid" .PP .Vb 2 \& int \& guestfs_int_waitpid (guestfs_h *g, pid_t pid, int *status, const char *errmsg) .Ve .PP A safe version of \fBwaitpid\fR\|(3) which retries if \f(CW\*(C`EINTR\*(C'\fR is returned. .PP \&\fINote:\fR this only needs to be used in the library, or in programs that install a non-restartable \f(CW\*(C`SIGCHLD\*(C'\fR handler (which is not the case for any current libguestfs virt tools). .PP 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, \f(CW\*(C`SIGCHLD\*(C'\fR is sent to the process, and the wait syscall returns \f(CW\*(C`EINTR\*(C'\fR. Since the library cannot control the signal handler, we have to instead restart the wait syscall, which is the purpose of this wrapper. .PP Function \f(CW\*(C`lib/wait.c:guestfs_int_waitpid_noerror\*(C'\fR .IX Subsection "Function lib/wait.c:guestfs_int_waitpid_noerror" .PP .Vb 2 \& void \& guestfs_int_waitpid_noerror (pid_t pid) .Ve .PP Like \f(CW\*(C`guestfs_int_waitpid\*(C'\fR, but ignore errors. .PP Function \f(CW\*(C`lib/wait.c:guestfs_int_wait4\*(C'\fR .IX Subsection "Function lib/wait.c:guestfs_int_wait4" .PP .Vb 3 \& int \& guestfs_int_wait4 (guestfs_h *g, pid_t pid, int *status, \& struct rusage *rusage, const char *errmsg) .Ve .PP A safe version of \fBwait4\fR\|(2) which retries if \f(CW\*(C`EINTR\*(C'\fR is returned. .PP \fIFile lib/whole\-file.c\fR .IX Subsection "File lib/whole-file.c" .PP Function \f(CW\*(C`lib/whole\-file.c:guestfs_int_read_whole_file\*(C'\fR .IX Subsection "Function lib/whole-file.c:guestfs_int_read_whole_file" .PP .Vb 3 \& int \& guestfs_int_read_whole_file (guestfs_h *g, const char *filename, \& char **data_r, size_t *size_r) .Ve .PP Read the whole file \f(CW\*(C`filename\*(C'\fR into a memory buffer. .PP The memory buffer is initialized and returned in \f(CW\*(C`data_r\*(C'\fR. The size of the file in bytes is returned in \f(CW\*(C`size_r\*(C'\fR. The return buffer must be freed by the caller. .PP On error this sets the error in the handle and returns \f(CW\-1\fR. .PP For the convenience of callers, the returned buffer is NUL-terminated (the NUL is not included in the size). .PP The file must be a \fBregular\fR, \fBlocal\fR, \fBtrusted\fR 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. .SS "Subdirectory \fIcommon/edit\fP" .IX Subsection "Subdirectory common/edit" \fIFile common/edit/file\-edit.c\fR .IX Subsection "File common/edit/file-edit.c" .PP This file implements common file editing in a range of utilities including \fBguestfish\fR\|(1), \fBvirt\-edit\fR\|(1), \fBvirt\-customize\fR\|(1) and \fBvirt\-builder\fR\|(1). .PP It contains the code for both interactive\-(editor\-)based editing and non-interactive editing using Perl snippets. .PP Function \f(CW\*(C`common/edit/file\-edit.c:edit_file_editor\*(C'\fR .IX Subsection "Function common/edit/file-edit.c:edit_file_editor" .PP .Vb 3 \& int \& edit_file_editor (guestfs_h *g, const char *filename, const char *editor, \& const char *backup_extension, int verbose) .Ve .PP Edit \f(CW\*(C`filename\*(C'\fR using the specified \f(CW\*(C`editor\*(C'\fR application. .PP If \f(CW\*(C`backup_extension\*(C'\fR is not null, then a copy of \f(CW\*(C`filename\*(C'\fR is saved with \f(CW\*(C`backup_extension\*(C'\fR appended to its file name. .PP If \f(CW\*(C`editor\*(C'\fR is null, then the \f(CW$EDITOR\fR environment variable will be queried for the editor application, leaving \f(CW\*(C`vi\*(C'\fR as fallback if not set. .PP Returns \f(CW\-1\fR for failure, \f(CW0\fR on success, \f(CW1\fR if the editor did not change the file (e.g. the user closed the editor without saving). .PP Function \f(CW\*(C`common/edit/file\-edit.c:edit_file_perl\*(C'\fR .IX Subsection "Function common/edit/file-edit.c:edit_file_perl" .PP .Vb 3 \& int \& edit_file_perl (guestfs_h *g, const char *filename, const char *perl_expr, \& const char *backup_extension, int verbose) .Ve .PP Edit \f(CW\*(C`filename\*(C'\fR running the specified \f(CW\*(C`perl_expr\*(C'\fR using Perl. .PP If \f(CW\*(C`backup_extension\*(C'\fR is not null, then a copy of \f(CW\*(C`filename\*(C'\fR is saved with \f(CW\*(C`backup_extension\*(C'\fR appended to its file name. .PP Returns \f(CW\-1\fR for failure, \f(CW0\fR on success. .SS "Subdirectory \fIcommon/options\fP" .IX Subsection "Subdirectory common/options" \fIFile common/options/config.c\fR .IX Subsection "File common/options/config.c" .PP This file parses the guestfish configuration file, usually \&\fI~/.libguestfs\-tools.rc\fR or \fI/etc/libguestfs\-tools.conf\fR. .PP Note that \f(CW\*(C`parse_config\*(C'\fR is called very early, before command line parsing, before the \f(CW\*(C`verbose\*(C'\fR flag has been set, even before the global handle \f(CW\*(C`g\*(C'\fR is opened. .PP \fIFile common/options/decrypt.c\fR .IX Subsection "File common/options/decrypt.c" .PP This file implements the decryption of disk images, usually done before mounting their partitions. .PP Function \f(CW\*(C`common/options/decrypt.c:make_mapname\*(C'\fR .IX Subsection "Function common/options/decrypt.c:make_mapname" .PP .Vb 2 \& static char * \& make_mapname (const char *device) .Ve .PP Make a LUKS map name from the partition or logical volume name, eg. \&\f(CW"/dev/vda2" => "cryptvda2"\fR, or \f(CW"/dev/vg\-ssd/lv\-root7" => "cryptvgssdlvroot7"\fR. Note that, in logical volume device names, \&\fBc_isalnum()\fR eliminates the "/" separator from between the VG and the LV, so this mapping is not unique; but for our purposes, it will do. .PP Function \f(CW\*(C`common/options/decrypt.c:inspect_do_decrypt\*(C'\fR .IX Subsection "Function common/options/decrypt.c:inspect_do_decrypt" .PP .Vb 2 \& void \& inspect_do_decrypt (guestfs_h *g, struct key_store *ks) .Ve .PP Simple implementation of decryption: look for any encrypted partitions and decrypt them, then rescan for VGs. .PP \fIFile common/options/display\-options.c\fR .IX Subsection "File common/options/display-options.c" .PP This file contains common code used to implement \fI\-\-short\-options\fR and \fI\-\-long\-options\fR in C virt tools. (The equivalent for OCaml virt tools is implemented by \fIcommon/mltools/getopt.ml\fR). .PP These "hidden" options are used to implement bash tab completion. .PP Function \f(CW\*(C`common/options/display\-options.c:display_short_options\*(C'\fR .IX Subsection "Function common/options/display-options.c:display_short_options" .PP .Vb 2 \& void \& display_short_options (const char *format) .Ve .PP Implements the internal \f(CW\*(C`tool \fR\f(CI\-\-short\-options\fR\f(CW\*(C'\fR flag, which just lists out the short options available. Used by bash completion. .PP Function \f(CW\*(C`common/options/display\-options.c:display_long_options\*(C'\fR .IX Subsection "Function common/options/display-options.c:display_long_options" .PP .Vb 2 \& void \& display_long_options (const struct option *long_options) .Ve .PP Implements the internal \f(CW\*(C`tool \fR\f(CI\-\-long\-options\fR\f(CW\*(C'\fR flag, which just lists out the long options available. Used by bash completion. .PP \fIFile common/options/domain.c\fR .IX Subsection "File common/options/domain.c" .PP Implements the guestfish (and other tools) \fI\-d\fR option. .PP Function \f(CW\*(C`common/options/domain.c:add_libvirt_drives\*(C'\fR .IX Subsection "Function common/options/domain.c:add_libvirt_drives" .PP .Vb 2 \& int \& add_libvirt_drives (guestfs_h *g, const char *guest) .Ve .PP This function is called when a user invokes \f(CW\*(C`guestfish\ \-d\ guest\*(C'\fR. .PP Returns the number of drives added (\f(CW\*(C`>\ 0\*(C'\fR), or \f(CW\-1\fR for failure. .PP \fIFile common/options/inspect.c\fR .IX Subsection "File common/options/inspect.c" .PP This file implements inspecting the guest and mounting the filesystems found in the right places. It is used by the \&\fBguestfish\fR\|(1) \fI\-i\fR option and some utilities such as \&\fBvirt\-cat\fR\|(1). .PP Function \f(CW\*(C`common/options/inspect.c:inspect_mount_handle\*(C'\fR .IX Subsection "Function common/options/inspect.c:inspect_mount_handle" .PP .Vb 2 \& void \& inspect_mount_handle (guestfs_h *g, struct key_store *ks) .Ve .PP This function implements the \fI\-i\fR option. .PP Function \f(CW\*(C`common/options/inspect.c:print_inspect_prompt\*(C'\fR .IX Subsection "Function common/options/inspect.c:print_inspect_prompt" .PP .Vb 2 \& void \& print_inspect_prompt (void) .Ve .PP This function is called only if \f(CW\*(C`inspect_mount_root\*(C'\fR was called, and only after we've printed the prompt in interactive mode. .PP \fIFile common/options/keys.c\fR .IX Subsection "File common/options/keys.c" .PP Function \f(CW\*(C`common/options/keys.c:read_key\*(C'\fR .IX Subsection "Function common/options/keys.c:read_key" .PP .Vb 2 \& char * \& read_key (const char *param) .Ve .PP Read a passphrase ('Key') from \fI/dev/tty\fR with echo off. .PP The caller (\fIfish/cmds.c\fR) will call free on the string afterwards. Based on the code in cryptsetup file \fIlib/utils.c\fR. .PP \fIFile common/options/options.c\fR .IX Subsection "File common/options/options.c" .PP This file contains common options parsing code used by guestfish and many other tools which share a common options syntax. .PP For example, guestfish, virt-cat, virt-ls etc all support the \fI\-a\fR option, and that is handled in all of those tools using a macro \&\f(CW\*(C`OPTION_a\*(C'\fR defined in \fIfish/options.h\fR. .PP There are a lot of common global variables used, \f(CW\*(C`drvs\*(C'\fR accumulates the list of drives, \f(CW\*(C`verbose\*(C'\fR for the \fI\-v\fR flag, and many more. .PP Function \f(CW\*(C`common/options/options.c:option_a\*(C'\fR .IX Subsection "Function common/options/options.c:option_a" .PP .Vb 3 \& void \& option_a (const char *arg, const char *format, int blocksize, \& struct drv **drvsp) .Ve .PP Handle the guestfish \fI\-a\fR option on the command line. .PP Function \f(CW\*(C`common/options/options.c:option_d\*(C'\fR .IX Subsection "Function common/options/options.c:option_d" .PP .Vb 2 \& void \& option_d (const char *arg, struct drv **drvsp) .Ve .PP Handle the \fI\-d\fR option when passed on the command line. .PP Function \f(CW\*(C`common/options/options.c:display_mountpoints_on_failure\*(C'\fR .IX Subsection "Function common/options/options.c:display_mountpoints_on_failure" .PP .Vb 3 \& static void \& display_mountpoints_on_failure (const char *mp_device, \& const char *user_supplied_options) .Ve .PP If the \fI\-m\fR option fails on any command, display a useful error message listing the mountpoints. .PP \fIFile common/options/uri.c\fR .IX Subsection "File common/options/uri.c" .PP This file implements URI parsing for the \fI\-a\fR option, in many utilities including \fBguestfish\fR\|(1), \fBvirt\-cat\fR\|(1), \&\fBvirt\-builder\fR\|(1), \fBvirt\-customize\fR\|(1), etc. .SS "Subdirectory \fIcommon/parallel\fP" .IX Subsection "Subdirectory common/parallel" \fIFile common/parallel/domains.c\fR .IX Subsection "File common/parallel/domains.c" .PP This file is used by \f(CW\*(C`virt\-df\*(C'\fR and some of the other tools when they are implicitly asked to operate over all libvirt domains (VMs), for example when \f(CW\*(C`virt\-df\*(C'\fR is called without specifying any particular disk image. .PP It hides the complexity of querying the list of domains from libvirt. .PP Function \f(CW\*(C`common/parallel/domains.c:free_domains\*(C'\fR .IX Subsection "Function common/parallel/domains.c:free_domains" .PP .Vb 2 \& void \& free_domains (void) .Ve .PP Frees up everything allocated by \f(CW\*(C`get_all_libvirt_domains\*(C'\fR. .PP Function \f(CW\*(C`common/parallel/domains.c:get_all_libvirt_domains\*(C'\fR .IX Subsection "Function common/parallel/domains.c:get_all_libvirt_domains" .PP .Vb 2 \& void \& get_all_libvirt_domains (const char *libvirt_uri) .Ve .PP Read all libguest guests into the global variables \f(CW\*(C`domains\*(C'\fR and \&\f(CW\*(C`nr_domains\*(C'\fR. The guests are ordered by name. This exits on any error. .PP \fIFile common/parallel/estimate\-max\-threads.c\fR .IX Subsection "File common/parallel/estimate-max-threads.c" .PP Function \f(CW\*(C`common/parallel/estimate\-max\-threads.c:estimate_max_threads\*(C'\fR .IX Subsection "Function common/parallel/estimate-max-threads.c:estimate_max_threads" .PP .Vb 2 \& size_t \& estimate_max_threads (void) .Ve .PP This function uses the output of \f(CW\*(C`free \-m\*(C'\fR to estimate how many libguestfs appliances could be safely started in parallel. Note that it always returns ≥ 1. .PP Function \f(CW\*(C`common/parallel/estimate\-max\-threads.c:read_line_from\*(C'\fR .IX Subsection "Function common/parallel/estimate-max-threads.c:read_line_from" .PP .Vb 2 \& static char * \& read_line_from (const char *cmd) .Ve .PP Run external command and read the first line of output. .PP \fIFile common/parallel/parallel.c\fR .IX Subsection "File common/parallel/parallel.c" .PP This file is used by \f(CW\*(C`virt\-df\*(C'\fR 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. .PP 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). .PP Function \f(CW\*(C`common/parallel/parallel.c:start_threads\*(C'\fR .IX Subsection "Function common/parallel/parallel.c:start_threads" .PP .Vb 2 \& int \& start_threads (size_t option_P, guestfs_h *options_handle, work_fn work) .Ve .PP Run the threads and work through the global list of libvirt domains. .PP \&\f(CW\*(C`option_P\*(C'\fR is whatever the user passed in the \fI\-P\fR option, or \&\f(CW0\fR if the user didn't use the \fI\-P\fR option (in which case the number of threads is chosen heuristically). .PP \&\f(CW\*(C`options_handle\*(C'\fR (which may be \f(CW\*(C`NULL\*(C'\fR) is the global guestfs handle created by the options mini-library. .PP The work function (\f(CW\*(C`work\*(C'\fR) should do the work (inspecting the domain, etc.) on domain index \f(CW\*(C`i\*(C'\fR. However it \fImust not\fR print out any result directly. Instead it prints anything it needs to the supplied \f(CW\*(C`FILE *\*(C'\fR. The work function should return \f(CW0\fR on success or \f(CW\-1\fR on error. .PP The \f(CW\*(C`start_threads\*(C'\fR function returns \f(CW0\fR if all work items completed successfully, or \f(CW\-1\fR if there was an error. .SS "Subdirectory \fIcommon/progress\fP" .IX Subsection "Subdirectory common/progress" \fIFile common/progress/progress.c\fR .IX Subsection "File common/progress/progress.c" .PP This file implements the progress bar in \fBguestfish\fR\|(1), \&\fBvirt\-resize\fR\|(1) and \fBvirt\-sparsify\fR\|(1). .PP Function \f(CW\*(C`common/progress/progress.c:progress_bar_init\*(C'\fR .IX Subsection "Function common/progress/progress.c:progress_bar_init" .PP .Vb 2 \& struct progress_bar * \& progress_bar_init (unsigned flags) .Ve .PP Initialize a progress bar struct. .PP It is intended that you can reuse the same struct for multiple commands (but only in a single thread). Call \f(CW\*(C`progress_bar_reset\*(C'\fR before each new command. .PP Function \f(CW\*(C`common/progress/progress.c:progress_bar_free\*(C'\fR .IX Subsection "Function common/progress/progress.c:progress_bar_free" .PP .Vb 2 \& void \& progress_bar_free (struct progress_bar *bar) .Ve .PP Free a progress bar struct. .PP Function \f(CW\*(C`common/progress/progress.c:progress_bar_reset\*(C'\fR .IX Subsection "Function common/progress/progress.c:progress_bar_reset" .PP .Vb 2 \& void \& progress_bar_reset (struct progress_bar *bar) .Ve .PP This function should be called just before you issue any command. .PP Function \f(CW\*(C`common/progress/progress.c:estimate_remaining_time\*(C'\fR .IX Subsection "Function common/progress/progress.c:estimate_remaining_time" .PP .Vb 2 \& static double \& estimate_remaining_time (struct progress_bar *bar, double ratio) .Ve .PP Return remaining time estimate (in seconds) for current call. .PP 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). .PP Function \f(CW\*(C`common/progress/progress.c:progress_bar_set\*(C'\fR .IX Subsection "Function common/progress/progress.c:progress_bar_set" .PP .Vb 3 \& void \& progress_bar_set (struct progress_bar *bar, \& uint64_t position, uint64_t total) .Ve .PP Set the position of the progress bar. .PP This should be called from a \f(CW\*(C`GUESTFS_EVENT_PROGRESS\*(C'\fR event callback. .SS "Subdirectory \fIcommon/qemuopts\fP" .IX Subsection "Subdirectory common/qemuopts" \fIFile common/qemuopts/qemuopts\-tests.c\fR .IX Subsection "File common/qemuopts/qemuopts-tests.c" .PP Unit tests of internal functions. .PP These tests may use a libguestfs handle, but must not launch the handle. Also, avoid long-running tests. .PP \fIFile common/qemuopts/qemuopts.c\fR .IX Subsection "File common/qemuopts/qemuopts.c" .PP Mini-library for writing qemu command lines and qemu config files. .PP There are some shortcomings with the model used for qemu options which aren't clear until you try to convert options into a configuration file. However if we attempted to model the options in more detail then this library would be both very difficult to use and incompatible with older versions of qemu. Hopefully the current model is a decent compromise. .PP For reference here are the problems: .IP \(bu 4 There's inconsistency in qemu between options and config file, eg. \&\f(CW\*(C`\-smp 4\*(C'\fR becomes: .Sp .Vb 2 \& [smp\-opts] \& cpus = "4" .Ve .IP \(bu 4 Similar to the previous point, you can write either \f(CW\*(C`\-smp 4\*(C'\fR or \&\f(CW\*(C`\-smp cpus=4\*(C'\fR (although this won't work in very old qemu). When generating a config file you need to know the implicit key name. .IP \(bu 4 In \f(CW\*(C`\-opt key=value,...\*(C'\fR the \f(CW\*(C`key\*(C'\fR is really a tree/array specifier. The way this works is complicated but hinted at here: http://git.qemu.org/?p=qemu.git;a=blob;f=util/keyval.c;h=93d5db6b590427e412dfb172f1c406d6dd8958c1;hb=HEAD .IP \(bu 4 Some options are syntactic sugar. eg. \f(CW\*(C`\-kernel foo\*(C'\fR is sugar for \f(CW\*(C`\-machine kernel=foo\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_create\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_create" .PP .Vb 2 \& struct qemuopts * \& qemuopts_create (void) .Ve .PP Create an empty list of qemu options. .PP The caller must eventually free the list by calling \&\f(CW\*(C`qemuopts_free\*(C'\fR. .PP Returns \f(CW\*(C`NULL\*(C'\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_free\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_free" .PP .Vb 2 \& void \& qemuopts_free (struct qemuopts *qopts) .Ve .PP Free the list of qemu options. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_add_flag\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_add_flag" .PP .Vb 2 \& int \& qemuopts_add_flag (struct qemuopts *qopts, const char *flag) .Ve .PP Add a command line flag which has no argument. eg: .PP .Vb 1 \& qemuopts_add_flag (qopts, "\-no\-user\-config"); .Ve .PP Returns \f(CW0\fR on success. Returns \f(CW\-1\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_add_arg\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_add_arg" .PP .Vb 2 \& int \& qemuopts_add_arg (struct qemuopts *qopts, const char *flag, const char *value) .Ve .PP Add a command line flag which has a single argument. eg: .PP .Vb 1 \& qemuopts_add_arg (qopts, "\-m", "1024"); .Ve .PP Don't use this if the argument is a comma-separated list, since quoting will not be done properly. See \f(CW\*(C`qemuopts_add_arg_list\*(C'\fR. .PP Returns \f(CW0\fR on success. Returns \f(CW\-1\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_add_arg_format\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_add_arg_format" .PP .Vb 3 \& int \& qemuopts_add_arg_format (struct qemuopts *qopts, const char *flag, \& const char *fs, ...) .Ve .PP Add a command line flag which has a single formatted argument. eg: .PP .Vb 1 \& qemuopts_add_arg_format (qopts, "\-m", "%d", 1024); .Ve .PP Don't use this if the argument is a comma-separated list, since quoting will not be done properly. See \f(CW\*(C`qemuopts_add_arg_list\*(C'\fR. .PP Returns \f(CW0\fR on success. Returns \f(CW\-1\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_add_arg_noquote\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_add_arg_noquote" .PP .Vb 3 \& int \& qemuopts_add_arg_noquote (struct qemuopts *qopts, const char *flag, \& const char *value) .Ve .PP This is like \f(CW\*(C`qemuopts_add_arg\*(C'\fR except that no quoting is done on the value. .PP For \f(CW\*(C`qemuopts_to_script\*(C'\fR and \f(CW\*(C`qemuopts_to_channel\*(C'\fR, this means that neither shell quoting nor qemu comma quoting is done on the value. .PP For \f(CW\*(C`qemuopts_to_argv\*(C'\fR this means that qemu comma quoting is not done. .PP \&\f(CW\*(C`qemuopts_to_config*\*(C'\fR will fail. .PP You should use this with great care. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_start_arg_list\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_start_arg_list" .PP .Vb 2 \& int \& qemuopts_start_arg_list (struct qemuopts *qopts, const char *flag) .Ve .PP Start an argument that takes a comma-separated list of fields. .PP Typical usage is like this (with error handling omitted): .PP .Vb 4 \& qemuopts_start_arg_list (qopts, "\-drive"); \& qemuopts_append_arg_list (qopts, "file=foo"); \& qemuopts_append_arg_list_format (qopts, "if=%s", "ide"); \& qemuopts_end_arg_list (qopts); .Ve .PP which would construct \f(CW\*(C`\-drive file=foo,if=ide\*(C'\fR .PP See also \f(CW\*(C`qemuopts_add_arg_list\*(C'\fR for a way to do simple cases in one call. .PP Returns \f(CW0\fR on success. Returns \f(CW\-1\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_add_arg_list\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_add_arg_list" .PP .Vb 3 \& int \& qemuopts_add_arg_list (struct qemuopts *qopts, const char *flag, \& const char *elem0, ...) .Ve .PP Add a command line flag which has a list of arguments. eg: .PP .Vb 1 \& qemuopts_add_arg_list (qopts, "\-drive", "file=foo", "if=ide", NULL); .Ve .PP This is turned into a comma-separated list, like: \&\f(CW\*(C`\-drive file=foo,if=ide\*(C'\fR. Note that this handles qemu quoting properly, so individual elements may contain commas and this will do the right thing. .PP Returns \f(CW0\fR on success. Returns \f(CW\-1\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_set_binary\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_set_binary" .PP .Vb 2 \& int \& qemuopts_set_binary (struct qemuopts *qopts, const char *binary) .Ve .PP Set the qemu binary name. .PP Returns \f(CW0\fR on success. Returns \f(CW\-1\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_set_binary_by_arch\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_set_binary_by_arch" .PP .Vb 2 \& int \& qemuopts_set_binary_by_arch (struct qemuopts *qopts, const char *arch) .Ve .PP Set the qemu binary name to \f(CW\*(C`qemu\-system\-[arch]\*(C'\fR. .PP As a special case if \f(CW\*(C`arch\*(C'\fR is \f(CW\*(C`NULL\*(C'\fR, the binary is set to the KVM binary for the current host architecture: .PP .Vb 1 \& qemuopts_set_binary_by_arch (qopts, NULL); .Ve .PP Returns \f(CW0\fR on success. Returns \f(CW\-1\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_to_script\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_to_script" .PP .Vb 2 \& int \& qemuopts_to_script (struct qemuopts *qopts, const char *filename) .Ve .PP Write the qemu options to a script. .PP \&\f(CW\*(C`qemuopts_set_binary*\*(C'\fR must be called first. .PP The script file will start with \f(CW\*(C`#!/bin/sh\*(C'\fR and will be chmod to mode \f(CW0755\fR. .PP Returns \f(CW0\fR on success. Returns \f(CW\-1\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:shell_quote\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:shell_quote" .PP .Vb 2 \& static void \& shell_quote (const char *str, FILE *fp) .Ve .PP Print \f(CW\*(C`str\*(C'\fR to \f(CW\*(C`fp\*(C'\fR, shell-quoting it if necessary. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:shell_and_comma_quote\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:shell_and_comma_quote" .PP .Vb 2 \& static void \& shell_and_comma_quote (const char *str, FILE *fp) .Ve .PP Print \f(CW\*(C`str\*(C'\fR to \f(CW\*(C`fp\*(C'\fR doing both shell and qemu comma quoting. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_to_channel\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_to_channel" .PP .Vb 2 \& int \& qemuopts_to_channel (struct qemuopts *qopts, FILE *fp) .Ve .PP Write the qemu options to a \f(CW\*(C`FILE *fp\*(C'\fR. .PP \&\f(CW\*(C`qemuopts_set_binary*\*(C'\fR must be called first. .PP Only the qemu command line is written. The caller may need to add \&\f(CW\*(C`#!/bin/sh\*(C'\fR and may need to chmod the resulting file to \f(CW0755\fR. .PP Returns \f(CW0\fR on success. Returns \f(CW\-1\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_to_argv\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_to_argv" .PP .Vb 2 \& char ** \& qemuopts_to_argv (struct qemuopts *qopts) .Ve .PP Return a NULL-terminated argument list, of the kind that can be passed directly to \fBexecv\fR\|(3). .PP \&\f(CW\*(C`qemuopts_set_binary*\*(C'\fR must be called first. It will be returned as \f(CW\*(C`argv[0]\*(C'\fR in the returned list. .PP The list of strings and the strings themselves must be freed by the caller. .PP Returns \f(CW\*(C`NULL\*(C'\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_to_config_file\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_to_config_file" .PP .Vb 2 \& int \& qemuopts_to_config_file (struct qemuopts *qopts, const char *filename) .Ve .PP Write the qemu options to a qemu config file, suitable for reading in using \f(CW\*(C`qemu \-readconfig filename\*(C'\fR. .PP Note that qemu config files have limitations on content and quoting, so not all qemuopts structs can be written (this function returns an error in these cases). For more information see https://habkost.net/posts/2016/12/qemu\-apis\-qemuopts.html https://bugs.launchpad.net/qemu/+bug/1686364 .PP Also, command line argument names and config file sections sometimes have different names. For example the equivalent of \&\f(CW\*(C`\-m 1024\*(C'\fR is: .PP .Vb 2 \& [memory] \& size = "1024" .Ve .PP This code does \fInot\fR attempt to convert between the two forms. You just need to know how to do that yourself. .PP Returns \f(CW0\fR on success. Returns \f(CW\-1\fR on error, setting \f(CW\*(C`errno\*(C'\fR. .PP Function \f(CW\*(C`common/qemuopts/qemuopts.c:qemuopts_to_config_channel\*(C'\fR .IX Subsection "Function common/qemuopts/qemuopts.c:qemuopts_to_config_channel" .PP .Vb 2 \& int \& qemuopts_to_config_channel (struct qemuopts *qopts, FILE *fp) .Ve .PP Same as \f(CW\*(C`qemuopts_to_config_file\*(C'\fR, but this writes to a \f(CW\*(C`FILE *fp\*(C'\fR. .SS "Subdirectory \fIcommon/utils\fP" .IX Subsection "Subdirectory common/utils" \fIFile common/utils/cleanups.c\fR .IX Subsection "File common/utils/cleanups.c" .PP Libguestfs uses \f(CW\*(C`CLEANUP_*\*(C'\fR macros to simplify temporary allocations. They are implemented using the \&\f(CW\*(C`_\|_attribute_\|_((cleanup))\*(C'\fR feature of gcc and clang. Typical usage is: .PP .Vb 6 \& fn () \& { \& CLEANUP_FREE char *str = NULL; \& str = safe_asprintf (g, "foo"); \& // str is freed automatically when the function returns \& } .Ve .PP There are a few catches to be aware of with the cleanup mechanism: .IP \(bu 4 If a cleanup variable is not initialized, then you can end up calling \fBfree\fR\|(3) with an undefined value, resulting in the program crashing. For this reason, you should usually initialize every cleanup variable with something, eg. \f(CW\*(C`NULL\*(C'\fR .IP \(bu 4 Don't mark variables holding return values as cleanup variables. .IP \(bu 4 The \f(CWmain()\fR function shouldn't use cleanup variables since it is normally exited by calling \fBexit\fR\|(3), and that doesn't call the cleanup handlers. .PP The functions in this file are used internally by the \f(CW\*(C`CLEANUP_*\*(C'\fR macros. Don't call them directly. .PP \fIFile common/utils/gnulib\-cleanups.c\fR .IX Subsection "File common/utils/gnulib-cleanups.c" .PP Libguestfs uses \f(CW\*(C`CLEANUP_*\*(C'\fR macros to simplify temporary allocations. They are implemented using the \&\f(CW\*(C`_\|_attribute_\|_((cleanup))\*(C'\fR feature of gcc and clang. Typical usage is: .PP .Vb 6 \& fn () \& { \& CLEANUP_FREE char *str = NULL; \& str = safe_asprintf (g, "foo"); \& // str is freed automatically when the function returns \& } .Ve .PP There are a few catches to be aware of with the cleanup mechanism: .IP \(bu 4 If a cleanup variable is not initialized, then you can end up calling \fBfree\fR\|(3) with an undefined value, resulting in the program crashing. For this reason, you should usually initialize every cleanup variable with something, eg. \f(CW\*(C`NULL\*(C'\fR .IP \(bu 4 Don't mark variables holding return values as cleanup variables. .IP \(bu 4 The \f(CWmain()\fR function shouldn't use cleanup variables since it is normally exited by calling \fBexit\fR\|(3), and that doesn't call the cleanup handlers. .PP The functions in this file are used internally by the \f(CW\*(C`CLEANUP_*\*(C'\fR macros. Don't call them directly. .PP \fIFile common/utils/guestfs\-utils.h\fR .IX Subsection "File common/utils/guestfs-utils.h" .PP This header file is included in all "frontend" parts of libguestfs, namely the library, non-C language bindings, virt tools and tests. .PP The daemon does \fBnot\fR use this header. If you need a place to put something shared with absolutely everything including the daemon, put it in \fIlib/guestfs\-internal\-all.h\fR .PP If a definition is only needed by a single component of libguestfs (eg. just the library, or just a single virt tool) then it should \&\fBnot\fR be here! .PP \fIFile common/utils/libxml2\-writer\-macros.h\fR .IX Subsection "File common/utils/libxml2-writer-macros.h" .PP These macros make it easier to write XML. To use them correctly you must be aware of these assumptions: .IP \(bu 4 The \f(CW\*(C`xmlTextWriterPtr\*(C'\fR is called \f(CW\*(C`xo\*(C'\fR. It is used implicitly by all the macros. .IP \(bu 4 On failure, a function called \f(CW\*(C`xml_error\*(C'\fR is called which you must define (usually as a macro). You must use \f(CW\*(C`CLEANUP_*\*(C'\fR macros in your functions if you want correct cleanup of local variables along the error path. .IP \(bu 4 All the "bad" casting is hidden inside the macros. .PP Definition \f(CW\*(C`common/utils/libxml2\-writer\-macros.h:start_element\*(C'\fR .IX Subsection "Definition common/utils/libxml2-writer-macros.h:start_element" .PP .Vb 1 \& #define start_element .Ve .PP To define an XML element use: .PP .Vb 3 \& start_element ("name") { \& ... \& } end_element (); .Ve .PP which produces \f(CW\*(C`...\*(C'\fR .PP Definition \f(CW\*(C`common/utils/libxml2\-writer\-macros.h:empty_element\*(C'\fR .IX Subsection "Definition common/utils/libxml2-writer-macros.h:empty_element" .PP .Vb 1 \& #define empty_element .Ve .PP To define an empty element: .PP .Vb 1 \& empty_element ("name"); .Ve .PP which produces \f(CW\*(C`\*(C'\fR .PP Definition \f(CW\*(C`common/utils/libxml2\-writer\-macros.h:single_element\*(C'\fR .IX Subsection "Definition common/utils/libxml2-writer-macros.h:single_element" .PP .Vb 1 \& #define single_element .Ve .PP To define a single element with no attributes containing some text: .PP .Vb 1 \& single_element ("name", text); .Ve .PP which produces \f(CW\*(C`text\*(C'\fR .PP Definition \f(CW\*(C`common/utils/libxml2\-writer\-macros.h:single_element_format\*(C'\fR .IX Subsection "Definition common/utils/libxml2-writer-macros.h:single_element_format" .PP .Vb 1 \& #define single_element_format .Ve .PP To define a single element with no attributes containing some text using a format string: .PP .Vb 1 \& single_element_format ("cores", "%d", nr_cores); .Ve .PP which produces \f(CW\*(C`4\*(C'\fR .PP Definition \f(CW\*(C`common/utils/libxml2\-writer\-macros.h:attribute\*(C'\fR .IX Subsection "Definition common/utils/libxml2-writer-macros.h:attribute" .PP .Vb 1 \& #define attribute .Ve .PP To define an XML element with attributes, use: .PP .Vb 5 \& start_element ("name") { \& attribute ("foo", "bar"); \& attribute_format ("count", "%d", count); \& ... \& } end_element (); .Ve .PP which produces \f(CW\*(C`...\*(C'\fR .PP Definition \f(CW\*(C`common/utils/libxml2\-writer\-macros.h:attribute_ns\*(C'\fR .IX Subsection "Definition common/utils/libxml2-writer-macros.h:attribute_ns" .PP .Vb 1 \& #define attribute_ns .Ve .PP \&\f(CW\*(C`attribute_ns (prefix, key, namespace_uri, value)\*(C'\fR defines a namespaced attribute. .PP Definition \f(CW\*(C`common/utils/libxml2\-writer\-macros.h:string\*(C'\fR .IX Subsection "Definition common/utils/libxml2-writer-macros.h:string" .PP .Vb 1 \& #define string .Ve .PP To define a verbatim string, use: .PP .Vb 1 \& string ("hello"); .Ve .PP Definition \f(CW\*(C`common/utils/libxml2\-writer\-macros.h:string_format\*(C'\fR .IX Subsection "Definition common/utils/libxml2-writer-macros.h:string_format" .PP .Vb 1 \& #define string_format .Ve .PP To define a verbatim string using a format string, use: .PP .Vb 1 \& string ("%s, world", greeting); .Ve .PP Definition \f(CW\*(C`common/utils/libxml2\-writer\-macros.h:base64\*(C'\fR .IX Subsection "Definition common/utils/libxml2-writer-macros.h:base64" .PP .Vb 1 \& #define base64 .Ve .PP To write a string encoded as base64: .PP .Vb 1 \& base64 (data, size); .Ve .PP Definition \f(CW\*(C`common/utils/libxml2\-writer\-macros.h:comment\*(C'\fR .IX Subsection "Definition common/utils/libxml2-writer-macros.h:comment" .PP .Vb 1 \& #define comment .Ve .PP To define a comment in the XML, use: .PP .Vb 1 \& comment ("number of items = %d", nr_items); .Ve .PP \fIFile common/utils/stringlists\-utils.c\fR .IX Subsection "File common/utils/stringlists-utils.c" .PP Utility functions used by the library, tools and language bindings. .PP These functions \fImust not\fR call internal library functions such as \f(CW\*(C`safe_*\*(C'\fR, \f(CW\*(C`error\*(C'\fR or \f(CW\*(C`perrorf\*(C'\fR, or any \f(CW\*(C`guestfs_int_*\*(C'\fR. .PP Function \f(CW\*(C`common/utils/stringlists\-utils.c:guestfs_int_split_string\*(C'\fR .IX Subsection "Function common/utils/stringlists-utils.c:guestfs_int_split_string" .PP .Vb 2 \& char ** \& guestfs_int_split_string (char sep, const char *str) .Ve .PP Split string at separator character \f(CW\*(C`sep\*(C'\fR, returning the list of strings. Returns \f(CW\*(C`NULL\*(C'\fR on memory allocation failure. .PP Note (assuming \f(CW\*(C`sep\*(C'\fR is \f(CW\*(C`:\*(C'\fR): .ie n .IP """str == NULL""" 4 .el .IP "\f(CWstr == NULL\fR" 4 .IX Item "str == NULL" aborts .ie n .IP """str == """"""" 4 .el .IP "\f(CWstr == """"\fR" 4 .IX Item "str == """"" returns \f(CW\*(C`[]\*(C'\fR .ie n .IP """str == ""abc""""" 4 .el .IP "\f(CWstr == ""abc""\fR" 4 .IX Item "str == ""abc""" returns \f(CW\*(C`["abc"]\*(C'\fR .ie n .IP """str == "":""""" 4 .el .IP "\f(CWstr == "":""\fR" 4 .IX Item "str == "":""" returns \f(CW\*(C`["", ""]\*(C'\fR .PP \fIFile common/utils/utils.c\fR .IX Subsection "File common/utils/utils.c" .PP Utility functions used by the library, tools and language bindings. .PP These functions \fImust not\fR call internal library functions such as \f(CW\*(C`safe_*\*(C'\fR, \f(CW\*(C`error\*(C'\fR or \f(CW\*(C`perrorf\*(C'\fR, or any \f(CW\*(C`guestfs_int_*\*(C'\fR. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_replace_string\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_replace_string" .PP .Vb 2 \& char * \& guestfs_int_replace_string (const char *str, const char *s1, const char *s2) .Ve .PP Replace every instance of \f(CW\*(C`s1\*(C'\fR appearing in \f(CW\*(C`str\*(C'\fR with \f(CW\*(C`s2\*(C'\fR. A newly allocated string is returned which must be freed by the caller. If allocation fails this can return \f(CW\*(C`NULL\*(C'\fR. .PP For example: .PP .Vb 1 \& replace_string ("abcabb", "ab", "a"); .Ve .PP would return \f(CW"acab"\fR. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_exit_status_to_string\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_exit_status_to_string" .PP .Vb 3 \& char * \& guestfs_int_exit_status_to_string (int status, const char *cmd_name, \& char *buffer, size_t buflen) .Ve .PP Translate a wait/system exit status into a printable string. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_random_string\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_random_string" .PP .Vb 2 \& int \& guestfs_int_random_string (char *ret, size_t len) .Ve .PP Return a random string of characters. .PP Notes: .IP \(bu 4 The \f(CW\*(C`ret\*(C'\fR buffer must have length \f(CW\*(C`len+1\*(C'\fR in order to store the final \f(CW\*(C`\e0\*(C'\fR character. .IP \(bu 4 There is about 5 bits of randomness per output character (so about \&\f(CW\*(C`5*len\*(C'\fR bits of randomness in the resulting string). .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_drive_name\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_drive_name" .PP .Vb 2 \& char * \& guestfs_int_drive_name (size_t index, char *ret) .Ve .PP This turns a drive index (eg. \f(CW27\fR) into a drive name (eg. \f(CW"ab"\fR). .PP Drive indexes count from \f(CW0\fR. The return buffer has to be large enough for the resulting string, and the returned pointer points to the *end* of the string. .PP https://rwmj.wordpress.com/2011/01/09/how\-are\-linux\-drives\-named\-beyond\-drive\-26\-devsdz/ .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_drive_index\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_drive_index" .PP .Vb 2 \& ssize_t \& guestfs_int_drive_index (const char *name) .Ve .PP The opposite of \f(CW\*(C`guestfs_int_drive_name\*(C'\fR. Take a string like \&\f(CW"ab"\fR and return the index (eg \f(CW27\fR). .PP Note that you must remove any prefix such as \f(CW"hd"\fR, \f(CW"sd"\fR etc, or any partition number before calling the function. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_is_true\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_is_true" .PP .Vb 2 \& int \& guestfs_int_is_true (const char *str) .Ve .PP Similar to \f(CW\*(C`Tcl_GetBoolean\*(C'\fR. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_string_is_valid\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_string_is_valid" .PP .Vb 4 \& bool \& guestfs_int_string_is_valid (const char *str, \& size_t min_length, size_t max_length, \& int flags, const char *extra) .Ve .PP Check a string for validity, that it contains only certain characters, and minimum and maximum length. This function is usually wrapped in a VALID_* macro, see \fIlib/drives.c\fR for an example. .PP \&\f(CW\*(C`str\*(C'\fR is the string to check. .PP \&\f(CW\*(C`min_length\*(C'\fR and \f(CW\*(C`max_length\*(C'\fR are the minimum and maximum length checks. \f(CW0\fR means no check. .PP The flags control: .ie n .IP """VALID_FLAG_ALPHA""" 4 .el .IP \f(CWVALID_FLAG_ALPHA\fR 4 .IX Item "VALID_FLAG_ALPHA" 7\-bit ASCII-only alphabetic characters are permitted. .ie n .IP """VALID_FLAG_DIGIT""" 4 .el .IP \f(CWVALID_FLAG_DIGIT\fR 4 .IX Item "VALID_FLAG_DIGIT" 7\-bit ASCII-only digits are permitted. .PP \&\f(CW\*(C`extra\*(C'\fR is a set of extra characters permitted, in addition to alphabetic and/or digits. (\f(CW\*(C`extra = NULL\*(C'\fR for no extra). .PP Returns boolean \f(CW\*(C`true\*(C'\fR if the string is valid (passes all the tests), or \f(CW\*(C`false\*(C'\fR if not. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_fadvise_normal\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_fadvise_normal" .PP .Vb 2 \& void \& guestfs_int_fadvise_normal (int fd) .Ve .PP Hint that we will read or write the file descriptor normally. .PP On Linux, this clears the \f(CW\*(C`FMODE_RANDOM\*(C'\fR flag on the file [see below] and sets the per-file number of readahead pages to equal the block device readahead setting. .PP It's OK to call this on a non-file since we ignore failure as it is only a hint. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_fadvise_sequential\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_fadvise_sequential" .PP .Vb 2 \& void \& guestfs_int_fadvise_sequential (int fd) .Ve .PP Hint that we will read or write the file descriptor sequentially. .PP On Linux, this clears the \f(CW\*(C`FMODE_RANDOM\*(C'\fR flag on the file [see below] and sets the per-file number of readahead pages to twice the block device readahead setting. .PP It's OK to call this on a non-file since we ignore failure as it is only a hint. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_fadvise_random\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_fadvise_random" .PP .Vb 2 \& void \& guestfs_int_fadvise_random (int fd) .Ve .PP Hint that we will read or write the file descriptor randomly. .PP On Linux, this sets the \f(CW\*(C`FMODE_RANDOM\*(C'\fR flag on the file. The effect of this flag is to: .IP \(bu 4 Disable normal sequential file readahead. .IP \(bu 4 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] .PP It's OK to call this on a non-file since we ignore failure as it is only a hint. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_fadvise_noreuse\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_fadvise_noreuse" .PP .Vb 2 \& void \& guestfs_int_fadvise_noreuse (int fd) .Ve .PP Hint that we will access the data only once. .PP On Linux, this does nothing. .PP It's OK to call this on a non-file since we ignore failure as it is only a hint. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_fadvise_dontneed\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_fadvise_dontneed" .PP .Vb 2 \& void \& guestfs_int_fadvise_dontneed (int fd) .Ve .PP Hint that we will not access the data in the near future. .PP 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. .PP It's OK to call this on a non-file since we ignore failure as it is only a hint. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_fadvise_willneed\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_fadvise_willneed" .PP .Vb 2 \& void \& guestfs_int_fadvise_willneed (int fd) .Ve .PP Hint that we will access the data in the near future. .PP 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. .PP It's OK to call this on a non-file since we ignore failure as it is only a hint. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_shell_unquote\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_shell_unquote" .PP .Vb 2 \& char * \& guestfs_int_shell_unquote (const char *str) .Ve .PP Unquote a shell-quoted string. .PP Augeas passes strings to us which may be quoted, eg. if they come from files in \fI/etc/sysconfig\fR. This function can do simple unquoting of these strings. .PP 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. .PP \&\f(CW\*(C`str\*(C'\fR 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. \f(CW\*(C`NULL\*(C'\fR is returned on error and \f(CW\*(C`errno\*(C'\fR is set accordingly. .PP For information on double-quoting in bash, see https://www.gnu.org/software/bash/manual/html_node/Double\-Quotes.html .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_is_reg\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_is_reg" .PP .Vb 2 \& int \& guestfs_int_is_reg (int64_t mode) .Ve .PP Return true if the \f(CW\*(C`guestfs_statns\*(C'\fR or \f(CW\*(C`guestfs_lstatns\*(C'\fR \&\f(CW\*(C`st_mode\*(C'\fR field represents a regular file. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_is_dir\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_is_dir" .PP .Vb 2 \& int \& guestfs_int_is_dir (int64_t mode) .Ve .PP Return true if the \f(CW\*(C`guestfs_statns\*(C'\fR or \f(CW\*(C`guestfs_lstatns\*(C'\fR \&\f(CW\*(C`st_mode\*(C'\fR field represents a directory. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_is_chr\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_is_chr" .PP .Vb 2 \& int \& guestfs_int_is_chr (int64_t mode) .Ve .PP Return true if the \f(CW\*(C`guestfs_statns\*(C'\fR or \f(CW\*(C`guestfs_lstatns\*(C'\fR \&\f(CW\*(C`st_mode\*(C'\fR field represents a char device. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_is_blk\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_is_blk" .PP .Vb 2 \& int \& guestfs_int_is_blk (int64_t mode) .Ve .PP Return true if the \f(CW\*(C`guestfs_statns\*(C'\fR or \f(CW\*(C`guestfs_lstatns\*(C'\fR \&\f(CW\*(C`st_mode\*(C'\fR field represents a block device. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_is_fifo\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_is_fifo" .PP .Vb 2 \& int \& guestfs_int_is_fifo (int64_t mode) .Ve .PP Return true if the \f(CW\*(C`guestfs_statns\*(C'\fR or \f(CW\*(C`guestfs_lstatns\*(C'\fR \&\f(CW\*(C`st_mode\*(C'\fR field represents a named pipe (FIFO). .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_is_lnk\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_is_lnk" .PP .Vb 2 \& int \& guestfs_int_is_lnk (int64_t mode) .Ve .PP Return true if the \f(CW\*(C`guestfs_statns\*(C'\fR or \f(CW\*(C`guestfs_lstatns\*(C'\fR \&\f(CW\*(C`st_mode\*(C'\fR field represents a symbolic link. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_is_sock\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_is_sock" .PP .Vb 2 \& int \& guestfs_int_is_sock (int64_t mode) .Ve .PP Return true if the \f(CW\*(C`guestfs_statns\*(C'\fR or \f(CW\*(C`guestfs_lstatns\*(C'\fR \&\f(CW\*(C`st_mode\*(C'\fR field represents a Unix domain socket. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_full_path\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_full_path" .PP .Vb 2 \& char * \& guestfs_int_full_path (const char *dir, const char *name) .Ve .PP Concatenate \f(CW\*(C`dir\*(C'\fR and \f(CW\*(C`name\*(C'\fR to create a path. This correctly handles the case of concatenating \f(CW"/" + "filename"\fR as well as \f(CW"/dir" + "filename"\fR. \f(CW\*(C`name\*(C'\fR may be \f(CW\*(C`NULL\*(C'\fR. .PP The caller must free the returned path. .PP This function sets \f(CW\*(C`errno\*(C'\fR and returns \f(CW\*(C`NULL\*(C'\fR on error. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_hexdump\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_hexdump" .PP .Vb 2 \& void \& guestfs_int_hexdump (const void *data, size_t len, FILE *fp) .Ve .PP Hexdump a block of memory to \f(CW\*(C`FILE *\*(C'\fR, used for debugging. .PP Function \f(CW\*(C`common/utils/utils.c:guestfs_int_strerror\*(C'\fR .IX Subsection "Function common/utils/utils.c:guestfs_int_strerror" .PP .Vb 2 \& const char * \& guestfs_int_strerror (int errnum, char *buf, size_t buflen) .Ve .PP Thread-safe strerror_r. .PP This is a wrapper around the two variants of \fBstrerror_r\fR\|(3) in glibc since it is hard to use correctly (RHBZ#2030396). .PP The buffer passed in should be large enough to store the error message (256 chars at least) and should be non-static. Note that the buffer might not be used, use the return value. .SS "Subdirectory \fIcommon/visit\fP" .IX Subsection "Subdirectory common/visit" \fIFile common/visit/visit.c\fR .IX Subsection "File common/visit/visit.c" .PP This file contains a recursive function for visiting all files and directories in a guestfs filesystem. .PP Adapted from https://rwmj.wordpress.com/2010/12/15/tip\-audit\-virtual\-machine\-for\-setuid\-files/ .PP Function \f(CW\*(C`common/visit/visit.c:visit\*(C'\fR .IX Subsection "Function common/visit/visit.c:visit" .PP .Vb 2 \& int \& visit (guestfs_h *g, const char *dir, visitor_function f, void *opaque) .Ve .PP Visit every file and directory in a guestfs filesystem, starting at \f(CW\*(C`dir\*(C'\fR. .PP \&\f(CW\*(C`dir\*(C'\fR may be \f(CW"/"\fR to visit the entire filesystem, or may be some subdirectory. Symbolic links are not followed. .PP The visitor function \f(CW\*(C`f\*(C'\fR is called once for every directory and every file. The parameters passed to \f(CW\*(C`f\*(C'\fR include the current directory name, the current file name (or \f(CW\*(C`NULL\*(C'\fR when we're visiting a directory), the \f(CW\*(C`guestfs_statns\*(C'\fR (file permissions etc), and the list of extended attributes of the file. The visitor function may return \f(CW\-1\fR which causes the whole recursion to stop with an error. .PP Also passed to this function is an \f(CW\*(C`opaque\*(C'\fR pointer which is passed through to the visitor function. .PP Returns \f(CW0\fR if everything went OK, or \f(CW\-1\fR 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. .SS "Subdirectory \fIcommon/windows\fP" .IX Subsection "Subdirectory common/windows" \fIFile common/windows/windows.c\fR .IX Subsection "File common/windows/windows.c" .PP This file implements \f(CW\*(C`win:\*(C'\fR Windows file path support in \&\fBguestfish\fR\|(1). .PP Function \f(CW\*(C`common/windows/windows.c:is_windows\*(C'\fR .IX Subsection "Function common/windows/windows.c:is_windows" .PP .Vb 2 \& int \& is_windows (guestfs_h *g, const char *root) .Ve .PP Checks whether \f(CW\*(C`root\*(C'\fR is a Windows installation. .PP This relies on an already being done introspection. .PP Function \f(CW\*(C`common/windows/windows.c:windows_path\*(C'\fR .IX Subsection "Function common/windows/windows.c:windows_path" .PP .Vb 2 \& char * \& windows_path (guestfs_h *g, const char *root, const char *path, int readonly) .Ve .PP Resolves \f(CW\*(C`path\*(C'\fR as possible Windows path according to \f(CW\*(C`root\*(C'\fR, giving a new path that can be used in libguestfs API calls. .PP Notes: .IP \(bu 4 \&\f(CW\*(C`root\*(C'\fR must be a Windows installation .IP \(bu 4 relies on an already being done introspection .IP \(bu 4 will unmount all the existing mount points and mount the Windows root (according to \f(CW\*(C`readonly\*(C'\fR) .IP \(bu 4 calls \fBexit\fR\|(3) on memory allocation failures .SS "Subdirectory \fIdaemon\fP" .IX Subsection "Subdirectory daemon" \fIFile daemon/command.c\fR .IX Subsection "File daemon/command.c" .PP This file contains a number of useful functions for running external commands and capturing their output. .PP Function \f(CW\*(C`daemon/command.c:commandf\*(C'\fR .IX Subsection "Function daemon/command.c:commandf" .PP .Vb 3 \& int \& commandf (char **stdoutput, char **stderror, unsigned flags, \& const char *name, ...) .Ve .PP Run a command. Optionally capture stdout and stderr as strings. .PP Returns \f(CW0\fR if the command ran successfully, or \f(CW\-1\fR if there was any error. .PP For a description of the \f(CW\*(C`flags\*(C'\fR see \f(CW\*(C`commandrvf\*(C'\fR. .PP There is also a macro \f(CW\*(C`command(out,err,name,...)\*(C'\fR which calls \&\f(CW\*(C`commandf\*(C'\fR with \f(CW\*(C`flags=0\*(C'\fR. .PP Function \f(CW\*(C`daemon/command.c:commandrf\*(C'\fR .IX Subsection "Function daemon/command.c:commandrf" .PP .Vb 3 \& int \& commandrf (char **stdoutput, char **stderror, unsigned flags, \& const char *name, ...) .Ve .PP Same as \f(CW\*(C`command\*(C'\fR, but we allow the status code from the subcommand to be non-zero, and return that status code. .PP We still return \f(CW\-1\fR if there was some other error. .PP There is also a macro \f(CW\*(C`commandr(out,err,name,...)\*(C'\fR which calls \&\f(CW\*(C`commandrf\*(C'\fR with \f(CW\*(C`flags=0\*(C'\fR. .PP Function \f(CW\*(C`daemon/command.c:commandvf\*(C'\fR .IX Subsection "Function daemon/command.c:commandvf" .PP .Vb 3 \& int \& commandvf (char **stdoutput, char **stderror, unsigned flags, \& char const *const *argv) .Ve .PP Same as \f(CW\*(C`command\*(C'\fR, but passing in an argv array. .PP There is also a macro \f(CW\*(C`commandv(out,err,argv)\*(C'\fR which calls \&\f(CW\*(C`commandvf\*(C'\fR with \f(CW\*(C`flags=0\*(C'\fR. .PP Function \f(CW\*(C`daemon/command.c:commandrvf\*(C'\fR .IX Subsection "Function daemon/command.c:commandrvf" .PP .Vb 3 \& int \& commandrvf (char **stdoutput, char **stderror, unsigned flags, \& char const* const *argv) .Ve .PP This is a more sane version of \fBsystem\fR\|(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. .PP If \f(CW\*(C`stdoutput\*(C'\fR is not \f(CW\*(C`NULL\*(C'\fR, then \f(CW*stdoutput\fR will return the stdout of the command as a string. .PP If \f(CW\*(C`stderror\*(C'\fR is not \f(CW\*(C`NULL\*(C'\fR, then \f(CW*stderror\fR will return the stderr of the command. If there is a final \en character, it is removed so you can use the error string directly in a call to \&\f(CW\*(C`reply_with_error\*(C'\fR. .PP Flags are: .ie n .IP """COMMAND_FLAG_FOLD_STDOUT_ON_STDERR""" 4 .el .IP \f(CWCOMMAND_FLAG_FOLD_STDOUT_ON_STDERR\fR 4 .IX Item "COMMAND_FLAG_FOLD_STDOUT_ON_STDERR" 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 \f(CW*stderror\fR buffer. If using this flag, you should pass \f(CW\*(C`stdoutput=NULL\*(C'\fR because nothing could ever be captured in that buffer. .ie n .IP """COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN""" 4 .el .IP \f(CWCOMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN\fR 4 .IX Item "COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN" 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 \fIdaemon/hexdump.c\fR for an example of usage. .PP There is also a macro \f(CW\*(C`commandrv(out,err,argv)\*(C'\fR which calls \&\f(CW\*(C`commandrvf\*(C'\fR with \f(CW\*(C`flags=0\*(C'\fR. .PP \fIFile daemon/device\-name\-translation.c\fR .IX Subsection "File daemon/device-name-translation.c" .PP Function \f(CW\*(C`daemon/device\-name\-translation.c:device_name_translation_init\*(C'\fR .IX Subsection "Function daemon/device-name-translation.c:device_name_translation_init" .PP .Vb 2 \& void \& device_name_translation_init (void) .Ve .PP Cache daemon disk mapping. .PP When the daemon starts up, populate a cache with the contents of /dev/disk/by\-path. It's easiest to use \f(CW\*(C`ls \-lv\*(C'\fR here since the names are sorted awkwardly. .PP Function \f(CW\*(C`daemon/device\-name\-translation.c:device_name_translation\*(C'\fR .IX Subsection "Function daemon/device-name-translation.c:device_name_translation" .PP .Vb 2 \& char * \& device_name_translation (const char *device) .Ve .PP Perform device name translation. .PP Libguestfs defines a few standard formats for device names. (see also "BLOCK DEVICE NAMING" in \fBguestfs\fR\|(3) and "guestfs_canonical_device_name" in \fBguestfs\fR\|(3)). They are: .IP \fI/dev/sdX[N]\fR 4 .IX Item "/dev/sdX[N]" .PD 0 .IP \fI/dev/hdX[N]\fR 4 .IX Item "/dev/hdX[N]" .IP \fI/dev/vdX[N]\fR 4 .IX Item "/dev/vdX[N]" .PD These mean the Nth partition on the Xth device. Because Linux no longer enumerates devices in the order they are passed to qemu, we must translate these by looking up the actual device using /dev/disk/by\-path/ .IP \fI/dev/mdX\fR 4 .IX Item "/dev/mdX" .PD 0 .IP \fI/dev/VG/LV\fR 4 .IX Item "/dev/VG/LV" .IP \fI/dev/mapper/...\fR 4 .IX Item "/dev/mapper/..." .IP \fI/dev/dm\-N\fR 4 .IX Item "/dev/dm-N" .PD These are not translated here. .PP It returns a newly allocated string which the caller must free. .PP It returns \f(CW\*(C`NULL\*(C'\fR on error. \fBNote\fR it does \fInot\fR call \&\f(CW\*(C`reply_with_*\*(C'\fR. .PP We have to open the device and test for \f(CW\*(C`ENXIO\*(C'\fR, because the device nodes may exist in the appliance. .PP \fIFile daemon/guestfsd.c\fR .IX Subsection "File daemon/guestfsd.c" .PP This is the guestfs daemon which runs inside the guestfs appliance. This file handles start up and connecting back to the library. .PP Function \f(CW\*(C`daemon/guestfsd.c:shell_quote\*(C'\fR .IX Subsection "Function daemon/guestfsd.c:shell_quote" .PP .Vb 2 \& void \& shell_quote (const char *str, FILE *fp) .Ve .PP Write \f(CW\*(C`str\*(C'\fR to the file \f(CW\*(C`fp\*(C'\fR, ensuring it is shell quoted. .PP Function \f(CW\*(C`daemon/guestfsd.c:sysroot_shell_quote\*(C'\fR .IX Subsection "Function daemon/guestfsd.c:sysroot_shell_quote" .PP .Vb 2 \& void \& sysroot_shell_quote (const char *path, FILE *fp) .Ve .PP Write \f(CW\*(C`sysroot\*(C'\fR + \f(CW\*(C`path\*(C'\fR to the file \f(CW\*(C`fp\*(C'\fR, ensuring it is shell quoted. The path must be an absolute path. .PP \fIFile daemon/internal.c\fR .IX Subsection "File daemon/internal.c" .PP Internal functions that are not part of the public API. .PP \fIFile daemon/utils\-c.c\fR .IX Subsection "File daemon/utils-c.c" .PP Bindings for utility functions. .PP Note that functions called from OCaml code \fBmust never\fR call any of the \f(CW\*(C`reply*\*(C'\fR functions. .PP \fIFile daemon/utils.c\fR .IX Subsection "File daemon/utils.c" .PP Miscellaneous utility functions used by the daemon. .PP Function \f(CW\*(C`daemon/utils.c:is_root_device_stat\*(C'\fR .IX Subsection "Function daemon/utils.c:is_root_device_stat" .PP .Vb 2 \& static int \& is_root_device_stat (struct stat *statbuf) .Ve .PP Return true iff device is the root device (and therefore should be ignored from the point of view of user calls). .PP Function \f(CW\*(C`daemon/utils.c:is_device_parameter\*(C'\fR .IX Subsection "Function daemon/utils.c:is_device_parameter" .PP .Vb 2 \& int \& is_device_parameter (const char *device) .Ve .PP Parameters marked as \f(CW\*(C`Device\*(C'\fR, \f(CW\*(C`Dev_or_Path\*(C'\fR, etc can be passed a block device name. This function tests if the parameter is a block device name. .PP It can also be used in daemon code to test if the string passed as a \f(CW\*(C`Dev_or_Path\*(C'\fR parameter is a device or path. .PP Function \f(CW\*(C`daemon/utils.c:sysroot_path\*(C'\fR .IX Subsection "Function daemon/utils.c:sysroot_path" .PP .Vb 2 \& char * \& sysroot_path (const char *path) .Ve .PP Turn \f(CW"/path"\fR into \f(CW"/sysroot/path"\fR. .PP Returns \f(CW\*(C`NULL\*(C'\fR on failure. The caller \fImust\fR check for this and call \f(CW\*(C`reply_with_perror\ ("malloc")\*(C'\fR. The caller must also free the returned string. .PP See also the custom \f(CW%R\fR printf formatter which does shell quoting too. .PP Function \f(CW\*(C`daemon/utils.c:sysroot_realpath\*(C'\fR .IX Subsection "Function daemon/utils.c:sysroot_realpath" .PP .Vb 2 \& char * \& sysroot_realpath (const char *path) .Ve .PP Resolve path within sysroot, calling \f(CW\*(C`sysroot_path\*(C'\fR on the resolved path. .PP Returns \f(CW\*(C`NULL\*(C'\fR on failure. The caller \fImust\fR check for this and call \f(CW\*(C`reply_with_perror\ ("malloc")\*(C'\fR. The caller must also free the returned string. .PP See also the custom \f(CW%R\fR printf formatter which does shell quoting too. .PP Function \f(CW\*(C`daemon/utils.c:is_power_of_2\*(C'\fR .IX Subsection "Function daemon/utils.c:is_power_of_2" .PP .Vb 2 \& int \& is_power_of_2 (unsigned long v) .Ve .PP Returns true if \f(CW\*(C`v\*(C'\fR is a power of 2. .PP Uses the algorithm described at http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2 .PP Function \f(CW\*(C`daemon/utils.c:split_lines_sb\*(C'\fR .IX Subsection "Function daemon/utils.c:split_lines_sb" .PP .Vb 2 \& struct stringsbuf \& split_lines_sb (char *str) .Ve .PP Split an output string into a NULL-terminated list of lines, wrapped into a stringsbuf. .PP 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. .PP The corner cases here are quite tricky. Note in particular: .ie n .IP """""" 4 .el .IP "\f(CW""""\fR" 4 .IX Item """""" returns \f(CW\*(C`[]\*(C'\fR .ie n .IP """\en""" 4 .el .IP "\f(CW""\en""\fR" 4 .IX Item """n""" returns \f(CW\*(C`[""]\*(C'\fR .ie n .IP """a\enb""" 4 .el .IP "\f(CW""a\enb""\fR" 4 .IX Item """anb""" returns \f(CW\*(C`["a"; "b"]\*(C'\fR .ie n .IP """a\enb\en""" 4 .el .IP "\f(CW""a\enb\en""\fR" 4 .IX Item """anbn""" returns \f(CW\*(C`["a"; "b"]\*(C'\fR .ie n .IP """a\enb\en\en""" 4 .el .IP "\f(CW""a\enb\en\en""\fR" 4 .IX Item """anbnn""" returns \f(CW\*(C`["a"; "b"; ""]\*(C'\fR .PP The original string is written over and destroyed by this function (which is usually OK because it's the 'out' string from \&\f(CW\*(C`command*()\*(C'\fR). You can free the original string, because \&\f(CWadd_string()\fR strdups the strings. .PP \&\f(CW\*(C`argv\*(C'\fR in the \f(CW\*(C`struct stringsbuf\*(C'\fR will be \f(CW\*(C`NULL\*(C'\fR in case of errors. .PP Function \f(CW\*(C`daemon/utils.c:filter_list\*(C'\fR .IX Subsection "Function daemon/utils.c:filter_list" .PP .Vb 2 \& char ** \& filter_list (bool (*p) (const char *str), char **strs) .Ve .PP Filter a list of strings. Returns a newly allocated list of only the strings where \f(CW\*(C`p (str) == true\*(C'\fR. .PP \&\fBNote\fR it does not copy the strings, be careful not to double-free them. .PP Function \f(CW\*(C`daemon/utils.c:trim\*(C'\fR .IX Subsection "Function daemon/utils.c:trim" .PP .Vb 2 \& void \& trim (char *str) .Ve .PP Skip leading and trailing whitespace, updating the original string in-place. .PP Function \f(CW\*(C`daemon/utils.c:parse_btrfsvol\*(C'\fR .IX Subsection "Function daemon/utils.c:parse_btrfsvol" .PP .Vb 2 \& int \& parse_btrfsvol (const char *desc_orig, mountable_t *mountable) .Ve .PP Parse the mountable descriptor for a btrfs subvolume. Don't call this directly; it is only used from the stubs. .PP A btrfs subvolume is given as: .PP .Vb 1 \& btrfsvol:/dev/sda3/root .Ve .PP where \fI/dev/sda3\fR 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 \f(CW"btrfsvol:"\fR. .PP On success, \f(CW\*(C`mountable\->device\*(C'\fR and \f(CW\*(C`mountable\->volume\*(C'\fR must be freed by the caller. .PP Function \f(CW\*(C`daemon/utils.c:mountable_to_string\*(C'\fR .IX Subsection "Function daemon/utils.c:mountable_to_string" .PP .Vb 2 \& char * \& mountable_to_string (const mountable_t *mountable) .Ve .PP Convert a \f(CW\*(C`mountable_t\*(C'\fR back to its string representation .PP This function can be used in an error path, so must not call \&\f(CW\*(C`reply_with_error\*(C'\fR. .PP Function \f(CW\*(C`daemon/utils.c:prog_exists\*(C'\fR .IX Subsection "Function daemon/utils.c:prog_exists" .PP .Vb 2 \& int \& prog_exists (const char *prog) .Ve .PP Check program exists and is executable on \f(CW$PATH\fR. .PP Function \f(CW\*(C`daemon/utils.c:random_name\*(C'\fR .IX Subsection "Function daemon/utils.c:random_name" .PP .Vb 2 \& int \& random_name (char *template) .Ve .PP Pass a template such as \f(CW"/sysroot/XXXXXXXX.XXX"\fR. This updates the template to contain a randomly named file. Any \f(CW\*(AqX\*(Aq\fR characters after the final \f(CW\*(Aq/\*(Aq\fR in the template are replaced with random characters. .PP 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. .PP 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). .PP If there is an error, \f(CW\-1\fR is returned. .PP Function \f(CW\*(C`daemon/utils.c:udev_settle_file\*(C'\fR .IX Subsection "Function daemon/utils.c:udev_settle_file" .PP .Vb 2 \& void \& udev_settle_file (const char *file) .Ve .PP LVM and other commands aren't synchronous, especially when udev is involved. eg. You can create or remove some device, but the \&\f(CW\*(C`/dev\*(C'\fR device node won't appear until some time later. This means that you get an error if you run one command followed by another. .PP Use \f(CW\*(C`udevadm settle\*(C'\fR after certain commands, but don't be too fussed if it fails. .PP Function \f(CW\*(C`daemon/utils.c:make_exclude_from_file\*(C'\fR .IX Subsection "Function daemon/utils.c:make_exclude_from_file" .PP .Vb 2 \& char * \& make_exclude_from_file (const char *function, char *const *excludes) .Ve .PP Turn list \f(CW\*(C`excludes\*(C'\fR into a temporary file, and return a string containing the temporary file name. Caller must unlink the file and free the string. .PP \&\f(CW\*(C`function\*(C'\fR is the function that invoked this helper, and it is used mainly for errors/debugging. .PP Function \f(CW\*(C`daemon/utils.c:read_whole_file\*(C'\fR .IX Subsection "Function daemon/utils.c:read_whole_file" .PP .Vb 2 \& char * \& read_whole_file (const char *filename, size_t *size_r) .Ve .PP Read whole file into dynamically allocated array. If there is an error, DON'T call reply_with_perror, just return NULL. Returns a \&\f(CW\*(C`\e0\*(C'\fR\-terminated string. \f(CW\*(C`size_r\*(C'\fR can be specified to get the size of the returned data. .PP \fIFile daemon/xattr.c\fR .IX Subsection "File daemon/xattr.c" .PP Function \f(CW\*(C`daemon/xattr.c:split_attr_names\*(C'\fR .IX Subsection "Function daemon/xattr.c:split_attr_names" .PP .Vb 2 \& static char ** \& split_attr_names (char *buf, size_t len) .Ve .PP \&\fBlistxattr\fR\|(2) returns the string \f(CW"foo\e0bar\e0baz"\fR of length \&\f(CW\*(C`len\*(C'\fR. (The last string in the list is \e0\-terminated but the \e0 is not included in \f(CW\*(C`len\*(C'\fR). .PP This function splits it into a regular list of strings. .PP \&\fBNote\fR that the returned list contains pointers to the original strings in \f(CW\*(C`buf\*(C'\fR so be careful that you do not double-free them. .SS "Subdirectory \fIfish\fP" .IX Subsection "Subdirectory fish" \fIFile fish/alloc.c\fR .IX Subsection "File fish/alloc.c" .PP This file implements the guestfish \f(CW\*(C`alloc\*(C'\fR and \f(CW\*(C`sparse\*(C'\fR commands. .PP Function \f(CW\*(C`fish/alloc.c:alloc_disk\*(C'\fR .IX Subsection "Function fish/alloc.c:alloc_disk" .PP .Vb 2 \& int \& alloc_disk (const char *filename, const char *size_str, int add, int sparse) .Ve .PP This is the underlying allocation function. It's called from a few other places in guestfish. .PP \fIFile fish/copy.c\fR .IX Subsection "File fish/copy.c" .PP This file implements the guestfish commands \f(CW\*(C`copy\-in\*(C'\fR and \&\f(CW\*(C`copy\-out\*(C'\fR. .PP \fIFile fish/destpaths.c\fR .IX Subsection "File fish/destpaths.c" .PP The file handles tab-completion of filesystem paths in guestfish. .PP \fIFile fish/display.c\fR .IX Subsection "File fish/display.c" .PP The file implements the guestfish \f(CW\*(C`display\*(C'\fR command, for displaying graphical files (icons, images) in disk images. .PP \fIFile fish/echo.c\fR .IX Subsection "File fish/echo.c" .PP The file implements the guestfish \f(CW\*(C`echo\*(C'\fR command. .PP \fIFile fish/edit.c\fR .IX Subsection "File fish/edit.c" .PP guestfish \f(CW\*(C`edit\*(C'\fR command, suggested by Ján Ondrej. .PP \fIFile fish/events.c\fR .IX Subsection "File fish/events.c" .PP This file implements the guestfish event-related commands, \&\f(CW\*(C`event\*(C'\fR, \f(CW\*(C`delete\-event\*(C'\fR and \f(CW\*(C`list\-events\*(C'\fR. .PP \fIFile fish/fish.c\fR .IX Subsection "File fish/fish.c" .PP guestfish, the guest filesystem shell. This file contains the main loop and utilities. .PP Function \f(CW\*(C`fish/fish.c:parse_command_line\*(C'\fR .IX Subsection "Function fish/fish.c:parse_command_line" .PP .Vb 2 \& static struct parsed_command \& parse_command_line (char *buf, int *exit_on_error_rtn) .Ve .PP Parse a command string, splitting at whitespace, handling \f(CW\*(Aq!\*(Aq\fR, \&\f(CW\*(Aq#\*(Aq\fR etc. This destructively updates \f(CW\*(C`buf\*(C'\fR. .PP \&\f(CW\*(C`exit_on_error_rtn\*(C'\fR is used to pass in the global \f(CW\*(C`exit_on_error\*(C'\fR setting and to return the local setting (eg. if the command begins with \f(CW\*(Aq\-\*(Aq\fR). .PP Returns in \f(CW\*(C`parsed_command.status\*(C'\fR: .ie n .IP 1 4 .el .IP \f(CW1\fR 4 .IX Item "1" got a guestfish command (returned in \f(CW\*(C`cmd_rtn\*(C'\fR/\f(CW\*(C`argv_rtn\*(C'\fR/\f(CW\*(C`pipe_rtn\*(C'\fR) .ie n .IP 0 4 .el .IP \f(CW0\fR 4 .IX Item "0" no guestfish command, but otherwise OK .ie n .IP \-1 4 .el .IP \f(CW\-1\fR 4 .IX Item "-1" an error .PP Function \f(CW\*(C`fish/fish.c:parse_quoted_string\*(C'\fR .IX Subsection "Function fish/fish.c:parse_quoted_string" .PP .Vb 2 \& static ssize_t \& parse_quoted_string (char *p) .Ve .PP 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. .PP Function \f(CW\*(C`fish/fish.c:execute_and_inline\*(C'\fR .IX Subsection "Function fish/fish.c:execute_and_inline" .PP .Vb 2 \& static int \& execute_and_inline (const char *cmd, int global_exit_on_error) .Ve .PP Used to handle \f(CW\*(C`