This file is indexed.

/usr/lib/xcp/bin/xe-reset-networking is in xcp-xapi 1.3.2-5.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/env python

"""
Copyright (C) 2006-2009 Citrix Systems Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; version 2.1 only. with the special
exception on linking described in file LICENSE.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
"""

import sys
import os
import commands
import time
from optparse import OptionParser
#import XenAPI

pool_conf = '/etc/xcp/pool.conf'
interface_reconfigure = "/usr/lib/xcp/lib/interface-reconfigure"
inventory_file = '/etc/xcp/inventory'
management_conf = '/etc/firstboot.d/data/management.conf'
network_reset = '/tmp/network-reset'

def read_dict_file(fname):
	f = open(fname, 'r')
	d = {}
	for l in f.readlines():
		kv = l.split('=')
		d[kv[0]] = kv[1][1:-2]
	return d

def read_inventory():
	return read_dict_file(inventory_file)

def read_management_conf():
	return read_dict_file(management_conf)

def write_inventory(inventory):
	f = open(inventory_file, 'w')
	for k in inventory:
		f.write(k + "='" + inventory[k] + "'\n")
	f.close()

if __name__ == "__main__":
	parser = OptionParser()
	parser.add_option("-m", "--master", help="Master's address", dest="address", default=None)
	parser.add_option("--device", help="Device name of new management interface", dest="device", default=None)
	parser.add_option("--mode", help='IP configuration mode for new management interface: "dhcp" or "static" (default is dhcp)', dest="mode", default="dhcp")
	parser.add_option("--ip", help="IP address for new management interface", dest="ip", default='')
	parser.add_option("--netmask", help="Netmask for new management interface", dest="netmask", default='')
	parser.add_option("--gateway", help="Gateway for new management interface", dest="gateway", default='')
	parser.add_option("--dns", help="DNS server for new management interface", dest="dns", default='')
	(options, args) = parser.parse_args()
	
	# Determine pool role
	try:
		f = open(pool_conf, 'r')
		try:
			l = f.readline()
			ls = l.split(':')
			if ls[0] == 'master':
				master = True
				address = 'localhost'
			else:
				master = False
				address = ls[1]
		finally:
			f.close()
	except:
		pass
	
	# Get the management device from the firstboot data if not specified by the user
	if options.device == None:
		try:
			conf = read_management_conf()
			device = conf['LABEL']
		except:
			print "Could not figure out which interface should become the management interface. \
				Please specify one using the --device option."
			sys.exit(1)
	else:
		device = options.device
	print "The management interface will be", device

	# Determine IP configuration for management interface
	options.mode = options.mode.lower()
	if options.mode not in ["dhcp", "static"]:
		parser.error('mode should be either "dhcp" or "static"')
		sys.exit(1)
		
	if options.mode == 'static' and (options.ip == '' or options.netmask == ''):
		parser.error("if static IP mode is selected, an IP address and netmask need to be specified")
		sys.exit(1)
	
	# Warn user
	if not os.access('/tmp/fist_network_reset_no_warning', os.F_OK):
		warning = """!! WARNING !!

This command will reboot the host and reset its network configuration.
Before completing this command:
- Shut down all VMs running on this host.
- Disable HA if this host is part of a resource pool with HA enabled.

Type 'yes' to continue.
"""
		res = raw_input(warning)
		if res <> 'yes':
			sys.exit(1)
	
	# Update master's IP, if needed and given
	if master == True and options.address != None:
		address = options.address
		print "Setting master's ip (" + address + ")..."
		try:
			f = open(pool_conf, 'w')
			f.write('slave:' + address)
		finally:
			f.close()

	# Construct bridge name for management interface based on convention
	if device[:3] == 'eth':
		bridge = 'xenbr' + device[3:]
	else:
		bridge = 'br' + device
	
	# Ensure xapi is not running
	print "Stopping xapi..."
	os.system('service xapi stop >/dev/null 2>/dev/null')
	
	# Reconfigure new management interface
	print "Reconfiguring " + device + "..."
	if os.access('/tmp/do-not-use-networkd', os.F_OK):
		if_args = ' --force ' + bridge + ' rewrite --mac=x --device=' + device + ' --mode=' + options.mode
		if options.mode == 'static':
			if_args += ' --ip=' + options.ip + ' --netmask=' + options.netmask
			if options.gateway != '':
				if_args += ' --gateway=' + options.gateway
		os.system(interface_reconfigure + if_args + ' >/dev/null 2>/dev/null')
	else:
		os.remove('/var/lib/xcp/networkd.db')

	# Update interfaces in inventory file
	print 'Updating inventory file...'
	inventory = read_inventory()
	inventory['MANAGEMENT_INTERFACE'] = bridge
	inventory['CURRENT_INTERFACES'] = ''
	write_inventory(inventory)

	# Rewrite firstboot management.conf file, which will be picked it by xcp-networkd on restart (if used)
	try:
		f = file(management_conf, 'w')
		f.write("LABEL='" + device + "'\n")
		f.write("MODE='" + options.mode + "'\n")
		if options.mode == 'static':
			f.write("IP='" + options.ip + "'\n")
			f.write("NETMASK='" + options.netmask + "'\n")
			if options.gateway != '':
				f.write("GATEWAY='" + options.gateway + "'\n")
			if options.dns != '':
				f.write("DNS='" + options.dns + "'\n")
	finally:
		f.flush()
		os.fsync(f.fileno())
		f.close()

	# Write trigger file for XAPI to continue the network reset on startup
	try:
		f = file(network_reset, 'w')
		f.write('DEVICE=' + device + '\n')
		f.write('MODE=' + options.mode + '\n')
		if options.mode == 'static':
			f.write('IP=' + options.ip + '\n')
			f.write('NETMASK=' + options.netmask + '\n')
			if options.gateway != '':
				f.write('GATEWAY=' + options.gateway + '\n')
			if options.dns != '':
				f.write('DNS=' + options.dns + '\n')
	finally:
		f.flush()
		os.fsync(f.fileno())
		f.close()

	# Reset the domain 0 network interface naming configuration
	# back to a fresh-install state for the currently-installed
	# hardware.
	os.system("/etc/sysconfig/network-scripts/interface-rename.py --reset-to-install")

	# Reboot
	os.system("/sbin/reboot -f")