diff options
author | WlodekM <[email protected]> | 2025-02-19 10:24:56 +0200 |
---|---|---|
committer | WlodekM <[email protected]> | 2025-02-19 10:24:56 +0200 |
commit | 3cafac0479d25ec70375712576ad2d965d10ebac (patch) | |
tree | d013c36a26406e1884bb2f9362760cb897bb2172 /sbin/devscan/devscan.c | |
parent | 4583635f5d11cc7aa6d71ccba0ceea5644d9e02b (diff) |
devscan and stuff
Diffstat (limited to 'sbin/devscan/devscan.c')
-rw-r--r-- | sbin/devscan/devscan.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/sbin/devscan/devscan.c b/sbin/devscan/devscan.c new file mode 100644 index 0000000..f3f10b4 --- /dev/null +++ b/sbin/devscan/devscan.c @@ -0,0 +1,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 + } +} |