build_my_kernel.sh 3.43 KB
Newer Older
psachin's avatar
psachin committed
1
#!/usr/bin/env bash
psachin's avatar
psachin committed
2 3
# Script to compile the kernel
# Usage: bash build_my_kernel.sh
4
# and follow the instructions
psachin's avatar
psachin committed
5

psachin's avatar
psachin committed
6 7
# ---- You need to configure these variables ----
# Default location of the (working)config file.
8 9
DEFAULT_CONFIG_FILE=$(pwd)/config-bleeding-edge
# Partition where root filesystem resides.
psachin's avatar
psachin committed
10
ROOTFS_DEV="sda2"
psachin's avatar
psachin committed
11
# -----------------------------------------------
psachin's avatar
psachin committed
12

psachin's avatar
psachin committed
13
# TODO: When CONFIG_LOCALVERSION_AUTO=y, the kernel module dir:
psachin's avatar
psachin committed
14
# /lib/modules/KERNEL_VERSION-(EXTRA_STRING) has extra string.
psachin's avatar
psachin committed
15
# mkinitrd requires this KERNEL_VERSION-(EXTRA_STRING) to create
psachin's avatar
psachin committed
16
# initrd compressed file.
17 18
# DONE: Defined KERNEL_RELEASE
# Note: `make kernelrelease` requires .config
psachin's avatar
psachin committed
19 20 21
KERNEL_VERSION=$(make kernelversion)
DATE_TIME=$(date +"%d-%b-%Y_%T")

psachin's avatar
psachin committed
22 23
KERNEL_SRC=$(pwd)
BUILD_PATH=$(dirname "$KERNEL_SRC")/build-$KERNEL_VERSION
psachin's avatar
psachin committed
24 25 26 27 28 29

echo $KERNEL_SRC
echo $BUILD_PATH

if [ -d "$KERNEL_SRC" ];
then
psachin's avatar
psachin committed
30
    echo "Kernel source directory: OK"
psachin's avatar
psachin committed
31 32 33 34 35 36 37
else
    echo "$KERNEL_SRC does not exist"
    exit 1
fi

if [ -d "$BUILD_PATH" ];
then
psachin's avatar
psachin committed
38
    echo "Build directory: OK"
psachin's avatar
psachin committed
39 40 41 42 43 44 45 46
else
    echo "$BUILD_PATH does not exist"
    echo "Creating build directory..$BUILD_PATH"
    mkdir -p $BUILD_PATH
fi

echo "Cleaning kernel source.."
pushd $KERNEL_SRC
psachin's avatar
psachin committed
47 48 49 50
if [ -f .config ];
then
    mv .config config-current
    CONFIG_TRUE=1
51 52
else
    CONFIG_TRUE=0
psachin's avatar
psachin committed
53 54
fi

psachin's avatar
psachin committed
55
make distclean; make mrproper; make clean
psachin's avatar
psachin committed
56
echo "Copy reference config file $DEFAULT_CONFIG_FILE to start with.."
57
if [ -f $DEFAULT_CONFIG_FILE ];
psachin's avatar
psachin committed
58
then
psachin's avatar
psachin committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    # TODO: While
    read -p "Do you want to copy default config file? (y/n)" ANS
    case $ANS in
	[Yy] ) cp -v $DEFAULT_CONFIG_FILE .config
	       ;;
	[Nn] ) echo "Will use default config file available'"
	       ;;
        *) echo "Please answer y/n."
	      exit 1
	      ;;
    esac
fi

if [ ! -f .config ];
then
    if [ "${CONFIG_TRUE}" -eq 1 ];
    then
	mv config-current .config
    else
78
	echo "You do not have config file, please run 'make localmodconfig'"
psachin's avatar
psachin committed
79 80 81
	echo "and start this script again"
	exit 1
    fi
psachin's avatar
psachin committed
82
fi
psachin's avatar
psachin committed
83 84

# want to configure?
psachin's avatar
psachin committed
85 86
# TODO: While
read -p "Do you want to configure the kernel? (y/n/q)" ANS
psachin's avatar
psachin committed
87 88 89 90 91 92
case $ANS in
    [Yy] ) make menuconfig
	   echo "Done configuring kernel.."
	   ;;
    [Nn] ) echo "Going with existing configuration.."
	   ;;
psachin's avatar
psachin committed
93
    [Qq] ) echo "Exit."
psachin's avatar
psachin committed
94 95
	   exit 0
	   ;;
psachin's avatar
psachin committed
96 97 98
	*) echo "Please answer y/n/q."
	   exit 1
	   ;;
psachin's avatar
psachin committed
99 100 101 102 103 104 105 106 107
esac

echo "Cleaning build directory.."
pushd $BUILD_PATH
rm -rf *; rm -rf .*

popd
# TODO: Backup this configuration file? to configs/
# custom name to config file or date-time stamp?
psachin's avatar
psachin committed
108
# TODO: Prompt to backup configuration file
psachin's avatar
psachin committed
109
# TODO: with custom name
psachin's avatar
psachin committed
110 111 112 113 114
mv -v .config $BUILD_PATH
make -j4 O=$BUILD_PATH
make modules O=$BUILD_PATH

pushd $KERNEL_SRC
psachin's avatar
psachin committed
115
echo "Installing kernel modules.."
psachin's avatar
psachin committed
116 117
make modules_install O=$BUILD_PATH

118 119
KERNEL_RELEASE=$(cat $BUILD_PATH/include/config/kernel.release 2> /dev/null)

psachin's avatar
psachin committed
120
echo "Creating initrd.."
121
mkinitrd -c -k $KERNEL_RELEASE -f ext4 -r /dev/${ROOTFS_DEV} -m ext4 -u -o /boot/initrd-$KERNEL_RELEASE.gz
psachin's avatar
psachin committed
122
echo "Installing kernel.."
psachin's avatar
psachin committed
123
make install O=$BUILD_PATH
psachin's avatar
psachin committed
124

psachin's avatar
psachin committed
125 126
if [ -f /boot/vmlinuz ];
then
127
    echo "Renaming kernel binary by release version"
psachin's avatar
psachin committed
128
    mv -v /boot/vmlinuz /boot/vmlinuz-$KERNEL_RELEASE
psachin's avatar
psachin committed
129 130 131 132 133
else
    echo "ERR: /boot/vmlinuz, not found, kernel installation must have failed!"
fi

# Check the existance
134
if [ -f /boot/vmlinuz-$KERNEL_RELEASE ];
psachin's avatar
psachin committed
135 136
then
    echo "Kernel installed sucessfully"
137
    echo "Check entry in /etc/lilo.conf and run 'lilo'"
psachin's avatar
psachin committed
138 139 140 141
else
    echo "ERR: Something went wrong!"
    echo "ERR: I don't find vmlinuz with kernel version you have compiled"
fi