The C program below will read a disk label giving information about the physical disk characteristics. Although format (and prtvtoc partially) show this information, this program is useful for scripts, particularly for displaying the type of disk.

Simply compile it and run it by typing rdlabel /dev/rdsk/cntndnsn

 /***************************************************************/
 /*                                                             */
 /*  rdlabel  ---  Read the disk label                          */
 /*                                                             */
 /*  Usage:                                                     */
 /*           rdlabel <device>                                  */
 /*                                                             */
 /* Notes: on SPARC arch devices are /dev/rdsk/cXtYdZsS         */
 /*           OTHER arch devices are /dev/rdsk/cXtYdZpN         */
 /*                                                             */
 /* X = controller,                                             */
 /* Y = target,                                                 */
 /* Z = disk #,                                                 */
 /* N = fdisk partition                                         */
 /* S = 0 or 2                                                  */
 /*                                                             */
 /***************************************************************/


 #include <stdio.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <sys/dkio.h>
 #include <sys/dklabel.h>
 #include <fcntl.h>

 main(argc, argv)
 int argc;
 char *argv[];
 {

 #ifdef SPARC_ARC
        int label_loc = (512 * DK_LABEL_LOC);
 #define ZEXAMPLE       "\tExample: rdlabel /dev/rdsk/c0t0d0s0\n"
 #else
 #define ZEXAMPLE       "\tExample: rdlabel /dev/rdsk/c0t0d0p1\n"
        int label_loc = 0;
 #endif
        int disk_fd;
        int i;
        struct dk_label label;
        int partition;
        char buffer[80];

        if (argc < 2)
        {
                fprintf(stderr, "Error: Argument expected!\n");
                usage();
                exit(1);
        }

        sprintf(buffer, "%s", argv[1]);
        disk_fd = open( buffer, O_RDONLY );
        if ( disk_fd < 0 )
        {
                perror("Uable to open device ");
                exit(1);
        }
        else
        {
                lseek(disk_fd,label_loc,SEEK_SET);
                (void) read(disk_fd, (char *)&label, sizeof(label));
                (void) close(disk_fd);
        }

        (void) printf("Read label for device %s returned the following:\n",
buffer);

        (void) printf("\t<%s>\n", label.dkl_asciilabel);

 }

 usage()
 {
        fprintf(stderr, "Usage: rdlabel <device>\n");
        fprintf(stderr, "Warning: You must be root to run rdlabel!\n");
        fprintf(stderr,ZEXAMPLE);
 }

Recent Changes

Contribute to this wiki

Why not help others by sharing your knowledge? Contribute something to this wiki and join out hall of fame!
Contact us for a user name and password