README.adoc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. = GRUB
  2. :idprefix:
  3. :idseparator: -
  4. :sectanchors:
  5. :sectlinks:
  6. :sectnumlevels: 6
  7. :sectnums:
  8. :toc: macro
  9. :toclevels: 6
  10. :toc-title:
  11. toc::[]
  12. == Symlinks
  13. This directory relies on the following symlinks to make directory structure modifiable in the future:
  14. . mbrs: directory that contains the Makefile for `bios_hello_world.img` and other MBRs
  15. . bios_hello_world.img.sym: boot sector that says hello world with BIOS
  16. +
  17. The `.sym` extension must be used because otherwise this symlink would be gitignored.
  18. === Introduction
  19. If you have a Linux dual boot, and you see a menu prompting you to choose the OS, there is a good chance that this is GRUB, since it is the most popular bootloader today.
  20. It allows you basic graphical interaction even before starting any OS.
  21. Everything is configurable, from the menu entries to the background image. This is why Ubuntu's GRUB is purple.
  22. The main job for GRUB userspace utilities such as `grub-install` and `update-grub` is to look at the input configuration files, interpret them and write the output configuration information to the correct locations on the hard disk so that they can be found at boot time.
  23. GRUB has knowledge about filesystems, and is able to read configuration files and the disk image from it.
  24. === GRUB versions
  25. GRUB has 2 versions
  26. * 0.97, usually known just as GRUB, or Legacy GRUB.
  27. * GRUB >= 2, which is backwards incompatible, and has more features.
  28. +
  29. GRUB 2 is still beta.
  30. Some distros like Ubuntu have already adopted GRUB 2, while others are still using GRUB for stability concerns.
  31. Determine your GRUB version with:
  32. ....
  33. grub-install -v
  34. ....
  35. Here we discuss GRUB 2.
  36. === Supported architectures
  37. x86 is of course the primary... ARM was recently added in 2.0.2 it seems: https://wiki.linaro.org/LEG/Engineering/Kernel/GRUB
  38. === Configuration files
  39. Input files:
  40. * `/etc/grub.d/*`
  41. * `/etc/default/grub`
  42. Generated files and data after `sudo update-grub`:
  43. * `/boot/grub/grub.cfg`
  44. * MBR bootstrap code
  45. ==== /etc/default/grub
  46. Shell script sourced by `grub-mkconfig`.
  47. Can defined some variables which configure grub, but is otherwise an arbitrary shell script:
  48. ....
  49. sudo vim /etc/default/grub
  50. ....
  51. * `GRUB_DEFAULT`: default OS choice if cursor is not moved:
  52. +
  53. Starts from 0, the order is the same as shown at grub OS choice menu:
  54. +
  55. ....
  56. GRUB_DEFAULT=0
  57. ....
  58. +
  59. The order can be found on the generated `/boot/grub/grub.cfg`: you have to count the number of `menuentry` calls.
  60. +
  61. To select sub-menus, which are created with the `submenu` call on `/boot/grub/grub.cfg`, use:
  62. +
  63. ....
  64. GRUB_DEFAULT='0>1'
  65. ....
  66. +
  67. You can also use OS name instead of a number, e.g.:
  68. +
  69. ....
  70. GRUB_DEFAULT='Ubuntu'
  71. ....
  72. +
  73. For a line from `/boot/grub/grub.cfg` of type:
  74. +
  75. ....
  76. menuentry 'Ubuntu'
  77. ....
  78. * `GRUB_TIMEOUT` : time before auto OS choice in seconds
  79. * `GRUB_CMDLINE_LINUX_DEFAULT`: space separated list of Kernel boot parameters.
  80. +
  81. Sample:
  82. +
  83. ....
  84. GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
  85. ....
  86. +
  87. The parameters will not be discussed here.
  88. +
  89. Those parameters can also be edited from the boot menu for a single session by selecting the partition and clicking `e`.
  90. ** useless options on by default on Ubuntu 12.04 which you should really remove because they hide kernel state and potentially useful debug information:
  91. *** `quiet`: suppress kernel messages.
  92. *** `splash`: shows nice and useless image while the kernel is booting. On by default on Ubuntu 12.04. Remove this useless option,
  93. ==== /etc/grub.d/
  94. Contains executables.
  95. Each one is called in alphabetical order, and its stdout is used by GRUB.
  96. A common choice for custom scripts in Ubuntu 14.04 is `40_custom`.
  97. Create a menu entry:
  98. ....
  99. #!/bin/sh -e
  100. echo "stdout"
  101. echo "stderr" >&2
  102. cat << EOF
  103. menuentry "menuentry title" {
  104. set root=(hd0,1)
  105. -- boot parameters --
  106. }
  107. EOF
  108. ....
  109. You will see `stdout` when running `update-grub`. stderr is ignored.
  110. `set root=(hd0,1)` specifies the partition, here `sda1`. `hd0` means first device, `1` means first partition. Yes, one if 0 based, and the other is 1 based.
  111. `-- boot parameters --` depends on your OS.
  112. Linux example:
  113. ....
  114. linux /boot/vmlinuz
  115. initrd /boot/initrd.img
  116. ....
  117. Windows example:
  118. ....
  119. chainloader (hdX,Y)+1
  120. ....
  121. It is common to add one OS menu entry per file so that it is easy to change their order (just change alphabetical order).
  122. === Configuration scripts
  123. ==== update-grub
  124. Just calls:
  125. ....
  126. grub-mkconfig -o /boot/grub/grub.cfg
  127. ....
  128. ==== grub-mkconfig
  129. Called by `update-grub` as:
  130. ....
  131. grub-mkconfig -o /boot/grub/grub.cfg
  132. ....
  133. Important actions:
  134. * sources `/etc/default/grub`
  135. * sources `/etc/default/grub.d/*.cfg`, which may override options in `/etc/default/grub`
  136. * runs scripts under `/etc/grub.d`, which use the variables defined in the above sourced files
  137. ==== grub-install
  138. Given a `/boot/grub/grub.cfg` in some filesystem, install GRUB to some hard disk.
  139. Interpret input configuration files and update the MBR on the given disk:
  140. ....
  141. sudo grub-install /dev/sda
  142. ....
  143. If for example you install a new Linux distro, and you want to restore your old distro's GRUB configuration, you must log into the old distro and do `grub-install`, therefore telling your system via the MBR to use the installation parameters given on the old distro.
  144. TODO get a minimal example working using a minimal kernel from: https://github.com/cirosantilli/x86-bare-metal-examples:
  145. ....
  146. img="a.img"
  147. dd if=/dev/zero of="$img" bs=1024 count=64
  148. loop="$(sudo losetup -f --show "$img")"
  149. printf 'o\nn\np\n1\n\n\nw\n' | sudo fdisk "$loop"
  150. sudo kpartx -av "$img"
  151. ls /dev/mapper
  152. echo y | mke2fs -t ext4
  153. sudo mount "/dev/mapper/${loop}p1" d
  154. # Need a new Ubuntu.
  155. #sudo losetup --show -f -P test.img
  156. sudo grub-install /dev/loop0
  157. mkdir -p d
  158. mount /dev/loop0 d
  159. #grub-install --boot-directory=d /dev/sdb
  160. ....
  161. ==== grub-mkrescue
  162. Generates a rescue image from a root filesystem.
  163. Example: https://github.com/cirosantilli/x86-bare-metal-examples/blob/48614b45fa6edeb97adbaad942595a4c25216113/multiboot/hello-world/Makefile#L6
  164. You can then burn the output to an USB or CD
  165. Vs `grub-install`: generates a live boot USB / CD, but does not use the USB as a filesystem.
  166. Easier to setup however.
  167. ==== os_prober
  168. Looks for several OS and adds them automatically to GRUB menu.
  169. Recognizes Linux and Windows.
  170. TODO how to use it
  171. === rescue prompt
  172. If things fail really badly, you may be put on a `rescue >` prompt.
  173. You are likely better off reinstalling things correctly in practice. But here go a few commands you can use from there.
  174. https://www.linux.com/learn/tutorials/776643-how-to-rescue-a-non-booting-grub-2-on-linux/
  175. * `ls`
  176. * `ls (hd0,1)/`
  177. * `cat (hd0,1)/etc/issue`
  178. * Boot:
  179. +
  180. ....
  181. set root=(hd0,1)
  182. linux /boot/vmlinuz-3.13.0-29-generic root=/dev/sda1
  183. initrd /boot/initrd.img-3.13.0-29-generic
  184. boot
  185. ....
  186. ==== timeout
  187. No timeout on boot menu:
  188. ....
  189. set timeout=0
  190. ....
  191. ==== default
  192. Default no Nth (zero based) entry of boot menu:
  193. ....
  194. set default="0"
  195. ....
  196. ==== menuentry
  197. The following commands can be used inside a menu entry, e.g.:
  198. ....
  199. menuentry "main" {
  200. }
  201. ....
  202. Point to a multiboot file:
  203. ....
  204. multiboot /boot/main.elf
  205. ....
  206. E.g.: https://github.com/cirosantilli/x86-bare-metal-examples/blob/48614b45fa6edeb97adbaad942595a4c25216113/multiboot/hello-world/iso/boot/grub/grub.cfg
  207. Load a linux kernel with a given root filesystem:
  208. ....
  209. linux /boot/bzImage
  210. initrd /boot/rootfs.cpio.gz
  211. ....
  212. You can pass kernel command line arguments with:
  213. ....
  214. linux /boot/bzImage BOOT_IMAGE=/boot/vmlinuz-3.19.0-28-generic root=UUID=2a49bac4-b9dd-466d-9c0c-c432aa4ca086 ro loop.max_part=15
  215. ....
  216. You can then check that they've appeared under `cat /proc/cmdline`.
  217. === Alternatives
  218. * `syslinux`: Linux specific. Used by default by the kernel, e.g. on 4.2 `make isoimage`.
  219. * LILO: old popular bootloader, largely replaced by GRUB now.
  220. === Legacy
  221. Documentation: http://www.gnu.org/software/grub/manual/legacy/grub.html
  222. ==== kernel
  223. Directive used to boot _both_ multiboot and Linux.
  224. Got split up more or less into `multiboot` and `linux` directives.
  225. === Bibliography
  226. * https://www.gnu.org/software/grub/grub-documentation.html
  227. * http://www.dedoimedo.com/computers/grub-2.html
  228. +
  229. Great configuration tutorial.