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',
28 libvirt.VIR_CONNECT_CLOSE_REASON_ERROR: 'Misc I/O error',
29 libvirt.VIR_CONNECT_CLOSE_REASON_EOF: 'End-of-file from server',
30 libvirt.VIR_CONNECT_CLOSE_REASON_KEEPALIVE: 'Keepalive timer triggered',
31 libvirt.VIR_CONNECT_CLOSE_REASON_CLIENT: 'Client requested it',
38 class Sliver_Libvirt(Account):
43 def getConnection(sliver_type):
44 # TODO: error checking
45 # vtype is of the form sliver.[LXC/QEMU] we need to lower case to lxc/qemu
46 vtype = sliver_type.split('.')[1].lower()
48 return connections.setdefault(uri, libvirt.open(uri))
50 def __init__(self, rec):
51 self.name = rec['name']
52 logger.verbose ('sliver_libvirt: %s init'%(self.name))
54 # Assume the directory with the image and config files
59 self.slice_id = rec['slice_id']
61 self.conn = Sliver_Libvirt.getConnection(rec['type'])
62 self.xid = bwlimit.get_xid(self.name)
66 dom = self.conn.lookupByName(self.name)
68 logger.log('sliver_libvirt: Domain %s does not exist. ' \
69 'Will try to create it again.' % (self.name))
70 self.__class__.create(rec['name'], rec)
71 dom = self.conn.lookupByName(self.name)
75 def dom_details (dom):
77 output += " id=%s - OSType=%s"%(dom.ID(),dom.OSType())
78 # calling state() seems to be working fine
79 (state,reason)=dom.state()
80 output += " state=%s, reason=%s"%(STATES.get(state,state),REASONS.get(reason,reason))
82 # try to use info() - this however does not work for some reason on f20
83 # info cannot get info operation failed: Cannot read cputime for domain
84 [state, maxmem, mem, ncpu, cputime] = dom.info()
85 output += " [info: maxmem = %s, mem = %s, ncpu = %s, cputime = %s]" % (STATES.get(state, state), maxmem, mem, ncpu, cputime)
87 # too bad but libvirt.py prints out stuff on stdout when this fails, don't know how to get rid of that..
88 output += " [info: not available]"
92 ''' Helper method to get a "nice" output of the domain struct for debug purposes'''
93 output="Domain %s"%self.name
96 output += " [no attached dom ?!?]"
98 output += Sliver_Libvirt.dom_details (dom)
101 # Thierry : I am not quite sure if /etc/libvirt/lxc/<>.xml holds a reliably up-to-date
102 # copy of the sliver XML config; I feel like issuing a virsh dumpxml first might be safer
103 def repair_veth(self):
104 # See workaround email, 2-14-2014, "libvirt 1.2.1 rollout"
105 xml = open("/etc/libvirt/lxc/%s.xml" % self.name).read()
106 veths = re.findall("<target dev='veth[0-9]*'/>", xml)
107 veths = [x[13:-3] for x in veths]
109 command = ["ip", "link", "delete", veth]
110 logger.log_call(command)
112 logger.log("trying to redefine the VM")
113 command = ["virsh", "define", "/etc/libvirt/lxc/%s.xml" % self.name]
114 logger.log_call(command)
116 def start(self, delay=0):
117 ''' Just start the sliver '''
118 logger.verbose('sliver_libvirt: %s start'%(self.name))
120 # Check if it's running to avoid throwing an exception if the
121 # domain was already running, create actually means start
122 if not self.is_running():
126 # XXX smbaker: attempt to resolve slivers that are stuck in
127 # "failed to allocate free veth".
128 if "ailed to allocate free veth" in str(e):
129 logger.log("failed to allocate free veth on %s" % self.name)
131 logger.log("trying dom.create again")
136 logger.verbose('sliver_libvirt: sliver %s already started'%(self.name))
138 # After the VM is started... we can play with the virtual interface
139 # Create the ebtables rule to mark the packets going out from the virtual
140 # interface to the actual device so the filter canmatch against the mark
141 bwlimit.ebtables("-A INPUT -i veth%d -j mark --set-mark %d" % \
142 (self.xid, self.xid))
145 logger.verbose('sliver_libvirt: %s stop'%(self.name))
147 # Remove the ebtables rule before stopping
148 bwlimit.ebtables("-D INPUT -i veth%d -j mark --set-mark %d" % \
149 (self.xid, self.xid))
154 logger.log_exc("in sliver_libvirt.stop",name=self.name)
156 def is_running(self):
157 ''' Return True if the domain is running '''
158 (state,_) = self.dom.state()
159 result = (state == libvirt.VIR_DOMAIN_RUNNING)
160 logger.verbose('sliver_libvirt.is_running: %s => %s'%(self,result))
163 def configure(self, rec):
165 #sliver.[LXC/QEMU] tolower case
166 #sliver_type = rec['type'].split('.')[1].lower()
168 #BASE_DIR = '/cgroup/libvirt/%s/%s/'%(sliver_type, self.name)
171 # No way through cgroups... figure out how to do that with user/dir quotas.
172 # There is no way to do quota per directory. Chown-ing would create
173 # problems as username namespaces are not yet implemented (and thus, host
174 # and containers share the same name ids
176 # Btrfs support quota per volumes
178 if rec.has_key("rspec") and rec["rspec"].has_key("tags"):
179 if cgroups.get_cgroup_path(self.name) == None:
180 # If configure is called before start, then the cgroups won't exist
181 # yet. NM will eventually re-run configure on the next iteration.
182 # TODO: Add a post-start configure, and move this stuff there
183 logger.log("Configure: postponing tag check on %s as cgroups are not yet populated" % self.name)
185 tags = rec["rspec"]["tags"]
186 # It will depend on the FS selection
187 if tags.has_key('disk_max'):
188 disk_max = tags['disk_max']
193 # limit to certain number
197 if tags.has_key('memlock_hard'):
198 mem = str(int(tags['memlock_hard']) * 1024) # hard limit in bytes
199 cgroups.write(self.name, 'memory.limit_in_bytes', mem, subsystem="memory")
200 if tags.has_key('memlock_soft'):
201 mem = str(int(tags['memlock_soft']) * 1024) # soft limit in bytes
202 cgroups.write(self.name, 'memory.soft_limit_in_bytes', mem, subsystem="memory")
205 # Only cpu_shares until figure out how to provide limits and guarantees
207 if tags.has_key('cpu_share'):
208 cpu_share = tags['cpu_share']
209 cgroups.write(self.name, 'cpu.shares', cpu_share)
211 # Call the upper configure method (ssh keys...)
212 Account.configure(self, rec)
215 def get_unique_vif():
216 return 'veth%s' % random.getrandbits(32)
218 # A placeholder until we get true VirtualInterface objects
220 def get_interfaces_xml(rec):
222 <interface type='network'>
223 <source network='default'/>
226 """ % (Sliver_Libvirt.get_unique_vif())
228 tags = rec['rspec']['tags']
229 if 'interface' in tags:
230 interfaces = eval(tags['interface'])
231 if not isinstance(interfaces, (list, tuple)):
232 # if interface is not a list, then make it into a singleton list
233 interfaces = [interfaces]
235 for interface in interfaces:
236 if 'vlan' in interface:
237 vlanxml = "<vlan><tag id='%s'/></vlan>" % interface['vlan']
240 if 'bridge' in interface:
241 tag_xml = tag_xml + """
242 <interface type='bridge'>
243 <source bridge='%s'/>
245 <virtualport type='openvswitch'/>
248 """ % (interface['bridge'], vlanxml, Sliver_Libvirt.get_unique_vif())
250 tag_xml = tag_xml + """
251 <interface type='network'>
252 <source network='default'/>
255 """ % (Sliver_Libvirt.get_unique_vif())
258 logger.log('sliver_libvirty.py: interface XML is: %s' % xml)
261 logger.log('sliver_libvirt.py: ERROR parsing "interface" tag for slice %s' % rec['name'])
262 logger.log('sliver_libvirt.py: tag value: %s' % tags['interface'])