|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
3 |
| -set -x |
| 3 | +set -ex |
4 | 4 |
|
5 | 5 | LSB_FILE="/etc/lsb-release.host"
|
6 | 6 | OS_RELEASE_FILE="/etc/os-release.host"
|
7 | 7 | TARGET_DIR="/usr/src"
|
8 | 8 | HOST_MODULES_DIR="/lib/modules.host"
|
9 | 9 |
|
| 10 | +KERNEL_VERSION="${KERNEL_VERSION:-$(uname -r)}" |
| 11 | + |
10 | 12 | generate_headers()
|
11 | 13 | {
|
12 | 14 | echo "Generating kernel headers"
|
13 |
| - cd ${BUILD_DIR} |
| 15 | + |
| 16 | + cd "${BUILD_DIR}" |
14 | 17 | zcat /proc/config.gz > .config
|
15 | 18 | make ARCH=x86 oldconfig > /dev/null
|
16 | 19 | make ARCH=x86 prepare > /dev/null
|
17 | 20 |
|
18 | 21 | # Clean up abundant non-header files to speed-up copying
|
19 |
| - find ${BUILD_DIR} -regex '.*\.c\|.*\.txt\|.*Makefile\|.*Build\|.*Kconfig' -type f -delete |
| 22 | + find "${BUILD_DIR}" -regex '.*\.c\|.*\.txt\|.*Makefile\|.*Build\|.*Kconfig' -type f -delete |
20 | 23 | }
|
21 | 24 |
|
22 | 25 | fetch_cos_linux_sources()
|
23 | 26 | {
|
24 | 27 | echo "Fetching upstream kernel sources."
|
25 |
| - mkdir -p ${BUILD_DIR} |
26 |
| - curl -s "https://storage.googleapis.com/cos-tools/${BUILD_ID}/kernel-src.tar.gz" | tar -xzf - -C ${BUILD_DIR} |
| 28 | + mkdir -p "${BUILD_DIR}" |
| 29 | + curl -s "https://storage.googleapis.com/cos-tools/${BUILD_ID}/kernel-src.tar.gz" \ |
| 30 | + | tar -xzf - -C "${BUILD_DIR}" |
27 | 31 | }
|
28 | 32 |
|
29 | 33 | fetch_generic_linux_sources()
|
30 | 34 | {
|
31 |
| - kernel_version=$(uname -r | tr -d '+') |
32 |
| - major_version=$(echo ${kernel_version} | cut -d . -f 1) |
33 |
| - echo "Fetching upstream kernel sources for ${kernel_version}." |
34 |
| - mkdir -p ${BUILD_DIR} |
35 |
| - curl -sL https://www.kernel.org/pub/linux/kernel/v${major_version}.x/linux-$kernel_version.tar.gz | tar --strip-components=1 -xzf - -C ${BUILD_DIR} |
| 35 | + kernel_version="$(echo "${KERNEL_VERSION}" | awk -vFS=+ '{ print $1 }')" |
| 36 | + major_version="$(echo "${KERNEL_VERSION}" | awk -vFS=. '{ print $1 }')" |
36 | 37 |
|
| 38 | + echo "Fetching upstream kernel sources for ${kernel_version}." |
| 39 | + mkdir -p "${BUILD_DIR}" |
| 40 | + curl -sL "https://www.kernel.org/pub/linux/kernel/v${major_version}.x/linux-$kernel_version.tar.gz" \ |
| 41 | + | tar --strip-components=1 -xzf - -C "${BUILD_DIR}" |
37 | 42 | }
|
38 | 43 |
|
39 | 44 | install_cos_linux_headers()
|
40 | 45 | {
|
41 |
| - if grep -q CHROMEOS_RELEASE_VERSION ${LSB_FILE};then |
42 |
| - BUILD_ID=$(grep CHROMEOS_RELEASE_VERSION ${LSB_FILE} | cut -d = -f 2) |
| 46 | + if grep -q CHROMEOS_RELEASE_VERSION "${LSB_FILE}" >/dev/null; then |
| 47 | + BUILD_ID=$(awk '/CHROMEOS_RELEASE_VERSION =/ { print $3 }' "${LSB_FILE}") |
43 | 48 | BUILD_DIR="/linux-lakitu-${BUILD_ID}"
|
44 | 49 | SOURCES_DIR="${TARGET_DIR}/linux-lakitu-${BUILD_ID}"
|
45 | 50 |
|
46 |
| - if [ ! -e "${SOURCES_DIR}/.installed" ];then |
| 51 | + if [[ ! -e "${SOURCES_DIR}/.installed" ]]; then |
47 | 52 | echo "Installing kernel headers for COS build ${BUILD_ID}"
|
48 | 53 | time fetch_cos_linux_sources
|
49 | 54 | time generate_headers
|
50 |
| - time mv ${BUILD_DIR} ${TARGET_DIR} |
| 55 | + time rm -rf "${TARGET_DIR}${BUILD_DIR}" |
| 56 | + time mv "${BUILD_DIR}" "${TARGET_DIR}" |
51 | 57 | touch "${SOURCES_DIR}/.installed"
|
52 | 58 | fi
|
53 | 59 | fi
|
54 | 60 | }
|
55 | 61 |
|
56 | 62 | install_generic_linux_headers()
|
57 | 63 | {
|
58 |
| - BUILD_DIR="/linux-generic-$(uname -r)" |
59 |
| - SOURCES_DIR="${TARGET_DIR}/linux-generic-$(uname -r)" |
| 64 | + BUILD_DIR="/linux-generic-${KERNEL_VERSION}" |
| 65 | + SOURCES_DIR="${TARGET_DIR}/linux-generic-${KERNEL_VERSION}" |
60 | 66 |
|
61 |
| - if [ ! -e "${SOURCES_DIR}/.installed" ];then |
| 67 | + if [[ ! -e "${SOURCES_DIR}/.installed" ]];then |
62 | 68 | echo "Installing kernel headers for generic kernel"
|
63 | 69 | time fetch_generic_linux_sources
|
64 | 70 | time generate_headers
|
65 |
| - time mv ${BUILD_DIR} ${TARGET_DIR} |
| 71 | + time rm -rf "${TARGET_DIR}${BUILD_DIR}" |
| 72 | + time mv "${BUILD_DIR}" "${TARGET_DIR}" |
66 | 73 | touch "${SOURCES_DIR}/.installed"
|
67 | 74 | fi
|
68 | 75 | }
|
69 | 76 |
|
70 | 77 | install_headers()
|
71 | 78 | {
|
72 |
| - distro=$(grep ^NAME ${OS_RELEASE_FILE} | cut -d = -f 2) |
| 79 | + distro="$(awk '/^NAME =/ { print $3 }' "${OS_RELEASE_FILE}")" |
73 | 80 |
|
74 | 81 | case $distro in
|
75 | 82 | *"Container-Optimized OS"*)
|
76 | 83 | install_cos_linux_headers
|
77 |
| - HEADERS_TARGET=${SOURCES_DIR} |
| 84 | + HEADERS_TARGET="${SOURCES_DIR}" |
78 | 85 | ;;
|
79 | 86 | *)
|
80 | 87 | echo "WARNING: Cannot find distro-specific headers for ${distro}. Fetching generic headers."
|
81 | 88 | install_generic_linux_headers
|
82 |
| - HEADERS_TARGET=${SOURCES_DIR} |
| 89 | + HEADERS_TARGET="${SOURCES_DIR}" |
83 | 90 | ;;
|
84 | 91 | esac
|
85 | 92 | }
|
86 | 93 |
|
87 | 94 | check_headers()
|
88 | 95 | {
|
89 |
| - modules_path=$1 |
90 |
| - utsname=$(uname -r) |
91 |
| - arch=$(uname -m) |
92 |
| - kdir="${modules_path}/${utsname}" |
| 96 | + modules_path="$1" |
| 97 | + arch="$(uname -m)" |
| 98 | + kdir="${modules_path}/${KERNEL_VERSION}" |
93 | 99 |
|
94 |
| - [ "${arch}" == "x86_64" ] && arch="x86" |
| 100 | + [[ "${arch}" == "x86_64" ]] && arch="x86" |
95 | 101 |
|
96 |
| - [ ! -e ${kdir} ] && return 1 |
97 |
| - [ ! -e "${kdir}/source" ] && [ ! -e "${kdir}/build" ] && return 1 |
| 102 | + [[ ! -e "${kdir}" ]] && return 1 |
| 103 | + [[ ! -e "${kdir}/source" ]] && [[ ! -e "${kdir}/build" ]] && return 1 |
98 | 104 |
|
99 |
| - header_dir=$([ -e "${kdir}/source" ] && echo "${kdir}/source" || echo "${kdir}/build") |
| 105 | + header_dir="$([[ -e "${kdir}/source" ]] && echo "${kdir}/source" || echo "${kdir}/build")" |
100 | 106 |
|
101 |
| - [ ! -e "${header_dir}/include/linux/kconfig.h" ] && return 1 |
102 |
| - [ ! -e "${header_dir}/include/generated/uapi" ] && return 1 |
103 |
| - [ ! -e "${header_dir}/arch/${arch}/include/generated/uapi" ] && return 1 |
| 107 | + [[ ! -e "${header_dir}/include/linux/kconfig.h" ]] && return 1 |
| 108 | + [[ ! -e "${header_dir}/include/generated/uapi" ]] && return 1 |
| 109 | + [[ ! -e "${header_dir}/arch/${arch}/include/generated/uapi" ]] && return 1 |
104 | 110 |
|
105 | 111 | return 0
|
106 | 112 | }
|
107 | 113 |
|
108 |
| -if [ ! -e /lib/modules/.installed ];then |
109 |
| - if ! check_headers ${HOST_MODULES_DIR}; then |
110 |
| - install_headers |
| 114 | +if [[ ! -e /lib/modules/.installed ]]; then |
| 115 | + if check_headers "${HOST_MODULES_DIR}"; then |
| 116 | + HEADERS_TARGET="${HOST_MODULES_DIR}/source" |
111 | 117 | else
|
112 |
| - HEADERS_TARGET=${HOST_MODULES_DIR}/source |
| 118 | + install_headers |
113 | 119 | fi
|
114 | 120 |
|
115 |
| - mkdir -p "/lib/modules/$(uname -r)" |
116 |
| - ln -sf ${HEADERS_TARGET} "/lib/modules/$(uname -r)/source" |
117 |
| - ln -sf ${HEADERS_TARGET} "/lib/modules/$(uname -r)/build" |
| 121 | + mkdir -p "/lib/modules/${KERNEL_VERSION}" |
| 122 | + ln -sf "${HEADERS_TARGET}" "/lib/modules/${KERNEL_VERSION}/source" |
| 123 | + ln -sf "${HEADERS_TARGET}" "/lib/modules/${KERNEL_VERSION}/build" |
118 | 124 | touch /lib/modules/.installed
|
119 | 125 | exit 0
|
120 | 126 | else
|
|
0 commit comments