From nobody Tue Dec 16 08:54:17 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 1522041437105274.25986507549464; Sun, 25 Mar 2018 22:17:17 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 56BFE80F7A; Mon, 26 Mar 2018 05:17:15 +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 2326B60F82; Mon, 26 Mar 2018 05:17:15 +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 B9C4E181BA03; Mon, 26 Mar 2018 05:17:14 +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 w2Q5H9hE027628 for ; Mon, 26 Mar 2018 01:17:10 -0400 Received: by smtp.corp.redhat.com (Postfix) id D14B92026E04; Mon, 26 Mar 2018 05:17: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 646BC202322B for ; Mon, 26 Mar 2018 05:17:09 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Mon, 26 Mar 2018 07:16:44 +0200 Message-Id: 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 3/5] virfile: Introduce virFileMajMinToName 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.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 26 Mar 2018 05:17:15 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This function can be used to translate MAJ:MIN device pair into /dev/blah name. Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virfile.c | 53 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virfile.h | 3 +++ 3 files changed, 57 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1bbff72263..a705bfd5ef 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1787,6 +1787,7 @@ virFileLength; virFileLinkPointsTo; virFileLock; virFileLoopDeviceAssociate; +virFileMajMinToName; virFileMakeParentPath; virFileMakePath; virFileMakePathWithMode; diff --git a/src/util/virfile.c b/src/util/virfile.c index f89e4bd823..62620862a1 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -68,6 +68,12 @@ # include #endif =20 +#ifdef MAJOR_IN_MKDEV +# include +#elif MAJOR_IN_SYSMACROS +# include +#endif + #include "configmake.h" #include "intprops.h" #include "viralloc.h" @@ -4317,3 +4323,50 @@ virFileGetMPathTargets(const char *path ATTRIBUTE_UN= USED, return -1; } #endif /* ! WITH_DEVMAPPER */ + + +/** + * virFileMajMinToName: + * @device: device (rdev) to translate + * @name: returned name + * + * For given MAJ:MIN pair (stored in one integer like in st_rdev) + * fetch device name, e.g. 8:0 is translated to "/dev/sda". + * Caller is responsible for freeing @name when no longer needed. + * + * Returns 0 on success, -1 otherwise. + */ +int +virFileMajMinToName(unsigned long long device, + char **name) +{ + struct stat sb; + char *sysfsPath =3D NULL; + char *link =3D NULL; + int ret =3D -1; + + *name =3D NULL; + if (virAsprintf(&sysfsPath, "/sys/dev/block/%u:%u", + major(device), minor(device)) < 0) + goto cleanup; + + if (lstat(sysfsPath, &sb) < 0) + goto cleanup; + + if (!S_ISLNK(sb.st_mode)) { + errno =3D ENXIO; + goto cleanup; + } + + if (virFileReadLink(sysfsPath, &link) < 0) + goto cleanup; + + if (virAsprintf(name, "/dev/%s", last_component(link)) < 0) + goto cleanup; + + ret =3D 0; + cleanup: + VIR_FREE(link); + VIR_FREE(sysfsPath); + return ret; +} diff --git a/src/util/virfile.h b/src/util/virfile.h index 0db8bf0b99..390940dc98 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -362,4 +362,7 @@ int virFileGetMPathTargets(const char *path, unsigned long long **devs, size_t *ndevs); =20 +int virFileMajMinToName(unsigned long long device, + char **name); + #endif /* __VIR_FILE_H */ --=20 2.16.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list