2018-03-26 12:48:07 +00:00
|
|
|
/*
|
|
|
|
* virdevmapper.c: Functions for handling device mapper
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
#include "virdevmapper.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
2020-01-24 17:32:48 +00:00
|
|
|
#ifdef __linux__
|
2018-03-26 12:48:07 +00:00
|
|
|
# include <sys/sysmacros.h>
|
2020-07-23 14:02:00 +00:00
|
|
|
# include <linux/dm-ioctl.h>
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# include <fcntl.h>
|
2018-03-26 12:48:07 +00:00
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
# include "virthread.h"
|
|
|
|
# include "viralloc.h"
|
|
|
|
# include "virstring.h"
|
|
|
|
# include "virfile.h"
|
2020-08-19 11:35:55 +00:00
|
|
|
# include "virlog.h"
|
2020-07-23 14:02:00 +00:00
|
|
|
|
|
|
|
# define VIR_FROM_THIS VIR_FROM_STORAGE
|
|
|
|
|
2020-08-19 11:35:55 +00:00
|
|
|
VIR_LOG_INIT("util.virdevmapper");
|
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
# define PROC_DEVICES "/proc/devices"
|
|
|
|
# define DM_NAME "device-mapper"
|
|
|
|
# define DEV_DM_DIR "/dev/" DM_DIR
|
|
|
|
# define CONTROL_PATH DEV_DM_DIR "/" DM_CONTROL_NODE
|
|
|
|
# define BUF_SIZE (16 * 1024)
|
|
|
|
|
|
|
|
G_STATIC_ASSERT(BUF_SIZE > sizeof(struct dm_ioctl));
|
|
|
|
|
2018-03-26 12:48:07 +00:00
|
|
|
|
|
|
|
static int
|
2020-08-18 09:08:15 +00:00
|
|
|
virDevMapperGetMajor(unsigned int *major)
|
2018-03-26 12:48:07 +00:00
|
|
|
{
|
2020-07-23 14:02:00 +00:00
|
|
|
g_autofree char *buf = NULL;
|
|
|
|
VIR_AUTOSTRINGLIST lines = NULL;
|
|
|
|
size_t i;
|
|
|
|
|
2020-08-18 09:04:24 +00:00
|
|
|
if (!virFileExists(CONTROL_PATH))
|
|
|
|
return -2;
|
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
if (virFileReadAll(PROC_DEVICES, BUF_SIZE, &buf) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
lines = virStringSplit(buf, "\n", 0);
|
|
|
|
if (!lines)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; lines[i]; i++) {
|
|
|
|
g_autofree char *dev = NULL;
|
|
|
|
unsigned int maj;
|
|
|
|
|
|
|
|
if (sscanf(lines[i], "%u %ms\n", &maj, &dev) == 2 &&
|
|
|
|
STREQ(dev, DM_NAME)) {
|
2020-08-18 09:08:15 +00:00
|
|
|
*major = maj;
|
2020-07-23 14:02:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lines[i]) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to find major for %s"),
|
|
|
|
DM_NAME);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-26 12:48:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
static void *
|
|
|
|
virDMIoctl(int controlFD, int cmd, struct dm_ioctl *dm, char **buf)
|
|
|
|
{
|
|
|
|
size_t bufsize = BUF_SIZE;
|
|
|
|
|
|
|
|
reread:
|
|
|
|
*buf = g_new0(char, bufsize);
|
|
|
|
|
|
|
|
dm->version[0] = DM_VERSION_MAJOR;
|
|
|
|
dm->version[1] = 0;
|
|
|
|
dm->version[2] = 0;
|
|
|
|
dm->data_size = bufsize;
|
|
|
|
dm->data_start = sizeof(struct dm_ioctl);
|
|
|
|
|
|
|
|
memcpy(*buf, dm, sizeof(struct dm_ioctl));
|
|
|
|
|
|
|
|
if (ioctl(controlFD, cmd, *buf) < 0) {
|
|
|
|
VIR_FREE(*buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dm, *buf, sizeof(struct dm_ioctl));
|
|
|
|
|
|
|
|
if (dm->flags & DM_BUFFER_FULL_FLAG) {
|
|
|
|
bufsize += BUF_SIZE;
|
|
|
|
VIR_FREE(*buf);
|
|
|
|
goto reread;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *buf + dm->data_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-26 12:48:07 +00:00
|
|
|
static int
|
2020-07-23 14:02:00 +00:00
|
|
|
virDMOpen(void)
|
|
|
|
{
|
|
|
|
VIR_AUTOCLOSE controlFD = -1;
|
|
|
|
struct dm_ioctl dm;
|
|
|
|
g_autofree char *tmp = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&dm, 0, sizeof(dm));
|
|
|
|
|
2020-08-18 09:04:24 +00:00
|
|
|
if ((controlFD = open(CONTROL_PATH, O_RDWR)) < 0) {
|
2020-08-19 11:35:55 +00:00
|
|
|
/* We can't talk to devmapper. Produce a warning and let
|
|
|
|
* the caller decide what to do next. */
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
VIR_DEBUG("device mapper not available");
|
|
|
|
} else {
|
|
|
|
VIR_WARN("unable to open %s: %s",
|
|
|
|
CONTROL_PATH, g_strerror(errno));
|
|
|
|
}
|
|
|
|
return -2;
|
2020-08-18 09:04:24 +00:00
|
|
|
}
|
2020-07-23 14:02:00 +00:00
|
|
|
|
|
|
|
if (!virDMIoctl(controlFD, DM_VERSION, &dm, &tmp)) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to get device-mapper version"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dm.version[0] != DM_VERSION_MAJOR) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
|
|
|
|
_("Unsupported device-mapper version. Expected %d got %d"),
|
|
|
|
DM_VERSION_MAJOR, dm.version[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = controlFD;
|
|
|
|
controlFD = -1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
virDMSanitizepath(const char *path)
|
|
|
|
{
|
|
|
|
g_autofree char *dmDirPath = NULL;
|
|
|
|
struct dirent *ent = NULL;
|
|
|
|
struct stat sb[2];
|
2020-10-25 21:50:51 +00:00
|
|
|
g_autoptr(DIR) dh = NULL;
|
2020-07-23 14:02:00 +00:00
|
|
|
const char *p;
|
|
|
|
|
|
|
|
/* If a path is NOT provided then assume it's DM name */
|
|
|
|
p = strrchr(path, '/');
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return g_strdup(path);
|
|
|
|
else
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/* It's a path. Check if the last component is DM name */
|
|
|
|
if (stat(path, &sb[0]) < 0) {
|
|
|
|
virReportError(errno,
|
|
|
|
_("Unable to stat %p"),
|
|
|
|
path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dmDirPath = g_strdup_printf(DEV_DM_DIR "/%s", p);
|
|
|
|
|
|
|
|
if (stat(dmDirPath, &sb[1]) == 0 &&
|
|
|
|
sb[0].st_rdev == sb[1].st_rdev) {
|
|
|
|
return g_strdup(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The last component of @path wasn't DM name. Let's check if
|
|
|
|
* there's a device under /dev/mapper/ with the same rdev. */
|
|
|
|
if (virDirOpen(&dh, DEV_DM_DIR) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2020-08-03 11:36:39 +00:00
|
|
|
while (virDirRead(dh, &ent, DEV_DM_DIR) > 0) {
|
2020-07-23 14:02:00 +00:00
|
|
|
g_autofree char *tmp = g_strdup_printf(DEV_DM_DIR "/%s", ent->d_name);
|
|
|
|
|
|
|
|
if (stat(tmp, &sb[1]) == 0 &&
|
2020-11-13 09:45:30 +00:00
|
|
|
sb[0].st_rdev == sb[1].st_rdev) {
|
2020-10-27 21:49:11 +00:00
|
|
|
return g_steal_pointer(&tmp);
|
2020-07-23 14:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 21:49:11 +00:00
|
|
|
return NULL;
|
2020-07-23 14:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virDevMapperGetTargetsImpl(int controlFD,
|
|
|
|
const char *path,
|
2018-03-26 12:48:07 +00:00
|
|
|
char ***devPaths_ret,
|
|
|
|
unsigned int ttl)
|
|
|
|
{
|
2020-07-23 14:02:00 +00:00
|
|
|
g_autofree char *sanitizedPath = NULL;
|
|
|
|
g_autofree char *buf = NULL;
|
|
|
|
struct dm_ioctl dm;
|
|
|
|
struct dm_target_deps *deps = NULL;
|
2020-07-24 07:40:04 +00:00
|
|
|
VIR_AUTOSTRINGLIST devPaths = NULL;
|
2018-03-26 12:48:07 +00:00
|
|
|
size_t i;
|
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
memset(&dm, 0, sizeof(dm));
|
2018-03-26 12:48:07 +00:00
|
|
|
*devPaths_ret = NULL;
|
|
|
|
|
|
|
|
if (ttl == 0) {
|
|
|
|
errno = ELOOP;
|
2020-07-23 14:02:00 +00:00
|
|
|
return -1;
|
2018-03-26 12:48:07 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 10:14:33 +00:00
|
|
|
if (!virIsDevMapperDevice(path))
|
2020-04-24 11:17:51 +00:00
|
|
|
return 0;
|
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
if (!(sanitizedPath = virDMSanitizepath(path)))
|
|
|
|
return 0;
|
2018-03-26 12:48:07 +00:00
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
if (virStrncpy(dm.name, sanitizedPath, -1, DM_TABLE_DEPS) < 0) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
|
|
|
|
_("Resolved device mapper name too long"));
|
|
|
|
return -1;
|
2018-04-10 06:00:59 +00:00
|
|
|
}
|
2018-03-26 12:48:07 +00:00
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
deps = virDMIoctl(controlFD, DM_TABLE_DEPS, &dm, &buf);
|
|
|
|
if (!deps) {
|
|
|
|
if (errno == ENXIO)
|
|
|
|
return 0;
|
2018-03-26 12:48:07 +00:00
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to query dependencies for %s"),
|
|
|
|
path);
|
|
|
|
return -1;
|
2018-03-26 12:48:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 11:42:08 +00:00
|
|
|
devPaths = g_new0(char *, deps->count + 1);
|
2018-03-26 12:48:07 +00:00
|
|
|
for (i = 0; i < deps->count; i++) {
|
2019-10-22 13:26:14 +00:00
|
|
|
devPaths[i] = g_strdup_printf("/dev/block/%u:%u",
|
2020-07-23 14:02:00 +00:00
|
|
|
major(deps->dev[i]),
|
|
|
|
minor(deps->dev[i]));
|
2018-03-26 12:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < deps->count; i++) {
|
2020-07-24 07:40:04 +00:00
|
|
|
VIR_AUTOSTRINGLIST tmpPaths = NULL;
|
2018-03-26 12:48:07 +00:00
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
if (virDevMapperGetTargetsImpl(controlFD, devPaths[i], &tmpPaths, ttl - 1) < 0)
|
|
|
|
return -1;
|
2018-03-26 12:48:07 +00:00
|
|
|
|
2020-07-24 07:40:04 +00:00
|
|
|
if (virStringListMerge(&devPaths, &tmpPaths) < 0)
|
2020-07-23 14:02:00 +00:00
|
|
|
return -1;
|
2018-03-26 12:48:07 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
*devPaths_ret = g_steal_pointer(&devPaths);
|
2020-07-23 14:02:00 +00:00
|
|
|
return 0;
|
2018-03-26 12:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virDevMapperGetTargets:
|
|
|
|
* @path: devmapper target
|
|
|
|
* @devPaths: returned string list of devices
|
|
|
|
*
|
|
|
|
* For given @path figure out its targets, and store them in
|
|
|
|
* @devPaths array. Note, @devPaths is a string list so it's NULL
|
|
|
|
* terminated.
|
|
|
|
*
|
|
|
|
* If @path is not a devmapper device, @devPaths is set to NULL and
|
|
|
|
* success is returned.
|
|
|
|
*
|
|
|
|
* If @path consists of yet another devmapper targets these are
|
|
|
|
* consulted recursively.
|
|
|
|
*
|
|
|
|
* Returns 0 on success,
|
|
|
|
* -1 otherwise (with errno set, no libvirt error is
|
|
|
|
* reported)
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virDevMapperGetTargets(const char *path,
|
|
|
|
char ***devPaths)
|
|
|
|
{
|
2020-07-23 14:02:00 +00:00
|
|
|
VIR_AUTOCLOSE controlFD = -1;
|
2018-03-26 12:48:07 +00:00
|
|
|
const unsigned int ttl = 32;
|
|
|
|
|
|
|
|
/* Arbitrary limit on recursion level. A devmapper target can
|
|
|
|
* consist of devices or yet another targets. If that's the
|
|
|
|
* case, we have to stop recursion somewhere. */
|
|
|
|
|
2020-08-18 09:04:24 +00:00
|
|
|
if ((controlFD = virDMOpen()) < 0) {
|
|
|
|
if (controlFD == -2) {
|
2020-08-19 11:35:55 +00:00
|
|
|
/* The CONTROL_PATH doesn't exist or is unusable.
|
|
|
|
* Probably the module isn't loaded, yet. Don't error
|
|
|
|
* out, just exit. */
|
2020-08-18 09:04:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
return -1;
|
2020-08-18 09:04:24 +00:00
|
|
|
}
|
2020-07-23 14:02:00 +00:00
|
|
|
|
|
|
|
return virDevMapperGetTargetsImpl(controlFD, path, devPaths, ttl);
|
2018-03-26 12:48:07 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:57:14 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
virIsDevMapperDevice(const char *dev_name)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
2020-08-18 09:08:15 +00:00
|
|
|
unsigned int major;
|
2020-06-11 09:57:14 +00:00
|
|
|
|
2020-08-18 09:08:15 +00:00
|
|
|
if (virDevMapperGetMajor(&major) < 0)
|
2020-07-23 14:02:00 +00:00
|
|
|
return false;
|
|
|
|
|
2020-06-11 09:57:14 +00:00
|
|
|
if (!stat(dev_name, &buf) &&
|
|
|
|
S_ISBLK(buf.st_mode) &&
|
2020-08-18 09:08:15 +00:00
|
|
|
major(buf.st_rdev) == major)
|
2020-07-23 14:02:00 +00:00
|
|
|
return true;
|
2020-06-11 09:57:14 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:02:00 +00:00
|
|
|
#else /* !defined(__linux__) */
|
2020-06-11 09:57:14 +00:00
|
|
|
|
2020-07-23 14:30:56 +00:00
|
|
|
int
|
|
|
|
virDevMapperGetTargets(const char *path G_GNUC_UNUSED,
|
|
|
|
char ***devPaths G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-11 09:57:14 +00:00
|
|
|
bool
|
|
|
|
virIsDevMapperDevice(const char *dev_name G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-23 14:02:00 +00:00
|
|
|
#endif /* ! defined(__linux__) */
|