Created
January 18, 2013 07:56
-
-
Save amitsaha/4563032 to your computer and use it in GitHub Desktop.
Get sector size, etc using ioctl()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Relevant: | |
http://lists.gnu.org/archive/html/bug-parted/2012-10/msg00018.html | |
http://stackoverflow.com/questions/8416241/block-device-information-without-mounting-in-linux | |
*/ | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <linux/fs.h> | |
#include <stdio.h> | |
#ifndef BLKGETSIZE | |
#define BLKGETSIZE _IO(0x12,96) /* return device size */ | |
#endif | |
#ifndef BLKPBSZGET | |
#define BLKSSZGET _IO(0x12,104)/* get block device sector size */ | |
#endif | |
int main(int argc, char **argv) | |
{ | |
int size; | |
int logicalsectsize = 0; | |
int retval; | |
int fd = open("/dev/sdb", O_RDONLY | O_NONBLOCK); | |
if (ioctl(fd, BLKGETSIZE, &size) != 0) | |
size=0; | |
/*retval=ioctl(fd, BLKSSZGET, &logicalsectsize);*/ | |
if (ioctl(fd, BLKSSZGET, &logicalsectsize) < 0) | |
{ | |
printf("Can't get logical sector size. Return code: %d\n",retval); | |
logicalsectsize = 0; | |
} | |
printf("%d %d\n",size, logicalsectsize); | |
return 0; | |
} |
If you are doing this in a loop, you will reach the maximum open file limit, and your system can no longer open files.
Make sure to close(fd)
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
logicalsectsize
must belong
.