deblob-main 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #! /bin/sh
  2. # Copyright (C) 2008-2012 Alexandre Oliva <lxoliva@fsfla.org>
  3. # This program is part of GNU Linux-libre, a GNU project that
  4. # publishes scripts to clean up Linux so as to make it suitable for
  5. # use in the GNU Project and in Free System Distributions.
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  17. # USA
  18. # deblob-main - prepare a GNU Linux-libre tarball out of a non-libre
  19. # Linux tarball. It expects the Linux release (mver, say 3.0) as the
  20. # first argument, the gnu sub-release (extra) as the second optional
  21. # argument, and the patch release (sver, say .13) as an optional third
  22. # argument. mver and sver are pasted together to form kver.
  23. # linux-$kver.tar.bz2 and deblob-$mver must exist in the current
  24. # directory, and the line that sets kver and extra in deblob-$mver
  25. # must match mver and extra.
  26. # The resulting tarball is put in linux-libre-$kver-gnu$extra.tar.bz2.
  27. # An uncompressed xdelta that produces linux-libre-$kver-gnu$extra.tar
  28. # out of linux-$kver.tar is put in linux-libre-$kver-gnu$extra.xdelta.
  29. # This xdelta can be distributed to enable third parties to easily
  30. # reconstruct the binary tarball starting out of sources downloaded
  31. # from kernel.org, but without distributing non-Free Software
  32. # yourself, because xdelta (unlike patches) is not reversible: the
  33. # removed bits are not present in it at all.
  34. # xdelta version 3 uses different command line syntax, and it switched
  35. # to the more standardized but less efficient vcdiff file format.
  36. # This script will also produce a vcdiff file if xdelta3 is present,
  37. # and it expects the xdelta program to use the version 1 syntax.
  38. # To enable you to check the differences between the tarballs, a patch
  39. # file is generated in linux-libre-$kver-gnu$extra.patch. This patch
  40. # file contains the non-Free blobs, even though in reversed form, so
  41. # its distribution is discouraged.
  42. # The tar files and binary deltas are finally compressed with bzip2,
  43. # and optionally with lzip and xz too, if the compressors are
  44. # available.
  45. # At the end, the script attempts to generate a digital signature for
  46. # the newly-created tarball. This is the last thing the script does,
  47. # so interrupting it at that point to skip the signing won't fail to
  48. # do anything else.
  49. # It is safe to interrupt the script at any other point. When it gets
  50. # a ^C (other than during signing), it starts cleaning up all of its
  51. # temporary and output files. If you insist, it may leave junk
  52. # behind, and then it will refuse to run again before you clean it up
  53. # by hand. It takes extra care to avoid overwriting useful files.
  54. # If deblob-$mver finds any unexpected situation, it will error out,
  55. # and then deblob-main will quit. Pass --force to deblob-main, before
  56. # any other argument, for deblob-main to ignore any such situations.
  57. case $1 in
  58. --force) force=--force; shift;;
  59. *) force=;;
  60. esac
  61. mver=$1 extra=$2 sver=$3
  62. kver=$mver$sver gnu=gnu$extra
  63. deblob= dir=`echo "$0" | sed 's,[^/]*$,,;s,^$,.,;s,/*$,,'`
  64. if test -f linux-$kver.tar; then
  65. zext=tar zcmd=
  66. elif test -f linux-$kver.tar.bz2; then
  67. zext=tar.bz2 zcmd=bunzip2
  68. elif test -f linux-$kver.tar.xz; then
  69. zext=tar.xz zcmd=unxz
  70. elif test -f linux-$kver.tar.lz; then
  71. zext=tar.lz zcmd="lzip -d"
  72. elif test -f linux-$kver.tar.gz; then
  73. zext=tar.gz zcmd=gunzip
  74. elif test -f linux-$kver.tgz; then
  75. zext=tgz zcmd=gunzip
  76. else
  77. echo linux-$kver.tar not found, tried .bz2, .xz, .lz, .gz and .tgz too >&2
  78. exit 1
  79. fi
  80. if test -f deblob-$mver; then
  81. deblob=deblob-$mver
  82. elif test -f deblob; then
  83. deblob=deblob
  84. elif test -f $dir/deblob-$mver; then
  85. cp $dir/deblob-$mver deblob
  86. deblob=deblob
  87. else
  88. echo deblob does not exist >&2
  89. exit 1
  90. fi
  91. x1="kver=$mver extra=$extra"
  92. x2=`grep "^kver=[^ ]* extra=" $deblob`
  93. if test "$x1" = "$x2"; then
  94. :
  95. else
  96. echo deblob script does not match command-line arguments >&2
  97. echo expected: $x1 >&2
  98. echo found : $x2 >&2
  99. exit 1
  100. fi
  101. cleanup=
  102. for f in \
  103. linux-libre-$kver-$gnu.tar.bz2 \
  104. linux-libre-$kver-$gnu.tar.bz2.asc \
  105. linux-libre-$kver-$gnu.tar.bz2.sign \
  106. linux-libre-$kver-$gnu.tar.xz \
  107. linux-libre-$kver-$gnu.tar.xz.asc \
  108. linux-libre-$kver-$gnu.tar.xz.sign \
  109. linux-libre-$kver-$gnu.tar.lz \
  110. linux-libre-$kver-$gnu.tar.lz.asc \
  111. linux-libre-$kver-$gnu.tar.lz.sign \
  112. linux-libre-$kver-$gnu.tar \
  113. linux-libre-$kver-$gnu.tar.asc \
  114. linux-libre-$kver-$gnu.tar.sign \
  115. linux-libre-$kver-$gnu.patch \
  116. linux-libre-$kver-$gnu.log \
  117. linux-libre-$kver-$gnu.vcdiff \
  118. linux-libre-$kver-$gnu.vcdiff.bz2 \
  119. linux-libre-$kver-$gnu.vcdiff.bz2.asc \
  120. linux-libre-$kver-$gnu.vcdiff.bz2.sign \
  121. linux-libre-$kver-$gnu.vcdiff.xz \
  122. linux-libre-$kver-$gnu.vcdiff.xz.asc \
  123. linux-libre-$kver-$gnu.vcdiff.xz.sign \
  124. linux-libre-$kver-$gnu.vcdiff.lz \
  125. linux-libre-$kver-$gnu.vcdiff.lz.asc \
  126. linux-libre-$kver-$gnu.vcdiff.lz.sign \
  127. linux-libre-$kver-$gnu.xdelta \
  128. linux-libre-$kver-$gnu.xdelta.bz2 \
  129. linux-libre-$kver-$gnu.xdelta.bz2.asc \
  130. linux-libre-$kver-$gnu.xdelta.bz2.sign \
  131. linux-libre-$kver-$gnu.xdelta.xz \
  132. linux-libre-$kver-$gnu.xdelta.xz.asc \
  133. linux-libre-$kver-$gnu.xdelta.xz.sign \
  134. linux-libre-$kver-$gnu.xdelta.lz \
  135. linux-libre-$kver-$gnu.xdelta.lz.asc \
  136. linux-libre-$kver-$gnu.xdelta.lz.sign \
  137. ; do
  138. if test -f $f; then
  139. echo $f already exists >&2
  140. exit 1
  141. fi
  142. cleanup="$cleanup $f"
  143. done
  144. for d in \
  145. linux-$kver \
  146. linux-libre-$kver-$gnu \
  147. orig-linux-$kver \
  148. ; do
  149. if test -d $d; then
  150. echo $d already exists >&2
  151. exit 1
  152. fi
  153. cleanup="$cleanup $d"
  154. done
  155. if test -f $dir/deblob-$kver; then
  156. if cmp $dir/deblob-$kver $deblob; then
  157. :
  158. else
  159. echo $dir/deblob-$kver and $deblob are different >&2
  160. exit 1
  161. fi
  162. fi
  163. if test ! -f deblob-check; then
  164. if test -f $dir/deblob-check; then
  165. cp $dir/deblob-check deblob-check
  166. fi
  167. else
  168. if test -f $dir/deblob-check; then
  169. if cmp $dir/deblob-check deblob-check; then
  170. :
  171. else
  172. echo $dir/deblob-check and deblob-check are different >&2
  173. exit 1
  174. fi
  175. fi
  176. fi
  177. trap "status=$?; echo cleaning up...; rm -rf $cleanup; (exit $status); exit" 0 1 2 15
  178. set -e
  179. if test -n "$zcmd"; then
  180. echo Uncompressing linux-$kver.$zext into linux-$kver.tar
  181. rm -rf linux-$kver.tar
  182. cleanup="$cleanup linux-$kver.tar"
  183. $zcmd < linux-$kver.$zext > linux-$kver.tar
  184. fi
  185. echo Extracting linux-$kver.tar into linux-$kver
  186. rm -rf linux-$kver
  187. tar -xf linux-$kver.tar
  188. rm -rf linux-libre-$kver-$gnu linux-libre-$kver-$gnu.tar
  189. echo Copying linux-$kver to linux-libre-$kver-$gnu
  190. cp linux-$kver.tar linux-libre-$kver-$gnu.tar
  191. cp -lR linux-$kver/. linux-libre-$kver-$gnu
  192. rm -f linux-libre-$kver-$gnu.log linux-libre-$kver-$gnu.log.tmp
  193. echo Deblobbing within linux-libre-$kver-$gnu, saving output to linux-libre-$kver-$gnu.log
  194. # We can't just pipe deblob into tee, for then we fail to detect
  195. # error conditions. Use file renaming to tell whether we succeeded.
  196. if (cd linux-libre-$kver-$gnu && /bin/sh ../$deblob $force) 2>&1; then
  197. mv linux-libre-$kver-$gnu.log.tmp linux-libre-$kver-$gnu.log
  198. fi | tee linux-libre-$kver-$gnu.log.tmp
  199. if test ! -f linux-libre-$kver-$gnu.log; then
  200. mv linux-libre-$kver-$gnu.log.tmp linux-libre-$kver-$gnu.log
  201. echo $deblob failed, aborting >&2
  202. exit 1
  203. fi
  204. rm -f linux-libre-$kver-$gnu.patch
  205. # Do not copy these scripts for now, deblob-check regards itself as a blob.
  206. # cp -p $0 $deblob deblob-check linux-libre-$kver-$gnu
  207. echo Generating linux-libre-$kver-$gnu.patch
  208. diff -druN linux-$kver linux-libre-$kver-$gnu > linux-libre-$kver-$gnu.patch || :
  209. echo Removing removed or modified files from linux-libre-$kver-$gnu.tar
  210. diff -rq linux-$kver linux-libre-$kver-$gnu |
  211. sed -n "
  212. s,^Only in \\(linux-$kver\\(/.*\\)\\?\\): \\(.*\\),\1/\3,p;
  213. s,^Files \\(linux-$kver\\)/\\(.*\\) and linux-libre-$kver-$gnu/\\2 differ,\\1/\\2,p;
  214. " |
  215. xargs tar --delete -f linux-libre-$kver-$gnu.tar
  216. echo Adding modified or added files to linux-libre-$kver-$gnu.tar
  217. rm -rf orig-linux-$kver
  218. mv linux-$kver orig-linux-$kver
  219. mv linux-libre-$kver-$gnu linux-$kver
  220. diff -rq orig-linux-$kver linux-$kver |
  221. sed -n "
  222. s,^Files orig-\\(linux-$kver/.*\\) and \\1 differ,\\1,p;
  223. s,^Only in \\(linux-$kver\\(/.*\\)\\?\\): \\(.*\\),\\1/\\3,p;
  224. " |
  225. xargs tar --append -f linux-libre-$kver-$gnu.tar
  226. echo Wiping out extracted trees
  227. rm -rf linux-$kver orig-linux-$kver
  228. echo Creating vcdiff between linux-$kver.tar and linux-libre-$kver-$gnu.tar
  229. xdelta3 -e -9 -S djw -s linux-$kver.tar linux-libre-$kver-$gnu.tar linux-libre-$kver-$gnu.vcdiff || : # don't fail if xdelta3 is not present
  230. echo Creating xdelta between linux-$kver.tar and linux-libre-$kver-$gnu.tar
  231. xdelta delta -0 linux-$kver.tar linux-libre-$kver-$gnu.tar linux-libre-$kver-$gnu.xdelta || : # xdelta returns nonzero on success
  232. echo Compressing binary deltas and linux-libre-$kver-$gnu.tar
  233. rm -f linux-$kver.tar
  234. if test -f linux-libre-$kver-$gnu.vcdiff; then
  235. bzip2 -k9 linux-libre-$kver-$gnu.vcdiff
  236. xz -k9 linux-libre-$kver-$gnu.vcdiff || :
  237. lzip -k9 linux-libre-$kver-$gnu.vcdiff || :
  238. rm -f linux-libre-$kver-$gnu.vcdiff
  239. fi
  240. if test -f linux-libre-$kver-$gnu.xdelta; then
  241. bzip2 -k9 linux-libre-$kver-$gnu.xdelta
  242. xz -k9 linux-libre-$kver-$gnu.xdelta || :
  243. lzip -k9 linux-libre-$kver-$gnu.xdelta || :
  244. rm -f linux-libre-$kver-$gnu.xdelta
  245. fi
  246. bzip2 -k9 linux-libre-$kver-$gnu.tar
  247. xz -k9 linux-libre-$kver-$gnu.tar || :
  248. lzip -k9 linux-libre-$kver-$gnu.tar || :
  249. cleanup=linux-libre-$kver-$gnu.tar
  250. echo Done except for signing, feel free to interrupt
  251. for f in \
  252. linux-libre-$kver-$gnu.tar \
  253. linux-libre-$kver-$gnu.tar.bz2 \
  254. linux-libre-$kver-$gnu.tar.xz \
  255. linux-libre-$kver-$gnu.tar.lz \
  256. linux-libre-$kver-$gnu.vcdiff.bz2 \
  257. linux-libre-$kver-$gnu.vcdiff.xz \
  258. linux-libre-$kver-$gnu.vcdiff.lz \
  259. linux-libre-$kver-$gnu.xdelta.bz2 \
  260. linux-libre-$kver-$gnu.xdelta.xz \
  261. linux-libre-$kver-$gnu.xdelta.lz \
  262. ; do
  263. if test -f $f; then
  264. gpg -a --detach-sign $f
  265. mv $f.asc $f.sign
  266. fi
  267. done
  268. rm -f linux-libre-$kver-$gnu.tar
  269. cleanup=
  270. trap "status=$?; (exit $status); exit" 0 1 2 15
  271. echo All set, please review linux-libre-$kver-$gnu.patch
  272. exit 0