(root)/
util-linux-2.39/
libblkid/
samples/
topology.c
       1  /*
       2   * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
       3   *
       4   * This file may be redistributed under the terms of the
       5   * GNU Lesser General Public License.
       6   */
       7  
       8  #include <stdio.h>
       9  #include <stdlib.h>
      10  #include <sys/types.h>
      11  #include <sys/stat.h>
      12  #include <fcntl.h>
      13  #include <errno.h>
      14  #include <inttypes.h>
      15  
      16  #include <blkid.h>
      17  
      18  #include "c.h"
      19  
      20  int main(int argc, char *argv[])
      21  {
      22  	int rc;
      23  	char *devname;
      24  	blkid_probe pr;
      25  	blkid_topology tp;
      26  
      27  	if (argc < 2) {
      28  		fprintf(stderr, "usage: %s <device>  "
      29  				"-- prints topology details about the device\n",
      30  				program_invocation_short_name);
      31  		return EXIT_FAILURE;
      32  	}
      33  
      34  	devname = argv[1];
      35  	pr = blkid_new_probe_from_filename(devname);
      36  	if (!pr)
      37  		err(EXIT_FAILURE, "%s: failed to create a new libblkid probe",
      38  				devname);
      39  	/*
      40  	 * Binary interface
      41  	 */
      42  	tp = blkid_probe_get_topology(pr);
      43  	if (tp) {
      44  		printf("----- binary interface:\n");
      45  		printf("\talignment offset     : %lu\n",
      46  				blkid_topology_get_alignment_offset(tp));
      47  		printf("\tminimum io size      : %lu\n",
      48  				blkid_topology_get_minimum_io_size(tp));
      49  		printf("\toptimal io size      : %lu\n",
      50  				blkid_topology_get_optimal_io_size(tp));
      51  		printf("\tlogical sector size  : %lu\n",
      52  				blkid_topology_get_logical_sector_size(tp));
      53  		printf("\tphysical sector size : %lu\n",
      54  				blkid_topology_get_physical_sector_size(tp));
      55  		printf("\tdax support          : %lu\n",
      56  				blkid_topology_get_dax(tp));
      57  		printf("\tdiskseq              : %"PRId64"\n",
      58  				blkid_topology_get_diskseq(tp));
      59  	}
      60  
      61  	/*
      62  	 * NAME=value interface
      63  	 */
      64  
      65  	/* enable topology probing */
      66  	blkid_probe_enable_topology(pr, TRUE);
      67  
      68  	/* disable superblocks probing (enabled by default) */
      69  	blkid_probe_enable_superblocks(pr, FALSE);
      70  
      71  	rc = blkid_do_fullprobe(pr);
      72  	if (rc == -1)
      73  		errx(EXIT_FAILURE, "%s: blkid_do_fullprobe() failed", devname);
      74  	else if (rc == 1)
      75  		warnx("%s: missing topology information", devname);
      76  	else {
      77  		int i, nvals = blkid_probe_numof_values(pr);
      78  
      79  		printf("----- NAME=value interface (values: %d):\n", nvals);
      80  
      81  		for (i = 0; i < nvals; i++) {
      82  			const char *name, *data;
      83  
      84  			blkid_probe_get_value(pr, i, &name, &data, NULL);
      85  			printf("\t%s = %s\n", name, data);
      86  		}
      87  	}
      88  
      89  	blkid_free_probe(pr);
      90  	return EXIT_SUCCESS;
      91  }