Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/sbin/runscript
  2.  
  3. # Not sure why but gentoo forgot to add /opt/bin to the path.
  4. VBOXPATH="/usr/bin:/opt/bin"
  5. VBOXNAME="${SVCNAME#*.}"
  6.  
  7. depend() {
  8. need net
  9.  
  10. if [ "${SVCNAME}" != "virtualbox" ] ; then
  11. need virtualbox
  12. fi
  13. }
  14.  
  15.  
  16. checkconfig() {
  17. if [ ! -r /etc/conf.d/$SVCNAME ] ; then
  18. eerror "Please create /etc/conf.d/$SVCNAME"
  19. eerror "Sample conf: /etc/conf.d/virtualbox.example"
  20. return 1
  21. fi
  22.  
  23. return 0
  24. }
  25.  
  26. checkpath() {
  27. local r=0
  28.  
  29. if ! su $VM_USER -c "PATH=$VBOXPATH command -v VBoxHeadless &>/dev/null" -s /bin/sh ; then
  30. eerror "Could not locate VBoxHeadless"
  31. r=1
  32. fi
  33.  
  34. if ! su $VM_USER -c "PATH=$VBOXPATH command -v VBoxManage &>/dev/null" -s /bin/sh ; then
  35. eerror "Could not locate VBoxManage"
  36. r=1
  37. fi
  38.  
  39. if [ $r -gt 0 ] ; then
  40. eerror "Please verify the vm users path."
  41. fi
  42.  
  43. return $r
  44. }
  45.  
  46. isloaded() {
  47. lsmod | grep -q "$1[^_-]"
  48. }
  49.  
  50. isvm() {
  51. [ $SVCNAME != "virtualbox" ]
  52. }
  53.  
  54. loadmodules() {
  55. if ! isloaded vboxdrv ; then
  56. if ! modprobe vboxdrv > /dev/null 2>&1 ; then
  57. eerror "modprobe vboxdrv failed."
  58. return 1
  59. fi
  60. fi
  61.  
  62. if ! isloaded vboxnetflt ; then
  63. if ! modprobe vboxnetflt > /dev/null 2>&1 ; then
  64. eerror "modprobe vboxnetflt failed."
  65. return 1
  66. fi
  67. fi
  68.  
  69. if ! isloaded vboxnetadp ; then
  70. if ! modprobe vboxnetadp > /dev/null 2>&1 ; then
  71. eerror "modprobe vboxnetadp failed."
  72. return 1
  73. fi
  74. fi
  75.  
  76. return 0
  77. }
  78.  
  79. unloadmodules() {
  80. if isloaded vboxnetflt ; then
  81. if ! rmmod vboxnetflt > /dev/null 2>&1 ; then
  82. eerror "rmmod vboxnetflt failed."
  83. return 1
  84. fi
  85. fi
  86.  
  87. if isloaded vboxnetadp ; then
  88. if ! rmmod vboxnetadp > /dev/null 2>&1 ; then
  89. eerror "rmmod vboxnetadp failed."
  90. return 1
  91. fi
  92. fi
  93.  
  94. if isloaded vboxdrv ; then
  95. if ! rmmod vboxdrv > /dev/null 2>&1 ; then
  96. eerror "rmmod vboxdrv failed."
  97. return 1
  98. fi
  99. fi
  100.  
  101. return 0
  102. }
  103.  
  104. start() {
  105. # If we are the original virtualbox script [ $SVCNAME = "virtualbox" ]
  106. if ! isvm ; then
  107. ebegin "Starting Virtualbox"
  108. loadmodules
  109. eend $?
  110. else
  111. checkconfig || return $?
  112. checkpath || return $?
  113.  
  114. ebegin "Starting Virtualbox: $VBOXNAME"
  115. su $VM_USER -c "PATH=$VBOXPATH nice -n $VM_NICE VBoxHeadless -startvm \"$VM_NAME\" &>/dev/null" -s /bin/sh &
  116. pid=$!
  117. sleep 1
  118.  
  119. kill -CHLD $pid &>/dev/null
  120. eend $?
  121. fi
  122. }
  123.  
  124. stop() {
  125. # If we are the original virtualbox script [ $SVCNAME = "virtualbox" ]
  126. if ! isvm ; then
  127. ebegin "Stopping Virtualbox"
  128. unloadmodules
  129. eend $?
  130. else
  131. checkconfig || return $?
  132. checkpath || return $?
  133.  
  134. ebegin "Stopping Virtualbox: $VBOXNAME"
  135. su ${VM_USER} -c "PATH=$VBOXPATH VBoxManage controlvm \"$VM_NAME\" $VM_SHUTDOWN &>/dev/null" -s /bin/sh &
  136. c=0
  137. while [ "$(su ${VM_USER} -c "PATH=$VBOXPATH VBoxManage showvminfo \"$VM_NAME\" | grep State | grep 'runn\|saving' 2>/dev/null")" != "" ]
  138. do
  139. echo -n "."
  140. sleep 1
  141. let c=c+1
  142.  
  143. if [ "$c" = "300" ]; then
  144. echo -n " Trying again $VM_SHUTDOWN..."
  145. su ${VM_USER} -c "PATH=$VBOXPATH VBoxManage controlvm \"$VM_NAME\" $VM_SHUTDOWN &>/dev/null" -s /bin/sh &
  146. fi
  147.  
  148. if [ "$c" = "360" ]; then
  149. echo ""
  150. echo -n "$VM_SHUTDOWN not working, trying poweroff."
  151. su ${VM_USER} -c "PATH=$VBOXPATH VBoxManage controlvm \"$VM_NAME\" poweroff &>/dev/null" -s /bin/sh &
  152. fi
  153.  
  154. done
  155.  
  156. sleep 1
  157. echo
  158.  
  159. eend $?
  160. fi
  161. }