initrd.txt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. This document describes one way to create the initrd directory hierarchy
  2. in order to allow an initrd to be built into your kernel. The trick
  3. here is to steal the initrd file used on your Linux laptop, Ubuntu in
  4. this case. There are probably much better ways of doing this.
  5. That said, here are the commands:
  6. ------------------------------------------------------------------------
  7. cd tools/testing/selftests/rcutorture
  8. zcat /initrd.img > /tmp/initrd.img.zcat
  9. mkdir initrd
  10. cd initrd
  11. cpio -id < /tmp/initrd.img.zcat
  12. ------------------------------------------------------------------------
  13. Another way to create an initramfs image is using "dracut"[1], which is
  14. available on many distros, however the initramfs dracut generates is a cpio
  15. archive with another cpio archive in it, so an extra step is needed to create
  16. the initrd directory hierarchy.
  17. Here are the commands to create a initrd directory for rcutorture using
  18. dracut:
  19. ------------------------------------------------------------------------
  20. dracut --no-hostonly --no-hostonly-cmdline --module "base bash shutdown" /tmp/initramfs.img
  21. cd tools/testing/selftests/rcutorture
  22. mkdir initrd
  23. cd initrd
  24. /usr/lib/dracut/skipcpio /tmp/initramfs.img | zcat | cpio -id < /tmp/initramfs.img
  25. ------------------------------------------------------------------------
  26. Interestingly enough, if you are running rcutorture, you don't really
  27. need userspace in many cases. Running without userspace has the
  28. advantage of allowing you to test your kernel independently of the
  29. distro in place, the root-filesystem layout, and so on. To make this
  30. happen, put the following script in the initrd's tree's "/init" file,
  31. with 0755 mode.
  32. ------------------------------------------------------------------------
  33. #!/bin/sh
  34. [ -d /dev ] || mkdir -m 0755 /dev
  35. [ -d /root ] || mkdir -m 0700 /root
  36. [ -d /sys ] || mkdir /sys
  37. [ -d /proc ] || mkdir /proc
  38. [ -d /tmp ] || mkdir /tmp
  39. mkdir -p /var/lock
  40. mount -t sysfs -o nodev,noexec,nosuid sysfs /sys
  41. mount -t proc -o nodev,noexec,nosuid proc /proc
  42. # Some things don't work properly without /etc/mtab.
  43. ln -sf /proc/mounts /etc/mtab
  44. # Note that this only becomes /dev on the real filesystem if udev's scripts
  45. # are used; which they will be, but it's worth pointing out
  46. if ! mount -t devtmpfs -o mode=0755 udev /dev; then
  47. echo "W: devtmpfs not available, falling back to tmpfs for /dev"
  48. mount -t tmpfs -o mode=0755 udev /dev
  49. [ -e /dev/console ] || mknod --mode=600 /dev/console c 5 1
  50. [ -e /dev/kmsg ] || mknod --mode=644 /dev/kmsg c 1 11
  51. [ -e /dev/null ] || mknod --mode=666 /dev/null c 1 3
  52. fi
  53. mkdir /dev/pts
  54. mount -t devpts -o noexec,nosuid,gid=5,mode=0620 devpts /dev/pts || true
  55. mount -t tmpfs -o "nosuid,size=20%,mode=0755" tmpfs /run
  56. mkdir /run/initramfs
  57. # compatibility symlink for the pre-oneiric locations
  58. ln -s /run/initramfs /dev/.initramfs
  59. # Export relevant variables
  60. export ROOT=
  61. export ROOTDELAY=
  62. export ROOTFLAGS=
  63. export ROOTFSTYPE=
  64. export IP=
  65. export BOOT=
  66. export BOOTIF=
  67. export UBIMTD=
  68. export break=
  69. export init=/sbin/init
  70. export quiet=n
  71. export readonly=y
  72. export rootmnt=/root
  73. export debug=
  74. export panic=
  75. export blacklist=
  76. export resume=
  77. export resume_offset=
  78. export recovery=
  79. for i in /sys/devices/system/cpu/cpu*/online
  80. do
  81. case $i in
  82. '/sys/devices/system/cpu/cpu0/online')
  83. ;;
  84. '/sys/devices/system/cpu/cpu*/online')
  85. ;;
  86. *)
  87. echo 1 > $i
  88. ;;
  89. esac
  90. done
  91. while :
  92. do
  93. sleep 10
  94. done
  95. ------------------------------------------------------------------------
  96. References:
  97. [1]: https://dracut.wiki.kernel.org/index.php/Main_Page
  98. [2]: http://blog.elastocloud.org/2015/06/rapid-linux-kernel-devtest-with-qemu.html
  99. [3]: https://www.centos.org/forums/viewtopic.php?t=51621