Include /etc/hosts and /etc/resolv.conf in initramfs

This commit is contained in:
jiangjianfeng 2025-10-21 03:14:20 +00:00 committed by Tate, Hongliang Tian
parent 0126d9b356
commit 77ef9e0368
6 changed files with 53 additions and 3 deletions

View File

@ -43,6 +43,8 @@ SYSCALL_TEST_WORKDIR ?= /tmp
# NETDEV possible values are user,tap
NETDEV ?= user
VHOST ?= off
# The name server listed by /etc/resolv.conf inside the Asterinas VM
DNS_SERVER ?= none
# End of network settings
# ========================= End of Makefile options. ==========================

View File

@ -6,6 +6,7 @@ VERBOSE ?= 1
SYSCALL_TEST_SUITE ?= ltp
SYSCALL_TEST_WORKDIR ?= /tmp
ENABLE_BASIC_TEST ?= false
DNS_SERVER ?= none
# Set Nix's cached tarballs to be live for a longer period of time (30 days) to avoid network traffics.
# Nix's default value is rather small (1 hour or 3600 seconds).
NIXPKGS_CACHE_TTL := 2592000 # In seconds
@ -65,6 +66,7 @@ $(INITRAMFS_IMAGE): $(INITRAMFS)
--arg enableSyscallTest $(ENABLE_SYSCALL_TEST) \
--argstr syscallTestSuite $(SYSCALL_TEST_SUITE) \
--argstr syscallTestWorkDir $(SYSCALL_TEST_WORKDIR) \
--argstr dnsServer ${DNS_SERVER} \
--arg initramfsCompressed $(INITRAMFS_COMPRESSED) \
--arg smp $(SMP) \
--out-link $@ \
@ -80,6 +82,7 @@ $(INITRAMFS):
--arg enableSyscallTest $(ENABLE_SYSCALL_TEST) \
--argstr syscallTestSuite $(SYSCALL_TEST_SUITE) \
--argstr syscallTestWorkDir $(SYSCALL_TEST_WORKDIR) \
--argstr dnsServer ${DNS_SERVER} \
--arg smp $(SMP) \
--out-link $@ \
nix -A initramfs

View File

@ -1,6 +1,7 @@
{ target ? "x86_64", enableBasicTest ? false, enableBenchmark ? false
, enableSyscallTest ? false, syscallTestSuite ? "ltp"
, syscallTestWorkDir ? "/tmp", smp ? 1, initramfsCompressed ? true, }:
, syscallTestWorkDir ? "/tmp", dnsServer ? "none", smp ? 1
, initramfsCompressed ? true, }:
let
crossSystem.config = if target == "x86_64" then
"x86_64-unknown-linux-gnu"
@ -35,6 +36,7 @@ in rec {
apps = if enableBasicTest then apps else null;
benchmark = if enableBenchmark then benchmark else null;
syscall = if enableSyscallTest then syscall else null;
dnsServer = dnsServer;
};
initramfs-image = pkgs.callPackage ./initramfs-image.nix {
inherit initramfs;

View File

@ -1,5 +1,5 @@
{ lib, stdenvNoCC, fetchFromGitHub, hostPlatform, writeClosure, busybox, apps
, benchmark, syscall, }:
, benchmark, syscall, dnsServer, pkgs }:
let
etc = lib.fileset.toSource {
root = ./../src/etc;
@ -9,7 +9,9 @@ let
name = "gvisor-libs";
path = "/lib/x86_64-linux-gnu";
};
all_pkgs = [ busybox etc ] ++ lib.optionals (apps != null) [ apps.package ]
resolv_conf = pkgs.callPackage ./resolv_conf.nix { dnsServer = dnsServer; };
all_pkgs = [ busybox etc resolv_conf ]
++ lib.optionals (apps != null) [ apps.package ]
++ lib.optionals (benchmark != null) [ benchmark.package ]
++ lib.optionals (syscall != null) [ syscall.package ];
in stdenvNoCC.mkDerivation {
@ -26,6 +28,8 @@ in stdenvNoCC.mkDerivation {
cp -r ${etc}/* $out/etc/
cp ${resolv_conf}/resolv.conf $out/etc/
${lib.optionalString (apps != null) ''
cp -r ${apps.package}/* $out/test/
''}

38
test/nix/resolv_conf.nix Normal file
View File

@ -0,0 +1,38 @@
{ stdenv, dnsServer }:
let
host_resolv_conf = builtins.path {
name = "host-resolv-conf";
path = "/etc/resolv.conf";
};
in stdenv.mkDerivation {
name = "resolv-conf";
buildCommand = ''
RESOLV_CONF_FILE="$out/resolv.conf"
mkdir -p $out
is_host_resolve_conf_valid() {
if [ ! -f "${host_resolv_conf}" ]; then
return 1
fi
if grep -qE "nameserver\s+127\.0\.0\." "${host_resolv_conf}"; then
return 1
else
return 0
fi
}
if [ -n "${dnsServer}" ] && [ "${dnsServer}" != "none" ]; then
echo "nameserver ${dnsServer}" > "$RESOLV_CONF_FILE"
elif is_host_resolve_conf_valid; then
cp ${host_resolv_conf} $RESOLV_CONF_FILE
echo "resolv.conf is generated from the host's /etc/resolv.conf"
else
echo "Warning: the host's /etc/resolv.conf is not valid for the guest VM (containing lookback addresses)." >&2
echo "Fall back to Cloudflare's public DNS servers (1.1.1.1)." >&2
echo "Consider using the DNS_SERVER Makefile variable to specify DNS server explicitly." >&2
echo "For example: make DNS_SERVER=\"192.168.1.1\"" >&2
echo "nameserver 1.1.1.1" > "$RESOLV_CONF_FILE"
fi
'';
}

1
test/src/etc/hosts Normal file
View File

@ -0,0 +1 @@
127.0.0.1 localhost