summaryrefslogtreecommitdiffstats
path: root/tools/esx/guest_tool.py
blob: e7b742cc0fe8625a1daf82b4ff4f4f60a2f2b4ae (plain)
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
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2011 Citrix Systems, Inc.
# Copyright 2011 OpenStack LLC.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Guest tools for ESX to set up network in the guest.
On Windows we require pyWin32 installed on Python.
"""

import array
import gettext
import logging
import os
import platform
import socket
import struct
import subprocess
import sys
import time

gettext.install('nova', unicode=1)

PLATFORM_WIN = 'win32'
PLATFORM_LINUX = 'linux2'
ARCH_32_BIT = '32bit'
ARCH_64_BIT = '64bit'
NO_MACHINE_ID = 'No machine id'

# Logging
FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
if sys.platform == PLATFORM_WIN:
    LOG_DIR = os.path.join(os.environ.get('ALLUSERSPROFILE'), 'openstack')
elif sys.platform == PLATFORM_LINUX:
    LOG_DIR = '/var/log/openstack'
else:
    LOG_DIR = 'logs'
if not os.path.exists(LOG_DIR):
    os.mkdir(LOG_DIR)
LOG_FILENAME = os.path.join(LOG_DIR, 'openstack-guest-tools.log')
logging.basicConfig(filename=LOG_FILENAME, format=FORMAT)

if sys.hexversion < 0x3000000:
    _byte = ord    # 2.x chr to integer
else:
    _byte = int    # 3.x byte to integer


class ProcessExecutionError:
    """Process Execution Error Class."""

    def __init__(self, exit_code, stdout, stderr, cmd):
        self.exit_code = exit_code
        self.stdout = stdout
        self.stderr = stderr
        self.cmd = cmd

    def __str__(self):
        return str(self.exit_code)


def _bytes2int(bytes):
    """Convert bytes to int."""
    intgr = 0
    for byt in bytes:
        intgr = (intgr << 8) + _byte(byt)
    return intgr


def _parse_network_details(machine_id):
    """
    Parse the machine_id to get MAC, IP, Netmask and Gateway fields per NIC.
    machine_id is of the form ('NIC_record#NIC_record#', '')
    Each of the NIC will have record NIC_record in the form
    'MAC;IP;Netmask;Gateway;Broadcast;DNS' where ';' is field separator.
    Each record is separated by '#' from next record.
    """
    logging.debug(_("Received machine_id from vmtools : %s") % machine_id[0])
    network_details = []
    if machine_id[1].strip() == "1":
        pass
    else:
        for machine_id_str in machine_id[0].split('#'):
            network_info_list = machine_id_str.split(';')
            if len(network_info_list) % 6 != 0:
                break
            no_grps = len(network_info_list) / 6
            i = 0
            while i < no_grps:
                k = i * 6
                network_details.append((
                                network_info_list[k].strip().lower(),
                                network_info_list[k + 1].strip(),
                                network_info_list[k + 2].strip(),
                                network_info_list[k + 3].strip(),
                                network_info_list[k + 4].strip(),
                                network_info_list[k + 5].strip().split(',')))
                i += 1
    logging.debug(_("NIC information from vmtools : %s") % network_details)
    return network_details


def _get_windows_network_adapters():
    """Get the list of windows network adapters."""
    import win32com.client
    wbem_locator = win32com.client.Dispatch('WbemScripting.SWbemLocator')
    wbem_service = wbem_locator.ConnectServer('.', 'root\cimv2')
    wbem_network_adapters = wbem_service.InstancesOf('Win32_NetworkAdapter')
    network_adapters = []
    for adapter in wbem_network_adapters:
        if (adapter.NetConnectionStatus == 2 or
            adapter.NetConnectionStatus == 7):
            adapter_name = adapter.NetConnectionID
            mac_address = adapter.MacAddress.lower()
            config = adapter.associators_(
                          'Win32_NetworkAdapterSetting',
                          'Win32_NetworkAdapterConfiguration')[0]
            ip_address = ''
            subnet_mask = ''
            if config.IPEnabled:
                ip_address = config.IPAddress[0]
                subnet_mask = config.IPSubnet[0]
                #config.DefaultIPGateway[0]
            network_adapters.append({'name': adapter_name,
                                    'mac-address': mac_address,
                                    'ip-address': ip_address,
                                    'subnet-mask': subnet_mask})
    return network_adapters


def _get_linux_network_adapters():
    """Get the list of Linux network adapters."""
    import fcntl
    max_bytes = 8096
    arch = platform.architecture()[0]
    if arch == ARCH_32_BIT:
        offset1 = 32
        offset2 = 32
    elif arch == ARCH_64_BIT:
        offset1 = 16
        offset2 = 40
    else:
        raise OSError(_("Unknown architecture: %s") % arch)
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    names = array.array('B', '\0' * max_bytes)
    outbytes = struct.unpack('iL', fcntl.ioctl(
        sock.fileno(),
        0x8912,
        struct.pack('iL', max_bytes, names.buffer_info()[0])))[0]
    adapter_names = [names.tostring()[n_cnt:n_cnt + offset1].split('\0', 1)[0]
                     for n_cnt in xrange(0, outbytes, offset2)]
    network_adapters = []
    for adapter_name in adapter_names:
        ip_address = socket.inet_ntoa(fcntl.ioctl(
            sock.fileno(),
            0x8915,
            struct.pack('256s', adapter_name))[20:24])
        subnet_mask = socket.inet_ntoa(fcntl.ioctl(
            sock.fileno(),
            0x891b,
            struct.pack('256s', adapter_name))[20:24])
        raw_mac_address = '%012x' % _bytes2int(fcntl.ioctl(
            sock.fileno(),
            0x8927,
            struct.pack('256s', adapter_name))[18:24])
        mac_address = ":".join([raw_mac_address[m_counter:m_counter + 2]
            for m_counter in range(0, len(raw_mac_address), 2)]).lower()
        network_adapters.append({'name': adapter_name,
                                 'mac-address': mac_address,
                                 'ip-address': ip_address,
                                 'subnet-mask': subnet_mask})
    return network_adapters


def _get_adapter_name_and_ip_address(network_adapters, mac_address):
    """Get the adapter name based on the MAC address."""
    adapter_name = None
    ip_address = None
    for network_adapter in network_adapters:
        if network_adapter['mac-address'] == mac_address.lower():
            adapter_name = network_adapter['name']
            ip_address = network_adapter['ip-address']
            break
    return adapter_name, ip_address


def _get_win_adapter_name_and_ip_address(mac_address):
    """Get Windows network adapter name."""
    network_adapters = _get_windows_network_adapters()
    return _get_adapter_name_and_ip_address(network_adapters, mac_address)


def _get_linux_adapter_name_and_ip_address(mac_address):
    """Get Linux network adapter name."""
    network_adapters = _get_linux_network_adapters()
    return _get_adapter_name_and_ip_address(network_adapters, mac_address)


def _execute(cmd_list, process_input=None, check_exit_code=True):
    """Executes the command with the list of arguments specified."""
    cmd = ' '.join(cmd_list)
    logging.debug(_("Executing command: '%s'") % cmd)
    env = os.environ.copy()
    obj = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
    result = None
    if process_input is not None:
        result = obj.communicate(process_input)
    else:
        result = obj.communicate()
    obj.stdin.close()
    if obj.returncode:
        logging.debug(_("Result was %s") % obj.returncode)
        if check_exit_code and obj.returncode != 0:
            (stdout, stderr) = result
            raise ProcessExecutionError(exit_code=obj.returncode,
                                        stdout=stdout,
                                        stderr=stderr,
                                        cmd=cmd)
    time.sleep(0.1)
    return result


def _windows_set_networking():
    """Set IP address for the windows VM."""
    program_files = os.environ.get('PROGRAMFILES')
    program_files_x86 = os.environ.get('PROGRAMFILES(X86)')
    vmware_tools_bin = None
    if os.path.exists(os.path.join(program_files, 'VMware', 'VMware Tools',
                                   'vmtoolsd.exe')):
        vmware_tools_bin = os.path.join(program_files, 'VMware',
                                    'VMware Tools', 'vmtoolsd.exe')
    elif os.path.exists(os.path.join(program_files, 'VMware', 'VMware Tools',
                                     'VMwareService.exe')):
        vmware_tools_bin = os.path.join(program_files, 'VMware',
                                      'VMware Tools', 'VMwareService.exe')
    elif program_files_x86 and os.path.exists(os.path.join(program_files_x86,
                                       'VMware', 'VMware Tools',
                                       'VMwareService.exe')):
        vmware_tools_bin = os.path.join(program_files_x86, 'VMware',
                                        'VMware Tools', 'VMwareService.exe')
    if vmware_tools_bin:
        cmd = ['"' + vmware_tools_bin + '"', '--cmd', 'machine.id.get']
        for network_detail in _parse_network_details(_execute(cmd,
                                              check_exit_code=False)):
            (mac_address, ip_address, subnet_mask, gateway, broadcast,
             dns_servers) = network_detail
            name_and_ip = _get_win_adapter_name_and_ip_address(mac_address)
            adapter_name, current_ip_address = name_and_ip
            if adapter_name and not ip_address == current_ip_address:
                cmd = ['netsh', 'interface', 'ip', 'set', 'address',
                       'name="%s"' % adapter_name, 'source=static', ip_address,
                       subnet_mask, gateway, '1']
                _execute(cmd)
                # Windows doesn't let you manually set the broadcast address
                for dns_server in dns_servers:
                    if dns_server:
                        cmd = ['netsh', 'interface', 'ip', 'add', 'dns',
                               'name="%s"' % adapter_name, dns_server]
                        _execute(cmd)
    else:
        logging.warn(_("VMware Tools is not installed"))


def _filter_duplicates(all_entries):
    final_list = []
    for entry in all_entries:
        if entry and entry not in final_list:
            final_list.append(entry)
    return final_list


def _set_rhel_networking(network_details=None):
    """Set IPv4 network settings for RHEL distros."""
    network_details = network_details or []
    all_dns_servers = []
    for network_detail in network_details:
        (mac_address, ip_address, subnet_mask, gateway, broadcast,
         dns_servers) = network_detail
        all_dns_servers.extend(dns_servers)
        name_and_ip = _get_linux_adapter_name_and_ip_address(mac_address)
        adapter_name, current_ip_address = name_and_ip
        if adapter_name and not ip_address == current_ip_address:
            interface_file_name = ('/etc/sysconfig/network-scripts/ifcfg-%s' %
                                   adapter_name)
            # Remove file
            os.remove(interface_file_name)
            # Touch file
            _execute(['touch', interface_file_name])
            interface_file = open(interface_file_name, 'w')
            interface_file.write('\nDEVICE=%s' % adapter_name)
            interface_file.write('\nUSERCTL=yes')
            interface_file.write('\nONBOOT=yes')
            interface_file.write('\nBOOTPROTO=static')
            interface_file.write('\nBROADCAST=%s' % broadcast)
            interface_file.write('\nNETWORK=')
            interface_file.write('\nGATEWAY=%s' % gateway)
            interface_file.write('\nNETMASK=%s' % subnet_mask)
            interface_file.write('\nIPADDR=%s' % ip_address)
            interface_file.write('\nMACADDR=%s' % mac_address)
            interface_file.close()
    if all_dns_servers:
        dns_file_name = "/etc/resolv.conf"
        os.remove(dns_file_name)
        _execute(['touch', dns_file_name])
        dns_file = open(dns_file_name, 'w')
        dns_file.write("; generated by OpenStack guest tools")
        unique_entries = _filter_duplicates(all_dns_servers)
        for dns_server in unique_entries:
            dns_file.write("\nnameserver %s" % dns_server)
        dns_file.close()
    _execute(['/sbin/service', 'network', 'restart'])


def _set_ubuntu_networking(network_details=None):
    """Set IPv4 network settings for Ubuntu."""
    network_details = network_details or []
    all_dns_servers = []
    interface_file_name = '/etc/network/interfaces'
    # Remove file
    os.remove(interface_file_name)
    # Touch file
    _execute(['touch', interface_file_name])
    interface_file = open(interface_file_name, 'w')
    for device, network_detail in enumerate(network_details):
        (mac_address, ip_address, subnet_mask, gateway, broadcast,
         dns_servers) = network_detail
        all_dns_servers.extend(dns_servers)
        name_and_ip = _get_linux_adapter_name_and_ip_address(mac_address)
        adapter_name, current_ip_address = name_and_ip

        if adapter_name:
            interface_file.write('\nauto %s' % adapter_name)
            interface_file.write('\niface %s inet static' % adapter_name)
            interface_file.write('\nbroadcast %s' % broadcast)
            interface_file.write('\ngateway %s' % gateway)
            interface_file.write('\nnetmask %s' % subnet_mask)
            interface_file.write('\naddress %s\n' % ip_address)
        logging.debug(_("Successfully configured NIC %(device)d with "
                        "NIC info %(detail)s"), {'device': device,
                                                 'detail': network_detail))
    interface_file.close()

    if all_dns_servers:
        dns_file_name = "/etc/resolv.conf"
        os.remove(dns_file_name)
        _execute(['touch', dns_file_name])
        dns_file = open(dns_file_name, 'w')
        dns_file.write("; generated by OpenStack guest tools")
        unique_entries = _filter_duplicates(all_dns_servers)
        for dns_server in unique_entries:
            dns_file.write("\nnameserver %s" % dns_server)
        dns_file.close()

    logging.debug(_("Restarting networking....\n"))
    _execute(['/etc/init.d/networking', 'restart'])


def _linux_set_networking():
    """Set IP address for the Linux VM."""
    vmware_tools_bin = None
    if os.path.exists('/usr/sbin/vmtoolsd'):
        vmware_tools_bin = '/usr/sbin/vmtoolsd'
    elif os.path.exists('/usr/bin/vmtoolsd'):
        vmware_tools_bin = '/usr/bin/vmtoolsd'
    elif os.path.exists('/usr/sbin/vmware-guestd'):
        vmware_tools_bin = '/usr/sbin/vmware-guestd'
    elif os.path.exists('/usr/bin/vmware-guestd'):
        vmware_tools_bin = '/usr/bin/vmware-guestd'
    if vmware_tools_bin:
        cmd = [vmware_tools_bin, '--cmd', 'machine.id.get']
        network_details = _parse_network_details(_execute(cmd,
                                                check_exit_code=False))
        # TODO(sateesh): For other distros like suse, debian, BSD, etc.
        if(platform.dist()[0] == 'Ubuntu'):
            _set_ubuntu_networking(network_details)
        elif (platform.dist()[0] == 'redhat'):
            _set_rhel_networking(network_details)
        else:
            logging.warn(_("Distro '%s' not supported") % platform.dist()[0])
    else:
        logging.warn(_("VMware Tools is not installed"))

if __name__ == '__main__':
    pltfrm = sys.platform
    if pltfrm == PLATFORM_WIN:
        _windows_set_networking()
    elif pltfrm == PLATFORM_LINUX:
        _linux_set_networking()
    else:
        raise NotImplementedError(_("Platform not implemented: '%s'") % pltfrm)