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.

156 lines
3.7 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 - 2009 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. suffix=
  19. running=
  20. mkinitrd_usage() {
  21. cat <<EOT
  22. Usage: ${0##*/} [ -R <root> ] [ -T <template> ] [ -s <suffix>] [<kernelver>]
  23. root.....: location of the sandbox to work in (default: /)
  24. template.: file to use as template for this image
  25. (default: \$root/boot/initrd.img)
  26. kernelver: kerner version to use when grabbing the modules.
  27. (default: $( uname -r ))
  28. EOT
  29. }
  30. while [ $# -gt 0 ]; do
  31. case "$1" in
  32. -R) root="$2"; shift ;;
  33. -T) template="$2"; shift ;;
  34. -s) suffix="$2"; shift ;;
  35. [0-9]*) kernelver="$1" ;;
  36. *) mkinitrd_usage; exit 1 ;;
  37. esac
  38. shift
  39. done
  40. # $root - root of the sandbox
  41. [ "$root" ] || root="/"
  42. if [ ! -d "$root" ]; then
  43. echo "ERROR: '$root' is not a directory"
  44. mkinitrd_usage
  45. exit 2
  46. else
  47. root=$( cd "$root"; pwd -P )
  48. [ "$root" != "/" ] || root=""
  49. [ -z "$root" ] || echo "root: $root"
  50. fi
  51. # $kernelver - kernel version, only useful if we have modules
  52. if [ -z "$kernelver" ]; then
  53. kernelver=$( uname -r )
  54. running=yes
  55. fi
  56. # $template - cpio.gz file to use as base for this initrd
  57. [ "$template" ] || template="${root}/boot/initrd.img"
  58. if [ ! -r "$template" ]; then
  59. echo "ERROR: template '$template' not found."
  60. mkinitrd_usage
  61. exit 3
  62. else
  63. # skip readlink -f as dependency
  64. x="${template##*/}"
  65. [ "$x" != "$template" ] || template="$PWD/$x"
  66. [ "${template:0:1}" == "/" ] || template="$PWD/$template"
  67. template="$( cd "${template%/*}"; pwd -P )/$x"
  68. fi
  69. moddir="${root}/lib/modules/$kernelver"
  70. sysmap="${root}/boot/System.map_$kernelver"
  71. libdir="${root}D_libdir"
  72. if [ -d "$moddir" ]; then
  73. echo "kernel: $kernelver, module dir: ${moddir#$root/}"
  74. if [ ! -r "$sysmap" ]; then
  75. echo "ERROR: System.map file not found."
  76. mkinitrd_usage
  77. exit 4
  78. fi
  79. else
  80. echo "kernel: $kernelver, no modules found."
  81. moddir=
  82. fi
  83. if [ $UID -ne 0 ]; then
  84. echo "ERROR: only root can run $0."
  85. mkinitrd_usage
  86. exit 5
  87. fi
  88. for tool in mktemp cpio gzip gunzip; do
  89. if [ -z "$( type -p $tool )" ]; then
  90. echo "ERROR: $tool is no available"
  91. exit 6
  92. fi
  93. done
  94. tmpdir=$( mktemp -d )
  95. olddir="$PWD"
  96. cd "$tmpdir"
  97. # here we go, extract the template
  98. echo "Extracting '${template#$root/}' into '$tmpdir'..."
  99. gunzip -c < "$template" | cpio -i
  100. if [ $? -eq 0 ]; then
  101. errno=0
  102. # prepare the environment for the plugins
  103. export root tmpdir kernelver moddir libdir sysmap running
  104. # call the plugins
  105. plugindir="etc/mkinitramfs.d"
  106. [ -d "$plugindir" ] || plugindir="$libdir"
  107. for x in "$plugindir"/*.sh; do
  108. [ -s "$x" ] || continue
  109. echo "Calling ${x#$plugindir/}"
  110. $SHELL "$x" || errno=$?
  111. [ $errno -eq 0 ] || break
  112. done
  113. # repack if everything went well
  114. if [ $errno -eq 0 ]; then
  115. initrd="boot/initrd-${suffix:-$kernelver}.img"
  116. echo "Expanded size: $( du -sh . | cut -d' ' -f1 )"
  117. echo "Repacking '$tmpdir' into \$root/$initrd"
  118. find . | grep -v '^./etc/mkinitramfs.d' |
  119. cpio -o -H newc | gzip -9 > "$root/$initrd.$$"
  120. if [ $? -eq 0 ]; then
  121. mv -f "$root/$initrd.$$" "$root/$initrd"
  122. du -sh "$root/$initrd"
  123. cd "$olddir"; rm -rf "$tmpdir"
  124. echo "done."
  125. exit 0
  126. else
  127. rm -f "$root/$initrd.$$"
  128. fi
  129. fi
  130. fi
  131. cd "$olddir"; rm -rf "$tmpdir"
  132. echo "failed."
  133. exit 1