Tuesday, May 20, 2014

Removing Linux kernel images in a snap of finger!

Recently I have been writing and modifying some code in the Linux kernel tree,
I have been using the  configuration file: .config, which was generated via localmodconfig.
probably you're asking yourself what is localmodconfig?

I'll elaborate about it, it's a tool which generates the .config file.
The generated .config file is quite slim compare to the default distribution kernel's configuration,
since many unnecessary kernel modules are not getting compiled during compilation phase (make modules_install),
so it's compilation state is much shorter in time.
During this installation routine, few basic steps are taken:


  1. Copies the final image to the folder /boot .  You can easily recognize the file since the name consists the prefix "vm-linuz-" following with the Kernel-version name.
  2. Copies the compiled kernel modules to /lib/modules and other necessary stuff while working with modules (module dependency trees, etc.)
  3. Modifies the /boot/grub/grub.cfg, so now your fresh linux kernel image entry was added to the GRUB menu. Check it while rebooting your system.  

Some times it occurred to me, that I need to get rid of those old kernel images which are installed on my system.
Doing it manually annoys me, So I decided to write a script which does the work for me.


take a look, see below:


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
#!/bin/bash

option=1
names=""
cd /boot
clear
echo "The kernels which are installed on your system are:"
for file in ./vmlinuz-*
do
 temp_kernel_name=${file:2:`expr length $file`-2}
 temp_kernel_name=`echo ${temp_kernel_name} | cut -d "-" -f 2-7`
 echo "[${option}] " ${temp_kernel_name}
 names=${names}" "${temp_kernel_name}
 let option=option+1
done
echo "[${option}] Exit"

exit_code=option

echo "Pick the linux kernel you would like to remove from your system?"
read user_pick

if [ ${user_pick}==${exit_code} ]; then
 echo "Exiting... Bye!" 
 exit -1
fi

kernel_name_to_remove=`echo ${names} | cut -d " "  -f ${user_pick}` # f option in cut commad holds the number of field after the space delimeter
kernel_version=`echo ${kernel_name_to_remove} | cut -d "-" -f 1-2`

if [ `uname -r` == ${kernel_name_to_remove} ]; then
 echo "attention: Can't remove kernel, the current kernel is running on your system!"
 echo "please check your request, exiting the script..."
 exit -1
fi

echo "Are you sure you would like to remove kernel: " ${kernel_name_to_remove} "? [y/n]"
read ans

if [ "n" == ${ans} ]; then
 echo "please re-think about it, exiting the script..."
 exit -1;
fi

echo "Starting to remove kernel: " ${kernel_name_to_remove} 
echo "Kernel version: " ${kernel_version}

mkdir -p /boot/removed_kernel_images

# Step 1
for file in ./*${kernel_version}*
do
 temp_file_name=${file:2:`expr length $file`-2}
 echo "removing file: " ${temp_file_name} 
 mv ${file} /boot/removed_kernel_images
done

echo "Finished step 1!"

#Step 2
mkdir -p /lib/modules/removed_kernel_modules
mv /lib/modules/${kernel_name_to_remove} to /lib/modules/removed_kernel_modules
echo "Finished step 2!"

#Step 3 modifying: /boot/grub/grub.cfg
mkdir -p /boot/grub/grub_conf_files_removed

if [[ ! -f /boot/grub/grub_conf_files_removed/grub`date +"_%m_%d_%Y_%H_%M_%S"`.cfg ]]; then
 cp /boot/grub/grub.cfg /boot/grub/grub_conf_files_removed/grub`date +"_%m_%d_%Y_%H_%M_%S"`.cfg #backing up the file
fi

cd /boot/grub
res=`grep -n ${kernel_name_to_remove} /boot/grub/grub.cfg | cut -d : -f 1`
echo "res = " ${res}
echo ""
echo ""
echo ""
echo ""

number_of_matches=`echo ${res} | wc -w`
#echo "number_of_matches = " ${number_of_matches}
last_line=`echo ${res} | cut -d " " -f ${number_of_matches}`
let last_line=last_line+1 #removing the last bracket too
start_line=`echo ${res} | cut -d " " -f 1`

echo "start_line = " ${start_line}
echo "last_line = " ${last_line}

numbers=`seq ${start_line} 1 ${last_line}`

#echo "numbers = " ${numbers}

for row_number in ${numbers}
do
 sed -i ${rownumber}" d" /boot/grub/grub.cfg
done

echo "Finished step 3!"

#Step 4:
update-grub
echo "Finished step 4!"

cd -

echo "Finished, Bye :-)"

Fill free to grab the script in my GitHub repository: 

https://github.com/codingforpleasure

So that's all for today, next time I'll be talking about exciting concept of device trees, See you till then!

No comments:

Post a Comment

About