Operating Systems 2014F: Tutorial 7

From Soma-notes
Revision as of 16:44, 11 November 2014 by Soma (talk | contribs) (→‎Devices)
Jump to navigation Jump to search

In this tutorial you will be learning about the basics of Linux kernel modules and character devices.

Building and running your first module

  1. Download the source for this simple module, unpack, and build it by typing "make".
  2. Install the module using "sudo insmod simple.ko". The hello message is recorded in the kernel logs. How do you view the kernel logs?
  3. Check to see that the module has been loaded. How do you do this?
  4. Remove the module from the kernel. What did you do?

Devices

  1. Look at the details of files in /dev. What kind of filesystem stores these files? (Hint: look at the output of df.)
  2. How can you tell the difference between block and character devices using ls?
  3. What are the major and minor numbers of the loopback block devices (loopX)?
  4. What block device is used for the root filesystem on your system (the class VM or other Linux system)?
  5. When you type commands in a terminal window, you are interacting with a "pseudo tty" character device. Specifically, what device file(s) are being used by the terminal window, and what are the permissions and special file attributes (major, minor numbers) of these files? Are the permissions important?

Source

simple.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>

static int __init simple_init(void)
{
        printk ("Hello kernel world!\n");
        return 0;
}

static void __exit simple_exit(void)
{
        printk ("Goodbye kernel world.\n");
        return;
}

module_init(simple_init);
module_exit(simple_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Somayaji <soma@scs.carleton.ca>");
MODULE_DESCRIPTION("A simple module");


Makefile (for simple)

obj-m := simple.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules