Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

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!

Tuesday, May 1, 2012

How to turn my PC off at night?

Hey guys,
A month ago my brother asked me to recommend him a software for automatically shutdown the PC,
I refered him a software called Task Scheduler (for Windows 7), which has a message notifier before  shut down.
I decided to search for a similliar tool for my linux box, but couldn't find any tool with a msgbox notifier.
So eventually I decided to write one in bash, I added an entry to the crontab table, which will execute the script each night at 02:00 AM.
In case I'm working on the PC at night, there is a option to postpone the shutdown to a later hour.
I used Zenity for adding a nice touch of GUI, so it would be more user friendly.
A dialog box pops up on the screen and shows via progress bar how much time is left before shutdown, here is a screen shot:



Here is the script:

#!/bin/bash
if [ -z "$1" ]; then
    echo "Error: Please enter the duration in minutes..."
    exit
fi

scriptPath=`pwd`
# converting minutes into seconds
duration=$(($1*60));      
timeToShutDown=$((`date +%s` + $duration))
steps=100;
userName=`whoami`

counter=1;

while [  $(($counter<= 100 )) ]    
 do
  echo $counter
 timeLeft=$(($timeToShutDown-`date +%s`))
 echo "#Hi $userName, the PC would get turned off in $timeLeft Seconds"
 sleep  `echo "scale=2; $duration/$steps" | bc`
 counter=$((counter+1));
 done | zenity --display=:0.0 --width 600 --progress --title="Turning off PC at "`date +"%H:%M:%S[Seconds]"` 
 --text="Turning off PC at ..." --auto-close

#        Value 0  The have passed so shutting down engines
#   Value 1  User pressed Either cancel or the exit X, so we should postpone the shutdown
if [ "$?" = 1 ];
then
   echo "Schedualing a task in 2 minutes..."
   echo "$scriptPath/shutDownByOrder 20" | at now + 120 minutes
else
 echo "Shutting down!!!"
 sudo shutdown -h +1 "turning off your PC in 1 minute!!"
fi


Try it on your linux box and Enjoy! ;-)

About