install.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #!/usr/bin/env bash
  2. #-
  3. # Copyright © 2010
  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. # Prepare a USB stick or CF/SD/MMC card or hard disc for installation
  27. # of OpenADK:
  28. # • install a Master Boot Record containing a MirBSD PBR loading GRUB
  29. # • write GRUB2 core.img just past the MBR
  30. # • create a root partition with ext2fs and extract the OpenADK image
  31. # just built there
  32. # • create a cfgfs partition
  33. TOPDIR=$(pwd)
  34. me=$0
  35. case :$PATH: in
  36. (*:$TOPDIR/bin/tools:*) ;;
  37. (*) export PATH=$PATH:$TOPDIR/bin/tools ;;
  38. esac
  39. test -n "$KSH_VERSION" || if ! which mksh >/dev/null 2>&1; then
  40. make package=mksh fetch || exit 1
  41. df=$(cd package/mksh; TOPDIR="$TOPDIR" gmake show=DISTFILES)
  42. mkdir -p build_mksh
  43. gzip -dc dl/"$df" | (cd build_mksh; cpio -mid)
  44. cd build_mksh/mksh
  45. bash Build.sh -r || exit 1
  46. cp mksh "$TOPDIR"/bin/tools/
  47. cd "$TOPDIR"
  48. rm -rf build_mksh
  49. fi
  50. test -n "$KSH_VERSION" || exec mksh "$me" "$@"
  51. if test -z "$KSH_VERSION"; then
  52. echo >&2 Fatal error: could not run myself with mksh!
  53. exit 255
  54. fi
  55. ### run with mksh from here onwards ###
  56. me=${me##*/}
  57. if (( USER_ID )); then
  58. print -u2 Installation is only possible as root!
  59. exit 1
  60. fi
  61. TOPDIR=$(realpath .)
  62. ostype=$(uname -s)
  63. cfgfs=1
  64. quiet=0
  65. serial=0
  66. speed=115200
  67. function usage {
  68. cat >&2 <<EOF
  69. Syntax: $me [-c cfgfssize] [±qt] [-s serialspeed] /dev/sdb image
  70. Defaults: -c 1 -s 115200
  71. EOF
  72. exit $1
  73. }
  74. while getopts "c:hqs:t" ch; do
  75. case $ch {
  76. (c) if (( (cfgfs = OPTARG) < 0 || cfgfs > 5 )); then
  77. print -u2 "$me: -c $OPTARG out of bounds"
  78. exit 1
  79. fi ;;
  80. (h) usage 0 ;;
  81. (q) quiet=1 ;;
  82. (+q) quiet=0 ;;
  83. (s) if [[ $OPTARG != @(96|192|384|576|1152)00 ]]; then
  84. print -u2 "$me: serial speed $OPTARG invalid"
  85. exit 1
  86. fi
  87. speed=$OPTARG ;;
  88. (t) serial=1 ;;
  89. (+t) serial=0 ;;
  90. (*) usage 1 ;;
  91. }
  92. done
  93. shift $((OPTIND - 1))
  94. (( $# == 2 )) || usage 1
  95. f=0
  96. tools='mke2fs tune2fs'
  97. case $ostype {
  98. (DragonFly|*BSD*)
  99. ;;
  100. (Darwin)
  101. tools="$tools fuse-ext2"
  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 [[ ! -b $tgt ]]; then
  123. print -u2 "'$tgt' is not a block device, exiting"
  124. exit 1
  125. fi
  126. if [[ ! -f $src ]]; then
  127. print -u2 "'$src' is not a file, exiting"
  128. exit 1
  129. fi
  130. (( quiet )) || print "Installing $src on $tgt."
  131. case $ostype {
  132. (DragonFly|*BSD*)
  133. basedev=${tgt%c}
  134. tgt=${basedev}c
  135. part=${basedev}i
  136. match=\'${basedev}\''[a-p]'
  137. function mount_ext2fs {
  138. mount -t ext2fs "$1" "$2"
  139. }
  140. ;;
  141. (Darwin)
  142. basedev=$tgt
  143. part=${basedev}s1
  144. match=\'${basedev}\''?(s+([0-9]))'
  145. function mount_ext2fs {
  146. fuse-ext2 "$1" "$2" -o force
  147. sleep 3
  148. }
  149. ;;
  150. (Linux)
  151. basedev=$tgt
  152. part=${basedev}1
  153. match=\'${basedev}\''+([0-9])'
  154. function mount_ext2fs {
  155. mount -t ext2 "$1" "$2"
  156. }
  157. ;;
  158. }
  159. mount | while read dev rest; do
  160. [[ $dev = $match ]] || continue
  161. print -u2 "Block device $tgt is in use, please umount first."
  162. exit 1
  163. done
  164. if (( !quiet )); then
  165. print "WARNING: This will overwrite $basedev - type Yes to continue!"
  166. read x
  167. [[ $x = Yes ]] || exit 0
  168. fi
  169. dksz=$(dkgetsz "$tgt")
  170. heads=64
  171. secs=32
  172. (( cyls = dksz / heads / secs ))
  173. if (( cyls < (cfgfs + 2) )); then
  174. print -u2 "Size of $tgt is $dksz, this looks fishy?"
  175. exit 1
  176. fi
  177. if stat --help >/dev/null 2>&1; then
  178. statcmd='stat -c %s' # GNU stat
  179. else
  180. statcmd='stat -f %z' # BSD stat (or so we assume)
  181. fi
  182. if ! T=$(mktemp -d /tmp/openadk.XXXXXXXXXX); then
  183. print -u2 Error creating temporary directory.
  184. exit 1
  185. fi
  186. "$TOPDIR/scripts/tar" -xOzf "$src" ./usr/share/grub-bin/core.img >"$T/core.img"
  187. integer coreimgsz=$($statcmd "$T/core.img")
  188. if (( coreimgsz < 1024 )); then
  189. print -u2 core.img is probably too small: $coreimgsz
  190. rm -rf "$T"
  191. exit 1
  192. fi
  193. if (( coreimgsz > 65024 )); then
  194. print -u2 core.img is larger than 64K-512: $coreimgsz
  195. rm -rf "$T"
  196. exit 1
  197. fi
  198. (( coreendsec = (coreimgsz + 511) / 512 ))
  199. # partition offset: at least coreendsec+1 but aligned on a multiple of secs
  200. (( partofs = ((coreendsec / secs) + 1) * secs ))
  201. (( quiet )) || print Preparing MBR and GRUB2...
  202. dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
  203. echo 1 $coreendsec | mksh "$TOPDIR/scripts/bootgrub.mksh" \
  204. -A -g $((cyls-cfgfs)):$heads:$secs -M 1:0x83 -O $partofs | \
  205. dd of="$T/firsttrack" conv=notrunc 2>/dev/null
  206. dd if="$T/core.img" of="$T/firsttrack" conv=notrunc seek=1 2>/dev/null
  207. # set partition where it can find /boot/grub
  208. print -n '\0\0\0\0' | \
  209. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x414)) 2>/dev/null
  210. # create cfgfs partition (mostly taken from bootgrub.mksh)
  211. set -A thecode
  212. typeset -Uui8 thecode
  213. mbrpno=0
  214. set -A g_code $cyls $heads $secs
  215. (( psz = g_code[0] * g_code[1] * g_code[2] ))
  216. (( pofs = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  217. set -A o_code # g_code equivalent for partition offset
  218. (( o_code[2] = pofs % g_code[2] + 1 ))
  219. (( o_code[1] = pofs / g_code[2] ))
  220. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  221. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  222. # boot flag; C/H/S offset
  223. thecode[mbrpno++]=0x00
  224. (( thecode[mbrpno++] = o_code[1] - 1 ))
  225. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  226. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  227. (( thecode[mbrpno++] = cylno & 0x00FF ))
  228. # partition type; C/H/S end
  229. (( thecode[mbrpno++] = 0x88 ))
  230. (( thecode[mbrpno++] = g_code[1] - 1 ))
  231. (( cylno = g_code[0] > 1024 ? 1023 : g_code[0] - 1 ))
  232. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  233. (( thecode[mbrpno++] = cylno & 0x00FF ))
  234. # partition offset, size (LBA)
  235. (( thecode[mbrpno++] = pofs & 0xFF ))
  236. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  237. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  238. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  239. (( pssz = psz - pofs ))
  240. (( thecode[mbrpno++] = pssz & 0xFF ))
  241. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  242. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  243. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  244. # write partition table entry
  245. ostr=
  246. curptr=0
  247. while (( curptr < 16 )); do
  248. ostr=$ostr\\0${thecode[curptr++]#8#}
  249. done
  250. print -n "$ostr" | \
  251. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) 2>/dev/null
  252. (( quiet )) || print Writing MBR and GRUB2 to target device...
  253. dd if="$T/firsttrack" of="$tgt"
  254. (( quiet )) || print "Creating ext2fs on ${part}..."
  255. q=
  256. (( quiet )) && q=-q
  257. mke2fs $q "$part"
  258. #partuuid=$(tune2fs -l /dev/sd0i | sed -n '/^Filesystem UUID:[ ]*/s///p')
  259. (( quiet )) || print Extracting installation archive...
  260. mount_ext2fs "$part" "$T"
  261. gzip -dc "$src" | (cd "$T"; tar -xpf -)
  262. cd "$T"
  263. rnddev=/dev/urandom
  264. [[ -c /dev/arandom ]] && rnddev=/dev/arandom
  265. dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
  266. (( quiet )) || print Fixing up permissions...
  267. chown 0:0 tmp
  268. chmod 1777 tmp
  269. chmod 4755 bin/busybox
  270. (( quiet )) || print Configuring GRUB2 bootloader...
  271. mkdir -p boot/grub
  272. (
  273. print set default=0
  274. print set timeout=1
  275. if (( serial )); then
  276. print serial --unit=0 --speed=$speed
  277. print terminal_output serial
  278. print terminal_input serial
  279. consargs="console=ttyS0,$speed console=tty0"
  280. else
  281. print terminal_output console
  282. print terminal_input console
  283. consargs="console=tty0"
  284. fi
  285. print
  286. print 'menuentry "GNU/Linux (OpenADK)" {'
  287. # print "\tlinux /boot/vmlinuz-adk root=UUID=$partuuid $consargs panic=10"
  288. print "\tlinux /boot/vmlinuz-adk $consargs panic=10"
  289. print '}'
  290. ) >boot/grub/grub.cfg
  291. set -A grubfiles
  292. ngrubfiles=0
  293. for a in usr/lib/grub/*-pc/{*.mod,efiemu??.o,command.lst,moddep.lst,fs.lst,handler.lst,parttool.lst}; do
  294. [[ -e $a ]] && grubfiles[ngrubfiles++]=$a
  295. done
  296. cp "${grubfiles[@]}" boot/grub/
  297. (( quiet )) || print Finishing up...
  298. cd "$TOPDIR"
  299. umount "$T"
  300. rm -rf "$T"
  301. exit 0