Skip to content

Commit 0ef1681

Browse files
authored
Merge pull request torvalds#519 from thehajime/feature-nameserver
lkl: add nameserver config for json
2 parents 2f7f422 + cfeefae commit 0ef1681

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

Documentation/lkl.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,17 @@ The following are the list of keys to describe a JSON file.
449449
"delay_main":"500000"
450450
```
451451

452+
* nameserver
453+
454+
key: "nameserver"
455+
value type: string
456+
457+
a name server address, which will be written in /etc/resolv.conf into a
458+
filesystem used by a LKL instance.
459+
```
460+
"nameserver":"8.8.8.8"
461+
```
462+
452463
FAQ
453464
===
454465

tools/lkl/include/lkl_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct lkl_config {
5050
char *boot_cmdline;
5151
char *dump;
5252
char *delay_main;
53+
char *nameserver;
5354
};
5455

5556
#ifdef LKL_HOST_CONFIG_JSMN

tools/lkl/lib/config.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ int lkl_load_config_json(struct lkl_config *cfg, const char *jstr)
178178
cfgptr = &cfg->dump;
179179
} else if (jsoneq(jstr, &toks[pos], "delay_main") == 0) {
180180
cfgptr = &cfg->delay_main;
181+
} else if (jsoneq(jstr, &toks[pos], "nameserver") == 0) {
182+
cfgptr = &cfg->nameserver;
181183
} else {
182184
lkl_printf("unexpected key in json %.*s\n",
183185
toks[pos].end-toks[pos].start,
@@ -218,6 +220,7 @@ void lkl_show_config(struct lkl_config *cfg)
218220
return;
219221
lkl_printf("gateway: %s\n", cfg->gateway);
220222
lkl_printf("gateway6: %s\n", cfg->gateway6);
223+
lkl_printf("nameserver: %s\n", cfg->nameserver);
221224
lkl_printf("debug: %s\n", cfg->debug);
222225
lkl_printf("mount: %s\n", cfg->mount);
223226
lkl_printf("singlecpu: %s\n", cfg->single_cpu);
@@ -689,6 +692,7 @@ static int lkl_clean_config(struct lkl_config *cfg)
689692
free_cfgparam(cfg->boot_cmdline);
690693
free_cfgparam(cfg->dump);
691694
free_cfgparam(cfg->delay_main);
695+
free_cfgparam(cfg->nameserver);
692696
return 0;
693697
}
694698

@@ -777,6 +781,20 @@ int lkl_load_config_post(struct lkl_config *cfg)
777781
}
778782
}
779783

784+
if (cfg->nameserver) {
785+
int fd;
786+
char ns[32] = "nameserver ";
787+
788+
/* ignore error */
789+
lkl_sys_mkdir("/etc", 0xff);
790+
lkl_sys_chdir("/etc");
791+
fd = lkl_sys_open("/etc/resolv.conf", LKL_O_CREAT | LKL_O_RDWR, 0);
792+
793+
strcat(ns, cfg->nameserver);
794+
lkl_sys_write(fd, ns, sizeof(ns));
795+
lkl_sys_close(fd);
796+
}
797+
780798
return 0;
781799
}
782800

tools/lkl/tests/config.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ static const char *config_json =
1111
"{\n"
1212
" \"gateway\":\"192.168.113.1\",\n"
1313
" \"gateway6\":\"fc03::1\",\n"
14+
" \"nameserver\":\"2001:4860:4860::8888\",\n"
1415
" \"debug\":\"1\",\n"
1516
" \"interfaces\": [\n"
1617
" {\n"
@@ -56,6 +57,11 @@ int lkl_test_config_load_json(void)
5657
return TEST_FAILURE;
5758
}
5859

60+
if (strcmp(cfg->nameserver, "2001:4860:4860::8888") != 0) {
61+
lkl_test_logf("bad nameserver\n");
62+
return TEST_FAILURE;
63+
}
64+
5965
if (strcmp(cfg->debug, "1") != 0) {
6066
lkl_test_logf("bad debug\n");
6167
return TEST_FAILURE;

0 commit comments

Comments
 (0)