From ce0ab6b8368b1886203b29e14c6c589f668faf63 Mon Sep 17 00:00:00 2001 From: Gao feng Date: Thu, 24 Dec 2015 15:51:16 +0800 Subject: [PATCH] find scsi disk device if scsi addr is given scsi disk name is not consistent with scsi addr. find the device through scsi addr. Signed-off-by: Gao feng --- src/container.c | 6 ++++++ src/container.h | 2 ++ src/parse.c | 13 +++++++++++++ src/util.c | 34 ++++++++++++++++++++++++++++++++++ src/util.h | 1 + 5 files changed, 56 insertions(+) diff --git a/src/container.c b/src/container.c index afccdbd6..c174d981 100644 --- a/src/container.c +++ b/src/container.c @@ -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", @@ -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); diff --git a/src/container.h b/src/container.h index a74f963a..a3a6bd28 100644 --- a/src/container.h +++ b/src/container.h @@ -10,6 +10,7 @@ struct env { struct volume { char *device; + char *scsiaddr; char *mountpoint; char *fstype; int readonly; @@ -30,6 +31,7 @@ struct hyper_container { char *id; char *rootfs; char *image; + char *scsiaddr; char *workdir; char *fstype; struct volume *vols; diff --git a/src/parse.c b/src/parse.c index c7283266..2e879e2d 100644 --- a/src/parse.c +++ b/src/parse.c @@ -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; @@ -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])); @@ -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; @@ -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); @@ -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; } diff --git a/src/util.c b/src/util.c index 5af7e505..3f481da8 100644 --- a/src/util.c +++ b/src/util.c @@ -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; diff --git a/src/util.h b/src/util.h index dbaa352e..4d5b4f8d 100644 --- a/src/util.h +++ b/src/util.h @@ -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);