OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.5 KiB

  1. #!/bin/sh
  2. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. #
  5. # Filename: package/.../mkinitrd/mkinitrd.sh
  6. # Copyright (C) 2007 The OpenSDE Project
  7. #
  8. # More information can be found in the files COPYING and README.
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; version 2 of the License. A copy of the
  13. # GNU General Public License can be found in the file COPYING.
  14. # --- SDE-COPYRIGHT-NOTE-END ---
  15. root=
  16. kernever=
  17. template=
  18. running=
  19. mkinitrd_usage() {
  20. cat <<EOT
  21. Usage: ${0##*/} [ -R <root> ] [ -T <template> ] [<kernelver>]
  22. root.....: location of the sandbox to work in (default: /)
  23. template.: file to use as template for this image
  24. (default: \$root/boot/initrd.img)
  25. kernelver: kerner version to use when grabbing the modules.
  26. (default: $( uname -r ))
  27. EOT
  28. }
  29. while [ $# -gt 0 ]; do
  30. case "$1" in
  31. -R) root="$2"; shift ;;
  32. -T) template="$2"; shift ;;
  33. [0-9]*) kernelver="$1" ;;
  34. *) mkinitrd_usage; exit 1 ;;
  35. esac
  36. shift
  37. done
  38. # $root - root of the sandbox
  39. [ "$root" ] || root="/"
  40. if [ ! -d "$root" ]; then
  41. echo "ERROR: '$root' is not a directory"
  42. mkinitrd_usage
  43. exit 2
  44. else
  45. root=$( cd "$root"; pwd -P )
  46. [ "$root" != "/" ] || root=""
  47. [ -z "$root" ] || echo "root: $root"
  48. fi
  49. # $kernelver - kernel version, only useful if we have modules
  50. if [ -z "$kernelver" ]; then
  51. kernelver=$( uname -r )
  52. running=yes
  53. fi
  54. # $template - cpio.gz file to use as base for this initrd
  55. [ "$template" ] || template="${root}/boot/initrd.img"
  56. if [ ! -r "$template" ]; then
  57. echo "ERROR: template '$template' not found."
  58. mkinitrd_usage
  59. exit 3
  60. else
  61. # skip readlink -f as dependency
  62. x="${template##*/}"
  63. [ "$x" != "$template" ] || template="$PWD/$x"
  64. [ "${template:0:1}" == "/" ] || template="$PWD/$template"
  65. template="$( cd "${template%/*}"; pwd -P )/$x"
  66. fi
  67. moddir="${root}/lib/modules/$kernelver"
  68. sysmap="${root}/boot/System.map_$kernelver"
  69. if [ -d "$moddir" ]; then
  70. echo "kernel: $kernelver, module dir: ${moddir#$root/}"
  71. if [ ! -r "$sysmap" ]; then
  72. echo "ERROR: System.map file not found."
  73. mkinitrd_usage
  74. exit 4
  75. fi
  76. else
  77. echo "kernel: $kernelver, no modules found."
  78. moddir=
  79. fi
  80. if [ $UID -ne 0 ]; then
  81. echo "ERROR: only root can run $0."
  82. mkinitrd_usage
  83. exit 5
  84. fi
  85. for tool in mktemp cpio gzip gunzip; do
  86. if [ -z "$( type -p $tool )" ]; then
  87. echo "ERROR: $tool is no available"
  88. exit 6
  89. fi
  90. done
  91. tmpdir=$( mktemp -d )
  92. olddir="$PWD"
  93. cd "$tmpdir"
  94. # here we go, extract the template
  95. echo "Extracting '${template#$root/}' into '$tmpdir'..."
  96. gunzip -c < "$template" | cpio -i
  97. if [ $? -eq 0 ]; then
  98. errno=0
  99. # prepare the environment for the plugins
  100. export root tmpdir kernelver moddir sysmap running
  101. # call the plugins
  102. for x in $( ls -1d $root/usr/lib/mkinitrd/*.sh 2> /dev/null ); do
  103. echo "Calling ${x#$root/usr/lib/mkinitrd/}"
  104. $SHELL "$x" || errno=$?
  105. [ $errno -eq 0 ] || break
  106. done
  107. # repack if everything went well
  108. if [ $errno -eq 0 ]; then
  109. initrd=boot/initrd-$kernelver.img
  110. echo "Expanded size: $( du -sh . | cut -d' ' -f1 )"
  111. echo "Repacking '$tmpdir' into \$root/$initrd"
  112. find . | cpio -o -H newc | gzip -9 > "$root/$initrd.$$"
  113. if [ $? -eq 0 ]; then
  114. mv -f "$root/$initrd.$$" "$root/$initrd"
  115. du -sh "$root/$initrd"
  116. cd "$olddir"; rm -rf "$tmpdir"
  117. echo "done."
  118. exit 0
  119. else
  120. rm -f "$root/$initrd.$$"
  121. fi
  122. fi
  123. fi
  124. cd "$olddir"; rm -rf "$tmpdir"
  125. echo "failed."
  126. exit 1