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

Commit 3b0ff1d

Browse files
committed
Merge pull request #21 from laijs/new-container
New container
2 parents b5f8137 + 62dea14 commit 3b0ff1d

File tree

3 files changed

+114
-26
lines changed

3 files changed

+114
-26
lines changed

src/hyper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ enum {
2727
NEXT,
2828
WRITEFILE,
2929
READFILE,
30+
NEWCONTAINER,
3031
};
3132

3233
enum {
@@ -43,7 +44,7 @@ struct hyper_pod {
4344
struct list_head exec_head;
4445
//struct list_head ce_head;
4546
char *hostname;
46-
char *tag;
47+
char *share_tag;
4748
int init_pid;
4849
uint32_t c_num;
4950
uint32_t i_num;

src/init.c

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -349,16 +349,24 @@ static int hyper_pod_init(void *data)
349349
goto out;
350350
}
351351

352-
static int hyper_do_start_containers(void *data)
352+
struct hyper_stage0_arg {
353+
struct hyper_pod *pod;
354+
struct hyper_container *container;
355+
int ctl_pipe[2];
356+
};
357+
358+
// stage0: enter the pidns
359+
static int hyper_container_stage0(void *data)
353360
{
354-
int i, pidns, ipcns, utsns, ret;
361+
int pidns, ipcns, utsns, ret;
355362
struct hyper_container *c;
356-
struct hyper_pod_arg *arg;
363+
struct hyper_stage0_arg *arg;
357364
struct hyper_pod *pod;
358365
char path[64];
359366

360367
arg = data;
361368
pod = arg->pod;
369+
c = arg->container;
362370

363371
ret = pidns = ipcns = utsns = -1;
364372

@@ -390,15 +398,7 @@ static int hyper_do_start_containers(void *data)
390398
goto out;
391399
}
392400

393-
for (i = 0; i < pod->c_num; i++) {
394-
c = &pod->c[i];
395-
if (hyper_start_container(c, utsns, ipcns, pod) < 0) {
396-
fprintf(stderr, "fail to start container\n");
397-
goto out;
398-
}
399-
}
400-
401-
ret = 0;
401+
ret = hyper_start_container(c, utsns, ipcns, pod);
402402
out:
403403
if (hyper_send_type(arg->ctl_pipe[1], ret ? ERROR : READY) < 0) {
404404
fprintf(stderr, "container init send ready message failed\n");
@@ -413,12 +413,13 @@ static int hyper_do_start_containers(void *data)
413413
_exit(ret);
414414
}
415415

416-
int hyper_start_containers(struct hyper_pod *pod)
416+
int hyper_start_container_stage0(struct hyper_container *c, struct hyper_pod *pod)
417417
{
418418
int stacksize = getpagesize() * 4;
419419
void *stack = NULL;
420-
struct hyper_pod_arg arg = {
420+
struct hyper_stage0_arg arg = {
421421
.pod = pod,
422+
.container = c,
422423
.ctl_pipe = {-1, -1},
423424
};
424425
int ret = -1, pid;
@@ -436,7 +437,7 @@ int hyper_start_containers(struct hyper_pod *pod)
436437
goto out;
437438
}
438439

439-
pid = clone(hyper_do_start_containers, stack + stacksize, CLONE_VM| CLONE_FILES| SIGCHLD, &arg);
440+
pid = clone(hyper_container_stage0, stack + stacksize, CLONE_VM| CLONE_FILES| SIGCHLD, &arg);
440441
free(stack);
441442
if (pid < 0) {
442443
perror("enter container pid ns failed");
@@ -462,6 +463,18 @@ int hyper_start_containers(struct hyper_pod *pod)
462463
return ret;
463464
}
464465

466+
int hyper_start_containers(struct hyper_pod *pod)
467+
{
468+
int i, ret = 0;
469+
470+
for (i = 0; i < pod->c_num; i++) {
471+
ret = hyper_start_container_stage0(&pod->c[i], pod);
472+
if (ret)
473+
return ret;
474+
}
475+
return 0;
476+
}
477+
465478
static int hyper_setup_container(struct hyper_pod *pod)
466479
{
467480
int stacksize = getpagesize() * 4;
@@ -555,7 +568,7 @@ static int hyper_setup_shared(struct hyper_pod *pod)
555568
{
556569
struct vbsf_mount_info_new mntinf;
557570

558-
if (pod->tag == NULL) {
571+
if (pod->share_tag == NULL) {
559572
fprintf(stdout, "no shared directroy\n");
560573
return 0;
561574
}
@@ -573,7 +586,7 @@ static int hyper_setup_shared(struct hyper_pod *pod)
573586
mntinf.length = sizeof(mntinf);
574587
mntinf.dmode = ~0U;
575588
mntinf.fmode = ~0U;
576-
strcpy(mntinf.name, pod->tag);
589+
strcpy(mntinf.name, pod->share_tag);
577590

578591
if (mount(NULL, "/tmp/hyper/shared", "vboxsf",
579592
MS_NODEV, &mntinf) < 0) {
@@ -586,7 +599,7 @@ static int hyper_setup_shared(struct hyper_pod *pod)
586599
#else
587600
static int hyper_setup_shared(struct hyper_pod *pod)
588601
{
589-
if (pod->tag == NULL) {
602+
if (pod->share_tag == NULL) {
590603
fprintf(stdout, "no shared directroy\n");
591604
return 0;
592605
}
@@ -596,7 +609,7 @@ static int hyper_setup_shared(struct hyper_pod *pod)
596609
return -1;
597610
}
598611

599-
if (mount(pod->tag, "/tmp/hyper/shared", "9p",
612+
if (mount(pod->share_tag, "/tmp/hyper/shared", "9p",
600613
MS_MGC_VAL| MS_NODEV, "trans=virtio,cache=loose") < 0) {
601614

602615
perror("fail to mount shared dir");
@@ -674,6 +687,32 @@ static int hyper_start_pod(char *json, int length)
674687
return 0;
675688
}
676689

690+
static int hyper_new_container(char *json, int length)
691+
{
692+
int ret;
693+
struct hyper_pod *pod = &global_pod;
694+
695+
fprintf(stdout, "call hyper_new_container, json %s, len %d\n", json, length);
696+
697+
if (!pod->init_pid)
698+
fprintf(stdout, "the pod is not created yet\n");
699+
700+
if (hyper_parse_new_container(pod, json, length) < 0) {
701+
fprintf(stderr, "parse container json failed\n");
702+
return -1;
703+
}
704+
705+
ret = hyper_start_container_stage0(&pod->c[pod->c_num - 1], pod);
706+
if (ret < 0) {
707+
//TODO full grace cleanup
708+
pod->remains -= 1;
709+
pod->c_num -= 1;
710+
return ret;
711+
}
712+
713+
return 0;
714+
}
715+
677716
static int hyper_cmd_write_file(char *json, int length)
678717
{
679718
struct hyper_writter writter;
@@ -917,16 +956,16 @@ static void hyper_cleanup_hostname(struct hyper_pod *pod)
917956

918957
static void hyper_cleanup_shared(struct hyper_pod *pod)
919958
{
920-
if (pod->tag == NULL) {
959+
if (pod->share_tag == NULL) {
921960
fprintf(stdout, "no shared directroy\n");
922961
return;
923962
}
924963

925-
free(pod->tag);
926-
pod->tag = NULL;
964+
free(pod->share_tag);
965+
pod->share_tag = NULL;
927966
if (umount("/tmp/hyper/shared") < 0 &&
928967
umount2("/tmp/hyper/shared", MNT_DETACH)) {
929-
perror("fail to umount 9p dir");
968+
perror("fail to umount shared dir");
930969
return;
931970
}
932971

@@ -1099,6 +1138,9 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len)
10991138
case WINSIZE:
11001139
ret = hyper_set_win_size((char *)buf->data + 8, len - 8);
11011140
break;
1141+
case NEWCONTAINER:
1142+
ret = hyper_new_container((char *)buf->data + 8, len - 8);
1143+
break;
11021144
default:
11031145
ret = -1;
11041146
break;

src/parse.c

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,8 @@ int hyper_parse_pod(struct hyper_pod *pod, char *json, int length)
510510

511511
i += next;
512512
} else if (json_token_streq(json, t, "shareDir") && t->size == 1) {
513-
pod->tag = strdup(json_token_str(json, &toks[++i]));
514-
fprintf(stdout, "9p tag is %s\n", pod->tag);
513+
pod->share_tag = strdup(json_token_str(json, &toks[++i]));
514+
fprintf(stdout, "share tag is %s\n", pod->share_tag);
515515
} else if (json_token_streq(json, t, "hostname") && t->size == 1) {
516516
pod->hostname = strdup(json_token_str(json, &toks[++i]));
517517
fprintf(stdout, "hostname is %s\n", pod->hostname);
@@ -530,6 +530,51 @@ int hyper_parse_pod(struct hyper_pod *pod, char *json, int length)
530530
return next;
531531
}
532532

533+
int hyper_parse_new_container(struct hyper_pod *pod, char *json, int length)
534+
{
535+
int n;
536+
jsmn_parser p;
537+
int toks_num = 100;
538+
jsmntok_t *toks = NULL;
539+
struct hyper_container *c;
540+
541+
realloc:
542+
toks = realloc(toks, toks_num * sizeof(jsmntok_t));
543+
544+
jsmn_init(&p);
545+
n = jsmn_parse(&p, json, length, toks, toks_num);
546+
if (n < 0) {
547+
fprintf(stdout, "jsmn parse failed, n is %d\n", n);
548+
if (n == JSMN_ERROR_NOMEM) {
549+
toks_num *= 2;
550+
goto realloc;
551+
}
552+
553+
goto fail;
554+
}
555+
556+
c = realloc(pod->c, (pod->c_num + 1) * sizeof(*pod->c));
557+
if (c == NULL) {
558+
fprintf(stdout, "alloc memory for container failed\n");
559+
goto fail;
560+
}
561+
pod->c = c;
562+
memset(&pod->c[pod->c_num], 0, sizeof(pod->c[pod->c_num]));
563+
564+
// trick: toks-1, TODO: change all "i = 1" to "i = 0"
565+
if (hyper_parse_container(pod, &pod->c[pod->c_num], json, toks-1) < 0)
566+
goto fail;
567+
568+
pod->remains += 1;
569+
pod->c_num += 1;
570+
free(toks);
571+
return 0;
572+
573+
fail:
574+
free(toks);
575+
return -1;
576+
}
577+
533578
int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length)
534579
{
535580
int i, n, ret = 0;

0 commit comments

Comments
 (0)