-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvmware
executable file
·539 lines (492 loc) · 17 KB
/
vmware
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/bin/bash
# Copyright 2012 Robert Auch
# Licensed under GPLv3
# v1.1 - added "gksu / sudo" autodetection for ease-of-use
# v1.2 - added variable pathnames for easier support of non-Ubuntu
# v1.3 - added fixing of links per LinuxReCon in communities.vmware.com/message/2159266#2159266 as fixlinks()
# v1.4 - update Fedora path detection, fix quotes around sudo detection
# v1.5 - update AC/Battery sys paths and add a removal routine
# v1.6 - update NVIDIA_VERSION, add some root cleanup to /run/user/$UID
# v1.7 - support for VMware Workstation 10 finally
# v1.8 - add some additional skeletons for greater distro support for issue 5
# v1.8.1 - add first part of Manjaro / Arch support - may not work untill issue 6 closed
# v1.9 - add vmplayer support, finalize Arch support
# v1.10 - OpenSuse support from @linuxrecon
# v2.0 - automatically determine Nvidia version better
# v2.1 - merge with vmware12 for better debian x64 library detection
# v2.2 close issue 17 with AC detection
gVers="2.2"
#set this value appropriate for your laptop, and report updates and OS type to github or @docsmooth
ACADAPTER="/sys/class/power_supply/AC*"
BATTERY="/sys/class/power_supply/BAT*"
# The default 3D method - autodetection, forced off, or forced on:
FORCE3D="a" #a for auto, n for no (forced off), y for yes (forced on)
#This is how we'll track what we decide to do. Default safe for power.
#The script will change this value based on logic below. Change to "y" to default on for speed
DO3D="n"
scriptname=`basename $0`
# allow calling this as vmware or vmplayer to get the appropriate GUI
if [ "X$scriptname" = "Xvmplayer" ]; then
VMware="/usr/bin/vmplayer"
else
VMware="/usr/bin/vmware"
fi
echo $1 | egrep -q -w '(help|h|\?)'
if [ $? -eq 0 ]; then
echo "vmware-bumblebee ${gVers}"
echo "Called as $scriptname"
echo "from https://github.com/docsmooth/vmware-bumblebee"
echo "Choose whether to run VMware Workstation via primusrun/optirun or not, based on laptop power state"
echo " (plugged in or not)"
echo "Options are slim:"
echo ""
echo " -?|-h|--help -- this help text"
echo " -f|-y|--yes|--force -- force using optirun/primusrun"
echo " -n|--no -- force NOT using optirun/primusrun"
echo " -i|--install -- install a required script in your vmware folder"
echo " -r|--remove -- remove the script from your vmware folder"
echo " Removal/reinstall is required on script update"
echo ""
echo "If no options are passed, auto-detection is used. Autodetection simply attempts to figure out if"
echo "your AC adapter is plugged in or not."
echo ""
exit 1
fi
if [ -x "`which gksudo`" ]; then
SUDO="`which gksudo` -k"
elif [ -x "`which gksu`" ]; then
SUDO="`which gksu` -S -k"
elif [ -x "`which kdesudo`" ]; then
echo "WARNING: kdesudo doesn't support a '--preserve-environment' flag, please install gksu. Using 'sudo -E', which requires a working terminal."
SUDO="sudo -E"
else
echo "WARNING: no graphical sudo found - using 'sudo -E', which requires a working terminal."
SUDO="sudo -E"
fi
determineos() {
# Set up some paths so this script runs properly on multiple distros eventually.
# Only tested on Ubuntu 12.10 as of 2013-04-15, but we should only need to add
# new paths for SuSE/RHEL/Fedora/Mint/Other.
#
# This function needs to be duplicated into the installvmx_script() function
# below, any time it's changed. That's the only reason it exists as a function:
# to delineate what needs to be duplicated into the installvmx_script()
ARCH=`uname -p`
if [ "$ARCH" = "unknown" ]; then
ARCH=`uname -m`
fi
if [ "$ARCH" = "unknown" ]; then
ARCH=`uname -i`
fi
if [ `which dpkg` ]; then
OStype="deb"
if [ `grep -c -i "ubuntu" /etc/os-release` -gt 0 ]; then
DISTRO="ubuntu"
LIB32="/lib32"
ULIB32="/usr/lib32"
LIB64="/lib"
ULIB64="/usr/lib"
elif grep -q "debian" /etc/os-release; then
DISTRO="debian"
LIB32="/lib/i386-linux-gnu"
ULIB32="/usr/lib/i386-linux-gnu"
LIB64="/lib/x86_64-linux-gnu/"
ULIB64="/usr/lib/x86_64-linux-gnu/"
fi
#Fill in Debian/Mint info here
elif [ `which rpm` ]; then
OStype="rpm"
if [ `grep -c -i "fedora" /etc/redhat-release` -gt 0 ]; then #corrected for fedora 21
DISTRO="fedora"
LIB32="/lib"
ULIB32="/usr/lib"
LIB64="/lib64"
ULIB64="/usr/lib64"
elif [ -f /etc/redhat-release ]; then
DISTRO="rhel"
LIB32="/lib"
ULIB32="/usr/lib"
LIB64="/lib64"
ULIB64="/usr/lib64"
elif [ `grep -c -i "opensuse" /etc/os-release` -gt 0 ]; then
DISTRO="opensuse"
LIB32="/lib"
ULIB32="/usr/lib"
LIB64="/lib64"
ULIB64="/usr/lib64"
fi
elif [ `which pacman` ]; then
OStype="arch"
if [ `grep -c -i "arch linux" /etc/lsb-release` -gt 0 ]; then
DISTRO="arch"
LIB32="/lib32"
ULIB32="/usr/lib32"
LIB64="/lib"
ULIB64="/usr/lib"
elif [ `grep -c -i "manjaro" /etc/lsb-release` -gt 0 ]; then
DISTRO="manjaro"
LIB32="/lib32"
ULIB32="/usr/lib32"
LIB64="/lib"
ULIB64="/usr/lib"
fi
fi
NVIDIA_DEST32="${ULIB32}"
if [ -d "${NVIDIA_DEST32}/linux-gnu" ]; then
NVIDIA_DEST32="${ULIB32}/linux-gnu"
fi
NVIDIA_DEST64="${ULIB64}"
if [ -d "${NVIDIA_DEST64}/x86_64-linux-gnu" ]; then
NVIDIA_DEST64="${ULIB64}/x86_64-linux-gnu"
fi
# Set the default library path to be 64-bit for 64-bit hardware, 32-bit for 32-bit hardware.
# we'll assume they installed the right VMware workstation version for their OS.
if [ "${ARCH}" = "x86_64" -o "${ARCH}" = "amd64" ]; then
ULIB=${ULIB64}
LIB=${LIB64}
NVIDIA_DEST=${NVIDIA_DEST64}
else
ULIB=${ULIB32}
LIB=${LIB32}
NVIDIA_DEST=${NVIDIA_DEST32}
fi
if [ -z ${ULIB} ]; then
echo "ERROR: Couldn't determine paths properly!"
exit 2
elif [ -z ${LIB} ]; then
echo "ERROR: Couldn't determine paths properly!"
exit 2
elif [ -z ${DISTRO} ]; then
echo "ERROR: Couldn't determine paths properly!"
exit 2
elif [ -z ${NVIDIA_DEST} ]; then
echo "ERROR: Couldn't determine paths properly!"
exit 2
fi
#Automatically determine which Nvidia driver we need.
if [ -d "$ULIB/nvidia/current" ]; then
nvdir="$ULIB/nvidia/current"
else
if ls -d $ULIB/nvidia*/xorg > /dev/null 2>&1; then
nvdir=`ls -d $ULIB/nvidia*/xorg | sort -r | head -1`
else # required on 18.04
nvdir=`ls -d $NVIDIA_DEST/nvidia*/xorg | sort -r | head -1`
fi
fi
nvdir=`dirname $nvdir`
NVIDIAVERSION=`basename $nvdir`
echo "Using NVIDIAVERSION $NVIDIAVERSION from directory '$nvdir'"
export ARCH
export DISTRO
export OStype
export ULIB
export LIB
export ULIB32
export ULIB64
export NVIDIA_DEST
export NVIDIAVERSION
}
determineos
restore_file() {
FILE="$@"
if [ -x ${FILE} ]; then
filetype=`file ${FILE}`
echo ${filetype} | grep -q "ASCII text"
if [ $? -eq 0 -a -x ${FILE}.real ]; then
# last chance to make sure this is our script, rather than VMware
# changing the product on us...
egrep -q "\bbbrun\b" ${FILE}
if [ $? -eq 0 ]; then
echo "Uninstalling vmware-bumblebee script from ${FILE} and restoring original binary"
rm -f ${FILE}
mv ${FILE}.real ${FILE}
if [ $? -ne 0 ]; then
echo "ERROR: Couldn't move ${FILE}.real back to ${FILE} - please try to do this manually!!"
echo "ERROR! Your VMware install is in an unusable state. Try:"
echo " sudo mv ${FILE}.real ${FILE}"
exit 16
else
echo "Uninstallation completed successfully"
return 0
fi
else
echo "ERROR: Did not find the expected 'bbrun' line in ${FILE}"
echo "ERROR: Aborting for safety."
exit 4
fi
else
echo "ERROR: ${FILE} is not an ASCII text executable script."
echo "ERROR: or ${FILE} is not an executable file."
echo "ERROR: Aborting for safety."
exit 4
fi
else
echo "ERROR: ${FILE} is not executable, so it can't be from this script."
exit 4
fi
}
removevmx_script() {
restore_file "${ULIB}/vmware/bin/vmware-vmx"
#restore_file "/usr/bin/vmware"
restore_file $VMware
}
backup_file() {
FILETYPE=$1
shift
FILE="$@"
if [ -x ${FILE} ]; then
filetype=`file ${FILE}`
echo ${filetype} | grep -q "${FILETYPE}"
if [ $? -ne 0 ]; then
echo "ERROR: ${FILE} is not a ${FILETYPE}!"
echo "${filetype}"
echo "ERROR: Refusing to continue!"
exit 16
fi
else
echo "ERROR: ${FILE} is not executable!"
echo "Don't know how to continue!"
exit 16
fi
if [ -f ${FILE}.real ]; then
echo "WARNING: there's already a ${FILE}.real"
echo "Moving it to ${FILE}.real.$$"
if [ -f ${FILE}.real.$$ ]; then
echo "Or not! ${FILE}.real.$$ already exists!"
echo "ERROR: can't continue! Please clean up your VMware install!"
exit 16
fi
mv ${FILE}.real ${FILE}.real.$$
if [ $? -ne 0 ]; then
echo "ERROR $? moving ${FILE}.real."
echo " Can't continue!"
exit 16
fi
fi
mv ${FILE} ${FILE}.real
if [ $? -ne 0 ]; then
echo "ERROR $? moving ${FILE} to ${FILE}.real!"
echo " Can't continue!"
exit 16
fi
}
installvmx_script() {
backup_file "setuid ELF" "${ULIB}/vmware/bin/vmware-vmx"
touch ${ULIB}/vmware/bin/vmware-vmx
cat > ${ULIB}/vmware/bin/vmware-vmx <<EOF
#!/bin/bash
DO3D="\${DO3D}"
if [ -z "\${DO3D}" ]; then
ps -ef |egrep -q '[p]rimusrun.*vmware'
if [ \$? -eq 0 ]; then
DO3D="y"
else
DO3D="n"
fi
fi
if [ -z "\${NVIDIA_DEST}" ]; then
echo "ERROR: NVIDIA_DEST variable is empty, we should have inherited from the user vmware script!"
exit 4
fi
# Find primusrun or optirun, since this can work with both, eventually
bbrun="/usr/bin/primusrun"
PRELOAD="\${NVIDIA_DEST}/primus/libGL.so.1"
if [ ! -x \${bbrun} ]; then
bbrun="/usr/bin/optirun"
#PRELOAD="/usr/lib/x86_64-linux-gnu/optimus/libGL.so.1"
PRELOAD=""
fi
if [ ! -x \${bbrun} ]; then
echo "ERROR: Couldn't find Primus or Optimus!"
exit 4
fi
if [ "\${DO3D}" = "y" ]; then
export PRIMUS_libGLa='${ULIB}/${NVIDIAVERSION}/libGL.so.1'
export LD_LIBRARY_PATH=${ULIB64}/${NVIDIAVERSION}:${ULIB32}/${NVIDIAVERSION}:${ULIB64}/${NVIDIAVERSION}/tls:${ULIB32}/${NVIDIAVERSION}/tls
LD_PRELOAD=\${PRELOAD} exec \${bbrun} /usr/lib/vmware/bin/vmware-vmx.real "\$@"
else
/usr/lib/vmware/bin/vmware-vmx.real "\$@"
fi
EOF
if [ $? -ne 0 ]; then
echo "There was an error installing the updated vmware-vmx file! Backing out!"
removevmx_script
exit 16
fi
chmod u+s,a+x ${ULIB}/vmware/bin/vmware-vmx
if [ $? -ne 0 ]; then
echo "ERROR: Could not set permissions properly, please run:"
echo "chmod u+s,a+x ${ULIB}/vmware/bin/vmware-vmx"
exit 16
fi
# now to edit the /usr/bin/vmware script to also use primusrun, if requested...
backup_file "ASCII" $VMware
sed -e 's/exec/exec \$\{bbrun}/g' ${VMware}.real > $VMware
if [ $? -ne 0 ]; then
echo "ERROR: Couldn't replace 'exec' with 'exec \$bbrun' in \$VMware"
exit 16
fi
chmod 755 $VMware
if [ $? -ne 0 ]; then
echo "ERROR: Couldn't fix permissions in $VMware"
exit 16
fi
chown root:root $VMware
if [ $? -ne 0 ]; then
echo "ERROR: Couldn't fix owner in $VMware"
exit 16
fi
exit 0
}
fedora_remove_links() {
sudo unlink "/usr/lib64/vmware"
sudo unlink "/usr/lib64/vmware-installer"
sudo unlink "/usr/lib64/vmware-ovftool"
sudo unlink "/usr/lib64/vmware-vix"
}
fedora_add_links() {
sudo ln -s "/usr/lib/vmware" "/usr/lib64/vmware"
sudo ln -s "/usr/lib/vmware-installer" "/usr/lib64/vmware-installer"
sudo ln -s "/usr/lib/vmware-ovftool" "/usr/lib64/vmware-ovftool"
sudo ln -s "/usr/lib/vmware-vix" "/usr/lib64/vmware-vix"
}
fixlinks() {
DEST=`ls ${NVIDIA_DEST}/*nvidia* 2>/dev/null`
if [ -z "${DEST}" ]; then
#No Nvidia links were found, so we'll create them.
echo "No Nvidia so links were found in ${NVIDIA_DEST}, so they must be created."
echo "You may be prompted for sudo credentials to do so now:"
for file in ${ULIB}/nvidia-304/libnvidia-glcore* ${ULIB}/nvidia-304/tls/libnvidia-tls.so*; do
echo "sudo ln -s $file ${NVIDIA_DEST}"
sudo ln -s $file ${NVIDIA_DEST}
done
fi
}
#figure out which options the user wants
echo $1 | egrep -q -w '(yes|y|f|force)'
if [ $? -eq 0 ]; then
echo "forcing 3d on"
FORCE3D="y"
fi
echo $1 | egrep -q -w '(n|no)'
if [ $? -eq 0 ]; then
echo "Forcing 3D off"
FORCE3D="n"
fi
echo $1 | egrep -q -w '(r|remove)'
if [ $? -eq 0 ]; then
if [ "$USER" = "root" ]; then
removevmx_script
if [ $? -eq 0 ]; then
fixlinks
if ( [ "${DISTRO}" = "fedora" ] || [ "${DISTRO}" = "opensuse" ] ) && ( [ "${ARCH}" = "x86_64" -o "${ARCH}" = "amd64" ] ); then
fedora_remove_links
fi
exit 0
else
echo "Some error happened in the removal!"
exit 1
fi
else
echo "ERROR: Have to be root to run the removal!"
echo " Please try again via $SUDO."
exit 2
fi
fi
echo $1 | egrep -q -w '(i|install)'
if [ $? -eq 0 ]; then
if [ "$USER" = "root" ]; then
if ( [ "${DISTRO}" = "fedora" ] || [ "${DISTRO}" = "opensuse" ] ) && ( [ "${ARCH}" = "x86_64" -o "${ARCH}" = "amd64" ] ); then
fedora_add_links
fi
installvmx_script
if [ $? -eq 0 ]; then
fixlinks
exit 0
else
echo "Some error happened in the installer!"
exit 1
fi
else
echo "ERROR: Have to be root to run the install!"
echo " Please try again via $SUDO."
exit 2
fi
fi
case "$-" in
*x*)
echo "'-x' is set, continuing in forked process"
SHOPT="bash -x "
;;
*)
SHOPT=""
;;
esac
fixlinks
# Find primusrun or optirun, since this can work with both
bbrun="/usr/bin/primusrun"
if [ ! -x ${bbrun} ]; then
bbrun="/usr/bin/optirun"
fi
if [ ! -x ${bbrun} ]; then
echo "ERROR: Couldn't find Primus or Optimus!"
exit 4
fi
if [ "${FORCE3D}" = "y" ]; then
#The user wants it forced on! Easy!
DO3D="y"
elif [ "${FORCE3D}" = "n" ]; then
DO3D="n"
else
#automatic!
# Now determine if we're on AC (mains) power or not
ACSTATE=`head -1 ${ACADAPTER}/online`
BATSTATE=`head -1 ${BATTERY}/status`
if [ -z "${BATSTATE}" -a -f ${BATTERY} ]; then
BATPRESENT=`head -1 ${BATTERY}/present`
else
BATSTATE="NF"
fi
if [ "${ACSTATE}" = "0" -o "X${BATSTATE}" = "XDischarging" ]; then
echo "AC is offline"
DO3D="n"
else
DO3D="y"
fi
fi
export DO3D
if [ "${DO3D}" = "y" ]; then
#TODO Fix up for non-ubuntu installs
#export PRIMUS_libGLa='/usr/lib/${NVIDIAVERSION}/libGL.so.1'
#export LD_LIBRARY_PATH=/usr/lib/${NVIDIAVERSION}:/usr/lib32/${NVIDIAVERSION}:/usr/lib/${NVIDIAVERSION}/tls:/usr/lib32/${NVIDIAVERSION}/tls
export bbrun
#this is to get sudo to work properly with vmware
if [ -z "${XAUTHORITY}" ]; then
export XAUTHORITY=${HOME}/.Xauthority
else
export XAUTHORITY
fi
#XAUTHORITY=${XAUTHORITY} $SUDO -- "${bbrun} ${SHOPT} ${ULIB}/vmware/bin/vmware"
#cmd=`echo "${bbrun} ${SHOPT} $VMware" | sed -r -e 's/([^[[:alnum:]]_\-\$])/\\1/g'`
XAUTHORITY=${XAUTHORITY} $SUDO -- ${bbrun} ${SHOPT} $VMware
else
export bbrun=""
$VMware
fi
# CLEAN UP!! Running VMware under sudo takes over a few files on Ubuntu
#TODO Figure out if these paths are the same on Fedora / others?
fixx11owners() {
FILE="$@"
if [ -d "$XDG_RUNTIME_DIR/${FILE}" -a "${DO3D}" = "y" ]; then
echo "attempting to clean up any chown operations vmware may have done to you"
OWNER=`stat --printf %u:%g "${XDG_RUNTIME_DIR}/${FILE}"`
NEWOWNER=`stat --printf %u:%g "${XDG_RUNTIME_DIR}"`
if [ "$NEWOWNER" != "$OWNER" ]; then
#vmware changed our file
$SUDO "chown -R $NEWOWNER ${XDG_RUNTIME_DIR}/${FILE}"
fi
fi
}
fixx11owners "pulse"
fixx11owners "X11-display"
exit 0