@@ -236,7 +236,10 @@ memory and starts it. For this we use GRUB, one of the most widely used
236
236
bootloaders. It has a ton of features but we are going to keep it very simple.
237
237
Installing it is very simple, we just do this:
238
238
` ` ` bash
239
- grub-install --modules=part_msdos --target=i386-pc --boot-directory=" $PWD /boot" /dev/loop0
239
+ grub-install --modules=part_msdos \
240
+ --target=i386-pc \
241
+ --boot-directory=" $PWD /boot" \
242
+ /dev/loop0
240
243
` ` `
241
244
The ` ` --target=i386-pc` ` tells grub to use the simple msdos MBR bootloader. This
242
245
is often the default but this can vary from machine to machine so you better
@@ -304,7 +307,98 @@ $ qemu-system-x86_64 -enable-kvm image
304
307
And if everything went right you should now be dropped in a shell in our
305
308
homemade operating system.
306
309
310
+ ** Side note:** When using QEMU, you don' t actually need a bootloader. You can
311
+ tell QEMU to load the kernel for us.
312
+ ```bash
313
+ $ qemu-system-x86_64 -enable-kvm \
314
+ -kernel bzImage \
315
+ -append "quiet init=/bin/sh root=/dev/sda1" \
316
+ image
317
+
318
+ ```
319
+ Where ``bzImage`` points to the kernel we built on your system, not the image.
320
+ and ``-append`` specifies the kernel arguments(don' t forget the quotes). This
321
+ could be useful when you would like to try different kernel parameters without
322
+ changing ` ` grub.cfg` ` every time.
323
+
307
324
PID 1: /sbin/init
308
325
---------------
309
326
310
- % TODO
327
+ The first process started by the kernel (now ` ` /bin/sh` ` ) has process id 1. This
328
+ is not just a number and has some special implications for this process. The
329
+ most important thing to note is that when this process ends, you' ll end up with
330
+ a kernel panic. PID 1 can never ever die or exit during the entire runtime of
331
+ your system. A second and less important consequence of being PID 1 is when
332
+ another process ' reparents' like when a process forks to the background PID 1
333
+ will become the parent process.
334
+
335
+ This implies that PID 1 has a special role to fill in our operating system.
336
+ Namely that of starting everything, keeping everything running, and shutting
337
+ everything down because it' s the first and last process to live.
338
+
339
+ This also makes this ` ` init` ` process very suitable to start and manage services
340
+ as is the case with the very common ` ` sysvinit` ` and the more modern
341
+ ` ` systemd` ` . But this isn' t strictly necessary and some other process can cary
342
+ the burden of service supervision, which is the case with the
343
+ [runit](http://smarden.org/runit/)-like ``init`` that is included with
344
+ ``busybox``.
345
+
346
+ Unless you passed the ``rw`` kernel parameter the root filesystem is mounted as
347
+ read-only. So before we can make changes to our running system we have to
348
+ remount it as read-write first. And before we can do any mounting at all we have
349
+ to mount the ``proc`` pseudo filesystem that serves as an interface to kernel.
350
+ ```bash
351
+ $ mount -t proc proc /proc
352
+ $ mount / -o remount,rw
353
+ ```
354
+
355
+ So first things first, we' ll create a script
356
+ Don' t forget to ``chmod +x`` this file when you' re creating it.
357
+ ` ` ` bash
358
+ #! /bin/sh
359
+ # /etc/init.d/startup
360
+
361
+ # mount the special pseudo filesytems /proc and /sys
362
+ mount -t proc proc /proc -o nosuid,noexec,nodev
363
+ mount -t sysfs sys /sys -o nosuid,noexec,nodev
364
+ # /dev isn't required if we boot without initramfs because the kernel
365
+ # will have done this for us but it doesn't hurt
366
+ mount -t devtmpfs dev /dev -o mode=0755,nosuid
367
+ mkdir -p /dev/pts /dev/shm
368
+ # /dev/pts contains pseudo-terminals, gid 5 should be the
369
+ # tty user group
370
+ mount -t devpts devpts /dev/pts -o mode=0620,gid=5,nosuid,noexec
371
+ # /run contains runtime files like pid files and domain sockets
372
+ # they don't need to be stored on the disk, we'll store them in RAM
373
+ mount -t tmpfs run /run -o mode=0755,nosuid,nodev
374
+ mount -t tmpfs shm /dev/shm -o mode=1777,nosuid,nodev
375
+
376
+ # the kernel does not read /etc/hostname on it's own
377
+ # you need to write it in /proc/sys/kernel/hostname to set it
378
+ if [[ -f /etc/hostname ]]; then
379
+ cat /etc/hostname > /proc/sys/kernel/hostname
380
+ fi
381
+
382
+ # populate /dev with devices by analyzing /sys
383
+ mdev -s
384
+ echo /sbin/mdev > /proc/sys/kernel/hotplug
385
+
386
+ # the "localhost" loopback network interface is
387
+ # down at boot, we have to set it 'up' or we won't be able to
388
+ # make local network connections
389
+ ip link set up dev lo
390
+
391
+ # mounts all filesystems in /etc/fstab
392
+ mount -a
393
+ # make the root writable if this hasn't been done already
394
+ mount -o remount,rw /
395
+ # end of /etc/init.d/startup
396
+ ` ` `
397
+
398
+ ` ` ` inittab
399
+ # /etc/inittab
400
+ ::sysinit:/etc/init.d/startup
401
+ ::askfirst:-/bin/sh
402
+ ::ctrlaltdel:/bin/umount -a -r
403
+ ::shutdown:/bin/umount -a -r
404
+ ` ` `
0 commit comments