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

Commit 5a94bba

Browse files
committed
setup /etc/hostname for container
Signed-off-by: Gao feng <omarapazanadi@gmail.com>
1 parent b4d4741 commit 5a94bba

File tree

4 files changed

+65
-11
lines changed

4 files changed

+65
-11
lines changed

src/container.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,39 +431,48 @@ static int container_setup_sysctl(struct hyper_container *container)
431431
return 0;
432432
}
433433

434-
static int container_setup_dns(struct hyper_container *container)
434+
static int container_binding_file(char *src, char *dest)
435435
{
436436
int fd;
437437
struct stat st;
438-
char *src = "/tmp/hyper/resolv.conf";
439438

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

446-
perror("stat resolve.conf failed");
445+
fprintf(stderr, "stat %s failed", src);
447446
return -1;
448447
}
449448

450-
hyper_mkdir("./etc", 0755);
451-
452-
fd = open("./etc/resolv.conf", O_CREAT| O_WRONLY, 0644);
449+
fd = open(dest, O_CREAT| O_WRONLY, 0644);
453450
if (fd < 0) {
454-
perror("create /etc/resolv.conf failed");
451+
fprintf(stderr, "create %s failed", dest);
455452
return -1;
456453
}
457454
close(fd);
458455

459-
if (mount(src, "./etc/resolv.conf", NULL, MS_BIND, NULL) < 0) {
460-
perror("bind to /etc/resolv.conf failed");
456+
if (mount(src, dest, NULL, MS_BIND, NULL) < 0) {
457+
fprintf(stderr, "bind to %s failed", dest);
461458
return -1;
462459
}
463460

464461
return 0;
465462
}
466463

464+
static int container_setup_dns()
465+
{
466+
hyper_mkdir("./etc", 0755);
467+
return container_binding_file("/tmp/hyper/resolv.conf", "./etc/resolv.conf");
468+
}
469+
470+
static int container_setup_hostname()
471+
{
472+
hyper_mkdir("./etc", 0755);
473+
return container_binding_file("/tmp/hyper/hostname", "./etc/hostname");
474+
}
475+
467476
static int container_setup_workdir(struct hyper_container *container)
468477
{
469478
if (container->initialize) {
@@ -637,11 +646,16 @@ static int hyper_setup_container_rootfs(void *data)
637646
goto fail;
638647
}
639648

640-
if (container_setup_dns(container) < 0) {
649+
if (container_setup_dns() < 0) {
641650
fprintf(stderr, "container sets up dns failed\n");
642651
goto fail;
643652
}
644653

654+
if (container_setup_hostname() < 0) {
655+
fprintf(stderr, "container sets up hostname failed\n");
656+
goto fail;
657+
}
658+
645659
// manipulate the rootfs of the container/namespace: move the prepared path @rootfs to /
646660
if (mount(rootfs, "/", NULL, MS_MOVE, NULL) < 0) {
647661
perror("failed to move rootfs");

src/init.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,12 @@ static int hyper_setup_pod(struct hyper_pod *pod)
510510
}
511511

512512
if (hyper_setup_dns(pod) < 0) {
513-
fprintf(stderr, "setup network failed\n");
513+
fprintf(stderr, "setup dns file failed\n");
514+
return -1;
515+
}
516+
517+
if (hyper_setup_hostname(pod) < 0) {
518+
fprintf(stderr, "setup hostname file failed\n");
514519
return -1;
515520
}
516521

src/net.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,40 @@ int hyper_cmd_setup_route(char *json, int length) {
677677
return ret;
678678
}
679679

680+
int hyper_setup_hostname(struct hyper_pod *pod)
681+
{
682+
int l, fd, ret = -1, len = 0, size = 0;
683+
char buf[512];
684+
685+
if (pod->hostname == NULL)
686+
return 0;
687+
688+
fd = open("/tmp/hyper/hostname", O_CREAT| O_TRUNC| O_WRONLY, 0644);
689+
if (fd < 0) {
690+
perror("create /tmp/hostname failed");
691+
return -1;
692+
}
693+
694+
size = snprintf(buf, sizeof(buf), "%s\n", pod->hostname);
695+
if (size < 0) {
696+
fprintf(stderr, "sprintf hostname failed\n");
697+
goto out;
698+
}
699+
700+
while (len < size) {
701+
l = write(fd, buf + len, size - len);
702+
if (l < 0) {
703+
perror("fail to write hostname");
704+
goto out;
705+
}
706+
len += l;
707+
}
708+
ret = 0;
709+
out:
710+
close(fd);
711+
return ret;
712+
}
713+
680714
int hyper_write_dns_file(int fd, char *field, char **data, int num)
681715
{
682716
int i = 0, len = 0, ret = -1, size;

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ int hyper_setup_network(struct hyper_pod *pod);
4949
int hyper_cmd_setup_interface(char *json, int length);
5050
int hyper_cmd_setup_route(char *json, int length);
5151
int hyper_setup_dns(struct hyper_pod *pod);
52+
int hyper_setup_hostname(struct hyper_pod *pod);
5253
int hyper_get_type(int fd, uint32_t *type);
5354
int hyper_send_type(int fd, uint32_t type);
5455
int hyper_send_data_block(int fd, uint8_t *data, uint32_t len);

0 commit comments

Comments
 (0)