1
00:00:00,005 --> 00:00:02,000
- [Instructor] Fun with modules.

2
00:00:02,000 --> 00:00:05,003
Let's look at commands related to working

3
00:00:05,003 --> 00:00:08,005
with loadable kernel modules.

4
00:00:08,005 --> 00:00:11,002
We'll start with lsmod, list the modules

5
00:00:11,002 --> 00:00:14,006
that are currently loaded into your kernel.

6
00:00:14,006 --> 00:00:18,000
These are listed chronologically.

7
00:00:18,000 --> 00:00:22,001
The top of the list there, in this case, vhost_net

8
00:00:22,001 --> 00:00:26,000
is the most recently loaded module.

9
00:00:26,000 --> 00:00:29,006
And then there's probably more than just four.

10
00:00:29,006 --> 00:00:33,003
The very last one would be the very first module loaded.

11
00:00:33,003 --> 00:00:37,001
And we can see there's actually four columns,

12
00:00:37,001 --> 00:00:38,009
but only three headers.

13
00:00:38,009 --> 00:00:40,009
The first column is the name of the module,

14
00:00:40,009 --> 00:00:45,006
so that would be typically the .ko name,

15
00:00:45,006 --> 00:00:49,000
and then size and bytes, and then a number,

16
00:00:49,000 --> 00:00:51,006
that's the use count.

17
00:00:51,006 --> 00:00:55,009
And then, after that is the used by column.

18
00:00:55,009 --> 00:01:04,000
So we see, for example here, macvtap is used by vhost_net

19
00:01:04,000 --> 00:01:09,007
and mtacvlan is used by macvtap.

20
00:01:09,007 --> 00:01:12,009
And we could tell by the order here

21
00:01:12,009 --> 00:01:15,006
that they have to be loaded in an order

22
00:01:15,006 --> 00:01:21,002
to satisfy these used by requirements.

23
00:01:21,002 --> 00:01:24,007
You remove a module with rmmod.

24
00:01:24,007 --> 00:01:27,009
It is possible to configure a kernel

25
00:01:27,009 --> 00:01:29,007
where you're not allowed to remove modules.

26
00:01:29,007 --> 00:01:32,003
You can load 'em but not remove 'em.

27
00:01:32,003 --> 00:01:36,000
Now, you might not be able to remove a module

28
00:01:36,000 --> 00:01:40,002
if it's in use, kind of a good idea.

29
00:01:40,002 --> 00:01:43,002
There could be pointers into it, for example,

30
00:01:43,002 --> 00:01:44,002
in use by the kernel.

31
00:01:44,002 --> 00:01:45,005
And if you remove the module,

32
00:01:45,005 --> 00:01:48,005
then there's a pointer in the kernel that doesn't point

33
00:01:48,005 --> 00:01:50,007
to something valid anymore.

34
00:01:50,007 --> 00:01:54,008
You can try to force-remove a module with the -f.

35
00:01:54,008 --> 00:01:56,009
Not such a good idea.

36
00:01:56,009 --> 00:01:59,001
During development, when you got a system of yourself

37
00:01:59,001 --> 00:02:03,000
and you're writing a driver, it might be kind of handy,

38
00:02:03,000 --> 00:02:06,006
but still pretty risky.

39
00:02:06,006 --> 00:02:12,005
And again, I would caution you in ever removing a module.

40
00:02:12,005 --> 00:02:17,008
It's really easy for a module to have some code in it

41
00:02:17,008 --> 00:02:21,003
that leaves the kernel in a fragile state.

42
00:02:21,003 --> 00:02:25,002
So generally load 'em, leave them there,

43
00:02:25,002 --> 00:02:27,004
don't ever remove them.

44
00:02:27,004 --> 00:02:32,004
Baked into a module can be some additional information.

45
00:02:32,004 --> 00:02:37,006
There's macros to specify the author, for example.

46
00:02:37,006 --> 00:02:42,008
And a parameter list can exist for a module.

47
00:02:42,008 --> 00:02:44,004
So when you load a module,

48
00:02:44,004 --> 00:02:48,006
you can set various tunable parameters

49
00:02:48,006 --> 00:02:51,002
and there could be a list

50
00:02:51,002 --> 00:02:55,005
of what kind of devices this module supports.

51
00:02:55,005 --> 00:02:57,002
So you got an ethernet driver,

52
00:02:57,002 --> 00:03:00,008
which ethernet devices does it support, could be in there.

53
00:03:00,008 --> 00:03:04,004
And there's a version magic string coding

54
00:03:04,004 --> 00:03:07,005
which kernel version the module goes to,

55
00:03:07,005 --> 00:03:12,001
that is checked when you try to load a module.

56
00:03:12,001 --> 00:03:16,003
We saw the file modules.dep, that gets created

57
00:03:16,003 --> 00:03:18,000
by a command called depmod,

58
00:03:18,000 --> 00:03:20,009
that file as well as some others.

59
00:03:20,009 --> 00:03:23,007
Now, you probably don't have to run depmod,

60
00:03:23,007 --> 00:03:26,006
but it's kind of good to know it exists.

61
00:03:26,006 --> 00:03:30,001
And if you install some modules, like from a package,

62
00:03:30,001 --> 00:03:34,000
that package should run the depmod command for you

63
00:03:34,000 --> 00:03:39,001
to rebuild the files, like modules about dep.

64
00:03:39,001 --> 00:03:42,002
Let's say you have a module, maybe you built your own,

65
00:03:42,002 --> 00:03:46,003
you got this .ko or .kozst file.

66
00:03:46,003 --> 00:03:49,009
You load it into the kernel, you insert it into the kernel

67
00:03:49,009 --> 00:03:52,003
with the insmod command.

68
00:03:52,003 --> 00:03:55,003
And then what happens is, the kernel runs

69
00:03:55,003 --> 00:03:58,008
and initialization function in the module.

70
00:03:58,008 --> 00:04:01,004
And when that function returns,

71
00:04:01,004 --> 00:04:04,008
then the insmod command returns.

72
00:04:04,008 --> 00:04:06,009
It's possible for the module to fail

73
00:04:06,009 --> 00:04:10,001
for that initialization function to fail.

74
00:04:10,001 --> 00:04:11,009
Maybe it's looking for something

75
00:04:11,009 --> 00:04:16,003
that's something's not there, so it's going to return.

76
00:04:16,003 --> 00:04:19,006
And the insmod could print an error message.

77
00:04:19,006 --> 00:04:22,003
There might be more information from the module.

78
00:04:22,003 --> 00:04:25,006
You could check that with dmesg.

79
00:04:25,006 --> 00:04:29,001
With the insmod command, you provide the path to the module,

80
00:04:29,001 --> 00:04:35,001
insmod/mysubdur/mymodule.ko, say.

81
00:04:35,001 --> 00:04:38,007
An alternative to insmod is modprobe.

82
00:04:38,007 --> 00:04:41,008
modprobe uses the information

83
00:04:41,008 --> 00:04:44,002
in the lib modules kernel version directory,

84
00:04:44,002 --> 00:04:47,001
like the modules.dep file.

85
00:04:47,001 --> 00:04:49,008
So you don't give a path name to a module with modprobe.

86
00:04:49,008 --> 00:04:52,001
You just say the name of the module.

87
00:04:52,001 --> 00:04:55,008
And that makes it easier in some ways than insmod.

88
00:04:55,008 --> 00:04:58,003
But if you wrote your own module, then you'd have

89
00:04:58,003 --> 00:05:00,002
to put it in the lib module directory.

90
00:05:00,002 --> 00:05:02,000
You'd have to run the depmod command

91
00:05:02,000 --> 00:05:03,005
if you wanted to use modprobe.

92
00:05:03,005 --> 00:05:08,007
So insmod's easier when you're writing your own module,

93
00:05:08,007 --> 00:05:10,007
like we'll do later on.

94
00:05:10,007 --> 00:05:13,004
modprobe also lets you remove modules

95
00:05:13,004 --> 00:05:16,001
and it'll remove the dependencies also.

96
00:05:16,001 --> 00:05:20,000
And there's a number of other options for modprobe.

97
00:05:20,000 --> 00:05:24,007
modprobe is what you'll most commonly use for working

98
00:05:24,007 --> 00:05:29,000
with installed loadable kernel modules.

