|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2026 Flant JSC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# Build usbip-core, usbip-host, vhci-hcd for the running kernel and put .ko into a tmp dir. |
| 17 | +# Needs: kernel headers (e.g. linux-headers-$(uname -r)), make, gcc. No full kernel source. |
| 18 | +# |
| 19 | +# Usage: |
| 20 | +# ./build-usbip-modules.sh [OUTPUT_DIR] |
| 21 | +# OUTPUT_DIR defaults to /tmp/usbip-modules-$(uname -r) |
| 22 | +# USBIP_SRC defaults to the directory where this script lives (driver source). |
| 23 | +# |
| 24 | +# Env: |
| 25 | +# KVER - kernel version to build for (default: uname -r) |
| 26 | +# USBIP_SRC - path to driver source (must contain .c and Makefile.standalone) |
| 27 | +# OUTPUT_DIR - output directory (overrides optional argument) |
| 28 | +# |
| 29 | +# Minimal deps: bash, make, gcc, kernel headers package (e.g. linux-headers-${KVER}). |
| 30 | + |
| 31 | +set -e |
| 32 | + |
| 33 | +KVER="${KVER:-$(uname -r)}" |
| 34 | +for base in /lib/modules /usr/lib/modules; do |
| 35 | + [[ -d "${base}/${KVER}/build" || -L "${base}/${KVER}/build" ]] || continue |
| 36 | + KBUILD="${base}/${KVER}/build" |
| 37 | + break |
| 38 | +done |
| 39 | +KBUILD="${KBUILD:-/lib/modules/${KVER}/build}" |
| 40 | +USBIP_SRC="${USBIP_SRC:-$(cd "$(dirname "$0")" && pwd)}" |
| 41 | +OUTPUT_DIR="${OUTPUT_DIR:-${1:-/tmp/usbip-modules-${KVER}}}" |
| 42 | + |
| 43 | +if [[ ! -d "$KBUILD" && ! -L "$KBUILD" ]]; then |
| 44 | + echo "build-usbip-modules: kernel build dir not found for ${KVER}" >&2 |
| 45 | + echo "Install kernel headers (kernel-devel or linux-headers-${KVER}) on the host." >&2 |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +# Resolve symlink so make sees the real path (must be visible in container) |
| 50 | +if [[ -L "$KBUILD" ]]; then |
| 51 | + KBUILD=$(readlink -f "$KBUILD") |
| 52 | +fi |
| 53 | +if [[ ! -d "$KBUILD" ]]; then |
| 54 | + echo "build-usbip-modules: build is a symlink but its target is not visible in the container." >&2 |
| 55 | + echo " resolved path: $KBUILD" >&2 |
| 56 | + echo "Mount the kernel build tree from the host, e.g.:" >&2 |
| 57 | + echo " -v /usr/src/kernels:/usr/src/kernels:ro" >&2 |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +if [[ ! -f "$USBIP_SRC/Makefile" ]]; then |
| 62 | + echo "build-usbip-modules: $USBIP_SRC/Makefile not found" >&2 |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | + |
| 66 | +make -C "$KBUILD" M="$USBIP_SRC" CC="${CC:-gcc}" modules |
| 67 | + |
| 68 | +mkdir -p "$OUTPUT_DIR" |
| 69 | +cp -f "$USBIP_SRC"/usbip-core.ko "$USBIP_SRC"/usbip-host.ko "$USBIP_SRC"/vhci-hcd.ko "$OUTPUT_DIR/" |
| 70 | + |
| 71 | +echo "Built for ${KVER}; modules in: ${OUTPUT_DIR}" |
| 72 | +ls -la "$OUTPUT_DIR"/*.ko |
0 commit comments