init.d 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: qubes-core-agent
  4. # Required-Start: $network $local_fs $remote_fs
  5. # Required-Stop:
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Qubes qrexec agent
  9. # Description: The qrexec agent runs in qubes domU domains. It runs
  10. # commands on request from dom0.
  11. ### END INIT INFO
  12. # Author: Davíð Steinn Geirsson <david@dsg.is>
  13. # Most of this script is copied from vm-init.d/qubes-core with
  14. # some fedora-specific stuff removed.
  15. # PATH should only include /usr/* if it runs after the mountnfs.sh script
  16. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  17. DESC=qrexec-agent
  18. NAME=qrexec-agent
  19. DAEMON=/usr/lib/qubes/qrexec-agent
  20. DAEMON_ARGS=""
  21. PIDFILE=/var/run/$NAME.pid
  22. SCRIPTNAME=/etc/init.d/$NAME
  23. # Exit if the package is not installed
  24. [ -x $DAEMON ] || exit 0
  25. # Read configuration variable file if it is present
  26. [ -r /etc/default/$NAME ] && . /etc/default/$NAME
  27. # Load the VERBOSE setting and other rcS variables
  28. . /lib/init/vars.sh
  29. # Define LSB log_* functions.
  30. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  31. . /lib/lsb/init-functions
  32. #
  33. # Function that starts the daemon/service
  34. #
  35. do_start()
  36. {
  37. # Return
  38. # 0 if daemon has been started
  39. # 1 if daemon was already running
  40. # 2 if daemon could not be started
  41. # Ensure necessary modules are loaded
  42. modprobe xen_evtchn
  43. modprobe u2mfn
  44. # Set permissions to /proc/xen/xenbus, so normal user can use xenstore-read
  45. chmod 666 /proc/xen/xenbus
  46. # Set permissions to files needed to listen at vchan
  47. chmod 666 /proc/u2mfn
  48. mkdir -p /var/run/xen-hotplug
  49. name=$(/usr/sbin/xenstore-read name)
  50. if ! [ -f /etc/this-is-dvm ] ; then
  51. # we don't want to set hostname for DispVM
  52. # because it makes some of the pre-created dotfiles invalid (e.g. .kde/cache-<hostname>)
  53. # (let's be frank: nobody's gonna use xterm on DispVM)
  54. if ! [ -z "$name" ]; then
  55. echo $name > /etc/hostname
  56. hostname $name
  57. grep '127.0.1.1' /etc/hosts > /dev/null
  58. if [ $? -ne 0 ]; then
  59. echo "127.0.1.1 $name" >> /etc/hosts
  60. else
  61. sed -i "s/127\.0\.1\.1.*/127.0.1.1 $name/" /etc/hosts
  62. fi
  63. fi
  64. fi
  65. timezone=`/usr/sbin/xenstore-read qubes-timezone 2> /dev/null`
  66. if [ -n "$timezone" ]; then
  67. ln -f /usr/share/zoneinfo/$timezone /etc/localtime
  68. fi
  69. # Set IP address again (besides action in udev rules); this is needed by
  70. # DispVM (to override DispVM-template IP) and in case when qubes-ip was
  71. # called by udev before loading evtchn kernel module - in which case
  72. # xenstore-read fails
  73. INTERFACE=eth0 /usr/lib/qubes/setup-ip
  74. mkdir -p /var/run/qubes
  75. if [ -e /dev/xvdb ] ; then
  76. resize2fs /dev/xvdb 2> /dev/null || echo "'resize2fs /dev/xvdb' failed"
  77. mount /rw
  78. if ! [ -d /rw/home ] ; then
  79. echo
  80. echo "--> Virgin boot of the VM: Linking /home to /rw/home"
  81. mkdir -p /rw/config
  82. touch /rw/config/rc.local
  83. mkdir -p /rw/home
  84. cp -a /home.orig/user /rw/home
  85. mkdir -p /rw/usrlocal
  86. cp -a /usr/local.orig/* /rw/usrlocal
  87. touch /var/lib/qubes/first-boot-completed
  88. fi
  89. fi
  90. if [ -L /home ]; then
  91. rm /home
  92. mkdir /home
  93. fi
  94. mount /home
  95. [ -x /rw/config/rc.local ] && /rw/config/rc.local
  96. start-stop-daemon --start --quiet -b --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
  97. || return 1
  98. start-stop-daemon --start --quiet -b --pidfile $PIDFILE --exec $DAEMON -- \
  99. $DAEMON_ARGS \
  100. || return 2
  101. # Add code here, if necessary, that waits for the process to be ready
  102. # to handle requests from services started subsequently which depend
  103. # on this one. As a last resort, sleep for some time.
  104. }
  105. do_stop()
  106. {
  107. # Return
  108. # 0 if daemon has been stopped
  109. # 1 if daemon was already stopped
  110. # 2 if daemon could not be stopped
  111. # other if a failure occurred
  112. start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
  113. RETVAL="$?"
  114. [ "$RETVAL" = 2 ] && return 2
  115. # Wait for children to finish too if this is a daemon that forks
  116. # and if the daemon is only ever run from this initscript.
  117. # If the above conditions are not satisfied then add some other code
  118. # that waits for the process to drop all resources that could be
  119. # needed by services started subsequently. A last resort is to
  120. # sleep for some time.
  121. start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
  122. [ "$?" = 2 ] && return 2
  123. # Many daemons don't delete their pidfiles when they exit.
  124. rm -f $PIDFILE
  125. return "$RETVAL"
  126. }
  127. #
  128. # Function that sends a SIGHUP to the daemon/service
  129. #
  130. do_reload() {
  131. #
  132. # If the daemon can reload its configuration without
  133. # restarting (for example, when it is sent a SIGHUP),
  134. # then implement that here.
  135. #
  136. start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
  137. return 0
  138. }
  139. case "$1" in
  140. start)
  141. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
  142. do_start
  143. case "$?" in
  144. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  145. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  146. esac
  147. ;;
  148. stop)
  149. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  150. do_stop
  151. case "$?" in
  152. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  153. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  154. esac
  155. ;;
  156. status)
  157. status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
  158. ;;
  159. #reload|force-reload)
  160. #
  161. # If do_reload() is not implemented then leave this commented out
  162. # and leave 'force-reload' as an alias for 'restart'.
  163. #
  164. #log_daemon_msg "Reloading $DESC" "$NAME"
  165. #do_reload
  166. #log_end_msg $?
  167. #;;
  168. restart|force-reload)
  169. #
  170. # If the "reload" option is implemented then remove the
  171. # 'force-reload' alias
  172. #
  173. log_daemon_msg "Restarting $DESC" "$NAME"
  174. do_stop
  175. case "$?" in
  176. 0|1)
  177. do_start
  178. case "$?" in
  179. 0) log_end_msg 0 ;;
  180. 1) log_end_msg 1 ;; # Old process is still running
  181. *) log_end_msg 1 ;; # Failed to start
  182. esac
  183. ;;
  184. *)
  185. # Failed to stop
  186. log_end_msg 1
  187. ;;
  188. esac
  189. ;;
  190. *)
  191. #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
  192. echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
  193. exit 3
  194. ;;
  195. esac
  196. :