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

Commit 64d5c61

Browse files
authored
Merge pull request #288 from gao-feng/hostname
setup /etc/hostname for container
2 parents 686708b + 5a94bba commit 64d5c61

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
@@ -417,39 +417,48 @@ static int container_setup_sysctl(struct hyper_container *container)
417417
return 0;
418418
}
419419

420-
static int container_setup_dns(struct hyper_container *container)
420+
static int container_binding_file(char *src, char *dest)
421421
{
422422
int fd;
423423
struct stat st;
424-
char *src = "/tmp/hyper/resolv.conf";
425424

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

432-
perror("stat resolve.conf failed");
431+
fprintf(stderr, "stat %s failed", src);
433432
return -1;
434433
}
435434

436-
hyper_mkdir("./etc", 0755);
437-
438-
fd = open("./etc/resolv.conf", O_CREAT| O_WRONLY, 0644);
435+
fd = open(dest, O_CREAT| O_WRONLY, 0644);
439436
if (fd < 0) {
440-
perror("create /etc/resolv.conf failed");
437+
fprintf(stderr, "create %s failed", dest);
441438
return -1;
442439
}
443440
close(fd);
444441

445-
if (mount(src, "./etc/resolv.conf", NULL, MS_BIND, NULL) < 0) {
446-
perror("bind to /etc/resolv.conf failed");
442+
if (mount(src, dest, NULL, MS_BIND, NULL) < 0) {
443+
fprintf(stderr, "bind to %s failed", dest);
447444
return -1;
448445
}
449446

450447
return 0;
451448
}
452449

450+
static int container_setup_dns()
451+
{
452+
hyper_mkdir("./etc", 0755);
453+
return container_binding_file("/tmp/hyper/resolv.conf", "./etc/resolv.conf");
454+
}
455+
456+
static int container_setup_hostname()
457+
{
458+
hyper_mkdir("./etc", 0755);
459+
return container_binding_file("/tmp/hyper/hostname", "./etc/hostname");
460+
}
461+
453462
static int container_setup_workdir(struct hyper_container *container)
454463
{
455464
if (container->initialize) {
@@ -623,11 +632,16 @@ static int hyper_setup_container_rootfs(void *data)
623632
goto fail;
624633
}
625634

626-
if (container_setup_dns(container) < 0) {
635+
if (container_setup_dns() < 0) {
627636
fprintf(stderr, "container sets up dns failed\n");
628637
goto fail;
629638
}
630639

640+
if (container_setup_hostname() < 0) {
641+
fprintf(stderr, "container sets up hostname failed\n");
642+
goto fail;
643+
}
644+
631645
// manipulate the rootfs of the container/namespace: move the prepared path @rootfs to /
632646
if (mount(rootfs, "/", NULL, MS_MOVE, NULL) < 0) {
633647
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)