install.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #!/usr/bin/env bash
  2. #-
  3. # Copyright © 2010-2014
  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. HOST=$(gcc -dumpmachine)
  35. me=$0
  36. case :$PATH: in
  37. (*:$TOPDIR/host_$HOST/usr/bin:*) ;;
  38. (*) export PATH=$PATH:$TOPDIR/host_$HOST/usr/bin ;;
  39. esac
  40. test -n "$KSH_VERSION" || exec mksh "$me" "$@"
  41. if test -z "$KSH_VERSION"; then
  42. echo >&2 Fatal error: could not run myself with mksh!
  43. exit 255
  44. fi
  45. ### run with mksh from here onwards ###
  46. me=${me##*/}
  47. if (( USER_ID )); then
  48. print -u2 Installation is only possible as root!
  49. exit 1
  50. fi
  51. TOPDIR=$(realpath .)
  52. ostype=$(uname -s)
  53. cfgfs=1
  54. noformat=0
  55. quiet=0
  56. serial=0
  57. speed=115200
  58. panicreboot=10
  59. function usage {
  60. cat >&2 <<EOF
  61. Syntax: $me [-c cfgfssize] [-p panictime] [±q] [-s serialspeed]
  62. [±t] -n /dev/sdb image
  63. Defaults: -c 1 -p 10 -s 115200; -t = enable serial console
  64. EOF
  65. exit $1
  66. }
  67. while getopts "c:hp:qs:nt" 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. (s) if [[ $OPTARG != @(96|192|384|576|1152)00 ]]; then
  81. print -u2 "$me: serial speed $OPTARG invalid"
  82. exit 1
  83. fi
  84. speed=$OPTARG ;;
  85. (n) noformat=1 ;;
  86. (t) serial=1 ;;
  87. (+t) serial=0 ;;
  88. (*) usage 1 ;;
  89. }
  90. done
  91. shift $((OPTIND - 1))
  92. (( $# == 2 )) || usage 1
  93. f=0
  94. tools='mke2fs tune2fs'
  95. case $ostype {
  96. (DragonFly|*BSD*)
  97. ;;
  98. (Darwin)
  99. tools="$tools fuse-ext2"
  100. ;;
  101. (Linux)
  102. ;;
  103. (*)
  104. print -u2 Sorry, not ported to the OS "'$ostype'" yet.
  105. exit 1
  106. ;;
  107. }
  108. for tool in $tools; do
  109. print -n Checking if $tool is installed...
  110. if whence -p $tool >/dev/null; then
  111. print " okay"
  112. else
  113. print " failed"
  114. f=1
  115. fi
  116. done
  117. (( f )) && exit 1
  118. tgt=$1
  119. src=$2
  120. if [[ ! -b $tgt ]]; then
  121. print -u2 "'$tgt' is not a block device, exiting"
  122. exit 1
  123. fi
  124. if [[ ! -f $src ]]; then
  125. print -u2 "'$src' is not a file, exiting"
  126. exit 1
  127. fi
  128. (( quiet )) || print "Installing $src on $tgt."
  129. case $ostype {
  130. (DragonFly|*BSD*)
  131. basedev=${tgt%c}
  132. tgt=${basedev}c
  133. part=${basedev}i
  134. match=\'${basedev}\''[a-p]'
  135. function mount_ext2fs {
  136. mount -t ext2fs "$1" "$2"
  137. }
  138. ;;
  139. (Darwin)
  140. basedev=$tgt
  141. part=${basedev}s1
  142. match=\'${basedev}\''?(s+([0-9]))'
  143. function mount_ext2fs {
  144. fuse-ext2 "$1" "$2" -o rw+
  145. sleep 3
  146. }
  147. ;;
  148. (Linux)
  149. basedev=$tgt
  150. part=${basedev}1
  151. match=\'${basedev}\''+([0-9])'
  152. function mount_ext2fs {
  153. mount -t ext2 "$1" "$2"
  154. }
  155. ;;
  156. }
  157. mount |&
  158. while read -p dev rest; do
  159. eval [[ \$dev = $match ]] || continue
  160. print -u2 "Block device $tgt is in use, please umount first."
  161. exit 1
  162. done
  163. if (( !quiet )); then
  164. print "WARNING: This will overwrite $basedev - type Yes to continue!"
  165. read x
  166. [[ $x = Yes ]] || exit 0
  167. fi
  168. dksz=$(dkgetsz "$tgt")
  169. heads=64
  170. secs=32
  171. (( cyls = dksz / heads / secs ))
  172. if (( cyls < (cfgfs + 2) )); then
  173. print -u2 "Size of $tgt is $dksz, this looks fishy?"
  174. exit 1
  175. fi
  176. if stat -qs .>/dev/null 2>&1; then
  177. statcmd='stat -f %z' # BSD stat (or so we assume)
  178. else
  179. statcmd='stat -c %s' # GNU stat
  180. fi
  181. if ! T=$(mktemp -d /tmp/openadk.XXXXXXXXXX); then
  182. print -u2 Error creating temporary directory.
  183. exit 1
  184. fi
  185. tar -xOzf "$src" boot/grub/core.img >"$T/core.img"
  186. integer coreimgsz=$($statcmd "$T/core.img")
  187. if (( coreimgsz < 1024 )); then
  188. print -u2 core.img is probably too small: $coreimgsz
  189. rm -rf "$T"
  190. exit 1
  191. fi
  192. if (( coreimgsz > 65024 )); then
  193. print -u2 core.img is larger than 64K-512: $coreimgsz
  194. rm -rf "$T"
  195. exit 1
  196. fi
  197. (( coreendsec = (coreimgsz + 511) / 512 ))
  198. if [[ $basedev = /dev/svnd+([0-9]) ]]; then
  199. # BSD svnd0 mode: protect sector #1
  200. corestartsec=2
  201. (( ++coreendsec ))
  202. corepatchofs=$((0x614))
  203. else
  204. corestartsec=1
  205. corepatchofs=$((0x414))
  206. fi
  207. # partition offset: at least coreendsec+1 but aligned on a multiple of secs
  208. (( partofs = ((coreendsec / secs) + 1) * secs ))
  209. (( quiet )) || print Preparing MBR and GRUB2...
  210. dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
  211. echo $corestartsec $coreendsec | mksh "$TOPDIR/scripts/bootgrub.mksh" \
  212. -A -g $((cyls-cfgfs)):$heads:$secs -M 1:0x83 -O $partofs | \
  213. dd of="$T/firsttrack" conv=notrunc 2>/dev/null
  214. dd if="$T/core.img" of="$T/firsttrack" conv=notrunc seek=$corestartsec \
  215. 2>/dev/null
  216. # set partition where it can find /boot/grub
  217. print -n '\0\0\0\0' | \
  218. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$corepatchofs 2>/dev/null
  219. # create cfgfs partition (mostly taken from bootgrub.mksh)
  220. set -A thecode
  221. typeset -Uui8 thecode
  222. mbrpno=0
  223. set -A g_code $cyls $heads $secs
  224. (( psz = g_code[0] * g_code[1] * g_code[2] ))
  225. (( pofs = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  226. set -A o_code # g_code equivalent for partition offset
  227. (( o_code[2] = pofs % g_code[2] + 1 ))
  228. (( o_code[1] = pofs / g_code[2] ))
  229. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  230. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  231. # boot flag; C/H/S offset
  232. thecode[mbrpno++]=0x00
  233. (( thecode[mbrpno++] = o_code[1] - 1 ))
  234. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  235. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  236. (( thecode[mbrpno++] = cylno & 0x00FF ))
  237. # partition type; C/H/S end
  238. (( thecode[mbrpno++] = 0x88 ))
  239. (( thecode[mbrpno++] = g_code[1] - 1 ))
  240. (( cylno = g_code[0] > 1024 ? 1023 : g_code[0] - 1 ))
  241. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  242. (( thecode[mbrpno++] = cylno & 0x00FF ))
  243. # partition offset, size (LBA)
  244. (( thecode[mbrpno++] = pofs & 0xFF ))
  245. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  246. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  247. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  248. (( pssz = psz - pofs ))
  249. (( thecode[mbrpno++] = pssz & 0xFF ))
  250. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  251. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  252. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  253. # write partition table entry
  254. ostr=
  255. curptr=0
  256. while (( curptr < 16 )); do
  257. ostr=$ostr\\0${thecode[curptr++]#8#}
  258. done
  259. print -n "$ostr" | \
  260. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) 2>/dev/null
  261. (( quiet )) || print Writing MBR and GRUB2 to target device...
  262. dd if="$T/firsttrack" of="$tgt"
  263. if [[ $basedev = /dev/svnd+([0-9]) ]]; then
  264. (( quiet )) || print "Creating BSD disklabel on target device..."
  265. # c: whole device (must be so)
  266. # i: ext2fs (matching first partition)
  267. # j: cfgfs (matching second partition)
  268. # p: MBR and GRUB2 area (by tradition)
  269. cat >"$T/bsdlabel" <<-EOF
  270. type: vnd
  271. disk: vnd device
  272. label: OpenADK
  273. flags:
  274. bytes/sector: 512
  275. sectors/track: $secs
  276. tracks/cylinder: $heads
  277. sectors/cylinder: $((heads * secs))
  278. cylinders: $cyls
  279. total sectors: $((cyls * heads * secs))
  280. rpm: 3600
  281. interleave: 1
  282. trackskew: 0
  283. cylinderskew: 0
  284. headswitch: 0
  285. track-to-track seek: 0
  286. drivedata: 0
  287. 16 partitions:
  288. c: $((cyls * heads * secs)) 0 unused
  289. i: $(((cyls - cfgfs) * heads * secs - partofs)) $partofs ext2fs
  290. j: $((cfgfs * heads * secs)) $(((cyls - cfgfs) * heads * secs)) unknown
  291. p: $partofs 0 unknown
  292. EOF
  293. disklabel -R ${basedev#/dev/} "$T/bsdlabel"
  294. fi
  295. (( quiet )) || print "Creating ext2fs on ${part}..."
  296. q=
  297. (( quiet )) && q=-q
  298. (( noformat )) || mke2fs $q "$part"
  299. partuuid=$(tune2fs -l "$part" | sed -n '/^Filesystem UUID:[ ]*/s///p')
  300. (( noformat )) || tune2fs -c 0 -i 0 "$part"
  301. (( quiet )) || print Extracting installation archive...
  302. mount_ext2fs "$part" "$T"
  303. gzip -dc "$src" | (cd "$T"; tar -xvpf -)
  304. cd "$T"
  305. rnddev=/dev/urandom
  306. [[ -c /dev/arandom ]] && rnddev=/dev/arandom
  307. dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
  308. (( quiet )) || print Fixing up permissions...
  309. chown 0:0 tmp
  310. chmod 1777 tmp
  311. chmod 4755 bin/busybox
  312. [[ -f usr/bin/Xorg ]] && chmod 4755 usr/bin/Xorg
  313. [[ -f usr/bin/sudo ]] && chmod 4755 usr/bin/sudo
  314. (( quiet )) || print Configuring GRUB2 bootloader...
  315. mkdir -p boot/grub
  316. (
  317. print set default=0
  318. print set timeout=1
  319. if (( serial )); then
  320. print serial --unit=0 --speed=$speed
  321. print terminal_output serial
  322. print terminal_input serial
  323. consargs="console=ttyS0,$speed console=tty0"
  324. else
  325. print terminal_output console
  326. print terminal_input console
  327. consargs="console=tty0"
  328. fi
  329. print
  330. print 'menuentry "GNU/Linux (OpenADK)" {'
  331. linuxargs="root=UUID=$partuuid $consargs"
  332. (( panicreboot )) && linuxargs="$linuxargs panic=$panicreboot"
  333. print "\tlinux /boot/kernel $linuxargs"
  334. print '}'
  335. ) >boot/grub/grub.cfg
  336. (( quiet )) || print Finishing up...
  337. cd "$TOPDIR"
  338. umount "$T"
  339. (( quiet )) || print "\nNote: the rootfs UUID is: $partuuid"
  340. rm -rf "$T"
  341. exit 0