From nobody Mon Dec 15 01:54:24 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1522075404891940.8947708913103; Mon, 26 Mar 2018 07:43:24 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4DEBE7FD42; Mon, 26 Mar 2018 14:43:22 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 091D52CFCF; Mon, 26 Mar 2018 14:43:22 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id BD492181BA07; Mon, 26 Mar 2018 14:43:21 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w2QEh9Ng032021 for ; Mon, 26 Mar 2018 10:43:09 -0400 Received: by smtp.corp.redhat.com (Postfix) id 82DCA2026E04; Mon, 26 Mar 2018 14:43:09 +0000 (UTC) Received: from localhost.localdomain (ovpn-204-33.brq.redhat.com [10.40.204.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id E2F932026E03 for ; Mon, 26 Mar 2018 14:43:08 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Mon, 26 Mar 2018 16:43:01 +0200 Message-Id: <40750004097334a39aa537d4ab9e205bc04013f1.1522075241.git.mprivozn@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 1/3] util: Introduce virDevMapperGetTargets X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 26 Mar 2018 14:43:23 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This helper fetches dependencies for given device mapper target. At the same time, we need to provide a dummy log function because by default libdevmapper prints out error messages to stderr which we need to suppress. Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 4 ++ src/util/Makefile.inc.am | 2 + src/util/virdevmapper.c | 160 +++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virdevmapper.h | 32 ++++++++++ 4 files changed, 198 insertions(+) create mode 100644 src/util/virdevmapper.c create mode 100644 src/util/virdevmapper.h diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 03fe3b315f..3b7a19cff1 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1669,6 +1669,10 @@ virDBusMessageUnref; virDBusSetSharedBus; =20 =20 +# util/virdevmapper.h +virDevMapperGetTargets; + + # util/virdnsmasq.h dnsmasqAddDhcpHost; dnsmasqAddHost; diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am index a3c3b711fd..9624fb687c 100644 --- a/src/util/Makefile.inc.am +++ b/src/util/Makefile.inc.am @@ -35,6 +35,8 @@ UTIL_SOURCES =3D \ util/virdbus.c \ util/virdbus.h \ util/virdbuspriv.h \ + util/virdevmapper.c \ + util/virdevmapper.h \ util/virdnsmasq.c \ util/virdnsmasq.h \ util/virebtables.c \ diff --git a/src/util/virdevmapper.c b/src/util/virdevmapper.c new file mode 100644 index 0000000000..9717482da8 --- /dev/null +++ b/src/util/virdevmapper.c @@ -0,0 +1,160 @@ +/* + * virdevmapper.c: Functions for handling devmapper + * + * 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 + * . + * + * Authors: + * Michal Privoznik + */ + +#include + +#ifdef MAJOR_IN_MKDEV +# include +#elif MAJOR_IN_SYSMACROS +# include +#endif + +#ifdef WITH_DEVMAPPER +# include +#endif + +#include "virdevmapper.h" +#include "internal.h" +#include "virthread.h" +#include "viralloc.h" + +#ifdef WITH_DEVMAPPER +static void +virDevMapperDummyLogger(int level ATTRIBUTE_UNUSED, + const char *file ATTRIBUTE_UNUSED, + int line ATTRIBUTE_UNUSED, + int dm_errno ATTRIBUTE_UNUSED, + const char *fmt ATTRIBUTE_UNUSED, + ...) +{ + return; +} + +static int +virDevMapperOnceInit(void) +{ + /* Ideally, we would not need this. But libdevmapper prints + * error messages to stderr by default. Sad but true. */ + dm_log_with_errno_init(virDevMapperDummyLogger); + return 0; +} + + +VIR_ONCE_GLOBAL_INIT(virDevMapper) + +/** + * virDevMapperGetTargets: + * @path: multipath device + * @maj: returned array of MAJOR device numbers + * @min: returner array of MINOR device numbers + * @nmaj: number of items in @maj array + * + * For given @path figure out its targets, and store them in @maj + * and @min arrays. Both arrays have the same number of items + * upon return. + * + * If @path is not a multipath device, @ndevs is set to 0 and + * success is returned. + * + * If we don't have permissions to talk to kernel, -1 is returned + * and errno is set to EBADF. + * + * Returns 0 on success, + * -1 otherwise (with errno set, no libvirt error is + * reported) + */ +int +virDevMapperGetTargets(const char *path, + unsigned int **maj, + unsigned int **min, + size_t *nmaj) +{ + struct dm_task *dmt =3D NULL; + struct dm_deps *deps; + struct dm_info info; + size_t i; + int ret =3D -1; + + *nmaj =3D 0; + + if (virDevMapperInitialize() < 0) + goto cleanup; + + if (!(dmt =3D dm_task_create(DM_DEVICE_DEPS))) + goto cleanup; + + if (!dm_task_set_name(dmt, path)) { + if (errno =3D=3D ENOENT) { + /* It's okay, @path is not managed by devmapper =3D> + * not a multipath device. */ + ret =3D 0; + } + goto cleanup; + } + + dm_task_no_open_count(dmt); + + if (!dm_task_run(dmt)) + goto cleanup; + + if (!dm_task_get_info(dmt, &info)) + goto cleanup; + + if (!info.exists) { + ret =3D 0; + goto cleanup; + } + + if (!(deps =3D dm_task_get_deps(dmt))) + goto cleanup; + + if (VIR_ALLOC_N_QUIET(*maj, deps->count) < 0 || + VIR_ALLOC_N_QUIET(*min, deps->count) < 0) { + VIR_FREE(*maj); + goto cleanup; + } + *nmaj =3D deps->count; + + for (i =3D 0; i < deps->count; i++) { + (*maj)[i] =3D major(deps->device[i]); + (*min)[i] =3D minor(deps->device[i]); + } + + ret =3D 0; + cleanup: + dm_task_destroy(dmt); + return ret; +} + +#else /* ! WITH_DEVMAPPER */ + +int +virDevMapperGetTargets(const char *path ATTRIBUTE_UNUSED, + unsigned int **maj ATTRIBUTE_UNUSED, + unsigned int **min ATTRIBUTE_UNUSED, + size_t *nmaj ATTRIBUTE_UNUSED) +{ + errno =3D ENOSYS; + return -1; +} +#endif /* ! WITH_DEVMAPPER */ diff --git a/src/util/virdevmapper.h b/src/util/virdevmapper.h new file mode 100644 index 0000000000..e88ac0f8ce --- /dev/null +++ b/src/util/virdevmapper.h @@ -0,0 +1,32 @@ +/* + * virdevmapper.h: Functions for handling devmapper + * + * 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 + * . + * + * Authors: + * Michal Privoznik + */ + +#ifndef __VIR_DEVMAPPER_H__ +# define __VIR_DEVMAPPER_H__ + +int virDevMapperGetTargets(const char *path, + unsigned int **maj, + unsigned int **min, + size_t *nmaj); + +#endif /* __VIR_DEVMAPPER_H__ */ --=20 2.16.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list