187 Commits

Author SHA1 Message Date
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
Richard W.M. Jones
c9ee831aff inspection: Fix inspection of recent RPM guests using non-BDB.
Recent RPM-based guests have switched from using Berkeley DB (BDB) to
sqlite.  In order to inspect these guests (and earlier ones) we need
to stop using the hokey parsing of the BDB and use librpm APIs
instead.

This commit adds a new internal API so we can call librpm from the
daemon, and changes the library part to use the new API for RPM-based
guests.

This change removes the requirement for BDB tools like db_dump.

See also:
http://lists.rpm.org/pipermail/rpm-ecosystem/2021-March/000751.html
http://lists.rpm.org/pipermail/rpm-ecosystem/2021-March/000754.html
https://blog.fpmurphy.com/2011/08/programmatically-retrieve-rpm-package-details.html

This breaks the virt-inspector test (now in the separate guestfs-tools
repository).  However this is not a bug in libguestfs, but a bug in
the phoney Fedora guest that we use for testing - we created a
BDB-style RPM database which was supposed to be just enough to make
the old code work.  The new code using real librpm needs
/usr/lib/rpm/rpmrc (not present in the phoney image) and also cannot
parse the phoney database, so we will need to separately rework that
test.

Thanks: Panu Matilainen
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1766487
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1409024
2021-03-26 16:26:00 +00:00
Richard W.M. Jones
13ceb6a87b appliance: Use <cpu mode="maximum"/> for -cpu max on libvirt.
Note this requires libvirt >= 7.1.0 which was only released in March 2021.

With an older libvirt you will see this error:

  Original error from libvirt: unsupported configuration: Invalid mode attribute 'maximum' [code=67 int1=-1]

In theory we could check if this is supported by looking at the
libvirt capabilities and fall back, but this commit does not do that,
in the expectation that most people will be using the default backend
(direct) and on Fedora/RHEL we will add an explicit minimum version
dependency to the package.

qemu support has been around quite a bit longer (at least since 2017).

Fixes: commit 30f74f38bd
2021-03-18 12:42:35 +00:00
Sam Eiderman
5d686b92a6 launch: libvirt, direct: Add force_kvm backend setting.
By using:

  export LIBGUESTFS_BACKEND_SETTINGS=force_kvm

you can force the backend to use KVM and never fall back to
TCG (software emulation).
2021-03-16 16:11:23 +00:00
Richard W.M. Jones
82493579f3 Port libguestfs to use pcre2 instead of pcre.
https://bugzilla.redhat.com/show_bug.cgi?id=1938982
2021-03-16 11:24:37 +00:00
Richard W.M. Jones
20dbc24d68 lib/fuse.c: Use safe_malloc instead of malloc.
Avoids having to check the return value, and in this case avoids a GCC
analyzer error.
2021-02-22 10:38:19 +00:00
Richard W.M. Jones
30f74f38bd appliance: Use -cpu max.
QEMU has a newish feature (from about 2017 / qemu 2.9) called -cpu max
which is supposed to select the best CPU, ideal for libguestfs.

After this change, on x86-64:

               KVM                          TCG

Direct         -cpu max                     -cpu max
(non-libvirt)

Libvirt   <cpu mode="host-passthrough">     <cpu mode="host-model">
            <model fallback="allow"/>         <model fallback="allow"/>
          </cpu>                            </cpu>

Thanks: Daniel Berrangé
2021-01-28 14:04:29 +00:00
Richard W.M. Jones
fdda111e0e lib/qemu.c: Suppress another bogus -fanalyser warning. 2021-01-28 12:27:41 +00:00
Richard W.M. Jones
03347d49ee lib: Move CLEANUP_GL_RECURSIVE_LOCK_UNLOCK to libguestfs header.
Previously this was in common/utils.  However it is not used anywhere
else, and guestfs-tools wants to remove gnulib dependencies, so move
this to libguestfs.
2021-01-22 13:39:16 +00:00
Richard W.M. Jones
07585189cf lib: Ignore a bunch of bogus GCC 11 warnings.
-Werror=analyzer-file-leak doesn't follow CLEANUP_FCLOSE properly.

yara.c:64:24: error: leak of FILE '<unknown>' [CWE-775] [-Werror=analyzer-file-leak]
   64 |   CLEANUP_FCLOSE FILE *fp = NULL;
      |                        ^~
2021-01-05 10:09:21 +00:00
Richard W.M. Jones
0896dea338 gnulib: Replace hash_delete with hash_remove.
Renamed in gnulib with the old function deprecated.
2020-10-22 14:50:58 +01:00
Richard W.M. Jones
3f4a529ab7 fuse: Don't override access(X_OK) if user is root.
Bug originally reported here by trysis:
https://stackoverflow.com/questions/64273334/test-x-in-mounted-filesystem

If the user is root then we override normally access controls in FUSE,
see https://bugzilla.redhat.com/show_bug.cgi?id=1106548.

However this causes test -x to mark all files as executable.  We
shouldn't let root execute any file, only ones which have the 'x' bit
set.  Therefore this narrows the fix in bug 1106548 so it only applies
to read and write bits.

To test this I created a disk with guestfish which had an executable
and a non-executable file:

  $ guestfish -N fs -m /dev/sda1
  ><fs> touch /file1
  ><fs> touch /file2
  ><fs> chmod 0755 /file1
  ><fs> ll /
  total 24
  drwxr-xr-x  3 root root  4096 Oct 12 14:04 .
  drwxr-xr-x 19 root root  4096 Oct 12 14:04 ..
  -rwxr-xr-x  1 root root     0 Oct 12 14:04 file1
  -rw-r--r--  1 root root     0 Oct 12 14:04 file2
  drwx------  2 root root 16384 Oct 12 14:04 lost+found

I then mounted and tested it as non-root:

  $ guestmount -a test1.img -m /dev/sda1 /tmp/mnt -v -x
  $ ls -l /tmp/mnt
  total 16
  -rwxr-xr-x. 1 root root     0 Oct 12 15:04 file1
  -rw-r--r--. 1 root root     0 Oct 12 15:04 file2
  drwx------. 2 root root 16384 Oct 12 15:04 lost+found
  $ test -x /tmp/mnt/file1; echo $?
  0
  $ test -x /tmp/mnt/file2; echo $?
  1

and as root:

  $ sudo guestmount -a test1.img -m /dev/sda1 /tmp/mnt -v -x
  $ test -x /tmp/mnt/file1; echo $?
  0
  $ test -x /tmp/mnt/file2; echo $?
  0

In the debug output for non-root we can see the difference:

  libguestfs: /file1: testing access mask X_OK: caller UID:GID = 1000:1000, file UID:GID = 0:0, file mode = 100755, result = OK
  libguestfs: /file2: testing access mask X_OK: caller UID:GID = 1000:1000, file UID:GID = 0:0, file mode = 100644, result = EACCESS

and for root:

  libguestfs: /file1: testing access mask X_OK: caller UID:GID = 0:0, file UID:GID = 0:0, file mode = 100755, result = OK
  libguestfs: /file2: testing access mask X_OK: caller UID:GID = 0:0, file UID:GID = 0:0, file mode = 100644, result = OK

After this commit the root output changes to this (ie. same decision
as non-root):

  libguestfs: /file1: testing access mask X_OK: caller UID:GID = 0:0, file UID:GID = 0:0, file mode = 100755, result = OK
  libguestfs: /file2: testing access mask X_OK: caller UID:GID = 0:0, file UID:GID = 0:0, file mode = 100644, result = EACCESS
2020-10-12 15:17:41 +01:00
Richard W.M. Jones
4663112d89 lib/canonical-name.c: Hide errors from underlying API call.
When guestfs_lvm_canonical_lv_name was called with a /dev/dm* or
/dev/mapper* name which was not an LV then a noisy error would be
printed.  This would typically have happened with encrypted disks, and
now happens very noticably when inspecting Windows BitLocker-
encrypted guests.

This commit hides this error in all cases, although it is still logged
to debug.  See comment and the thread below for detailed rationale.

https://www.redhat.com/archives/libguestfs/2020-October/thread.html#00055
2020-10-12 10:46:10 +01:00
Richard W.M. Jones
c456ea0332 New APIs: cryptsetup-open and cryptsetup-close.
This commit deprecates luks-open/luks-open-ro/luks-close for the more
generic sounding names cryptsetup-open/cryptsetup-close, which also
correspond directly to the cryptsetup commands.

The optional cryptsetup-open readonly flag is used to replace the
functionality of luks-open-ro.

The optional cryptsetup-open crypttype parameter can be used to select
the type (corresponding to cryptsetup open --type), which allows us to
open BitLocker-encrypted disks with no extra effort.  As a convenience
the crypttype parameter may be omitted, and libguestfs will use a
heuristic (based on vfs-type output) to try to determine the correct
type to use.

The deprecated functions and the new functions are all (re-)written in
OCaml.

There is no new test here, unfortunately.  It would be nice to test
Windows BitLocker support in this new API, however the Linux tools do
not support creating BitLocker disks, and while it is possible to
create one under Windows, the smallest compressed disk I could create
is 37M because of a mixture of the minimum support size for BitLocker
disks and the fact that encrypted parts of NTFS cannot be compressed.

Also synchronise with common module.
2020-10-12 10:44:08 +01:00
Pino Toscano
dbfab7d3b2 build: fix includedir in uninstalled libguestfs.pc
Update includedir with the new directory that contains guestfs.h.

Updates commit 75abec1f70.
2020-09-22 18:12:05 +02:00
Richard W.M. Jones
75abec1f70 include: Move lib/guestfs.h to include/guestfs.h
This brings libguestfs into line with other projects which have a
separate include/ directory for the public header.

It's also the case that <guestfs.h> has never particularly belonged in
the lib/ subdirectory.  Some tools add -Ilib/ but they only need
<guestfs.h> and not any other headers from that directory, and
separating out the public header allows us to clean those up.  This is
certainly the case for examples, and some language bindings and some
tests.

In future I'm hopeful we can use this as the basis to tease out other
dependencies, as a prelude to separating them out from the repo.
2020-09-21 18:38:28 +01:00
Yuri Chornoivan
fce82fe55a Fix minor typos 2020-08-24 16:24:38 +01:00
Andrey Shinkevich
3cad943a85 appliance: extract UUID from QCOW2 disk image
For the appliance of the QCOW2 format, the function get_root_uuid()
fails to get the UUID of the disk image.
In this case, let us read the first 256k bytes of the disk image  with
the 'qemu-img dd' command. Then pass the read block to the 'file'
command.

Suggested-by: Denis V. Lunev <den@openvz.org>
Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
2020-08-13 10:11:09 +01:00
Richard W.M. Jones
eb78e990ac daemon, lib: Replace deprecated security_context_t with char *.
This gives deprecation warnings.  It always was simply a char *, and
the recommendation upstream is to replace uses with char *:

9eb9c93275
2020-07-30 13:58:35 +01:00
Richard W.M. Jones
224f373043 lib: Increase default memsize to 1280 (RHBZ#1837765).
Argon2 is the default LUKS Password-Based Key Derivation Function
(PBKDF) for some new guests such as RHEL 8.2 and Fedora.  It is
designed to be "memory hard", meaning that by design it requires large
amounts of memory, making it expensive to brute-force.  Unfortunately
the default for guests which had more than a few GB of RAM at install
time is to require about 1 GB of RAM to decrypt the block device,
which is considerably larger than the default available in the
libguestfs appliance.

To make it possible to open these encrypted disks we need to make the
appliance larger.  This could be done as a one-off, and the current
workaround is simply to set LIBGUESTFS_MEMSIZE=2048 or a similar
amount.  However since we don't know in advance whether we could be
dealing with an encrypted disk, partition, etc. or what PBKDF it uses,
the only way to deal with this in all circumstances is to increase the
default memsize.  This commit increases it quite a lot (768 -> 1280)
which is unfortunate.

Note as there is some confusion on this point: Since libguestfs does
not attempt to decrypt disks in parallel, you only need ~ 1GB in
total, not per encrypted disk.

For a reproducer, see:
https://bugzilla.redhat.com/show_bug.cgi?id=1837765#c14
2020-07-17 10:47:18 +01:00
Pino Toscano
7265f08ce9 lib: remove extra @LIBS@ from pkg-config file
At the moment it is empty, so probably it does not exist. Remove it to
avoid adding spurious content to the pkg-config file in case that
variable will get a value in the future.
2020-03-12 11:45:30 +01:00
Richard W.M. Jones
e33b3c83a0 build: Allow C programs using libguestfs to be compiled against build dir.
We use a similar trick to libvirt to allow external C programs that
use libguestfs to be compiled against the built (but not installed)
libguestfs with:

  ../libguestfs/run ./configure
  make

What actually happens is we have a second pkg-config file
(lib/local/libguestfs.pc) which points to the locally built
libguestfs.  The ./run script sets up PKG_CONFIG_PATH to point to this
directory.  Assuming that ./configure is using pkg-config/pkgconf and
not some other half-baked solution it will pick up the libguestfs.pc
file from here which will set CFLAGS and LIBS appropriately.
2020-03-12 10:05:39 +00:00
Richard W.M. Jones
786dba91d1 Revert "lib: Autodetect backing format and specify it explicitly."
This reverts commit 92fd5d5d40.

See discussion here:
https://www.redhat.com/archives/libguestfs/2020-March/thread.html#00041
2020-03-09 12:54:05 +00:00
Richard W.M. Jones
0e17236d7d Update copyright dates to 2020. 2020-03-06 19:32:32 +00:00
Richard W.M. Jones
18c3f40c60 appliance: Pass root=UUID=<uuid> instead of appliance device name (RHBZ#1804207).
Appliance device names are not reliable since the kernel no longer
enumerates virtio-scsi devices serially.  Instead get the UUID of the
appliance and pass this as the parameter.

Note this requires supermin >= 5.1.18 (from around July 2017).
2020-03-06 19:03:03 +00:00
Richard W.M. Jones
bca9b94fc5 daemon: Translate device names if Linux device ordering is unstable (RHBZ#1804207).
Linux from around 5.6 now enumerates individual disks in any order
(whereas previously it enumerated only drivers in parallel).  This
means that /dev/sdX ordering is no longer stable - in particular we
cannot be sure that /dev/sda inside the guest is the first disk that
was attached to the appliance, /dev/sdb the second disk and so on.

However we can still use SCSI PCI device numbering as found in
/dev/disk/by-path.  Use this to translate device names in and out of
the appliance.

Thanks: Vitaly Kuznetsov, Paolo Bonzini, Dan Berrangé.
2020-03-06 19:03:03 +00:00
Richard W.M. Jones
92fd5d5d40 lib: Autodetect backing format and specify it explicitly.
In the guestfs_disk_create API we have traditionally allowed you to
set backingfile without setting backingformat.  The meaning of this is
to let qemu autodetect the backing format when opening the overlay
disk.

However libvirt >= 6.0 refuses to even pass such disks to qemu (see
https://bugzilla.redhat.com/show_bug.cgi?id=1798148).

For this reason, move the autodetection earlier and make it explicit.
We now autodetect the format of the backing disk at the time of
creation of the overlay, and set that as the backing format in the
overlay disk itself, allowing libvirt to open the disk later.
2020-03-06 19:03:03 +00:00
Richard W.M. Jones
0eb8d428a2 lib: Fix leak of XPath objects.
These are two unrelated leaks of XPath objects, both found by valgrind.

Fixes commit 9484136fd0
and commit 94843f155a.
2020-03-06 13:10:10 +00:00
Richard W.M. Jones
3cea2cfe04 lib: Move guestfs_device_index impl from daemon to library.
This function doesn't work reliably with the proposed change to device
name translation.  The reason is that strings returned by
Devsparts.list_devices contained translated names, so their indexes
did not correspond to the untranslated names used outside the
appliance..

We can avoid this and make the function much simpler and faster by
implementing it on the library side instead.
2020-03-05 13:18:27 +00:00
Nikolay Ivanets
94843f155a lib: add support for disks with 4096 bytes sector size
Nowadays there are hard drives and operating systems which support
"4K native" sector size.  In this mode physical and logical block size
exposed to the operating system is equal to 4096 bytes.

GPT partition table (as a known example) being created in this mode will
place GPT header at LBA1 which is 4096 bytes.  libguetfs is unable to
recognize partition table on such physical block devices or disk images.
The reason is that libguestfs appliance will look for a GPT header at
LBA1 which is seen at 512 byte offset.

In order to fix the issue we need a way to provide correct logical block
size for attached disks.  Fortunately QEMU and libvirt already provides
a way to specify physical/logical block size per disk basis.

After discussion in a mailing list we agreed that physical block size is
rarely used and is not so important.  Thus both physical and logical
block size will be set to the same value.

In this patch one more optional parameter 'blocksize' is added
to add_drive_opts API method.  Valid values are 512 and 4096.

add_drive_scratch has the same optional parameter for a consistency and
testing purpose.

add-domain and add_libvirt_dom will pass logical_block_size value from
libvirt XML to add_drive_opts method.
2020-02-11 15:20:09 +00:00
Daria Phoebe Brashear
56834875b2 properly initialize error_data_lock_list before use
when a handle is allocated, the error_data_list_lock must be initialized
2020-02-06 13:23:31 +00:00
Pino Toscano
a754cd4307 launch: libvirt: use machine type when querying for domcaps
On some architectures a certain machine type is used, so use it when
querying the libvirt domain capabilities.
2020-01-20 13:00:12 +01:00
Pino Toscano
e636e64d34 lib: uefi: use the efi libvirt firmware if available
In case libvirt supports the firmware autoselection and there is an EFI
firmware available, use it directly instead of handling the firmware
manually.
2020-01-20 13:00:12 +01:00
Pino Toscano
029901113c lib: allow to use libvirt firmware autoselection
Enhance the UEFI firmware lookup function with the information on the
libvirt firmware autoselection, allowing it to return a value to use for
the appliance.

At the moment no firmware is selected this way, so there is no behaviour
change.
2020-01-20 13:00:06 +01:00
Pino Toscano
777a21b16b lib: uefi: reset out parameters earlier
Make sure they are always reset, no matter the code branches later on.

This is mostly code motion.
2020-01-20 13:00:06 +01:00
Pino Toscano
9484136fd0 launch: libvirt: parse firmware autoselection
Parse from the domain capabilities whether libvirt supports the
autoselection of firmware, and which values are supported.
2020-01-20 13:00:06 +01:00
Pino Toscano
eb43478f83 inspect: avoid returning "unknownX.Y" for unknown Linux distros
If it is not possible to detect the distribution of a Linux OS, do not
propose "unknownX.Y" (where X is the major version number, and Y the
minor) as short osinfo ID. Just return "unknown" instead.
2020-01-09 14:57:59 +01:00
Pino Toscano
8bd80847fe inspect: correct osinfo ID for CentOS >= 8
CentOS 8 is represented with a simpler "centos8" osinfo ID.
2019-12-11 13:35:30 +01:00
Pino Toscano
206ce8bbf1 New API: luks_uuid
Return the UUID of a LUKS device.
2019-11-29 12:04:55 +01:00
Pino Toscano
72d83cc6eb build: remove unused gnulib modules
Remove gnulib modules that provide stuff clearly not used within
libguestfs (library, daemon, and C tools).  Among directly and
indirectly modules used previous (and now no more), they are:

  cycle-check
  d-ino
  dev-ino
  dup3
  dup3-tests
  fcntl-safer
  fcntl-safer-tests
  fdopendir
  fdopendir-tests
  filevercmp
  filevercmp-tests
  ftell
  ftell-tests
  ftello
  ftello-tests
  fts
  getaddrinfo
  getaddrinfo-tests
  getcwd
  getcwd-tests
  gnu-make
  hostent
  i-ring
  i-ring-tests
  iconv
  iconv-tests
  inet_ntop
  inet_ntop-tests
  isatty
  isatty-tests
  openat-safer
  openat-safer-tests
  opendirat
  ptsname_r
  ptsname_r-tests
  read-file
  read-file-tests
  rewinddir
  servent
  ttyname_r
  ttyname_r-tests
  xgetcwd

Some of the removed modules are still used pulled indirectly as
dependency of other modules.  There should be no behaviour change on
recent Linux distros, although older distros were not tested (adding
a module back is easy, anyway).

Remove accordingly unused automake variables, and ignored files.
2019-11-28 13:09:21 +01:00
Richard W.M. Jones
85c99edec1 v2v: Remove virt-v2v.
It has moved to a new repository:
https://github.com/libguestfs/virt-v2v
2019-11-13 12:49:55 +00:00
Eric Blake
730642676b drives: Typo fix
Favor 'atomically' over 'atomicly'.

Signed-off-by: Eric Blake <eblake@redhat.com>
2019-09-10 16:40:17 +01:00
Pino Toscano
c9543de73d launch: libvirt: fix custom hypervisor check
Previously, is_custom_hv() used to compare the QEMU executable found
during configure to the hypervisor set to check whether it is a custom
one; however, the QEMU found at configure time can be different than
what libvirt was configured with.

This fixes the libvirt backend when libguestfs is configured with a
different QEMU, that now will be specified as emulator overriding the
libvirt one.
2019-05-24 14:49:05 +02:00
Pino Toscano
797925cc3c launch: libvirt: get default QEMU from domcapabilities
Parse the libvirt domain capabilities for the default emulator
capabilities, and get the path to the default QEMU executable.
2019-05-24 14:49:05 +02:00
Pino Toscano
9d1fc91cca build: stop using GUESTFS_WARN_DEPRECATED
This is no more used now, as compiler deprecation warnings are triggered
by default.
2019-04-23 18:08:19 +02:00
Pino Toscano
3720900438 inspect: get icon of OpenMandriva guests
Followup of commit a4ef6716b4.
2019-04-08 13:34:11 +02:00
Pino Toscano
c4205809bc inspect: correct osinfo ID for ALT Linux >= 8
ALT Linux 8.x has different IDs in osinfo-db, so return the proper IDs
for the newest stable series.
2019-04-04 10:25:03 +02:00
Pino Toscano
764ee8c74e inspect: return osinfo short IDs for rolling distros
Return the right osinfo short IDs for some rolling Linux distributions,
such as Arch Linux, Gentoo, and Void Linux.  Their IDs were recently
added to osinfo-db.
2019-04-04 10:25:03 +02:00