@@ -287,10 +287,13 @@ static int container_setup_mount(struct hyper_container *container)
287
287
char src [512 ];
288
288
289
289
// 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
+ }
294
297
295
298
if (mount ("proc" , "./proc" , "proc" , MS_NOSUID | MS_NODEV | MS_NOEXEC , NULL ) < 0 ||
296
299
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)
377
380
static int container_setup_init_layer (struct hyper_container * container ,
378
381
int setup_dns )
379
382
{
380
- if (!container -> initialize )
383
+ if (!container -> initialize || container -> readonly )
381
384
return 0 ;
382
385
383
386
hyper_mkdir ("./etc/" , 0755 );
@@ -471,7 +474,7 @@ static int container_setup_hostname()
471
474
472
475
static int container_setup_workdir (struct hyper_container * container )
473
476
{
474
- if (container -> initialize ) {
477
+ if (container -> initialize && ! container -> readonly ) {
475
478
// create workdir
476
479
return hyper_mkdir (container -> exec .workdir , 0755 );
477
480
}
@@ -572,6 +575,7 @@ static int hyper_setup_container_rootfs(void *data)
572
575
if (container -> fstype ) {
573
576
char dev [128 ];
574
577
char * options = NULL ;
578
+ unsigned long flags = 0 ;
575
579
576
580
/* wait for rootfs ready message */
577
581
if (hyper_eventfd_recv (arg -> container_root_dev_efd ) < 0 ) {
@@ -587,10 +591,13 @@ static int hyper_setup_container_rootfs(void *data)
587
591
sprintf (dev , "/dev/%s" , container -> image );
588
592
fprintf (stdout , "device %s\n" , dev );
589
593
594
+ if (container -> readonly )
595
+ flags = MS_RDONLY ;
596
+
590
597
if (!strncmp (container -> fstype , "xfs" , strlen ("xfs" )))
591
598
options = "nouuid" ;
592
599
593
- if (mount (dev , root , container -> fstype , 0 , options ) < 0 ) {
600
+ if (mount (dev , root , container -> fstype , flags , options ) < 0 ) {
594
601
perror ("mount device failed" );
595
602
goto fail ;
596
603
}
@@ -604,6 +611,10 @@ static int hyper_setup_container_rootfs(void *data)
604
611
perror ("mount src dir failed" );
605
612
goto fail ;
606
613
}
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
+ }
607
618
}
608
619
609
620
fprintf (stdout , "root directory for container is %s/%s, init task %s\n" ,
0 commit comments