from xml.dom.minidom import getDOMImplementation
from xml.dom.minidom import parse as parseXML
+the_root_prefix = ""
+def root_prefix():
+ """Returns a string to prefix to all file name references, which
+ is useful for testing."""
+ return the_root_prefix
+def set_root_prefix(prefix):
+ global the_root_prefix
+ the_root_prefix = prefix
+
#
# Logging.
#
def run_command(command):
log("Running command: " + ' '.join(command))
- rc = os.spawnl(os.P_WAIT, command[0], *command)
+ rc = os.spawnl(os.P_WAIT, root_prefix() + command[0], *command)
if rc != 0:
log("Command failed %d: " % rc + ' '.join(command))
return False
class DatabaseCache(object):
def __read_xensource_inventory(self):
- filename = "/etc/xensource-inventory"
+ filename = root_prefix() + "/etc/xensource-inventory"
f = open(filename, "r")
lines = [x.strip("\n") for x in f.readlines()]
f.close()
else:
log("Loading xapi database cache from %s" % cache_file)
- xml = parseXML(cache_file)
+ xml = parseXML(root_prefix() + cache_file)
self.__pifs = {}
self.__bonds = {}
#
def netdev_exists(netdev):
- return os.path.exists("/sys/class/net/" + netdev)
+ return os.path.exists(root_prefix() + "/sys/class/net/" + netdev)
def pif_netdev_name(pif):
"""Get the netdev name for a PIF."""
# XXX Need a datapath object for bridgeless PIFs
try:
- network_conf = open("/etc/xensource/network.conf", 'r')
+ network_conf = open(root_prefix() + "/etc/xensource/network.conf", 'r')
network_backend = network_conf.readline().strip()
network_conf.close()
except Exception, e:
import sys
import time
-sysfs_bonding_masters = "/sys/class/net/bonding_masters"
+sysfs_bonding_masters = root_prefix() + "/sys/class/net/bonding_masters"
def open_pif_ifcfg(pif):
pifrec = db().get_pif_record(pif)
interface = pif_netdev_name(pif)
log("Configuring %s (%s)" % (interface, pifrec['MAC']))
- f = ConfigurationFile("/etc/sysconfig/network-scripts/ifcfg-%s" % interface)
+ f = ConfigurationFile("%s/etc/sysconfig/network-scripts/ifcfg-%s" % (root_prefix(), interface))
f.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
(os.path.basename(f.path()), os.path.basename(sys.argv[0])))
log("Failed to load bonding driver: %s" % e)
def bonding_driver_loaded():
- lines = open("/proc/modules").read().split("\n")
+ lines = open(root_prefix() + "/proc/modules").read().split("\n")
modules = [line.split(" ")[0] for line in lines]
return "bonding" in modules
def interface_is_up(pif):
try:
interface = pif_netdev_name(pif)
- state = open("/sys/class/net/%s/operstate" % interface).read().strip()
+ state = open("%s/sys/class/net/%s/operstate" % (root_prefix(), interface)).read().strip()
return state == "up"
except:
return False # interface prolly doesn't exist
# which they were attached. The first slave attached must be the last detached since
# the bond is using its MAC address.
try:
- attached_slaves = open("/sys/class/net/%s/bonding/slaves" % pifrec['device']).readline().split()
+ attached_slaves = open("%s/sys/class/net/%s/bonding/slaves" % (root_prefix(), pifrec['device'])).readline().split()
for slave in attached_slaves:
pifs = [p for p in db().get_pifs_by_device(slave) if not pif_is_vlan(p)]
slave_pif = pifs[0]
--pif A PIF reference within the session.
--pif-uuid The UUID of a PIF.
--force An interface name.
+ --root-prefix=DIR Use DIR as alternate root directory (for testing).
"""
# Notes:
pifrec = db().get_pif_record(pif)
try:
- f = open("/proc/ardence")
+ f = open(root_prefix() + "/proc/ardence")
macline = filter(lambda x: x.startswith("HWaddr:"), f.readlines())
f.close()
if len(macline) == 1:
def get_netdev_mac(device):
try:
- return read1("/sys/class/net/%s/address" % device)
+ return read1("%s/sys/class/net/%s/address" % (root_prefix(), device))
except:
# Probably no such device.
return None
def get_netdev_tx_queue_len(device):
try:
- return int(read1("/sys/class/net/%s/tx_queue_len" % device))
+ return int(read1("%s/sys/class/net/%s/tx_queue_len" % (root_prefix(), device)))
except:
# Probably no such device.
return None
def get_netdev_by_mac(mac):
- for device in os.listdir("/sys/class/net"):
+ for device in os.listdir(root_prefix() + "/sys/class/net"):
dev_mac = get_netdev_mac(device)
if (dev_mac and mac.lower() == dev_mac.lower() and
get_netdev_tx_queue_len(device)):
if not netdev_exists(netdev):
log("ifdown: device %s does not exist, ignoring" % netdev)
return
- if not os.path.exists("/etc/sysconfig/network-scripts/ifcfg-%s" % netdev):
+ if not os.path.exists("%s/etc/sysconfig/network-scripts/ifcfg-%s" % (root_prefix(), netdev)):
log("ifdown: device %s exists but ifcfg-%s does not" % (netdev,netdev))
run_command(["/sbin/ifconfig", netdev, 'down'])
return
def ifup(netdev):
"""Bring up a network interface"""
- if not os.path.exists("/etc/sysconfig/network-scripts/ifcfg-%s" % netdev):
+ if not os.path.exists(root_prefix() + "/etc/sysconfig/network-scripts/ifcfg-%s" % netdev):
raise Error("ifup: device %s exists but ifcfg-%s does not" % (netdev,netdev))
run_command(["/sbin/ifup", netdev])
# The key is not present, i.e. there are no static routes
lines = []
- child = ConfigurationFile("/etc/sysconfig/network-scripts/route-%s" % interface)
+ child = ConfigurationFile("%s/etc/sysconfig/network-scripts/route-%s" % (root_prefix(), interface))
child.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
(os.path.basename(child.path()), os.path.basename(sys.argv[0])))
log("Writing network configuration for %s" % ipdev)
- f = ConfigurationFile("/etc/sysconfig/network-scripts/ifcfg-%s" % ipdev)
+ f = ConfigurationFile("%s/etc/sysconfig/network-scripts/ifcfg-%s" % (root_prefix(), ipdev))
f.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
(os.path.basename(f.path()), os.path.basename(sys.argv[0])))
is_gatewaydev = defaultroute_pif == pif
if is_dnsdev or is_gatewaydev:
- fnetwork = ConfigurationFile("/etc/sysconfig/network")
+ fnetwork = ConfigurationFile(root_prefix() + "/etc/sysconfig/network")
for line in fnetwork.readlines():
if is_dnsdev and line.lstrip().startswith('DNSDEV='):
fnetwork.write('DNSDEV=%s\n' % ipdev)
dp.post()
# Update /etc/issue (which contains the IP address of the management interface)
- os.system("/sbin/update-issue")
+ os.system(root_prefix() + "/sbin/update-issue")
f.commit()
except Error, e:
log("Configuring %s using %s configuration" % (bridge, mode))
- f = ConfigurationFile(dbcache_file)
+ f = ConfigurationFile(root_prefix() + dbcache_file)
pif_uuid = getUUID()
network_uuid = getUUID()
"force-interface=",
"management",
"mac=", "device=", "mode=", "ip=", "netmask=", "gateway=",
+ "root-prefix=",
"help" ]
arglist, args = getopt.gnu_getopt(argv[1:], shortops, longops)
except getopt.GetoptError, msg:
force_management = True
elif o in ["--mac", "--device", "--mode", "--ip", "--netmask", "--gateway"]:
force_rewrite_config[o[2:]] = a
+ elif o == "--root-prefix":
+ set_root_prefix(a)
elif o == "-h" or o == "--help":
print __doc__ % {'command-name': os.path.basename(argv[0])}
return 0