summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak2009-09-16 16:24:14 +0200
committerKarel Zak2009-09-16 16:24:14 +0200
commitc7fcbf5a891dc74206e945bf0011192e0956469f (patch)
tree103ace7229862109369a01cffb1005a162d0cb2f
parentlibblkid: add EFI GPT partitions support (diff)
downloadkernel-qcow2-util-linux-c7fcbf5a891dc74206e945bf0011192e0956469f.tar.gz
kernel-qcow2-util-linux-c7fcbf5a891dc74206e945bf0011192e0956469f.tar.xz
kernel-qcow2-util-linux-c7fcbf5a891dc74206e945bf0011192e0956469f.zip
libblkid: add partitions sample
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--shlibs/blkid/samples/.gitignore1
-rw-r--r--shlibs/blkid/samples/Makefile.am2
-rw-r--r--shlibs/blkid/samples/partitions.c93
3 files changed, 95 insertions, 1 deletions
diff --git a/shlibs/blkid/samples/.gitignore b/shlibs/blkid/samples/.gitignore
index e27df1c91..fd2a433cb 100644
--- a/shlibs/blkid/samples/.gitignore
+++ b/shlibs/blkid/samples/.gitignore
@@ -1 +1,2 @@
topology
+partitions
diff --git a/shlibs/blkid/samples/Makefile.am b/shlibs/blkid/samples/Makefile.am
index 3b7de7da6..126f72f2d 100644
--- a/shlibs/blkid/samples/Makefile.am
+++ b/shlibs/blkid/samples/Makefile.am
@@ -3,5 +3,5 @@ include $(top_srcdir)/config/include-Makefile.am
AM_CPPFLAGS += -I$(ul_libblkid_srcdir)
AM_LDFLAGS += $(ul_libblkid_la)
-noinst_PROGRAMS = topology
+noinst_PROGRAMS = topology partitions
diff --git a/shlibs/blkid/samples/partitions.c b/shlibs/blkid/samples/partitions.c
new file mode 100644
index 000000000..78bcc0be5
--- /dev/null
+++ b/shlibs/blkid/samples/partitions.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <err.h>
+#include <errno.h>
+
+#include <blkid.h>
+
+int main(int argc, char *argv[])
+{
+ int fd, i, nparts;
+ char *devname;
+ blkid_probe pr;
+ blkid_partlist ls;
+ blkid_parttable root_tab = NULL;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s <device|file> "
+ "-- prints partitions\n",
+ program_invocation_short_name);
+ return EXIT_FAILURE;
+ }
+
+ devname = argv[1];
+
+ fd = open(devname, O_RDONLY);
+ if (fd < 0)
+ err(EXIT_FAILURE, "%s: open() failed", devname);
+
+ pr = blkid_new_probe();
+ if (!pr)
+ err(EXIT_FAILURE, "failed to allocate a new libblkid probe");
+
+ if (blkid_probe_set_device(pr, fd, 0, 0) != 0)
+ err(EXIT_FAILURE, "failed to assign device to libblkid probe");
+
+ /* Binary interface */
+ ls = blkid_probe_get_partitions(pr);
+ if (!ls)
+ errx(EXIT_FAILURE, "%s: failed to read partitions\n", devname);
+
+ nparts = blkid_partlist_numof_partitions(ls);
+ if (!nparts)
+ errx(EXIT_FAILURE, "%s: does not contains any "
+ "known partition table\n", devname);
+
+ for (i = 0; i < nparts; i++) {
+ const char *p;
+ blkid_partition par = blkid_partlist_get_partition(ls, i);
+ blkid_parttable tab = blkid_partition_get_table(par);
+
+ if (i == 0) {
+ root_tab = tab;
+ printf("size: %llu, sector size: %u, "
+ "PT: %s, offset: %llu\n---\n",
+ (unsigned long long) blkid_probe_get_size(pr),
+ blkid_probe_get_sectorsize(pr),
+ blkid_parttable_get_type(tab),
+ (unsigned long long) blkid_parttable_get_offset(tab));
+ }
+ printf("#%d: %10llu %10llu 0x%x",
+ blkid_partition_get_partno(par),
+ (unsigned long long) blkid_partition_get_start(par),
+ (unsigned long long) blkid_partition_get_size(par),
+ blkid_partition_get_type(par));
+
+ if (root_tab != tab)
+ /* subpartition */
+ printf(" (%s)", blkid_parttable_get_type(tab));
+
+ p = blkid_partition_get_name(par);
+ if (p)
+ printf(" name='%s'", p);
+ p = blkid_partition_get_uuid(par);
+ if (p)
+ printf(" uuid='%s'", p);
+
+ putc('\n', stdout);
+ }
+
+ blkid_free_probe(pr);
+
+ return EXIT_SUCCESS;
+}