install-genext2fs.sh 10 KB

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