105 Commits

Author SHA1 Message Date
Richard W.M. Jones
56da6b36d3 daemon: btrfs: Simplify snapshot code and fix invalid memory access
The existing code had a bug which you can demonstrate by doing:

  $ guestfish -N fs:btrfs:10G -m /dev/sda1 \
  btrfs-subvolume-create /sub :
  btrfs-subvolume-snapshot /sub /snap1 : \
  btrfs-subvolume-snapshot /sub /snap123 : \
  btrfs-subvolume-snapshot /sub /snap123456 : \
  btrfs-subvolume-show /sub
  ...
  libguestfs: error: appliance closed the connection unexpectedly.
  This usually means the libguestfs appliance crashed.

As the code for parsing the output and creating the comma-separated
list of snapshots was unncessarily complicated in the first place,
simplify it.  This also fixes the bug.

This also adds a regression test.

Thanks: Arye Yurkovsky
Link: https://lists.libguestfs.org/archives/list/guestfs@lists.libguestfs.org/thread/QV5VDHIH7WRUNAE54K6OEOKJMWL6M7EM/
2025-11-24 10:44:44 +00:00
Richard W.M. Jones
ea3dd97f1d New API: Replace btrfs-fsck with btrfs-scrub-full
The old btrfs-fsck API used "btrfs check" which appears to be broken
or deprecated.  The real tool you should use is "btrfs scrub".  We
have already implemented that API, but it is very awkward to use from
libguestfs.  In particular there's no existing way to run the scrub
and wait for it to finish.

Fix this by deprecating btrfs-fsck.  Implement a new API
btrfs-scrub-full which runs btrfs scrub in the foreground, waits for
it to finish, and handles errors.  It's much more like fsck tools in
other filesystems.

Thanks: Eric Sandeen
Fixes: https://issues.redhat.com/browse/RHEL-91936
2025-05-19 13:42:44 +01:00
Richard W.M. Jones
9b32056061 Fix miscellaneous spelling mistakes
$ git ls-files | xargs codespell
2025-04-29 19:05:07 +01:00
Richard W.M. Jones
72cfaff5c5 Update copyright dates for 2025
Automated using this command:

perl -pi.bak -e 's/(20[012][0-9])-20[12][01234]/$1-2025/g' `git ls-files`
2025-02-16 17:00:46 +00:00
Juergen Hoetzel
102e503648 daemon/btrfs: Use new syntax for balance command
Support for the obsolete short syntax 'btrfs balance /path' was
removed in btrfs-progs 6.6.
2023-12-14 16:53:50 +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
9108ad152d daemon/btrfs.c: Ignore mkfs.btrfs allocstart option.
This was deprecated in btrfs 4.14.1 and recently removed (see
btrfs-progs commit 4bd94dba8a "btrfs-progs: mkfs: remove alloc start
options and docs").  If the option is set simply ignore it.
2021-03-22 14:26:57 +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
0177552375 daemon: btrfs: Fix parsing for new output from btrfs scrub.
The format changed at some point breaking our existing parsing code.
Typical example of the new format is below.

UUID:             b8604449-c7b5-4d3b-9fad-56114e8594f5
Scrub started:    Wed Oct  2 12:54:06 2019
Status:           finished
Duration:         0:00:00
\tdata_extents_scrubbed: 0
\ttree_extents_scrubbed: 16
\tdata_bytes_scrubbed: 0
\ttree_bytes_scrubbed: 262144
\tread_errors: 0
\tcsum_errors: 0
\tverify_errors: 0
\tno_csum: 0
\tcsum_discards: 0
\tsuper_errors: 0
\tmalloc_errors: 0
\tuncorrectable_errors: 0
\tunverified_errors: 0
\tcorrected_errors: 0
\tlast_physical: 253493248
2019-10-02 14:29:35 +01:00
Pino Toscano
31ee088812 btrfs_balance_status: delay allocation of 'ret'
Allocate 'ret' as late as possible, so there is no risk that early
returns will leak it.
2018-08-06 15:36:46 +02:00
Pino Toscano
b91ec0697d daemon: btrfs: fix bad handling of a couple of errors
Remove two wrong error handling situations in btrfs_balance_status:
- if the calloc for 'ret' fails, the 'goto error' would try to
  dereference the resulting null pointer (to free the items of the
  return struct); hence, just return null directly, instead of jumping
  to 'error'
- if the strdup() for 'btrfsbalance_status' fails, then the directly
  return would leak 'ret'
2017-11-03 16:44:20 +01:00
Pino Toscano
8213a572e4 daemon: btrfs: remove dead check
In btrfs_subvolume_show, an extra check on 'key' is used in a place
where this variable is already checked to be non-null.
2017-11-03 16:33:09 +01:00
Richard W.M. Jones
eeda6edca1 daemon: Reimplement ‘btrfs_subvolume_list’ and ‘btrfs_subvolume_get_default’ in OCaml. 2017-07-27 22:31:22 +01:00
Richard W.M. Jones
82bbd9c8a5 daemon: Reimplement ‘mount’, ‘mount_ro’, ‘mount_options’, ‘mount_vfs’ APIs in OCaml.
Some of the oldest and most core APIs, reimplemented.

This also moves the strange ‘mount_vfs_nochroot’ function into
btrfs.c.
2017-07-27 22:31:22 +01:00
Richard W.M. Jones
381c8b68c4 daemon: Remove GUESTFSD_EXT_CMD.
GUESTFSD_EXT_CMD was used by OpenSUSE to track which external commands
are run by the daemon and package those commands into the appliance.

It is no longer used by recent SUSE builds, so remove it.

Thanks: Pino Toscano, Olaf Hering.
2017-07-27 17:31:41 +01:00
Richard W.M. Jones
707f5bcfe0 daemon: Link guestfsd with libutils.
After the previous refactoring, we are able to link the daemon to
common/utils, and also remove some of the "duplicate" functions that
the daemon carried ("duplicate" in quotes because they were often not
exact duplicates).

Also this removes the duplicate reimplementation of (most) cleanup
functions in the daemon, since those are provided by libutils now.

It also allows us in future (but not in this commit) to move utility
functions from the daemon into libutils.
2017-07-10 17:01:59 +01:00
Pino Toscano
2c41f001de btrfs_replace: fix position of subcommand options
btrfs-progs 4.11 is more strict with option parsing, so subcommand
options must appear now before the subcommand arguments.
2017-05-30 13:15:55 +02:00
Pino Toscano
12bfb3c631 btrfs_filesystem_show: work with another old btrfs version
An old version of btrfs-progs (3.17 in this case) has a different
version string, so adapt our filtering to include this line as well.

Related to/updates commit 839ae5bcd5.
2017-03-20 10:20:20 +01:00
Pino Toscano
c5fa352087 daemon: btrfs: check end_stringsbuf return values everywhere
Make sure to check the return value of end_stringsbuf everywhere, as
that would generate invalid string lists.
2017-03-06 15:04:24 +01:00
Richard W.M. Jones
d5a8f82887 Use 'const' for stack integers where possible.
May improve optimization possibilities in a few cases.
2016-07-26 10:43:45 +01:00
Pino Toscano
9fa8e07465 daemon: fix cleanup of stringsbuf usages
Declare most of the stringsbuf as CLEANUP_FREE_STRINGSBUF, so they are
freed completely on stack unwind: use take_stringsbuf() in return
places to take away from the stringsbuf its content, and remove all the
manual calls to free_stringslen (no more needed now).
This requires to not use free_stringslen anymore on failure in the
helper functions of stringsbuf, which now leave the content as-is (might
be still useful even on error).

This allows us to simplify the memory management of stringsbuf's, which
are not properly fully freed, fixing memory leaks in some error paths
(which were not calling free_stringslen).
2016-07-07 16:28:18 +02:00
Pino Toscano
6a06b87559 daemon: free the string on stringsbuf add failure
If add_string_nodup fails free the passed string instead of leaking it,
as that string would have been owned by the stringbuf.

Adapt few places to this behaviour.
2016-07-07 16:27:36 +02:00
Pino Toscano
839ae5bcd5 btrfs_filesystem_show: work with btrfs < 4.3.1
Versions older than 4.3.1 output also the version string after the
output.  Ignore such line, since it isn't relevant to what
btrfs_filesystem_show needs.
2016-05-16 14:28:26 +02:00
Pino Toscano
e3d429d37d New API: btrfs-filesystem-show (RHBZ#1164765)
Add a new API to list all the devices where a btrfs filesystem is
spanned.
2016-05-12 17:14:11 +02:00
Richard W.M. Jones
07c496c53c Use less stack.
GCC has two warnings related to large stack frames.  We were already
using the -Wframe-larger-than warning, but this reduces the threshold
from 10000 to 5000 bytes.

However that warning only covers the static part of frames (not
alloca).  So this change also enables -Wstack-usage=10000 which covers
both the static and dynamic usage (alloca and variable length arrays).

Multiple changes are made throughout the code to reduce frames to fit
within these new limits.

Note that stack allocation of large strings can be a security issue.
For example, we had code like:

 size_t len = strlen (fs->windows_systemroot) + 64;
 char software[len];
 snprintf (software, len, "%s/system32/config/software",
           fs->windows_systemroot);

where fs->windows_systemroot is guest controlled.  It's not clear what
the effects might be of allowing the guest to allocate potentially
very large stack frames, but at best it allows the guest to cause
libguestfs to segfault.  It turns out we are very lucky that
fs->windows_systemroot cannot be set arbitrarily large (see checks in
is_systemroot).

This commit changes those to large heap allocations instead.
2016-03-07 17:36:24 +00:00
Richard W.M. Jones
c0c91f96d7 daemon: btrfs: Use COMPILE_REGEXP macro to compile regular expressions. 2016-02-23 10:55:17 +00:00
Pino Toscano
474e3051c7 btrfs: also check return value of sysroot_path in btrfs_minimum_size
Followup of commit 79ac68030f.
2016-01-27 14:09:16 +01:00
Pino Toscano
79ac68030f btrfs: fix leak in btrfs_minimum_size
Make sure the result of sysroot_path is properly free'd.
2016-01-27 13:45:24 +01:00
Pino Toscano
fa262e2540 btrfs: fix wrapping in message in btrfs_minimum_size 2016-01-27 13:29:16 +01:00
Pino Toscano
c22dea9871 btrfs_subvolume_show: fix root/toplevel check w/ btrfs-progs >= 4.4
In trfs-progs 4.4 the error message has been changed, and our check did
not work anymore.  Yes, parsing user messages really sucks...
2016-01-25 19:53:42 +01:00
Richard W.M. Jones
120b658ccc Avoid various "declaration of <var> shadows a previous local"
I enabled the -Wshadow warning temporarily in order to do these fixes,
but had to disable it again afterwards.  The reason is that this warns
about shadowing globals, which is sort of a good thing, but because we
have a global called "verbose" just about everywhere, and at the same
time we baked a function argument called "verbose" into several
unchangable APIs, well, we're stuck without being able to use this
warning.
2015-11-13 11:53:12 +00:00
Maxim Perevedentsev
858a9de297 Added btrfs support to vfs_minimum_size. 2015-10-27 14:58:42 +00:00
Richard W.M. Jones
677c721e85 Fix whitespace.
Because of previous automated commits, such as changing 'guestfs___'
-> 'guestfs_int_', several function calls no longer lined up with
their parameters, and some lines were too long.

The bulk of this commit was done using emacs batch mode and the
technique described here:

  http://www.cslab.pepperdine.edu/warford/BatchIndentationEmacs.html

The changes suggested by emacs were then reviewed by hand.
2015-10-05 14:28:33 +01:00
Chen Hanxiao
c3edd076b9 daemon: add a space after func name to fit code-style
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-07-13 12:31:52 +01:00
Chen Hanxiao
16232ffd58 labels: move btrfslabel to btrfs.c
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-07-08 09:56:56 +02:00
Chen Hanxiao
a477e282db New API: set_uuid_random
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-07-03 11:31:41 +02:00
Chen Hanxiao
80cf46e110 uuid: add support to change uuid of btrfs partition
btrfs-progs v4.1 add support to change uuid of btrfs fs.

Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-07-02 11:08:42 +02:00
Richard W.M. Jones
336b8f6b16 tests: Add a test of btrfs-subvolume-list using the captive daemon. 2015-06-29 23:08:47 +01:00
Cao jin
36a197e2cc New API: btrfs_replace
Signed-off-by: Cao jin <caoj.fnst@cn.fujitsu.com>
2015-06-29 16:39:13 +02:00
Chen Hanxiao
1d58cd4e1d do_btrfs_subvolume_list: fix a bad return value
don't return a value which is to be freed.

Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-06-23 21:31:32 +01:00
Chen Hanxiao
268c425791 do_btrfs_qgroup_show: fix a bad return value
We should not use temporary lines buffer as return value,
for lines buffer will be freed.

Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-06-23 21:31:32 +01:00
Cao jin
68ed6dd88a Modify the function: analyze_line, make it more flexible
Modify the function from a fixed delimiter to a variable. So,
it can be used in more APIs later. Also modified the existing
callers

Signed-off-by: Cao jin <caoj.fnst@cn.fujitsu.com>
2015-06-23 13:28:31 +02:00
Chen Hanxiao
35710595c1 btrfs: remove redundant whitespace
s/  =/ =

Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-06-23 11:51:40 +02:00
Chen Hanxiao
74f5bb17a1 btrfs: fix leak in btrfs_balance_status
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-06-19 11:26:59 +01:00
Pino Toscano
9f683fbf0f btrfs: fix leak in btrfs_scrub_status
Properly free the output buffer.
2015-06-18 14:04:43 +02:00
Chen Hanxiao
596b8f85a9 btrfs: use CLEANUP_FREE_STRING_LIST for list free
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-06-17 11:05:36 +02:00
Chen Hanxiao
9c24314f6f btrfs: keep calloc and its error message match
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-06-17 11:05:32 +02:00
Pino Toscano
3f4ba2e98d btrfs: use calloc instead of malloc+memset
Small optimization, and eases the code.
2015-06-16 17:31:43 +02:00
Pino Toscano
9574e793e2 btrfs: remove unused 'out' variables
If we are not interested in output of a command being run, then there's
no need to still capture it in a (automatically freed, but still)
variable.

If any of those outputs is needed, a variable for it can be easily added
back.
2015-06-16 17:07:47 +02:00
Chen Hanxiao
52d1b0c593 New API: btrfs-image
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-03-24 13:35:28 +00:00