1
00:00:00,000 --> 00:00:05,006
(upbeat music)

2
00:00:05,006 --> 00:00:07,001
- [Instructor] Alright, the moment of truth.

3
00:00:07,001 --> 00:00:09,001
Did you get it to work?

4
00:00:09,001 --> 00:00:12,004
You got to have those header files, got to have make

5
00:00:12,004 --> 00:00:14,001
and GCC installed and so forth.

6
00:00:14,001 --> 00:00:17,009
So there's a little bit of background work maybe you had

7
00:00:17,009 --> 00:00:21,006
to do, but let's see what happens with mine.

8
00:00:21,006 --> 00:00:24,004
Here's my lab.c.

9
00:00:24,004 --> 00:00:26,004
Really only needed these two includes

10
00:00:26,004 --> 00:00:29,002
for this particular module.

11
00:00:29,002 --> 00:00:31,008
And just like the example, we've got the prototypes,

12
00:00:31,008 --> 00:00:34,009
we've got a init function, we've got a cleanup function,

13
00:00:34,009 --> 00:00:39,004
we register 'em with the module in it and module exit macros

14
00:00:39,004 --> 00:00:43,004
and we've got a module license and then we have a makefile.

15
00:00:43,004 --> 00:00:45,007
I did Makefile

16
00:00:45,007 --> 00:00:49,007
and obj-m = lab.o.

17
00:00:49,007 --> 00:00:50,005
So you got, you know,

18
00:00:50,005 --> 00:00:53,005
a little bit different colon equals or equals.

19
00:00:53,005 --> 00:00:56,005
That's pretty much the same.

20
00:00:56,005 --> 00:00:59,009
And then we'll do our make here.

21
00:00:59,009 --> 00:01:04,002
So as before we're making modules.

22
00:01:04,002 --> 00:01:06,002
Got a little bit of extra help, we got a warning

23
00:01:06,002 --> 00:01:09,006
that the compiler is different than the one used

24
00:01:09,006 --> 00:01:10,004
to build the kernel.

25
00:01:10,004 --> 00:01:13,005
So that's a little bit of a scary situation.

26
00:01:13,005 --> 00:01:16,000
It's actually more hygienic

27
00:01:16,000 --> 00:01:20,000
to make sure you're using exactly the same compiler

28
00:01:20,000 --> 00:01:22,006
to build your modules that were used to build a kernel.

29
00:01:22,006 --> 00:01:26,003
What if the compiler lays out structs differently

30
00:01:26,003 --> 00:01:28,003
than the kernel compiler did?

31
00:01:28,003 --> 00:01:29,006
Then you'll have misalignment.

32
00:01:29,006 --> 00:01:32,002
So this is a little bit dangerous,

33
00:01:32,002 --> 00:01:34,005
but unfortunately it's kind of a common practice,

34
00:01:34,005 --> 00:01:37,004
but heads up for that, using exactly the same compiler

35
00:01:37,004 --> 00:01:39,006
is a good idea.

36
00:01:39,006 --> 00:01:42,000
So we got some extra files there,

37
00:01:42,000 --> 00:01:44,003
but we got our lab.ko

38
00:01:44,003 --> 00:01:50,000
so we should be able to load that insmod.

39
00:01:50,000 --> 00:01:55,005
And let's look if we got our message, we got the hello.

40
00:01:55,005 --> 00:01:57,000
You can see I had done it before,

41
00:01:57,000 --> 00:01:58,005
so I got the cleanup and all it before.

42
00:01:58,005 --> 00:02:00,005
But the last line is the hello.

43
00:02:00,005 --> 00:02:03,007
And if we do an lsmod,

44
00:02:03,007 --> 00:02:05,005
we can see the top one there is lab.

45
00:02:05,005 --> 00:02:07,002
That's the most recently loaded module.

46
00:02:07,002 --> 00:02:08,007
So our module's loaded.

47
00:02:08,007 --> 00:02:12,008
Now let's do an RM mod lab, lsmod.

48
00:02:12,008 --> 00:02:15,006
Is it gone?

49
00:02:15,006 --> 00:02:17,001
It's gone.

50
00:02:17,001 --> 00:02:21,003
And we do our dmesg

51
00:02:21,003 --> 00:02:23,002
and it says cleaning up.

52
00:02:23,002 --> 00:02:28,004
Alright, now we want to edit our code instead

53
00:02:28,004 --> 00:02:33,003
of returning zero return minus one,

54
00:02:33,003 --> 00:02:37,006
that indicates some sort of error, negative value.

55
00:02:37,006 --> 00:02:42,005
And then we do the make again.

56
00:02:42,005 --> 00:02:46,009
All right, then we insmod that

57
00:02:46,009 --> 00:02:50,001
and we got this operation not permitted.

58
00:02:50,001 --> 00:02:52,007
Well, we didn't really get operation not permitted.

59
00:02:52,007 --> 00:02:55,003
Minus one means operation not permitted.

60
00:02:55,003 --> 00:02:57,003
You put minus two, you'll get a different error message.

61
00:02:57,003 --> 00:02:59,005
So that's kind of misleading.

62
00:02:59,005 --> 00:03:04,004
If we do lsmod, it's not loaded.

63
00:03:04,004 --> 00:03:11,008
If we look at dmesg,

64
00:03:11,008 --> 00:03:14,004
it did do the hello

65
00:03:14,004 --> 00:03:17,000
because that happened before the return.

