fshalt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/rc
  2. # fshalt [-r] [new-kernel] - sync (flush) and, if possible, halt all
  3. # file servers and optionally reboot
  4. rfork nes
  5. kern=()
  6. kerncopy=()
  7. loadkern=yes
  8. reboot=no
  9. fn usage {
  10. echo usage: $1 '[-r] [-R] [new-kernel]' >[1=2]
  11. exit usage
  12. }
  13. if (! ~ $#* 0)
  14. switch ($1) {
  15. case -r
  16. reboot=yes
  17. shift
  18. case -R
  19. loadkern=no
  20. reboot=yes
  21. shift
  22. case -*
  23. usage $0
  24. }
  25. switch ($#*) {
  26. case 0
  27. case 1
  28. kern = $1
  29. case *
  30. usage $0
  31. }
  32. path=(/bin)
  33. builtin cd /
  34. 9fs boot # in case of odd namespace
  35. ramfs # stash files here that we'll need after the fs halts
  36. setrtc
  37. # turn off graphics, if any
  38. if (test -e '#v/vgactl' && test -e '#P/realmode') {
  39. vgatype=`{read '#v/vgactl'}
  40. if (! ~ $vgatype(2) cga) {
  41. prompt=''
  42. kill rio | rc -i &
  43. sleep 2
  44. aux/vga -l text
  45. }
  46. }
  47. fn isbootable {
  48. test -e $1 && ~ `{file $1} *' plan 9 boot image' \
  49. *' plan 9 executable'* *ELF*' executable'
  50. }
  51. fn usekernel {
  52. kerncopy=kern
  53. cp $1 /tmp/$kerncopy && echo -n using kernel $1...
  54. }
  55. # make a copy of the right kernel
  56. if (~ $reboot yes)
  57. if (~ $loadkern yes) {
  58. if (~ $#kern 0)
  59. kern=`{echo $terminal |
  60. sed 's;^([^ ]+) .*/([^/ ]+).*$;/n/boot/'$cputype'/9\2;'}
  61. if (isbootable $kern)
  62. usekernel $kern
  63. if not {
  64. # $kern is probably a raw binary, try s^name as boot image
  65. kern=`{ echo $kern | sed 's;.*/;&s;' }
  66. if (isbootable $kern)
  67. usekernel $kern
  68. if not
  69. echo -n using default kernel...
  70. }
  71. }
  72. # start venti flushing
  73. venti/sync -h localhost >[2]/dev/null &
  74. venti/sync >[2]/dev/null &
  75. unmount /mnt/consoles >[2]/dev/null
  76. kill consolefs | rc # don't compete with /mnt/consoles
  77. kill cron | rc # unlock /cron/lock
  78. kill watchdog | rc
  79. sleep 1
  80. f=`{ls /srv/fscons*>[2]/dev/null}
  81. k=`{ls /srv/kfs*cmd >[2]/dev/null|sort -r}
  82. echo -n syncing...
  83. for(i in $f) @ {
  84. echo -n $i...
  85. {
  86. echo
  87. dial/drain &
  88. sleep 2
  89. echo fsys all sync
  90. if(! dial/expect -t 120 ': ')
  91. echo -n 'not synced...' > /dev/cons
  92. } < $i >> $i
  93. }
  94. # flush the last bit of possible fossil traffic
  95. if (ls /srv | grep -s fossil) {
  96. echo -n venti...
  97. venti/sync -h localhost >[2]/dev/null &
  98. venti/sync >[2]/dev/null &
  99. sleep 5
  100. }
  101. for (i in $k){
  102. echo -n $i...
  103. switch($i){
  104. case /srv/kfs.cmd
  105. disk/kfscmd sync
  106. case *
  107. disk/kfscmd -n `{echo $i | sed -n 's%/srv/kfs.(.*).cmd%\1%p'} sync
  108. }
  109. sleep 2
  110. }
  111. # halting (binaries we run can't be on the fs we're halting)
  112. builtin cd /bin
  113. cp dial/expect echo disk/kfscmd rc sed /tmp
  114. builtin cd /tmp
  115. mkdir /tmp/lib
  116. cp /rc/lib/rcmain /tmp/lib
  117. bind /tmp /rc
  118. bind /tmp /bin
  119. # put this in a shell function so this rc script doesn't get read
  120. # when it's no longer accessible
  121. fn x {
  122. rfork s
  123. echo
  124. echo -n halting...
  125. for(i in $f) @ {
  126. echo -n $i...
  127. {
  128. echo fsys all halt
  129. if(! expect -t 60 ': ')
  130. echo -n 'not halted...' > /dev/cons
  131. } < $i >> $i
  132. }
  133. for (i in $k){
  134. echo -n $i...
  135. switch($i){
  136. case /srv/kfs.cmd
  137. kfscmd halt
  138. case *
  139. kfscmd -n `{echo $i | sed -n 's%/srv/kfs.(.*).cmd%\1%p'} halt
  140. }
  141. }
  142. echo
  143. echo done halting
  144. if (~ $reboot yes)
  145. echo reboot $kerncopy >'#c/reboot'
  146. }
  147. x