#!/bin/sh
##########################################
# Linux build script for Puppy Linux #
##########################################
# the Linux version to build
LINUX_VERSION="2.6.32.41"
# the file name of the Linux sources tarball
LINUX_SOURCES_TARBALL="linux-2.6.32.41.tar.bz2"
# the Linux download mirror
LINUX_MIRROR="http://www.il.kernel.org/pub/linux/kernel/v2.6/longterm/v2.6.32"
# the Linux image and modules package name
LINUX_PACKAGE_NAME="linux_kernel"
# the Linux API headers package name
LINUX_HEADERS_PACKAGE_NAME="kernel_headers"
# the Linux sources package name
LINUX_SOURCES_PACKAGE_NAME="kernel_sources"
# the Aufs version to use
AUFS_VERSION="2.1"
# get the Linux major version number
LINUX_MAJOR_VERSION=`echo $LINUX_VERSION | cut -f 1-3 -d .`
# get the Linux release number
LINUX_RELEASE_NUMBER=`echo $LINUX_VERSION | cut -f 3 -d .`
# get the Linux minor version number
LINUX_MINOR_VERSION=`echo $LINUX_VERSION | cut -f 4 -d .`
# the package name suffix
PACKAGE_NAME_SUFFIX="${LINUX_MINOR_VERSION}-lts-libre-bfs"
# the Linux-libre scripts to use
LINUX_LIBRE_SCRIPTS="deblob-${LINUX_MAJOR_VERSION} deblob-check"
# the Linux-libre mirror to download the scripts from
LINUX_LIBRE_MIRROR="http://www.fsfla.org/svn/fsfla/software/linux-libre/scripts"
# get the build date
BUILD_DATE=`date +%d%m%Y`
# the absolute log file path
LOG_PATH=`pwd`/build.log
# delete old logs
[ -f "$LOG_PATH" ] && rm -f "$LOG_PATH"
# create directories for the output
mkdir -p dist/sources/{patches,vanilla}
# download the Linux sources tarball
if [ ! -f dist/sources/vanilla/$LINUX_SOURCES_TARBALL ]; then
echo "Downloading the Linux sources tarball"
wget -P dist/sources/vanilla $LINUX_MIRROR/$LINUX_SOURCES_TARBALL > "$LOG_PATH" 2>&1
if [ $? -ne 0 ]
then
echo "Error: failed to download the Linux sources tarball."
exit 1
fi
fi
# check whether the Linux-libre scripts are missing
download=0
for script in $LINUX_LIBRE_SCRIPTS
do
if [ ! -f dist/sources/vanilla/$script ]
then
download=1
break
fi
done
# if at least one script is missing, download it
if [ $download -eq 1 ]
then
echo "Downloading the Linux-libre deblob scripts"
for script in $LINUX_LIBRE_SCRIPTS
do
if [ ! -f dist/sources/vanilla/$script ]
then
wget -P dist/sources/vanilla $LINUX_LIBRE_MIRROR/$script >> build.log 2>&1
if [ $? -ne 0 ]
then
[ -f dist/sources/vanilla/$script ] && rm -f dist/sources/vanilla/$script
echo "Error: failed to download the $script script."
exit 1
fi
fi
done
fi
# get the full Aufs package name
AUFS_FULL_PACKAGE_NAME=aufs${AUFS_VERSION}-${LINUX_RELEASE_NUMBER}-git${BUILD_DATE}
# download the Aufs sources through git
if [ ! -f dist/sources/vanilla/aufs$AUFS_VERSION-$LINUX_RELEASE_NUMBER-git$BUILD_DATE.tar.bz2 ]
then
echo "Downloading the Aufs sources through git"
git clone http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-standalone.git $AUFS_FULL_PACKAGE_NAME>> "$LOG_PATH" 2>&1
if [ $? -ne 0 ]
then
echo "Error: failed to download the Aufs sources through git."
exit 1
fi
cd aufs$AUFS_VERSION-$LINUX_RELEASE_NUMBER-git$BUILD_DATE
git checkout origin/aufs$AUFS_VERSION-$LINUX_RELEASE_NUMBER >> "$LOG_PATH" 2>&1
if [ $? -ne 0 ]
then
echo "Error: failed to download the Aufs sources through git."
exit 1
fi
rm -rf .git
cd ..
echo "Creating the Aufs sources tarball"
tar -c $AUFS_FULL_PACKAGE_NAME| bzip2 -9 > dist/sources/vanilla/aufs$AUFS_VERSION-$LINUX_RELEASE_NUMBER-git$BUILD_DATE.tar.bz2
else
echo "Extracting the Aufs sources tarball"
tar xf dist/sources/vanilla/aufs$AUFS_VERSION-$LINUX_RELEASE_NUMBER-git$BUILD_DATE.tar.bz2 >> "$LOG_PATH" 2>&1
if [ $? -ne 0 ]
then
echo "Error: failed to extract the Aufs sources tarball."
exit 1
fi
fi
# patch Aufs
echo "Patching the Aufs sources"
patch -d $AUFS_FULL_PACKAGE_NAME -p1 < aufs-allow-sfs.patch >> "$LOG_PATH" 2>&1
if [ $? -ne 0 ]
then
echo "Error: failed to patch the Aufs sources."
exit 1
fi
cp aufs-allow-sfs.patch dist/sources/patches
# extract the Linux sources tarball
echo "Extracting the Linux sources tarball"
tar xf dist/sources/vanilla/$LINUX_SOURCES_TARBALL >> "$LOG_PATH" 2>&1
if [ $? -ne 0 ]
then
echo "Error: failed to extract the Linux sources tarball."
exit 1
fi
echo "Deblobing the Linux sources"
echo " creating a clean copy of the sources"
cp -r linux-$LINUX_VERSION linux-$LINUX_VERSION-orig
for script in $LINUX_LIBRE_SCRIPTS
do
chmod +x dist/sources/vanilla/$script
done
echo " deblobing"
cd linux-$LINUX_VERSION
../dist/sources/vanilla/deblob-$LINUX_MAJOR_VERSION > "$LOG_PATH" 2>&1
if [ $? -ne 0 ]
then
echo "Error: failed to deblob the Linux sources."
exit 1
fi
echo " creating a patch"
cd ..
diff -rupN linux-$LINUX_VERSION-orig linux-$LINUX_VERSION > dist/sources/patches/deblob.patch
rm -rf linux-%LINUX_VERSION-orig
mkdir dist/packages
cd linux-$LINUX_VERSION
echo "Cleaning the Linux sources"
make clean >> "$LOG_PATH" 2>&1
make mrproper >> "$LOG_PATH" 2>&1
echo "Creating the Linux headers package"
make headers_check >> "$LOG_PATH" 2>&1
LINUX_HEADERS_FULL_PACKAGE_NAME=${LINUX_HEADERS_PACKAGE_NAME}-${LINUX_MAJOR_VERSION}-${PACKAGE_NAME_SUFFIX}
make INSTALL_HDR_PATH=$LINUX_HEADERS_FULL_PACKAGE_NAME headers_install >> "$LOG_PATH" 2>&1
find $LINUX_HEADERS_FULL_PACKAGE_NAME/include \( -name .install -o -name ..install.cmd \) -delete
mv $LINUX_HEADERS_FULL_PACKAGE_NAME ../dist/packages
# patch the Linux sources with the Aufs patches and add the file system code itself
echo "Adding Aufs to the Linux sources"
for i in kbuild base standalone; do
patch -p1 < ../aufs$AUFS_VERSION-$LINUX_RELEASE_NUMBER-git$BUILD_DATE/aufs2-$i.patch >> "$LOG_PATH" 2>&1
if [ $? -ne 0 ]
then
echo "Error: failed to add Aufs to the Linux sources."
exit 1
fi
done
cp -r ../aufs$AUFS_VERSION-$LINUX_RELEASE_NUMBER-git$BUILD_DATE/{fs,Documentation} .
cp -r ../aufs$AUFS_VERSION-$LINUX_RELEASE_NUMBER-git$BUILD_DATE/include/linux/aufs_type.h include/linux
cat ../aufs$AUFS_VERSION-$LINUX_RELEASE_NUMBER-git$BUILD_DATE/include/linux/Kbuild >> include/linux/Kbuild
echo "Resetting the minor version number"
cp Makefile Makefile-orig
sed -i s/'^EXTRAVERSION = .*$'/'EXTRAVERSION = '/ Makefile
diff -up Makefile-orig Makefile > ../dist/sources/patches/extra-version.patch
rm Makefile-orig
echo "Reducing the number of consoles"
cp kernel/printk.c kernel/printk.c-orig
sed -i s/'#define MAX_CMDLINECONSOLES 8'/'#define MAX_CMDLINECONSOLES 5'/ kernel/printk.c
diff -up kernel/printk.c-orig kernel/printk.c > ../dist/sources/patches/less-consoles.patch
echo "Reducing the verbosity level"
cp -f kernel/printk.c kernel/printk.c-orig
sed -i s/'#define DEFAULT_CONSOLE_LOGLEVEL 7 \/\* anything MORE serious than KERN_DEBUG \*\/'/'#define DEFAULT_CONSOLE_LOGLEVEL 3 \/\* anything MORE serious than KERN_ERR \*\/'/ kernel/printk.c
diff -up kernel/printk.c-orig kernel/printk.c > ../dist/sources/patches/lower-verbosity.patch
for patch in ../patches/*
do
echo "Applying $patch"
patch -p1 < $patch >> "$LOG_PATH" 2>&1
if [ $? -ne 0 ]
then
echo "Error: failed to apply $patch on the Linux sources."
exit 1
fi
cp $patch ../dist/sources/patches
done
find . -name '*.orig' -delete
find . -name '*.rej' -delete
find . -name '*~' -delete
echo "Building Linux"
cp ../DOTconfig .config
make bzImage modules >> "$LOG_PATH" 2>&1
if [[ ! -f arch/x86/boot/bzImage || ! -f System.map ]]; then
echo "Error: failed to build Linux."
exit 1
fi
cp .config ../dist/sources/DOTconfig-$LINUX_VERSION-$BUILD_DATE
echo "Creating the kernel package"
LINUX_FULL_PACKAGE_NAME=${LINUX_PACKAGE_NAME}-${LINUX_RELEASE_NUMBER}-${PACKAGE_NAME_SUFFIX}
make INSTALL_MOD_PATH=$LINUX_FULL_PACKAGE_NAME modules_install
rm $LINUX_FULL_PACKAGE_NAME/lib/modules/$LINUX_MAJOR_VERSION/{build,source}
ln -s $LINUX_MAJOR_VERSION $LINUX_FULL_PACKAGE_NAME/lib/modules/$kernel_version
mkdir $LINUX_FULL_PACKAGE_NAME/boot
cp `find arch -name bzImage -type f` $LINUX_FULL_PACKAGE_NAME/boot/vmlinuz
cp System.map $LINUX_FULL_PACKAGE_NAME/boot
mv $LINUX_FULL_PACKAGE_NAME ../dist/packages
echo "Cleaning the kernel sources"
make clean >> "$LOG_PATH" 2>&1
make prepare >> "$LOG_PATH" 2>&1
cd ..
echo "Creating a kernel sources SFS"
LINUX_SOURCES_FULL_PACKAGE_NAME=${LINUX_SOURCES_PACKAGE_NAME}-${LINUX_RELEASE_NUMBER}-${PACKAGE_NAME_SUFFIX}
mkdir -p $LINUX_SOURCES_FULL_PACKAGE_NAME/usr/src
mv linux-$kernel_version $LINUX_SOURCES_FULL_PACKAGE_NAME/usr/src/linux
mkdir -p $LINUX_SOURCES_FULL_PACKAGE_NAME/lib/modules/$LINUX_MAJOR_VERSION
ln -s /usr/src/linux $LINUX_SOURCES_FULL_PACKAGE_NAME/lib/modules/$LINUX_MAJOR_VERSION/build
ln -s /usr/src/linux $LINUX_SOURCES_FULL_PACKAGE_NAME/lib/modules/$LINUX_MAJOR_VERSION/source
mksquashfs $LINUX_SOURCES_FULL_PACKAGE_NAME dist/sources/$LINUX_SOURCES_FULL_PACKAGE_NAME.sfs
echo "Compressing the log"
bzip2 -9 build.log
cp build.log.bz2 dist/sources
echo "Done!"
aplay /usr/share/sounds/2barks.au