#include <sys/param.h>
#include <sys/diskslice.h>
#include <sys/sysctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "libaura/dict.h"
#include "commands.h"
#include "diskutil.h"
#include "functions.h"
static int fgets_chomp(char *, int, FILE *);
static int parse_geometry_info(char *, int *, int *, int *);
static int parse_slice_info(char *, int *,
unsigned long *, unsigned long *, int *, int *);
static int
fgets_chomp(char *line, int size, FILE *f)
{
if (fgets(line, size, f) == NULL)
return(0);
while (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = '\0';
return(1);
}
static int
parse_geometry_info(char *line, int *cyl, int *head, int *sec)
{
char *word;
if ((word = strtok(line, " \t")) == NULL)
return(0);
if ((word = strtok(NULL, " \t")) == NULL)
return(0);
*cyl = atoi(word);
if ((word = strtok(NULL, " \t")) == NULL)
return(0);
if ((word = strtok(NULL, " \t")) == NULL)
return(0);
*head = atoi(word);
if ((word = strtok(NULL, " \t")) == NULL)
return(0);
if ((word = strtok(NULL, " \t")) == NULL)
return(0);
*sec = atoi(word);
return(1);
}
static int
parse_slice_info(char *line, int *slice,
unsigned long *start, unsigned long *size,
int *type, int *flags)
{
char *word;
if ((word = strtok(line, " \t")) == NULL)
return(0);
*slice = atoi(word);
if ((word = strtok(NULL, " \t")) == NULL)
return(0);
*start = strtoul(word, NULL, 10);
if ((word = strtok(NULL, " \t")) == NULL)
return(0);
*size = strtoul(word, NULL, 10);
if ((word = strtok(NULL, " \t")) == NULL)
return(0);
if (!hex_to_int(word, type))
return(0);
if ((word = strtok(NULL, " \t")) == NULL)
return(0);
if (!hex_to_int(word, flags))
return(0);
return(1);
}
int
survey_storage(struct i_fn_args *a)
{
unsigned long mem = 0;
char disks[256], line[256];
char *disk, *disk_ptr;
struct commands *cmds;
struct command *cmd;
FILE *f;
char *filename;
struct disk *d = NULL;
int number = 0;
int failure = 0;
int ndisks = 0;
int fd;
size_t len;
struct aura_dict *di;
void *rk;
size_t rk_len;
struct partinfo diskpart;
char diskpath[PATH_MAX];
sleep(1);
disks_free(a->s);
len = sizeof(mem);
if (sysctlbyname("hw.physmem", &mem, &len, NULL, 0) < 0) {
failure |= 1;
} else {
storage_set_memsize(a->s, next_power_of_two(mem >> 20));
}
len = 256;
if (sysctlbyname("kern.disks", disks, &len, NULL, 0) < 0) {
failure |= 1;
}
disk_ptr = disks;
di = aura_dict_new(1, AURA_DICT_SORTED_LIST);
while (!failure && (disk = strsep(&disk_ptr, " ")) != NULL) {
if (disk[0] == '\0')
continue;
if (strncmp(disk, "md", 2) == 0 ||
strncmp(disk, "cd", 2) == 0 ||
strncmp(disk, "acd", 3) == 0 ||
strncmp(disk, "fd", 2) == 0)
continue;
aura_dict_store(di, disk, strlen(disk) + 1, "", 1);
ndisks++;
}
if (ndisks == 0)
failure |= 1;
cmds = commands_new();
cmd = command_add(cmds, "%s%s -n '' >%ssurvey.txt",
a->os_root, cmd_name(a, "ECHO"), a->tmp);
command_set_log_mode(cmd, COMMAND_LOG_SILENT);
for (aura_dict_rewind(di); !aura_dict_eof(di); aura_dict_next(di) ) {
aura_dict_get_current_key(di, &rk, &rk_len);
disk = (char *)rk;
bzero(&diskpart, sizeof(diskpart));
snprintf(diskpath, PATH_MAX, "/dev/%s", disk);
if ((fd = open(diskpath, O_RDONLY)) < 0)
continue;
if (ioctl(fd, DIOCGPART, &diskpart) < 0)
continue;
cmd = command_add(cmds, "%s%s '@DISK' >>%ssurvey.txt",
a->os_root, cmd_name(a, "ECHO"), a->tmp);
command_set_log_mode(cmd, COMMAND_LOG_SILENT);
cmd = command_add(cmds, "%s%s '%s' >>%ssurvey.txt",
a->os_root, cmd_name(a, "ECHO"), disk, a->tmp);
command_set_log_mode(cmd, COMMAND_LOG_SILENT);
cmd = command_add(cmds, "%s%s '@DESC' >>%ssurvey.txt",
a->os_root, cmd_name(a, "ECHO"), a->tmp);
command_set_log_mode(cmd, COMMAND_LOG_SILENT);
cmd = command_add(cmds, "%s%s '%s: %luMB' >>%ssurvey.txt",
a->os_root, cmd_name(a, "ECHO"),
disk,
diskpart.media_size / 1024 / 1024,
a->tmp);
close(fd);
cmd = command_add(cmds, "%s%s '@END' >>%ssurvey.txt",
a->os_root, cmd_name(a, "ECHO"), a->tmp);
command_set_log_mode(cmd, COMMAND_LOG_SILENT);
cmd = command_add(cmds, "%s%s '@SERNO' >>%ssurvey.txt",
a->os_root, cmd_name(a, "ECHO"), a->tmp);
command_set_log_mode(cmd, COMMAND_LOG_SILENT);
cmd = command_add(cmds, "if %s%s -d /dev/serno; then %s%s -l /dev/serno | %s%s \"`%s%s -l /dev/%s | %s%s '{print $5, $6;}'`\" | %s%s '{print $10;}' >>%ssurvey.txt; fi",
a->os_root, cmd_name(a, "TEST"),
a->os_root, cmd_name(a, "LS"),
a->os_root, cmd_name(a, "GREP"),
a->os_root, cmd_name(a, "LS"),
disk,
a->os_root, cmd_name(a, "AWK"),
a->os_root, cmd_name(a, "AWK"),
a->tmp);
cmd = command_add(cmds, "%s%s '@END' >>%ssurvey.txt",
a->os_root, cmd_name(a, "ECHO"), a->tmp);
command_set_log_mode(cmd, COMMAND_LOG_SILENT);
cmd = command_add(cmds, "%s%s '@SLICES' >>%ssurvey.txt",
a->os_root, cmd_name(a, "ECHO"), a->tmp);
command_set_log_mode(cmd, COMMAND_LOG_SILENT);
cmd = command_add(cmds, "%s%s -s %s 2>/dev/null >>%ssurvey.txt || %s%s '' >>%ssurvey.txt",
a->os_root, cmd_name(a, "FDISK"),
disk,
a->tmp,
a->os_root, cmd_name(a, "ECHO"),
a->tmp);
cmd = command_add(cmds, "%s%s '@END' >>%ssurvey.txt",
a->os_root, cmd_name(a, "ECHO"), a->tmp);
command_set_log_mode(cmd, COMMAND_LOG_SILENT);
}
cmd = command_add(cmds, "%s%s '.' >>%ssurvey.txt",
a->os_root, cmd_name(a, "ECHO"), a->tmp);
command_set_log_mode(cmd, COMMAND_LOG_SILENT);
if (!commands_execute(a, cmds))
failure |= 1;
commands_free(cmds);
temp_file_add(a, "survey.txt");
aura_dict_free(di);
asprintf(&filename, "%ssurvey.txt", a->tmp);
if ((f = fopen(filename, "r")) == NULL)
failure |= 1;
free(filename);
while (!failure && fgets_chomp(line, 255, f)) {
if (strcmp(line, "@DISK") == 0) {
if (fgets_chomp(line, 255, f)) {
d = disk_new(a->s, line);
disk_set_number(d, number++);
}
} else if (strcmp(line, "@DESC") == 0) {
while (d != NULL && strcmp(line, "@END") != 0 && fgets_chomp(line, 255, f)) {
disk_set_desc(d, line);
}
} else if (strcmp(line, "@SERNO") == 0) {
fgets_chomp(line, 255, f);
if (line[0] != '\0' && strcmp(line, "@END") != 0)
disk_set_serno(d, line);
} else if (strcmp(line, "@SLICES") == 0) {
int cyl, hd, sec;
int sliceno, type, flags;
unsigned long start, size;
while (d != NULL && strcmp(line, "@END") != 0 && fgets_chomp(line, 255, f)) {
if (strncmp(line, "/dev/", 5) == 0) {
cyl = hd = sec = 0;
parse_geometry_info(line, &cyl, &hd, &sec);
disk_set_geometry(d, cyl, hd, sec);
} else if (strncmp(line, "Part", 4) == 0) {
} else {
if (parse_slice_info(line, &sliceno, &start, &size,
&type, &flags)) {
slice_new(d, sliceno, type, flags, start, size);
}
}
}
}
}
if (f != NULL)
fclose(f);
for (d = storage_disk_first(a->s); d != NULL; d = disk_next(d)) {
if (disk_get_desc(d) == NULL)
disk_set_desc(d, disk_get_device_name(d));
}
return(!failure);
}