I've seen some questions on reading the superblock of an ext2 partition, but I have some questions that weren't answered in these.
Here they go:
1. using read() to read from disk into a ext2_super_block struct should require that all the fields in the struct are compiled in the order they are presented in the code, as well as the necessity of there being no struct padding (or the correct struct padding). How is that guaranteed?
2. How does Linux behave when trying to read from a device when unprivileged? There must be an initial offset for the reads (a mapping, to be more precise, forbidding access to the first N bytes), because the program I wrote only works when running as root. Anyways, how does Linux behave in that situation?
3. Where can I find good documentation on working with ext2/ext3? So far I've been reading /usr/include/linux/ext2_fs.h and some random documents on the internet, but have found nothing complete yet.
I would also like to hear suggestions/corrections on the code below, which so far works fine on my machine (includes omitted for brevity, the program prints "ef53"):
int main() {
int fd;
char boot[1024];
struct ext2_super_block super_block;
fd = open("/dev/sda1", O_RDONLY);
/* Reads the boot section and the superblock */
read(fd, boot, 1024);
read(fd, &super_block, sizeof(struct ext2_super_block));
/* Prints the Magic Number */
printf("%x\n", super_block.s_magic);
close(fd);
return 0;
}
Thanks in advance.