Skip to content
This repository has been archived by the owner on Feb 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #36 from gao-feng/scsi
Browse files Browse the repository at this point in the history
find scsi disk device if scsi addr is given
  • Loading branch information
feiskyer committed Dec 25, 2015
2 parents d563b3e + ce0ab6b commit 02ba85f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/container.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ static int container_setup_volume(struct hyper_container *container)
for (i = 0; i < container->vols_num; i++) {
vol = &container->vols[i];

if (vol->scsiaddr)
hyper_find_sd("/.oldroot", vol->scsiaddr, &vol->device);

sprintf(dev, "/.oldroot/dev/%s", vol->device);
sprintf(path, "/tmp/%s", vol->mountpoint);
fprintf(stdout, "mount %s to %s, tmp path %s\n",
Expand Down Expand Up @@ -371,6 +374,9 @@ static int hyper_container_init(void *data)
if (container->fstype) {
char dev[128];

if (container->scsiaddr)
hyper_find_sd("", container->scsiaddr, &container->image);

sprintf(dev, "/dev/%s", container->image);
fprintf(stdout, "device %s\n", dev);

Expand Down
2 changes: 2 additions & 0 deletions src/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct env {

struct volume {
char *device;
char *scsiaddr;
char *mountpoint;
char *fstype;
int readonly;
Expand All @@ -30,6 +31,7 @@ struct hyper_container {
char *id;
char *rootfs;
char *image;
char *scsiaddr;
char *workdir;
char *fstype;
struct volume *vols;
Expand Down
13 changes: 13 additions & 0 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static void container_free_volumes(struct hyper_container *c)
free(c->vols[i].device);
free(c->vols[i].mountpoint);
free(c->vols[i].fstype);
free(c->vols[i].scsiaddr);
}
free(c->vols);
c->vols = NULL;
Expand Down Expand Up @@ -115,6 +116,9 @@ static int container_parse_volumes(struct hyper_container *c, char *json, jsmnto
c->vols[j].device =
strdup(json_token_str(json, &toks[++i]));
fprintf(stdout, "volume %d device %s\n", j, c->vols[j].device);
} else if (json_token_streq(json, &toks[i], "addr")) {
c->vols[j].scsiaddr = strdup(json_token_str(json, &toks[++i]));
fprintf(stdout, "volume %d scsi id %s\n", j, c->vols[j].scsiaddr);
} else if (json_token_streq(json, &toks[i], "mount")) {
c->vols[j].mountpoint =
strdup(json_token_str(json, &toks[++i]));
Expand Down Expand Up @@ -321,6 +325,9 @@ void hyper_free_container(struct hyper_container *c)
free(c->image);
c->image = NULL;

free(c->scsiaddr);
c->scsiaddr = NULL;

free(c->workdir);
c->workdir = NULL;

Expand Down Expand Up @@ -403,6 +410,10 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
c->image = strdup(json_token_str(json, &toks[++i]));
fprintf(stdout, "container image %s\n", c->image);
i++;
} else if (json_token_streq(json, t, "addr") && t->size == 1) {
c->scsiaddr = strdup(json_token_str(json, &toks[++i]));
fprintf(stdout, "container image scsi id %s\n", c->scsiaddr);
i++;
} else if (json_token_streq(json, t, "fstype") && t->size == 1) {
c->fstype = strdup(json_token_str(json, &toks[++i]));
fprintf(stdout, "container fstype %s\n", c->fstype);
Expand Down Expand Up @@ -1098,6 +1109,8 @@ int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length)
return ret;
fail:
free(reader->id);
reader->id = NULL;
free(reader->file);
reader->file = NULL;
goto out;
}
34 changes: 34 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,40 @@ int hyper_list_dir(char *path)
return 0;
}

int hyper_find_sd(char *prefix, char *addr, char **dev) {
struct dirent **list;
struct dirent *dir;
char path[512];
int i, num;

sprintf(path, "%s/sys/class/scsi_disk/0:0:%s/device/block/", prefix, addr);
fprintf(stdout, "orig dev %s, scan path %s\n", *dev, path);

num = scandir(path, &list, NULL, NULL);
if (num < 0) {
perror("scan path failed");
return -1;
}

for (i = 0; i < num; i++) {
dir = list[i];
if (dir->d_name[0] == '.') {
continue;
}

fprintf(stdout, "%s get %s\n", path, dir->d_name);
free(*dev);
*dev = strdup(dir->d_name);
break;
}

for (i = 0; i < num; i++)
free(list[i]);

free(list);
return 0;
}

int hyper_mkdir(char *hyper_path)
{
struct stat st;
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct env;

char *read_cmdline(void);
int hyper_setup_env(struct env *envs, int num);
int hyper_find_sd(char *prefix, char *addr, char **dev);
int hyper_list_dir(char *path);
int hyper_mkdir(char *path);
int hyper_open_channel(char *channel, int mode);
Expand Down

0 comments on commit 02ba85f

Please sign in to comment.