• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

need scripting help (add a line of code in middle of file)

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

mbentley

Gloriously Lead, Overclockix Chief Architect
Joined
Sep 26, 2002
Location
Indianapolis, IN
ok, i've been working on a script for my debian box... basically what the script does is it automatically runs all of the commands necessary to install a new kernel (with some slight user intervention of course). here is what i have so far:

Code:
#!/bin/sh
# kernel update script
# created by:		matt bentley
# creation date:		09/10/2006	2:01 AM
# last update:		09/10/2006	9:41 AM

# usage:  run script in the form:  './kernel_update 2.6.16.20' where 2.6.16.20 is the kernel you wish to use


if [ -z $1 ]; then
	echo "error!  please specify which kernel version you which to use"
	echo "(example: './kernel_update 2.6.16.20')"
	
else
	
	# install required debian packages from repository
	echo "---INSTALLING NECESSARY PROGRAMS---"
	sleep 3
	apt-get install kernel-package ncurses-dev fakeroot wget bzip2 module-init-tools initrd-tools procps
	
	# change pwd to /usr/src directory
	cd /usr/src
	
	# remove previous link to linux source
	echo "---REMOVING PREVIOUS SOURCE LINKS---"
	sleep 3
	rm -r linux
	
	# remove any old files from failed build
	echo "---REMOVING OLD FAILED BUILD FILES---"
	sleep 3
	rm -r linux-$1
	
	if [ -e /usr/src/linux-$1.tar.bz2  ]; then
		echo "---NO DOWNLOAD IS NECESSARY, PROPER SOURCE FOUND---"
		sleep 3
		echo "---EXTRACTING SOURCE---"
		sleep 3
		tar -xjf linux-$1.tar.bz2
			
	else
		echo "---DOWNLOAD REQUIRED, DOWNLOADING PROPER SOURCE---"
		sleep 3
		wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-$1.tar.bz2
		echo "---EXTRACTING SOURCE---"
		sleep 3
		tar -xjf linux-$1.tar.bz2
	
	fi

	# create symbolic link
	ln -s linux-$1 linux

	# change pwd to /boot directory
	cd /boot

	# copy old config from current kernel
	echo "---COPYING CONFIG FROM CURRENT KERNEL---"
	sleep 3
	cp config-$(uname -r) /usr/src/linux/.config

	# change pwd to /usr/src/linux
	cd /usr/src/linux

	# build menuconfig
	echo "---BUILDING MENUCONFIG---"
	sleep 3
	make menuconfig

	# clean
	echo "---CLEANING BUILD FILES---"
	sleep 3
	make-kpkg clean

	# create .deb file
	echo "---CREATING .DEB FILE---"
	sleep 3
	fakeroot make-kpkg --revision=custom.1.0 kernel_image

	# change pwd to /usr/src
	cd /usr/src
	
	# install .deb file
	echo "---INSTALLING .DEB KERNEL FILE---"
	sleep 3
	dpkg -i kernel-image-$1_custom.1.0_i386.deb

	# change pwd to /boot
	cd /boot

	# create initrd.img
	echo "---CREATING INITRD.IMG FOR NEW KERNEL---"
	sleep 3
	mkinitrd -o /boot/initrd.img-$1 $1

	# reminder to add initrd section to grub config
	echo "don't forget to add initrd section to grub config!"
	sleep 3
	
	# change pwd to /boot/grub
	cd /boot/grub
	
	# starting vi for menu.lst
	echo "---STARTING VI EDITING MENU.LST---"
	sleep 3
	vi menu.lst
fi


now for the part where i am clueless: i want to make it so the script file (instead of starting vi to edit menu.lst) will automatically edit /boot/grub/menu.lst and make it search until it finds the line:
kernel /boot/vmlinuz-$1 root=/dev/hdc1 ro

and adds:
initrd /boot/initrd.img-$1

right after it. that will totally automate the whole process of creating a new kernel (with an exception of configuring the kernel in this case since that sort of defeats the purpose of using the latest kernel if you don't enable new options)
 
dpkg -i kernel-image should automatically add the kernel to your boot menu.

And there is almost never any reason to have initrd in custom kernels. Distro kernels need it since they need to include every driver and the kitchensink, something custom kernels usually don't or there is no point for a custom kernel in the first place.
 
i haven't been able to find a way to make a kernel in debian without one... i don't know if i am doing something wrong but i have spent all day trying to get it to work without one...
 
You probably want to use sed in order to replace the line in the file with the same line + initrd, there is a huge number of ways to do stuff like that.
 
brakezone said:
You probably want to use sed in order to replace the line in the file with the same line + initrd, there is a huge number of ways to do stuff like that.


yeah, someone in IRC mentioned sed... i was toying with it the other day but i was a bit tired and trying to figure out the syntax for what i wanted it to do wasn't working when i wanted to sleep... maybe when i am a little more awake will it give it another shot...
 
The important part about krenels without initrd is to have a few modules built in and not as a module: Drivers for harddisk controller where you boot from, filesystem driver for your root and boot partition and network card drivers. Those are the 3 I always compile in. That's why you never use the .config file provided by your distro when making a custom kernel. besides, without a custom .config there is not really a point in compiling your own kernel anyways. Cause all you have then is a kernel which is the same as the distro kernel. What's the point of doing it yourself then, it's just useless work.
 
klingens said:
The important part about krenels without initrd is to have a few modules built in and not as a module: Drivers for harddisk controller where you boot from, filesystem driver for your root and boot partition and network card drivers. Those are the 3 I always compile in. That's why you never use the .config file provided by your distro when making a custom kernel. besides, without a custom .config there is not really a point in compiling your own kernel anyways. Cause all you have then is a kernel which is the same as the distro kernel. What's the point of doing it yourself then, it's just useless work.


ah i see... makes more sense now. i have stripped the config of practically anything (at least driver wise) that i was not using according to my lsmod output... i guess i will see how that goes when the kernel finished compiling... i just hope that i selected the correct ide controller drivers :)
 

Attachments

  • lsmod_output.txt
    3.8 KB · Views: 63
Last edited:
hmm... it seems to have problems where it does this with my kernel using an initrd:

"VFS: Mounted root (cramfs filesystem) read only."

it then gives me a kernel panic when using my custom kernel...
 
mbentley said:
ah i see... makes more sense now. i have stripped the config of practically anything (at least driver wise) that i was not using according to my lsmod output... i guess i will see how that goes when the kernel finished compiling... i just hope that i selected the correct ide controller drivers :)
Umm... lsmod only shows a module as used if it is used by another module, not if it is used by your system. When compiling the kernel, just pick the drivers you need for your system. If you need it to boot or if you use it every time, compile it in to the kernel. If it is only used occasionally, compile it as a module.
 
it would appear that my problem is selecting the correct ide driver... i selected all of the ide drivers listed and compiled them in the kernel and it works great. now to figure out which one i really want for:

0000:00:1f.1 IDE interface: Intel Corp. 6300ESB PATA Storage Controller (rev 02)

that'll have to be an after work activity seeing as it is already 2 pm... time for me to get ready for work. thanks for the help :)
 
Any loaded module, if used or not shows up in lsmod. The number after the names shows if it's needed by some other module(s).
 
turns out that generic ide drivers are what work... i spent some time and figured out which ones did not work and which ones did... definitely worth the time. my lsmod output is much smaller now...

Code:
mbentley@dual-xeon:~$ lsmod
Module                  Size  Used by
nvidia               4554196  12
ipv6                  239200  20
smbfs                  61176  3
af_packet              22792  2
pcspkr                  4612  0
psmouse                37256  0
ide_cd                 40196  0
cdrom                  37408  1 ide_cd
unix                   27920  453
 
Back