Skip to content

Commit

Permalink
Merge pull request #519 from thehajime/feature-nameserver
Browse files Browse the repository at this point in the history
lkl: add nameserver config for json
  • Loading branch information
thehajime authored Jun 9, 2023
2 parents 2f7f422 + cfeefae commit 0ef1681
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Documentation/lkl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@ The following are the list of keys to describe a JSON file.
"delay_main":"500000"
```

* nameserver

key: "nameserver"
value type: string

a name server address, which will be written in /etc/resolv.conf into a
filesystem used by a LKL instance.
```
"nameserver":"8.8.8.8"
```

FAQ
===

Expand Down
1 change: 1 addition & 0 deletions tools/lkl/include/lkl_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct lkl_config {
char *boot_cmdline;
char *dump;
char *delay_main;
char *nameserver;
};

#ifdef LKL_HOST_CONFIG_JSMN
Expand Down
18 changes: 18 additions & 0 deletions tools/lkl/lib/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ int lkl_load_config_json(struct lkl_config *cfg, const char *jstr)
cfgptr = &cfg->dump;
} else if (jsoneq(jstr, &toks[pos], "delay_main") == 0) {
cfgptr = &cfg->delay_main;
} else if (jsoneq(jstr, &toks[pos], "nameserver") == 0) {
cfgptr = &cfg->nameserver;
} else {
lkl_printf("unexpected key in json %.*s\n",
toks[pos].end-toks[pos].start,
Expand Down Expand Up @@ -218,6 +220,7 @@ void lkl_show_config(struct lkl_config *cfg)
return;
lkl_printf("gateway: %s\n", cfg->gateway);
lkl_printf("gateway6: %s\n", cfg->gateway6);
lkl_printf("nameserver: %s\n", cfg->nameserver);
lkl_printf("debug: %s\n", cfg->debug);
lkl_printf("mount: %s\n", cfg->mount);
lkl_printf("singlecpu: %s\n", cfg->single_cpu);
Expand Down Expand Up @@ -689,6 +692,7 @@ static int lkl_clean_config(struct lkl_config *cfg)
free_cfgparam(cfg->boot_cmdline);
free_cfgparam(cfg->dump);
free_cfgparam(cfg->delay_main);
free_cfgparam(cfg->nameserver);
return 0;
}

Expand Down Expand Up @@ -777,6 +781,20 @@ int lkl_load_config_post(struct lkl_config *cfg)
}
}

if (cfg->nameserver) {
int fd;
char ns[32] = "nameserver ";

/* ignore error */
lkl_sys_mkdir("/etc", 0xff);
lkl_sys_chdir("/etc");
fd = lkl_sys_open("/etc/resolv.conf", LKL_O_CREAT | LKL_O_RDWR, 0);

strcat(ns, cfg->nameserver);
lkl_sys_write(fd, ns, sizeof(ns));
lkl_sys_close(fd);
}

return 0;
}

Expand Down
6 changes: 6 additions & 0 deletions tools/lkl/tests/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static const char *config_json =
"{\n"
" \"gateway\":\"192.168.113.1\",\n"
" \"gateway6\":\"fc03::1\",\n"
" \"nameserver\":\"2001:4860:4860::8888\",\n"
" \"debug\":\"1\",\n"
" \"interfaces\": [\n"
" {\n"
Expand Down Expand Up @@ -56,6 +57,11 @@ int lkl_test_config_load_json(void)
return TEST_FAILURE;
}

if (strcmp(cfg->nameserver, "2001:4860:4860::8888") != 0) {
lkl_test_logf("bad nameserver\n");
return TEST_FAILURE;
}

if (strcmp(cfg->debug, "1") != 0) {
lkl_test_logf("bad debug\n");
return TEST_FAILURE;
Expand Down

0 comments on commit 0ef1681

Please sign in to comment.