1
0

install.sh 10 KB

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