#!/bin/sh echo "*** BUILD KERNEL BEGIN ***" global SRC_DIR := $[pwd] # Read the 'JOB_FACTOR' property from '.config' global JOB_FACTOR := $[grep -i ^JOB_FACTOR .config | cut -f2 -d'=] # Find the number of available CPU cores. global NUM_CORES := $[grep ^processor /proc/cpuinfo | wc -l] # Calculate the number of 'make' jobs to be used later. global NUM_JOBS := $(NUM_CORES * JOB_FACTOR) cd work/kernel # Prepare the kernel install area. rm -rf kernel_installed mkdir kernel_installed # Change to the kernel source directory which ls finds, e.g. 'linux-4.4.6'. cd $[ls -d linux-*] # Cleans up the kernel sources, including configuration files. echo "Preparing kernel work area..." make mrproper -j $NUM_JOBS # Read the 'USE_PREDEFINED_KERNEL_CONFIG' property from '.config' global USE_PREDEFINED_KERNEL_CONFIG := $[grep -i ^USE_PREDEFINED_KERNEL_CONFIG $SRC_DIR/.config | cut -f2 -d'=] if test $USE_PREDEFINED_KERNEL_CONFIG = "true" -a ! -f $SRC_DIR/minimal_config/kernel.config { echo "Config file $SRC_DIR/minimal_config/kernel.config does not exist." global USE_PREDEFINED_KERNEL_CONFIG := '"false'" } if test $USE_PREDEFINED_KERNEL_CONFIG = "true" { # Use predefined configuration file for the kernel. echo "Using config file $SRC_DIR/minimal_config/kernel.config" cp -f $SRC_DIR/minimal_config/kernel.config .config } else { # Create default configuration file for the kernel. make defconfig -j $NUM_JOBS echo "Generated default kernel configuration." # Changes the name of the system to 'minimal'. sed -i "s/.*CONFIG_DEFAULT_HOSTNAME.*/CONFIG_DEFAULT_HOSTNAME=\"minimal\"/" .config # Enable overlay support, e.g. merge ro and rw directories. sed -i "s/.*CONFIG_OVERLAY_FS.*/CONFIG_OVERLAY_FS=y/" .config # Step 1 - disable all active kernel compression options (should be only one). sed -i "s/.*\\(CONFIG_KERNEL_.*\\)=y/\\#\\ \\1 is not set/" .config # Step 2 - enable the 'xz' compression option. sed -i "s/.*CONFIG_KERNEL_XZ.*/CONFIG_KERNEL_XZ=y/" .config # Enable the VESA framebuffer for graphics support. sed -i "s/.*CONFIG_FB_VESA.*/CONFIG_FB_VESA=y/" .config # Read the 'USE_BOOT_LOGO' property from '.config' global USE_BOOT_LOGO := $[grep -i ^USE_BOOT_LOGO $SRC_DIR/.config | cut -f2 -d'=] if test $USE_BOOT_LOGO = "true" { sed -i "s/.*CONFIG_LOGO_LINUX_CLUT224.*/CONFIG_LOGO_LINUX_CLUT224=y/" .config echo "Boot logo is enabled." } else { sed -i "s/.*CONFIG_LOGO_LINUX_CLUT224.*/\\# CONFIG_LOGO_LINUX_CLUT224 is not set/" .config echo "Boot logo is disabled." } # Disable debug symbols in kernel => smaller kernel binary. sed -i "s/^CONFIG_DEBUG_KERNEL.*/\\# CONFIG_DEBUG_KERNEL is not set/" .config # Enable the EFI stub sed -i "s/.*CONFIG_EFI_STUB.*/CONFIG_EFI_STUB=y/" .config # Check if we are building 32-bit kernel. The exit code is '1' when we are # building 64-bit kernel, otherwise the exit code is '0'. grep -q "CONFIG_X86_32=y" .config # The '$?' variable holds the exit code of the last issued command. if test $Status = 1 { # Enable the mixed EFI mode when building 64-bit kernel. echo "CONFIG_EFI_MIXED=y" >> .config } } # Compile the kernel with optimization for 'parallel jobs' = 'number of processors'. # Good explanation of the different kernels: # http://unix.stackexchange.com/questions/5518/what-is-the-difference-between-the-following-kernel-makefile-terms-vmlinux-vmlinux echo "Building kernel..." make \ CFLAGS="-Os -s -fno-stack-protector -U_FORTIFY_SOURCE" \ bzImage -j $NUM_JOBS # Install the kernel file. cp arch/x86/boot/bzImage \ $SRC_DIR/work/kernel/kernel_installed/kernel # Install kernel headers which are used later when we build and configure the # GNU C library (glibc). echo "Generating kernel headers..." make \ INSTALL_HDR_PATH=$SRC_DIR/work/kernel/kernel_installed \ headers_install -j $NUM_JOBS cd $SRC_DIR echo "*** BUILD KERNEL END ***" (CommandList children: [ (C {(echo)} {(DQ ("*** BUILD KERNEL BEGIN ***"))}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:SRC_DIR) op: Equal rhs: { (CommandSubPart command_list: (CommandList children:[(C {(pwd)})]) left_token: spids: [12 14] ) } spids: [11] ) ] spids: [11] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:JOB_FACTOR) op: Equal rhs: { (DQ (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(grep)} {(-i)} {(Lit_Other "^") (JOB_FACTOR)} {(.config)}) (C {(cut)} {(-f2)} {(-d) (SQ <"=">)}) ] negated: False ) ] ) left_token: spids: [22 42] ) ) } spids: [20] ) ] spids: [20] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:NUM_CORES) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(grep)} {(Lit_Other "^") (processor)} {(/proc/cpuinfo)}) (C {(wc)} {(-l)}) ] negated: False ) ] ) left_token: spids: [50 63] ) } spids: [49] ) ] spids: [49] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:NUM_JOBS) op: Equal rhs: { (ArithSubPart anode: (ArithBinary op_id: Arith_Star left: (ArithVarRef name:NUM_CORES) right: (ArithVarRef name:JOB_FACTOR) ) spids: [70 77] ) } spids: [69] ) ] spids: [69] ) (C {(cd)} {(work/kernel)}) (C {(rm)} {(-rf)} {(kernel_installed)}) (C {(mkdir)} {(kernel_installed)}) (C {(cd)} { (CommandSubPart command_list: (CommandList children:[(C {(ls)} {(-d)} {(linux-) (Lit_Other "*")})]) left_token: spids: [104 111] ) } ) (C {(echo)} {(DQ ("Preparing kernel work area..."))}) (C {(make)} {(mrproper)} {(-j)} {($ VSub_Name "$NUM_JOBS")}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:USE_PREDEFINED_KERNEL_CONFIG) op: Equal rhs: { (DQ (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(grep)} {(-i)} {(Lit_Other "^") (USE_PREDEFINED_KERNEL_CONFIG)} {($ VSub_Name "$SRC_DIR") (/.config)} ) (C {(cut)} {(-f2)} {(-d) (SQ <"=">)}) ] negated: False ) ] ) left_token: spids: [137 158] ) ) } spids: [135] ) ] spids: [135] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$USE_PREDEFINED_KERNEL_CONFIG"))} {(Lit_Other "=")} {(DQ (true))} {(-a)} {(KW_Bang "!")} {(-f)} {($ VSub_Name "$SRC_DIR") (/minimal_config/kernel.config)} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(echo)} { (DQ ("Config file ") ($ VSub_Name "$SRC_DIR") ("/minimal_config/kernel.config does not exist.") ) } ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:USE_PREDEFINED_KERNEL_CONFIG) op: Equal rhs: {(DQ (false))} spids: [201] ) ] spids: [201] ) ] spids: [-1 189] ) ] spids: [-1 206] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$USE_PREDEFINED_KERNEL_CONFIG"))} {(Lit_Other "=")} {(DQ (true))} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(echo)} {(DQ ("Using config file ") ($ VSub_Name "$SRC_DIR") (/minimal_config/kernel.config))} ) (C {(cp)} {(-f)} {($ VSub_Name "$SRC_DIR") (/minimal_config/kernel.config)} {(.config)}) ] spids: [-1 227] ) ] else_action: [ (C {(make)} {(defconfig)} {(-j)} {($ VSub_Name "$NUM_JOBS")}) (C {(echo)} {(DQ ("Generated default kernel configuration."))}) (C {(sed)} {(-i)} { (DQ ("s/.*CONFIG_DEFAULT_HOSTNAME.*/CONFIG_DEFAULT_HOSTNAME=") (EscapedLiteralPart token:) (minimal) (EscapedLiteralPart token:) (/) ) } {(.config)} ) (C {(sed)} {(-i)} {(DQ ("s/.*CONFIG_OVERLAY_FS.*/CONFIG_OVERLAY_FS=y/"))} {(.config)}) (C {(sed)} {(-i)} { (DQ ("s/.*") (EscapedLiteralPart token:) ("(CONFIG_KERNEL_.*") (EscapedLiteralPart token:) (")=y/") (EscapedLiteralPart token:) ("#") (EscapedLiteralPart token:) (" ") (EscapedLiteralPart token:) ("1 is not set/") ) } {(.config)} ) (C {(sed)} {(-i)} {(DQ ("s/.*CONFIG_KERNEL_XZ.*/CONFIG_KERNEL_XZ=y/"))} {(.config)}) (C {(sed)} {(-i)} {(DQ ("s/.*CONFIG_FB_VESA.*/CONFIG_FB_VESA=y/"))} {(.config)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:USE_BOOT_LOGO) op: Equal rhs: { (DQ (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(grep)} {(-i)} {(Lit_Other "^") (USE_BOOT_LOGO)} {($ VSub_Name "$SRC_DIR") (/.config)} ) (C {(cut)} {(-f2)} {(-d) (SQ <"=">)}) ] negated: False ) ] ) left_token: spids: [380 401] ) ) } spids: [378] ) ] spids: [378] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$USE_BOOT_LOGO"))} {(Lit_Other "=")} {(DQ (true))} {(Lit_Other "]")} ) terminator: ) ] action: [ (C {(sed)} {(-i)} {(DQ ("s/.*CONFIG_LOGO_LINUX_CLUT224.*/CONFIG_LOGO_LINUX_CLUT224=y/"))} {(.config)} ) (C {(echo)} {(DQ ("Boot logo is enabled."))}) ] spids: [-1 424] ) ] else_action: [ (C {(sed)} {(-i)} { (DQ ("s/.*CONFIG_LOGO_LINUX_CLUT224.*/") (EscapedLiteralPart token:) ("# CONFIG_LOGO_LINUX_CLUT224 is not set/") ) } {(.config)} ) (C {(echo)} {(DQ ("Boot logo is disabled."))}) ] spids: [445 468] ) (C {(sed)} {(-i)} { (DQ ("s/^CONFIG_DEBUG_KERNEL.*/") (EscapedLiteralPart token:) ("# CONFIG_DEBUG_KERNEL is not set/") ) } {(.config)} ) (C {(sed)} {(-i)} {(DQ ("s/.*CONFIG_EFI_STUB.*/CONFIG_EFI_STUB=y/"))} {(.config)}) (C {(grep)} {(-q)} {(DQ ("CONFIG_X86_32=y"))} {(.config)}) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {($ VSub_QMark "$?")} {(Lit_Other "=")} {(1)} {(Lit_Other "]")}) terminator: ) ] action: [ (SimpleCommand words: [{(echo)} {(DQ ("CONFIG_EFI_MIXED=y"))}] redirects: [(Redir op_id:Redir_DGreat fd:-1 arg_word:{(.config)} spids:[558])] ) ] spids: [-1 545] ) ] spids: [-1 563] ) ] spids: [253 565] ) (C {(echo)} {(DQ ("Building kernel..."))}) (C {(make)} {(Lit_VarLike "CFLAGS=") (DQ ("-Os -s -fno-stack-protector -U_FORTIFY_SOURCE"))} {(bzImage)} {(-j)} {($ VSub_Name "$NUM_JOBS")} ) (C {(cp)} {(arch/x86/boot/bzImage)} {($ VSub_Name "$SRC_DIR") (/work/kernel/kernel_installed/kernel)} ) (C {(echo)} {(DQ ("Generating kernel headers..."))}) (C {(make)} {(Lit_VarLike "INSTALL_HDR_PATH=") ($ VSub_Name "$SRC_DIR") (/work/kernel/kernel_installed)} {(headers_install)} {(-j)} {($ VSub_Name "$NUM_JOBS")} ) (C {(cd)} {($ VSub_Name "$SRC_DIR")}) (C {(echo)} {(DQ ("*** BUILD KERNEL END ***"))}) ] )