1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
---
title: WTF is Linux
author: Rémi Nicole <remi.nicole@smile.fr>
date: 2020-09-29
slide-level: 2
aspectratio: 169
theme: metropolis
colortheme: owl
beameroption: "show notes on second screen=right"
toc: true
highlightstyle: breezedark
lang: en-US
bibliography: ../bibliography.bib
---
# Before we get started
## Reminder
- Please fill the [form](https://mensuel.framapad.org/p/zufgimy2yy-9j3n)
- Please have a GNU/Linux system with an SD card reader
## Where were we
::: notes
- We managed to boot the system!
- What we needed was:
- A file system to store files, and to be used as the root directory
- An init program in this file system, and started by the kernel
:::
# It works but...
## The init problem
- init is a shell
- we want to automate some tasks:
- re-mounting the `/` directory in read-write mode
- networking?
- anything our system should do
## The networking problem
- We want to access the network:
- get updates
- execute our primary function
## The user problem
users, anyone?
::: notes
- At no point in our creation of a system we had to create a user, or add
a password
:::
# Automating tasks
## Goal
We want to execute programs or code when the system is starting
## Attempt number 1
Let's make a shell script
::: notes
- Instead of just launching a command-line as first program, let's execute some
commands automatically before, and *then* launch the command-line
:::
## Attempt number 2
Let's use SysV-style init
## SysV-style init
- The init process reads `/etc/inittab`
- Starts processes as specified by this configuration
## inittab
This file is a series of files in this format:
```ini
<id>:<runlevels>:<action>:<process>
```
::: notes
Busybox does not support each of these features
:::
## inittab (Busybox version)
```ini
# ,-- out of scope
# |
# vvv
<tty>::<action>:<process>
```
`action` can be one of:
- sysinit
: normal process started at boot
- shutdown
: process started before shutting down
- reboot
: process started before rebooting
- ...
## inittab example
```ini
# Mount /proc and /sys, and remount / in read-write mode
::sysinit:/etc/init.d/mount-special-filesystems
# Setup the network
::sysinit:/etc/init.d/setup-network start
# Start additional services
::sysinit:/etc/init.d/miniserve start
# ...
```
::: notes
- Note the start argument in the two last services
:::
## Changing to SysV-style init
We just have to change the `init=` kernel parameter to Busybox's `init`.
## The long-running processes problem
- We need to launch long-running processes in the background
- We also need a way to stop them
::: notes
- We might want to stop a process if something goes wrong, if we want to modify
the configuration, etc.
:::
## The `start-stop-daemon` command
Can do multiple things:
- Start a process in a background
- Start a process as a different user
- Write the PID of the launched process into a file (called a "PID file")
- Kill a process with a PID file
:::notes
- We don't have our users figured out yet
- Reminder, this is still the old/light way of doing things, so you might not
have this command on your Linux
:::
## Example service script
```sh
#!/bin/sh
PIDFILE="/var/run/myprogram.pid"
case "$1" in
start)
start-stop-daemon -S -p "$PIDFILE" -m -b \
-x myprogram -- args...
;;
stop)
start-stop-daemon -K -p "$PIDFILE"
;;
esac
```
# Networking
## What's in an IP packet
![Anatomy of an IP packet[@corkami:rfc791]](../res/rfc791.png){ height=80% }
::: notes
- From this, we can conclude that we need an IP address for every packet sent
over IP
- Also, we see that we cannot contact a server through it's domain name (like
google.com), we have to use IP addresses
:::
## A home network
![Home network example[@wikimedia:network1]](../res/home-network.jpg){ height=80% }
## Some definitions
- IP address
: Identifier that allows you to get messages/packets
- Subnet
: A defined subset of all IP addresses. For example 192.168.1.0/24 means all IP
addresses from 192.168.1.0 to 192.168.1.255
- Router
: Machine that connects one subnet to another. Usually, it is the subnet of
your home, to the global internet
- Gateway
: A machine where you send your internet packets, so that they are transferred
to the "real" recipient. It is usually your router.
::: notes
- The `/24` in the example subnet is what we call a "mask"
It is nice to have a postal mail analogy. In this case:
- IP address is your home address
- Subnet is your town name, street name
- Router and gateway would be your postman company (La Poste, UPS, Fedex, or
whatever)
:::
## Network interfaces
- Represent a way to connect to a network
- Stores each:
- One or more IP address
- A subnet
::: notes
- If you have two Ethernet ports, you could connect to two different networks
- In this example you would have one interface for each of your Ethernet port
- And maybe one more interface for your WiFi connection, if you have the
hardware for it
:::
## What you need to connect to a network
- An IP address
- The subnet of your network
- A gateway, if you want to connect your network to other networks, like the
world wide web
- A DNS server's IP address, if you want to resolve host names, like
<http://example.com>
## Configuring a network interface
- The static way
- The dynamic way
::: notes
- The static way is simpler to configure, but you have to know in advance the
device that are going to connect to your network, and have a static IP
address for all of them
- You also have to know in advance the subnet and IP address of your router
- The dynamic way is the one that you use on your personal computers
- This is the mode were you try to ask for all these parameters
:::
## The static way
```sh
# Set the IP address and subnet
ip addr add $ip/$mask dev $interface
# Activate this interface
ip link set $interface up
# Set the default gateway
ip route add default via $gateway dev $interface
# Add the default DNS server
echo "nameserver $nameserver_ip" >> /etc/resolv.conf
```
::: notes
For QEMU's defaults:
- `busybox ip addr add 10.0.2.42/24 dev eth0`
- `busybox ip link set eth0 up`
- `busybox ip route add default via 10.0.2.2 dev eth0`
- `echo "nameserver 10.0.2.3" >> /etc/resolv.conf`
:::
## The dynamic way
- DHCP for the win!
- meaning Dynamic Host Configuration Protocol
. . .
- we need a DHCP client on our machine and a DHCP server on the network
::: notes
DHCP is a protocol that allows us to get all of this information:
- A new, unused IP address
- The subnet of the network
- The address of the gateway
- The address of the DNS server
:::
## DHCP overview
![DHCP overview[@wikimedia:dhcp]](../res/DHCP_session.png){ height=80% }
## Implementation
- Here we can use the `udhcpc` command of Busybox
# Users
## Current status
Right now we have none
::: notes
Do a `ls -l` on the machine
:::
## The passwd file
Each line of `/etc/passwd` contains:
- Login name (what we were missing)
- Field unused today (was the password, but it got moved to another file)
- Numerical ID of the user
- Numerical ID of the main group for user
- User name or comment
- Home directory
- Optional default shell
## Example
```ini
# ,--- tells that the password is in another file
# |
# v
root:x:0:0:System administrator:/root:/bin/sh
```
## Login "screen"
- Now we can add one to the `inittab`
- This is the `getty` command of `busybox`
# References
|