Added sample 'common.sh' in the main sources. The kernel download process has been rewritten to use the functionality from 'common.sh'. The whole project should adopt functional structure where it makes sense (e.g. the common download proces in most places).

This commit is contained in:
Ivan Davidov 2017-12-06 03:07:21 +02:00
parent 93ee4c794d
commit f8128e83fb
3 changed files with 90 additions and 49 deletions

View File

@ -2,43 +2,24 @@
set -e set -e
# Load common properties and functions in the current script.
. ./common.sh
echo "*** GET KERNEL BEGIN ***" echo "*** GET KERNEL BEGIN ***"
SRC_DIR=$(pwd) # Read the 'KERNEL_SOURCE_URL' property from '.config'.
DOWNLOAD_URL=`read_property KERNEL_SOURCE_URL`
# Grab everything after the '=' character.
DOWNLOAD_URL=$(grep -i ^KERNEL_SOURCE_URL .config | cut -f2 -d'=')
# Grab everything after the last '/' character. # Grab everything after the last '/' character.
ARCHIVE_FILE=${DOWNLOAD_URL##*/} ARCHIVE_FILE=${DOWNLOAD_URL##*/}
# Read the 'USE_LOCAL_SOURCE' property from '.config' # Download kernel source archive in the 'source' directory.
USE_LOCAL_SOURCE="$(grep -i ^USE_LOCAL_SOURCE .config | cut -f2 -d'=')" download_source $DOWNLOAD_URL $SOURCE_DIR/$ARCHIVE_FILE
if [ "$USE_LOCAL_SOURCE" = "true" -a ! -f $SRC_DIR/source/$ARCHIVE_FILE ] ; then # Extract the kernel sources in the 'work/kernel' directory.
echo "Source bundle $SRC_DIR/source/$ARCHIVE_FILE is missing and will be downloaded." extract_source $SOURCE_DIR/$ARCHIVE_FILE kernel
USE_LOCAL_SOURCE="false"
fi
cd source
if [ ! "$USE_LOCAL_SOURCE" = "true" ] ; then
# Downloading kernel source bundle file. The '-c' option allows the download to resume.
echo "Downloading kernel source bundle from $DOWNLOAD_URL"
wget -c $DOWNLOAD_URL
else
echo "Using local kernel source bundle $SRC_DIR/source/$ARCHIVE_FILE"
fi
# Delete folder with previously extracted kernel.
echo "Removing kernel work area. This may take a while."
rm -rf ../work/kernel
mkdir ../work/kernel
# Extract kernel to folder 'work/kernel'.
# Full path will be something like 'work/kernel/linux-4.4.6'.
tar -xvf $ARCHIVE_FILE -C ../work/kernel
# Just in case we go back to the main MLL source folder.
cd $SRC_DIR cd $SRC_DIR
echo "*** GET KERNEL END ***" echo "*** GET KERNEL END ***"

View File

@ -2,22 +2,11 @@
set -e set -e
# Load common properties and functions in the current script.
. ./common.sh
echo "*** BUILD KERNEL BEGIN ***" echo "*** BUILD KERNEL BEGIN ***"
SRC_DIR=$(pwd)
# Read the 'JOB_FACTOR' property from '.config'
JOB_FACTOR="$(grep -i ^JOB_FACTOR .config | cut -f2 -d'=')"
# Read the 'CFLAGS' property from '.config'
CFLAGS="$(grep -i ^CFLAGS .config | cut -f2 -d'=')"
# Find the number of available CPU cores.
NUM_CORES=$(grep ^processor /proc/cpuinfo | wc -l)
# Calculate the number of 'make' jobs to be used later.
NUM_JOBS=$((NUM_CORES * JOB_FACTOR))
cd work/kernel cd work/kernel
# Prepare the kernel install area. # Prepare the kernel install area.
@ -32,16 +21,16 @@ echo "Preparing kernel work area."
make mrproper -j $NUM_JOBS make mrproper -j $NUM_JOBS
# Read the 'USE_PREDEFINED_KERNEL_CONFIG' property from '.config' # Read the 'USE_PREDEFINED_KERNEL_CONFIG' property from '.config'
USE_PREDEFINED_KERNEL_CONFIG="$(grep -i ^USE_PREDEFINED_KERNEL_CONFIG $SRC_DIR/.config | cut -f2 -d'=')" USE_PREDEFINED_KERNEL_CONFIG=`read_property USE_PREDEFINED_KERNEL_CONFIG`
if [ "$USE_PREDEFINED_KERNEL_CONFIG" = "true" -a ! -f $SRC_DIR/minimal_config/kernel.config ] ; then if [ "$USE_PREDEFINED_KERNEL_CONFIG" = "true" -a ! -f $SRC_DIR/minimal_config/kernel.config ] ; then
echo "Config file $SRC_DIR/minimal_config/kernel.config does not exist." echo "Config file '$SRC_DIR/minimal_config/kernel.config' does not exist."
USE_PREDEFINED_KERNEL_CONFIG="false" USE_PREDEFINED_KERNEL_CONFIG=false
fi fi
if [ "$USE_PREDEFINED_KERNEL_CONFIG" = "true" ] ; then if [ "$USE_PREDEFINED_KERNEL_CONFIG" = "true" ] ; then
# Use predefined configuration file for the kernel. # Use predefined configuration file for the kernel.
echo "Using config file $SRC_DIR/minimal_config/kernel.config" echo "Using config file '$SRC_DIR/minimal_config/kernel.config'."
cp -f $SRC_DIR/minimal_config/kernel.config .config cp -f $SRC_DIR/minimal_config/kernel.config .config
else else
# Create default configuration file for the kernel. # Create default configuration file for the kernel.
@ -70,7 +59,7 @@ else
sed -i "s/.*CONFIG_FB_VESA.*/CONFIG_FB_VESA=y/" .config sed -i "s/.*CONFIG_FB_VESA.*/CONFIG_FB_VESA=y/" .config
# Read the 'USE_BOOT_LOGO' property from '.config' # Read the 'USE_BOOT_LOGO' property from '.config'
USE_BOOT_LOGO="$(grep -i ^USE_BOOT_LOGO $SRC_DIR/.config | cut -f2 -d'=')" USE_BOOT_LOGO=`read_property USE_BOOT_LOGO`
if [ "$USE_BOOT_LOGO" = "true" ] ; then if [ "$USE_BOOT_LOGO" = "true" ] ; then
sed -i "s/.*CONFIG_LOGO_LINUX_CLUT224.*/CONFIG_LOGO_LINUX_CLUT224=y/" .config sed -i "s/.*CONFIG_LOGO_LINUX_CLUT224.*/CONFIG_LOGO_LINUX_CLUT224=y/" .config
@ -86,7 +75,7 @@ else
# Enable the EFI stub # Enable the EFI stub
sed -i "s/.*CONFIG_EFI_STUB.*/CONFIG_EFI_STUB=y/" .config sed -i "s/.*CONFIG_EFI_STUB.*/CONFIG_EFI_STUB=y/" .config
# Request that the firmware clear the contents of RAM after a reboot (4.14+). # Request that the firmware clear the contents of RAM after reboot (4.14+).
echo "CONFIG_RESET_ATTACK_MITIGATION=y" >> .config echo "CONFIG_RESET_ATTACK_MITIGATION=y" >> .config
# Disable Apple Properties (Useful for Macs but useless in general) # Disable Apple Properties (Useful for Macs but useless in general)

71
src/common.sh Executable file
View File

@ -0,0 +1,71 @@
#!/bin/sh
set -e
SRC_DIR=`realpath --no-symlinks $PWD`
SOURCE_DIR=$SRC_DIR/source
WORK_DIR=$SRC_DIR/work
CONFIG=$SRC_DIR/.config
# This function reads property from the main '.config' file.
#
# Using () instead of {} for the function body is a POSIX
# compliant way to execute subshell and as consequence all
# variables in the function will become effectively in local
# scope. Note that the 'local' keyword is supported by most
# shells but it is not POSIX compliant.
read_property() (
# The property we are looking for.
prop_name=$1
# The value of the property set initially to empty string.
prop_value=
if [ ! "$prop_name" = "" ] ; then
# Search in the main '.config' file.
prop_value=`grep -i ^${prop_name}= $CONFIG | cut -f2- -d'=' | xargs`
fi
echo $prop_value
)
# Read commonly used properties from the main '.config' file.
JOB_FACTOR=`read_property JOB_FACTOR`
CFLAGS=`read_property CFLAGS`
NUM_CORES=$(grep ^processor /proc/cpuinfo | wc -l)
# Calculate the number of 'make' jobs to be used later.
NUM_JOBS=$((NUM_CORES * JOB_FACTOR))
download_source() (
url=$1 # Download from this URL.
file=$2 # Save the resource in this file.
local=`read_property USE_LOCAL_SOURCE`
if [ "$local" = "true" -a ! -f $file ] ; then
echo "Source file '$file' is missing and will be downloaded."
local=false
fi
if [ ! "$local" = "true" ] ; then
echo "Downloading source file from '$url'."
echo "Saving source file in '$file'".
wget -O $file -c $url
else
echo "Using local source file '$file'."
fi
)
extract_source() (
file=$1
name=$2
# Delete folder with previously extracted source.
echo "Removing '$name' work area. This may take a while."
rm -rf $WORK_DIR/$name
mkdir $WORK_DIR/$name
# Extract source to folder 'work/$source'.
tar -xvf $file -C $WORK_DIR/$name
)