/usr/lib/xcp/bin/static-vdis 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 195 196 197 198 199 200 201 202 203 204 205 206 | #!/usr/bin/env python
# Common functions for managing statically-attached (ie onboot, without xapi) VDIs
import sys, os, subprocess
import XenAPI, inventory, xmlrpclib
main_dir = "/etc/xcp/static-vdis"
def read_whole_file(filename):
f = open(filename)
try:
return reduce(lambda x, y: x + y, f.readlines(), "").strip()
finally:
f.close()
def write_whole_file(filename, contents):
f = open(filename, "w")
try:
f.write(contents)
finally:
f.close()
def load(name):
"""Return a dictionary describing a single static VDI"""
d = { "id": name }
for key in [ "vdi-uuid", "config", "reason" ]:
d[key] = read_whole_file("%s/%s/%s" % (main_dir, name, key))
try:
disk = "%s/%s/disk" % (main_dir, name)
os.stat(disk) # throws an error if missing
d["disk"] = os.readlink(disk)
except:
pass
dnb = "False"
try:
os.stat("%s/%s/delete-next-boot" % (main_dir, name))
dnb = "True"
except:
pass
d["delete-next-boot"] = dnb
return d
def list():
all = []
try:
all = os.listdir(main_dir)
except:
pass
return map(load, all)
def fresh_name():
all = []
try:
all = os.listdir(main_dir)
for i in range(0, len(all) + 1): # guarantees to find a unique number
i = str(i)
if not(i in all):
return i
except:
# Directory doesn't exist
os.mkdir(main_dir)
return "0"
def to_string_list(d):
keys = [ "vdi-uuid", "reason", "currently-attached", "delete-next-boot" "path" ]
m = 0
for key in keys:
if len(key) > m:
m = m + len(key)
def left(key, value):
return key + (" " * (m - len(key))) + ": " + value
def right(key, value):
return (" " * (m - len(key))) + key + ": " + value
l = [ left("vdi-uuid", d["vdi-uuid"]), right("reason", d["reason"]) ]
l.append(right("delete-next-boot", d["delete-next-boot"]))
if d.has_key("disk"):
l.append(right("currently-attached", "True"))
l.append(right("path", d['disk']))
else:
l.append(right("currently-attached", "False"))
return l
def add(session, vdi_uuid, reason):
for existing in list():
if existing['vdi-uuid'] == vdi_uuid:
if existing['delete-next-boot'] == "True":
# Undo the 'delete-next-boot' flag to reinstitute
path = main_dir + "/" + existing['id']
os.unlink(path + "/delete-next-boot")
os.unlink(path + "/reason")
write_whole_file(path + "/reason", reason)
# Assume config is still valid
return
raise "Static configuration for VDI already exists"
vdi = session.xenapi.VDI.get_by_uuid(vdi_uuid)
host = session.xenapi.host.get_by_uuid(inventory.get_localhost_uuid ())
config = None
try:
config = session.xenapi.VDI.generate_config(host, vdi)
except XenAPI.Failure, e:
raise "Failed generating static config file: %s" % (str(e))
sr = session.xenapi.VDI.get_SR(vdi)
ty = session.xenapi.SR.get_type(sr)
filename = None
all_sm = session.xenapi.SM.get_all_records()
for sm_ref in all_sm.keys():
if all_sm[sm_ref]['type'] == ty:
filename = session.xenapi.SM.get_driver_filename(sm_ref)
if filename == None:
raise "Unable to discover SM plugin driver filename"
# Make a fresh directory in main_dir to store the configuration. Note
# there is no locking so please run this script serially.
fresh = fresh_name()
path = main_dir + "/" + fresh
os.mkdir(path)
write_whole_file(path + "/vdi-uuid", vdi_uuid)
write_whole_file(path + "/config", config)
write_whole_file(path + "/reason", reason)
os.symlink(filename, path + "/driver")
def delete(vdi_uuid):
found = False
for existing in list():
if existing['vdi-uuid'] == vdi_uuid:
found = True
path = main_dir + "/" + existing['id']
f = open(path + "/delete-next-boot", "w")
f.close()
# If not currently attached then zap the whole tree
if not(existing.has_key("disk")):
os.system("/bin/rm -rf %s" % path)
if not found:
raise "Disk configuration not found"
# Copied by util.py
def doexec(args, inputtext=None):
"""Execute a subprocess, then return its return code, stdout and stderr"""
proc = subprocess.Popen(args,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)
(stdout,stderr) = proc.communicate(inputtext)
rc = proc.returncode
return (rc,stdout,stderr)
def call_backend_attach(driver, config):
xml = doexec([ driver, config ])
if xml[0] <> 0:
raise "SM_BACKEND_FAILURE(%d, %s, %s)" % xml
xmlrpc = xmlrpclib.loads(xml[1])
path = xmlrpc[0][0]
return path
def attach(vdi_uuid):
found = False
for existing in list():
if existing['vdi-uuid'] == vdi_uuid:
found = True
if not(existing.has_key('path')):
d = main_dir + "/" + existing['id']
# Delete any old symlink
try:
os.unlink(d + "/disk")
except:
pass
config = read_whole_file(d + "/config")
path = call_backend_attach(d + "/driver", config)
os.symlink(path, d + "/disk")
return d + "/disk"
if not found:
raise "Disk configuration not found"
def usage():
print "Usage:"
print " %s list -- print a list of VDIs which will be attached on host boot" % sys.argv[0]
print " %s add <uuid> <reason> -- make the VDI <uuid> available on host boot" % sys.argv[0]
print " %s del <uuid> -- cease making the VDI <uuid> available on host boot" % sys.argv[0]
print " %s attach <uuid> -- attach the VDI immediately" % sys.argv[0]
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) < 2:
usage()
if sys.argv[1] == "list" and len(sys.argv) == 2:
for i in list ():
for line in to_string_list(i):
print line
print
elif sys.argv[1] == "add" and len(sys.argv) == 4:
session = XenAPI.xapi_local()
session.xenapi.login_with_password("root", "")
try:
add(session, sys.argv[2], sys.argv[3])
finally:
session.xenapi.logout()
elif sys.argv[1] == "del" and len(sys.argv) == 3:
delete(sys.argv[2])
elif sys.argv[1] == "attach" and len(sys.argv) == 3:
path = attach(sys.argv[2])
print path
else:
usage()
|