summaryrefslogtreecommitdiffstats
path: root/remote/modules/vmware/data/opt/openslx/scripts/systemd-vmware_env
blob: dc60e3d5a1cfd0fa356b9029c1a61a61eb3d2b97 (plain) (blame)
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
#!/opt/openslx/bin/ash
# Copyright (c) 2009 - OpenSLX GmbH 
# 
# This program is free software distributed under the GPL version 2. 
# See http://openslx.org/COPYING 
# 
# If you have any feedback please consult http://openslx.org/feedback and 
# send your suggestions, praise, or complaints to feedback@openslx.org 
# 
# General information about OpenSLX can be found at http://openslx.org/ 
# -----------------------------------------------------------------------------

VMWARE_CONF_DIR=/opt/openslx/vmchooser/vmware
VMCHOOSER_CONF_DIR=/opt/openslx/vmchooser/config

for wait in 1 1 2 2 3 end; do
	grep '^SLX_DNS' "/opt/openslx/config" > /dev/null && break
	[ "$wait" == "end" ] && echo "No DNS config found, using google dns" && break
	echo "Waiting for DNS config.."
	sleep "$wait"
done

#source config file
. "/opt/openslx/config"
. "${VMWARE_CONF_DIR}/vmware.conf" || slxlog "vmware-conf" "Could not source '${VMWARE_CONF_DIR}/vmware.conf' in $0" "${VMWARE_CONF_DIR}/vmware.conf"

vm_dhcpd() {

   #from plugins/vmware/init-hooks/60-have-servconfig/vm-dhcpd.sh

   # read the DNS configuration and if available set WPAD configuration
   [ -z "${SLX_DNS}" ] && SLX_DNS="8.8.8.8"
   [ -n "${wpad}" ] && wpad_config="option  wpad    ${wpad}"

   # configuring dhcpd stub for virtual networks
cat > "${VMWARE_CONF_DIR}/udhcpd.conf" << EOF
# general udhcpd configuration file for virtual machines written by $0

# start and end of the IP lease block
start 		CNETWORK.20
end		CNETWORK.100

# interface that udhcpd will use
interface	NWIF

# how long an offered address is reserved (leased) in seconds
offer_time	6000

# location of the leases file
lease_file	LEASEFILE

# location of the pid file
pidfile		PIDFILE

option	dns	${SLX_DNS}
option	subnet	255.255.255.0
option	router	CNETWORK.1
option	wins	CNETWORK.10
option	domain	${SLX_NET_DOMAIN} virtual.site
${wpad_config}

# additional options known to udhcpd
#subnet			#timezone
#router			#timesvr
#namesvr		#dns
#logsvr			#cookiesvr
#lprsvr			#bootsize
#domain			#swapsvr
#rootpath		#ipttl
#mtu			#broadcast
#wins			#lease
#ntpsrv			#tftp
#bootfile		#wpad
EOF

}

setup_network() {
	#from plugins/vmware/XX_vmware.sh

	mkdir -p "/tmp/virt/vmware" -m 1777
	mkdir -p "/dev/shm/vmware" -m 1777
	mkdir -p "/var/run/vmware" -m 1777
	mkdir -p "/etc/vmware/udhcpd"    

	# variable might contain ",NAT" which is to be taken off
	if [ -n "$vmnet1" ] ; then
	      local vmip="${vmnet1%/*}" # x.x.x.x/yy => 'x.x.x.x'">
	      local vmsub=$(echo $vmip |sed 's,\(.*\)\..*,\1,') # x.x.x.x => x.x.x
	      # setting up the udhcpd configuration
	      sed -e "s,NWIF,vmnet1,;s,/misc/,/vmware/," \
			  -e "s,CNETWORK,$vmsub,;s,PIDFILE,/var/run/vmware/udhcpd-vmnet1.pid," \
			  -e "s,LEASEFILE,/var/run/vmware/udhcpd-vmnet1.leases," \
		      "${VMWARE_CONF_DIR}/udhcpd.conf" > "/etc/vmware/udhcpd/udhcpd-vmnet1.conf"
	      # setting the base samba configuration if available
	      if [ -f "$VMCHOOSER_CONF_DIR/smb.conf" -a -f "/usr/sbin/smbd" ] ; then
				sed -e "s#NWIF#vmnet1,vmnet8#;s,PIDDIR,/var/run/vmware," \
					"${VMCHOOSER_CONF_DIR}/smb.conf" > "/etc/vmware/smb.conf"
	      fi
	fi

	# vmware nat interface configuration
	if [ -n "$vmnet8" ] ; then
	      local vmnet8ip="${vmnet8%/*}"
	      local vmpx="${vmnet8#*/}"
	      local vmsub=$(echo "$vmnet8ip" | sed 's,\(.*\)\..*,\1,') # x.x.x.x => x.x.x">
	      # vmip is user for vmnet8 device
	      # vmnet is user for config files nat.conf/dhcp
	      local vmip="${vmsub}.1"
	      local vmnet="${vmsub}.2"
	      # setting up the udhcpd configuration
	      sed -e "s,NWIF,vmnet8,;s,/misc/,/vmware/," \
		  -e "s,CNETWORK,$vmsub,;s,PIDFILE,/var/run/vmware/udhcpd-vmnet8.pid," \
		  -e "s,LEASEFILE,/var/run/vmware/udhcpd-vmnet8.leases," \
	      ${VMWARE_CONF_DIR}/udhcpd.conf >/etc/vmware/udhcpd/udhcpd-vmnet8.conf
	      # might be not needed any more ...
	      echo "# Linux NAT configuration file" \
		> /etc/vmware/nat.conf
	      echo "[host]" \
		>> /etc/vmware/nat.conf
	      echo "ip = $vmnet/$vmpx" \
		>> /etc/vmware/nat.conf
	      echo "device = /dev/vmnet8" \
		>> /etc/vmware/nat.conf
	      echo "activeFTP = 1" \
		>> /etc/vmware/nat.conf
	      echo "[udp]" \
		>> /etc/vmware/nat.conf
	      echo "timeout = 60" \
		>> /etc/vmware/nat.conf
	      echo "[incomingtcp]" \
		>> /etc/vmware/nat.conf
	      echo "[incomingudp]" \
		>> /etc/vmware/nat.conf
	      echo "00:50:56:F1:30:50" > /etc/vmware/vmnet-natd-8.mac
	fi

	############################################################################
	# vmware stuff second part: setting up the environment
	# make udhcpd more silent
	touch "/var/run/vmware/udhcpd-vmnet1.leases" \
	      "/var/run/vmware/udhcpd-vmnet8.leases"

	# create the needed devices which effects all vmware options
	# they are not created automatically via module load
	for i in "/dev/vmnet0 c 119 0" "/dev/vmnet1 c 119 1" \
		 "/dev/vmnet8 c 119 8" "/dev/vmmon c 10 165"; do
		 mknod $i # no ""
	done

	chmod 0700 /dev/vmnet*
}

load_modules () {
	local FAILED=""
   # VMplayer common stuff
   insmod /lib/modules/vmware/vmmon.ko || FAILED="$FAILED vmmon"
   insmod /lib/modules/vmware/vmnet.ko || FAILED="$FAILED vmnet"
   # VMplayer 3.X specific stuff
   insmod /lib/modules/vmware/vmci.ko || FAILED="$FAILED vmci"
   insmod /lib/modules/vmware/vmblock.ko || FAILED="$FAILED vmblock"
   insmod /lib/modules/vmware/vsock.ko || FAILED="$FAILED vsock"
	[ -n "$FAILED" ] && slxlog "vmware-insmod" "Error loading vmware kernel module(s) $FAILED"
}

unload_modules () { 
   rmmod vmnet vmmonvsock vmci vmblock
}

vmnetif () { 
	# let point the path directly to the directory where the binary lives
	location="/usr/bin"
	if [ -n "$vmnet0" ] ; then
		# the path might be directly point to the plugin dir
		if ! $location/vmnet-bridge -d /var/run/vmnet-bridge-0.pid -n 0; then
			slxlog "vmware-bridge" "Launching $location/vmnet-bridge failed."
		fi
	fi
	if [ -n "$vmnet1" ] ; then
		if ! $location/vmnet-netifup -d /var/run/vmnet-netifup-vmnet1.pid /dev/vmnet1 vmnet1; then
			slxlog "vmware-netifup" "Launching $location/vmnet-netifup for vmnet1 failed."
		fi
		ip addr add $vmnet1 dev vmnet1
		ip link set vmnet1 up
		if [ -n "$vmnet1nat" ] ; then
			echo "1" >/proc/sys/net/ipv4/conf/vmnet1/forwarding
			echo "1" >/proc/sys/net/ipv4/conf/br0/forwarding 2>/dev/null
		fi
		/opt/openslx/usr/sbin/udhcpd -S /etc/vmware/udhcpd/udhcpd-vmnet1.conf
	fi
	if [ -n "$vmnet8" ] ; then
		if ! $location/vmnet-netifup -d /var/run/vmnet-netifup-vmnet8.pid /dev/vmnet8 vmnet8; then
			slxlog "vmware-netifup" "Launching $location/vmnet-netifup for vmnet8 failed."
		fi
		ip addr add $vmnet8 dev vmnet8
		ip link set vmnet8 up
		echo "1" >/proc/sys/net/ipv4/conf/vmnet8/forwarding
		echo "1" >/proc/sys/net/ipv4/conf/br0/forwarding 2>/dev/null
		iptables -t nat -A POSTROUTING -o br0 -j MASQUERADE
		# /etc/vmware/vmnet-natd-8.mac simply contains a mac like 00:50:56:F1:30:50
		if ! $location/vmnet-natd -d /var/run/vmnet-natd-8.pid -m /etc/vmware/vmnet-natd-8.mac -c /etc/vmware/nat.conf; then
			slxlog "vmware-natd" "Launching $location/vmnet-natd for vmnet8 failed."
		fi
		/opt/openslx/usr/sbin/udhcpd -S /etc/vmware/udhcpd/udhcpd-vmnet8.conf
	fi
}

vmblock () { 
	if ! /usr/bin/vmware-usbarbitrator; then
		slxlog "vmware-usbarb" "Launching /usr/bin/vmware-usbarbitrator failed."
	fi
}



case "$1" in 
  start)
    #start: defines start function for initscript
    vm_dhcpd
    setup_network
    # hack to access the first serial/parallel port
    chmod a+rw /dev/ttyS0
    chmod a+rw /dev/parport0
    mkdir -m 1777 -p /tmp/vmware
    load_modules
    vmnetif
    vmblock
  ;;
  stop)
    #stop: defines stop function for initscript
    killall vmnet-netifup vmnet-natd vmnet-bridge vmware vmplayer \
      vmware-tray vmnet-dhcpd
    # might take a while until all services are shut down
    sleep 1
    umount -l /tmp/vmware 2>/dev/null
    unload_modules
    
  ;;
  restart)
    #restart: defines restart function for initscript
    $0 stop && $0 start
  ;;
  status)
    #status: defines status function for initscript
    vmstatus
  ;;
  *)
    #usage: defines usage function for initscript
    ## print out usage 
    echo "Usage: $0 {start, stop, restart, status}" >&2 
    exit 1
  ;;
esac