Skip to content

Commit

Permalink
setup /etc/hostname for container
Browse files Browse the repository at this point in the history
Signed-off-by: Gao feng <omarapazanadi@gmail.com>
  • Loading branch information
gao-feng committed Apr 6, 2017
1 parent b4d4741 commit 5a94bba
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
34 changes: 24 additions & 10 deletions src/container.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,39 +431,48 @@ static int container_setup_sysctl(struct hyper_container *container)
return 0;
}

static int container_setup_dns(struct hyper_container *container)
static int container_binding_file(char *src, char *dest)
{
int fd;
struct stat st;
char *src = "/tmp/hyper/resolv.conf";

if (stat(src, &st) < 0) {
if (errno == ENOENT) {
fprintf(stdout, "no dns configured\n");
return 0;
}

perror("stat resolve.conf failed");
fprintf(stderr, "stat %s failed", src);
return -1;
}

hyper_mkdir("./etc", 0755);

fd = open("./etc/resolv.conf", O_CREAT| O_WRONLY, 0644);
fd = open(dest, O_CREAT| O_WRONLY, 0644);
if (fd < 0) {
perror("create /etc/resolv.conf failed");
fprintf(stderr, "create %s failed", dest);
return -1;
}
close(fd);

if (mount(src, "./etc/resolv.conf", NULL, MS_BIND, NULL) < 0) {
perror("bind to /etc/resolv.conf failed");
if (mount(src, dest, NULL, MS_BIND, NULL) < 0) {
fprintf(stderr, "bind to %s failed", dest);
return -1;
}

return 0;
}

static int container_setup_dns()
{
hyper_mkdir("./etc", 0755);
return container_binding_file("/tmp/hyper/resolv.conf", "./etc/resolv.conf");
}

static int container_setup_hostname()
{
hyper_mkdir("./etc", 0755);
return container_binding_file("/tmp/hyper/hostname", "./etc/hostname");
}

static int container_setup_workdir(struct hyper_container *container)
{
if (container->initialize) {
Expand Down Expand Up @@ -637,11 +646,16 @@ static int hyper_setup_container_rootfs(void *data)
goto fail;
}

if (container_setup_dns(container) < 0) {
if (container_setup_dns() < 0) {
fprintf(stderr, "container sets up dns failed\n");
goto fail;
}

if (container_setup_hostname() < 0) {
fprintf(stderr, "container sets up hostname failed\n");
goto fail;
}

// manipulate the rootfs of the container/namespace: move the prepared path @rootfs to /
if (mount(rootfs, "/", NULL, MS_MOVE, NULL) < 0) {
perror("failed to move rootfs");
Expand Down
7 changes: 6 additions & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,12 @@ static int hyper_setup_pod(struct hyper_pod *pod)
}

if (hyper_setup_dns(pod) < 0) {
fprintf(stderr, "setup network failed\n");
fprintf(stderr, "setup dns file failed\n");
return -1;
}

if (hyper_setup_hostname(pod) < 0) {
fprintf(stderr, "setup hostname file failed\n");
return -1;
}

Expand Down
34 changes: 34 additions & 0 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,40 @@ int hyper_cmd_setup_route(char *json, int length) {
return ret;
}

int hyper_setup_hostname(struct hyper_pod *pod)
{
int l, fd, ret = -1, len = 0, size = 0;
char buf[512];

if (pod->hostname == NULL)
return 0;

fd = open("/tmp/hyper/hostname", O_CREAT| O_TRUNC| O_WRONLY, 0644);
if (fd < 0) {
perror("create /tmp/hostname failed");
return -1;
}

size = snprintf(buf, sizeof(buf), "%s\n", pod->hostname);
if (size < 0) {
fprintf(stderr, "sprintf hostname failed\n");
goto out;
}

while (len < size) {
l = write(fd, buf + len, size - len);
if (l < 0) {
perror("fail to write hostname");
goto out;
}
len += l;
}
ret = 0;
out:
close(fd);
return ret;
}

int hyper_write_dns_file(int fd, char *field, char **data, int num)
{
int i = 0, len = 0, ret = -1, size;
Expand Down
1 change: 1 addition & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ int hyper_setup_network(struct hyper_pod *pod);
int hyper_cmd_setup_interface(char *json, int length);
int hyper_cmd_setup_route(char *json, int length);
int hyper_setup_dns(struct hyper_pod *pod);
int hyper_setup_hostname(struct hyper_pod *pod);
int hyper_get_type(int fd, uint32_t *type);
int hyper_send_type(int fd, uint32_t type);
int hyper_send_data_block(int fd, uint8_t *data, uint32_t len);
Expand Down

0 comments on commit 5a94bba

Please sign in to comment.