All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, den@virtuozzo.com,
	andrey.drobyshev@virtuozzo.com, jsnow@redhat.com,
	vsementsov@yandex-team.ru, kwolf@redhat.com, hreitz@redhat.com
Subject: Re: [PATCH] block: Use LVM tools for LV block device truncation
Date: Mon, 11 Mar 2024 18:24:47 +0000	[thread overview]
Message-ID: <Ze9Mb6pHcTOdghCy@redhat.com> (raw)
In-Reply-To: <20240311174044.67094-1-alexander.ivanov@virtuozzo.com>

On Mon, Mar 11, 2024 at 06:40:44PM +0100, Alexander Ivanov wrote:
> If a block device is an LVM logical volume we can resize it using
> standard LVM tools.
> 
> In raw_co_truncate() check if the block device is a LV using lvdisplay
> and resize it executing lvresize.
> 
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
> ---
>  block/file-posix.c | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
> 
> diff --git a/block/file-posix.c b/block/file-posix.c
> index 35684f7e21..cf8a04e6f7 100644
> --- a/block/file-posix.c
> +++ b/block/file-posix.c
> @@ -2670,6 +2670,33 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
>      if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
>          int64_t cur_length = raw_getlength(bs);
>  
> +        /*
> +         * Check if the device is an LVM logical volume and try to resize
> +         * it using LVM tools.
> +         */
> +        if (S_ISBLK(st.st_mode) && offset > 0) {
> +            char cmd[PATH_MAX + 32];
> +
> +            snprintf(cmd, sizeof(cmd), "lvdisplay %s > /dev/null",
> +                     bs->filename);

PATH_MAX + snprint is a bad practice - g_strdup_printf() is recommended
for dynamic allocation, along with g_autofree for release.

> +            ret = system(cmd);

IMHO using 'system' for spawning processes is dubious in any non-trivial
program.

Historically at least it does not have well defined behaviour wrt signal
handling in the child, before execve is called. ie potentially a signal
handler registered by QEMU in the parent could run in the child having
ill-effects.

'system' also executes via the shell which opens up many risks with
unsanitized files path being substituted into the command line.

> +            if (ret != 0) {
> +                error_setg(errp, "lvdisplay returned %d error for '%s'",
> +                           ret, bs->filename);
> +                return ret;
> +            }

Calling 'lvdisplay' doesn't seem to be needed. Surely 'lvresize' is
going to report errors if it isn't an LVM device.

If we want to detect an LVM device though, couldn't we lookup
'device-mapper'  in /proc/devices and then major the device major
node number.

> +
> +            snprintf(cmd, sizeof(cmd), "lvresize -f -L %ldB %s > /dev/null",
> +                     offset, bs->filename);
> +            ret = system(cmd);
> +            if (ret != 0) {
> +                error_setg(errp, "lvresize returned %d error for '%s'",
> +                           ret, bs->filename);

lvresize might display an message on stderr on failure but that's at best
going to QEMU's stderr. Any error needs to be captured and put into
this error message that's fed back to the user / QMP client.

> +            }
> +
> +            return ret;
> +        }
> +

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



  reply	other threads:[~2024-03-11 18:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-11 17:40 [PATCH] block: Use LVM tools for LV block device truncation Alexander Ivanov
2024-03-11 18:24 ` Daniel P. Berrangé [this message]
2024-03-12 17:04   ` Alexander Ivanov
2024-03-12 17:13     ` Daniel P. Berrangé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Ze9Mb6pHcTOdghCy@redhat.com \
    --to=berrange@redhat.com \
    --cc=alexander.ivanov@virtuozzo.com \
    --cc=andrey.drobyshev@virtuozzo.com \
    --cc=den@virtuozzo.com \
    --cc=hreitz@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@yandex-team.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.