Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 28 Aug 1996 22:08:20 +0200 (SAT)
From:      Robert Nordier <rnordier@iafrica.com>
To:        hackers@freebsd.org
Subject:   Determining disk type & size
Message-ID:  <199608282008.WAA00279@eac.iafrica.com>

next in thread | raw e-mail | index | archive | help
Could anyone tell me if there is a quick and easy way to do the
following:

For a given (block special) device, determine whether it represents

   (1) a floppy disk
   (2) a hard disk slice that contains a filesystem (probably not ufs)
   (3) anything else (not interested, but could be cdrom, tape, etc)

and, given this info, the size (in bytes or 512-byte sectors) of the
disk or slice itself.  For example:

   device       blocks
   ----------   ------
   /dev/fd1       2400
   /dev/wd0s1    65457
   /dev/wd0     (error)

Most of the userland code I've looked at doesn't seem to handle this
very elegantly, where it checks.  And some of my attempts to do the
obvious seem to produce inconsistent results (below).

Thanks.

--
Robert Nordier

----- fdtest output -----

./fdtest /dev/fd0
sectors per cylinder: 158 [<-- 30 expected]
tracks: 80   size: 2400
./fdtest /dev/fd1
sectors per cylinder: 36
tracks: 80   size: 2880

----- fdtest.c -----

#include <stdio.h>
#include <fcntl.h>
#include <err.h>
#include <sys/disklabel.h>
#include <machine/ioctl_fd.h>

int main(int argc, char *argv[])
{
   struct disklabel dl;
   struct fd_type fdt;
   int fd;

   if (argc != 2) {
      fprintf(stderr, "usage: fdtest device\n");
      return 2;
   }
   if ((fd = open(argv[1], O_RDONLY, 0)) < 0)
      err(1, argv[1]);
   if (ioctl(fd, DIOCGDINFO, &dl) == -1)
      err(1, argv[1]);
   printf("sectors per cylinder: %lu\n", dl.d_secpercyl);
   if (ioctl(fd, FD_GTYPE, &fdt) < 0)
      printf("Not a floppy\n");
   else
      printf("tracks: %d   size: %d\n", fdt.tracks, fdt.size);
   return 0;
}




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199608282008.WAA00279>