[libvirt] [PATCH v2] Improve SCSI volume key generation

Eric Blake eblake at redhat.com
Mon Apr 4 17:12:17 UTC 2011


On 04/04/2011 10:59 AM, Daniel P. Berrange wrote:
> An update of patch 10 from:
> 
> http://www.redhat.com/archives/libvir-list/2010-November/msg00555.html
> 
> The SCSI volumes get a better 'key' field based on the fully
> qualified volume path. All SCSI volumes have a unique serial
> available in hardware which can be obtained by sending a
> suitable SCSI command. Call out to udev's 'scsi_id' command
> to fetch this value
> 
> +static char *
> +virStorageBackendSCSISerial(const char *dev)
> +{
> +    char *serial = NULL;
> +#ifdef HAVE_UDEV
> +    int fd = -1;
> +    FILE *list = NULL;
> +    char line[1024];
> +    virCommandPtr cmd = virCommandNewArgList(
> +        "/lib/udev/scsi_id",
> +        "--replace-whitespace",
> +        "--whitelisted",
> +        "--device", dev,
> +        NULL
> +        );
> +
> +    /* Run the program and capture its output */
> +    virCommandSetOutputFD(cmd, &fd);

Why not just run virCommandSetOutputBuffer, to capture the entire output
into a single string...

> +    if (virCommandRunAsync(cmd, NULL) < 0)
> +        goto cleanup;

...then you just use virCommandRun,

> +
> +    if ((list = VIR_FDOPEN(fd, "r")) == NULL) {

...you don't have to mess with fdopen,

> +        virStorageReportError(VIR_ERR_INTERNAL_ERROR,
> +                              "%s", _("cannot read fd"));
> +        goto cleanup;
> +    }
> +
> +    if (fgets(line, sizeof(line), list)) {

...and you don't have to mess with fgets,

> +cleanup:
> +    if (list)
> +        fclose(list);

and your cleanup gets a lot easier (not to mention that 'make
syntax-check' should have been telling you to use VIR_FORCE_FCLOSE if
you don't make those simplifications).

-- 
Eric Blake   eblake at redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 619 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20110404/53001b48/attachment-0001.sig>


More information about the libvir-list mailing list