1
0

create.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. TOPDIR=$(pwd)
  30. HOST=$(gcc -dumpmachine)
  31. me=$0
  32. case :$PATH: in
  33. (*:$TOPDIR/host_$HOST/usr/bin:*) ;;
  34. (*) export PATH=$PATH:$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. 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: disables installing GNU GRUB 2 (-g enables it, default)
  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=1
  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. (+g) usegrub=0 ;;
  75. (h) usage 0 ;;
  76. (i) if (( (tgtmib = OPTARG) < 7 || tgtmib > 2097150 )); then
  77. print -u2 "$me: -i $OPTARG out of bounds"
  78. usage
  79. fi ;;
  80. (p) if (( (panicreboot = OPTARG) < 0 || panicreboot > 300 )); then
  81. print -u2 "$me: -p $OPTARG out of bounds"
  82. usage
  83. fi ;;
  84. (s) if [[ $OPTARG != @(96|192|384|576|1152)00 ]]; then
  85. print -u2 "$me: serial speed $OPTARG invalid"
  86. usage
  87. fi
  88. speed=$OPTARG ;;
  89. (t) serial=1 ;;
  90. (+t) serial=0 ;;
  91. (T) if [[ $OPTARG != @(raw|vdi) ]]; then
  92. print -u2 "$me: image type $OPTARG invalid"
  93. usage
  94. fi
  95. tgttype=$OPTARG ;;
  96. (*) usage 1 ;;
  97. }
  98. done
  99. shift $((OPTIND - 1))
  100. (( $# == 2 )) || usage 1
  101. f=0
  102. tools='bc genext2fs'
  103. [[ $tgttype = vdi ]] && tools="$tools VBoxManage"
  104. for tool in $tools; do
  105. print -n Checking if $tool is installed...
  106. if whence -p $tool >/dev/null; then
  107. print " okay"
  108. else
  109. print " failed"
  110. f=1
  111. fi
  112. done
  113. (( f )) && exit 1
  114. if bc --help >/dev/null 2>&1; then
  115. # GNU bc shows a “welcome message” which irritates scripts
  116. bc='bc -q'
  117. else
  118. bc=bc
  119. fi
  120. tgt=$1
  121. [[ $tgt = /* ]] || tgt=$PWD/$tgt
  122. src=$2
  123. if [[ ! -f $src ]]; then
  124. print -u2 "'$src' is not a file, exiting"
  125. exit 1
  126. fi
  127. if ! T=$(mktemp -d /tmp/openadk.XXXXXXXXXX); then
  128. print -u2 Error creating temporary directory.
  129. exit 1
  130. fi
  131. print "Installing $src on $tgt."
  132. cyls=$tgtmib
  133. heads=64
  134. secs=32
  135. if stat -qs .>/dev/null 2>&1; then
  136. statcmd='stat -f %z' # BSD stat (or so we assume)
  137. else
  138. statcmd='stat -c %s' # GNU stat
  139. fi
  140. if (( usegrub )); then
  141. tar -xOzf "$src" usr/share/grub-bin/core.img >"$T/core.img"
  142. integer coreimgsz=$($statcmd "$T/core.img")
  143. if (( coreimgsz < 1024 )); then
  144. print -u2 core.img is probably too small: $coreimgsz
  145. rm -rf "$T"
  146. exit 1
  147. fi
  148. if (( coreimgsz > 65024 )); then
  149. print -u2 core.img is larger than 64K-512: $coreimgsz
  150. rm -rf "$T"
  151. exit 1
  152. fi
  153. else
  154. # fake it
  155. integer coreimgsz=1
  156. fi
  157. (( coreendsec = (coreimgsz + 511) / 512 ))
  158. corestartsec=1
  159. corepatchofs=$((0x414))
  160. # partition offset: at least coreendsec+1 but aligned on a multiple of secs
  161. (( partofs = ((coreendsec / secs) + 1) * secs ))
  162. # calculate size of ext2fs in KiB as image size minus cfgfs minus firsttrack
  163. ((# partfssz = ((cyls - cfgfs) * 64 * 32 - partofs) / 2 ))
  164. if (( usegrub )); then
  165. print Preparing MBR and GRUB2...
  166. else
  167. print Preparing partition table...
  168. fi
  169. dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
  170. echo $corestartsec $coreendsec | mksh "$TOPDIR/scripts/bootgrub.mksh" \
  171. -A -g $((cyls - cfgfs)):$heads:$secs -M 1:0x83 -O $partofs | \
  172. dd of="$T/firsttrack" conv=notrunc 2>/dev/null
  173. if (( usegrub )); then
  174. dd if="$T/core.img" of="$T/firsttrack" conv=notrunc \
  175. seek=$corestartsec 2>/dev/null
  176. # set partition where it can find /boot/grub
  177. print -n '\0\0\0\0' | \
  178. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$corepatchofs \
  179. 2>/dev/null
  180. fi
  181. # create cfgfs partition (mostly taken from bootgrub.mksh)
  182. set -A thecode
  183. typeset -Uui8 thecode
  184. mbrpno=0
  185. set -A g_code $cyls $heads $secs
  186. (( pssz = cfgfs * g_code[1] * g_code[2] ))
  187. (( pofs = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  188. set -A o_code # g_code equivalent for partition offset
  189. (( o_code[2] = pofs % g_code[2] + 1 ))
  190. (( o_code[1] = pofs / g_code[2] ))
  191. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  192. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  193. # boot flag; C/H/S offset
  194. thecode[mbrpno++]=0x00
  195. (( thecode[mbrpno++] = o_code[1] - 1 ))
  196. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  197. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  198. (( thecode[mbrpno++] = cylno & 0x00FF ))
  199. # partition type; C/H/S end
  200. (( thecode[mbrpno++] = 0x88 ))
  201. (( thecode[mbrpno++] = g_code[1] - 1 ))
  202. (( cylno = g_code[0] > 1024 ? 1023 : g_code[0] - 1 ))
  203. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  204. (( thecode[mbrpno++] = cylno & 0x00FF ))
  205. # partition offset, size (LBA)
  206. (( thecode[mbrpno++] = pofs & 0xFF ))
  207. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  208. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  209. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  210. (( thecode[mbrpno++] = pssz & 0xFF ))
  211. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  212. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  213. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  214. # write partition table entry
  215. ostr=
  216. curptr=0
  217. while (( curptr < 16 )); do
  218. ostr=$ostr\\0${thecode[curptr++]#8#}
  219. done
  220. print -n "$ostr" | \
  221. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) 2>/dev/null
  222. print Extracting installation archive...
  223. mkdir "$T/src"
  224. gzip -dc "$src" | (cd "$T/src"; tar -xpf -)
  225. cd "$T/src"
  226. rnddev=/dev/urandom
  227. [[ -c /dev/arandom ]] && rnddev=/dev/arandom
  228. dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
  229. print Fixing up permissions...
  230. #chown 0:0 tmp
  231. chmod 1777 tmp
  232. chmod 4755 bin/busybox
  233. [[ -f usr/bin/Xorg ]] && chmod 4755 usr/bin/Xorg
  234. [[ -f usr/bin/sudo ]] && chmod 4755 usr/bin/sudo
  235. if (( usegrub )); then
  236. print Configuring GRUB2 bootloader...
  237. mkdir -p boot/grub
  238. (
  239. print set default=0
  240. print set timeout=1
  241. if (( serial )); then
  242. print serial --unit=0 --speed=$speed
  243. print terminal_output serial
  244. print terminal_input serial
  245. consargs="console=ttyS0,$speed console=tty0"
  246. else
  247. print terminal_output console
  248. print terminal_input console
  249. consargs="console=tty0"
  250. fi
  251. print
  252. print 'menuentry "GNU/Linux (OpenADK)" {'
  253. linuxargs="root=/dev/sda1 $consargs"
  254. (( panicreboot )) && linuxargs="$linuxargs panic=$panicreboot"
  255. print "\tlinux /boot/kernel $linuxargs"
  256. print '}'
  257. ) >boot/grub/grub.cfg
  258. set -A grubfiles
  259. ngrubfiles=0
  260. for a in usr/lib/grub/*-pc/{*.mod,efiemu??.o,command.lst,moddep.lst,fs.lst,handler.lst,parttool.lst}; do
  261. [[ -e $a ]] && grubfiles[ngrubfiles++]=$a
  262. done
  263. cp "${grubfiles[@]}" boot/grub/
  264. fi
  265. print "Creating ext2fs filesystem image..."
  266. cd "$T"
  267. f=0
  268. genext2fs -U -N 32768 -b $((partfssz)) -d src fsimg || f=1
  269. if (( !f )); then
  270. # use bc(1): this may be over the shell’s 32-bit arithmetics
  271. wantsz=$($bc <<<"$((partfssz))*1024")
  272. gotsz=$($statcmd fsimg)
  273. if [[ $wantsz != "$gotsz" ]]; then
  274. print -u2 "Error: want $wantsz bytes, got $gotsz bytes!"
  275. f=1
  276. fi
  277. fi
  278. if (( f )); then
  279. print -u2 "Error creating ext2fs filesystem image"
  280. cd /
  281. rm -rf "$T"
  282. exit 1
  283. fi
  284. # delete source tree, to save disc space
  285. rm -rf src
  286. if [[ $tgttype = raw ]]; then
  287. tgttmp=$tgt
  288. else
  289. tgttmp=$T/dst.ima
  290. fi
  291. print "Putting together raw output image $tgttmp..."
  292. dd if=/dev/zero bs=1048576 count=$cfgfs 2>/dev/null | \
  293. cat firsttrack fsimg - >"$tgttmp"
  294. # use bc(1): this may be over the shell’s 32-bit arithmetics
  295. wantsz=$($bc <<<"$tgtmib*1048576")
  296. gotsz=$($statcmd "$tgttmp")
  297. if [[ $wantsz != "$gotsz" ]]; then
  298. print -u2 "Error: want $wantsz bytes, got $gotsz bytes!"
  299. cd /
  300. rm -rf "$T"
  301. exit 1
  302. fi
  303. case $tgttype {
  304. (raw)
  305. ;;
  306. (vdi)
  307. print "Converting raw image to VDI..."
  308. VBoxManage convertdd dst.ima dst.vdi
  309. rm dst.ima
  310. print "Moving VDI image to $tgt..."
  311. mv -f dst.vdi "$tgt".vdi
  312. ;;
  313. }
  314. print Finishing up...
  315. cd "$TOPDIR"
  316. rm -rf "$T"
  317. exit 0