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

Commit 9693004

Browse files
authored
Merge pull request #314 from bergwolf/rootfs_ro
support readonly rootfs
2 parents 89c69c2 + 354b607 commit 9693004

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

src/api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _HYPERSTART_API_H_
33

44
// when APIVERSION < 1000000, the version MUST be exactly matched on both sides
5-
#define APIVERSION 4243
5+
#define APIVERSION 4244
66

77
// control command id
88
enum {

src/container.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
377380
static 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

472475
static 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",

src/container.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct hyper_container {
5151
int sys_num;
5252
int ports_num;
5353
int initialize;
54+
int readonly;
5455
};
5556

5657
struct hyper_pod;

src/parse.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,12 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
716716
dbg_pr(stdout, "need to initialize container\n");
717717
}
718718
i++;
719+
} else if (json_token_streq(json, t, "readOnly") && t->size == 1) {
720+
if (!json_token_streq(json, &toks[++i], "false")) {
721+
c->readonly = 1;
722+
dbg_pr(stdout, "container rootfs is readonly\n");
723+
}
724+
i++;
719725
} else if (json_token_streq(json, t, "ports") && t->size == 1) {
720726
next = container_parse_ports(c, json, &toks[++i]);
721727
if (next < 0)

0 commit comments

Comments
 (0)