linux 内核编译脚本

上一次编译内核已经是三年前倒腾 LFS 的事了。这两天心血来潮编译了最新的内核,写个脚本备份一下。这个脚本在 debian 6.0 下成功编译 3.2.0-rc5 并配置好 grub,在别的系统上可能不好使,所以这里更多的是一个过程记录。注释掉的部分是 grub 相关的,因为经实验发现这个工作 update-grub 已经做了。

#!/bin/bash

function display_usage()
{
   echo "Usage: $0 [install src | uninstall version]" >&2
}

if [ $# -ne 2 ]; then
   display_usage
   exit 1
fi

if [ $1 = "install" ]; then
   cd $2
   if [ $? -ne 0 ]; then
      exit 1
   fi

   if ! [ -f Makefile ]; then
      echo "cannot get kernel version." >&2
      exit 1
   fi

   KERNEL_VERSION=`sed -n '1'p Makefile | awk '{print$3}'`.`sed -n '2'p Makefile | awk '{print$3}'`.`sed -n '3'p Makefile | awk '{print$3}'``sed -n '4'p Makefile | awk '{print$3}'`
   let nr_thread=`cat /proc/cpuinfo | grep processor | wc -l`*2

   make menuconfig
   if [ $? -ne 0 ]; then
      exit 1
   fi

   echo "-------------------- make begin --------------------"

   make -j $nr_thread
   if [ $? -ne 0 ]; then
      exit 1
   fi

   make -j $nr_thread modules
   if [ $? -ne 0 ]; then
      exit 1
   fi

   echo "--------------------- make end ---------------------"
   echo "-------------------- install begin --------------------"

   make modules_install
   if [ $? -ne 0 ]; then
      exit 1
   fi

   make install
   if [ $? -ne 0 ]; then
      exit 1
   fi

   echo "--------------------- install end ---------------------"
   echo "-------------------- update begin --------------------"

   cd /boot
   mkinitramfs -o initrd.img-$KERNEL_VERSION $KERNEL_VERSION
   if [ $? -ne 0 ]; then
      exit 1
   fi

   #matchline=(`sed -n '/menuentry.*'$KERNEL_VERSION'.*/'= /boot/grub/grub.cfg`)
   #let nr_line=${#matchline[@]}
   #if [ $nr_line -eq 0 ]; then
   #   echo "cannot find kernel version: $KERNEL_VERSION." >&2
   #   exit 1
   #fi

   #awk 'BEGIN{i = 0} {if ($0 ~ /^}/) {if (i < '$nr_line' && NR > '${matchline['i']}') {printf "\tinitrd\t%s\n}\n", "/boot/initrd.img-'$KERNEL_VERSION'"; ++i} else print $0} else print $0}' /boot/grub/grub.cfg > /tmp/tmpgrub.cfg
   #mv /tmp/tmpgrub.cfg /boot/grub/grub.cfg

   update-grub

   echo "--------------------- update end ---------------------"
elif [ $1 = "uninstall" ]; then
   rm -rf /lib/modules/$2
   rm /boot/*$2*

   update-grub
else
   display_usage
fi

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注