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.

154 lines
3.6 KiB

  1. #!/bin/sh
  2. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. #
  5. # Filename: package/.../mkinitramfs/install/D%sbindir_mkinitramfs.sh
  6. # Copyright (C) 2007 - 2008 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. libdir="${root}D_libdir"
  70. if [ -d "$moddir" ]; then
  71. echo "kernel: $kernelver, module dir: ${moddir#$root/}"
  72. if [ ! -r "$sysmap" ]; then
  73. echo "ERROR: System.map file not found."
  74. mkinitrd_usage
  75. exit 4
  76. fi
  77. else
  78. echo "kernel: $kernelver, no modules found."
  79. moddir=
  80. fi
  81. if [ $UID -ne 0 ]; then
  82. echo "ERROR: only root can run $0."
  83. mkinitrd_usage
  84. exit 5
  85. fi
  86. for tool in mktemp cpio gzip gunzip; do
  87. if [ -z "$( type -p $tool )" ]; then
  88. echo "ERROR: $tool is no available"
  89. exit 6
  90. fi
  91. done
  92. tmpdir=$( mktemp -d )
  93. olddir="$PWD"
  94. cd "$tmpdir"
  95. # here we go, extract the template
  96. echo "Extracting '${template#$root/}' into '$tmpdir'..."
  97. gunzip -c < "$template" | cpio -i
  98. if [ $? -eq 0 ]; then
  99. errno=0
  100. # prepare the environment for the plugins
  101. export root tmpdir kernelver moddir libdir sysmap running
  102. # call the plugins
  103. plugindir="etc/mkinitramfs.d"
  104. [ -d "$plugindir" ] || plugindir="$libdir"
  105. for x in "$plugindir"/*.sh; do
  106. [ -s "$x" ] || continue
  107. echo "Calling ${x#$plugindir/}"
  108. $SHELL "$x" || errno=$?
  109. [ $errno -eq 0 ] || break
  110. done
  111. # repack if everything went well
  112. if [ $errno -eq 0 ]; then
  113. initrd=boot/initrd-$kernelver.img
  114. echo "Expanded size: $( du -sh . | cut -d' ' -f1 )"
  115. echo "Repacking '$tmpdir' into \$root/$initrd"
  116. find . | grep -v '^./etc/mkinitramfs.d' |
  117. cpio -o -H newc | gzip -9 > "$root/$initrd.$$"
  118. if [ $? -eq 0 ]; then
  119. mv -f "$root/$initrd.$$" "$root/$initrd"
  120. du -sh "$root/$initrd"
  121. cd "$olddir"; rm -rf "$tmpdir"
  122. echo "done."
  123. exit 0
  124. else
  125. rm -f "$root/$initrd.$$"
  126. fi
  127. fi
  128. fi
  129. cd "$olddir"; rm -rf "$tmpdir"
  130. echo "failed."
  131. exit 1