summaryrefslogtreecommitdiffstats
path: root/remote/modules/kernel/kernel.build
blob: 197935f7da51b7d3fd69da5db8b4ec5fdaaf787a (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
fetch_source() {
	# get the source according to the distro
        if [ "x$PACKET_MANAGER" == "xapt" ]; then
		pdebug "apt-ing kernel source"
		apt-get install -y dpkg-dev || perror "Installing dpkg-dev failed."
                apt-get source linux-image-${KERNEL_CURRENT_VERSION} || perror "Fetching kernel source failed."
		[ -z "$(ls -d linux-*/)" ] && perror "Source directory not found."
		[ ! -e ksrc ] && ln -s  "$(ls -d linux-*/)" "ksrc"
        elif [ "x$PACKET_MANAGER" == "xzypper" ]; then
		pdebug "zypping kernel source"
                zypper --no-refresh install -y kernel-source || perror "Fetching kernel source failed."

		# find src directory
		local SOURCE_DIR=$(rpm -ql kernel-source |grep -E -o '^/.*src/linux-[^/]+/' |head -1)
		[ -z "${SOURCE_DIR}" ] && perror "Could not determine directory of kernel source..."
		ln -sf "${SOURCE_DIR}" ksrc
        else
		pdebug "Packet manager not determined!"
	fi

	# check for aufs
	local RSL=$(find ksrc/ -type d -name aufs)
	if [ -z "$RSL" ]; then
		pinfo "aufs not found in kernel sources, patching it..."
		patch_aufs
	else
		pinfo "aufs detected in kernel source :)"
	fi

	# remember the current kernel version
	echo "${KERNEL_CURRENT_VERSION}" > ksrc/KVERSION

}

build() {
	local TARGET_CONFIG_FILE="openslx.config"
	[ -e "${TARGET_CONFIG_FILE}" ] && rm -f "${TARGET_CONFIG_FILE}"
	# update config and copy to ksrc
	pinfo "Updating kernel config..."
	update_config
	cp "${TARGET_CONFIG_FILE}" ksrc/.config
	
	# make kernel with the new config
	cd ksrc || perror "Could not cd to ksrc, was the kernel source fetched properly?"
	pinfo "Preparing kernel for new config ('make oldconfig')"
	if [ "x$MLTK_QUIET" = "x1" ]; then
		yes "" | make oldconfig || perror "make oldconfig failed"
	else
		make oldconfig || perror "make oldconfig failed"
	fi
	pinfo "Compiling kernel... (this will take some time)"
	if gcc --version | grep "4\.7" && which distcc; then
		pinfo "USING DISTCC"
		make CC="distcc gcc-4.7" -j16 || perror "make failed"
	else
		# explicitly state number of cores here, as MAKEFLAGS seems to be overridden
		make "-j$CPU_CORES" || perror "make failed"
	fi
	
	# install modules to build directory
	pinfo "Installing kernel modules..."
	if [ -d "${MODULE_BUILD_DIR}/lib/modules" ]; then
		rm -r "${MODULE_BUILD_DIR}/lib/modules" || pwarning "Could not clean old modules"
	fi
	make INSTALL_MOD_PATH="${MODULE_BUILD_DIR}" INSTALL_MOD_STRIP=1 modules_install || perror "make modules_install failed in ${MODULE_BUILD_DIR}"
	cd - 2> /dev/null

	# copy kernel to build
	cp ksrc/arch/x86/boot/bzImage "${MODULE_BUILD_DIR}/${KERNEL_TARGET_NAME}"
	pinfo "Kernel was successfully built at ${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"
}

# helper function to update the current kernel config with our parameters
update_config() {

	# first we need to update the current config
	local BASE_CONFIG_FILE="/boot/config-$(uname -r)"
	[ -e "${BASE_CONFIG_FILE}" ] || perror "$BASE_CONFIG_FILE could not be found! This should not happen."
	
	# check for our wanted config parameter
	local OPENSLX_WANTED_CONFIG="${ROOT_DIR}/data/kernel.wanted.config"
	[ -e "${OPENSLX_WANTED_CONFIG}" ] || perror "$OPENSLX_WANTED_CONFIG does not exist! Please add a list of wanted kernel config parameters."

	# copy basic config file
	cp "$BASE_CONFIG_FILE" "$TARGET_CONFIG_FILE"

	for WANTED_CONFIG in $(cat $OPENSLX_WANTED_CONFIG|sort -u); do
		local CONFIG_PARAM_NAME="$(echo $WANTED_CONFIG | awk -F "=" '{print $1}')"
		local SEARCH_RESULT="$(grep -E "^\s*$(echo "$CONFIG_PARAM_NAME" | escape_search)=" "$BASE_CONFIG_FILE")"

		#echo "Process: $SEARCH_RESULT"
		# analyse results
		if [ "x$SEARCH_RESULT" == "x" ]; then
			# no match, add it
			sed -i -r "s/^\s*#.*\s$(echo "$CONFIG_PARAM_NAME" | escape_search)[^_A-Z0-9].*$//" "$TARGET_CONFIG_FILE"
			echo "$WANTED_CONFIG" >> "$TARGET_CONFIG_FILE"
		else
			# match, change to our setting if they differ
			if [ "x$SEARCH_RESULT" != "x$WANTED_CONFIG" ]; then
				sed -i "s/$(echo "$SEARCH_RESULT" | escape_search)/$(echo "$WANTED_CONFIG" | escape_replace)/" "$TARGET_CONFIG_FILE"
			fi
		fi
	done
}

# helper to patch aufs
patch_aufs() {
	pinfo "Cloning aufs3 standalone git"
	cd "$MODULE_DIR"
	if [ ! -d "aufs3-standalone/.git" ]; then
		rm -rf "aufs3-standalone"
		git clone "git://aufs.git.sourceforge.net/gitroot/aufs/aufs3-standalone.git" || perror "Cloning aufs3 failed."
	fi
	# get the needed version
	[ ! -z ${KERNEL_CURRENT_VERSION} ] && local NEEDED_BRANCH=$(echo $KERNEL_CURRENT_VERSION | awk -F "." '{print $1"."$2}') \
				|| perror "KERNEL_CURRENT_VERSION not set, this should not happen!"
	pinfo "Getting branch origin/$NEEDED_BRANCH"
	cd "$MODULE_DIR/aufs3-standalone" || perror "Could not CD to aufs3-standalone"
	git checkout "origin/aufs$NEEDED_BRANCH"
	pinfo "Starting to patch..."
	tarcopy "Documentation fs" "$MODULE_DIR/ksrc"
	local AUFSH
	if [ ! -e "include/linux/aufs_type.h" ]; then
		[ ! -e "include/uapi/linux/aufs_type.h" ] && perror "No aufs_type.h in aufs3-git"
		AUFSH="include/uapi/linux/aufs_type.h"
	elif [ -e "include/uapi/linux/aufs_type.h" -a "$(stat -c %s "include/uapi/linux/aufs_type.h")" -gt "$(stat -c %s "include/linux/aufs_type.h")" ]; then
		AUFSH="include/uapi/linux/aufs_type.h"
	else
		AUFSH="include/linux/aufs_type.h"
	fi
	pinfo "Using $AUFSH"
	cp "$AUFSH" "$MODULE_DIR/ksrc/include/linux/" || perror "could not copy $AUFSH"
	cd "$MODULE_DIR/ksrc" || perror "Could not CD to kernel-source dir ksrc"
	patch -p1 < "$MODULE_DIR/aufs3-standalone/aufs3-kbuild.patch" || perror "aufs3-standalone/aufs3-kbuild.patch failed"  
	patch -p1 < "$MODULE_DIR/aufs3-standalone/aufs3-base.patch" || perror "aufs3-standalone/aufs3-base.patch failed"
	#patch -p1 < "$MODULE_DIR/aufs3-standalone/aufs3-proc_map.patch" || perror "aufs3-standalone/aufs3-proc_map.patch failed"
	pinfo "Patched kernel source with aufs-${NEEDED_BRANCH}"
	cd "$MODULE_DIR"
}