15 from string import Template
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',
31 def getConnection(sliver_type):
32 # TODO: error checking
33 # vtype is of the form sliver.[LXC/QEMU] we need to lower case to lxc/qemu
34 vtype = sliver_type.split('.')[1].lower()
36 return connections.setdefault(uri, libvirt.open(uri))
39 ''' Helper method to get a "nice" output of the info struct for debug'''
40 [state, maxmem, mem, ncpu, cputime] = dom.info()
41 return '%s is %s, maxmem = %s, mem = %s, ncpu = %s, cputime = %s' % (dom.name(), STATES.get(state, state), maxmem, mem, ncpu, cputime)
45 class Sliver_Libvirt(accounts.Account):
47 def __init__(self, rec):
48 self.name = rec['name']
49 logger.verbose ('sliver_libvirt: %s init'%(self.name))
51 # Assume the directory with the image and config files
56 self.slice_id = rec['slice_id']
58 self.conn = getConnection(rec['type'])
59 self.xid = bwlimit.get_xid(self.name)
62 self.dom = self.conn.lookupByName(self.name)
64 logger.verbose('sliver_libvirt: Domain %s does not exist UNEXPECTED: %s'%(self.name, sys.exc_info()[0]))
67 def start(self, delay=0):
68 ''' Just start the sliver '''
69 logger.verbose('sliver_libvirt: %s start'%(self.name))
71 # Check if it's running to avoid throwing an exception if the
72 # domain was already running, create actually means start
73 if not self.is_running():
76 logger.verbose('sliver_libvirt: sliver %s already started'%(self.name))
78 # After the VM is started... we can play with the virtual interface
79 # Create the ebtables rule to mark the packets going out from the virtual
80 # interface to the actual device so the filter canmatch against the mark
81 bwlimit.ebtables("-A INPUT -i veth%d -j mark --set-mark %d" % \
85 logger.verbose('sliver_libvirt: %s stop'%(self.name))
87 # Remove the ebtables rule before stopping
88 bwlimit.ebtables("-D INPUT -i veth%d -j mark --set-mark %d" % \
94 logger.verbose('sliver_libvirt: Domain %s not running UNEXPECTED: %s'%(self.name, sys.exc_info()[0]))
95 print 'sliver_libvirt: Domain %s not running UNEXPECTED: %s'%(self.name, sys.exc_info()[0])
98 ''' Return True if the domain is running '''
99 logger.verbose('sliver_libvirt: %s is_running'%self.name)
101 [state, _, _, _, _] = self.dom.info()
102 if state == libvirt.VIR_DOMAIN_RUNNING:
103 logger.verbose('sliver_libvirt: %s is RUNNING'%self.name)
106 info = debuginfo(self.dom)
107 logger.verbose('sliver_libvirt: %s is NOT RUNNING...\n%s'%(self.name, info))
110 logger.verbose('sliver_libvirt: UNEXPECTED ERROR in %s...\n%s'%(self.name, sys.exc_info[0]))
111 print 'sliver_libvirt: UNEXPECTED ERROR in %s...\n%s'%(self.name, sys.exc_info[0])
113 def configure(self, rec):
115 #sliver.[LXC/QEMU] tolower case
116 #sliver_type = rec['type'].split('.')[1].lower()
118 #BASE_DIR = '/cgroup/libvirt/%s/%s/'%(sliver_type, self.name)
121 # No way through cgroups... figure out how to do that with user/dir quotas.
122 # There is no way to do quota per directory. Chown-ing would create
123 # problems as username namespaces are not yet implemented (and thus, host
124 # and containers share the same name ids
126 # Btrfs support quota per volumes
128 # It will depend on the FS selection
129 if rec.has_key('disk_max'):
130 disk_max = rec['disk_max']
135 # limit to certain number
139 if rec.has_key('memlock_hard'):
140 mem = rec['memlock_hard'] * 1024 # hard limit in bytes
141 cgroups.write(self.name, 'memory.limit_in_bytes', mem)
142 if rec.has_key('memlock_soft'):
143 mem = rec['memlock_soft'] * 1024 # soft limit in bytes
144 cgroups.write(self.name, 'memory.soft_limit_in_bytes', mem)
147 # Only cpu_shares until figure out how to provide limits and guarantees
149 if rec.has_key('cpu_share'):
150 cpu_share = rec['cpu_share']
151 cgroups.write(self.name, 'cpu.shares', cpu_share)
153 # Call the upper configure method (ssh keys...)
154 accounts.Account.configure(self, rec)