Advertisement
Guest User

Untitled

a guest
Nov 14th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Some colored makepkg-like functions
  4. msg() {
  5. printf "${GREEN}==>${ALL_OFF}${BOLD} $1${ALL_OFF}\n"
  6. }
  7.  
  8. msg_yellow() {
  9. printf "${YELLOW}==>${ALL_OFF}${BOLD} $1${ALL_OFF}\n"
  10. }
  11.  
  12. msg2() {
  13. printf "${BLUE} ->${ALL_OFF}${BOLD} $1${ALL_OFF}\n"
  14. }
  15.  
  16. read_msg2() {
  17. read -p "${BLUE} ->${ALL_OFF}${BOLD} $1${ALL_OFF}"
  18. }
  19.  
  20. msg3() {
  21. printf "${YELLOW} ->${ALL_OFF}${BOLD} $1${ALL_OFF}\n"
  22. }
  23.  
  24. info_yellow() {
  25. printf "${YELLOW}$1${ALL_OFF}${BOLD} $2${ALL_OFF}\n"
  26. }
  27.  
  28. warning() {
  29. printf "${YELLOW}==> WARNING:${ALL_OFF}${BOLD} $1${ALL_OFF}\n"
  30. }
  31.  
  32. error() {
  33. printf "${RED}==> ERROR:${ALL_OFF}${BOLD} $1${ALL_OFF}\n"
  34. }
  35.  
  36. # Variables for them
  37. ALL_OFF="$(tput sgr0)"
  38. BOLD="$(tput bold)"
  39. BLUE="${BOLD}$(tput setaf 4)"
  40. GREEN="${BOLD}$(tput setaf 2)"
  41. RED="${BOLD}$(tput setaf 1)"
  42. YELLOW="${BOLD}$(tput setaf 3)"
  43.  
  44. # Don't continue with an unknown flag
  45. if [[ "$@" ]] && [[ "$@" != '-f' ]]; then
  46. error "Unknown option: $*"
  47. info_yellow "Available options:" "-f (force reinstallation even, if modules have already been built)."
  48. exit 1
  49. fi
  50.  
  51. # Make sure we are root
  52. if [ $(whoami) != "root" ]; then
  53. error "This script needs to be run as root."
  54. exit 1
  55. fi
  56.  
  57. # Is a VMware product installed?
  58. if [ ! -f /usr/bin/vmware-installer ]; then
  59. error "No VMware product found. Exiting.."
  60. exit 1
  61. fi
  62.  
  63. # Set some variables (|&: hide "No protocol specified" in KDE by redirecting all output to cut/grep)
  64. KERNEL=$(uname -r | cut -d "." -f-2)
  65. VER=$(vmware-installer -l |& egrep "(player|workstation)" | grep -Po "(\d+\.){2}\d+")
  66. if [ $(vmware-installer -l |& egrep "(player|workstation)" | cut -d " " -f1) = "vmware-workstation" ]; then
  67. NAME="VMware Workstation"
  68. else
  69. NAME="VMware Player (Plus)"
  70. fi
  71.  
  72. # Make sure vmware.service includes our USB Arbitrator service
  73. if [[ ! $(grep usbarbitrator /usr/lib/systemd/system/vmware.service) ]]; then
  74. msg "Updating vmware.service.."
  75. sed '/Description/a Requires=vmware-usbarbitrator.service\nBefore=vmware-usbarbitrator.service' \
  76. -i /usr/lib/systemd/system/vmware.service
  77. fi
  78.  
  79. # Make sure there's a version in /etc/arch-release for versions prior to Workstation 10 / Player (Plus) 6:
  80. # https://wiki.archlinux.org/index.php/Vmware#2.29_The_vmware-usbarbitrator_binary_is_segfaulting
  81. if [[ ${VER} != 6.0.? ]] && [[ ${VER} != 10.0.? ]]; then
  82. if [ ! $(cat /etc/arch-release) ]; then
  83. msg "Updating /etc/arch-release.."
  84. echo 2013.09.01 > /etc/arch-release
  85. fi
  86. fi
  87.  
  88. # Remove leftover header locations
  89. for i in /usr/src/*; do
  90. if [[ $(ls $i) = 'include' ]]; then
  91. msg_yellow "Removing leftover header location $i/"
  92. rm -r $i
  93. fi
  94. done
  95.  
  96. # Symlink version.h, if necessary (# 1) We don't have one already (accept files too))
  97. if [ ! -f /usr/src/linux-$(uname -r)/include/linux/version.h ]; then
  98. # 2) Installed Workstation/Player less than 9.0.2/5.0.2
  99. if [[ ${VER} != [59].0.[2-6] ]] && [[ ${VER} != 6.0.? ]] && [[ ${VER} != 10.0.? ]]; then
  100. # 3) Kernel version 3.5 or above (another way to deal with numbers)
  101. if [ $(echo ${KERNEL} | tr -d .) -ge 35 ]; then
  102. msg "Symlinking version.h for $(uname -r)"
  103. install -d "${pkgdir}"/usr/src/linux-$(uname -r)/include/linux/
  104. ln -s /usr/src/linux-$(uname -r)/include/generated/uapi/linux/version.h \
  105. "${pkgdir}"/usr/src/linux-$(uname -r)/include/linux/
  106. fi
  107. fi
  108. fi
  109.  
  110. # Remove leftover module locations
  111. for i in /usr/lib/modules/*; do
  112. if [[ $(ls $i) = 'misc' ]]; then
  113. msg_yellow "Removing leftover module location $i/"
  114. rm -r $i
  115. fi
  116. done
  117.  
  118. # Backup/Convert/Remove old backups, whatever necessary
  119. cd /usr/lib/vmware/modules/
  120. # 1) Use double quotes ("[[") and "ls" instead of the builtin -d/-f for multiple arguments
  121. # 2) 2>&-: supported by "ls", basically same as "2>/dev/null" only shorter (ignore errors (STDERR))
  122. if [[ $(ls -d source?*/ source/*.tar-* 2>&- | grep -vx source-${VER}/) ]]; then
  123. msg "Cleaning up old backups.."
  124. # 1) Check the new location first
  125. # 2) Remove any duplicated/old ones
  126. # 3) Print full paths
  127. # 4) Sed: leave out the first result containing ${VER}
  128. for i in $(readlink -e source?*/ | sed "0,/${VER}/{//d}"); do
  129. msg2 "Removing ${i}"
  130. rm -r "${i}"
  131. done
  132. # If we already have a backup in the new location, remove any from the old one
  133. if [ -d source-${VER}*/ ]; then
  134. for i in $(readlink -e source/*.tar-*); do
  135. msg2 "Removing ${i}"
  136. rm -r "${i}"
  137. done
  138. # Rename it as we like (or as I do)
  139. if [ -d source-${VER}?*/ ]; then
  140. msg3 "Renaming: $(readlink -f source-${VER}*)/ -> $(readlink -f source-${VER})/"
  141. mv source-${VER}*/ source-${VER}/
  142. fi
  143. else
  144. # Remove any duplicated/old backups
  145. for i in vmblock vmci vmmon vmnet vsock; do
  146. for j in $(readlink -e source/${i}.tar-* | sed "0,/${VER}/{//d}"); do
  147. msg2 "Removing ${j}"
  148. rm -r "${j}"
  149. done
  150. done
  151. # Convert leftovers
  152. if [ -f source/vmblock.tar-* ]; then
  153. # Shorten the msg3 function
  154. _old=$(readlink -f source/vmblock.tar-* | sed 's/vmblock/*/')
  155. msg3 "Converting: ${_old} -> $(readlink -f source-${VER})/"
  156. mkdir source-${VER}/
  157. for i in vmblock vmci vmmon vmnet vsock; do
  158. mv source/${i}.tar-* source-${VER}/${i}.tar
  159. done
  160. fi
  161. fi
  162. fi
  163.  
  164. # Support the '-f' flag
  165. # 1) Have we already built (vsock is built last)?
  166. # 2) Don't check this until we've removed the leftovers
  167. if [ -f /usr/lib/modules/$(uname -r)/misc/vsock.ko ]; then
  168. # Offer the ability reinstall this thing
  169. if [[ "$@" = '-f' ]]; then
  170. msg "Reinstalling modules.."
  171. elif [[ ! "$@" ]]; then
  172. error "VMware modules already installed (use -f to override). Exiting.."
  173. exit 1
  174. fi
  175. fi
  176.  
  177. # Patch
  178. # 1) Make sure we can build
  179. # 2) Shorther without double brackets ("[["):
  180. # 3) &>/dev/null: hide all output (STDIN/STDERR)
  181. if ls patches/*${VER}*${KERNEL}*.patch &>/dev/null; then
  182. # Create a backup/Revert
  183. if [ ! -d source-${VER} ]; then
  184. msg "Creating a backup.."
  185. cp -r source/ source-${VER}/
  186. else # Get the original sources from the backup (invalidate the need for 'vmware-unpatch')
  187. cp source-${VER}/* source/
  188. fi
  189. # Loop modules that require patching (vmblock, vmci, vmmon, vmnet and/or vsock)
  190. msg "Patching ${NAME} v${VER} for kernel $(uname -r).."
  191. cd source
  192. for mod in $(ls ../patches/*${VER}*${KERNEL}*.patch | cut -d "/" -f3 | cut -d "-" -f1); do
  193. # Untar
  194. tar -xf ${mod}.tar
  195. # Patch
  196. msg2 "${mod}"
  197. # Shorten
  198. _patch=../patches/${mod}*${VER}*${KERNEL}*.patch
  199. if ! patch -p0 -s -f -i ${_patch}; then
  200. error "Failed to apply '$(basename ${_patch})'"
  201. read_msg2 "Continue? (Y/n) "
  202. if [[ "$REPLY" != [Yy] ]]; then
  203. # Revert
  204. msg "Reverting.."
  205. cd ..
  206. rm -r source/
  207. mv source-${VER}/ source/
  208. msg "Done."
  209. exit 1
  210. fi
  211. fi
  212. # Tar patched modules
  213. tar -cf ${mod}.tar ${mod}-only
  214. rm -r ${mod}-only
  215. done
  216. else
  217. msg "No patching required."
  218. fi
  219.  
  220. # Install
  221. msg "Installing modules.."
  222. vmware-modconfig --console --install-all &>/dev/null
  223. msg "Done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement