Week 10
The Linux KernelMore Development Tools
CSCI 156
Types of Kernels
• Monolithic– One large executable does everything– Advantages: speed– Disadvantages: inflexible
• Microkernel– Small core, interacts with modules in user
space. Almost everything done in modules– Advantages: highly modifiable– Disadvantages: typically slower
The Linux Kernel
• Hybrid! (best of both worlds)– Monolithic kernel with modular support– Modules are inserted into the running kernel
• thus, everything runs in kernel space
– Keep the speed, add the flexibility
• Linux kernel can be built as purely monolithic– Disadvantages: huge, slows booting, hogs RAM– Advantages: some older hardware has problems
with modules
Loading Modules
• Module loding utilities (must be root)– lsmod – list currently running modules– insmod – insert module into kernel (requires .o)– modprobe – smarter insmod: uses module name– rmmod – remove module from kernel
• Some modules loaded at startup– In Fedora 2 (these machines) /etc/modprobe.conf– Other systems /etc/modules.conf– (Redhat = Evil)
Writing a Module• Big Learning Curve
– Memory allocation is different in kernel-space
– Allow for pre-emption
• Alternatives
– Hijacking system calls to add/modify: #include <sys/syscall.h> #include <sys/types.h> pid_t getpid(void) { printf("w00t!\n"); return syscall(SYS_getpid); }
root@newdell6a:~> gcc -Wall -fPIC -shared -o getpid.so getpid.croot@newdell6a:~> LD_PRELOAD=./getpid.so bash -c 'echo $$'
To Run:
Building the Kernel• Steps
– Download the source to /usr/src/linux-version– configure the kernel
• make menuconfig
– build the kernel• make bzImage
– build the kernel modules• make modules && make modules_install
• How long does it take?– 30 minutes w/kernel hacking disabled: 1.2MB– 45 minutes w/kernel hacking enabled: 1.3MB
Installing the Kernel• Bold replacement
– cp arch/i386/boot/bzImage /boot/bzImage-version– what if the kernel fails to boot?
• Safety Net– cp arch/i386/boot/bzImage /boot/MYIMG-version
• Using the safety net:– How to specify which kernel to boot (old/new)?
GRUB• Bootloader, configured with grub.conf
– In the /boot/grub dir (or /etc/grub.conf)– Naming convention: /dev/hda2 -> (hd0,1)– Must specify title, root drive, kernel– Allows for dual-booting multiple Oss
• Even multiple versions of the same OS: tweak away!
• Other options– LILO: older boot-loader, fallen out of use– others for other hardware (non-i386)
• this room?
Switching Gears...Tools• ctags
– ctags -R• recurse through all directories, find tags
– while in vim:• CTRL-]
– jumps to function definition
• CTRL-T– jumps back to the caller
• gd– in command mode, go to local variable definition
• CTRL-N– in insert mode, complete the variable/method name
Method Folding• In vim:
– :set foldmethod=indent– :set foldlevel=0
– on a folded method: zO to expand/unfold– on an unfolded method: zC to close/fold
Top Related