kexec.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ..
  2. Setting up kexec
  3. ================
  4. Prerequisites
  5. -------------
  6. To be able to use kexec and initramfs images, we have to make sure the following options have been enabled in the kernel config:
  7. .. code::
  8. :lang: text
  9. CONFIG_BLK_DEV_INITRD=y
  10. CONFIG_RD_GZIP=y
  11. CONFIG_RD_BZIP2=y
  12. CONFIG_RD_LZMA=y
  13. CONFIG_RD_XZ=y
  14. CONFIG_RD_LZO=y
  15. CONFIG_RD_LZ4=y
  16. CONFIG_KEXEC=y
  17. Building an initramfs
  18. ---------------------
  19. Let's first create the general structure of our initramfs:
  20. .. code::
  21. :lang: text
  22. mkdir -p /usr/src/initramfs/{bin,dev,etc,lib,lib64,mnt,proc,root,sbin,sys}
  23. Rather than compiling a lot of the common utilities that can be found in e.g. coreutils, we compile a static version of busybox to install within in our initramfs:
  24. .. code::
  25. :lang: text
  26. USE="static" emerge busybox
  27. cp /usr/bin/busybox /usr/src/initramfs/bin/busybox
  28. In addition to busybox, we will also need kexec-tools to boot our kernel:
  29. .. code::
  30. :lang: text
  31. USE="static" emerge kexec-tools
  32. cp /usr/sbin/kexec /usr/src/initramfs/sbin/kexec
  33. If we run ldd on /usr/sbin/kexec, we can also figure out the dependencies that it will need to run:
  34. .. code::
  35. :lang: text
  36. ldd /usr/sbin/kexec
  37. cp -r /lib/{libc.so.6,libz.so.1,ld-linux-armhf.so.3} /usr/src/initramfs/lib
  38. In /usr/src/initramfs/init, we set up the following init script:
  39. .. code::
  40. :lang: text
  41. #!/bin/busybox sh
  42. mount -t proc none /proc
  43. mount -t sysfs none /sys
  44. mount -t devtmpfs none
  45. busybox sh
  46. umount /dev
  47. exec switch root /mnt/root /sbin/init
  48. The above script will drop us in a busybox shell session before resuming to switch to the mounted rootfs at /mnt/root.
  49. Also, do make sure that is executable:
  50. .. code::
  51. :lang: text
  52. chmod +x /usr/src/initramfs/init
  53. Once everything has set up, we can pack it all into a initramfs as follows:
  54. .. code::
  55. :lang: text
  56. cd /usr/src/initramfs
  57. find . -print0 | cpio --null -ov --format=newc | gzip -9 > ~/initramfs.cpio.gz
  58. Booting a kernel
  59. ----------------
  60. .. code::
  61. :lang: text
  62. cd /usr/src/linux
  63. kexec --dtb=arch/arm/boot/dts/sun7i-a20-olinuxino-lime2.dtb --ramdisk=initramfs.cpio.gz --command-line="console=tty1 rootfs=/dev/mmcblk2p2 rootfstype=ext4 rootwait rw" arch/arm/boot/zImage
  64. Using kexec-tools to boot a uImage also works.
  65. Booting u-boot
  66. --------------
  67. First, we have to clone the u-boot repository and build u-boot:
  68. .. code::
  69. :lang: text
  70. git clone git://git.denx.de/u-boot.git --depth=1 -b v2017.01
  71. cd u-boot
  72. make A20-OLinuXino-Lime2_defconfig
  73. make
  74. Once we have built u-boot, we have to pack it up as a uImage, so that kexec can boot it.
  75. However, before we can pack it up, we have to figure out the load address and the entry address for the board that we are trying to target.
  76. For instance, for the Allwinner boards, we can find the load address as follows:
  77. .. code::
  78. :lang: text
  79. grep CONFIG_SYS_LOAD_ADDR include/configs/sunxi-common.h
  80. grep CONFIG_SYS_TEXT_BASE include/configs/sunxi-common.h
  81. In this case, we'll find that the load address and text base are either configured to be ``0x22000000`` and ``0x2a000000`` or ``0x42000000`` and ``0x4a000000`` respectively.
  82. Closer inspection shows that the Allwinner A20 (sun7i) uses the latter two.
  83. After we have found the load address and text base, we can pack up u-boot:
  84. .. code::
  85. :lang: text
  86. mkimage -A arm -O linux -T kernel -C none -a 0x42000000 -e 0x4a000000 -d u-boot-dtb.bin u-boot.img
  87. TODO: this boots up to the point where u-boot display a bit of information and then just hangs.
  88. References
  89. ----------
  90. * https://wiki.gentoo.org/wiki/Custom_Initramfs
  91. * https://blogs.s-osg.org/use-mainline-u-boot-non-signed-kernels-exynos-chromebooks/
  92. * https://www.riscosopen.org/forum/forums/5/topics/350
  93. * https://github.com/c0d3z3r0/rockboot
  94. * https://prabagaranvt.blogspot.nl/2012/09/kexec.html
  95. * http://elinux.org/images/2/2f/ELC-2010-Damm-Kexec.pdf