Magellan Linux

Contents of /smage/trunk/include/kernel.sminc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3581 - (show annotations) (download)
Mon Jul 9 17:44:28 2012 UTC (12 years, 3 months ago) by niro
File size: 13801 byte(s)
-merged with upstream
1 # kernel global smage functions
2 # $Id$
3
4 # needed extra vars:
5 # ${KERNEL_TYPE}: needed, 'sources', 'headers' or 'image'
6 # ${BRANCH}: needed, style branch of the kernel
7 # ${KERNEL_CONFIG}: optional, needed if you want to override the default
8 # searchpath ${SOURCEDIR}/${PNAME}/config-${PVER}-${BRANCH}-${PBUILD}
9 #
10 # ${PSUBVER}: optional, only needed for 2.6.x.x naming scheme
11 # ${EXTRAVERSION}: optinal, extra version for the kernel name
12 # default setting is ${PSUBVER}-${BRANCH}-${PBUILD}
13 # ${KERNEL_IMAGE_PATH}: override the default KERNEL_IMAGE_PATH (to build xen images for e.g.)
14 # ${COMPRESS_MODULES}: compresses the kernel modules with given compressor (gzip|bzip2|xz)
15 # ${LINUX_FIRMWARE}: remove included firmware bits if requested
16 # set the version here like USE_LINUX_FIRMARE="20110605"
17 # version "internal" keeps the bits too
18 #
19
20 sminclude mtools
21
22 # remove me when all distris support this:
23 if [[ ${USE_KERNEL_ARCH_EXTENSION} = true ]]
24 then
25 KERNEL_ARCH_EXTENSION="-${ARCH}"
26 else
27 KERNEL_ARCH_EXTENSION=""
28 fi
29
30 if [[ ! -z ${COMPRESS_MODULES} ]]
31 then
32 case ${COMPRESS_MODULES} in
33 gzip)
34 COMPRESS_MODULES_CMD="gzip -9v"
35 SDEPEND="${SDEPEND}
36 >= app-arch/gzip-1.4"
37 ;;
38 bzip2)
39 COMPRESS_MODULES_CMD="bzip2 -9v"
40 SDEPEND="${SDEPEND}
41 >= app-arch/bzip2-1.0.6"
42 ;;
43 xz)
44 COMPRESS_MODULES_CMD="xz -9v"
45 SDEPEND="${SDEPEND}
46 >= app-arch/xz-5"
47 ;;
48 none)
49 echo "Disabled compressed kernel-modules!"
50 COMPRESS_MODULES_CMD=""
51 ;;
52 *) die "Unkown compression format '${COMPRESS_MODULES}'" ;;
53 esac
54 fi
55
56 # some sane defaults
57 : ${PCAT="sys-kernel"}
58 : ${HOMEPAGE="http://magellan-linux.de/"}
59 : ${TARBALLPVER="${PVER}"}
60 : ${SRCFILE="linux-${TARBALLPVER}.tar.bz2"}
61 : ${SRCDIR="${BUILDDIR}/linux-${PVER}-${BRANCH}-${PBUILD}"}
62
63 if [[ -z ${SRC_URI[*]} ]]
64 then
65 SRC_URI=(
66 http://www.kernel.org/pub/linux/kernel/v$(echo ${PVER} | cut -d. -f1).x/${SRCFILE}
67 http://ftp.uni-erlangen.de/pub/Linux/MIRROR.kernel/v$(echo ${PVER} | cut -d. -f1).x/${SRCFILE}
68 mirror://kernel-${BRANCH}/${SRCFILE}
69 )
70 case ${KERNEL_TYPE} in
71 image|sources)
72 SRC_URI=(
73 ${SRC_URI[*]}
74 mirror://kernel-${BRANCH}/config-${PVER}-${BRANCH}-${PBUILD}-i686
75 mirror://kernel-${BRANCH}/config-${PVER}-${BRANCH}-${PBUILD}-x86_64
76 )
77 ;;
78 esac
79 fi
80
81 case ${KERNEL_TYPE} in
82 image)
83 : ${PROVIDE="virtual/kernel"}
84 : ${DEPEND=">= virtual/module-tools
85 >= sys-fs/udev-115
86 >= virtual/initramfs-tools
87 >= virtual/initscripts"}
88 if [[ ! -z ${LINUX_FIRMWARE} ]] && [[ ${LINUX_FIRMWARE} != internal ]]
89 then
90 DEPEND="${DEPEND}
91 >= sys-kernel/linux-firmware-${LINUX_FIRMWARE}"
92 fi
93 : ${DESCRIPTION="Kernel image of the '${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}' kernel branch."}
94 ;;
95
96 sources)
97 : ${PROVIDE="virtual/kernel-sources"}
98 : ${DESCRIPTION="Kernel sources of the '${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}' kernel branch."}
99 ;;
100
101 headers)
102 : ${PROVIDE="virtual/kernel-headers"}
103 : ${DESCRIPTION="Sanitized kernel headers for libc."}
104 ;;
105 esac
106
107 EXTRAVERSION="${PSUBVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}"
108 KERNEL_CONFIG="${SOURCEDIR}/${PNAME}/config-${PVER}-${BRANCH}-${PBUILD}-${ARCH}"
109
110 # special vars for mage file only
111 KPVER="${PVER}"
112 KPBUILD="${PBUILD}"
113 KBRANCH="${BRANCH}"
114 SPECIAL_VARS="${SPECIAL_VARS} KPVER KPBUILD KBRANCH"
115
116 kernel_src_prepare()
117 {
118 munpack ${SRCFILE} || die
119 mv ${BUILDDIR}/linux-${TARBALLPVER} ${SRCDIR} || die
120
121 # fix permissions
122 chown -R 0:0 ${SRCDIR} || die
123 chmod -R a+r-w+X,u+w ${SRCDIR} || die
124
125 cd ${SRCDIR}
126
127 # fixes INSTALL_PATH to /boot and EXTRAVERSION
128 sed -i -e 's:#export\tINSTALL_PATH:export\tINSTALL_PATH:' \
129 -e "s:^\(EXTRAVERSION =\).*:\1 ${EXTRAVERSION}:" Makefile || die
130
131 # fix sublevel version with 3.x kernels and higher
132 local kver=$(echo ${PVER} | sed "s:\.::g")
133 if [[ ${PVER%%.*} -gt 2 ]]
134 then
135 local sublevel="$(echo ${PVER} | cut -d. -f3)"
136 sed -i "s:^\(SUBLEVEL =\).*:\1 ${sublevel}:" Makefile || die
137 fi
138 }
139
140 kernel_src_compile()
141 {
142 cd ${SRCDIR}
143
144 make mrproper || die
145
146 if [[ ${KERNEL_TYPE} = sources ]] || [[ ${KERNEL_TYPE} = image ]]
147 then
148 # inject our kernel config
149 cp ${KERNEL_CONFIG} ${SRCDIR}/.config || die
150 make ARCH=${ARCH/i*86/x86} oldconfig || die
151 fi
152
153 case ${KERNEL_TYPE} in
154 image)
155 mmake ARCH=${ARCH/i*86/x86} || die
156 ;;
157
158 headers)
159 make ARCH=${ARCH/i*86/x86} headers_check || die
160 ;;
161
162 sources)
163 make clean || die
164
165 # needed to compile other programs like iptables
166 make ARCH=${ARCH/i*86/x86} prepare || die
167 # needed to cover >=2.6.14 builds
168 # but deprecated with >=2.6.21, prepare cover all targets there
169 local kver=$(echo ${PVER} | sed "s:\.::g")
170 # honor >=3.0.x; add one zero
171 if [[ ${PVER%%.*} -gt 2 ]]
172 then
173 kver="${kver}0"
174 fi
175 if [[ ${kver} < 2621 ]]
176 then
177 make ARCH=${ARCH/i*86/x86} prepare-all || die
178 fi
179 make ARCH=${ARCH/i*86/x86} scripts || die
180
181 # at least alsa-drivers needs this one ...
182 touch include/linux/autoconf.h || die
183 ;;
184 esac
185 }
186
187 kernel_image_src_install()
188 {
189 cd ${SRCDIR}
190
191 # needed directories
192 minstalldir /boot || die
193
194 # install modules
195 make ARCH=${ARCH/i*86/x86} INSTALL_MOD_PATH=${BINDIR}/usr modules_install || die
196
197 # install kernelimage
198 if [[ -z ${KERNEL_IMAGE_PATH} ]]
199 then
200 local my_arch="${ARCH}"
201 [[ ${my_arch} = i*86 ]] && my_arch=i386
202 minstallfile arch/${my_arch}/boot/bzImage \
203 /boot/kernel-${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION} \
204 || die
205 else
206 minstallfile ${KERNEL_IMAGE_PATH} \
207 /boot/kernel-${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION} \
208 || die
209 fi
210
211 # install System.map
212 minstallfile System.map \
213 /boot/System.map-${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION} \
214 || die
215
216 # install Module.symvers in boot and /usr/src
217 minstallfile Module.symvers \
218 /boot/Module.symvers-${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION} \
219 || die
220 minstalldir /usr/src/linux-${PVER}-${BRANCH}-${PBUILD} || die
221 minstallfile Module.symvers \
222 /usr/src/linux-${PVER}-${BRANCH}-${PBUILD} \
223 || die
224
225 # install kernel description
226 echo "
227 KRNVER=${BRANCH}-${PVER}-${ARCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}
228 KRNLIB=${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}
229 KRNIMG=kernel-${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}
230 KRNINITRD=
231 " > ${BINDIR}/boot/kernelversion || die
232
233 # install kernel-config from srcdir
234 minstallfile .config \
235 /boot/config-${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION} \
236 || die
237
238 # fix build symlink
239 if [ -L ${BINDIR}/usr/lib/modules/${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}/build ]
240 then
241 echo "fixing build symlink"
242 rm ${BINDIR}/usr/lib/modules/${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}/build || die
243 mlink /usr/src/linux-${PVER}-${BRANCH}-${PBUILD} \
244 /usr/lib/modules/${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}/build \
245 || die
246 fi
247
248 # and fixing source symlink
249 if [ -L ${BINDIR}/usr/lib/modules/${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}/source ]
250 then
251 echo "fixing source symlink"
252 rm ${BINDIR}/usr/lib/modules/${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}/source || die
253 mlink /usr/src/linux-${PVER}-${BRANCH}-${PBUILD} \
254 /usr/lib/modules/${PVER}-${BRANCH}-${PBUILD}${KERNEL_ARCH_EXTENSION}/source \
255 || die
256 fi
257
258 # remove firmware bits if $LINUX_FIRMWARE was set
259 if [[ ! -z ${LINUX_FIRMWARE} ]] && [[ ${LINUX_FIRMWARE} != internal ]]
260 then
261 if [[ -d ${BINDIR}/usr/lib/firmware ]]
262 then
263 rm -rv ${BINDIR}/usr/lib/firmware
264 fi
265 fi
266
267 # compress modules if COMPRESS_MODULES_CMD was set
268 if [[ ! -z ${COMPRESS_MODULES_CMD} ]]
269 then
270 # compress all kernel modules
271 find ${BINDIR} -name \*.ko | xargs ${COMPRESS_MODULES_CMD} || die
272 fi
273 }
274
275 kernel_headers_src_install()
276 {
277 cd ${SRCDIR}
278 make ARCH=${ARCH/i*86/x86} INSTALL_HDR_PATH=${BINDIR}/usr headers_install || die
279
280 # clean-up unnecessary files
281 find ${BINDIR} -name .install -or -name ..install.cmd | xargs --no-run-if-empty rm -f
282 }
283
284 kernel_sources_src_install()
285 {
286 cd ${BUILDDIR}
287
288 echo "Creating linux-${PVER}-${BRANCH}-${PBUILD} sources tarball ... "
289 tar cvjf ${BUILDDIR}/linux-${PVER}-${BRANCH}-${PBUILD}.tar.bz2 $(basename ${SRCDIR}) || die
290
291 echo "Moving linux-${PVER}-${BRANCH}-${PBUILD} tarball to \${BINDIR}/usr/src ... "
292 minstalldir /usr/src || die
293 mv ${BUILDDIR}/linux-${PVER}-${BRANCH}-${PBUILD}.tar.bz2 ${BINDIR}/usr/src || die
294 }
295
296 # installs module-sources path/to/SRCDIR to MODDIR/MODNAME
297 # kernel_external_modules_install path/to/SRCDIR DESTDIRNAME
298 kernel_external_modules_install()
299 {
300 cd ${BUILDDIR}
301
302 local modsrc="$1"
303 local moddest="$2"
304 local moddir="${BUILDDIR}/kernel-modules-${PVER}-${BRANCH}-${PBUILD}"
305
306 echo "Installing ${modsrc} to ${moddir}/${moddest} ..."
307 install -d ${moddir} || die
308 mv ${modsrc} ${moddir}/${moddest} || die
309 }
310
311 kernel_external_modules_tarball()
312 {
313 cd ${BUILDDIR}
314
315 echo "Creating kernel-modules-${PVER}-${BRANCH}-${PBUILD} sources tarball ... "
316 tar cvjf ${BUILDDIR}/kernel-modules-${PVER}-${BRANCH}-${PBUILD}.tar.bz2 \
317 kernel-modules-${PVER}-${BRANCH}-${PBUILD} || die
318
319 echo "Moving kernel-modules-${PVER}-${BRANCH}-${PBUILD} tarball to \${BINDIR}/usr/src ... "
320 minstalldir /usr/src || die
321 mv ${BUILDDIR}/kernel-modules-${PVER}-${BRANCH}-${PBUILD}.tar.bz2 ${BINDIR}/usr/src || die
322 }
323
324 kernel_src_install()
325 {
326 case ${KERNEL_TYPE} in
327 image)
328 kernel_image_src_install || die
329 ;;
330
331 headers)
332 kernel_headers_src_install || die
333 ;;
334
335 sources)
336 kernel_sources_src_install || die
337 ;;
338 esac
339 }
340
341 kernel_major_version()
342 {
343 local ksrc="$@"
344 local kv
345
346 if [[ -z ${ksrc} ]]
347 then
348 kv="$(uname -r|cut -d. -f1-2)"
349 else
350 local version
351 local patchlevel
352
353 # get version from makefile
354 version=$(grep "^VERSION[[:space:]]*=[[:space:]]*[[:digit:]]" ${ksrc}/Makefile | sed "s/^.*=[[:space:]]*\([[:digit:]]\+\)/\1/g")
355 # get patchlevel from makefile
356 patchlevel=$(grep "^PATCHLEVEL[[:space:]]*=[[:space:]]*[[:digit:]]" ${ksrc}/Makefile | sed "s/^.*=[[:space:]]*\([[:digit:]]\+\)/\1/g")
357 # kernelversion
358 kv="${version}.${patchlevel}"
359 fi
360
361 echo "${kv}"
362 }
363
364 mod_suffix()
365 {
366 local ksrc="$@"
367 local mod_suffix
368 local kmv
369
370 kmv=$(kernel_major_version ${ksrc})
371 if [[ ${kmv//.} -ge 26 ]]
372 then
373 mod_suffix="ko"
374 else
375 mod_suffix="o"
376 fi
377
378 echo ${mod_suffix}
379 }
380
381 kernel_image_preinstall()
382 {
383 if [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
384 then
385 mount /boot &> /dev/null
386 fi
387 }
388
389 kernel_image_preremove()
390 {
391 if [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
392 then
393 mount /boot &> /dev/null
394 fi
395 }
396
397 kernel_image_postremove()
398 {
399 if [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
400 then
401 # remove old image entry from grub
402 new-kernel-pkg -v --rminitrd --remove ${KPVER}-${KBRANCH}-${KPBUILD}
403 fi
404 }
405
406 kernel_image_postinstall()
407 {
408 [ ! -d ${MROOT}/sys ] && install -d ${MROOT}/sys
409
410 # fixes a small glitch in <=mage-0.3.6-r15 with symlinks
411 [ -f ${MROOT}/usr/src/linux-${KPVER}-${KBRANCH}-${KPBUILD} ] && \
412 rm ${MROOT}/usr/src/linux-${KPVER}-${KBRANCH}-${KPBUILD}
413
414 [ -x ${MROOT}/sbin/depmod ] &&
415 ${MROOT}/sbin/depmod -ae \
416 -F /boot/System.map-${KPVER}-${KBRANCH}-${KPBUILD} \
417 ${KPVER}-${KBRANCH}-${KPBUILD}
418
419 # do not run this if ${MROOT} is set !
420 if [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
421 then
422 new-kernel-pkg -v --mkinitrd --make-default --install ${KPVER}-${KBRANCH}-${KPBUILD}
423 fi
424
425 # symlink actual kernel to /boot/vmlinuz etc...
426 ln -snf kernel-${KPVER}-${KBRANCH}-${KPBUILD} ${MROOT}/boot/vmlinuz
427 ln -snf System.map-${KPVER}-${KBRANCH}-${KPBUILD} ${MROOT}/boot/System.map
428 ln -snf config-${KPVER}-${KBRANCH}-${KPBUILD} ${MROOT}/boot/config
429 [[ -f ${MROOT}/boot/initrd-${KPVER}-${KBRANCH}-${KPBUILD}.img ]] &&
430 ln -snf initrd-${KPVER}-${KBRANCH}-${KPBUILD}.img ${MROOT}/boot/initrd
431 }
432
433 kernel_sources_postinstall()
434 {
435 # check for linux-VERSION/include as the kernel-image installs
436 # the file Modules.symver in the linux-VERSION directory
437 if mqueryfeature "kernelsrcunpack" &&
438 [ ! -d ${MROOT}/usr/src/linux-${KPVER}-${KBRANCH}-${KPBUILD}/include ]
439 then
440 echo "Unpacking linux-${KPVER}-${KBRANCH}-${KPBUILD} source tarball ... "
441 tar xvjf ${MROOT}/usr/src/linux-${KPVER}-${KBRANCH}-${KPBUILD}.tar.bz2 -C ${MROOT}/usr/src
442 fi
443
444 if mqueryfeature "kernelsrcunpack" &&
445 [ ! -d ${MROOT}/usr/src/kernel-modules-${KPVER}-${KBRANCH}-${KPBUILD} ] &&
446 [ -f ${MROOT}/usr/src/kernel-modules-${KPVER}-${KBRANCH}-${KPBUILD}.tar.bz2 ]
447 then
448 echo "Unpacking kernel-modules-${KPVER}-${KBRANCH}-${KPBUILD} source tarball ... "
449 tar xvjf ${MROOT}/usr/src/kernel-modules-${KPVER}-${KBRANCH}-${KPBUILD}.tar.bz2 -C ${MROOT}/usr/src
450 fi
451
452 # create linux symlink
453 if mqueryfeature "linuxsymlink"
454 then
455 echo "Creating linux -> linux-${KPVER}-${KBRANCH}-${KPBUILD} symlink ... "
456 [ ! -d ${MROOT}/usr/src ] && install ${MROOT}/usr/src
457 ln -snf ${MROOT}/usr/src/linux-${KPVER}-${KBRANCH}-${KPBUILD} \
458 ${MROOT}/usr/src/linux || die
459 fi
460
461 # create kernel-modules symlink
462 if mqueryfeature "linuxsymlink" &&
463 [ -d ${MROOT}/usr/src/kernel-modules-${KPVER}-${KBRANCH}-${KPBUILD} ]
464 then
465 echo "Creating kernel-modules -> kernel-modules-${KPVER}-${KBRANCH}-${KPBUILD} symlink ... "
466 [ ! -d ${MROOT}/usr/src ] && install ${MROOT}/usr/src
467 ln -snf ${MROOT}/usr/src/kernel-modules-${KPVER}-${KBRANCH}-${KPBUILD} \
468 ${MROOT}/usr/src/kernel-modules || die
469 fi
470 }
471
472 export_inherits kernel src_prepare src_compile src_install
473
474 # export pre/postinstall functions depending on KERNEL_TYPE
475 case ${KERNEL_TYPE} in
476 image)
477 export_inherits kernel_image preinstall preremove postinstall postremove
478 SPECIAL_FUNCTIONS="${SPECIAL_FUNCTIONS} kernel_image_preinstall kernel_image_preremove kernel_image_postinstall kernel_image_postremove"
479 ;;
480 sources)
481 export_inherits kernel_sources postinstall
482 SPECIAL_FUNCTIONS="${SPECIAL_FUNCTIONS} kernel_sources_postinstall"
483 ;;
484 esac