1
0

create.sh 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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=mksh-R48b.tgz
  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] [+g] [-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. +g: disables installing GNU GRUB 2 (-g enables it, default)
  63. -i: total image, default 512 (MiB; max. approx. 2 TiB)
  64. -p: default 10 (seconds; 0 disables; max. 300)
  65. -s: default 115200 (bps, others: 9600 19200 38400 57600)
  66. -t: enable serial console (+t disables it, default)
  67. -T: image type (default raw, others: vdi)
  68. EOF
  69. exit ${1:-1}
  70. }
  71. cfgfs=1
  72. usegrub=1
  73. tgtmib=512
  74. panicreboot=10
  75. speed=115200
  76. serial=0
  77. tgttype=raw
  78. while getopts "c:ghi:p:s:tT:" 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. (g) usegrub=1 ;;
  85. (+g) usegrub=0 ;;
  86. (h) usage 0 ;;
  87. (i) if (( (tgtmib = OPTARG) < 7 || tgtmib > 2097150 )); then
  88. print -u2 "$me: -i $OPTARG out of bounds"
  89. usage
  90. fi ;;
  91. (p) if (( (panicreboot = OPTARG) < 0 || panicreboot > 300 )); then
  92. print -u2 "$me: -p $OPTARG out of bounds"
  93. usage
  94. fi ;;
  95. (s) if [[ $OPTARG != @(96|192|384|576|1152)00 ]]; then
  96. print -u2 "$me: serial speed $OPTARG invalid"
  97. usage
  98. fi
  99. speed=$OPTARG ;;
  100. (t) serial=1 ;;
  101. (+t) serial=0 ;;
  102. (T) if [[ $OPTARG != @(raw|vdi) ]]; then
  103. print -u2 "$me: image type $OPTARG invalid"
  104. usage
  105. fi
  106. tgttype=$OPTARG ;;
  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. if (( usegrub )); then
  152. tar -xOzf "$src" usr/share/grub-bin/core.img >"$T/core.img"
  153. integer coreimgsz=$($statcmd "$T/core.img")
  154. if (( coreimgsz < 1024 )); then
  155. print -u2 core.img is probably too small: $coreimgsz
  156. rm -rf "$T"
  157. exit 1
  158. fi
  159. if (( coreimgsz > 65024 )); then
  160. print -u2 core.img is larger than 64K-512: $coreimgsz
  161. rm -rf "$T"
  162. exit 1
  163. fi
  164. else
  165. # fake it
  166. integer coreimgsz=1
  167. fi
  168. (( coreendsec = (coreimgsz + 511) / 512 ))
  169. corestartsec=1
  170. corepatchofs=$((0x414))
  171. # partition offset: at least coreendsec+1 but aligned on a multiple of secs
  172. (( partofs = ((coreendsec / secs) + 1) * secs ))
  173. # calculate size of ext2fs in KiB as image size minus cfgfs minus firsttrack
  174. ((# partfssz = ((cyls - cfgfs) * 64 * 32 - partofs) / 2 ))
  175. if (( usegrub )); then
  176. print Preparing MBR and GRUB2...
  177. else
  178. print Preparing partition table...
  179. fi
  180. dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
  181. echo $corestartsec $coreendsec | mksh "$TOPDIR/scripts/bootgrub.mksh" \
  182. -A -g $((cyls - cfgfs)):$heads:$secs -M 1:0x83 -O $partofs | \
  183. dd of="$T/firsttrack" conv=notrunc 2>/dev/null
  184. if (( usegrub )); then
  185. dd if="$T/core.img" of="$T/firsttrack" conv=notrunc \
  186. seek=$corestartsec 2>/dev/null
  187. # set partition where it can find /boot/grub
  188. print -n '\0\0\0\0' | \
  189. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$corepatchofs \
  190. 2>/dev/null
  191. fi
  192. # create cfgfs partition (mostly taken from bootgrub.mksh)
  193. set -A thecode
  194. typeset -Uui8 thecode
  195. mbrpno=0
  196. set -A g_code $cyls $heads $secs
  197. (( pssz = cfgfs * g_code[1] * g_code[2] ))
  198. (( pofs = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  199. set -A o_code # g_code equivalent for partition offset
  200. (( o_code[2] = pofs % g_code[2] + 1 ))
  201. (( o_code[1] = pofs / g_code[2] ))
  202. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  203. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  204. # boot flag; C/H/S offset
  205. thecode[mbrpno++]=0x00
  206. (( thecode[mbrpno++] = o_code[1] - 1 ))
  207. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  208. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  209. (( thecode[mbrpno++] = cylno & 0x00FF ))
  210. # partition type; C/H/S end
  211. (( thecode[mbrpno++] = 0x88 ))
  212. (( thecode[mbrpno++] = g_code[1] - 1 ))
  213. (( cylno = g_code[0] > 1024 ? 1023 : g_code[0] - 1 ))
  214. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  215. (( thecode[mbrpno++] = cylno & 0x00FF ))
  216. # partition offset, size (LBA)
  217. (( thecode[mbrpno++] = pofs & 0xFF ))
  218. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  219. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  220. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  221. (( thecode[mbrpno++] = pssz & 0xFF ))
  222. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  223. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  224. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  225. # write partition table entry
  226. ostr=
  227. curptr=0
  228. while (( curptr < 16 )); do
  229. ostr=$ostr\\0${thecode[curptr++]#8#}
  230. done
  231. print -n "$ostr" | \
  232. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) 2>/dev/null
  233. print Extracting installation archive...
  234. mkdir "$T/src"
  235. gzip -dc "$src" | (cd "$T/src"; tar -xpf -)
  236. cd "$T/src"
  237. rnddev=/dev/urandom
  238. [[ -c /dev/arandom ]] && rnddev=/dev/arandom
  239. dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
  240. print Fixing up permissions...
  241. #chown 0:0 tmp
  242. chmod 1777 tmp
  243. chmod 4755 bin/busybox
  244. [[ -f usr/bin/Xorg ]] && chmod 4755 usr/bin/Xorg
  245. [[ -f usr/bin/sudo ]] && chmod 4755 usr/bin/sudo
  246. if (( usegrub )); then
  247. print Configuring GRUB2 bootloader...
  248. mkdir -p boot/grub
  249. (
  250. print set default=0
  251. print set timeout=1
  252. if (( serial )); then
  253. print serial --unit=0 --speed=$speed
  254. print terminal_output serial
  255. print terminal_input serial
  256. consargs="console=ttyS0,$speed console=tty0"
  257. else
  258. print terminal_output console
  259. print terminal_input console
  260. consargs="console=tty0"
  261. fi
  262. print
  263. print 'menuentry "GNU/Linux (OpenADK)" {'
  264. linuxargs="root=/dev/sda1 $consargs"
  265. (( panicreboot )) && linuxargs="$linuxargs panic=$panicreboot"
  266. print "\tlinux /boot/kernel $linuxargs"
  267. print '}'
  268. ) >boot/grub/grub.cfg
  269. set -A grubfiles
  270. ngrubfiles=0
  271. for a in usr/lib/grub/*-pc/{*.mod,efiemu??.o,command.lst,moddep.lst,fs.lst,handler.lst,parttool.lst}; do
  272. [[ -e $a ]] && grubfiles[ngrubfiles++]=$a
  273. done
  274. cp "${grubfiles[@]}" boot/grub/
  275. fi
  276. print "Creating ext2fs filesystem image..."
  277. cd "$T"
  278. f=0
  279. genext2fs -U -N 32768 -b $((partfssz)) -d src fsimg || f=1
  280. if (( !f )); then
  281. # use bc(1): this may be over the shell’s 32-bit arithmetics
  282. wantsz=$($bc <<<"$((partfssz))*1024")
  283. gotsz=$($statcmd fsimg)
  284. if [[ $wantsz != "$gotsz" ]]; then
  285. print -u2 "Error: want $wantsz bytes, got $gotsz bytes!"
  286. f=1
  287. fi
  288. fi
  289. if (( f )); then
  290. print -u2 "Error creating ext2fs filesystem image"
  291. cd /
  292. rm -rf "$T"
  293. exit 1
  294. fi
  295. # delete source tree, to save disc space
  296. rm -rf src
  297. if [[ $tgttype = raw ]]; then
  298. tgttmp=$tgt
  299. else
  300. tgttmp=$T/dst.ima
  301. fi
  302. print "Putting together raw output image $tgttmp..."
  303. dd if=/dev/zero bs=1048576 count=$cfgfs 2>/dev/null | \
  304. cat firsttrack fsimg - >"$tgttmp"
  305. # use bc(1): this may be over the shell’s 32-bit arithmetics
  306. wantsz=$($bc <<<"$tgtmib*1048576")
  307. gotsz=$($statcmd "$tgttmp")
  308. if [[ $wantsz != "$gotsz" ]]; then
  309. print -u2 "Error: want $wantsz bytes, got $gotsz bytes!"
  310. cd /
  311. rm -rf "$T"
  312. exit 1
  313. fi
  314. case $tgttype {
  315. (raw)
  316. ;;
  317. (vdi)
  318. print "Converting raw image to VDI..."
  319. VBoxManage convertdd dst.ima dst.vdi
  320. rm dst.ima
  321. print "Moving VDI image to $tgt..."
  322. mv -f dst.vdi "$tgt".vdi
  323. ;;
  324. }
  325. print Finishing up...
  326. cd "$TOPDIR"
  327. rm -rf "$T"
  328. exit 0