getting-started.adoc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. === Getting started
  2. Reserve 12Gb of disk and run:
  3. ....
  4. git clone https://github.com/cirosantilli/linux-kernel-module-cheat
  5. cd linux-kernel-module-cheat
  6. ./configure && ./build && ./run
  7. ....
  8. The first build will take a while (https://stackoverflow.com/questions/10833672/buildroot-environment-with-host-toolchain[GCC], Linux kernel), e.g.:
  9. * 2 hours on a mid end 2012 laptop
  10. * 30 minutes on a high end 2017 desktop
  11. If you don't want to wait, you could also try to compile the examples and run them on your host computer as explained on the link:run-on-host.md["Run on host" section], but as explained on that section, that is dangerous, limited, and will likely not work.
  12. After QEMU opens up, you can start playing with the kernel modules:
  13. ....
  14. root
  15. insmod /hello.ko
  16. insmod /hello2.ko
  17. rmmod hello
  18. rmmod hello2
  19. ....
  20. This should print to the screen:
  21. ....
  22. hello init
  23. hello2 init
  24. hello cleanup
  25. hello2 cleanup
  26. ....
  27. which are `printk` messages from `init` and `cleanup` methods of those modules.
  28. All available modules can be found in the link:kernel_module/[`kernel_module` directory].
  29. ==== Module documentation
  30. ....
  31. head kernel_module/modulename.c
  32. ....
  33. Many of the modules have userland test scripts / executables with the same name as the module, e.g. form inside the guest:
  34. ....
  35. /modulename.sh
  36. /modulename.out
  37. ....
  38. The sources of those tests will further clarify what the corresponding kernel modules does. To find them on the host, do a quick:
  39. ....
  40. git ls-files | grep modulename
  41. ....
  42. ==== Rebuild
  43. If you make changes to the kernel modules or most configurations tracked on this repository, you can just use again:
  44. ....
  45. ./build
  46. ./run
  47. ....
  48. and the modified files will be rebuilt.
  49. If you change any package besides `kernel_module`, you must also request those packages to be reconfigured or rebuilt with extra targets, e.g.:
  50. ....
  51. ./build -t linux-reconfigure -t host-qemu-reconfigure
  52. ....
  53. Those aren't turned on by default because they take quite a few seconds.
  54. ==== Filesystem persistency
  55. The root filesystem is persistent across:
  56. ....
  57. ./run
  58. date >f
  59. sync
  60. poweroff
  61. ....
  62. then:
  63. ....
  64. ./run
  65. cat f
  66. ....
  67. This is particularly useful to re-run shell commands from the history of a previous session with `Ctrl + R`.
  68. However, when you do:
  69. ....
  70. ./build
  71. ....
  72. the disk image gets overwritten by a fresh filesystem and you lose all changes.
  73. Remember that if you forcibly turn QEMU off without `sync` or `poweroff` from inside the VM, e.g. by closing the QEMU window, disk changes may not be saved.
  74. ==== Message control
  75. We use `printk` a lot, and it shows on the QEMU terminal by default. If that annoys you (e.g. you want to see stdout separately), do:
  76. ....
  77. dmesg -n 1
  78. ....
  79. See also: https://superuser.com/questions/351387/how-to-stop-kernel-messages-from-flooding-my-console
  80. You can scroll up a bit on the default TTY with:
  81. ....
  82. Shift + PgUp
  83. ....
  84. but I never managed to increase that buffer:
  85. * https://askubuntu.com/questions/709697/how-to-increase-scrollback-lines-in-ubuntu14-04-2-server-edition
  86. * https://unix.stackexchange.com/questions/346018/how-to-increase-the-scrollback-buffer-size-for-tty
  87. The superior alternative is to use text mode or a telnet connection.
  88. ==== Text mode
  89. Show serial console directly on the current terminal, without opening a QEMU window:
  90. ....
  91. ./run -n
  92. ....
  93. To exit, just do a regular:
  94. ....
  95. poweroff
  96. ....
  97. This mode is very useful to:
  98. * get full panic traces when you start making the kernel crash :-) See also: https://unix.stackexchange.com/questions/208260/how-to-scroll-up-after-a-kernel-panic
  99. * copy and paste commands and stdout output to / from host
  100. * have a large scroll buffer, and be able to search it, e.g. by using GNU `screen` on host
  101. If the system crashes and you can't can quit QEMU with `poweroff`, or if `poweroff` is just too slow for your patience, you can hard kill the VM with
  102. ....
  103. Ctrl-C X
  104. ....
  105. or:
  106. ....
  107. Ctrl-C A
  108. quit
  109. ....
  110. or on host:
  111. ....
  112. ./qemumonitor
  113. quit
  114. ....
  115. or:
  116. ....
  117. echo quit | ./qemumonitor
  118. ....
  119. See also:
  120. * http://stackoverflow.com/questions/14165158/how-to-switch-to-qemu-monitor-console-when-running-with-curses
  121. * https://superuser.com/questions/1087859/how-to-quit-qemu-monitor
  122. * https://superuser.com/questions/488263/problems-switching-to-qemu-control-panel-with-nographics
  123. * https://superuser.com/questions/1087859/how-to-quit-the-qemu-monitor-when-not-using-a-gui/1211516#1211516
  124. Limitations:
  125. * TODO: Ctrl + C kills the emulator for some setups (TODO which what exactly?), and not sent to guest processes. See:
  126. ** https://github.com/cloudius-systems/osv/issues/49
  127. ** https://unix.stackexchange.com/questions/167165/how-to-pass-ctrl-c-in-qemu
  128. +
  129. This is however fortunate when running QEMU with GDB, as the Ctrl + C reaches GDB and breaks.
  130. * Very early kernel messages such as `early console in extract_kernel` only show on the GUI, since at such early stages, not even the serial has been setup.
  131. ==== Automatic startup commands
  132. When debugging a module, it becomes tedious to wait for build and re-type:
  133. ....
  134. root
  135. /modulename.sh
  136. ....
  137. every time.
  138. Instead, you can either run them from a minimal init:
  139. ....
  140. ./run -e 'init=/eval.sh - lkmc_eval="insmod /hello.ko;/poweroff.out"' -n
  141. ....
  142. or run them at the end of the BusyBox init, which does things like setting up networking:
  143. ....
  144. ./run -e '- lkmc_eval="insmod /hello.ko;wget -S google.com;poweroff.out;"'
  145. ....
  146. or add them to a new `init.d` entry:
  147. ....
  148. cp rootfs_overlay/etc/init.d/S98 rootfs_overlay/etc/init.d/S99
  149. vim S99
  150. ./build
  151. ./run
  152. ....
  153. and they will be run automatically before the login prompt.
  154. `S99` is a git tracked convenience symlink to the gitignored `rootfs_overlay/etc/init.d/S99`
  155. Scripts under `/etc/init.d` are run by `/etc/init.d/rcS`, which gets called by the line `::sysinit:/etc/init.d/rcS` in `/etc/inittab`.
  156. ==== Kernel version
  157. We try to use the latest possible kernel major release version.
  158. In QEMU:
  159. ....
  160. cat /proc/version
  161. ....
  162. or in the source:
  163. ....
  164. cd linux
  165. git log | grep -E ' Linux [0-9]+\.' | head
  166. ....
  167. Build configuration can be observed in guest with:
  168. ....
  169. zcat /proc/config.gz
  170. ....
  171. or on host:
  172. ....
  173. cat buildroot/output.*~/build/linux-custom/.config
  174. ....
  175. ==== QEMU GUI is unresponsive
  176. Sometimes in Ubuntu 14.04, after the QEMU SDL GUI starts, it does not get updated after keyboard strokes, and there are artifacts like disappearing text.
  177. We have not managed to track this problem down yet, but the following workaround always works:
  178. ....
  179. Ctrl + Shift + U
  180. Ctrl + C
  181. root
  182. ....
  183. This started happening when we switched to building QEMU through Buildroot, and has not been observed on later Ubuntu.
  184. Using text mode is another workaround if you don't need GUI features.
  185. ==== Debug QEMU
  186. When you start interacting with QEMU hardware, it is useful to see what is going on inside of QEMU itself.
  187. This is of course trivial since QEMU is just an userland program on the host, but we make it a bit easier with:
  188. ....
  189. ./run -q
  190. ....
  191. Then you could:
  192. ....
  193. b edu_mmio_read
  194. c
  195. ....
  196. And in QEMU:
  197. ....
  198. /pci.sh
  199. ....
  200. Just make sure that you never click inside the QEMU window when doing that, otherwise you mouse gets captured forever, and the only solution I can find is to go to a TTY with Ctrl + Alt + F1 and `kill` QEMU.
  201. You can still send key presses to QEMU however even without the mouse capture, just either click on the title bar, or alt tab to give it focus.
  202. ==== Clean the build
  203. You did something crazy, and nothing seems to work anymore?
  204. All builds are stored under `buildroot/`,
  205. The most coarse thing you can do is:
  206. ....
  207. cd buildroot
  208. git checkout -- .
  209. git clean -xdf .
  210. ....
  211. To only nuke one architecture, do:
  212. ....
  213. rm -rf buildroot/output.x86_64~
  214. ....
  215. Only nuke one one package:
  216. ....
  217. rm -rf buildroot/output.x86_64~/build/<package>
  218. ....