Difference between revisions of "Embedded-Linux-Device-Driver/C2/Loading-and-Unloading-a-Module/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 379: Line 379:
 
| style="border:1pt solid #000000;padding:0.176cm;"| To go back to '''regular user''', type '''exit'''  
 
| style="border:1pt solid #000000;padding:0.176cm;"| To go back to '''regular user''', type '''exit'''  
  
To go back to '''user login, type''' exit'''.
+
To go back to '''user login''', type '''exit'''.
  
 
|-
 
|-

Revision as of 22:46, 1 April 2020

Visual cue Narration
Slide 1:

Welcome slide:

Welcome to the spoken tutorial on Loading and Unloading a Module.
Slide 2:

Learning objectives:

  • insmod tool to load a module.
  • rmmod tool to unload a module.
  • dmesg command.
  • depmod, modprobe, objdump tools.
In this tutorial, we will learn how to use,
  • insmod tool to load a module.
  • rmmod tool to unload a module.
  • dmesg command.
  • depmod, modprobe, objdump tools.
Slide 3:

System Requirements:

  • VirtualBox 5.2
  • Ubuntu Linux 18.04 LTS operating system.
  • Linux kernel version 5.0.0-31 generic.
  • gedit text editor.
To record this tutorial, I am using,
  • VirtualBox 5.2
  • Ubuntu Linux 18.04 LTS operating system.
  • Linux kernel version 5.0.0-31 generic
  • gedit text editor.
Slide 4:

Prerequisites:

To follow this tutorial, you should be familiar with:
  • C programming language and
  • Basics of Linux kernel
Open the terminal

Type >> cd DeviceDriver

Open the terminal by pressing ALT+Ctrl+T keys simultaneously.

Press the Enter key after every command.

Go to the folder where simple_module dot c is saved.

Type >> make all

Press Enter

Remember, we have used make clean command earlier.

To compile the module again, type make space all.

Type sudo su

Press Enter

Enter the password

Type sudo space su

Enter the password of the system.

Let us now load the module simple_module dot c. in the kernel using insmod tool.
Type >> insmod simple_module.ko

Highlight insmod

Type insmod space simple_module dot ko.

The insmod tool loads a module into the kernel.

Type >> clear Clear the screen.
Type lsmod | head -5

Press Enter

Highlight lsmod

Let us check whether a module is loaded into the kernel or not.

Type lsmod space pipe space head space hyphen 5.

We know lsmod command shows the list of modules loaded in the current kernel.

So this command displays the first five modules of the list.

Type >> man pipe

Press Enter

To know more about Linux command go through the man page as shown.

Type man space pipe


Press q

Here, you get more information about pipe command.

Likewise, explore the command head to know more.

Press ‘q’ to exit.

Show the output

Highlight simple_module

Highlight 16384

Here, you can see the name of the module in this list.

It shows memory utilised by this module.

It indicates that the module is loaded into the kernel successfully.

Type >> clear Clear the screen.
Type >> dmesg | grep my_module

Press Enter

Highlight dmesg

Highlight grep my_module

Highlight dmesg | grep my_module

Type dmesg space pipe space grep space my_module.

It displays messages from kernel log buffer.

The grep command is used to search for lines matching a specified pattern.

It displays the messages containing the string my_module in the simple_module.c

Highlight my_module : inside the init function of our module Here, dmesg prints the message from init function of a module.

As a module is loaded into the kernel, only init function is executed.

Type >> clear Clear the screen.
Now let us see how to unload a module from the kernel.
Type rmmod simple_module.ko

Press Enter

Highlight rmmod

Type rmmod space simple_module dot ko.

This tool unloads a module from the kernel.

Let us check this.

Type >> lsmod | head -5 and press Enter

Show the output

Type lsmod space pipe space head space hyphen 5.

Now, you can see the module name has disappeared from this list.

It indicates that the module is unloaded from the kernel successfully.

Type >> clear Clear the screen.
Type >> dmesg | grep my_module

Press Enter.

Show the output

Highlight Inside the exit function of our module

Let us see the printk messages of a module.

Type dmesg space pipe space grep space my_ module

Now, you can see the messages from exit function also.

As a module is unloaded from the kernel, exit function is executed.

Type >> clear

Press Enter

Clear the screen.
Let us see the use of the depmod command.
Type >> man 8 depmod

Press Enter

for that, type man space 8 space depmod.

It creates a dependency file named modules.dep.

This dependency file is later used by modprobe tool.

You can get more information about depmod from it’s manpage.

Press ‘q’ to exit.

Let us see the use of the modprobe tool.
Type >> man 8 modprobe

Press Enter

Type man space 8 space modprobe.

The modprobe tool intelligently adds or removes a module from the kernel.

Some modules depend on other modules.

This tool automatically loads and unloads its dependent modules.

You can get more information about modprobe tool from it’s manpage

The modprobe expects an up-to-date modules dot dep file generated by the depmod.

Press q Press ‘q’ to exit.
Type >> clear

Press Enter

Clear the screen.
Type >> insmod simple_module.ko

Press Enter

Let us now load a module in the kernel again.

Type insmod space simple_module dot ko

Type >> lsmod | head -5

Press Enter

Let us check a module is loaded into the kernel or not.

Type lsmod space pipe space head space hyphen 5

As a module loaded into the kernel successfully, it appears here.

Type >> clear

Press Enter.

Clear the screen.
Type >> readelf hyphen h simple_module.ko

Press Enter.

Highlight .ko

Highlight ELF Header.

Let us see the ELF file of the module.

Type readelf space hyphen h space simple_module dot ko

Here, the dot ko is an ELF file.

Each ELF file is made up of one ELF header, followed by file data.

The elf header describes the overall information of the module dot ko file.

ELF means “Executable and Linking Format”.

It is a standard executable file format in Linux.

Clear the screen.

Type >> objdump -f simple_module.ko

Press Enter

Highlight -f

Highlight simple_module.ko

Highlight elf64-x86-64

Highlight i386:x86-64

Highlight start address

Let us see the information about the object file

Type objdump space hyphen f space simple_module dot ko.

The hyphen f option shows our module’s elf header.

It shows the name of a module, the file format of a module file.

It shows the architecture of our system, and the entry point function’s address.

For more information, go through its manpage.

Type >> clear

Press Enter

Clear the screen.
Type >> rmmod simple_module

Press Enter

To unload the module from the kernel, type rmmod simple_module.ko
Type >> make clean

Press Enter

Type make space clean.

So all extra files will be removed from the present working directory.

Type >> exit

Press Enter

Type >> exit

Press Enter

To go back to regular user, type exit

To go back to user login, type exit.

Slide 5:

Summary:

In this tutorial, we learnt how to use
  • insmod tool to load a module.
  • rmmod tool to unload a module.
  • dmesg command.
  • depmod, modprobe, objdump tools.
Slide 6:

Assignment:

As an assignment,
  1. Open the simple_module.c file.
  2. Change the message in printk function of init_function.
  3. Load the module using insmod tool.
  4. See the output of dmesg command.
  5. Remove the module using rmmod tool
Slide 7:


About Spoken Tutorial Project:

  • The video at the following link summarizes the Spoken Tutorial project.
  • Please download and watch it.
Slide 8:

Spoken Tutorial workshops:

The Spoken Tutorial Project Team conducts workshops and gives certificates.

For more details, please write to us.

Slide 9:

Forum questions:

Please post your timed queries in this forum.
Slide 10:

Forum for specific questions:

  • Do you have any general or technical questions on Embedded Linux Device Driver?
  • Please visit the FOSSEE forum and post your question.
Slide 11

Acknowledgment:

Spoken Tutorial project is funded by MHRD, Government of India.
Slide 12:

Thank you slide:

This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.

This is Mayuri Panchakshari signing off.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat