create.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #!/usr/bin/env bash
  2. #-
  3. # Copyright © 2010, 2011, 2012
  4. # Thorsten Glaser <tg@mirbsd.org>
  5. # Copyright © 2010, 2011
  6. # Waldemar Brodkorb <wbx@openadk.org>
  7. #
  8. # Provided that these terms and disclaimer and all copyright notices
  9. # are retained or reproduced in an accompanying document, permission
  10. # is granted to deal in this work without restriction, including un‐
  11. # limited rights to use, publicly perform, distribute, sell, modify,
  12. # merge, give away, or sublicence.
  13. #
  14. # This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
  15. # the utmost extent permitted by applicable law, neither express nor
  16. # implied; without malicious intent or gross negligence. In no event
  17. # may a licensor, author or contributor be held liable for indirect,
  18. # direct, other damage, loss, or other issues arising in any way out
  19. # of dealing in the work, even if advised of the possibility of such
  20. # damage or existence of a defect, except proven that it results out
  21. # of said person’s immediate fault when using the work as intended.
  22. #
  23. # Alternatively, this work may be distributed under the Terms of the
  24. # General Public License, any version as published by the Free Soft‐
  25. # ware Foundation.
  26. #-
  27. # Create a hard disc image, bootable using GNU GRUB2, with an ext2fs
  28. # root partition and an OpenADK cfgfs partition.
  29. TOPDIR=$(pwd)
  30. me=$0
  31. case :$PATH: in
  32. (*:$TOPDIR/bin/tools:*) ;;
  33. (*) export PATH=$PATH:$TOPDIR/bin/tools ;;
  34. esac
  35. test -n "$KSH_VERSION" || if ! which mksh >/dev/null 2>&1; then
  36. make package=mksh fetch || exit 1
  37. df=$(make -s package=mksh show=DISTFILES)
  38. rm -rf build_mksh
  39. mkdir -p build_mksh
  40. gzip -dc dl/"$df" | (cd build_mksh; cpio -mid)
  41. cd build_mksh/mksh
  42. bash Build.sh -r -c lto || bash Build.sh -r || exit 1
  43. cp mksh "$TOPDIR"/bin/tools/
  44. cd "$TOPDIR"
  45. rm -rf build_mksh
  46. fi
  47. test -n "$KSH_VERSION" || exec mksh "$me" "$@"
  48. if test -z "$KSH_VERSION"; then
  49. echo >&2 Fatal error: could not run myself with mksh!
  50. exit 255
  51. fi
  52. ### run with mksh from here onwards ###
  53. me=${me##*/}
  54. TOPDIR=$(realpath .)
  55. ostype=$(uname -s)
  56. function usage {
  57. cat >&2 <<EOF
  58. Syntax: $me [-c cfgfssize] [-i imagesize] [-p panictime]
  59. [-s serialspeed] [-t] [-T imagetype] [+U] target.ima source.tgz
  60. Explanation/Defaults:
  61. -c: minimum 0, maximum 5, default 1 (MiB)
  62. -i: total image, default 512 (MiB; max. approx. 2 TiB)
  63. -p: default 10 (seconds; 0 disables; max. 300)
  64. -s: default 115200 (bps, others: 9600 19200 38400 57600)
  65. -t: enable serial console (+t disables it, default)
  66. -T: image type (default raw, others: vdi)
  67. +U: disables using root=UUID= (-U enables it, default)
  68. EOF
  69. exit ${1:-1}
  70. }
  71. cfgfs=1
  72. tgtmib=512
  73. panicreboot=10
  74. speed=115200
  75. serial=0
  76. tgttype=raw
  77. useuuid=1
  78. while getopts "c:hi:p:s:tT:U" ch; do
  79. case $ch {
  80. (c) if (( (cfgfs = OPTARG) < 0 || cfgfs > 5 )); then
  81. print -u2 "$me: -c $OPTARG out of bounds"
  82. usage
  83. fi ;;
  84. (h) usage 0 ;;
  85. (i) if (( (tgtmib = OPTARG) < 7 || tgtmib > 2097150 )); then
  86. print -u2 "$me: -i $OPTARG out of bounds"
  87. usage
  88. fi ;;
  89. (p) if (( (panicreboot = OPTARG) < 0 || panicreboot > 300 )); then
  90. print -u2 "$me: -p $OPTARG out of bounds"
  91. usage
  92. fi ;;
  93. (s) if [[ $OPTARG != @(96|192|384|576|1152)00 ]]; then
  94. print -u2 "$me: serial speed $OPTARG invalid"
  95. usage
  96. fi
  97. speed=$OPTARG ;;
  98. (t) serial=1 ;;
  99. (+t) serial=0 ;;
  100. (T) if [[ $OPTARG != @(raw|vdi) ]]; then
  101. print -u2 "$me: image type $OPTARG invalid"
  102. usage
  103. fi
  104. tgttype=$OPTARG ;;
  105. (U) useuuid=1 ;;
  106. (+U) useuuid=0 ;;
  107. (*) usage 1 ;;
  108. }
  109. done
  110. shift $((OPTIND - 1))
  111. (( $# == 2 )) || usage 1
  112. f=0
  113. tools='bc genext2fs'
  114. [[ $tgttype = vdi ]] && tools="$tools VBoxManage"
  115. for tool in $tools; do
  116. print -n Checking if $tool is installed...
  117. if whence -p $tool >/dev/null; then
  118. print " okay"
  119. else
  120. print " failed"
  121. f=1
  122. fi
  123. done
  124. (( f )) && exit 1
  125. if bc --help >/dev/null 2>&1; then
  126. # GNU bc shows a “welcome message” which irritates scripts
  127. bc='bc -q'
  128. else
  129. bc=bc
  130. fi
  131. tgt=$1
  132. [[ $tgt = /* ]] || tgt=$PWD/$tgt
  133. src=$2
  134. if [[ ! -f $src ]]; then
  135. print -u2 "'$src' is not a file, exiting"
  136. exit 1
  137. fi
  138. if ! T=$(mktemp -d /tmp/openadk.XXXXXXXXXX); then
  139. print -u2 Error creating temporary directory.
  140. exit 1
  141. fi
  142. print "Installing $src on $tgt."
  143. cyls=$tgtmib
  144. heads=64
  145. secs=32
  146. if stat -qs .>/dev/null 2>&1; then
  147. statcmd='stat -f %z' # BSD stat (or so we assume)
  148. else
  149. statcmd='stat -c %s' # GNU stat
  150. fi
  151. tar -xOzf "$src" usr/share/grub-bin/core.img >"$T/core.img"
  152. integer coreimgsz=$($statcmd "$T/core.img")
  153. if (( coreimgsz < 1024 )); then
  154. print -u2 core.img is probably too small: $coreimgsz
  155. rm -rf "$T"
  156. exit 1
  157. fi
  158. if (( coreimgsz > 65024 )); then
  159. print -u2 core.img is larger than 64K-512: $coreimgsz
  160. rm -rf "$T"
  161. exit 1
  162. fi
  163. (( coreendsec = (coreimgsz + 511) / 512 ))
  164. corestartsec=1
  165. corepatchofs=$((0x414))
  166. # partition offset: at least coreendsec+1 but aligned on a multiple of secs
  167. (( partofs = ((coreendsec / secs) + 1) * secs ))
  168. # calculate size of ext2fs in KiB as image size minus cfgfs minus firsttrack
  169. ((# partfssz = ((cyls - cfgfs) * 64 * 32 - partofs) / 2 ))
  170. print Preparing MBR and GRUB2...
  171. dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
  172. echo $corestartsec $coreendsec | mksh "$TOPDIR/scripts/bootgrub.mksh" \
  173. -A -g $((cyls - cfgfs)):$heads:$secs -M 1:0x83 -O $partofs | \
  174. dd of="$T/firsttrack" conv=notrunc 2>/dev/null
  175. dd if="$T/core.img" of="$T/firsttrack" conv=notrunc seek=$corestartsec \
  176. 2>/dev/null
  177. # set partition where it can find /boot/grub
  178. print -n '\0\0\0\0' | \
  179. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$corepatchofs 2>/dev/null
  180. # create cfgfs partition (mostly taken from bootgrub.mksh)
  181. set -A thecode
  182. typeset -Uui8 thecode
  183. mbrpno=0
  184. set -A g_code $cyls $heads $secs
  185. (( pssz = cfgfs * g_code[1] * g_code[2] ))
  186. (( pofs = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  187. set -A o_code # g_code equivalent for partition offset
  188. (( o_code[2] = pofs % g_code[2] + 1 ))
  189. (( o_code[1] = pofs / g_code[2] ))
  190. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  191. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  192. # boot flag; C/H/S offset
  193. thecode[mbrpno++]=0x00
  194. (( thecode[mbrpno++] = o_code[1] - 1 ))
  195. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  196. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  197. (( thecode[mbrpno++] = cylno & 0x00FF ))
  198. # partition type; C/H/S end
  199. (( thecode[mbrpno++] = 0x88 ))
  200. (( thecode[mbrpno++] = g_code[1] - 1 ))
  201. (( cylno = g_code[0] > 1024 ? 1023 : g_code[0] - 1 ))
  202. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  203. (( thecode[mbrpno++] = cylno & 0x00FF ))
  204. # partition offset, size (LBA)
  205. (( thecode[mbrpno++] = pofs & 0xFF ))
  206. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  207. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  208. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  209. (( thecode[mbrpno++] = pssz & 0xFF ))
  210. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  211. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  212. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  213. # write partition table entry
  214. ostr=
  215. curptr=0
  216. while (( curptr < 16 )); do
  217. ostr=$ostr\\0${thecode[curptr++]#8#}
  218. done
  219. print -n "$ostr" | \
  220. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) 2>/dev/null
  221. print Extracting installation archive...
  222. mkdir "$T/src"
  223. gzip -dc "$src" | (cd "$T/src"; tar -xpf -)
  224. cd "$T/src"
  225. rnddev=/dev/urandom
  226. [[ -c /dev/arandom ]] && rnddev=/dev/arandom
  227. dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
  228. print Fixing up permissions...
  229. #chown 0:0 tmp
  230. chmod 1777 tmp
  231. chmod 4755 bin/busybox
  232. [[ -f usr/bin/Xorg ]] && chmod 4755 usr/bin/Xorg
  233. [[ -f usr/bin/sudo ]] && chmod 4755 usr/bin/sudo
  234. print Configuring GRUB2 bootloader...
  235. mkdir -p boot/grub
  236. (
  237. print set default=0
  238. print set timeout=1
  239. if (( serial )); then
  240. print serial --unit=0 --speed=$speed
  241. print terminal_output serial
  242. print terminal_input serial
  243. consargs="console=ttyS0,$speed console=tty0"
  244. else
  245. print terminal_output console
  246. print terminal_input console
  247. consargs="console=tty0"
  248. fi
  249. print
  250. print 'menuentry "GNU/Linux (OpenADK)" {'
  251. linuxargs="root=/dev/sda1 $consargs"
  252. (( panicreboot )) && linuxargs="$linuxargs panic=$panicreboot"
  253. print "\tlinux /boot/kernel $linuxargs"
  254. print '}'
  255. ) >boot/grub/grub.cfg
  256. set -A grubfiles
  257. ngrubfiles=0
  258. for a in usr/lib/grub/*-pc/{*.mod,efiemu??.o,command.lst,moddep.lst,fs.lst,handler.lst,parttool.lst}; do
  259. [[ -e $a ]] && grubfiles[ngrubfiles++]=$a
  260. done
  261. cp "${grubfiles[@]}" boot/grub/
  262. print "Creating ext2fs filesystem image..."
  263. cd "$T"
  264. f=0
  265. genext2fs -U -b $((partfssz)) -d src fsimg || f=1
  266. if (( !f )); then
  267. # use bc(1): this may be over the shell’s 32-bit arithmetics
  268. wantsz=$($bc <<<"$((partfssz))*1024")
  269. gotsz=$($statcmd fsimg)
  270. if [[ $wantsz != "$gotsz" ]]; then
  271. print -u2 "Error: want $wantsz bytes, got $gotsz bytes!"
  272. f=1
  273. fi
  274. fi
  275. if (( f )); then
  276. print -u2 "Error creating ext2fs filesystem image"
  277. cd /
  278. rm -rf "$T"
  279. exit 1
  280. fi
  281. # delete source tree, to save disc space
  282. rm -rf src
  283. if [[ $tgttype = raw ]]; then
  284. tgttmp=$tgt
  285. else
  286. tgttmp=$T/dst.ima
  287. fi
  288. print "Putting together raw output image $tgttmp..."
  289. dd if=/dev/zero bs=1048576 count=$cfgfs 2>/dev/null | \
  290. cat firsttrack fsimg - >"$tgttmp"
  291. # use bc(1): this may be over the shell’s 32-bit arithmetics
  292. wantsz=$($bc <<<"$tgtmib*1048576")
  293. gotsz=$($statcmd "$tgttmp")
  294. if [[ $wantsz != "$gotsz" ]]; then
  295. print -u2 "Error: want $wantsz bytes, got $gotsz bytes!"
  296. cd /
  297. rm -rf "$T"
  298. exit 1
  299. fi
  300. case $tgttype {
  301. (raw)
  302. ;;
  303. (vdi)
  304. print "Converting raw image to VDI..."
  305. VBoxManage convertdd dst.ima dst.vdi
  306. rm dst.ima
  307. print "Moving VDI image to $tgt..."
  308. mv -f dst.vdi "$tgt"
  309. ;;
  310. }
  311. print Finishing up...
  312. cd "$TOPDIR"
  313. rm -rf "$T"
  314. exit 0