Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-who committed Apr 26, 2024
1 parent 88b62c0 commit 068b5d2
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 7 deletions.
4 changes: 3 additions & 1 deletion spit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ target_link_libraries(clustername spitlib m aio pthread numa pci uuid)
add_executable(dataset datasetMain.c dataset.c dataset.h numList.c numList.h)
target_link_libraries(dataset m)

add_executable(nfsexports nfsexportsMain.c nfsexports.c nfsexports.h)
add_executable(nfsexports nfsexportsMain.c nfsexports.c nfsexports.h)

add_executable(blockdevice blockdevice.c blockdevice.h blockdeviceMain.c)

add_executable(modcat modcat.c)
#target_link_libraries()
Expand Down
204 changes: 204 additions & 0 deletions spit/blockdevice.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
#define _POSIX_C_SOURCE 200809L

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/sysmacros.h>
#include <fcntl.h>

#include "blockdevice.h"


#define PATH_MAX 1024

char *getFieldFromUdev(unsigned int major, unsigned int minor, char *match) {
char s[PATH_MAX];
sprintf(s, "/run/udev/data/b%u:%u", major, minor);
FILE *fp = fopen(s, "rt");
if (!fp) {
// perror(s);
// exit(1);
return NULL;
}

char *line = calloc(PATH_MAX, 1);
char *result = calloc(PATH_MAX, 1);
while (fgets(line, PATH_MAX - 1, fp) != NULL) {
if (strstr(line, match)) {
sscanf(strstr(line, "=") + 1, "%s", result);
// fprintf(stderr,"matched: %s\n", result);
}
}
fclose(fp);
free(line);

return result;
}


/* creates a new string */
char *getSuffix(const char *path) {
if (!path) return NULL;
int found = -1;
for (size_t i = strlen(path) - 1; i > 0; i--) {
if (path[i] == '/') {
found = i + 1;
break;
}
}
if (found > 0) {
return strdup(path + found);
} else {
return NULL;
}
}


long getLongValueFromFile(const char *filename) {
FILE *fp = fopen (filename, "rt");
long value = 0; // if can't open file etc
if (fp) {
int ret = fscanf(fp, "%ld", &value);
if (ret < 1)
return 0;

fclose(fp);
} else {
// perror(filename);
}
return value;
}
char * getStringFromFile(const char *filename) {
int fd = open(filename, O_RDONLY);
char str[1024];
memset(str, 0, 1024);
if (fd >= 0) {
int ret = read(fd, str, 1023);
// int ret = fscanf(fp, "%s", str);
if (ret < 1)
return NULL;
str[ret] = 0; // add end 0
str[strlen(str)-1] = 0; // remove last \n
close(fd);
} else {
// perror(filename);
}
return strdup(str);
}

int majorAndMinor(int fd, unsigned int *major, unsigned int *minor) {
struct stat buf;
if (fstat(fd, &buf) == 0) {
dev_t dt = buf.st_rdev;
*major = major(dt);
*minor = minor(dt);
return 0;
}
return 1;
}


bdType *bdInit(const char *path) {
bdType *b = calloc(1, sizeof(bdType));
b->path = strdup(path);

int fd = open(path, O_RDONLY);
if (fd >= 0) {
majorAndMinor(fd, &b->major, &b->minor);
if (b->major == 253) {
b->mapper = 1;
}

char *suffix = getSuffix(path);
char s[1024];
sprintf(s, "/sys/class/block/%s", suffix);
int fd2 = open(s, O_RDONLY|O_DIRECTORY);
if (fd2 < 0) {
fprintf(stderr,"*warning* '%s' is not a block device\n", path);
return NULL;
} else {
close(fd2);
}


sprintf(s, "/sys/class/block/%s/partition", suffix);
fd2 = open(s, O_RDONLY);
if (fd2 >= 0) {
b->partition = 1;
close(fd2);
fprintf(stderr,"*warning* '%s' is not a block device, it's a partition\n", path);
return NULL;
}

b->serial = getFieldFromUdev(b->major, b->minor, "E:ID_SERIAL_SHORT=");
b->vendor = getFieldFromUdev(b->major, b->minor, "E:ID_VENDOR=");
b->model = getFieldFromUdev(b->major, b->minor, "E:ID_MODEL=");

b->usbdriver = getFieldFromUdev(b->major, b->minor, "E:ID_USB_DRIVER=");

sprintf(s, "/sys/class/block/%s/rotational", suffix);
b->rotational = getLongValueFromFile(s);

sprintf(s, "/sys/class/block/%s/removable", suffix);
b->removable = getLongValueFromFile(s);
if (b->usbdriver) {
if ((strcmp(b->usbdriver, "uas")==0) || (strcmp(b->usbdriver,"usb-storage")==0))
b->removable = 1;
}


sprintf(s, "/sys/class/block/%s/queue/write_cache", suffix);
b->writecache = getStringFromFile(s);

sprintf(s, "/sys/class/block/%s/size", suffix);
b->size = 512LL * getLongValueFromFile(s);

if (b->removable) {
b->type = strdup("Removeable");
} else {
if (b->major == 1) {
b->type = strdup("Volatile-RAM");
} else if (b->major == 8) {
if (b->rotational) {
b->type = strdup("HDD");
} else {
b->type = strdup("SSD");
}
} else if (b->major == 259) {
b->type = strdup("NVMe");
} else if (b->major >= 243) {
b->type = strdup("Virtual");
} else {
b->type = strdup("Unknown");
}
}


} else {
fprintf(stderr,"*warning* '%s' is not a block device\n", path);
return NULL;
}

return b;
}

void bdDumpKV(FILE *fp, bdType *bd) {
fprintf(fp, "path: %s\n", bd->path);
fprintf(fp, "type: %s\n", bd->type);
fprintf(fp, " partition: %d\n", bd->partition);
fprintf(fp, "maj:min: %u:%u\n", bd->major, bd->minor);
fprintf(fp, "device mapper: %d\n", bd->mapper);
fprintf(fp, "USB driver: %s\n", bd->usbdriver);
fprintf(fp, "sizeBytes: %lld\n", bd->size);
fprintf(fp, "sizeGiB: %.1lf\n", bd->size/1024.0/1024);
fprintf(fp, "serial: %s\n", bd->serial);
fprintf(fp, "vendor: %s\n", bd->vendor);
fprintf(fp, "model: %s\n", bd->model);
fprintf(fp, "removable: %d\n", bd->removable);
fprintf(fp, "volatile: %d\n", (bd->major == 1));
fprintf(fp, "write cache: %s\n", bd->writecache);
}

31 changes: 31 additions & 0 deletions spit/blockdevice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef _BLOCKDEVICE_H
#define _BLOCKDEVICE_H

#include <stdio.h>

typedef struct {
char *path;
unsigned int major;
unsigned int minor;
long long size;
int mapper;
int bs;
int rotational;
int removable;
int zoned;
int volatil;
int partition;
char *vendor;
char *model;
char *serial;
char *usbdriver;
char *writecache;

char *type;// overall
} bdType;


bdType *bdInit(const char *path);
void bdDumpKV(FILE *fp, bdType *bd);

#endif
5 changes: 2 additions & 3 deletions spit/blockdevices.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <linux/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>

#include "diskStats.h"
Expand Down Expand Up @@ -58,9 +60,6 @@ void blockDevicesScan(blockDevicesType *bd) {
int fd = open(path, O_RDONLY);
if (fd >= 0) {
fprintf(stderr,"%s\n", path);
if (strcmp(path, "/dev/sdd")==0) {
double d = pow(1,2);
}
keyvalueType *k = keyvalueInit();

char *suf = getSuffix(path);
Expand Down
66 changes: 63 additions & 3 deletions spit/nfsexports.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "nfsexports.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
Expand All @@ -7,10 +6,57 @@

#include <sys/statvfs.h>

#include "nfsexports.h"
#include <regex.h>

#define PATH_MAX 1024



// returns the number of lines
char *dumpFile(const char *fn, const char *regexstring) {

regex_t regex;
int reti;
reti = regcomp(&regex, regexstring, REG_EXTENDED | REG_ICASE);
if (reti) {
fprintf(stderr, "*error* could not compile regex\n");
return 0;
}

int linecount = 0;
FILE *stream = fopen(fn, "rt");
char *ret = NULL;

if (stream) {
char *line = NULL;
size_t len = 0;
ssize_t nread;

while ((nread = getline(&line, &len, stream)) != -1) {
// printf("Retrieved line of length %zu:\n", nread);

reti = regexec(&regex, line, 0, NULL, 0);
if (!reti) {
linecount++;

ret = line;
break;
}
}

if (ret == NULL) free(line);
fclose(stream);
} else {
perror(fn);
}

regfree(&regex);
return ret;
}



nfsRangeExports *nfsExportsInit(void) {
FILE *fp;
int status;
Expand Down Expand Up @@ -123,14 +169,28 @@ void nfsCheckMissing(nfsRangeExports *n) {



if (n->exports[i].start > 1) {
fprintf(stderr,"*warning* missing low drives. setting start to 1\n");
if (n->exports[i].prefix && (n->exports[i].start > 1)) {
fprintf(stderr,"*warning* missing low drives (maybe missing hypen). setting start to 1\n");
n->exports[i].start = 1;
}

// check /proc/mounts
char *ss = dumpFile("/proc/mounts", n->exports[i].prefix);
if (ss) {
char *first = strtok(ss, " \t");

fprintf(stderr,"---> %s\n", first);
}

// if there, get type from underlying block device
//
// otherwise get the device under / and return that.
// [root@nuc7b spit]# cat /proc/mounts | grep -w /
// /dev/mapper/almalinux_nuc7b-root / xfs rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota 0 0
// [root@nuc7b spit]# cat /sys/class/block/dm-*/dm/name | grep root
// almalinux_nuc7b-root


}
}

Expand Down

0 comments on commit 068b5d2

Please sign in to comment.