|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euxo pipefail |
| 4 | + |
| 5 | +# Determine active kernel version number. |
| 6 | +KERNEL_VERSION=`uname -r | grep -Eo '([0-9]+)*(\.?[0-9]+)*' | head -1` |
| 7 | + |
| 8 | +# URL for kernel sources of active kernel version. |
| 9 | +KERNEL_FILE_NAME_PREFIX=linux-msft-wsl- |
| 10 | +KERNEL_FILE_NAME_STEM=${KERNEL_FILE_NAME_PREFIX}${KERNEL_VERSION} |
| 11 | +KERNEL_FILE_NAME_SUFFIX=.tar.gz |
| 12 | +KERNEL_FILE_NAME=${KERNEL_FILE_NAME_STEM}${KERNEL_FILE_NAME_SUFFIX} |
| 13 | +KERNEL_URL_BASE=https://github.com/microsoft/WSL2-Linux-Kernel/archive/refs/tags |
| 14 | +KERNEL_URL_FILE=${KERNEL_URL_BASE}/${KERNEL_FILE_NAME} |
| 15 | +KERNEL_CONFIG_UTIL=scripts/config |
| 16 | + |
| 17 | +# WSL kernel source directory name. |
| 18 | +WSL_SRC_DIRECTORY_BASE_DEFAULT=${HOME}/src |
| 19 | +WSL_SRC_DIRECTORY_BASE=${WSL_SRC_DIRECTORY_BASE:=${WSL_SRC_DIRECTORY_BASE_DEFAULT}} |
| 20 | +WSL_SRC_DIRECTORY_PREFIX=WSL2-Linux-Kernel- |
| 21 | +WSL_SRC_DIRECTORY=${WSL_SRC_DIRECTORY_PREFIX}${KERNEL_FILE_NAME_STEM} |
| 22 | +WSL_SRC_CONFIG_FILE_NAME=config-${KERNEL_VERSION} |
| 23 | +WSL_SRC_CONFIG_DIRECTORY=Microsoft |
| 24 | +WSL_SRC_CONFIG=${WSL_SRC_CONFIG_DIRECTORY}/${WSL_SRC_CONFIG_FILE_NAME} |
| 25 | + |
| 26 | +# WSL kernel compilation arguments. |
| 27 | +WSL_KERNEL_COMPILE_ARG_PARALLEL="-j $(expr $(nproc) - 1)" |
| 28 | + |
| 29 | +# Additional arguments passed to wget when downloading kernel source. |
| 30 | +# Mostly used for testing. |
| 31 | +WGET_XTRA_ARGS= |
| 32 | + |
| 33 | +# Print debug information if DEBUG is set to 1. |
| 34 | +if [[ $DEBUG -eq 1 ]]; then |
| 35 | + echo "DEBUG: KERNEL_VERSION=${KERNEL_VERSION}" |
| 36 | + echo "DEBUG: KERNEL_FILE_NAME=${KERNEL_FILE_NAME}" |
| 37 | + echo "DEBUG: KERNEL_URL_FILE=${KERNEL_URL_FILE}" |
| 38 | + echo "DEBUG: WSL_SRC_DIRECTORY=${WSL_SRC_DIRECTORY}" |
| 39 | + echo "DEBUG: WSL_SRC_CONFIG_FILE_NAME=${WSL_SRC_CONFIG_FILE_NAME}" |
| 40 | + echo "DEBUG: WSL_SRC_CONFIG=${WSL_SRC_CONFIG}" |
| 41 | +fi |
| 42 | + |
| 43 | +# Install dependencies for building kernel modules. |
| 44 | +# (see https://github.com/microsoft/WSL2-Linux-Kernel) |
| 45 | +# added bc, python-is-python3 |
| 46 | +echo "Installing dependencies for building kernel modules..." |
| 47 | +sudo apt-get update |
| 48 | +sudo apt-get install -y build-essential bc bison flex dwarves kmod libssl-dev libelf-dev python-is-python3 wget |
| 49 | + |
| 50 | +# Download and unpack kernel source. |
| 51 | +echo "Downloading and unpacking kernel source..." |
| 52 | +if [[ ! -d ${WSL_SRC_DIRECTORY_BASE} ]]; then |
| 53 | + mkdir -p ${WSL_SRC_DIRECTORY_BASE} |
| 54 | +fi |
| 55 | + |
| 56 | +cd ${WSL_SRC_DIRECTORY_BASE} |
| 57 | +wget ${KERNEL_URL_FILE} ${WGET_XTRA_ARGS:+"${WGET_XTRA_ARGS}"} -O - | tar xzvf - |
| 58 | +cd ${WSL_SRC_DIRECTORY} |
| 59 | + |
| 60 | +# Prepare the kernel source with the configuration for the running kernel. |
| 61 | +echo "Preparing kernel source with configuration for running kernel..." |
| 62 | +if [[ ! -f ${WSL_SRC_CONFIG} ]]; then |
| 63 | + cat /proc/config.gz | gzip -d > ${WSL_SRC_CONFIG} |
| 64 | +fi |
| 65 | + |
| 66 | +if [[ ! -f .config ]]; then |
| 67 | + ln -s ${WSL_SRC_CONFIG} .config |
| 68 | +fi |
| 69 | + |
| 70 | +# Supply defaults for any new/unspecified options in the configuration. |
| 71 | +make olddefconfig |
| 72 | + |
| 73 | +# Prepare the source tree for building external modules. |
| 74 | +echo "Preparing kernel source tree for building external modules..." |
| 75 | +make prepare modules_prepare ${WSL_KERNEL_COMPILE_ARG_PARALLEL} |
| 76 | + |
| 77 | +# Update the configuration to build the mac80211_hwsim module and its dependencies. |
| 78 | +echo "Updating kernel configuration to build mac80211_hwsim module and its dependencies..." |
| 79 | +${KERNEL_CONFIG_UTIL} \ |
| 80 | + --module CONFIG_RFKILL \ |
| 81 | + --module CONFIG_CFG80211 \ |
| 82 | + --module CONFIG_MAC80211 \ |
| 83 | + --module CONFIG_MAC80211_HWSIM |
| 84 | + |
| 85 | +echo "Building kernel modules..." |
| 86 | +make modules ${WSL_KERNEL_COMPILE_ARG_PARALLEL} |
| 87 | + |
| 88 | +echo "Installing kernel modules..." |
| 89 | +sudo make modules_install |
| 90 | + |
| 91 | +echo "Done." |
0 commit comments