summaryrefslogtreecommitdiffstats
path: root/libblkid/samples
diff options
context:
space:
mode:
Diffstat (limited to 'libblkid/samples')
-rw-r--r--libblkid/samples/Makemodule.am22
-rw-r--r--libblkid/samples/mkfs.c78
-rw-r--r--libblkid/samples/partitions.c96
-rw-r--r--libblkid/samples/superblocks.c67
-rw-r--r--libblkid/samples/topology.c86
5 files changed, 349 insertions, 0 deletions
diff --git a/libblkid/samples/Makemodule.am b/libblkid/samples/Makemodule.am
new file mode 100644
index 0000000..dd05fc9
--- /dev/null
+++ b/libblkid/samples/Makemodule.am
@@ -0,0 +1,22 @@
+
+check_PROGRAMS += \
+ sample-mkfs \
+ sample-partitions \
+ sample-superblocks \
+ sample-topology
+
+sample_mkfs_SOURCES = libblkid/samples/mkfs.c
+sample_mkfs_LDADD = libblkid.la $(LDADD)
+sample_mkfs_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir)
+
+sample_partitions_SOURCES = libblkid/samples/partitions.c
+sample_partitions_LDADD = libblkid.la $(LDADD)
+sample_partitions_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir)
+
+sample_superblocks_SOURCES = libblkid/samples/superblocks.c
+sample_superblocks_LDADD = libblkid.la $(LDADD)
+sample_superblocks_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir)
+
+sample_topology_SOURCES = libblkid/samples/topology.c
+sample_topology_LDADD = libblkid.la $(LDADD)
+sample_topology_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir)
diff --git a/libblkid/samples/mkfs.c b/libblkid/samples/mkfs.c
new file mode 100644
index 0000000..5423f91
--- /dev/null
+++ b/libblkid/samples/mkfs.c
@@ -0,0 +1,78 @@
+/*
+ * 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 <errno.h>
+
+#include <blkid.h>
+
+#include "c.h"
+
+int main(int argc, char *argv[])
+{
+ int rc;
+ char *devname;
+ blkid_probe pr;
+ blkid_topology tp;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s <device> "
+ "-- checks based on libblkid for mkfs-like programs.\n",
+ program_invocation_short_name);
+ return EXIT_FAILURE;
+ }
+
+ devname = argv[1];
+ pr = blkid_new_probe_from_filename(devname);
+ if (!pr)
+ err(EXIT_FAILURE, "%s: failed to create a new libblkid probe",
+ devname);
+
+ /*
+ * check Filesystems / Partitions overwrite
+ */
+
+ /* enable partitions probing (superblocks are enabled by default) */
+ blkid_probe_enable_partitions(pr, TRUE);
+
+ rc = blkid_do_fullprobe(pr);
+ if (rc == -1)
+ errx(EXIT_FAILURE, "%s: blkid_do_fullprobe() failed", devname);
+ else if (rc == 0) {
+ const char *type;
+
+ if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL))
+ errx(EXIT_FAILURE, "%s: appears to contain an existing "
+ "%s superblock", devname, type);
+
+ if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL))
+ errx(EXIT_FAILURE, "%s: appears to contain an partition "
+ "table (%s)", devname, type);
+ }
+
+ /*
+ * get topology details
+ */
+ tp = blkid_probe_get_topology(pr);
+ if (!tp)
+ errx(EXIT_FAILURE, "%s: failed to read topology", devname);
+
+
+ /* ... your mkfs.<type> code or so ...
+
+ off = blkid_topology_get_alignment_offset(tp);
+
+ */
+
+ blkid_free_probe(pr);
+
+ return EXIT_SUCCESS;
+}
diff --git a/libblkid/samples/partitions.c b/libblkid/samples/partitions.c
new file mode 100644
index 0000000..c5830e6
--- /dev/null
+++ b/libblkid/samples/partitions.c
@@ -0,0 +1,96 @@
+/*
+ * 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 <errno.h>
+
+#include <blkid.h>
+#include "c.h"
+
+int main(int argc, char *argv[])
+{
+ int i, nparts;
+ char *devname;
+ blkid_probe pr;
+ blkid_partlist ls;
+ blkid_parttable root_tab;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s <device|file> "
+ "-- prints partitions\n",
+ program_invocation_short_name);
+ return EXIT_FAILURE;
+ }
+
+ devname = argv[1];
+ pr = blkid_new_probe_from_filename(devname);
+ if (!pr)
+ err(EXIT_FAILURE, "%s: failed to create a new libblkid probe",
+ devname);
+ /* Binary interface */
+ ls = blkid_probe_get_partitions(pr);
+ if (!ls)
+ errx(EXIT_FAILURE, "%s: failed to read partitions\n", devname);
+
+ /*
+ * Print info about the primary (root) partition table
+ */
+ root_tab = blkid_partlist_get_table(ls);
+ if (!root_tab)
+ errx(EXIT_FAILURE, "%s: does not contains any "
+ "known partition table\n", devname);
+
+ printf("size: %jd, sector size: %u, PT: %s, offset: %jd, id=%s\n---\n",
+ (intmax_t)blkid_probe_get_size(pr),
+ blkid_probe_get_sectorsize(pr),
+ blkid_parttable_get_type(root_tab),
+ (intmax_t)blkid_parttable_get_offset(root_tab),
+ blkid_parttable_get_id(root_tab));
+
+ /*
+ * List partitions
+ */
+ nparts = blkid_partlist_numof_partitions(ls);
+ if (!nparts)
+ goto done;
+
+ 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);
+
+ 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 (BSD, Minix, ...) */
+ 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);
+ p = blkid_partition_get_type_string(par);
+ if (p)
+ printf(" type='%s'", p);
+
+ putc('\n', stdout);
+ }
+
+done:
+ blkid_free_probe(pr);
+ return EXIT_SUCCESS;
+}
diff --git a/libblkid/samples/superblocks.c b/libblkid/samples/superblocks.c
new file mode 100644
index 0000000..7d95557
--- /dev/null
+++ b/libblkid/samples/superblocks.c
@@ -0,0 +1,67 @@
+/*
+ * 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 <errno.h>
+
+#include <blkid.h>
+
+#include "c.h"
+
+int main(int argc, char *argv[])
+{
+ int rc;
+ char *devname;
+ blkid_probe pr;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s <device> "
+ "-- prints superblocks details about the device\n",
+ program_invocation_short_name);
+ return EXIT_FAILURE;
+ }
+
+ devname = argv[1];
+ pr = blkid_new_probe_from_filename(devname);
+ if (!pr)
+ err(EXIT_FAILURE, "%s: failed to create a new libblkid probe",
+ devname);
+
+ /* enable topology probing */
+ blkid_probe_enable_superblocks(pr, TRUE);
+
+ /* set all flags */
+ blkid_probe_set_superblocks_flags(pr,
+ BLKID_SUBLKS_LABEL | BLKID_SUBLKS_LABELRAW |
+ BLKID_SUBLKS_UUID | BLKID_SUBLKS_UUIDRAW |
+ BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
+ BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION |
+ BLKID_SUBLKS_MAGIC);
+
+ rc = blkid_do_safeprobe(pr);
+ if (rc == -1)
+ errx(EXIT_FAILURE, "%s: blkid_do_safeprobe() failed", devname);
+ else if (rc == 1)
+ warnx("%s: cannot gather information about superblocks", devname);
+ else {
+ int i, nvals = blkid_probe_numof_values(pr);
+
+ for (i = 0; i < nvals; i++) {
+ const char *name, *data;
+
+ blkid_probe_get_value(pr, i, &name, &data, NULL);
+ printf("\t%s = %s\n", name, data);
+ }
+ }
+
+ blkid_free_probe(pr);
+ return EXIT_SUCCESS;
+}
diff --git a/libblkid/samples/topology.c b/libblkid/samples/topology.c
new file mode 100644
index 0000000..4112ba3
--- /dev/null
+++ b/libblkid/samples/topology.c
@@ -0,0 +1,86 @@
+/*
+ * 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 <errno.h>
+
+#include <blkid.h>
+
+#include "c.h"
+
+int main(int argc, char *argv[])
+{
+ int rc;
+ char *devname;
+ blkid_probe pr;
+ blkid_topology tp;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s <device> "
+ "-- prints topology details about the device\n",
+ program_invocation_short_name);
+ return EXIT_FAILURE;
+ }
+
+ devname = argv[1];
+ pr = blkid_new_probe_from_filename(devname);
+ if (!pr)
+ err(EXIT_FAILURE, "%s: failed to create a new libblkid probe",
+ devname);
+ /*
+ * Binary interface
+ */
+ tp = blkid_probe_get_topology(pr);
+ if (tp) {
+ printf("----- binary interface:\n");
+ printf("\talignment offset : %lu\n",
+ blkid_topology_get_alignment_offset(tp));
+ printf("\tminimum io size : %lu\n",
+ blkid_topology_get_minimum_io_size(tp));
+ printf("\toptimal io size : %lu\n",
+ blkid_topology_get_optimal_io_size(tp));
+ printf("\tlogical sector size : %lu\n",
+ blkid_topology_get_logical_sector_size(tp));
+ printf("\tphysical sector size : %lu\n",
+ blkid_topology_get_physical_sector_size(tp));
+ }
+
+ /*
+ * NAME=value interface
+ */
+
+ /* enable topology probing */
+ blkid_probe_enable_topology(pr, TRUE);
+
+ /* disable superblocks probing (enabled by default) */
+ blkid_probe_enable_superblocks(pr, FALSE);
+
+ rc = blkid_do_fullprobe(pr);
+ if (rc == -1)
+ errx(EXIT_FAILURE, "%s: blkid_do_fullprobe() failed", devname);
+ else if (rc == 1)
+ warnx("%s: missing topology information", devname);
+ else {
+ int i, nvals = blkid_probe_numof_values(pr);
+
+ printf("----- NAME=value interface (values: %d):\n", nvals);
+
+ for (i = 0; i < nvals; i++) {
+ const char *name, *data;
+
+ blkid_probe_get_value(pr, i, &name, &data, NULL);
+ printf("\t%s = %s\n", name, data);
+ }
+ }
+
+ blkid_free_probe(pr);
+ return EXIT_SUCCESS;
+}