Re: Really big mistake -- I need some help

alknaff@innet.lu
Wed, 16 Jul 1997 19:10:16 +0200


> Better yet -- I have a feeling that the kernel itself has
> enough information in internal memory to rebuild the partition
> table -- because *every* partition (except, of course, 4) is
> mounted right now (the mount was done back when I *did* have
> a partition table).

The HDIO_GETGEO ioctl allows to get the start of a partition, and the
BLKGETSIZE gets the size.

They are used in the following program. Usage:
blockinfo /dev/hda2 /dev/hda4 /dev/hda8 ...

Btw, this should also work on partitions which are not currently mounted.

Regards,

Alain

#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <linux/fs.h>
#include <linux/hdreg.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
struct hd_geometry loc;
int fd, size, i;

for(i=1; i < argc; i++) {
fd = open(argv[i], O_RDONLY);

if(fd < 0) {
perror("open");
exit(1);
}

if(ioctl(fd, HDIO_GETGEO, &loc) < 0) {
perror("ioctl start");
exit(1);
}


if(ioctl(fd, BLKGETSIZE, &size) < 0) {
perror("ioctl start");
exit(1);
}

printf("%s: h=%3d s=%3d c=%4d start=%8d size=%8d end=%8d\n",
argv[i],
loc.heads, (int)loc.sectors, loc.cylinders,
(int)loc.start, size, (int) loc.start + size -1);
close(fd);
}
exit(0);

}