1
0

create.sh 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #!/usr/bin/env bash
  2. #-
  3. # Copyright © 2010, 2011, 2012
  4. # Thorsten Glaser <tg@mirbsd.org>
  5. # Copyright © 2010-2014
  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. ADK_TOPDIR=$(pwd)
  30. HOST=$(gcc -dumpmachine)
  31. me=$0
  32. case :$PATH: in
  33. (*:$ADK_TOPDIR/host_$HOST/usr/bin:*) ;;
  34. (*) export PATH=$PATH:$ADK_TOPDIR/host_$HOST/usr/bin ;;
  35. esac
  36. test -n "$KSH_VERSION" || exec mksh "$me" "$@"
  37. if test -z "$KSH_VERSION"; then
  38. echo >&2 Fatal error: could not run myself with mksh!
  39. exit 255
  40. fi
  41. ### run with mksh from here onwards ###
  42. me=${me##*/}
  43. ADK_TOPDIR=$(realpath .)
  44. ostype=$(uname -s)
  45. function usage {
  46. cat >&2 <<EOF
  47. Syntax: $me [-c cfgfssize] [+g] [-i imagesize] [-p panictime]
  48. [-s serialspeed] [-t] [-T imagetype] [+U] target.ima source.tgz
  49. Explanation/Defaults:
  50. -c: minimum 0, maximum 5, default 1 (MiB)
  51. -g: enable installing GNU GRUB 2
  52. -i: total image, default 512 (MiB; max. approx. 2 TiB)
  53. -p: default 10 (seconds; 0 disables; max. 300)
  54. -s: default 115200 (bps, others: 9600 19200 38400 57600)
  55. -t: enable serial console (+t disables it, default)
  56. -T: image type (default raw, others: vdi)
  57. EOF
  58. exit ${1:-1}
  59. }
  60. cfgfs=1
  61. usegrub=0
  62. tgtmib=512
  63. panicreboot=10
  64. speed=115200
  65. serial=0
  66. tgttype=raw
  67. while getopts "c:ghi:p:s:tT:" ch; do
  68. case $ch {
  69. (c) if (( (cfgfs = OPTARG) < 0 || cfgfs > 5 )); then
  70. print -u2 "$me: -c $OPTARG out of bounds"
  71. usage
  72. fi ;;
  73. (g) usegrub=1 ;;
  74. (h) usage 0 ;;
  75. (i) if (( (tgtmib = OPTARG) < 7 || tgtmib > 2097150 )); then
  76. print -u2 "$me: -i $OPTARG out of bounds"
  77. usage
  78. fi ;;
  79. (p) if (( (panicreboot = OPTARG) < 0 || panicreboot > 300 )); then
  80. print -u2 "$me: -p $OPTARG out of bounds"
  81. usage
  82. fi ;;
  83. (s) if [[ $OPTARG != @(96|192|384|576|1152)00 ]]; then
  84. print -u2 "$me: serial speed $OPTARG invalid"
  85. usage
  86. fi
  87. speed=$OPTARG ;;
  88. (t) serial=1 ;;
  89. (+t) serial=0 ;;
  90. (T) if [[ $OPTARG != @(raw|vdi) ]]; then
  91. print -u2 "$me: image type $OPTARG invalid"
  92. usage
  93. fi
  94. tgttype=$OPTARG ;;
  95. (*) usage 1 ;;
  96. }
  97. done
  98. shift $((OPTIND - 1))
  99. (( $# == 2 )) || usage 1
  100. f=0
  101. tools='bc genext2fs'
  102. [[ $tgttype = vdi ]] && tools="$tools VBoxManage"
  103. for tool in $tools; do
  104. print -n Checking if $tool is installed...
  105. if whence -p $tool >/dev/null; then
  106. print " okay"
  107. else
  108. print " failed"
  109. f=1
  110. fi
  111. done
  112. (( f )) && exit 1
  113. if bc --help >/dev/null 2>&1; then
  114. # GNU bc shows a “welcome message” which irritates scripts
  115. bc='bc -q'
  116. else
  117. bc=bc
  118. fi
  119. tgt=$1
  120. [[ $tgt = /* ]] || tgt=$PWD/$tgt
  121. src=$2
  122. if [[ ! -f $src ]]; then
  123. print -u2 "'$src' is not a file, exiting"
  124. exit 1
  125. fi
  126. if ! T=$(mktemp -d /tmp/openadk.XXXXXXXXXX); then
  127. print -u2 Error creating temporary directory.
  128. exit 1
  129. fi
  130. print "Installing $src on $tgt."
  131. cyls=$tgtmib
  132. heads=64
  133. secs=32
  134. if stat -qs .>/dev/null 2>&1; then
  135. statcmd='stat -f %z' # BSD stat (or so we assume)
  136. else
  137. statcmd='stat -c %s' # GNU stat
  138. fi
  139. if (( usegrub )); then
  140. tar -xOzf "$src" boot/grub/core.img >"$T/core.img"
  141. integer coreimgsz=$($statcmd "$T/core.img")
  142. if (( coreimgsz < 1024 )); then
  143. print -u2 core.img is probably too small: $coreimgsz
  144. rm -rf "$T"
  145. exit 1
  146. fi
  147. if (( coreimgsz > 65024 )); then
  148. print -u2 core.img is larger than 64K-512: $coreimgsz
  149. rm -rf "$T"
  150. exit 1
  151. fi
  152. else
  153. # fake it
  154. integer coreimgsz=1
  155. fi
  156. (( coreendsec = (coreimgsz + 511) / 512 ))
  157. corestartsec=1
  158. corepatchofs=$((0x414))
  159. # partition offset: at least coreendsec+1 but aligned on a multiple of secs
  160. (( partofs = ((coreendsec / secs) + 1) * secs ))
  161. # calculate size of ext2fs in KiB as image size minus cfgfs minus firsttrack
  162. ((# partfssz = ((cyls - cfgfs) * 64 * 32 - partofs) / 2 ))
  163. if (( usegrub )); then
  164. print Preparing MBR and GRUB2...
  165. else
  166. print Preparing partition table...
  167. fi
  168. dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
  169. echo $corestartsec $coreendsec | mksh "$ADK_TOPDIR/scripts/bootgrub.mksh" \
  170. -A -g $((cyls - cfgfs)):$heads:$secs -M 1:0x83 -O $partofs | \
  171. dd of="$T/firsttrack" conv=notrunc 2>/dev/null
  172. if (( usegrub )); then
  173. dd if="$T/core.img" of="$T/firsttrack" conv=notrunc \
  174. seek=$corestartsec 2>/dev/null
  175. # set partition where it can find /boot/grub
  176. print -n '\0\0\0\0' | \
  177. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$corepatchofs \
  178. 2>/dev/null
  179. fi
  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. xz -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. chmod 1777 tmp
  230. [[ -f usr/bin/sudo ]] && chmod 4755 usr/bin/sudo
  231. if (( usegrub )); then
  232. print Configuring GRUB2 bootloader...
  233. mkdir -p boot/grub
  234. (
  235. print set default=0
  236. print set timeout=1
  237. if (( serial )); then
  238. print serial --unit=0 --speed=$speed
  239. print terminal_output serial
  240. print terminal_input serial
  241. consargs="console=ttyS0,$speed console=tty0"
  242. else
  243. print terminal_output console
  244. print terminal_input console
  245. consargs="console=tty0"
  246. fi
  247. print
  248. print 'menuentry "GNU/Linux (OpenADK)" {'
  249. linuxargs="root=/dev/sda1 $consargs"
  250. (( panicreboot )) && linuxargs="$linuxargs panic=$panicreboot"
  251. print "\tlinux /boot/kernel $linuxargs"
  252. print '}'
  253. ) >boot/grub/grub.cfg
  254. fi
  255. print "Creating ext2fs filesystem image..."
  256. cd "$T"
  257. f=0
  258. genext2fs -U -N 32768 -b $((partfssz)) -d src fsimg || f=1
  259. if (( !f )); then
  260. # use bc(1): this may be over the shell’s 32-bit arithmetics
  261. wantsz=$($bc <<<"$((partfssz))*1024")
  262. gotsz=$($statcmd fsimg)
  263. if [[ $wantsz != "$gotsz" ]]; then
  264. print -u2 "Error: want $wantsz bytes, got $gotsz bytes!"
  265. f=1
  266. fi
  267. fi
  268. if (( f )); then
  269. print -u2 "Error creating ext2fs filesystem image"
  270. cd /
  271. rm -rf "$T"
  272. exit 1
  273. fi
  274. # delete source tree, to save disc space
  275. rm -rf src
  276. if [[ $tgttype = raw ]]; then
  277. tgttmp=$tgt
  278. else
  279. tgttmp=$T/dst.ima
  280. fi
  281. print "Putting together raw output image $tgttmp..."
  282. dd if=/dev/zero bs=1048576 count=$cfgfs 2>/dev/null | \
  283. cat firsttrack fsimg - >"$tgttmp"
  284. # use bc(1): this may be over the shell’s 32-bit arithmetics
  285. wantsz=$($bc <<<"$tgtmib*1048576")
  286. gotsz=$($statcmd "$tgttmp")
  287. if [[ $wantsz != "$gotsz" ]]; then
  288. print -u2 "Error: want $wantsz bytes, got $gotsz bytes!"
  289. cd /
  290. rm -rf "$T"
  291. exit 1
  292. fi
  293. case $tgttype {
  294. (raw)
  295. ;;
  296. (vdi)
  297. print "Converting raw image to VDI..."
  298. VBoxManage convertdd dst.ima dst.vdi
  299. rm dst.ima
  300. print "Moving VDI image to $tgt.vdi..."
  301. mv -f dst.vdi "$tgt".vdi
  302. ;;
  303. }
  304. print Finishing up...
  305. cd "$ADK_TOPDIR"
  306. rm -rf "$T"
  307. exit 0