=head1 NAME

guestfs-building - How to build libguestfs from source

=head1 DESCRIPTION

This manual page describes how to build libguestfs from source.

The main steps are:

=over 4

=item *

Install the requirements.

=item *

Build, either from the git repository or from a tarball.

=item *

Run the tests.

=item *

Run the tools from the source directory, or install.

=back

=head1 REQUIREMENTS

=head2 Short cut for Fedora or Red Hat Enterprise Linux (RHEL) users

On Fedora, use L<dnf(8)> to install all the requirements:

 dnf builddep libguestfs
 dnf install autoconf automake libtool gettext-devel

On systems still using L<yum(8)>, do:

 yum-builddep libguestfs
 yum install autoconf automake libtool gettext-devel

=head2 Short cut for Debian or Ubuntu users

Use APT to install all the requirements:

 apt-get build-dep libguestfs
 apt-get install autoconf automake libtool-bin gettext

If that command doesn't work, take a look at the Debian source package
L<http://packages.debian.org/source/libguestfs>, at the list of
C<build-depends> and C<build-depends-indep>, and install everything
listed there.

=head2 Full list of requirements

=over 4

=item F<appliance/packagelist.in>

Install as many package names found in this file as possible.  (It is
not strictly required to install all of them).

I<Note>: If you build libguestfs followed by installing appliance
packages, the build will not pick them up automatically, even if you
do S<C<make clean>>.  You have to do this command to clean the old
supermin appliance and force a new one to be prepared:

 make -C appliance clean-supermin-appliance

=item qemu E<ge> 1.3.0

I<Required>.

=item qemu-img E<ge> 1.3.0

I<Required>.

=item kernel E<ge> 2.6.34

I<Required>.  The following features must be enabled:
C<virtio-pci>, C<virtio-serial>, C<virtio-block>, C<virtio-net>.

=item supermin E<ge> 5.1.18

I<Required>.  For alternatives, see L</USING A PREBUILT BINARY APPLIANCE>
below.

=item glibc

I<Required>.  We use the custom printf formatters extension of
glibc (see L<guestfs-hacking(1)/DAEMON CUSTOM PRINTF FORMATTERS>).

=item XDR (tirpc, glibc or other)

I<Required>.  We use the XDR implementation from
C<E<lt>rpc/xdr.hE<gt>>, which may come from glibc, tirpc or another
library.

The C<rpcgen> tool is optional, except if you want to compile from git
and/or patch libguestfs with new APIs.

=item Gcc or Clang

I<Required>.  We use C<__attribute__((cleanup))> which is a GCC
extension also supported by Clang.

=item Perl

I<Required>.  Various build steps and tests are written in Perl.  Perl
is not needed at runtime except if you need to run a small number of
virt tools which are still written in Perl.

=item Perl C<Pod::Man>

=item Perl C<Pod::Simple>

I<Required>.  Part of Perl core.

=item OCaml E<ge> 4.07

=item OCaml findlib

I<Required>.

=item autoconf

=item automake

=item gettext

I<Required> if compiling from git.
Optional if compiling from tarball.

=item cpio

I<Required>.

=item gperf

I<Required>.

=item realpath

I<Required>.

=item flex

=item bison

I<Required>.

=item Perl-compatible Regular Expressions (PCRE2) library

I<Required>.

=item xorriso, genisoimage or mkisofs

One of these is I<Required>.

=item libxml2

I<Required>.

=item ncurses

I<Required>.

=item augeas E<ge> 1.2.0

I<Required>.

=item ocaml-augeas

I<Required>.  These are the OCaml bindings for Augeas, found at:
L<http://people.redhat.com/~rjones/augeas/>

=item xz

I<Required>.

=item zstd

I<Required>.

=item Jansson E<ge> 2.7

I<Required>.

=item po4a

I<Required> if compiling from git.
Optional if compiling from tarball.

=item hivex E<ge> 1.2.7

=item ocaml-hivex

I<Required>.  ocaml-hivex is the OCaml binding for hivex, which is
required when building the daemon.

=item libmagic

I<Required>.  This is the library used by the L<file(1)> command.

=item libvirt E<ge> 0.10.2

Optional.  Always use the latest possible version of libvirt.

=item xmllint

Optional.  Used only for tests.

=item libconfig

Optional.  Used to parse libguestfs’s own config files,
eg. F</etc/libguestfs-tools.conf>.

=item libselinux

Optional.  Used by the L<libvirt backend|guestfs(3)/BACKEND> to
securely confine the appliance (sVirt).

=item readline

Optional.  For nicer command line editing in L<guestfish(1)>.

=item acl

Optional.  Library and programs for handling POSIX ACLs.

=item libcap

Optional.  Library and programs for handling Linux capabilities.

=item libldm

Optional.  Library and L<ldmtool(1)> for handling
Windows Dynamic Disks.

=item sd-journal

Optional.  Library for accessing systemd journals.

=item gdisk

Optional.  GPT disk support.

=item netpbm

Optional.  Render icons from guests.

=item icoutils

Optional.  Render icons from Windows guests.

=item librpm

Optional.  To parse the list of applications from RPM-based guests.

=item Perl C<Expect>

Optional.  Perl module used to test L<virt-rescue(1)>.

=item FUSE

Optional.  L<fusermount(1)>, libfuse and kernel module are all needed
if you want L<guestmount(1)> and/or L<mount-local|guestfs(3)/MOUNT LOCAL>
support.

=item static glibc

Optional.  Used only for testing.

=item qemu-nbd

=item nbdkit E<ge> 1.12

Optional.  qemu-nbd is used for testing.

=item curl

Optional.  Used by virt-builder for downloads.

=item GNU Privacy Guard (GnuPG, gpg) v1 or v2

Optional.  Used by virt-builder for checking digital signatures.

=item liblzma

Optional.  If available, virt-builder will use this library
for fast, parallel uncompression of templates.

=item python-evtx

Optional.  Used by L<virt-log(1)> to parse Windows Event Log files.

=item OCaml gettext

Optional.  For localizing OCaml virt tools.

=item ocaml-ounit E<ge> 2.0.0

Optional.  For testing the common OCaml modules.

=item Perl C<Module::Build> E<ge> 0.19

=item Perl C<Test::More>

Optional.  Used to build and test the Perl bindings.

=item Python E<ge> 3.6

Optional.  Used to build the Python bindings.
S<Python 2> support was removed in S<libguestfs 1.42.1>.

=item Python C<unittest>

Optional.  Used to run the Python testsuite.

=item Ruby

=item rake

=item rubygem-minitest

=item rubygem-rdoc

Optional.  Used to build the Ruby bindings.

=item Java E<ge> 1.6

Optional.  Java, JNI and jpackage-utils are needed for building Java
bindings.

=item GHC

Optional.  Used to build the Haskell bindings.

=item PHP

=item phpize

Optional.  Used to build the PHP bindings.

=item glib2

=item gobject-introspection

=item gjs

Optional.  Used to build and test the GObject bindings.

=item vala

Optional.  Used to build the Vala bindings.

=item LUA

Optional.  Used to build the LUA bindings.

=item Erlang E<ge> 23

=item ei

Optional.  Used to build the Erlang bindings.  Note that Erlang E<le>
22 will not work unless you use libguestfs E<le> 1.42.

=item golang E<ge> 1.1.1

Optional.  Used to build the Go bindings.

=item valgrind

Optional.  For testing memory problems.

=item libvirt-python

Optional.  For testing Python libvirt/libguestfs interactions.

=item Perl C<libintl>

Optional.

=item bash-completion

Optional.  For tab-completion of commands in bash.

=item libtsk

Optional.  Library for filesystem forensics analysis.

=item yara E<ge> 4.0.0

Optional.  Tool for categorizing files based on their content.

=back

=head1 BUILDING FROM GIT

You will need to install additional dependencies C<autoconf>,
C<automake>, C<gettext>, OCaml findlib and po4a when building from
git.

 git clone https://github.com/libguestfs/libguestfs
 cd libguestfs
 git submodule update --init
 autoreconf -i
 ./configure CFLAGS=-fPIC
 make

=head1 BUILDING FROM TARBALLS

Tarballs are downloaded from L<http://download.libguestfs.org/>.
Stable tarballs are signed with the GnuPG key for C<rich@annexia.org>,
see
L<https://pgp.mit.edu/pks/lookup?op=vindex&search=0x91738F73E1B768A0>.
The fingerprint is C<F777 4FB1 AD07 4A7E 8C87 67EA 9173 8F73 E1B7 68A0>.

Download and unpack the tarball.

 cd libguestfs-1.xx.yy
 ./configure
 make

=head1 RUNNING THE TESTS

B<DO NOT run the tests as root!>  Libguestfs can be built and tested as
non-root.  Running the tests as root could even be dangerous, don't do
it.

To sanity check that the build worked, do:

 make quickcheck

To run the basic tests, do:

 make check

There are many more tests you can run.  See L<guestfs-hacking(1)>
for details.

=head1 INSTALLING

B<DO NOT use C<make install>!>  You'll end up with conflicting versions
of libguestfs installed, and this causes constant headaches for users.
See the next section for how to use the F<./run> script instead.

Distro packagers can use:

 make INSTALLDIRS=vendor DESTDIR=[temp-build-dir] install

=head1 THE ./run SCRIPT

You can run L<guestfish(1)>, L<guestmount(1)> and the virt tools
without needing to install them by using the F<./run> script in the
top directory.  This script works by setting several environment
variables.

For example:

 ./run guestfish [usual guestfish args ...]

 ./run virt-inspector [usual virt-inspector args ...]

The F<./run> script adds every libguestfs binary to the C<$PATH>, so
the above examples run guestfish and virt-inspector from the build
directory (not the globally installed guestfish if there is one).

You can use the script from any directory.  If you wanted to run your
own libguestfs-using program, then the following command will also
work:

 /path/to/libguestfs/run ./my_program [...]

You can also run the C programs under valgrind like this:

 ./run valgrind [valgrind opts...] virt-cat [virt-cat opts...]

or under gdb:

 ./run gdb --args virt-cat [virt-cat opts...]

This also works with sudo (eg. if you need root access for libvirt or
to access a block device):

 sudo ./run virt-cat -d LinuxGuest /etc/passwd

To set environment variables, you can either do:

 LIBGUESTFS_HV=/my/qemu ./run guestfish

or:

 ./run env LIBGUESTFS_HV=/my/qemu guestfish

=head1 F<local*> FILES

Files in the top source directory that begin with the prefix F<local*>
are ignored by git.  These files can contain local configuration or
scripts that you need to build libguestfs.

I have a file called F<localconfigure> which is a simple wrapper
around F<configure> containing local configure customizations that I
need.  It looks like this:

 . localenv
 ./configure.sh \
     -C \
     --enable-werror \
     "$@"

So I can use this to build libguestfs:

 ./localconfigure && make

If there is a file in the top build directory called F<localenv>, then
it will be sourced by C<make>.  This file can contain any local
environment variables needed, eg. for skipping tests:

 # Skip this test, it is broken.
 export SKIP_TEST_BTRFS_FSCK=1

Note that F<localenv> is included by the top Makefile (so it’s a
Makefile fragment).  But if it is also sourced by your
F<localconfigure> script then it is used as a shell script.

=head1 SELECTED ./configure SETTINGS

There are many C<./configure> options.  Use:

 ./configure --help

to list them all.  This section covers some of the more important
ones.

=over 4

=item B<--disable-appliance --disable-daemon>

See L</USING A PREBUILT BINARY APPLIANCE> below.

=item B<--disable-erlang>

=item B<--disable-gobject>

=item B<--disable-golang>

=item B<--disable-haskell>

=item B<--disable-lua>

=item B<--disable-ocaml>

=item B<--disable-perl>

=item B<--disable-php>

=item B<--disable-python>

=item B<--disable-ruby>

Disable specific language bindings, even if C<./configure> finds all
the necessary libraries are installed so that they could be compiled.

Note that disabling OCaml (bindings) or Perl will have the knock-on
effect of disabling parts of the test suite and some tools.

OCaml is required to build libguestfs and this requirement cannot be
removed.  Using I<--disable-ocaml> only disables the bindings.

=item B<--disable-fuse>

Disable FUSE support in the API and the L<guestmount(1)> tool.

=item B<--disable-static>

Don’t build a static linked version of the libguestfs library.

=item B<--enable-install-daemon>

Normally L<guestfsd(8)> is not installed by C<make install>, since
that wouldn't be useful (instead it is "installed" inside the supermin
appliance).  However if packagers are building "libguestfs live" then
they should use this option.

=item B<--enable-werror>

This turns compiler warnings into errors (ie. C<-Werror>).  Use this
for development, especially when submitting patches.  It should
generally I<not> be used for production or distro builds.

=item B<--with-default-backend=libvirt>

This controls the default method that libguestfs uses to run qemu (see
L<guestfs(3)/BACKEND>).  If not specified, the default backend is
C<direct>, which means libguestfs runs qemu directly.

Fedora and Red Hat Enterprise Linux (RHEL) E<ge> 7 use this flag to
change the default backend to C<libvirt>, because (especially in RHEL)
the policy is not to allow any program to run qemu except via libvirt.

Note that despite this setting, all backends are built into
libguestfs, and you can override the backend at runtime by setting the
C<$LIBGUESTFS_BACKEND> environment variable (or using API methods).

=item B<--with-distro=REDHAT|DEBIAN|...>

Libguestfs needs to know which Linux distro is in use so it can choose
package names for the appliance correctly (see for example
F<appliance/packagelist.in>).  It normally does this automatically.

However if you can building or packaging libguestfs on a new distro
then you can use I<--with-distro> to specify that the distro is
similar to an existing one (eg. I<--with-distro=REDHAT> if the distro
is a new Red Hat or CentOS derivative).

Note that if your distro is completely new then it may still require
upstream modifications.

=item B<--with-extra=">I<distroname>=I<version>,libvirt,...B<">

=item B<--with-extra="local">

This option controls the "extra" field returned by
L<guestfs(3)/guestfs_version> and also printed by virt tools'
I<--version> option.  It is a free text field, but a good idea is to
encode a comma-separated list of facts such as the distro name and
version, whether libvirt is the default backend, and anything else
that may help with debugging problems raised by users.

For custom and/or local builds, this can be set to C<local> to
indicate this is I<not> a distro build.

=item B<--without-libvirt>

Compile libguestfs without libvirt support, even if libvirt
development libraries are installed.

=item B<--with-qemu=">bin1 bin2 ...B<">

Provide an alternate qemu binary (or list of binaries).  This can be
overridden at runtime by setting the C<LIBGUESTFS_HV> environment
variable.

=item B<--with-supermin-packager-config=>I<yum.conf>

This passes the I<--packager-config> option to L<supermin(1)>.

The most common use for this is to build the appliance using an
alternate repository (instead of using the installed yum/dnf/apt/etc
configuration to find and download packages).  You might need to use
this if you want to build libguestfs without having a network
connection.  Examples of using this can be found in the Fedora
C<libguestfs.spec> file (see L</BUILDING A PACKAGE FOR FEDORA> below
for resources).

=item B<--with-supermin-extra-options=">--opt1 --opt2 ...B<">

Pass additional options to L<supermin(1)>.  See
F<appliance/make.sh.in> to understand precisely what this does.

=item B<PYTHON>

This environment variable may be set to point to a python binary
(eg. C<python3>).  When C<./configure> runs, it inspects this python
binary to find the version of Python, the location of Python libraries
and so on.

=item B<SUPERMIN>

This environment variable can be set to choose an alternative
L<supermin(1)> binary.  This might be used, for example, if you want
to use a newer upstream version of supermin than is packaged for your
distro, or if supermin is not packaged at all.  On RHEL 7, you must
set C<SUPERMIN=/usr/bin/supermin5> when compiling libguestfs.

=back

=head1 NOTES ABOUT QEMU AND KVM

A common problem is with broken or incompatible qemu releases.

Different versions of qemu have problems booting the appliance for
different reasons.  This varies between versions of qemu, and Linux
distributions which add their own patches.

If you find a problem, you could try using your own qemu built from
source (qemu is very easy to build from source), with a "qemu
wrapper".  See L<guestfs(3)/QEMU WRAPPERS>.

By default the configure script will look for qemu-kvm (KVM support).
KVM is much faster than using plain qemu.

You may also need to enable KVM support for non-root users, by
following these instructions:
L<http://www.linux-kvm.org/page/FAQ#How_can_I_use_kvm_with_a_non-privileged_user.3F>

On some systems, this will work too:

 chmod 0666 /dev/kvm

On some systems, the chmod will not survive a reboot, and you will
need to make edits to the udev configuration.

=head1 USING CLANG (LLVM) INSTEAD OF GCC

 export CC=clang
 ./configure
 make

=head1 USING A PREBUILT BINARY APPLIANCE

To understand what the libguestfs appliance means, see
L<guestfs-internals(1)>.

If you are using non-Linux, or a Linux distribution that does not have
L<supermin(1)> support, or simply if you don't want to build your own
libguestfs appliance, then you can use one of the prebuilt binary
appliances that we supply:
L<http://libguestfs.org/download/binaries/appliance>

Build libguestfs like this:

 ./configure --disable-appliance --disable-daemon
 make

Set C<$LIBGUESTFS_PATH> to the path where you unpacked the appliance
tarball, eg:

 export LIBGUESTFS_PATH=/usr/local/lib/guestfs/appliance

and run the libguestfs programs and virt tools in the normal way,
eg. using the F<./run> script (see above).

=head1 BUILDING A PACKAGE FOR FEDORA

The Fedora spec file is stored under:
L<http://pkgs.fedoraproject.org/cgit/rpms/libguestfs.git/>

Libguestfs is built in Fedora using the ordinary Fedora build system
(Koji).

=head1 BUILDING A PACKAGE FOR RED HAT ENTERPRISE LINUX

Red Hat Enterprise Linux (RHEL) builds of libguestfs are heavily
patched.  There are broadly two types of patches we apply:

=over 4

=item *

We disable many features that we do not wish to support for RHEL
customers.  For example, the "libguestfs live" feature is disabled.

=item *

We backport upstream features.

=back

The patches we apply to RHEL releases are available publically in the
upstream git repository, in a branch called C<rhel-x.y>

For example, the RHEL 7.3 patches are available here:
L<https://github.com/libguestfs/libguestfs/commits/rhel-7.3>

The sources and spec files for RHEL versions of libguestfs are
available on L<https://git.centos.org/project/rpms>, and see also
L<https://wiki.centos.org/Sources>.

=head1 SEE ALSO

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

=head1 AUTHORS

Richard W.M. Jones (C<rjones at redhat dot com>)

=head1 COPYRIGHT

Copyright (C) 2009-2023 Red Hat Inc.
