create.sh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "$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='mke2fs tune2fs'
  105. case $ostype {
  106. (DragonFly|*BSD*)
  107. ;;
  108. (Darwin)
  109. tools="$tools fuse-ext2"
  110. ;;
  111. (Linux)
  112. ;;
  113. (*)
  114. print -u2 Sorry, not ported to the OS "'$ostype'" yet.
  115. exit 1
  116. ;;
  117. }
  118. for tool in $tools; do
  119. print -n Checking if $tool is installed...
  120. if whence -p $tool >/dev/null; then
  121. print " okay"
  122. else
  123. print " failed"
  124. f=1
  125. fi
  126. done
  127. (( f )) && exit 1
  128. tgt=$1
  129. src=$2
  130. if [[ ! -f $src ]]; then
  131. print -u2 "'$src' is not a file, exiting"
  132. exit 1
  133. fi
  134. (( quiet )) || print "Installing $src on $tgt."
  135. qemu-img create -f raw $tgt 524288k
  136. case $ostype {
  137. (DragonFly|*BSD*)
  138. basedev=${tgt%c}
  139. tgt=${basedev}c
  140. part=${basedev}i
  141. match=\'${basedev}\''[a-p]'
  142. function mount_ext2fs {
  143. mount -t ext2fs "$1" "$2"
  144. }
  145. ;;
  146. (Darwin)
  147. basedev=$tgt
  148. part=${basedev}s1
  149. match=\'${basedev}\''?(s+([0-9]))'
  150. function mount_ext2fs {
  151. fuse-ext2 "$1" "$2" -o rw+
  152. sleep 3
  153. }
  154. ;;
  155. (Linux)
  156. basedev=$tgt
  157. part=${basedev}1
  158. match=\'${basedev}\''+([0-9])'
  159. function mount_ext2fs {
  160. mount -t ext2 "$1" "$2"
  161. }
  162. ;;
  163. }
  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. if stat -qs .>/dev/null 2>&1; then
  170. statcmd='stat -f %z' # BSD stat (or so we assume)
  171. else
  172. statcmd='stat -c %s' # GNU stat
  173. fi
  174. dksz=$(($($statcmd "$tgt")*2))
  175. heads=64
  176. secs=32
  177. (( cyls = dksz / heads / secs ))
  178. if (( cyls < (cfgfs + 2) )); then
  179. print -u2 "Size of $tgt is $dksz, this looks fishy?"
  180. exit 1
  181. fi
  182. if ! T=$(mktemp -d /tmp/openadk.XXXXXXXXXX); then
  183. print -u2 Error creating temporary directory.
  184. exit 1
  185. fi
  186. 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. if [[ $basedev = /dev/svnd+([0-9]) ]]; then
  200. # BSD svnd0 mode: protect sector #1
  201. corestartsec=2
  202. (( ++coreendsec ))
  203. corepatchofs=$((0x614))
  204. else
  205. corestartsec=1
  206. corepatchofs=$((0x414))
  207. fi
  208. # partition offset: at least coreendsec+1 but aligned on a multiple of secs
  209. (( partofs = ((coreendsec / secs) + 1) * secs ))
  210. (( quiet )) || print Preparing MBR and GRUB2...
  211. dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
  212. echo $corestartsec $coreendsec | mksh "$TOPDIR/scripts/bootgrub.mksh" \
  213. -A -g $((cyls-cfgfs)):$heads:$secs -M 1:0x83 -O $partofs | \
  214. dd of="$T/firsttrack" conv=notrunc 2>/dev/null
  215. dd if="$T/core.img" of="$T/firsttrack" conv=notrunc seek=$corestartsec \
  216. 2>/dev/null
  217. # set partition where it can find /boot/grub
  218. print -n '\0\0\0\0' | \
  219. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$corepatchofs 2>/dev/null
  220. # create cfgfs partition (mostly taken from bootgrub.mksh)
  221. set -A thecode
  222. typeset -Uui8 thecode
  223. mbrpno=0
  224. set -A g_code $cyls $heads $secs
  225. (( psz = g_code[0] * g_code[1] * g_code[2] ))
  226. (( pofs = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  227. set -A o_code # g_code equivalent for partition offset
  228. (( o_code[2] = pofs % g_code[2] + 1 ))
  229. (( o_code[1] = pofs / g_code[2] ))
  230. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  231. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  232. # boot flag; C/H/S offset
  233. thecode[mbrpno++]=0x00
  234. (( thecode[mbrpno++] = o_code[1] - 1 ))
  235. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  236. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  237. (( thecode[mbrpno++] = cylno & 0x00FF ))
  238. # partition type; C/H/S end
  239. (( thecode[mbrpno++] = 0x88 ))
  240. (( thecode[mbrpno++] = g_code[1] - 1 ))
  241. (( cylno = g_code[0] > 1024 ? 1023 : g_code[0] - 1 ))
  242. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  243. (( thecode[mbrpno++] = cylno & 0x00FF ))
  244. # partition offset, size (LBA)
  245. (( thecode[mbrpno++] = pofs & 0xFF ))
  246. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  247. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  248. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  249. (( pssz = psz - pofs ))
  250. (( thecode[mbrpno++] = pssz & 0xFF ))
  251. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  252. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  253. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  254. # write partition table entry
  255. ostr=
  256. curptr=0
  257. while (( curptr < 16 )); do
  258. ostr=$ostr\\0${thecode[curptr++]#8#}
  259. done
  260. print -n "$ostr" | \
  261. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) 2>/dev/null
  262. (( quiet )) || print Writing MBR and GRUB2 to target device...
  263. dd if="$T/firsttrack" of="$tgt"
  264. if [[ $basedev = /dev/svnd+([0-9]) ]]; then
  265. (( quiet )) || print "Creating BSD disklabel on target device..."
  266. # c: whole device (must be so)
  267. # i: ext2fs (matching first partition)
  268. # j: cfgfs (matching second partition)
  269. # p: MBR and GRUB2 area (by tradition)
  270. cat >"$T/bsdlabel" <<-EOF
  271. type: vnd
  272. disk: vnd device
  273. label: OpenADK
  274. flags:
  275. bytes/sector: 512
  276. sectors/track: $secs
  277. tracks/cylinder: $heads
  278. sectors/cylinder: $((heads * secs))
  279. cylinders: $cyls
  280. total sectors: $((cyls * heads * secs))
  281. rpm: 3600
  282. interleave: 1
  283. trackskew: 0
  284. cylinderskew: 0
  285. headswitch: 0
  286. track-to-track seek: 0
  287. drivedata: 0
  288. 16 partitions:
  289. c: $((cyls * heads * secs)) 0 unused
  290. i: $(((cyls - cfgfs) * heads * secs - partofs)) $partofs ext2fs
  291. j: $((cfgfs * heads * secs)) $(((cyls - cfgfs) * heads * secs)) unknown
  292. p: $partofs 0 unknown
  293. EOF
  294. disklabel -R ${basedev#/dev/} "$T/bsdlabel"
  295. fi
  296. (( quiet )) || print "Creating ext2fs on ${part}..."
  297. gzip -dc "$src" | (cd "$T"; tar -xvpf -)
  298. cd "$T"
  299. rnddev=/dev/urandom
  300. [[ -c /dev/arandom ]] && rnddev=/dev/arandom
  301. dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
  302. (( quiet )) || print Fixing up permissions...
  303. chown 0:0 tmp
  304. chmod 1777 tmp
  305. chmod 4755 bin/busybox
  306. [[ -f usr/bin/Xorg ]] && chmod 4755 usr/bin/Xorg
  307. [[ -f usr/bin/sudo ]] && chmod 4755 usr/bin/sudo
  308. (( quiet )) || print Configuring GRUB2 bootloader...
  309. mkdir -p boot/grub
  310. (
  311. print set default=0
  312. print set timeout=1
  313. if (( serial )); then
  314. print serial --unit=0 --speed=$speed
  315. print terminal_output serial
  316. print terminal_input serial
  317. consargs="console=ttyS0,$speed console=tty0"
  318. else
  319. print terminal_output console
  320. print terminal_input console
  321. consargs="console=tty0"
  322. fi
  323. print
  324. print 'menuentry "GNU/Linux (OpenADK)" {'
  325. linuxargs="root=UUID=$partuuid $consargs"
  326. (( panicreboot )) && linuxargs="$linuxargs panic=$panicreboot"
  327. print "\tlinux /boot/kernel $linuxargs"
  328. print '}'
  329. ) >boot/grub/grub.cfg
  330. set -A grubfiles
  331. ngrubfiles=0
  332. for a in usr/lib/grub/*-pc/{*.mod,efiemu??.o,command.lst,moddep.lst,fs.lst,handler.lst,parttool.lst}; do
  333. [[ -e $a ]] && grubfiles[ngrubfiles++]=$a
  334. done
  335. cp "${grubfiles[@]}" boot/grub/
  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