使用 libblkid 查找分区的 UUID

发布于 2024-11-25 04:55:19 字数 160 浏览 1 评论 0原文

我正在查看 libblkid 并对文档。有人能给我提供一个示例,说明如何使用这个库找到 Linux 根分区的 UUID 吗?

I was looking at libblkid and was confused about the documentation. Could someone provide me with an example of how I could find the UUID of a root linux partition using this library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自←快乐 2024-12-02 04:55:19

它与手册看起来一样简单:创建一个探针结构,初始化它,询问它一些信息,然后释放它。您可以将前两个步骤合并为一个。这是一个工作程序:

#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <blkid/blkid.h>

int main (int argc, char *argv[]) {
  blkid_probe pr;
  const char *uuid;

  if (argc != 2) {
    fprintf(stderr, "Usage: %s devname\n", argv[0]);
    exit(1);
  }

  pr = blkid_new_probe_from_filename(argv[1]);
  if (!pr) {
    err(2, "Failed to open %s", argv[1]);
  }

  blkid_do_probe(pr);
  blkid_probe_lookup_value(pr, "UUID", &uuid, NULL);

  printf("UUID=%s\n", uuid);

  blkid_free_probe(pr);

  return 0;
}

blkid_probe_lookup_valueuuid 设置为指向属于 pr 结构的字符串,这就是参数为类型的原因const char *。如果需要,您可以将其复制到您自己管理的 char * 中,但如果只是传递给 printf,则不需要这样做。 blkid_probe_lookup_value 的第四个参数可让您获取返回值的长度,以防您也需要。 blkid_do_probeblkid_do_safeprobeblkid_do_fullprobe 之间存在一些细微差别,但在设备具有已知文件系统并且您只想拉取的情况下从中取出 UUID,从 blkid_do_probe 中获取第一个结果就可以了。

It's pretty much as simple as the manual makes it look: you create a probe structure, initialize it, ask it for some information, and then free it. And you can combine the first two steps into one. This is a working program:

#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <blkid/blkid.h>

int main (int argc, char *argv[]) {
  blkid_probe pr;
  const char *uuid;

  if (argc != 2) {
    fprintf(stderr, "Usage: %s devname\n", argv[0]);
    exit(1);
  }

  pr = blkid_new_probe_from_filename(argv[1]);
  if (!pr) {
    err(2, "Failed to open %s", argv[1]);
  }

  blkid_do_probe(pr);
  blkid_probe_lookup_value(pr, "UUID", &uuid, NULL);

  printf("UUID=%s\n", uuid);

  blkid_free_probe(pr);

  return 0;
}

blkid_probe_lookup_value sets uuid to point to a string that belongs to the pr structure, which is why the argument is of type const char *. If you needed to, you could copy it to a char * that you manage on your own, but for just passing to printf, that's not needed. The fourth argument to blkid_probe_lookup_value lets you get the length of the returned value in case you need that as well. There are some subtle differences between blkid_do_probe, blkid_do_safeprobe, and blkid_do_fullprobe, but in cases where the device has a known filesystem and you just want to pull the UUID out of it, taking the first result from blkid_do_probe should do.

雨轻弹 2024-12-02 04:55:19

首先,您需要找到以 root 身份安装的设备。参见 man getmntent (3)。一旦您知道了设备,请按照 hobbs 的描述使用 blkid_new_probe_from_filename 。

#include <stdio.h>
#include <mntent.h>

int main() {
    FILE* fstab = setmntent("/etc/mtab", "r");
    struct mntent *e;
    const char *devname = NULL;
    while ((e = getmntent(fstab))) {
        if (strcmp("/", e->mnt_dir) == 0) {
            devname = e->mnt_fsname;
            break;
        }
    }
    printf("root devname is %s\n", devname);
    endmntent(fstab);
    return 0;
}

First you need to find the device mounted as as root. See man getmntent (3). Once you know the device, use blkid_new_probe_from_filename as described by hobbs.

#include <stdio.h>
#include <mntent.h>

int main() {
    FILE* fstab = setmntent("/etc/mtab", "r");
    struct mntent *e;
    const char *devname = NULL;
    while ((e = getmntent(fstab))) {
        if (strcmp("/", e->mnt_dir) == 0) {
            devname = e->mnt_fsname;
            break;
        }
    }
    printf("root devname is %s\n", devname);
    endmntent(fstab);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文