From 802304d66d58d3b28638ac1f9ea390d0c8d1c2c0 Mon Sep 17 00:00:00 2001 From: Andrew Beltrano Date: Mon, 27 Nov 2023 15:55:28 -0700 Subject: [PATCH 1/6] Add script to build mac80211_hwsim for wsl2. --- .../build-mac80211_hwsim-kmod.sh | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 .docker/netremote-dev/build-mac80211_hwsim-kmod.sh diff --git a/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh b/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh new file mode 100644 index 00000000..ca17d288 --- /dev/null +++ b/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +DEBUG=1 + +# Determine active kernel version number. +KERNEL_VERSION=`uname -r | grep -Eo '([0-9]+)*(\.?[0-9]+)*' | head -1` + +# URL for kernel sources of active kernel version. +KERNEL_FILE_NAME_PREFIX=linux-msft-wsl- +KERNEL_FILE_NAME_STEM=${KERNEL_FILE_NAME_PREFIX}${KERNEL_VERSION} +KERNEL_FILE_NAME_SUFFIX=.tar.gz +KERNEL_FILE_NAME=${KERNEL_FILE_NAME_STEM}${KERNEL_FILE_NAME_SUFFIX} +KERNEL_URL_BASE=https://github.com/microsoft/WSL2-Linux-Kernel/archive/refs/tags +KERNEL_URL_FILE=${KERNEL_URL_BASE}/${KERNEL_FILE_NAME} +KERNEL_CONFIG_UTIL=scripts/config + +# WSL kernel source directory name. +WSL_SRC_DIRECTORY_BASE=${WSL_SRC_DIRECTORY_BASE:=$(mktemp -d)} +WSL_SRC_DIRECTORY_PREFIX=WSL2-Linux-Kernel- +WSL_SRC_DIRECTORY=${WSL_SRC_DIRECTORY_PREFIX}${KERNEL_FILE_NAME_STEM} +WSL_SRC_CONFIG_FILE_NAME=config-${KERNEL_VERSION} +WSL_SRC_CONFIG_DIRECTORY=Microsoft +WSL_SRC_CONFIG=${WSL_SRC_CONFIG_DIRECTORY}/${WSL_SRC_CONFIG_FILE_NAME} + +# WSL kernel compilation arguments. +WSL_KERNEL_COMPILE_ARG_PARALLEL=-j $(expr $(nproc) - 1) + +# Additional arguments passed to wget when downloading kernel source. +# Mostly used for testing. +WGET_XTRA_ARGS="--spider" + +if [[ $DEBUG -eq 1 ]]; then + echo "DEBUG: KERNEL_VERSION=${KERNEL_VERSION}" + echo "DEBUG: KERNEL_FILE_NAME=${KERNEL_FILE_NAME}" + echo "DEBUG: KERNEL_URL_FILE=${KERNEL_URL_FILE}" + echo "DEBUG: WSL_SRC_DIRECTORY=${WSL_SRC_DIRECTORY}" + echo "DEBUG: WSL_SRC_CONFIG_FILE_NAME=${WSL_SRC_CONFIG_FILE_NAME}" + echo "DEBUG: WSL_SRC_CONFIG=${WSL_SRC_CONFIG}" +fi + +# Install dependencies for building kernel modules. +# (see https://github.com/microsoft/WSL2-Linux-Kernel) +# added bc, python-is-python3 +echo "Installing dependencies for building kernel modules..." +sudo apt-get update +sudo apt-get install -y build-essential bc bison flex dwarves kmod libssl-dev libelf-dev python-is-python3 wget + +# Download and unpack kernel source. +echo "Downloading and unpacking kernel source..." +cd ${WSL_SRC_DIRECTORY_BASE:=${TMP}} +wget ${KERNEL_URL_FILE} ${WGET_XTRA_ARGS:+"${WGET_XTRA_ARGS}"} -O - | tar xzvf - +cd ${WSL_SRC_DIRECTORY} + +# Prepare the kernel source with the configuration for the running kernel. +echo "Preparing kernel source with configuration for running kernel..." +if [[ ! -f ${WSL_SRC_CONFIG} ]]; then + cat /proc/config.gz | gzip -d > ${WSL_SRC_CONFIG} +fi + +if [[ ! -f .config ]]; then + ln -s ${WSL_SRC_CONFIG} .config +fi + +# Supply defaults for any new/unspecified options in the configuration. +make olddefconfig + +# Prepare the source tree for building external modules. +echo "Preparing kernel source tree for building external modules..." +make prepare modules_prepare ${WSL_KERNEL_COMPILE_ARG_PARALLEL} + +# Update the configuration to build the mac80211_hwsim module and its dependencies. +echo "Updating kernel configuration to build mac80211_hwsim module and its dependencies..." +${KERNEL_CONFIG_UTIL} \ + --module CONFIG_RFKILL \ + --module CONFIG_CFG80211 \ + --module CONFIG_MAC80211 \ + --module CONFIG_MAC80211_HWSIM + +echo "Building kernel modules..." +make modules ${WSL_KERNEL_COMPILE_ARG_PARALLEL} + +echo "Installing kernel modules..." +sudo make modules_install + +echo "Loading new kernel modules..." +sudo depmod -a + +echo "Done." From 5a289ab6b411eb984d290ecb78dcdd406ecd4f57 Mon Sep 17 00:00:00 2001 From: Andrew Beltrano Date: Mon, 27 Nov 2023 16:06:47 -0700 Subject: [PATCH 2/6] Add hwsim module build to Dockerfile image. --- .docker/netremote-dev/Dockerfile | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/.docker/netremote-dev/Dockerfile b/.docker/netremote-dev/Dockerfile index 607e423b..b201ee41 100644 --- a/.docker/netremote-dev/Dockerfile +++ b/.docker/netremote-dev/Dockerfile @@ -52,7 +52,7 @@ RUN apt-get install -qq -y --no-install-recommends \ RUN apt-get clean && \ rm -rf /var/lib/apt/lists/* -FROM netremote as netremote-dev +FROM netremote as netremote-dev-base # Install packages. RUN apt-get update && \ @@ -107,6 +107,17 @@ RUN chmod 0755 /etc/update-motd.d/50-sysinfo && \ # Permission for entrypoint script chmod +x /bin/entrypoint.sh +FROM netremote-dev-base as netremote-build-mac80211hwsim + +COPY build-mac80211_hwsim-kmod.sh /tmp/build-mac80211_hwsim-kmod.sh +RUN chmod +x /tmp/build-mac80211_hwsim-kmod.sh && \ + /tmp/build-mac80211_hwsim-kmod.sh + +FROM netremote-dev-base as netremote-dev + +# Copy built-modules from build stage, discarding the source tree. +COPY --from=netremote-build-mac80211hwsim --chmod=755 /lib/modules/$(uname -r)/ /lib/modules/$(uname -r)/ + # Set entrypoint and start interactive shell (bash). ENTRYPOINT [ "/bin/entrypoint.sh" ] CMD /bin/bash -i From 7a1c4b6748bb7b17add5cb14b7ec7386e9acd87f Mon Sep 17 00:00:00 2001 From: Andrew Beltrano Date: Mon, 27 Nov 2023 16:29:47 -0700 Subject: [PATCH 3/6] Fix parallel arg expression. --- .docker/netremote-dev/build-mac80211_hwsim-kmod.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh b/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh index ca17d288..9e74c056 100644 --- a/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh +++ b/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -euxo pipefail + DEBUG=1 # Determine active kernel version number. @@ -23,11 +25,11 @@ WSL_SRC_CONFIG_DIRECTORY=Microsoft WSL_SRC_CONFIG=${WSL_SRC_CONFIG_DIRECTORY}/${WSL_SRC_CONFIG_FILE_NAME} # WSL kernel compilation arguments. -WSL_KERNEL_COMPILE_ARG_PARALLEL=-j $(expr $(nproc) - 1) +WSL_KERNEL_COMPILE_ARG_PARALLEL="-j $(expr $(nproc) - 1)" # Additional arguments passed to wget when downloading kernel source. # Mostly used for testing. -WGET_XTRA_ARGS="--spider" +WGET_XTRA_ARGS= if [[ $DEBUG -eq 1 ]]; then echo "DEBUG: KERNEL_VERSION=${KERNEL_VERSION}" From 72c311b8f7ac1d9ced439e9c8dd80c0b72a4db0f Mon Sep 17 00:00:00 2001 From: Andrew Beltrano Date: Mon, 27 Nov 2023 16:42:30 -0700 Subject: [PATCH 4/6] Add kernel module package 'kmod'. --- .docker/netremote-dev/Dockerfile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.docker/netremote-dev/Dockerfile b/.docker/netremote-dev/Dockerfile index b201ee41..138a8519 100644 --- a/.docker/netremote-dev/Dockerfile +++ b/.docker/netremote-dev/Dockerfile @@ -85,9 +85,10 @@ RUN apt-get update && \ ssh \ sudo \ # Generic development tools. - # emacs gdb nano policycoreutils-python-utils python-is-python3 vim + # emacs gdb kmod nano policycoreutils-python-utils python-is-python3 vim emacs \ gdb \ + kmod \ nano \ policycoreutils-python-utils \ python-is-python3 \ @@ -109,14 +110,13 @@ RUN chmod 0755 /etc/update-motd.d/50-sysinfo && \ FROM netremote-dev-base as netremote-build-mac80211hwsim -COPY build-mac80211_hwsim-kmod.sh /tmp/build-mac80211_hwsim-kmod.sh -RUN chmod +x /tmp/build-mac80211_hwsim-kmod.sh && \ - /tmp/build-mac80211_hwsim-kmod.sh +COPY --chmod=755 build-mac80211_hwsim-kmod.sh /tmp/build-mac80211_hwsim-kmod.sh +RUN /tmp/build-mac80211_hwsim-kmod.sh FROM netremote-dev-base as netremote-dev # Copy built-modules from build stage, discarding the source tree. -COPY --from=netremote-build-mac80211hwsim --chmod=755 /lib/modules/$(uname -r)/ /lib/modules/$(uname -r)/ +COPY --from=netremote-build-mac80211hwsim --chmod=755 /lib/modules/ /lib/modules/ # Set entrypoint and start interactive shell (bash). ENTRYPOINT [ "/bin/entrypoint.sh" ] From c127417d0522971b0208382670b5a2e7f6f69d22 Mon Sep 17 00:00:00 2001 From: Andrew Beltrano Date: Mon, 27 Nov 2023 17:24:38 -0700 Subject: [PATCH 5/6] Include kern source in image. --- .docker/netremote-dev/Dockerfile | 24 +++++++------------ .../build-mac80211_hwsim-kmod.sh | 9 +++++-- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/.docker/netremote-dev/Dockerfile b/.docker/netremote-dev/Dockerfile index 138a8519..94f8c9ec 100644 --- a/.docker/netremote-dev/Dockerfile +++ b/.docker/netremote-dev/Dockerfile @@ -52,7 +52,7 @@ RUN apt-get install -qq -y --no-install-recommends \ RUN apt-get clean && \ rm -rf /var/lib/apt/lists/* -FROM netremote as netremote-dev-base +FROM netremote as netremote-dev # Install packages. RUN apt-get update && \ @@ -99,24 +99,16 @@ RUN apt-get update && \ rm -rf /var/lib/apt/lists/* # Copy outside content into container. -COPY entrypoint.sh /bin/entrypoint.sh -COPY 50-sysinfo /etc/update-motd.d/50-sysinfo +COPY --chmod=0755 entrypoint.sh /bin/entrypoint.sh +COPY --chmod=0755 50-sysinfo /etc/update-motd.d/50-sysinfo +COPY --chmod=0755 build-mac80211_hwsim-kmod.sh /tmp/build-mac80211_hwsim-kmod.sh # Message of the day settings. -RUN chmod 0755 /etc/update-motd.d/50-sysinfo && \ - rm -f /etc/update-motd.d/50-landscape-sysinfo && \ - # Permission for entrypoint script - chmod +x /bin/entrypoint.sh +RUN rm -f /etc/update-motd.d/50-landscape-sysinfo -FROM netremote-dev-base as netremote-build-mac80211hwsim - -COPY --chmod=755 build-mac80211_hwsim-kmod.sh /tmp/build-mac80211_hwsim-kmod.sh -RUN /tmp/build-mac80211_hwsim-kmod.sh - -FROM netremote-dev-base as netremote-dev - -# Copy built-modules from build stage, discarding the source tree. -COPY --from=netremote-build-mac80211hwsim --chmod=755 /lib/modules/ /lib/modules/ +# Build the mac80211_hwsim kernel module. +RUN /tmp/build-mac80211_hwsim-kmod.sh && \ + mv /tmp/build-mac80211_hwsim-kmod.sh ${HOME}/ # Set entrypoint and start interactive shell (bash). ENTRYPOINT [ "/bin/entrypoint.sh" ] diff --git a/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh b/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh index 9e74c056..29fef7fa 100644 --- a/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh +++ b/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh @@ -17,7 +17,8 @@ KERNEL_URL_FILE=${KERNEL_URL_BASE}/${KERNEL_FILE_NAME} KERNEL_CONFIG_UTIL=scripts/config # WSL kernel source directory name. -WSL_SRC_DIRECTORY_BASE=${WSL_SRC_DIRECTORY_BASE:=$(mktemp -d)} +WSL_SRC_DIRECTORY_BASE_DEFAULT=${HOME}/src +WSL_SRC_DIRECTORY_BASE=${WSL_SRC_DIRECTORY_BASE:=${WSL_SRC_DIRECTORY_BASE_DEFAULT}} WSL_SRC_DIRECTORY_PREFIX=WSL2-Linux-Kernel- WSL_SRC_DIRECTORY=${WSL_SRC_DIRECTORY_PREFIX}${KERNEL_FILE_NAME_STEM} WSL_SRC_CONFIG_FILE_NAME=config-${KERNEL_VERSION} @@ -49,7 +50,11 @@ sudo apt-get install -y build-essential bc bison flex dwarves kmod libssl-dev li # Download and unpack kernel source. echo "Downloading and unpacking kernel source..." -cd ${WSL_SRC_DIRECTORY_BASE:=${TMP}} +if [[ ! -d ${WSL_SRC_DIRECTORY_BASE} ]]; then + mkdir -p ${WSL_SRC_DIRECTORY_BASE} +fi + +cd ${WSL_SRC_DIRECTORY_BASE} wget ${KERNEL_URL_FILE} ${WGET_XTRA_ARGS:+"${WGET_XTRA_ARGS}"} -O - | tar xzvf - cd ${WSL_SRC_DIRECTORY} From c2d245a5a80166515b15345cf9477c4c6db07da9 Mon Sep 17 00:00:00 2001 From: Andrew Beltrano Date: Mon, 27 Nov 2023 17:26:58 -0700 Subject: [PATCH 6/6] Remove debug setting from script. --- .docker/netremote-dev/build-mac80211_hwsim-kmod.sh | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh b/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh index 29fef7fa..09cd5fd1 100644 --- a/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh +++ b/.docker/netremote-dev/build-mac80211_hwsim-kmod.sh @@ -2,8 +2,6 @@ set -euxo pipefail -DEBUG=1 - # Determine active kernel version number. KERNEL_VERSION=`uname -r | grep -Eo '([0-9]+)*(\.?[0-9]+)*' | head -1` @@ -32,6 +30,7 @@ WSL_KERNEL_COMPILE_ARG_PARALLEL="-j $(expr $(nproc) - 1)" # Mostly used for testing. WGET_XTRA_ARGS= +# Print debug information if DEBUG is set to 1. if [[ $DEBUG -eq 1 ]]; then echo "DEBUG: KERNEL_VERSION=${KERNEL_VERSION}" echo "DEBUG: KERNEL_FILE_NAME=${KERNEL_FILE_NAME}" @@ -89,7 +88,4 @@ make modules ${WSL_KERNEL_COMPILE_ARG_PARALLEL} echo "Installing kernel modules..." sudo make modules_install -echo "Loading new kernel modules..." -sudo depmod -a - echo "Done."