334 Commits

Author SHA1 Message Date
Laszlo Ersek
bd3e033e08 lib: introduce guestfs_int_make_pid_path()
Introduce a small function for creating pathnames for PID files.

guestfs_int_make_pid_path() is something of an amalgamation of
guestfs_int_make_temp_path() [1] and guestfs_int_create_socketname() [2]:

- it creates a pathname under sockdir, like [2],

- it uses the handle's unique counter, like [1],

- it takes a name like both [1] and [2], but the name is not size-limited
  like in [2], plus we hardcode the suffix from [1] as ".pid".

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2184967
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Message-Id: <20230714132213.96616-7-lersek@redhat.com>
2023-07-14 17:57:19 +02:00
Laszlo Ersek
0b2ad40a09 lib: move guestfs_int_create_socketname() from "launch.c" to "tmpdirs.c"
Consider the following inverted call tree (effectively a dependency tree
-- callees are at the top and near the left margin):

  lazy_make_tmpdir()                  [lib/tmpdirs.c]
    guestfs_int_lazy_make_tmpdir()    [lib/tmpdirs.c]
      guestfs_int_make_temp_path()    [lib/tmpdirs.c]
    guestfs_int_lazy_make_sockdir()   [lib/tmpdirs.c]
      guestfs_int_create_socketname() [lib/launch.c]

lazy_make_tmpdir() is our common workhorse / helper function that
centralizes the mkdtemp() function call.

guestfs_int_lazy_make_tmpdir() and guestfs_int_lazy_make_sockdir() are the
next level functions, both calling lazy_make_tmpdir(), just feeding it
different dirname generator functions, and different "is_runtime_dir"
qualifications. These functions create temp dirs for various, more
specific, purposes (see the manual and "lib/guestfs-internal.h" for more
details).

On a yet higher level are guestfs_int_make_temp_path() and
guestfs_int_create_socketname() -- they serve for creating *entries* in
those specific temp directories.

The discrepancy here is that, although all the other functions live in
"lib/tmpdirs.c", guestfs_int_create_socketname() is defined in
"lib/launch.c". That makes for a confusing code reading; move the function
to "lib/tmpdirs.c", just below its sibling function
guestfs_int_make_temp_path().

While at it, correct the leading comment on
guestfs_int_create_socketname() -- the socket pathname is created in the
socket directory, not in the temporary directory.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2184967
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Message-Id: <20230714132213.96616-6-lersek@redhat.com>
2023-07-14 17:57:17 +02:00
Laszlo Ersek
21ccddecf7 docs: clarify sockdir's separation
There's another reason for separating sockdir from tmpdir, beyond "shorter
pathnames needed": permissions. For example, passt drops privileges such
that it cannot access "/tmp", and that restricts both the unix domain
socket and the PID file of passt.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2184967
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Message-Id: <20230714132213.96616-5-lersek@redhat.com>
2023-07-14 17:57:15 +02:00
Laszlo Ersek
b4a4b754c6 docs: fix broken link in the guestfs manual
Commit 55202a4d49 ("New API: get-sockdir", 2016-02-03) added identical
language to "fish/guestfish.pod" and "src/guestfs.pod", including an
internal link L</get-sockdir>. That's appropriate for
"fish/guestfish.pod", but the same API description is generated with a
different anchor for "src/guestfs.pod". Adapt the reference.

Fixes: 55202a4d49
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2184967
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Message-Id: <20230714132213.96616-4-lersek@redhat.com>
2023-07-14 17:57:14 +02:00
Laszlo Ersek
190609377f lib/launch-libvirt: support networking with passt
We generate the <interface type="user"> element on libvirt 3.8.0+ already.

For selecting passt rather than SLIRP, we only need to insert the child
element <backend type='passt'>. Make that child element conditional on
libvirt 9.0.0+, plus "passt --help" being executable.

For the latter, place the new helper function guestfs_int_passt_runnable()
in "lib/launch.c" -- we're going to use the same function for the direct
backend as well.

This change exposes a number of (perceived) shortcomings in libvirt; I've
filed <https://bugzilla.redhat.com/show_bug.cgi?id=2222766> about those.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2184967
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Message-Id: <20230714132213.96616-3-lersek@redhat.com>
2023-07-14 17:57:13 +02:00
Laszlo Ersek
85d361dd6a lib: fix NETWORK_ADDRESS: make it an actual IP address, not a subnet base
Currently we #define NETWORK_ADDRESS as "169.254.0.0". That's a bug in
libguestfs, but it's been invisible -- thus far it's been canceled out by
*independent* bugs in *both* QEMU and libvirt.

(1) With the direct backend, the current definition of NETWORK_ADDRESS
results in the following QEMU command line option:

  -netdev user,id=usernet,net=169.254.0.0/16

According to the QEMU documentation, the "net" property here is supposed
to specify the *exact* IP address that the guest will receive. The guest
however receives 169.254.2.15 (I've checked that with virt-rescue).

In other words, libguestfs doesn't do what the QEMU documentation says,
and QEMU doesn't do what the QEMU documentation says either. The end
result has been good enough -- but only until now.

(2) With the libvirt backend, the current definition of NETWORK_ADDRESS
results in the following domain XML snippet:

  <interface type="user">
    <model type="virtio"/>
    <ip family="ipv4" address="169.254.0.0" prefix="16"/>
  </interface>

which libvirt translates, in turn, to

  -netdev {"type":"user","net":"169.254.0.0/16","id":"hostnet0"}

According to the domain XML documentation, the @address attribute here is
again supposed to specify the *exact* IP address that the guest will
receive. However, the guest receives 169.254.2.15 (I've checked that with
virt-rescue).

In other words, libguestfs doesn't do what the libvirt documentation says,
and libvirt doesn't do what the libvirt documentation says either. The end
result has been good enough -- but only until now.

Where things break down though is the subsequent passt enablement, in the
rest of this series. For example, when using the libvirt backend together
with passt, libvirt translates the @address attribute to passt's
"--address" option, but passt takes the address *verbatim*, and not as a
subnet base address. That difference is visible in the appliance; for
example, when running virt-rescue on a Fedora 38 image, and issuing "ip
addr". Namely, after enabling passt for the libvirt backend, the
guest-visible IP address changes from 169.254.2.15 to 169.254.0.0, which
is an IP address that makes no sense for an endpoint.

Fix the latent bug by specifying the actual guest IP address we want, in
NETWORK_ADDRESS.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2184967
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Message-Id: <20230714132213.96616-2-lersek@redhat.com>
2023-07-14 17:57:10 +02:00
Laszlo Ersek
13c7052ff9 lib: remove guestfs_int_cmd_clear_close_files()
The last (only?) caller of guestfs_int_cmd_clear_close_files() disappeared
in commit e4c3968880 ("lib/info: Remove /dev/fd hacking and pass a true
filename to qemu-img info.", 2018-01-23), part of v1.37.36.

Simplify the code by removing guestfs_int_cmd_clear_close_files().

Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20230711113906.107340-1-lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
2023-07-12 12:22:37 +02:00
Richard W.M. Jones
b9ccfe3e03 lib: Get rid of minimum libvirt version check
We require libvirt >= 0.10.2, and we included code to check this at
configure-, compile- and run-time.  Remove the checks at compile and
run time (keep the ./configure check).  Libvirt 0.10.2 was released
over 10 years ago so it's safe to assume that everyone has it by now.
2023-02-16 11:58:30 +00:00
Richard W.M. Jones
f0f8e6c5fe lib: Choose q35 machine type for x86-64
This machine type is more modern than the older 'pc' type and as most
qemu development is now focused there we expect it will perform and
behave better.  In almost all respects this change should make no
difference.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2168578
Acked-by: Laszlo Ersek <lersek@redhat.com>
See-also: https://listman.redhat.com/archives/libguestfs/2023-February/030645.html
2023-02-09 14:40:29 +00:00
Richard W.M. Jones
e2c7bddf10 Update copyright dates for 2023
Run this command across the source:

  perl -pi.bak -e 's/(20[012][0-9])-20[12][012]/$1-2023/g' `git ls-files`

and remove changes to po{,-docs}/*.po{,t} (these will be regenerated
later when we run 'make dist').
2023-02-07 10:50:48 +00:00
Richard W.M. Jones
4f58017a5e lib/guestfs-internal-all.h: Use __restrict__ keyword for very old GCC
RHEL 7 GCC 4.8.5 does not allow the restrict keyword.  Use the
permitted alternative.
2023-01-31 15:54:46 +00:00
Richard W.M. Jones
8f6b82bc45 lib/guestfs-internal-all.h: Fix whitespace 2023-01-31 15:54:46 +00:00
Richard W.M. Jones
824c745748 lib: Return correct osinfo field for Windows 11
For Windows Client, we can only distinguish between Windows 10 and
Windows 11 using the build ID.  The product name in both cases is
"Windows 10 <something>", apparently intentionally.

References:
https://learn.microsoft.com/en-us/answers/questions/586619/windows-11-build-ver-is-still-10022000194.html
a263fe0b26/winsup/cygwin/wincap.cc (L429)
https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions

After this fix, the output of virt-inspector changes to this, which is
a bit odd, but correct:

    <name>windows</name>
    <arch>x86_64</arch>
    <distro>windows</distro>
    <product_name>Windows 10 Pro</product_name>
    <product_variant>Client</product_variant>
    <major_version>10</major_version>
    <minor_version>0</minor_version>
    <windows_systemroot>/Windows</windows_systemroot>
    <windows_current_control_set>ControlSet001</windows_current_control_set>
    <osinfo>win11</osinfo>

Thanks: Yaakov Selkowitz
Reported-by: Yongkui Guo
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2012658
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2022-12-02 10:16:57 +00:00
Richard W.M. Jones
f3dd67affe New API: inspect_get_build_id
Add an API to return the build ID of the guest.  This to allow a
future change to be able to distinguish between Windows 10 and Windows 11
which can only be done using the build ID.

For Windows we can read the CurrentBuildNumber key from the registry.
For Linux there happens to be a BUILD_ID field in /etc/os-release.
I've never seen a Linux distro that actually uses this.

Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2022-12-02 10:16:57 +00:00
Richard W.M. Jones
e1c9bbb3d1 lib: Avoid calling setenv between fork and exec
setenv can call malloc and is not safe to call here.  Glibc is usually
tolerant of this and we haven't had problems before, but if you use
GLIBC_TUNABLES glibc.malloc.check=1 (or any alternate malloc / libc
which serializes) then you would see hangs if starting multiple
libguestfs handles from different threads at the same time.

This commit also updates the common submodule to pick up:

  commit 3c64bcdeaf684f05f46f3928b55aadafdfe72720
  Author: Richard W.M. Jones <rjones@redhat.com>
  Date:   Fri Oct 14 11:07:21 2022 +0100

    utils: Add function for copying the environment and adding new entries

    libguestfs is currently calling setenv at an unsafe location between
    fork and exec.  To fix this we need a way to copy and modify the
    environment before fork and then we can pass the modified environ to
    execve-like functions.  nbdkit already does the same so use that code.

    This function is copied and adapted from here under a compatible license:
    https://gitlab.com/nbdkit/nbdkit/-/blob/master/common/utils/environ.c

Thanks: Siddhesh Poyarekar
2022-10-14 11:40:12 +01:00
Richard W.M. Jones
347e875911 lib: Remove -Wanalyzer-file-leak ignores
These were added for GCC 11.  The problem has been fixed in GCC 12.
On macOS (clang) these produced errors like this:

tsk.c:75:32: error: unknown warning group '-Wanalyzer-file-leak', ignored [-Werror,-Wunknown-warning-option]
                               ^
2022-08-16 15:42:30 +01:00
Richard W.M. Jones
5fe33b685a lib/readdir.c: Include <rpc/types.h> before <rpc/xdr.h>
On macOS, several pages of errors like:

In file included from readdir.c:26:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/rpc/xdr.h:126:3: error: type name requires a specifier or qualifier
                bool_t  (*x_getlong)(struct __rpc_xdr *, int *);
                ^
2022-08-16 15:40:23 +01:00
Richard W.M. Jones
e7f28159fb lib/launch.c: Include <signal.h> for sigemptyset, etc
launch.c:191:3: error: implicit declaration of function 'sigemptyset' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
  sigemptyset (&sigset);
  ^
launch.c:192:3: error: implicit declaration of function 'sigaddset' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
  sigaddset (&sigset, SIGTERM);
  ^
launch.c:193:3: error: implicit declaration of function 'sigprocmask' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
  sigprocmask (SIG_UNBLOCK, &sigset, NULL);
  ^
3 errors generated.
2022-08-16 15:38:51 +01:00
Richard W.M. Jones
908e41e556 gnulib: Copy in pipe2 and accept4 replacements from gnulib
These are shipped under a compatible license (LGPLv2+ or LGPLv3+).
2022-08-16 15:37:52 +01:00
Richard W.M. Jones
b018b35bd4 lib: Remove dtrace/systemtap probes
These were added in libguestfs 1.14, but never really used.  Only a
handful of probes were available.  When I was benchmarking libguestfs
in 2016 I didn't even use these probes because better/simpler
techniques were available.
2022-08-16 15:35:17 +01:00
Richard W.M. Jones
7149312632 lib: Don't use -cpu max on RISC-V
qemu (7.0) does not support -cpu max for TCG.

Note this change is necessary but not sufficient for getting
libguestfs to run on RISC-V, because there is also currently no
working path to make -kernel work.
2022-07-15 17:10:56 +01:00
Laszlo Ersek
9a3e9a6c03 introduce the "clevis_luks_unlock" API
Introduce a new guestfs API called "clevis_luks_unlock". At the libguestfs
level, it is quite simple; it wraps the "clevis luks unlock" guest command
(implemented by the "clevis-luks-unlock" executable, which is in fact a
shell script).

The complexity is instead in the network-based disk encryption
(Clevis/Tang) scheme. Useful documentation:

- https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/security_hardening/index#configuring-automated-unlocking-of-encrypted-volumes-using-policy-based-decryption_security-hardening
- https://github.com/latchset/clevis#clevis
- https://github.com/latchset/tang#tang

The package providing "clevis-luks-unlock" is usually called
"clevis-luks", occasionally "clevis". Some distros don't package clevis at
all. Add the new API under a new option group (which may not be available)
called "clevisluks".

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1809453
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20220630122048.19335-3-lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
2022-07-01 15:07:26 +02:00
Richard W.M. Jones
60e8ea0312 lib/readdir.c: Include unistd.h for unlink(2)
On riscv64:

readdir.c: In function ‘guestfs_impl_readdir’:
readdir.c:127:3: error: implicit declaration of function ‘unlink’ [-Werror=implicit-function-declaration]
  127 |   unlink (tmpfn);
      |   ^~~~~~

I also changed the #include lines to make them look a bit more
like use in other files.
2022-06-17 13:25:48 +01:00
Laszlo Ersek
544bb0ff50 guestfs.pod: document encrypted RBD disk limitation
Under "REMOTE STORAGE", the "NETWORK BLOCK DEVICE" section already
documents some limitations. Turns out we need to describe a quirky
exception for accessing encrypted RBD disks, too.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2033247
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20220518083014.9890-1-lersek@redhat.com>
Acked-by: Richard W.M. Jones <rjones@redhat.com>
2022-05-19 14:11:43 +02:00
Richard W.M. Jones
59d7e6e017 lib: Disable 5-level page tables when using -cpu max
In https://bugzilla.redhat.com/show_bug.cgi?id=2082806 we've been
tracking an insidious qemu bug which intermittently prevents the
libguestfs appliance from starting.  The symptoms are that SeaBIOS
starts and displays its messages, but the kernel isn't reached.  We
found that the kernel does in fact start, but when it tries to set up
page tables and jump to protected mode it gets a triple fault which
causes the emulated CPU in qemu to reset (qemu exits).

This seems to only affect TCG (not KVM).

Yesterday I found that this is caused by using -cpu max which enables
the "la57" feature (5-level page tables[0]), and that we can make the
problem go away using -cpu max,la57=off.  Note that I still don't
fully understand the qemu bug, so this is only a workaround.

I chose to disable 5-level page tables for both TCG and KVM, partly to
make the patch simpler, and partly because I guess it's not a feature
(ie. 57 bit linear addresses) that is useful for the libguestfs
appliance case, where we have limited physical memory and no need to
run any programs with huge address spaces.

I tested this by running both the direct & libvirt paths overnight.  I
expect that this patch will fail with old qemu/libvirt which doesn't
understand the "la57" feature, but this is only intended as a
temporary workaround.

[0] Article about 5-level page tables as background:
https://lwn.net/Articles/717293/

Thanks: Laszlo Ersek
Fixes: https://answers.launchpad.net/ubuntu/+source/libguestfs/+question/701625
Acked-by: Laszlo Ersek <lersek@redhat.com>
2022-05-13 13:00:28 +01:00
Laszlo Ersek
c8e3caf9e6 lib: rename VALID_FORMAT_IFACE to VALID_FORMAT
We no longer use VALID_FORMAT_IFACE for validating "iface"; rename the
macro to reflect that we only check "format" with it.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1844341
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20220504134155.11832-4-lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
2022-05-05 13:05:21 +02:00
Laszlo Ersek
f68eaee1d6 lib: drive_create_data, drive: remove field "iface"
Representing "iface" in the "drive_create_data" and "drive" structures is
now useless; the direct backend ignores "iface", while the libvirt one
rejects it unless it is empty. Unify both backends -- make them both
ignore "iface". (Which only relaxes the libvirt backend, so it cannot
cause compatibility problems.) This lets us remove the fields. Update the
documentation as well.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1844341
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20220504134155.11832-3-lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
2022-05-05 13:05:19 +02:00
Laszlo Ersek
3eb830dbae lib: launch-direct: ignore drive "iface" parameter
Rich said in <https://bugzilla.redhat.com/show_bug.cgi?id=1844341#c1>:

> The libvirt backend has never allowed the iface parameter.  We should
> probably ignore it in the direct backend since it's never been possible
> to use this parameter correctly.

Remove the handling of "iface" in the direct (QEMU) backend. Refresh the
documentation regarding both backends.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1844341
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20220504134155.11832-2-lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
2022-05-05 13:05:07 +02:00
Laszlo Ersek
45b7f1736b guestfs_readdir(): rewrite with FileOut transfer, to lift protocol limit
Currently the guestfs_readdir() API can not list long directories, due to
it sending back the whole directory listing in a single guestfs protocol
response, which is limited to GUESTFS_MESSAGE_MAX (approx. 4MB) in size.

Introduce the "internal_readdir" action, for transferring the directory
listing from the daemon to the library through a FileOut parameter.
Rewrite guestfs_readdir() on top of this new internal function:

- The new "internal_readdir" action is a daemon action. Do not repurpose
  the "readdir" proc_nr (138) for "internal_readdir", as some distros ship
  the binary appliance to their users, and reusing the proc_nr could
  create a mismatch between library & appliance with obscure symptoms.
  Replace the old proc_nr (138) with a new proc_nr (511) instead; a
  mismatch would then produce a clear error message. Assume the new action
  will first be released in libguestfs-1.48.2.

- Turn "readdir" from a daemon action into a non-daemon one. Call the
  daemon action guestfs_internal_readdir() manually, receive the FileOut
  parameter into a temp file, then deserialize the dirents array from the
  temp file.

This patch sneakily fixes an independent bug, too. In the pre-patch
do_readdir() function [daemon/readdir.c], when readdir() returns NULL, we
don't distinguish "end of directory stream" from "readdir() failed". This
rewrite fixes this problem -- I didn't see much value separating out the
fix for the original do_readdir().

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1674392
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20220502085601.15012-2-lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
2022-05-03 10:53:48 +02:00
Richard W.M. Jones
ac00e603f8 New API: guestfs_device_name returning the drive name
For each drive added, return the name.  For example calling this with
index 0 will return the string "/dev/sda".  I called it
guestfs_device_name (not drive_name) for consistency with the existing
guestfs_device_index function.

You don't really need to call this function.  You can follow the
advice here:
https://libguestfs.org/guestfs.3.html#block-device-naming
and assume that drives are added with predictable names like
"/dev/sda", "/dev/sdb", etc.

However it's useful to expose the internal guestfs_int_drive_name
function since especially handling names beyond index 26 is tricky
(https://rwmj.wordpress.com/2011/01/09/how-are-linux-drives-named-beyond-drive-26-devsdz/)

Fixes: https://github.com/libguestfs/libguestfs/issues/80
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2022-05-02 12:10:29 +01:00
Andrey Drobyshev
ef8c6593eb appliance: don't read extfs signature from QCOW2 image directly
If the appliance is a QCOW2 image, function get_root_uuid_with_file()
fails to read ext filesystem signature (0x53EF at offset 0x438) from it.
This results in the following error:

libguestfs: error: /usr/lib64/guestfs/appliance/root: appliance is not
an extfs filesystem

The error itself is harmless, but misleading.  So let's skip retrieving
the signature and UUID in case the image contains QCOW2 header.  It's
safe because in this case we'll retrieve it later from RAW image dumped
from that QCOW2 by "qemu-img dd".

Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
2022-04-25 20:54:48 +01:00
Richard W.M. Jones
4256737227 lib: Remove drive hotplugging support
This was a feature that allowed you to add drives to the appliance
after launching it.  It was complicated to implement, and only worked
for the libvirt backend (not "direct", which is the default backend).

It also turned out to be a bad idea.  The original concept was that
appliance creation was slow, so to examine multiple guests you should
launch the handle once then hot-add the disks from each guest in turn
to manipulate them.  However this is terrible from a security point of
view, especially for multi-tenant, because the drives from one guest
might compromise the appliance and thus the filesystems/drives from
subsequent guests.

It also turns out that hotplugging is very slow.  Nowadays appliance
creation should be faster than hotplugging.

The main use case for this was virt-df, but virt-df no longer uses it
after we discovered the problems outlined above.
2022-03-09 09:28:02 +00:00
Richard W.M. Jones
b9b0a90487 lib: Remove User-Mode Linux
User-Mode Linux was an alternative hypervisor that could run the
appliance, instead of using qemu.  It had many limitations including
lack of network, and UML support in Linux has been semi-broken for a
long time.  It was also slower than KVM on baremeal in general and had
various corner cases which were much slower including the emulated
serial port which made bulk uploads and downloads painful.  Also of
course it lacked qemu-specific features like qcow2 or any
network-backed disk, so many disk images could not be opened this way.

This was never supported in RHEL.

See-also: https://bugzilla.redhat.com/1144197
2022-03-09 09:28:02 +00:00
Richard W.M. Jones
dbc2fd8dc8 lib: Remove libguestfs live
This experimental feature allowed you (in theory) to connect to an
existing instance of the libguestfs daemon.  (Again, in theory) it
allowed you to attach to running guests.  This didn't work well in
practice.  If you want to do this, install qemu-guest-agent inside
your guest instead.

This also disables the --live options in guestfish and guestmount.
(The option now prints an error).

This was never supported in RHEL.

The daemon tests relied on this connection method to perform tests on
a bare daemon, so this removes those tests.  They were not especially
valuable.

See-also: https://bugzilla.redhat.com/798980
2022-03-09 09:27:19 +00:00
Richard W.M. Jones
f019cc01b0 lib: Better handling for problems creating the socket path
GCC 12 gives a warning about our previous attempt to check the length
of the socket path.  In the ensuing discussion it was pointed out that
it is easier to get snprintf to do the hard work.  snprintf will
return an int >= UNIX_PATH_MAX if the path is too long, or < 0 if
there are other errors such as locale/encoding problems.  So we should
just check for those two cases instead.

https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/thread/NPKWMTSJ2A2ABNJJEH6WTZIAEFTX6CQY/

Thanks: Martin Sebor and Laszlo Ersek
2022-01-17 15:50:36 +00:00
Laszlo Ersek
5858c2cf6c launch-libvirt: add virtio-net via the standard <interface> element
Starting with version 3.8.0, libvirt allows us to specify the network
address and network mask (as prefix) for SLIRP directly via the
<interface> element in the domain XML:
<https://libvirt.org/formatdomain.html#userspace-slirp-stack>. This means
we don't need the <qemu:commandline> hack for virtio-net on such versions.

Restrict the hack in construct_libvirt_xml_qemu_cmdline() to
libvirt<3.8.0, and generate the proper <interface> element in
construct_libvirt_xml_devices() on libvirt>=3.8.0.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2034160
Suggested-by: Richard W.M. Jones <rjones@redhat.com>
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20211223103701.12702-4-lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Tested-by: Richard W.M. Jones <rjones@redhat.com>
2021-12-23 13:22:38 +01:00
Laszlo Ersek
216de164e0 lib: extract NETWORK_ADDRESS and NETWORK_PREFIX as macros
The 169.254.0.0/16 network specification (for the appliance) is currently
duplicated between the direct backend and the libvirt backend. In a
subsequent patch, we're going to need the network specification in yet
another spot; extract it now to the NETWORK_ADDRESS and NETWORK_PREFIX
macros (simply as strings).

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2034160
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20211223103701.12702-3-lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Tested-by: Richard W.M. Jones <rjones@redhat.com>
2021-12-23 13:22:35 +01:00
Laszlo Ersek
5ce5ef6a97 launch-libvirt: place our virtio-net-pci device in slot 0x1e
The <qemu:commandline> trick we use for adding our virtio-net-pci device
in the libvirt backend can conflict with libvirtd's and QEMU's PCI address
assignment. Try to mitigate that by placing our device in slot 0x1e on the
root bus. In practice this could only conflict with a "dmi-to-pci-bridge"
device model, which libvirtd itself places in slot 0x1e. However, given
the XMLs we generate, and modern QEMU versions, libvirtd has no reason to
auto-add "dmi-to-pci-bridge". Refer to
<https://libvirt.org/formatdomain.html#controllers>.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2034160
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20211223103701.12702-2-lersek@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Tested-by: Richard W.M. Jones <rjones@redhat.com>
2021-12-23 13:22:14 +01:00
Neil Hanlon
631962c0e8 Add detection support for Rocky Linux (CentOS/RHEL-like)
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2030709
Thanks: label@rockylinux.org

---

RWMJ notes: I fixed the original patch so it compiled.  This patch
sets osinfo to "rocky8", which doesn't exist in the osinfo db yet.
Arguably we might want to set this to "centos8", but we can see what
libosinfo decides to do.  Here is partial virt-inspector output on a
Rocky Linux disk image:

$ ./run virt-inspector -a disk.img
<?xml version="1.0"?>
<operatingsystems>
  <operatingsystem>
    <root>/dev/rl/root</root>
    <name>linux</name>
    <arch>x86_64</arch>
    <distro>rocky</distro>
    <product_name>Rocky Linux 8.5 (Green Obsidian)</product_name>
    <major_version>8</major_version>
    <minor_version>5</minor_version>
    <package_format>rpm</package_format>
    <package_management>dnf</package_management>
    <hostname>localhost.localdomain</hostname>
    <osinfo>rocky8</osinfo>
    <mountpoints>
      <mountpoint dev="/dev/rl/root">/</mountpoint>
      <mountpoint dev="/dev/sda1">/boot</mountpoint>
    </mountpoints>
    <filesystems>
      <filesystem dev="/dev/rl/root">
        <type>xfs</type>
        <uuid>fed8331f-9f25-40cd-883e-090cd640559d</uuid>
      </filesystem>
      <filesystem dev="/dev/rl/swap">
        <type>swap</type>
        <uuid>6da2c121-ea7d-49ce-98a3-14a37fceaadd</uuid>
      </filesystem>
      <filesystem dev="/dev/sda1">
        <type>xfs</type>
        <uuid>4efafe61-2d20-4d93-8055-537e09bfd033</uuid>
      </filesystem>
    </filesystems>
2021-12-10 09:09:47 +00:00
Richard W.M. Jones
90eb3a4184 lib, lua: Fix usage of strerror_r
$ ./run guestfish -vx add-drive foo "readonly:true"
  libguestfs: trace: set_pgroup true
  libguestfs: trace: set_pgroup = 0
  libguestfs: trace: add_drive "foo" "readonly:true"
  libguestfs: error: foo:
  libguestfs: trace: add_drive = -1 (error)
  libguestfs: trace: close
  libguestfs: closing guestfs handle 0x55c0bacf12a0 (state 0)

Fix the usage of strerror_r by using the new wrapper defined in
libguestfs-common.  A similar fix is made in the Lua bindings.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2030396
Reported-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
Tested-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
2021-12-09 13:46:28 +00:00
Laszlo Ersek
98ed0243e7 lib/proto: suppress "may be used uninitialized" in send_file_complete()
gcc emits the following warning:

> proto.c: In function ‘send_file_complete’:
> proto.c:437:10: error: ‘buf’ may be used uninitialized
> [-Werror=maybe-uninitialized]
>   437 |   return send_file_chunk (g, 0, buf, 0);
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In theory, passing the 1-byte array "buf", with indeterminate contents, to
xdr_bytes() ultimately, could be fine -- assuming xdr_bytes() never reads
the contents of the buffer, due to the buffer size being zero. However,
the xdr_bytes() manual does not seem to guarantee this (it also does not
explicitly permit passing a NULL buffer alongside size=0, which would be
even simpler for the caller).

In order to shut up the compiler, just zero-initialize the buffer --
that's simpler than adding diagnostics pragmas. The "maybe-uninitialized"
warning is otherwise very useful, so keep it globally enabled (per
WARN_CFLAGS / WERROR_CFLAGS).

Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20211011223627.20856-3-lersek@redhat.com>
Acked-by: Richard W.M. Jones <rjones@redhat.com>
2021-10-12 16:48:01 +02:00
Richard W.M. Jones
e14ff93742 lib: direct: Remove use of sga
sga (or "sgabios" or "Serial Graphics Adapter") is an option ROM for
seabios which directs output to the serial adapter.  This is very
useful for debugging BIOS problems during boot.

RHEL wants to deprecate this feature (in fact, they just deprecated it
without telling us).  However there is an equivalent feature in
seabios (seabios >= 1.11 / qemu >= 2.11.0) which can be enabled using
either -nographic or -machine graphics=off

This commit removes sga and enables -machine graphics=off in the
direct backend.

References (for RHEL 9 qemu change):
https://bugzilla.redhat.com/show_bug.cgi?id=2002325
https://bugzilla.redhat.com/show_bug.cgi?id=2000845
https://lists.nongnu.org/archive/html/qemu-devel/2021-09/msg02417.html
https://listman.redhat.com/archives/libvir-list/2021-September/msg00205.html

For the libvirt backend we will continue to use <bios useserial=yes>.
This currently breaks when sga is not available, but I talked to Dan
and the plan there is to adapt libvirt so the same XML will enable
-machine graphics=off.  IOW libguestfs does not need to make any
change.

References (for libvirt change):
https://bugzilla.redhat.com/show_bug.cgi?id=2003092
https://listman.redhat.com/archives/libvir-list/2021-September/msg00193.html

Thanks: Gerd Hoffman, Daniel Berrangé
2021-09-13 10:40:50 +01:00
Richard W.M. Jones
45de287447 lib: Autodetect backing format for qemu-img create -b
qemu 6.1 has decided to change qemu-img create so that a backing
format (-F) is required if a backing file (-b) is specified.  Since we
don't want to change the libguestfs API to force callers to specify
this because that would be an API break, autodetect it.

This is similar to commit c8c181e8d9 ("launch: libvirt: Autodetect
backing format for readonly drive overlays").

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1998820
2021-08-31 08:35:52 +01:00
Richard W.M. Jones
73cd0a0c8d lib: Add osinfo information for Windows Server 2022 Datacenter
Windows Server 2022 preview is identified as <osinfo>win2k16</osinfo>.
Although current osinfo-db does not have an entry "win2k22", return
this instead.

osinfo-db issue to add win2k22:
https://gitlab.com/libosinfo/osinfo-db/-/issues/82

Inspection information for the guest:

    type: windows
    distro: windows
    product_name: Windows Server 2022 Datacenter
    product_variant: Server
    version: 10.0
    arch: x86_64

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1997446
Reported-by: Yongkui Guo
2021-08-25 11:09:37 +01:00
Heinrich Schuchardt
efb3d01992 launch: board model for RISC-V
On RISC-V there is no default machine type. Invoking QEMU requires to
specify a board model with the -M option. So let's define MACHINE_TYPE
as virt.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
2021-08-20 08:13:10 +01:00
Richard W.M. Jones
d3bbc02190 lib: qemu: Don't use -enable-fips option.
QEMU has deprecated this option:

    commit 166310299a1e7824bbff17e1f016659d18b4a559
    Author: Daniel P. Berrangé
    Date:   Tue Oct 20 17:08:27 2020 +0100

    os: deprecate the -enable-fips option and QEMU's FIPS enforcement

    The -enable-fips option was added a long time ago to prevent the use of
    single DES when VNC when FIPS mode is enabled. It should never have been
    added, because apps are supposed to unconditionally honour FIPS mode
    based on the '/proc/sys/crypto/fips_enabled' file contents.

    In addition there is more to achieving FIPS compliance than merely
    blocking use of certain algorithms. Those algorithms which are used
    need to perform self-tests at runtime.

    QEMU's built-in cryptography provider has no support for self-tests,
    and neither does the nettle library.

    If QEMU is required to be used in a FIPS enabled host, then it must be
    built with the libgcrypt library enabled, which will unconditionally
    enforce FIPS compliance in any algorithm usage.

    Thus there is no need to keep either the -enable-fips option in QEMU, or
    QEMU's internal FIPS checking methods.
2021-05-05 12:50:17 +01:00
Richard W.M. Jones
7ed0da779f Ignore return value from strerror_r.
It seems like newer glibc added warn_unused_result to this function.
Try harder to ignore the result.
2021-04-13 15:40:48 +01:00
Richard W.M. Jones
48e7520ec5 lib/guestfs-internal.h: Remove need to include gnulib "hash.h" here.
Centrally including "hash.h" means everything that needs this header
file (everything in lib/) has to depend on gnulib.
2021-04-08 11:12:17 +01:00
Richard W.M. Jones
9cfa1c410f Remove use of gnulib glthread.
This gnulib feature abstracts away threads, locks and TLS, and also
allowed libguestfs to be linked with or without pthread.  However
since pthread these days is part of glibc and so every program is
using pthread, and we want to get rid of gnulib as a dependency, just
use pthread directly.
2021-04-08 11:12:17 +01:00
Richard W.M. Jones
278d0d3226 lib/appliance-kcmdline.c: Read UUID directly from appliance.
Instead of using the external file utility, read the UUID directly
from the extfs filesystem.  file 5.40 broke parsing of UUIDs
(https://bugs.astron.com/view.php?id=253).

Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1945122
2021-03-31 13:45:17 +01:00