@@ -287,10 +287,13 @@ static int container_setup_mount(struct hyper_container *container)
287287 char src [512 ];
288288
289289 // current dir is container rootfs, the operations on "./PATH" are the operations on container's "/PATH"
290- hyper_mkdir ("./proc" , 0755 );
291- hyper_mkdir ("./sys" , 0755 );
292- hyper_mkdir ("./dev" , 0755 );
293- hyper_mkdir ("./lib/modules" , 0755 );
290+ if (!container -> readonly ) {
291+ hyper_mkdir ("./proc" , 0755 );
292+ hyper_mkdir ("./sys" , 0755 );
293+ hyper_mkdir ("./dev" , 0755 );
294+ hyper_mkdir ("./lib/modules" , 0755 );
295+
296+ }
294297
295298 if (mount ("proc" , "./proc" , "proc" , MS_NOSUID | MS_NODEV | MS_NOEXEC , NULL ) < 0 ||
296299 mount ("sysfs" , "./sys" , "sysfs" , MS_NOSUID | MS_NODEV | MS_NOEXEC , NULL ) < 0 ||
@@ -377,7 +380,7 @@ static int container_recreate_symlink(char *oldpath, char *newpath)
377380static int container_setup_init_layer (struct hyper_container * container ,
378381 int setup_dns )
379382{
380- if (!container -> initialize )
383+ if (!container -> initialize || container -> readonly )
381384 return 0 ;
382385
383386 hyper_mkdir ("./etc/" , 0755 );
@@ -471,7 +474,7 @@ static int container_setup_hostname()
471474
472475static int container_setup_workdir (struct hyper_container * container )
473476{
474- if (container -> initialize ) {
477+ if (container -> initialize && ! container -> readonly ) {
475478 // create workdir
476479 return hyper_mkdir (container -> exec .workdir , 0755 );
477480 }
@@ -572,6 +575,7 @@ static int hyper_setup_container_rootfs(void *data)
572575 if (container -> fstype ) {
573576 char dev [128 ];
574577 char * options = NULL ;
578+ unsigned long flags = 0 ;
575579
576580 /* wait for rootfs ready message */
577581 if (hyper_eventfd_recv (arg -> container_root_dev_efd ) < 0 ) {
@@ -587,10 +591,13 @@ static int hyper_setup_container_rootfs(void *data)
587591 sprintf (dev , "/dev/%s" , container -> image );
588592 fprintf (stdout , "device %s\n" , dev );
589593
594+ if (container -> readonly )
595+ flags = MS_RDONLY ;
596+
590597 if (!strncmp (container -> fstype , "xfs" , strlen ("xfs" )))
591598 options = "nouuid" ;
592599
593- if (mount (dev , root , container -> fstype , 0 , options ) < 0 ) {
600+ if (mount (dev , root , container -> fstype , flags , options ) < 0 ) {
594601 perror ("mount device failed" );
595602 goto fail ;
596603 }
@@ -604,6 +611,10 @@ static int hyper_setup_container_rootfs(void *data)
604611 perror ("mount src dir failed" );
605612 goto fail ;
606613 }
614+ if (container -> readonly && mount (NULL , root , NULL , MS_BIND | MS_REMOUNT | MS_RDONLY , NULL ) < 0 ) {
615+ perror ("mount src dir readonly failed" );
616+ goto fail ;
617+ }
607618 }
608619
609620 fprintf (stdout , "root directory for container is %s/%s, init task %s\n" ,
0 commit comments