1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/sysmacros.h>
#include <sys/mount.h>
void create_block_device(const char *name, int major, int minor) {
char path[256];
snprintf(path, sizeof(path), "/dev/%s", name);
if (mknod(path, S_IFBLK | 0660, makedev(major, minor)) < 0) {
perror("mknod");
} else {
printf("Created %s (major %d, minor %d)\n", path, major, minor);
}
}
void scan_and_create_devices() {
DIR *dir = opendir("/sys/block/");
if (!dir) {
// perror("opendir /sys/block");
return;
}
struct dirent *entry;
while ((entry = readdir(dir))) {
if (entry->d_name[0] == '.') continue; // Skip "." and ".."
char sys_path[256];
snprintf(sys_path, sizeof(sys_path), "/sys/block/%s/dev", entry->d_name);
FILE *f = fopen(sys_path, "r");
if (!f) continue;
int major, minor;
if (fscanf(f, "%d:%d", &major, &minor) == 2) {
create_block_device(entry->d_name, major, minor);
}
fclose(f);
}
closedir(dir);
}
int main() {
mount("devtmpfs", "/dev", "devtmpfs", 0, NULL);
mkdir("/sys", 0755);
mount("sysfs", "/sys", "sysfs", 0, NULL);
mkdir("/dev", 0755);
// create_block_device("sda", 8, 0);
// create_block_device("sda1", 8, 1);
// create_block_device("sda2", 8, 2);
// create_block_device("sdb", 8, 16);
// create_block_device("sdb1", 8, 17);
// create_block_device("sdb2", 8, 18);
while (1) {
scan_and_create_devices();
sleep(10); // Check every 10 seconds
}
}
|