2009-09-25 13:20:13 +00:00
|
|
|
/*
|
2012-12-13 15:25:48 +00:00
|
|
|
* virstoragefile.c: file utility functions for FS storage backend
|
2009-09-25 13:20:13 +00:00
|
|
|
*
|
2017-03-27 13:11:26 +00:00
|
|
|
* Copyright (C) 2007-2017 Red Hat, Inc.
|
2009-09-25 13:20:13 +00: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 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2009-09-25 13:20:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2021-01-21 15:47:52 +00:00
|
|
|
#include "virstoragefile.h"
|
2009-09-25 13:20:13 +00:00
|
|
|
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.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"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2009-09-29 08:34:48 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_STORAGE
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.storagefile");
|
|
|
|
|
2009-09-29 08:34:48 +00:00
|
|
|
|
2012-09-20 14:24:47 +00:00
|
|
|
#ifdef WITH_UDEV
|
2019-01-15 21:11:48 +00:00
|
|
|
/* virStorageFileGetSCSIKey
|
|
|
|
* @path: Path to the SCSI device
|
|
|
|
* @key: Unique key to be returned
|
2019-01-15 20:59:21 +00:00
|
|
|
* @ignoreError: Used to not report ENOSYS
|
2019-01-15 21:11:48 +00: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 20:59:21 +00:00
|
|
|
char **key,
|
2019-10-14 12:45:33 +00:00
|
|
|
bool ignoreError G_GNUC_UNUSED)
|
2011-07-20 09:40:53 +00:00
|
|
|
{
|
2012-12-11 19:10:51 +00:00
|
|
|
int status;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2019-01-31 18:16:44 +00: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 09:40:53 +00: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 18:16:44 +00:00
|
|
|
return -2;
|
2011-07-20 09:40:53 +00: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 09:40:53 +00:00
|
|
|
if (nl)
|
|
|
|
*nl = '\0';
|
|
|
|
}
|
|
|
|
|
2012-12-11 19:10:51 +00:00
|
|
|
if (*key && STREQ(*key, ""))
|
|
|
|
VIR_FREE(*key);
|
|
|
|
|
2019-01-31 18:16:44 +00:00
|
|
|
return 0;
|
2011-07-20 09:40:53 +00:00
|
|
|
}
|
|
|
|
#else
|
2012-12-11 19:10:51 +00:00
|
|
|
int virStorageFileGetSCSIKey(const char *path,
|
2019-10-14 12:45:33 +00:00
|
|
|
char **key G_GNUC_UNUSED,
|
2019-01-15 20:59:21 +00:00
|
|
|
bool ignoreError)
|
2011-07-20 09:40:53 +00:00
|
|
|
{
|
2019-01-15 20:59:21 +00:00
|
|
|
if (!ignoreError)
|
2023-03-09 14:13:48 +00:00
|
|
|
virReportSystemError(ENOSYS, _("Unable to get SCSI key for %1$s"), path);
|
2012-12-11 19:10:51 +00:00
|
|
|
return -1;
|
2011-07-20 09:40:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-10-12 22:29:14 +00:00
|
|
|
|
2017-02-23 16:10:43 +00:00
|
|
|
|
2019-01-16 00:07:07 +00: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 13:16:31 +00:00
|
|
|
g_autofree char *outbuf = NULL;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2019-01-31 18:16:44 +00:00
|
|
|
|
|
|
|
cmd = virCommandNewArgList("/lib/udev/scsi_id",
|
|
|
|
"--replace-whitespace",
|
|
|
|
"--whitelisted",
|
|
|
|
"--export",
|
|
|
|
"--device", path,
|
|
|
|
NULL
|
|
|
|
);
|
2019-01-16 00:07:07 +00:00
|
|
|
*key = NULL;
|
|
|
|
|
|
|
|
/* Run the program and capture its output */
|
|
|
|
virCommandSetOutputBuffer(cmd, &outbuf);
|
|
|
|
if (virCommandRun(cmd, &status) < 0)
|
2019-01-31 18:16:44 +00:00
|
|
|
return -2;
|
2019-01-16 00:07:07 +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 && *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 13:26:14 +00:00
|
|
|
*key = g_strdup_printf("%s_PORT%s", serial, port);
|
2019-01-16 00:07:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 18:16:44 +00:00
|
|
|
return 0;
|
2019-01-16 00:07:07 +00:00
|
|
|
}
|
|
|
|
#else
|
2019-10-14 12:45:33 +00:00
|
|
|
int virStorageFileGetNPIVKey(const char *path G_GNUC_UNUSED,
|
|
|
|
char **key G_GNUC_UNUSED)
|
2019-01-16 00:07:07 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-21 14:44:53 +00:00
|
|
|
|
2017-02-23 16:10:43 +00: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 08:21:32 +00:00
|
|
|
g_auto(GStrv) strings = NULL;
|
2017-02-23 16:10:43 +00:00
|
|
|
|
|
|
|
*chainIndex = 0;
|
|
|
|
|
2021-03-22 16:00:41 +00:00
|
|
|
if (!(strings = g_strsplit(str, "[", 2)))
|
2017-02-23 16:10:43 +00:00
|
|
|
return -1;
|
|
|
|
|
2021-03-22 16:00:41 +00:00
|
|
|
if (strings[0] && strings[1]) {
|
2017-02-23 16:10:43 +00:00
|
|
|
if (virStrToLong_uip(strings[1], &suffix, 10, &idx) < 0 ||
|
|
|
|
STRNEQ(suffix, "]"))
|
2019-01-31 17:18:35 +00:00
|
|
|
return -1;
|
2017-02-23 16:10:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
if (target)
|
|
|
|
*target = g_strdup(strings[0]);
|
2017-02-23 16:10:43 +00:00
|
|
|
|
|
|
|
*chainIndex = idx;
|
2019-01-31 17:18:35 +00:00
|
|
|
return 0;
|
2017-02-23 16:10:43 +00:00
|
|
|
}
|