I saw some questions on reading the superblock of the ext2 section, but I have some questions that were not answered in them.
Here they come:
1. Using read () to read from disk into the ext2_super_block structure should require that all fields in the structure be compiled in the order in which they are presented in the code, as well as the need for no structure laying (or proper struct padding ) How is this guaranteed?
2. How does Linux work when trying to read from a device when it is unprivileged? There must be an initial offset for reading (more precisely, a mapping that prohibits access to the first N bytes), because the program I wrote only works when run with root privileges. In any case, how does Linux behave in this situation?
3. Where can I find good documentation on working with ext2 / ext3? So far I have read / usr / include / linux / ext 2_fs.h and some random documents on the Internet, but haven’t found anything yet.
I would also like to hear suggestions / corrections on the code below, which still works fine on my machine (also 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);
read(fd, boot, 1024);
read(fd, &super_block, sizeof(struct ext2_super_block));
printf("%x\n", super_block.s_magic);
close(fd);
return 0;
}
Thanks in advance.
source
share