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

Commit ac5e6b1

Browse files
committed
unmount container mounts in container mnt ns
Otherwise we are in init process mnt ns and have no idea of what is mounted by containers. Signed-off-by: Peng Tao <[email protected]>
1 parent 2175595 commit ac5e6b1

File tree

7 files changed

+50
-10
lines changed

7 files changed

+50
-10
lines changed

src/container.c

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -789,15 +789,52 @@ struct hyper_container *hyper_find_container(struct hyper_pod *pod, const char *
789789
return NULL;
790790
}
791791

792-
void hyper_cleanup_container(struct hyper_container *c, struct hyper_pod *pod)
792+
static void hyper_cleanup_container_mounts(struct hyper_container *container, struct hyper_pod *pod)
793793
{
794-
char root[512];
794+
int pid, pipe[2] = {-1, -1};
795795

796-
sprintf(root, "/tmp/hyper/%s/devpts/", c->id);
797-
if (umount(root) < 0 && umount2(root, MNT_DETACH))
798-
perror("umount devpts failed");
796+
if (pipe2(pipe, O_CLOEXEC) < 0) {
797+
perror("create pipe for unmount failed");
798+
return;
799+
}
800+
801+
pid = fork();
802+
if (pid < 0) {
803+
perror("fork unmount process failed");
804+
goto out;
805+
} else if (pid == 0) {
806+
if (hyper_enter_sandbox(pod, -1) < 0) {
807+
hyper_send_type(pipe[1], -1);
808+
_exit(-1);
809+
}
810+
if (setns(container->ns, CLONE_NEWNS) < 0) {
811+
perror("fail to enter container ns");
812+
hyper_send_type(pipe[1], -1);
813+
_exit(-1);
814+
}
815+
hyper_unmount_all();
816+
hyper_send_type(pipe[1], 0);
817+
_exit(0);
818+
}
819+
hyper_get_type(pipe[0], (uint32_t *)&pid);
799820

821+
out:
822+
close(pipe[0]);
823+
close(pipe[1]);
824+
}
825+
826+
void hyper_cleanup_container(struct hyper_container *c, struct hyper_pod *pod)
827+
{
828+
hyper_cleanup_container_mounts(c, pod);
800829
close(c->ns);
801830
hyper_cleanup_container_portmapping(c, pod);
802831
hyper_free_container(c);
803832
}
833+
834+
void hyper_cleanup_mounts(struct hyper_pod *pod)
835+
{
836+
struct hyper_container *c;
837+
838+
list_for_each_entry(c, &pod->containers, list)
839+
hyper_cleanup_container_mounts(c, pod);
840+
}

src/container.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct hyper_pod;
5858
int hyper_setup_container(struct hyper_container *container, struct hyper_pod *pod);
5959
struct hyper_container *hyper_find_container(struct hyper_pod *pod, const char *id);
6060
void hyper_cleanup_container(struct hyper_container *container, struct hyper_pod *pod);
61+
void hyper_cleanup_mounts(struct hyper_pod *pod);
6162
void hyper_free_container(struct hyper_container *c);
6263

6364
static inline int hyper_has_container(struct hyper_pod *pod, const char *id) {

src/exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ static int hyper_release_exec(struct hyper_exec *exec)
715715

716716
if (--exec->pod->remains == 0 && exec->pod->req_destroy) {
717717
/* shutdown vm manually, hyper doesn't care the pod finished codes */
718-
hyper_pod_destroyed(0);
718+
hyper_pod_destroyed(exec->pod, 0);
719719
}
720720

721721
return 0;

src/hyper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static inline int hyper_create(char *hyper_path)
8484
}
8585

8686
int hyper_enter_sandbox(struct hyper_pod *pod, int pidpipe);
87-
void hyper_pod_destroyed(int failed);
87+
void hyper_pod_destroyed(struct hyper_pod *pod, int failed);
8888
int hyper_ctl_append_msg(struct hyper_event *he, uint32_t type, uint8_t *data, uint32_t len);
8989

9090
extern struct hyper_epoll hyper_epoll;

src/init.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,9 @@ static void hyper_flush_channel()
560560
hyper_send_data_block(hyper_epoll.tty.fd, tty_buf->data, tty_buf->get);
561561
}
562562

563-
void hyper_pod_destroyed(int failed)
563+
void hyper_pod_destroyed(struct hyper_pod *pod, int failed)
564564
{
565+
hyper_cleanup_mounts(pod);
565566
hyper_ctl_append_msg(&hyper_epoll.ctl, failed?ERROR:ACK, NULL, 0);
566567
// Todo: this doesn't make sure peer receives the data
567568
hyper_flush_channel();
@@ -574,7 +575,7 @@ static int hyper_destroy_pod(struct hyper_pod *pod, int error)
574575
{
575576
if (pod->init_pid == 0 || pod->remains == 0) {
576577
/* Pod stopped, just shutdown */
577-
hyper_pod_destroyed(error);
578+
hyper_pod_destroyed(pod, error);
578579
} else {
579580
/* Kill pod */
580581
hyper_term_all(pod);

src/util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ int hyper_setfd_nonblock(int fd)
746746
return flags;
747747
}
748748

749-
static void hyper_unmount_all(void)
749+
void hyper_unmount_all(void)
750750
{
751751
FILE *mtab;
752752
struct mntent *mnt;

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ int hyper_setfd_cloexec(int fd);
3636
int hyper_setfd_block(int fd);
3737
int hyper_setfd_nonblock(int fd);
3838
void hyper_shutdown();
39+
void hyper_unmount_all(void);
3940
int hyper_insmod(char *module);
4041
bool hyper_name_to_id(const char *name, unsigned long *val);
4142
struct passwd *hyper_getpwnam(const char *name);

0 commit comments

Comments
 (0)