summaryrefslogtreecommitdiffstats
path: root/remote/modules/kernel-system/kernel-system.build
blob: d542a80daeee2817a5f9deb6aa776058a35482e0 (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
# overloaded get_kernel_version function
get_kernel_version(){
	pinfo "KERNEL_CURRENT_VERSION: ${KERNEL_CURRENT_VERSION}"
	[ -z "${KERNEL_CURRENT_VERSION}" ] && perror "KERNEL_CURRENT_VERSION is not set, 'uname -r' output: '$(uname -r)'. Aborting..."

	# set SYS_UTS_RELEASE and SYS_KERNEL correctly
	declare -rg SYS_UTS_RELEASE="${KERNEL_CURRENT_VERSION}" 2>/dev/null
	declare -rg SYS_KERNEL="$(echo ${KERNEL_CURRENT_VERSION} | grep -o -E '^[0-9\.]+')" 2>/dev/null

	# KERNEL_NEW_VERSION is the same as KERNEL_CURRENT_VERSION
	declare -rg KERNEL_NEW_VERSION="$KERNEL_CURRENT_VERSION" 2>/dev/null

	# check linux headers directory
	if [ -d "/usr/src/linux-headers-$(uname -r)" ]; then
		declare -rg KERNEL_HEADERS_PATH="/usr/src/linux-headers-$(uname -r)" 2>/dev/null
	else
		# fetch source did not install the headers correctly.
		perror "kernel headers for $KERNEL_CURRENT_VERSION not found! Re-build the kernel-system module"
	fi

	# check kernel modules/firmware	directory
	if [ -d "/lib/modules/${KERNEL_CURRENT_VERSION}" -a \
		-d "/lib/firmware/${KERNEL_CURRENT_VERSION}" ]; then
		# The expected paths exists, set KERNEL_BASE_DIR to /
		declare -rg KERNEL_BASE_DIR="/" 2>/dev/null
	else
		perror "Could not find kernel modules / firmware for kernel version '$KERNEL_CURRENT_VERSION'. \
			Does '/lib/modules/$KERNEL_CURRENT_VERSION' exist?"
	fi

	# print debug info
	pdebug "SYS_UTS_RELEASE: '$SYS_UTS_RELEASE'"
	pdebug "SYS_KERNEL: '$SYS_KERNEL'"
	pdebug "KERNEL_NEW_VERSION: '$KERNEL_NEW_VERSION'"
	pdebug "KERNEL_BASE_DIR: '$KERNEL_BASE_DIR'"

	# set KERNEL_HEADERS_PATH
	#get_kernel_headers_path
}

get_kernel_headers_path(){
	# determine the path to the kernel headers we just installed
	for DIR in $(list_content_package --dirs linux-headers-$(uname -r) ); do
		if [[ "$DIR" =~ .*linux-headers-$(uname -r)$ ]]; then
			# validate candidate
			[ -e "$DIR/./include/generated/utsrelease.h" ] \
				|| perror "Could not find utsrelease.h at '$DIR/./include/generated/utsrelease.h'"
			# TODO: maybe check if SYS_UTS_RELEASE is the same as in the header.

			# all good, we found it
			pinfo "Kernel headers found at '$DIR'."
			declare -rg KERNEL_HEADERS_PATH="$DIR"
			break
		fi
	done
	
	[ -z "$KERNEL_HEADERS_PATH" ] && perror "KERNEL_HEADERS_PATH was not set correctly."

}

fetch_source() {

	# get the source from repos, use meta-package for now
	pinfo "Installing kernel headers from the repositories for version '${KERNEL_CURRENT_VERSION}'"
	
	# the actual install routine is done via config file
	# through the REQUIRED_INSTALLED_PACKAGES variable

}

build() {
	
	# simply copy the kernel from the running system
	# to the build directory as it is.
	local KERNEL_SYSTEM_PATH=""
	
	# 1st "parse" /proc/cmdline for the kernel name
	for i in $(cat /proc/cmdline); do
		[[ "$i" =~ ^BOOT_IMAGE=.* ]] && local BOOT_IMAGE="$(basename $(echo $i|cut -c 12-))"
	done

	# exit if KERNEL_SYSTEM_FILENAME is empty, should not happen
	[ -z "${BOOT_IMAGE}" ] && perror "Could not determine the full path to the running kernel..."

	# now find it in KERNEL_SYSTEM_SEARCH_PATH
	# NOTE: this variable should be expanded in the future if new locations are to be searched
	local KERNEL_SYSTEM_SEARCH_PATH="/boot"
	local KERNEL_SYSTEM_SEARCH_RESULTS="$(find "${KERNEL_SYSTEM_SEARCH_PATH}" -type f -iname "*${BOOT_IMAGE}*")"

	if [ "$(echo $KERNEL_SYSTEM_SEARCH_RESULTS|wc -w)" -eq 1 ]; then
		# we found the running kernel path
		KERNEL_SYSTEM_PATH="${KERNEL_SYSTEM_SEARCH_RESULTS}"
	else 
		# we found more than one, shouldn't happen...
		perror "Found more than one kernel named '${BOOT_IMAGE}' inside '${KERNEL_SYSTEM_SEARCH_PATH}'."
	fi

	# at this point, we should definitly have KERNEL_SYSTEM_PATH, check just in case :)
	[ -z "${KERNEL_SYSTEM_PATH}" ] && perror "KERNEL_SYSTEM_PATH isn't set, kernel not found. This shouldn't happen!"
	pinfo "Live kernel path: $KERNEL_SYSTEM_PATH"

	# copy kernel to build directory
	[ -z "${KERNEL_TARGET_NAME}" ] && perror "KERNEL_TARGET_NAME not set, this mean something is wrong with remote/include/kernel.inc"
	cp "${KERNEL_SYSTEM_PATH}" "${MODULE_BUILD_DIR}/${KERNEL_TARGET_NAME}" && \
		pinfo "Copied kernel from '${KERNEL_SYSTEM_PATH}' to '${MODULE_BUILD_DIR}/${KERNEL_TARGET_NAME}'"
	#[ -z "${KERNEL_BUILD_DIR}" ] && KERNEL_BUILD_DIR="${MODULE_BUILD_DIR}"
}

post_copy() {
	:
	#mkdir -p "$TARGET_BUILD_DIR/../kernel"
	#cp -a "$MODULE_BUILD_DIR/kernel" "$TARGET_BUILD_DIR/../kernel/kernel" || perror "could not copy kernel to remote build dir."
}