Download - Week 10 The Linux Kernel More Development Tools CSCI 156.

Transcript
Page 1: Week 10 The Linux Kernel More Development Tools CSCI 156.

Week 10

The Linux KernelMore Development Tools

CSCI 156

Page 2: Week 10 The Linux Kernel More 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

Page 3: Week 10 The Linux Kernel More Development Tools CSCI 156.

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

Page 4: Week 10 The Linux Kernel More Development Tools CSCI 156.

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)

Page 5: Week 10 The Linux Kernel More Development Tools CSCI 156.

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:

Page 6: Week 10 The Linux Kernel More Development Tools CSCI 156.

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

Page 7: Week 10 The Linux Kernel More Development Tools CSCI 156.

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)?

Page 8: Week 10 The Linux Kernel More Development Tools CSCI 156.

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?

Page 9: Week 10 The Linux Kernel More Development Tools CSCI 156.

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

Page 10: Week 10 The Linux Kernel More Development Tools CSCI 156.

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