summaryrefslogtreecommitdiffstats
path: root/helper/kernel.inc
blob: 61fa3efdf2576e59bc7d46f042721b46e4949923 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# 
#	Common functions to copy kernel related files
#
############################################################
#
#
# copies kernel modules as given in the module config file
# * depends on 'depmod'
# * requires REQUIRED_KERNEL_MODULES to be set.
#   (entries must be a relative path to /lib/modules/<KERNEL_CURRENT_VERSION>)
#
# ex: for 		/lib/modules/3.2.0/kernel/fs/nfs/nfs.ko
#     must be given as  kernel/fs/nfs/nfs.ko
#

# this code depends on KERNEL_CURRENT_VERSION, this file needs to be sourced after helper/system.inc!
# (TODO: maybe source system.inc if KERNEL_CURRENT_VERSION
if [ -z "${KERNEL_CURRENT_VERSION}" ]; then
	. ${ROOT_DIR}/helper/system.inc
	[ -z "${KERNEL_CURRENT_VERSION}" ] && perror "KERNEL_CURRENT_VERSION still not set after sourcing, something is very wrong..."
fi

# set global KERNEL_TARGET_NAME
KERNEL_TARGET_NAME="kernel"

check_kernel_build_dir() {
	[ -d "${MODULES_DIR}/kernel/build" ] && KERNEL_BUILD_DIR="${MODULES_DIR}/kernel/build" \
					|| perror "No build directory set for the kernel. Was is built?"
	# hack to get the real path of the installed modules
	KERNEL_NEW_VERSION=$(ls ${KERNEL_BUILD_DIR}/lib/modules)
}

copy_kernel_modules() {
	pinfo "Copying kernel modules for kernel ${KERNEL_CURRENT_VERSION}..."	
	[ -z "${REQUIRED_KERNEL_MODULES}" ] && perror "REQUIRED_KERNEL_MODULES is empty. Check your config file."
	check_kernel_build_dir
	#
        # process modules list
	#	
	# search for modules in KERNEL_BUILD_DIR
	cd "${KERNEL_BUILD_DIR}" || perror "Could not cd to ${KERNEL_BUILD_DIR}"

	local KERNEL_MODULES_DIR="lib/modules/${KERNEL_NEW_VERSION}"
	local KERNEL_MODULES_LIST=""
	local REQUIRED_KERNEL_MODULES_EXPANDED=""
	local KERNEL_MODULE=""
	local KERNEL_MODULE_PATH=""
	local ELEM=""
	
	# Do some fancy stuff to allow wildcards etc. in required kernel modules.
	cd "${KERNEL_MODULES_DIR}"
	for KERNEL_MODULE in ${REQUIRED_KERNEL_MODULES}; do
		for ELEM in $KERNEL_MODULE; do
			echo $ELEM | grep '\*' && pwarning "Could not expand '$ELEM'." && continue
			REQUIRED_KERNEL_MODULES_EXPANDED+=" $ELEM"
		done
	done
	cd -
	pinfo "Expanded the list of $(echo "$REQUIRED_KERNEL_MODULES" | wc -w) required kernel modules to $(echo "$REQUIRED_KERNEL_MODULES_EXPANDED" | wc -w)"

        for KERNEL_MODULE in ${REQUIRED_KERNEL_MODULES_EXPANDED}; do
                local KERNEL_MODULE_PATH="${KERNEL_MODULES_DIR}/${KERNEL_MODULE}"
		if grep "^${KERNEL_MODULE}$" "${KERNEL_BUILD_DIR}/${KERNEL_MODULES_DIR}/modules.builtin" >/dev/null; then
			pdebug "Already built-in ${KERNEL_MODULE}."
                elif [ -e "${KERNEL_MODULE_PATH}" ]; then
                        pdebug "Copying '${KERNEL_MODULE_PATH}'"
                        KERNEL_MODULES_LIST+=" ${KERNEL_MODULE_PATH}"
		else
                        pwarning "Module ${KERNEL_MODULE} not found. Skipping. (might cause problems on certain clients!)"
                        continue
                fi

		# check for dependencies
		local DEPS=$(grep "${KERNEL_MODULE}:" "${KERNEL_BUILD_DIR}/${KERNEL_MODULES_DIR}/modules.dep" | cut -d ":" -f2-)
		if [ ! -z "$DEPS" ]; then
			for DEP in $DEPS; do
				pdebug "Adding dep: ${KERNEL_MODULES_DIR}/$DEP"
				KERNEL_MODULES_LIST+=" ${KERNEL_MODULES_DIR}/$DEP"
			done
		else
			pdebug "${KERNEL_MODULE} has no dependencies."
		fi
        done

        if [ ! -z "${KERNEL_MODULES_LIST}" ]; then
                local COUNT=$(echo "${KERNEL_MODULES_LIST}" | wc -w)
                pinfo "Copying $COUNT modules to target directory."
                tarcopy "${KERNEL_MODULES_LIST}" "${TARGET_BUILD_DIR}"
        fi

	#
	# generate modules map files
	#
        # first strip modules.order of all the modules we don't use
        cat "${KERNEL_MODULES_DIR}/modules.order" | grep -E $(echo ${REQUIRED_KERNEL_MODULES} | tr '\ ' '|') \
                >> "${TARGET_BUILD_DIR}/${KERNEL_MODULES_DIR}/modules.order"
        # copy list of builtin kernel modules
        cp "${KERNEL_MODULES_DIR}/modules.builtin" "${TARGET_BUILD_DIR}/${KERNEL_MODULES_DIR}"
        # with modules.order and modules.builtin, we can run depmod for the rest of the files
        depmod -b "${TARGET_BUILD_DIR}" -a "${KERNEL_NEW_VERSION}"

	cd - >/dev/null
}

copy_firmware() {

	pinfo "Copying firmware for kernel ${KERNEL_CURRENT_VERSION}..."
	[ -z "${REQUIRED_FIRMWARE}" ] && perror "REQUIRED_FIRMWARE is empty. Check your config file."
	local OLD_DIR=$(pwd)
	check_kernel_build_dir
	#
        # process firmware list
	#
	cd "${KERNEL_BUILD_DIR}" || perror "Could not cd!"
        local FIRMWARE_DIR="lib/firmware"
	local FIRMWARE_LIST=""
        for FIRMWARE in ${REQUIRED_FIRMWARE}; do
                local FOUND=0
		# check for firmware in the build directory of the kernel
		for CANDIDATE in "${FIRMWARE_DIR}/${FIRMWARE}" "${FIRMWARE_DIR}/${KERNEL_NEW_VERSION}/${FIRMWARE}"; do
			if [ -e "${CANDIDATE}" ]; then
				pdebug "Copying from kernel build dir: '${CANDIDATE}'"
				FIRMWARE_LIST+=" ${CANDIDATE}"
				FOUND=1
			fi
		done

		# if we didn't found it in the kernel build directory, check for firmware in the system firmware directory
		if [ $FOUND -ne 1 ]; then
			for CANDIDATE in "/${FIRMWARE_DIR}/${FIRMWARE}" "/${FIRMWARE_DIR}/${KERNEL_CURRENT_VERSION}/${FIRMWARE}"; do
				if [ -e "${CANDIDATE}" ]; then
					if [ $(echo "${CANDIDATE}" | grep -c "${KERNEL_CURRENT_VERSION}") -eq 0 ]; then
						pdebug "Copying from system: '${CANDIDATE}'"
						FIRMWARE_LIST+=" ${CANDIDATE}"
					else
						pdebug "Copying from system: '${CANDIDATE}' to ${FIRMWARE_DIR}/${KERNEL_NEW_VERSION}/${FIRMWARE}"
						FIRMWARE_LIST+=" /${FIRMWARE_DIR}/${KERNEL_CURRENT_VERSION}/${FIRMWARE}"
					fi
					FOUND=1	
				fi
			done
		fi

                [ $FOUND -ne 1 ] && pwarning "Neither '${FIRMWARE_DIR}/${FIRMWARE}' nor '${FIRMWARE_DIR}/${KERNEL_NEW_VERSION}/${FIRMWARE}' "\
						" was found on the system. Skipping. (might cause problems on certain clients!)"
        done

        if [ ! -z "${FIRMWARE_LIST}" ]; then
                local COUNT=$(echo "${FIRMWARE_LIST}" | wc -w)
                pinfo "Copying $COUNT firmware to target directory."
                tarcopy "${FIRMWARE_LIST}" "${TARGET_BUILD_DIR}"
        fi

	# post-process to fix the path of the firmwares found on the system unter /lib/firmware/$(uname -r)
	# which have to be copied to /lib/firmware/${KERNEL_NEW_VERSION}
	if [ -d "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION}" ]; then
		mkdir -p "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_NEW_VERSION}/"
		cd "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION}" || perror "old kernel but no old kernel"
		tarcopy "$(ls)" "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_NEW_VERSION}/"
		cd -
		rm -r "${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION}" || perror "something went very wrong..."
	else
		pdebug "No ${TARGET_BUILD_DIR}/lib/firmware/${KERNEL_CURRENT_VERSION} directory, skipping the merge."
	fi

	cd "$OLD_DIR"
}

copy_kernel() {
	check_kernel_build_dir	

       	local TOOL_STR="$TOOL_STR copy_kernel:"
	local KERNEL_DIR="${MODE_DIR}/builds/kernel"
	pinfo "Copying '${KERNEL_TARGET_NAME}' to '${KERNEL_DIR}'."
	[ -d "${KERNEL_DIR}" ] || mkdir -p "${KERNEL_DIR}"
	cp "${KERNEL_BUILD_DIR}/${KERNEL_TARGET_NAME}" "${KERNEL_DIR}" || perror "Could not copy kernel!"
	pinfo "You may want to update your systems firmware/modules to match the current kernel."
}