Monday, June 30, 2014

Create a simple file system



This post is about creating a simple file system and load with QEMU(X86).
Requirements
  1. QEMU
  2. Kernel BzImage
  3. Busy box 
Step 1: For Installing QEMU on ubuntu machine
      sudo apt-get install qemu-system
Step 2: Download kernel from kernel.org and compile the kernel for bzImage
Step 3: Download the busybox from http://git.busybox.net/busybox/?h=1_22_stable
      /* Create a folder for holding folders*/          
      $ mkdir myfilesys

      /* Go to busybox folder and compile*/
      $ make menuconfig             <-- In this step select "Statically linked busybox"
      $ make
      $ make install CONFIG_PREFIX=path/to/myfilesys

      /* Create the standard directories */
      $ mkdir dev etc etc/init.d bin proc mnt tmp var var/shm  
      $ chmod 755 . dev etc etc/init.d bin proc mnt tmp var var/shm

      /* dev folder settings */
      $ mknod tty c 5 0
      $ mknod console c 5 1
      $ chmod 666 tty console
      $ mknod tty0 c 4 0
      $ chmod 666 tty0
      $ mknod ram0 b 1 0
      $ chmod 600 ram0
      $ mknod null c 1 3
      $ chmod 666 null

      /*  etc folder settings */ 
      $ cd myfilesys/etc/
      $ vi init.d/rcS        <-- Create a file name "rcS" and add the following command
         #! /bin/sh
         mount -a # Mount the default file systems mentioned in /etc/fstab.
      $ chmod 744 init.d/rcS
      $ vi fstab             <-- Create fstab file and copy the following content
             proc  /proc      proc    defaults     0      0
             none  /var/shm   shm     defaults     0      0
      $ chmod 644 fstab
      $ vi inittab           <-- Create inittab file 
           ::sysinit:/etc/init.d/rcS
           ::askfirst:/bin/sh
      $ chmod 644 inittab

      /* Create an ext2 filesystem image by running these commands */ 
      $ dd if=/dev/zero of=my.img bs=1M count=2
      $ mkfs.ext2 -N 512 my.img

      /* mount the filesystem and copy the folders which created early (myfilesys contents)*/
      $ mount -t ext2 my.img /mnt
      $ cp -fr myfilesys/* /mnt
      $ umount /mnt
Step 4: Now we have bzImagefile system and installed QEMU. Issue the following command for loading our images.
     $ qemu-system-i386 -kernel bzImage -hda my.img -append=/dev/sda
Now QEMU loads newly build kernel and file system. Hurrayyyyyyy !!! We made it. If you want to explore further , try creating new directories, do some extra stuff :)
References

Monday, June 23, 2014

kmemleak in ubuntu


  1. kmemleak is a kernel debugging tool which is used for collecting memory leak information
  2. This kmemleak is kernel version of valgrind's memcheck --leak-check
  3. The orphan objects are not freed but only reported via /sys/kernel/debug/kmemleak
  4. Compile the kernel with CONFIG_DEBUG_KMEMLEAK.
Follow the instructions given in the following link for compiling a new kernel and installing in ubuntu machine.http://mitchtech.net/compile-linux-kernel-on-ubuntu-12-04-lts-detailed/
Step 1: Go to root shell mode by sudo -i
Step 2: Check kmemleak availability using dmesg | grep kmemleak 
dmesg | grep kmemleak
[    1.000175] kmemleak: Kernel memory leak detector initialized
[    1.000274] kmemleak: Automatic memory scanning thread started
Step 3: change the permission of /sys/kernel/debug/kmemleak. By default, it will read-only.
$ ls -l /sys/kernel/debug/kmemleak 
-r--r--r-- 1 root root 0 Jun 23 13:23 /sys/kernel/debug/kmemleak

$ chmod 777 /sys/kernel/debug/kmemleak
$ ls -l /sys/kernel/debug/kmemleak 
-rwxrwxrwx 1 root root 0 Jun 23 13:23 /sys/kernel/debug/kmemleak
Step 4: Compile the following kernel module
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>

MODULE_LICENSE("GPL");

int __init ourinitmodule(void)
{
        int *a = NULL, *b = NULL;
        printk(KERN_ALERT "\n Welcome to sample application.... \n");
        b = kmalloc(1024, GFP_KERNEL);    //Intentionally kept for testing kmemleak
        a = kmalloc(1024, GFP_KERNEL);
        a[0] = 10;
        kfree(a);
        return 0;
}

void __exit ourcleanupmodule(void)
{
        printk(KERN_ALERT "\n Thanks....Exiting Application. \n");
}

module_init(ourinitmodule);
module_exit(ourcleanupmodule);
Step 5: Insert the module and unload using insmod & rmmod
Step 6: Wait for following message in dmesg
[  325.438226]
[  325.438226]  Welcome to sample application....
[  360.964221]
[  360.964221]  Thanks....Exiting Application.
[ 1263.301682] kmemleak: 1 new suspected memory leaks (see /sys/kernel/debug/kmemleak)
Since kmemleak's default scan frequency is 10 mins, Wait for 10 mins to get this message (Note: This frequency can be programmed, we will discuss this later.)
Step 7: Print memory leak report by $cat /sys/kernel/debug/kmemleak 
unreferenced object 0xe7801800 (size 1024):
  comm "insmod", pid 2700, jiffies 6359 (age 2367.608s)
  hex dump (first 32 bytes):
    00 1c 80 e7 24 0a 30 ff 24 0a 30 ff 24 0a 30 ff  ....$.0.$.0.$.0.
    24 0a 30 ff 24 0a 30 ff 24 0a 30 ff 24 0a 30 ff  $.0.$.0.$.0.$.0.
  backtrace:
    [<c15da9ec>] kmemleak_alloc+0x2c/0x60
    [<c114ae06>] kmem_cache_alloc_trace+0x96/0x130
    [<f847c028>] 0xf847c028
    [<c1003132>] do_one_initcall+0x112/0x160
    [<c10acb4a>] load_module+0x1e8a/0x2660
    [<c10ad398>] sys_init_module+0x78/0xb0
    [<c15f850d>] sysenter_do_call+0x12/0x28
    [<ffffffff>] 0xffffffff
From the above log, we observe that there are 1024 un-referenced bytes.

Friday, June 20, 2014

Linux kernel interview questions - 1


I have listed few questions which gives basic idea of Linux kernel programming. Mostly kernel related interview is about the work you have done in past. For example, if you have done I2C & SPI drivers then most of the question is from those interfaces. If interviewer wants to know about your kernel knowledge, then he/she may ask general kernel related questions. I will update these questions on regular basis.

1. What is __init , __initdata ??

       These macros are used to mark some functions or initialized data (doesn't apply to uninitialized data) as "initialization" functions.The kernel can take this as hint that the function is used only during the initialization phase and free up used memory resources after.

__init will be defined as

#define __init   __section(.init.text) __cold notrace

and internally it will be expanded as

#define __section(S) __attribute__ ((__section__(#S)))

2. what is module_init() and module_exit() ??

These are macros which provides appropriate flags(boilerplates) to compiler for ensuring the treatment of init and cleanup functions.
Here it is defined. 

References:
module_init and init_module of linux

3. What is EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL() ??

           If programmer wants some symbols(function/data) to be used in other kernel modules, then those symbols should be exported using these macro. As name implies, EXPORT_SYMBOL_GPL() exports symbols only to GPL licensed modules.

4. What is modprobe, insmod, rmmod & depmod ?
5. What is initcall mechanism??
6. Which function will be the first function to be called in linux kernel?