install.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 |&
  160. while read -p dev rest; do
  161. eval [[ \$dev = $match ]] || continue
  162. print -u2 "Block device $tgt is in use, please umount first."
  163. exit 1
  164. done
  165. if (( !quiet )); then
  166. print "WARNING: This will overwrite $basedev - type Yes to continue!"
  167. read x
  168. [[ $x = Yes ]] || exit 0
  169. fi
  170. dksz=$(dkgetsz "$tgt")
  171. heads=64
  172. secs=32
  173. (( cyls = dksz / heads / secs ))
  174. if (( cyls < (cfgfs + 2) )); then
  175. print -u2 "Size of $tgt is $dksz, this looks fishy?"
  176. exit 1
  177. fi
  178. if stat --help >/dev/null 2>&1; then
  179. statcmd='stat -c %s' # GNU stat
  180. else
  181. statcmd='stat -f %z' # BSD stat (or so we assume)
  182. fi
  183. if ! T=$(mktemp -d /tmp/openadk.XXXXXXXXXX); then
  184. print -u2 Error creating temporary directory.
  185. exit 1
  186. fi
  187. "$TOPDIR/scripts/tar" -xOzf "$src" ./usr/share/grub-bin/core.img >"$T/core.img"
  188. integer coreimgsz=$($statcmd "$T/core.img")
  189. if (( coreimgsz < 1024 )); then
  190. print -u2 core.img is probably too small: $coreimgsz
  191. rm -rf "$T"
  192. exit 1
  193. fi
  194. if (( coreimgsz > 65024 )); then
  195. print -u2 core.img is larger than 64K-512: $coreimgsz
  196. rm -rf "$T"
  197. exit 1
  198. fi
  199. (( coreendsec = (coreimgsz + 511) / 512 ))
  200. # partition offset: at least coreendsec+1 but aligned on a multiple of secs
  201. (( partofs = ((coreendsec / secs) + 1) * secs ))
  202. (( quiet )) || print Preparing MBR and GRUB2...
  203. dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
  204. echo 1 $coreendsec | mksh "$TOPDIR/scripts/bootgrub.mksh" \
  205. -A -g $((cyls-cfgfs)):$heads:$secs -M 1:0x83 -O $partofs | \
  206. dd of="$T/firsttrack" conv=notrunc 2>/dev/null
  207. dd if="$T/core.img" of="$T/firsttrack" conv=notrunc seek=1 2>/dev/null
  208. # set partition where it can find /boot/grub
  209. print -n '\0\0\0\0' | \
  210. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x414)) 2>/dev/null
  211. # create cfgfs partition (mostly taken from bootgrub.mksh)
  212. set -A thecode
  213. typeset -Uui8 thecode
  214. mbrpno=0
  215. set -A g_code $cyls $heads $secs
  216. (( psz = g_code[0] * g_code[1] * g_code[2] ))
  217. (( pofs = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  218. set -A o_code # g_code equivalent for partition offset
  219. (( o_code[2] = pofs % g_code[2] + 1 ))
  220. (( o_code[1] = pofs / g_code[2] ))
  221. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  222. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  223. # boot flag; C/H/S offset
  224. thecode[mbrpno++]=0x00
  225. (( thecode[mbrpno++] = o_code[1] - 1 ))
  226. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  227. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  228. (( thecode[mbrpno++] = cylno & 0x00FF ))
  229. # partition type; C/H/S end
  230. (( thecode[mbrpno++] = 0x88 ))
  231. (( thecode[mbrpno++] = g_code[1] - 1 ))
  232. (( cylno = g_code[0] > 1024 ? 1023 : g_code[0] - 1 ))
  233. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  234. (( thecode[mbrpno++] = cylno & 0x00FF ))
  235. # partition offset, size (LBA)
  236. (( thecode[mbrpno++] = pofs & 0xFF ))
  237. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  238. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  239. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  240. (( pssz = psz - pofs ))
  241. (( thecode[mbrpno++] = pssz & 0xFF ))
  242. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  243. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  244. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  245. # write partition table entry
  246. ostr=
  247. curptr=0
  248. while (( curptr < 16 )); do
  249. ostr=$ostr\\0${thecode[curptr++]#8#}
  250. done
  251. print -n "$ostr" | \
  252. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) 2>/dev/null
  253. (( quiet )) || print Writing MBR and GRUB2 to target device...
  254. dd if="$T/firsttrack" of="$tgt"
  255. (( quiet )) || print "Creating ext2fs on ${part}..."
  256. q=
  257. (( quiet )) && q=-q
  258. mke2fs $q "$part"
  259. #partuuid=$(tune2fs -l /dev/sd0i | sed -n '/^Filesystem UUID:[ ]*/s///p')
  260. (( quiet )) || print Extracting installation archive...
  261. mount_ext2fs "$part" "$T"
  262. gzip -dc "$src" | (cd "$T"; tar -xpf -)
  263. cd "$T"
  264. rnddev=/dev/urandom
  265. [[ -c /dev/arandom ]] && rnddev=/dev/arandom
  266. dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
  267. (( quiet )) || print Fixing up permissions...
  268. chown 0:0 tmp
  269. chmod 1777 tmp
  270. chmod 4755 bin/busybox
  271. (( quiet )) || print Configuring GRUB2 bootloader...
  272. mkdir -p boot/grub
  273. (
  274. print set default=0
  275. print set timeout=1
  276. if (( serial )); then
  277. print serial --unit=0 --speed=$speed
  278. print terminal_output serial
  279. print terminal_input serial
  280. consargs="console=ttyS0,$speed console=tty0"
  281. else
  282. print terminal_output console
  283. print terminal_input console
  284. consargs="console=tty0"
  285. fi
  286. print
  287. print 'menuentry "GNU/Linux (OpenADK)" {'
  288. # print "\tlinux /boot/vmlinuz-adk root=UUID=$partuuid $consargs panic=10"
  289. print "\tlinux /boot/vmlinuz-adk $consargs panic=10"
  290. print '}'
  291. ) >boot/grub/grub.cfg
  292. set -A grubfiles
  293. ngrubfiles=0
  294. for a in usr/lib/grub/*-pc/{*.mod,efiemu??.o,command.lst,moddep.lst,fs.lst,handler.lst,parttool.lst}; do
  295. [[ -e $a ]] && grubfiles[ngrubfiles++]=$a
  296. done
  297. cp "${grubfiles[@]}" boot/grub/
  298. (( quiet )) || print Finishing up...
  299. cd "$TOPDIR"
  300. umount "$T"
  301. rm -rf "$T"
  302. exit 0