libblockdev icon indicating copy to clipboard operation
libblockdev copied to clipboard

How to get the actual return code from bd_lvm_vgreduce() function

Open spictera opened this issue 4 years ago • 5 comments

Hi,

I would like to retry if the device is busy. But are unable to understand how to get the actuall error code from the command.

                ret = bd_lvm_vgreduce(vg_name,snapshot->device,NULL,&error);
                if (!ret) {
                        // ignore code 5:   Physical volume "/dev/nbd3" still in use
                        if(error->code != 5) {
                                printf(msg,"%s (%s, %d)\n", error->message, g_quark_to_string (error->domain), error->code);
                        }
                }

But this doesn't work.

Suggestions?

Regards Tomas

spictera avatar Jan 07 '22 22:01 spictera

I print out the error->code and error->domain, this is what I see:

ERRORCODE: 0 ERRORDOMAIN: 52 Process reported exit code 5: Physical volume "/dev/nbd3" still in use (g-bd-utils-exec-error-quark, 0)

How can I retry if the error code is with the above message? without have to perform a strcmp()

spictera avatar Jan 07 '22 22:01 spictera

Moved to libblockdev as indicated by the bd_ symbol prefix.

The exit code 5 is a return code from the particular lvm command call. However this is not always translated to a specific error in the BD_LVM_ERROR domain, like in this case.

tbzatek avatar Jan 08 '22 11:01 tbzatek

        Hi ThomasThanks for the replySo what is the best suggestion?to catch the error message, match the text using strcmp()? and then retry if there is a match?I tried to perform simpler way, by just retrying if there is an error, but that gives Glibc error as the error->xxx tries to expand the text.Why are you not using standard C routines where rc code is picked up by the caller, instead of a boolean?In that way it should be much simpler, as the caller can use the rc code to decide what action to perform anyhow, how can the bd_ function be called again?Do I have to release the error memory allocations before? and if so how?regards Tomas---- On Sat, 08 Jan 2022 12:06:09 +0100  ***@***.******@***.***> wrote ---- 

Moved to libblockdev as indicated by the bd_ symbol prefix. The exit code 5 is a return code from the particular lvm comman call. However this is not always translated to a specific error in the BD_LVM_ERROR domain, like in this case.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

spictera avatar Jan 08 '22 13:01 spictera

There are two reasons for not returning the error codes from the LVM plugin.

  1. We have two implementations of the plugin. The second one uses the LVM DBus API which doesn't propagate the error code and both implementations needs to have the same API.

  2. More importantly return codes from LVM are completely useless. For most commands they are basically boolean. vgreduce is actually a nice example:

# PV is in use by one or more LVs
[vtrefny@fedora ~]$ sudo vgreduce ssss /dev/sdc
Physical volume "/dev/sdc" still in use
[vtrefny@fedora ~]$ echo $?
5
# Invalid device
[vtrefny@fedora ~]$ sudo vgreduce ssss /dev/sdd
Cannot use /dev/sdd: device is partitioned
[vtrefny@fedora ~]$ echo $?
5
# Device that is not a PV
[vtrefny@fedora ~]$ sudo vgreduce ssss /dev/sdd1
Failed to find physical volume "/dev/sdd1".
[vtrefny@fedora ~]$ echo $?
5
# Non-existing device
[vtrefny@fedora ~]$ sudo vgreduce ssss /dev/sdf
Cannot use /dev/sdf: device not found
[vtrefny@fedora ~]$ echo $?
5
# Logical volume
[vtrefny@fedora ~]$ sudo vgreduce ssss /dev/mapper/ssss-dddd
Cannot use /dev/mapper/ssss-dddd: device is not in a usable state
[vtrefny@fedora ~]$ echo $?
5

So the 5 return code tells you absolutely nothing in this case.

We have special libblockdev errors for cases where the error codes make sense (I'm now talking about different plugins). We could add a new error code for your use case (pvmove is required before vgreduce) but that would only move the strncmp call from your code to libblockdev because we also don't have a way to get that from the return code. I instead recommend using bd_lvm_pvinfo to get information about the PV you are trying to remove and running bd_lvm_pvmove if it isn't free.

vojtechtrefny avatar Jan 12 '22 14:01 vojtechtrefny

Hmm, shall we strip the string from the message?

tbzatek avatar Jan 14 '22 17:01 tbzatek