create-image-with-grub.sh 8.6 KB

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