16 from string import Template
19 libvirt.VIR_DOMAIN_NOSTATE: 'no state',
20 libvirt.VIR_DOMAIN_RUNNING: 'running',
21 libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
22 libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
23 libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
24 libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
25 libvirt.VIR_DOMAIN_CRASHED: 'crashed',
32 def getConnection(sliver_type):
33 # TODO: error checking
34 # vtype is of the form sliver.[LXC/QEMU] we need to lower case to lxc/qemu
35 vtype = sliver_type.split('.')[1].lower()
37 return connections.setdefault(uri, libvirt.open(uri))
40 ''' Helper method to get a "nice" output of the info struct for debug'''
41 [state, maxmem, mem, ncpu, cputime] = dom.info()
42 return '%s is %s, maxmem = %s, mem = %s, ncpu = %s, cputime = %s' % (dom.name(), STATES.get(state, state), maxmem, mem, ncpu, cputime)
46 class Sliver_Libvirt(accounts.Account):
48 def __init__(self, rec):
49 self.name = rec['name']
50 logger.verbose ('sliver_libvirt: %s init'%(self.name))
52 # Assume the directory with the image and config files
57 self.slice_id = rec['slice_id']
59 self.conn = getConnection(rec['type'])
60 self.xid = bwlimit.get_xid(self.name)
63 self.dom = self.conn.lookupByName(self.name)
65 logger.verbose('sliver_libvirt: Domain %s does not exist UNEXPECTED: %s'%(self.name, sys.exc_info()[0]))
68 def start(self, delay=0):
69 ''' Just start the sliver '''
70 logger.verbose('sliver_libvirt: %s start'%(self.name))
72 # Check if it's running to avoid throwing an exception if the
73 # domain was already running, create actually means start
74 if not self.is_running():
77 logger.verbose('sliver_libvirt: sliver %s already started'%(self.name))
79 # After the VM is started... we can play with the virtual interface
80 # Create the ebtables rule to mark the packets going out from the virtual
81 # interface to the actual device so the filter canmatch against the mark
82 bwlimit.ebtables("-A INPUT -i veth%d -j mark --set-mark %d" % \
87 logger.verbose('sliver_libvirt: %s stop'%(self.name))
89 # Remove the ebtables rule before stopping
90 bwlimit.ebtables("-D INPUT -i veth%d -j mark --set-mark %d" % \
96 logger.verbose('sliver_libvirt: Domain %s not running UNEXPECTED: %s'%(self.name, sys.exc_info()[0]))
97 print 'sliver_libvirt: Domain %s not running UNEXPECTED: %s'%(self.name, sys.exc_info()[0])
100 ''' Return True if the domain is running '''
101 logger.verbose('sliver_libvirt: %s is_running'%self.name)
103 [state, _, _, _, _] = self.dom.info()
104 if state == libvirt.VIR_DOMAIN_RUNNING:
105 logger.verbose('sliver_libvirt: %s is RUNNING'%self.name)
108 info = debuginfo(self.dom)
109 logger.verbose('sliver_libvirt: %s is NOT RUNNING...\n%s'%(self.name, info))
112 logger.verbose('sliver_libvirt: UNEXPECTED ERROR in %s...\n%s'%(self.name, sys.exc_info[0]))
113 print 'sliver_libvirt: UNEXPECTED ERROR in %s...\n%s'%(self.name, sys.exc_info[0])
115 def configure(self, rec):
117 #sliver.[LXC/QEMU] tolower case
118 #sliver_type = rec['type'].split('.')[1].lower()
120 #BASE_DIR = '/cgroup/libvirt/%s/%s/'%(sliver_type, self.name)
123 # No way through cgroups... figure out how to do that with user/dir quotas.
124 # There is no way to do quota per directory. Chown-ing would create
125 # problems as username namespaces are not yet implemented (and thus, host
126 # and containers share the same name ids
128 # Btrfs support quota per volumes
130 # It will depend on the FS selection
131 if rec.has_key('disk_max'):
132 disk_max = rec['disk_max']
137 # limit to certain number
141 if rec.has_key('memlock_hard'):
142 mem = rec['memlock_hard'] * 1024 # hard limit in bytes
143 cgroups.write(self.name, 'memory.limit_in_bytes', mem)
144 if rec.has_key('memlock_soft'):
145 mem = rec['memlock_soft'] * 1024 # soft limit in bytes
146 cgroups.write(self.name, 'memory.soft_limit_in_bytes', mem)
149 # Only cpu_shares until figure out how to provide limits and guarantees
151 if rec.has_key('cpu_share'):
152 cpu_share = rec['cpu_share']
153 cgroups.write(self.name, 'cpu.shares', cpu_share)
155 # Call the upper configure method (ssh keys...)
156 accounts.Account.configure(self, rec)