@@ -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 );
402402out :
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+
465478static 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
587600static 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+
677716static 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
918957static 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 ;
0 commit comments