本文共 2106 字,大约阅读时间需要 7 分钟。
#!/bin/sh -e# shellcheck disable=SC1091. ../../lib/sh-test-libOUTPUT="$(pwd)/output"RESULT_FILE="${OUTPUT}/result.txt"export RESULT_FILELOGFILE="${OUTPUT}/kernel-compilation.txt"VERSION='4.4.34'NPROC=$(nproc)#定义一个函数说明这个脚本该如何使用usage() { echo "Usage: $0 [-v version] [-s true|false]" 1>&2 exit 1}#parse 可选参数while getopts "v:s:h" o; do case "$o" in v) VERSION="${OPTARG}" ;; s) SKIP_INSTALL="${OPTARG}" ;; h|*) usage ;; esacdone#执行函数得到发型板的名字dist_name# shellcheck disable=SC2154#目前只支持两类kernelcase "${dist}" in debian|ubuntu) pkgs="wget time bc xz-utils build-essential" ;; centos|fedora) pkgs="wget time bc xz gcc make" ;;esac#检查是否root 用户。非root用户的话,说出错误日志! check_root && error_msg "You need to be root to install packages!"# install_deps supports the above distributions.# It will skip package installation on other distributions by default.#安装必须的包install_deps "${pkgs}" "${SKIP_INSTALL}"create_out_dir "${OUTPUT}"cd "${OUTPUT}"# Download and extract Kernel tarball.#从kernel 官网下载用户指定版本的kernelmajor_version=$(echo "${VERSION}" | awk -F'.' '{print $1}')wget "https://cdn.kernel.org/pub/linux/kernel/v${major_version}.x/linux-${VERSION}.tar.xz"#解压tar 包,并cd到其目录下面tar xf "linux-${VERSION}.tar.xz"cd "linux-${VERSION}"# Compile Kernel with defconfig.# It is native not cross compiling.# It will not work on x86.#检查当前平台是arm64 还是x86detect_abi# shellcheck disable=SC2154case "${abi}" in arm64|armeabi) #生成.config make defconfig #开始build kernel.前面加time 表示会统计build kernel 用了多久的时间 { time -p make -j"${NPROC}" Image; } 2>&1 | tee "${LOGFILE}" ;; *) error_msg "Unsupported architecture!" ;;esac#从time -p输出的log中得到real的时间measurement="$(grep "^real" "${LOGFILE}" | awk '{print $2}')"#在特定目录下查找Image,如果可以查到说明kernel 编译通过if egrep "arch/.*/boot/Image" "${LOGFILE}"; then report_pass "kernel-compilation" add_metric "kernel-compilation-time" "pass" "${measurement}" "seconds"else report_fail "kernel-compilation" add_metric "kernel-compilation-time" "fail" "${measurement}" "seconds"fi# Cleanup.#删除kernel 目录cd ../rm -rf "linux-${VERSION}"*
转载地址:http://uknmi.baihongyu.com/