2020#include "hyper.h"
2121#include "parse.h"
2222#include "syscall.h"
23+ #include "netlink.h"
2324
2425static int container_populate_volume (char * src , char * dest )
2526{
@@ -134,7 +135,7 @@ static int container_setup_volume(struct hyper_container *container)
134135 if (!strcmp (vol -> fstype , "xfs" ))
135136 options = "nouuid" ;
136137
137- if (hyper_mount_blockdev (dev , path , vol -> fstype , options ) < 0 ) {
138+ if (mount (dev , path , vol -> fstype , 0 , options ) < 0 ) {
138139 perror ("mount volume device failed" );
139140 return -1 ;
140141 }
@@ -528,6 +529,7 @@ struct hyper_container_arg {
528529 struct hyper_pod * pod ;
529530 int mntns_referenced_efd ;
530531 int container_inited_efd ;
532+ int container_root_dev_efd ;
531533};
532534
533535static int hyper_setup_container_rootfs (void * data )
@@ -551,11 +553,6 @@ static int hyper_setup_container_rootfs(void *data)
551553 /* To create files/directories accessible for all users. */
552554 umask (0 );
553555
554- if (container -> fstype && hyper_rescan_scsi () < 0 ) {
555- fprintf (stdout , "rescan scsi failed\n" );
556- goto fail ;
557- }
558-
559556 if (mount ("" , "/" , NULL , MS_SLAVE |MS_REC , NULL ) < 0 ) {
560557 perror ("mount SLAVE failed" );
561558 goto fail ;
@@ -576,19 +573,24 @@ static int hyper_setup_container_rootfs(void *data)
576573 char dev [128 ];
577574 char * options = NULL ;
578575
576+ /* wait for rootfs ready message */
577+ if (hyper_eventfd_recv (arg -> container_root_dev_efd ) < 0 ) {
578+ fprintf (stderr , "wait for /proc/self/ns/mnt opened failed\n" );
579+ goto fail ;
580+ }
581+
579582 if (container -> scsiaddr ) {
580583 free (container -> image );
581584 container -> image = NULL ;
582585 hyper_find_sd (container -> scsiaddr , & container -> image );
583586 }
584-
585587 sprintf (dev , "/dev/%s" , container -> image );
586588 fprintf (stdout , "device %s\n" , dev );
587589
588590 if (!strncmp (container -> fstype , "xfs" , strlen ("xfs" )))
589591 options = "nouuid" ;
590592
591- if (hyper_mount_blockdev (dev , root , container -> fstype , options ) < 0 ) {
593+ if (mount (dev , root , container -> fstype , 0 , options ) < 0 ) {
592594 perror ("mount device failed" );
593595 goto fail ;
594596 }
@@ -717,6 +719,36 @@ static void hyper_cleanup_pty(struct hyper_container *c)
717719 perror ("clean up container pty failed" );
718720}
719721
722+ int container_prepare_rootfs_dev (struct hyper_container * container , struct hyper_pod * pod )
723+ {
724+ char dev [512 ];
725+
726+ if (container -> fstype == NULL )
727+ return 0 ;
728+
729+ if (hyper_rescan_scsi () < 0 ) {
730+ fprintf (stderr , "failed to issue scsi rescan\n" );
731+ return -1 ;
732+ }
733+
734+ if (container -> scsiaddr ) {
735+ free (container -> image );
736+ container -> image = NULL ;
737+ hyper_find_sd (container -> scsiaddr , & container -> image );
738+ }
739+
740+ if (container -> image ) {
741+ sprintf (dev , "/dev/%s" , container -> image );
742+ if (access (dev , R_OK ) == 0 )
743+ return 0 ;
744+ sprintf (dev , "/block/%s" , container -> image );
745+ } else {
746+ sprintf (dev , "/0:0:%s/block/" , container -> scsiaddr );
747+ }
748+
749+ return hyper_netlink_wait_dev (pod -> ueventfd , dev );
750+ }
751+
720752int hyper_setup_container (struct hyper_container * container , struct hyper_pod * pod )
721753{
722754 int stacksize = getpagesize () * 42 ;
@@ -725,6 +757,7 @@ int hyper_setup_container(struct hyper_container *container, struct hyper_pod *p
725757 .pod = pod ,
726758 .mntns_referenced_efd = -1 ,
727759 .container_inited_efd = -1 ,
760+ .container_root_dev_efd = -1 ,
728761 };
729762 int flags = CLONE_NEWNS | SIGCHLD ;
730763 char path [128 ];
@@ -735,7 +768,9 @@ int hyper_setup_container(struct hyper_container *container, struct hyper_pod *p
735768
736769 arg .mntns_referenced_efd = eventfd (0 , EFD_CLOEXEC );
737770 arg .container_inited_efd = eventfd (0 , EFD_CLOEXEC );
738- if (arg .mntns_referenced_efd < 0 || arg .container_inited_efd < 0 ) {
771+ arg .container_root_dev_efd = eventfd (0 , EFD_CLOEXEC );
772+ if (arg .mntns_referenced_efd < 0 || arg .container_inited_efd < 0 ||
773+ arg .container_root_dev_efd < 0 ) {
739774 perror ("create eventfd between pod init execcmd failed" );
740775 goto fail ;
741776 }
@@ -772,6 +807,13 @@ int hyper_setup_container(struct hyper_container *container, struct hyper_pod *p
772807 fprintf (stdout , "hyper send mntns referenced event: normal\n" );
773808 hyper_eventfd_send (arg .mntns_referenced_efd , HYPER_EVENTFD_NORMAL );
774809
810+ if (container_prepare_rootfs_dev (container , pod ) < 0 ) {
811+ fprintf (stderr , "fail to prepare container rootfs dev\n" );
812+ goto fail ;
813+ }
814+ fprintf (stdout , "hyper send root dev ready event: normal\n" );
815+ hyper_eventfd_send (arg .container_root_dev_efd , HYPER_EVENTFD_NORMAL );
816+
775817 /* wait for ready message */
776818 if (hyper_eventfd_recv (arg .container_inited_efd ) < 0 ) {
777819 fprintf (stderr , "wait for setup container rootfs failed\n" );
@@ -780,12 +822,14 @@ int hyper_setup_container(struct hyper_container *container, struct hyper_pod *p
780822
781823 close (arg .mntns_referenced_efd );
782824 close (arg .container_inited_efd );
825+ close (arg .container_root_dev_efd );
783826 return 0 ;
784827fail :
785828 close (container -> ns );
786829 container -> ns = -1 ;
787830 close (arg .mntns_referenced_efd );
788831 close (arg .container_inited_efd );
832+ close (arg .container_root_dev_efd );
789833 return -1 ;
790834}
791835
0 commit comments