12 from account import Account
14 import plnode.bwlimit as bwlimit
18 libvirt.VIR_DOMAIN_NOSTATE: 'no state',
19 libvirt.VIR_DOMAIN_RUNNING: 'running',
20 libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
21 libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
22 libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
23 libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
24 libvirt.VIR_DOMAIN_CRASHED: 'crashed',
27 # with fedora24 and (broken) libvirt-python-1.3.3-3,
28 # the following symbols are not available
29 # kashyap on IRC reported that libvirt-python-1.3.5-1.fc24.x86_64
30 # did not have the issue though
34 libvirt.VIR_CONNECT_CLOSE_REASON_ERROR: 'Misc I/O error',
36 libvirt.VIR_CONNECT_CLOSE_REASON_EOF: 'End-of-file from server',
38 libvirt.VIR_CONNECT_CLOSE_REASON_KEEPALIVE: 'Keepalive timer triggered',
40 libvirt.VIR_CONNECT_CLOSE_REASON_CLIENT: 'Client requested it',
44 # libvirt.VIR_CONNECT_CLOSE_REASON_ERROR
46 # libvirt.VIR_CONNECT_CLOSE_REASON_EOF
47 1 : 'End-of-file from server',
48 # libvirt.VIR_CONNECT_CLOSE_REASON_KEEPALIVE
49 2 : 'Keepalive timer triggered',
50 # libvirt.VIR_CONNECT_CLOSE_REASON_CLIENT
51 3 : 'Client requested it',
53 logger.log("WARNING : using hard-wired constants instead of symbolic names for CONNECT_CLOSE*")
59 class Sliver_Libvirt(Account):
64 def getConnection(sliver_type):
66 returns a connection to the underlying libvirt service
67 a single connection is created and shared among slivers
68 this call ensures the connection is alive
69 and will reconnect if it appears to be necessary
71 # sliver_type comes from rec['type'] and is of the form sliver.{LXC,QEMU}
72 # so we need to lower case to lxc/qemu
73 vtype = sliver_type.split('.')[1].lower()
75 if uri not in connections:
77 conn = libvirt.open(uri)
78 connections[uri] = conn
81 # connection already available : check for health
82 conn = connections[uri]
83 # see if a reconnection is needed
85 numDomains = conn.numOfDomains()
87 logger.log("libvirt connection to {} looks broken - reconnecting".format(uri))
88 conn = libvirt.open(uri)
89 # if this fails then an expection is thrown outside of this function
90 numDomains = conn.numOfDomains()
93 def __init__(self, rec):
94 self.name = rec['name']
95 logger.verbose ('sliver_libvirt: {} init'.format(self.name))
97 # Assume the directory with the image and config files
102 self.slice_id = rec['slice_id']
104 self.conn = Sliver_Libvirt.getConnection(rec['type'])
105 self.xid = bwlimit.get_xid(self.name)
109 dom = self.conn.lookupByName(self.name)
111 logger.log('sliver_libvirt: Domain {} does not exist. ' \
112 'Will try to create it again.'.format(self.name))
113 self.__class__.create(rec['name'], rec)
114 dom = self.conn.lookupByName(self.name)
118 def dom_details (dom):
120 output += " id={} - OSType={}".format(dom.ID(), dom.OSType())
121 # calling state() seems to be working fine
122 (state, reason) = dom.state()
123 output += " state={}, reason={}".format(STATES.get(state, state),
124 REASONS.get(reason, reason))
126 # try to use info() - this however does not work for some reason on f20
127 # info cannot get info operation failed: Cannot read cputime for domain
128 [state, maxmem, mem, ncpu, cputime] = dom.info()
129 output += " [info: state={}, maxmem = {}, mem = {}, ncpu = {}, cputime = {}]"\
130 .format(STATES.get(state, state), maxmem, mem, ncpu, cputime)
132 # too bad but libvirt.py prints out stuff on stdout when this fails, don't know how to get rid of that..
133 output += " [info: not available]"
137 ''' Helper method to get a "nice" output of the domain struct for debug purposes'''
138 output = "Domain {}".format(self.name)
141 output += " [no attached dom ?!?]"
143 output += Sliver_Libvirt.dom_details (dom)
146 # Thierry : I am not quite sure if /etc/libvirt/lxc/<>.xml holds a reliably up-to-date
147 # copy of the sliver XML config; I feel like issuing a virsh dumpxml first might be safer
148 def repair_veth(self):
149 # See workaround email, 2-14-2014, "libvirt 1.2.1 rollout"
150 xmlfilename = "/etc/libvirt/lxc/{}.xml".format(self.name)
151 with open(xmlfilename) as xmlfile:
153 veths = re.findall("<target dev='veth[0-9]*'/>", xml)
154 veths = [x[13:-3] for x in veths]
156 command = ["ip", "link", "delete", veth]
157 logger.log_call(command)
159 logger.log("trying to redefine the VM")
160 command = [ "virsh", "define", xmlfilename ]
161 logger.log_call(command)
163 def start(self, delay=0):
164 '''Just start the sliver'''
165 logger.verbose('sliver_libvirt: {} start'.format(self.name))
167 # Check if it's running to avoid throwing an exception if the
168 # domain was already running
169 if not self.is_running():
171 # create actually means start
173 except Exception as e:
174 # XXX smbaker: attempt to resolve slivers that are stuck in
175 # "failed to allocate free veth".
176 if "ailed to allocate free veth" in str(e):
177 logger.log("failed to allocate free veth on {}".format(self.name))
179 logger.log("trying dom.create again")
184 logger.verbose('sliver_libvirt: sliver {} already started'.format(self.name))
186 # After the VM is started... we can play with the virtual interface
187 # Create the ebtables rule to mark the packets going out from the virtual
188 # interface to the actual device so the filter canmatch against the mark
189 bwlimit.ebtables("-A INPUT -i veth{} -j mark --set-mark {}"
190 .format(self.xid, self.xid))
192 ### this is confusing, because it seems it is not used in fact
194 logger.verbose('sliver_libvirt: {} stop'.format(self.name))
196 # Remove the ebtables rule before stopping
197 bwlimit.ebtables("-D INPUT -i veth{} -j mark --set-mark {}"
198 .format(self.xid, self.xid))
203 logger.log_exc("in sliver_libvirt.stop", name=self.name)
205 def is_running(self):
206 ''' Return True if the domain is running '''
207 (state, _) = self.dom.state()
208 result = (state == libvirt.VIR_DOMAIN_RUNNING)
209 logger.verbose('sliver_libvirt.is_running: {} => {}'
210 .format(self, result))
213 def configure(self, rec):
215 #sliver.[LXC/QEMU] tolower case
216 #sliver_type = rec['type'].split('.')[1].lower()
218 #BASE_DIR = '/cgroup/libvirt/{}/{}/'.format(sliver_type, self.name)
221 # No way through cgroups... figure out how to do that with user/dir quotas.
222 # There is no way to do quota per directory. Chown-ing would create
223 # problems as username namespaces are not yet implemented (and thus, host
224 # and containers share the same name ids
226 # Btrfs support quota per volumes
228 if "rspec" in rec and "tags" in rec["rspec"]:
229 if cgroups.get_cgroup_path(self.name) == None:
230 # If configure is called before start, then the cgroups won't exist
231 # yet. NM will eventually re-run configure on the next iteration.
232 # TODO: Add a post-start configure, and move this stuff there
233 logger.log("Configure: postponing tag check on {} as cgroups are not yet populated"
236 tags = rec["rspec"]["tags"]
237 # It will depend on the FS selection
238 if 'disk_max' in tags:
239 disk_max = tags['disk_max']
244 # limit to certain number
248 if 'memlock_hard' in tags:
249 mem = str(int(tags['memlock_hard']) * 1024) # hard limit in bytes
250 cgroups.write(self.name, 'memory.limit_in_bytes', mem, subsystem="memory")
251 if 'memlock_soft' in tags:
252 mem = str(int(tags['memlock_soft']) * 1024) # soft limit in bytes
253 cgroups.write(self.name, 'memory.soft_limit_in_bytes', mem, subsystem="memory")
256 # Only cpu_shares until figure out how to provide limits and guarantees
258 if 'cpu_share' in tags:
259 cpu_share = tags['cpu_share']
260 cgroups.write(self.name, 'cpu.shares', cpu_share)
262 # Call the upper configure method (ssh keys...)
263 Account.configure(self, rec)
266 def get_unique_vif():
267 return 'veth{}'.format(random.getrandbits(32))
269 # A placeholder until we get true VirtualInterface objects
271 def get_interfaces_xml(rec):
273 <interface type='network'>
274 <source network='default'/>
277 """.format(Sliver_Libvirt.get_unique_vif())
279 tags = rec['rspec']['tags']
280 if 'interface' in tags:
281 interfaces = eval(tags['interface'])
282 if not isinstance(interfaces, (list, tuple)):
283 # if interface is not a list, then make it into a singleton list
284 interfaces = [interfaces]
286 for interface in interfaces:
287 if 'vlan' in interface:
288 vlanxml = "<vlan><tag id='{}'/></vlan>".format(interface['vlan'])
291 if 'bridge' in interface:
292 tag_xml = tag_xml + """
293 <interface type='bridge'>
294 <source bridge='{}'/>
296 <virtualport type='openvswitch'/>
299 """.format(interface['bridge'], vlanxml, Sliver_Libvirt.get_unique_vif())
301 tag_xml = tag_xml + """
302 <interface type='network'>
303 <source network='default'/>
306 """.format(Sliver_Libvirt.get_unique_vif())
309 logger.log('sliver_libvirty.py: interface XML is: {}'.format(xml))
312 logger.log('sliver_libvirt.py: ERROR parsing "interface" tag for slice {}'.format(rec['name']))
313 logger.log('sliver_libvirt.py: tag value: {}'.format(tags['interface']))