mkinitrd.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Create an initrd directory if one does not already exist.
  5. #
  6. # Copyright (C) IBM Corporation, 2013
  7. #
  8. # Author: Connor Shu <Connor.Shu@ibm.com>
  9. D=tools/testing/selftests/rcutorture
  10. # Prerequisite checks
  11. if [ ! -d "$D" ]; then
  12. echo >&2 "$D does not exist: Malformed kernel source tree?"
  13. exit 1
  14. fi
  15. if [ -s "$D/initrd/init" ]; then
  16. echo "$D/initrd/init already exists, no need to create it"
  17. exit 0
  18. fi
  19. # Create a C-language initrd/init infinite-loop program and statically
  20. # link it. This results in a very small initrd.
  21. echo "Creating a statically linked C-language initrd"
  22. cd $D
  23. mkdir -p initrd
  24. cd initrd
  25. cat > init.c << '___EOF___'
  26. #ifndef NOLIBC
  27. #include <unistd.h>
  28. #include <sys/time.h>
  29. #endif
  30. volatile unsigned long delaycount;
  31. int main(int argc, char *argv[])
  32. {
  33. int i;
  34. struct timeval tv;
  35. struct timeval tvb;
  36. printf("Torture-test rudimentary init program started, command line:\n");
  37. for (i = 0; i < argc; i++)
  38. printf(" %s", argv[i]);
  39. printf("\n");
  40. for (;;) {
  41. sleep(1);
  42. /* Need some userspace time. */
  43. if (gettimeofday(&tvb, NULL))
  44. continue;
  45. do {
  46. for (i = 0; i < 1000 * 100; i++)
  47. delaycount = i * i;
  48. if (gettimeofday(&tv, NULL))
  49. break;
  50. tv.tv_sec -= tvb.tv_sec;
  51. if (tv.tv_sec > 1)
  52. break;
  53. tv.tv_usec += tv.tv_sec * 1000 * 1000;
  54. tv.tv_usec -= tvb.tv_usec;
  55. } while (tv.tv_usec < 1000);
  56. }
  57. return 0;
  58. }
  59. ___EOF___
  60. # build using nolibc on supported archs (smaller executable) and fall
  61. # back to regular glibc on other ones.
  62. if echo -e "#if __x86_64__||__i386__||__i486__||__i586__||__i686__" \
  63. "||__ARM_EABI__||__aarch64__||(__mips__ && _ABIO32)" \
  64. "||__powerpc__||(__riscv && __riscv_xlen == 64)" \
  65. "||__s390x__||__loongarch__" \
  66. "\nyes\n#endif" \
  67. | ${CROSS_COMPILE}gcc -E -nostdlib -xc - \
  68. | grep -q '^yes'; then
  69. # architecture supported by nolibc
  70. ${CROSS_COMPILE}gcc -fno-asynchronous-unwind-tables -fno-ident \
  71. -nostdlib -include ../../../../include/nolibc/nolibc.h \
  72. -s -static -Os -o init init.c -lgcc
  73. ret=$?
  74. else
  75. ${CROSS_COMPILE}gcc -s -static -Os -o init init.c
  76. ret=$?
  77. fi
  78. if [ "$ret" -ne 0 ]
  79. then
  80. echo "Failed to create a statically linked C-language initrd"
  81. exit "$ret"
  82. fi
  83. rm init.c
  84. echo "Done creating a statically linked C-language initrd"
  85. exit 0