2009-09-25 14:20:13 +01:00
|
|
|
/*
|
2012-12-13 15:25:48 +00:00
|
|
|
* virstoragefile.c: file utility functions for FS storage backend
|
2009-09-25 14:20:13 +01:00
|
|
|
*
|
2017-03-27 08:11:26 -05:00
|
|
|
* Copyright (C) 2007-2017 Red Hat, Inc.
|
2009-09-25 14:20:13 +01:00
|
|
|
* Copyright (C) 2007-2008 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 16:30:55 -06:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 18:06:23 +08:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2009-09-25 14:20:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2021-01-21 16:47:52 +01:00
|
|
|
#include "virstoragefile.h"
|
2009-09-25 14:20:13 +01:00
|
|
|
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2014-06-23 10:40:49 -04:00
|
|
|
#include "viruuid.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-12 16:27:01 +00:00
|
|
|
#include "vircommand.h"
|
2012-10-13 10:47:15 -06:00
|
|
|
#include "virhash.h"
|
2013-04-03 12:36:23 +02:00
|
|
|
#include "virstring.h"
|
2014-05-02 19:22:17 +02:00
|
|
|
#include "virbuffer.h"
|
2017-09-15 15:21:35 -04:00
|
|
|
#include "virsecret.h"
|
2009-09-29 09:34:48 +01:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_STORAGE
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.storagefile");
|
|
|
|
|
2009-09-29 09:34:48 +01:00
|
|
|
|
2012-09-20 15:24:47 +01:00
|
|
|
#ifdef WITH_UDEV
|
2019-01-15 16:11:48 -05:00
|
|
|
/* virStorageFileGetSCSIKey
|
|
|
|
* @path: Path to the SCSI device
|
|
|
|
* @key: Unique key to be returned
|
2019-01-15 15:59:21 -05:00
|
|
|
* @ignoreError: Used to not report ENOSYS
|
2019-01-15 16:11:48 -05:00
|
|
|
*
|
|
|
|
* Using a udev specific function, query the @path to get and return a
|
|
|
|
* unique @key for the caller to use.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* 0 On success, with the @key filled in or @key=NULL if the
|
|
|
|
* returned string was empty.
|
|
|
|
* -1 When WITH_UDEV is undefined and a system error is reported
|
|
|
|
* -2 When WITH_UDEV is defined, but calling virCommandRun fails
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virStorageFileGetSCSIKey(const char *path,
|
2019-01-15 15:59:21 -05:00
|
|
|
char **key,
|
2019-10-14 14:45:33 +02:00
|
|
|
bool ignoreError G_GNUC_UNUSED)
|
2011-07-20 10:40:53 +01:00
|
|
|
{
|
2012-12-11 19:10:51 +00:00
|
|
|
int status;
|
2019-10-15 14:47:50 +02:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2019-01-31 13:16:44 -05:00
|
|
|
|
|
|
|
cmd = virCommandNewArgList("/lib/udev/scsi_id",
|
|
|
|
"--replace-whitespace",
|
|
|
|
"--whitelisted",
|
|
|
|
"--device", path,
|
|
|
|
NULL
|
|
|
|
);
|
2012-12-11 19:10:51 +00:00
|
|
|
*key = NULL;
|
2011-07-20 10:40:53 +01:00
|
|
|
|
|
|
|
/* Run the program and capture its output */
|
2012-12-11 19:10:51 +00:00
|
|
|
virCommandSetOutputBuffer(cmd, key);
|
|
|
|
if (virCommandRun(cmd, &status) < 0)
|
2019-01-31 13:16:44 -05:00
|
|
|
return -2;
|
2011-07-20 10:40:53 +01:00
|
|
|
|
2012-12-11 19:10:51 +00:00
|
|
|
/* Explicitly check status == 0, rather than passing NULL
|
|
|
|
* to virCommandRun because we don't want to raise an actual
|
|
|
|
* error in this scenario, just return a NULL key.
|
|
|
|
*/
|
|
|
|
if (status == 0 && *key) {
|
|
|
|
char *nl = strchr(*key, '\n');
|
2011-07-20 10:40:53 +01:00
|
|
|
if (nl)
|
|
|
|
*nl = '\0';
|
|
|
|
}
|
|
|
|
|
2012-12-11 19:10:51 +00:00
|
|
|
if (*key && STREQ(*key, ""))
|
|
|
|
VIR_FREE(*key);
|
|
|
|
|
2019-01-31 13:16:44 -05:00
|
|
|
return 0;
|
2011-07-20 10:40:53 +01:00
|
|
|
}
|
|
|
|
#else
|
2012-12-11 19:10:51 +00:00
|
|
|
int virStorageFileGetSCSIKey(const char *path,
|
2019-10-14 14:45:33 +02:00
|
|
|
char **key G_GNUC_UNUSED,
|
2019-01-15 15:59:21 -05:00
|
|
|
bool ignoreError)
|
2011-07-20 10:40:53 +01:00
|
|
|
{
|
2019-01-15 15:59:21 -05:00
|
|
|
if (!ignoreError)
|
|
|
|
virReportSystemError(ENOSYS, _("Unable to get SCSI key for %s"), path);
|
2012-12-11 19:10:51 +00:00
|
|
|
return -1;
|
2011-07-20 10:40:53 +01:00
|
|
|
}
|
|
|
|
#endif
|
2012-10-12 16:29:14 -06:00
|
|
|
|
2017-02-23 17:10:43 +01:00
|
|
|
|
2019-01-15 19:07:07 -05:00
|
|
|
#ifdef WITH_UDEV
|
|
|
|
/* virStorageFileGetNPIVKey
|
|
|
|
* @path: Path to the NPIV device
|
|
|
|
* @key: Unique key to be returned
|
|
|
|
*
|
|
|
|
* Using a udev specific function, query the @path to get and return a
|
|
|
|
* unique @key for the caller to use. Unlike the GetSCSIKey method, an
|
|
|
|
* NPIV LUN is uniquely identified by its ID_TARGET_PORT value.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* 0 On success, with the @key filled in or @key=NULL if the
|
|
|
|
* returned output string didn't have the data we need to
|
|
|
|
* formulate a unique key value
|
|
|
|
* -1 When WITH_UDEV is undefined and a system error is reported
|
|
|
|
* -2 When WITH_UDEV is defined, but calling virCommandRun fails
|
|
|
|
*/
|
|
|
|
# define ID_SERIAL "ID_SERIAL="
|
|
|
|
# define ID_TARGET_PORT "ID_TARGET_PORT="
|
|
|
|
int
|
|
|
|
virStorageFileGetNPIVKey(const char *path,
|
|
|
|
char **key)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
const char *serial;
|
|
|
|
const char *port;
|
2019-10-15 15:16:31 +02:00
|
|
|
g_autofree char *outbuf = NULL;
|
2019-10-15 14:47:50 +02:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2019-01-31 13:16:44 -05:00
|
|
|
|
|
|
|
cmd = virCommandNewArgList("/lib/udev/scsi_id",
|
|
|
|
"--replace-whitespace",
|
|
|
|
"--whitelisted",
|
|
|
|
"--export",
|
|
|
|
"--device", path,
|
|
|
|
NULL
|
|
|
|
);
|
2019-01-15 19:07:07 -05:00
|
|
|
*key = NULL;
|
|
|
|
|
|
|
|
/* Run the program and capture its output */
|
|
|
|
virCommandSetOutputBuffer(cmd, &outbuf);
|
|
|
|
if (virCommandRun(cmd, &status) < 0)
|
2019-01-31 13:16:44 -05:00
|
|
|
return -2;
|
2019-01-15 19:07:07 -05:00
|
|
|
|
|
|
|
/* Explicitly check status == 0, rather than passing NULL
|
|
|
|
* to virCommandRun because we don't want to raise an actual
|
|
|
|
* error in this scenario, just return a NULL key.
|
|
|
|
*/
|
|
|
|
if (status == 0 && *outbuf &&
|
|
|
|
(serial = strstr(outbuf, ID_SERIAL)) &&
|
|
|
|
(port = strstr(outbuf, ID_TARGET_PORT))) {
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
serial += strlen(ID_SERIAL);
|
|
|
|
port += strlen(ID_TARGET_PORT);
|
|
|
|
|
|
|
|
if ((tmp = strchr(serial, '\n')))
|
|
|
|
*tmp = '\0';
|
|
|
|
|
|
|
|
if ((tmp = strchr(port, '\n')))
|
|
|
|
*tmp = '\0';
|
|
|
|
|
|
|
|
if (*serial != '\0' && *port != '\0')
|
2019-10-22 15:26:14 +02:00
|
|
|
*key = g_strdup_printf("%s_PORT%s", serial, port);
|
2019-01-15 19:07:07 -05:00
|
|
|
}
|
|
|
|
|
2019-01-31 13:16:44 -05:00
|
|
|
return 0;
|
2019-01-15 19:07:07 -05:00
|
|
|
}
|
|
|
|
#else
|
2019-10-14 14:45:33 +02:00
|
|
|
int virStorageFileGetNPIVKey(const char *path G_GNUC_UNUSED,
|
|
|
|
char **key G_GNUC_UNUSED)
|
2019-01-15 19:07:07 -05:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-21 15:44:53 +01:00
|
|
|
|
2017-02-23 17:10:43 +01:00
|
|
|
/**
|
|
|
|
* virStorageFileParseBackingStoreStr:
|
|
|
|
* @str: backing store specifier string to parse
|
|
|
|
* @target: returns target device portion of the string
|
|
|
|
* @chainIndex: returns the backing store portion of the string
|
|
|
|
*
|
|
|
|
* Parses the backing store specifier string such as vda[1], or sda into
|
|
|
|
* components and returns them via arguments. If the string did not specify an
|
|
|
|
* index, 0 is assumed.
|
|
|
|
*
|
|
|
|
* Returns 0 on success -1 on error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virStorageFileParseBackingStoreStr(const char *str,
|
|
|
|
char **target,
|
|
|
|
unsigned int *chainIndex)
|
|
|
|
{
|
|
|
|
unsigned int idx = 0;
|
|
|
|
char *suffix;
|
2020-12-01 09:21:32 +01:00
|
|
|
g_auto(GStrv) strings = NULL;
|
2017-02-23 17:10:43 +01:00
|
|
|
|
|
|
|
*chainIndex = 0;
|
|
|
|
|
2021-03-22 17:00:41 +01:00
|
|
|
if (!(strings = g_strsplit(str, "[", 2)))
|
2017-02-23 17:10:43 +01:00
|
|
|
return -1;
|
|
|
|
|
2021-03-22 17:00:41 +01:00
|
|
|
if (strings[0] && strings[1]) {
|
2017-02-23 17:10:43 +01:00
|
|
|
if (virStrToLong_uip(strings[1], &suffix, 10, &idx) < 0 ||
|
|
|
|
STRNEQ(suffix, "]"))
|
2019-01-31 12:18:35 -05:00
|
|
|
return -1;
|
2017-02-23 17:10:43 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 13:49:46 +02:00
|
|
|
if (target)
|
|
|
|
*target = g_strdup(strings[0]);
|
2017-02-23 17:10:43 +01:00
|
|
|
|
|
|
|
*chainIndex = idx;
|
2019-01-31 12:18:35 -05:00
|
|
|
return 0;
|
2017-02-23 17:10:43 +01:00
|
|
|
}
|