Linux Driver Development

Source: Internet
Author: User

1. Install Linux
2. Write Hello World
hello.c

 #include <linux/kernel.h> /*Needed by all modules*/#include <linux/module.h> /*Needed for KERN_* */#include <linux/init.h> /* Needed for the macros */MODULE_LICENSE("GPL");static int year=2014;static int hello_init(void){  printk(KERN_WARNING "Hello kernel, it‘s %d!\n",year);  return 0;}static void hello_exit(void){  printk("Bye, kernel!\n");}/* main module function*/module_init(hello_init);module_exit(hello_exit);

Obj-m: = hello.o is compiled into a module, followed by the corresponding file name plus. o
Makefile

 obj-m  := hello.oKERNELDIR ?= /lib/modules/$(shell uname -r)/buildPWD       := $(shell pwd)all:    $(MAKE) -C $(KERNELDIR) M=$(PWD)clean:    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

Install Module command insmod./hello.ko
Remove Rmmod./hello.ko
viewing logs with command Tail/var/log/kern.log

 [email protected]:~/desktop# makemake-c/lib/modules/4.2.0-27-generic/build M=/root/Desktopmake[1]: Entering Directory '/usr/src/linux-headers-4.2.0-27-generic ' LD/ROOT/DESKTOP/BUILT-IN.O CC [M]/root/desktop/hello.o Buil  Ding modules, Stage 2. Modpost 1 Modules cc/root/desktop/hello.mod.o LD [M]/root/desktop/hello.komake[1]: Leaving directory '/usr/src/l Inux-headers-4.2.0-27-generic ' [email protected]:~/desktop# insmod./hello.ko[email protected]:~/ desktop# Tail/var/log/kern.logaug 05:54:38 ubuntu kernel: [11.882941] audit:type=1400 Audit (1535288078.331:11): AP Parmor= "STATUS" operation= "Profile_load" profile= "unconfined" name= "/usr/lib/lightdm/lightdm-guest-session" pid= 825 comm= "Apparmor_parser" 05:54:38 ubuntu kernel: [12.138198] Ipv6:addrconf (NETDEV_UP): Eth0:link is not ready 05:54:38 ubuntu kernel: [12.423300] E1000:eth0 NIC Link is up $ Mbps full Duplex, Flow Control:noneaug 26 0 5:54:38 ubuntu kernel: [12.436023] Ipv6:addrconf (netdev_change): Eth0:link becomes Readyaug-05:55:05 ubuntu kernel: [39.322268] Audit_printk_sk b:150 callbacks Suppressedaug 05:55:05 ubuntu kernel: [39.322272] audit:type=1400 Audit (1535288105.459:62): Apparm Or= "STATUS" operation= "Profile_replace" profile= "unconfined" name= "/usr/lib/cups/backend/cups-pdf" pid=2171 comm= "  Apparmor_parser "05:55:05 ubuntu kernel: [39.322282] audit:type=1400 Audit (1535288105.459:63): apparmor=" STATUS " operation= "Profile_replace" profile= "unconfined" name= "/usr/sbin/cupsd" pid=2171 comm= "Apparmor_parser" 26 05:55:05 ubuntu kernel: [39.322809] audit:type=1400 Audit (1535288105.459:64): apparmor= "STATUS" operation= "profile_re Place "profile=" unconfined "name="/usr/sbin/cupsd "pid=2171 comm=" Apparmor_parser "to 06:15:14 Ubuntu kernel: [ 1248.118385] Hello:module verification failed:signature and/or required key missing-tainting Kernelaug to 06:15:14 Ubu NTU kernel: [1248.121354] Hello kernel, it ' s 2014![email protected]:~/desktop# rmmod/hello.ko [email protected]:~/desktop# Tail/var/log/kern.logaug 26   05:54:38 ubuntu kernel: [12.138198] Ipv6:addrconf (NETDEV_UP): Eth0:link is not Readyaug-05:54:38 ubuntu kernel: [ 12.423300] E1000:eth0 NIC Link is up to $ Mbps full Duplex, Flow Control:noneaug 05:54:38 ubuntu kernel: [12.436 023] Ipv6:addrconf (netdev_change): Eth0:link becomes Readyaug-05:55:05 ubuntu kernel: [39.322268] Audit_printk_skb : Callbacks Suppressedaug 05:55:05 ubuntu kernel: [39.322272] audit:type=1400 Audit (1535288105.459:62): Apparmo R= "STATUS" operation= "Profile_replace" profile= "unconfined" name= "/usr/lib/cups/backend/cups-pdf" pid=2171 comm= "  Apparmor_parser "05:55:05 ubuntu kernel: [39.322282] audit:type=1400 Audit (1535288105.459:63): apparmor=" STATUS " operation= "Profile_replace" profile= "unconfined" name= "/usr/sbin/cupsd" pid=2171 comm= "Apparmor_parser" 26 05:55:05 ubuntu kernel: [39.322809] Audit:type=1Audit (1535288105.459:64): apparmor= "STATUS" operation= "Profile_replace" profile= "unconfined" Name= "/usr/sbin/ CUPSD "pid=2171 comm=" Apparmor_parser "06:15:14 ubuntu kernel: [1248.118385] hello:module verification Failed:sig Nature and/or required key missing-tainting Kernelaug 06:15:14 ubuntu kernel: [1248.121354] Hello kernel, it ' s 2014! 06:16:13 ubuntu kernel: [1306.921227] Bye, kernel!  [email protected]:~/desktop#

Problems that arise
1. The IDE may not be able to find the # include <linux/kernel.h> etc header file
Yes, the include does not
This is okay because the kernel source to see the kernel version first

[email protected]:~/Desktop# uname -r4.2.0-27-generic[email protected]:~/Desktop#

Use $ (shell uname-r) to write the makefile file again.

Linux Driver Development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.