diff --git a/Makefile b/Makefile index 37738588f..c442562eb 100644 --- a/Makefile +++ b/Makefile @@ -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. ========================== diff --git a/test/Makefile b/test/Makefile index 5c880e5c5..e5815c348 100644 --- a/test/Makefile +++ b/test/Makefile @@ -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 diff --git a/test/nix/default.nix b/test/nix/default.nix index 74541d271..6f0543495 100644 --- a/test/nix/default.nix +++ b/test/nix/default.nix @@ -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; diff --git a/test/nix/initramfs.nix b/test/nix/initramfs.nix index 286dcdf3c..d943bf628 100644 --- a/test/nix/initramfs.nix +++ b/test/nix/initramfs.nix @@ -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/ ''} diff --git a/test/nix/resolv_conf.nix b/test/nix/resolv_conf.nix new file mode 100644 index 000000000..246aee7b4 --- /dev/null +++ b/test/nix/resolv_conf.nix @@ -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 + ''; +} diff --git a/test/src/etc/hosts b/test/src/etc/hosts new file mode 100644 index 000000000..880ccd9b2 --- /dev/null +++ b/test/src/etc/hosts @@ -0,0 +1 @@ +127.0.0.1 localhost \ No newline at end of file