mkusbboot 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/rc
  2. # mkusbboot - make a bootable standalone plan 9 image to copy to a usb disk.
  3. #
  4. # due to name clashes in /srv, will only work on a machine
  5. # without a fossil named `fossil' already running.
  6. rfork en
  7. syscfg=/sys/lib/sysconfig
  8. proto=$syscfg/proto/stand-usb
  9. srcroot=/n/boot
  10. quantum=1000000
  11. # size of image in $quantum-byte units. 1900 is enough for production system
  12. # (fs, /n/boot, ~1.2GB); 900 is ample for our install image
  13. # (sources, /n/sources/plan9, ~370MB).
  14. defsize=1900
  15. size=()
  16. fn usage {
  17. echo usage: $argv0 '[-b boot] [-p proto] [-r root] [-s 10⁶-bytes]' >[1=2]
  18. exit usage
  19. }
  20. fn sigint sighup sigterm {
  21. rm -f /tmp/9load
  22. exit note
  23. }
  24. # process arguments
  25. done=0
  26. argv0=$0
  27. boot=/386/9loadusb
  28. while (~ $done 0 && ! ~ $#* 0 && ~ $1 -*) {
  29. switch ($1) {
  30. case -b; boot=$2; shift
  31. case -p; proto=$2; shift
  32. case -r; srcroot=$2; shift
  33. case -s; size=$2; shift
  34. case --; done=1 # no break in rc, alas
  35. case -*; usage
  36. }
  37. shift
  38. }
  39. if (! ~ $#* 0)
  40. usage
  41. # special case sources
  42. if (~ $srcroot /n/*)
  43. srcfs=`{ echo $srcroot | sed 's;^/n/([^/]+).*;\1;' }
  44. if (~ $srcfs sources)
  45. defsize=900
  46. if (~ $#size 0)
  47. size=$defsize
  48. # make empty disk image file of maximum size
  49. echo -n 'image: ' >[1=2]
  50. dd -ibs $quantum -obs $quantum -oseek `{hoc -e $size'-1'} -count 1 -quiet 1 \
  51. </dev/zero >image
  52. # partition it
  53. disk/partfs image
  54. cd /dev/sdXX
  55. # prep it: lay down mbr, fdisk partitions, 9 partitions
  56. echo -n mbr+fdisk+prep... >[1=2]
  57. disk/mbr -m /386/mbr data
  58. disk/fdisk -baw data
  59. disk/prep -bw -a^(9fat nvram fscfg fossil) plan9 >/dev/null
  60. # populate 9fat, nvram, fsconfig
  61. echo -n 9fat... >[1=2]
  62. switch ($objtype) {
  63. case 386
  64. # we don't normally keep gzipped kernels around,
  65. # but usb can be flakey and slow, so we'll make one.
  66. k=/tmp/9pccpuf.gz
  67. gzip -9 </$objtype/9pccpuf >$k
  68. case amd64
  69. k=/$objtype/9k8cpu
  70. case *
  71. echo $0: 'can''t cope with architecture' $objtype >[1=2]
  72. exit unknown-arch
  73. }
  74. cp $boot /tmp/9load # force format to record the name `9load'
  75. disk/format -b /386/pbslba -d -r 2 9fat /tmp/9load $k \
  76. $syscfg/usb/plan9.ini >[2=1] |
  77. grep -v '^(add .* at clust |Init|type |Adding file|used )'
  78. if (~ $objtype 386)
  79. rm -f $k
  80. rm -f /tmp/9load
  81. cp /dev/zero nvram >[2]/dev/null
  82. cp /dev/zero fscfg >[2]/dev/null
  83. # fill the fossil
  84. 9fs $srcfs
  85. echo -n load fossil... >[1=2]
  86. exec loadfossil /dev/sdXX/fossil $srcroot $proto $syscfg/usb/fossil.conf