Session 2 - Kernel: Code, Build, Run

Tasks

Important

  • All work from now on will be done on a virtual machine. It should be installed on your lab workstation. To get it for home, use this link.
    • ssh student@spook.local

# Compile and boot Linux kernel #* Check your current kernel version, and notice the target architecture # uname -a # cat /proc/version # cat /proc/cpuinfo #* wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.10.tar.xz #* tar xf linux-3.10.tar.xz #* Create .config file from i386_defconfig and check the options enabled # make defconfig #* Play around with make config and notice available options # What are the file systems compiled in kernel? # Is IPv6 support enabled ? #* Build kernel image and modules # Note: the current linux version needs the 'bc' package that is not on the vm. Install it with sudo apt-get install bc. # make -j3 bzImage #* grab a coffee # make -j3 modules #* Install kernel image # sudo cp arch/x86/boot/bzImage /boot/vmlinuz-3.10.0 # sudo cp .config /boot/config-3.10.0 # sudo cp System.map /boot/System.map-3.10.0 #* Install modules # sudo make modules_install #* Build initial ramdisk # cd /boot ; sudo mkinitramfs -o initrd.img-3.10.0 3.10.0 #* Update grub and reboot the system # sudo update-grub # reboot #* Boot into the new image and check current kernel version # cat /proc/version

  1. Troubleshooting
    • Notice that we can no longer connect, via ssh, to the new booted Linux kernel image inside the virtual machine.
      • Check the output of ifconfig or ip a s
      • Conclusion: There is no driver installed for the network card
    • Boot into the old working image and figure out the network card type
      • student@spook:~$ ethtool -i eth0
        driver: pcnet32
        version: 1.35
        firmware-version: 
        bus-info: 0000:00:11.0 
    • Compile support for pcnet32 into the kernel
      • make menuconfig
      • Search for PCNET32 (use /PCNET32)
      • Select Y for AMD PCnet32 PCI support
    • rebuild the bzImage as explained above, and reboot.
  2. Say hello from the kernel
    • Add a printk inside the sources (e.g init/main.c:512)
      •  printk("Hello World!\n"); 
    • Recompile the image and reboot
    • Check for the printk message
      • dmesg | grep Hello
  3. Simple kernel module
    • Analyze the source code for our simple module
    • simple.c
      #include <linux/kernel.h>
      #include <linux/init.h>
      #include <linux/module.h>
       
      MODULE_DESCRIPTION("My kernel module");
      MODULE_AUTHOR("Me");
      MODULE_LICENSE("GPL");
       
      static int simple_init(void)
      {
              printk( KERN_DEBUG "Hi\n" );
              return 0;
      }
       
      static void simple_exit(void)
      {
              printk( KERN_DEBUG "Bye\n" );
      }
       
      module_init(simple_init);
      module_exit(simple_exit);
    • For compiling this module we need Makefile and Kbuid files.
    • Makefile
      KDIR=/lib/modules/`uname -r`/build
      kbuild:
              make -C $(KDIR) M=`pwd`
      clean:
              make -C $(KDIR) M=`pwd` clean
    • Kbuild
      EXTRA_CFLAGS = -g
      obj-m        = simple.o
    • Compile simple kernel module
      • make
    • Insert it into the kernel
      • sudo insmod simple.ko
    • Check if module works
      • dmesg | grep Hi
      • modinfo simple.ko
      • rmmod simple.ko
      • dmesg | tail == Resources ==

* The Tanenbaum-Torvalds Debate

sesiuni/kernel/day-2.txt · Last modified: 2013/07/01 22:54 by alexj