17 <memory>32768</memory>
24 <on_poweroff>destroy</on_poweroff>
25 <on_reboot>restart</on_reboot>
26 <on_crash>destroy</on_crash>
28 <emulator>/usr/libexec/libvirt_lxc</emulator>
29 <filesystem type='mount'>
30 <source dir='/vservers/test_1/rootfs/'/>
33 <interface type='network'>
34 <source network='default'/>
36 <console type='pty' />
42 def createConnection():
43 conn = libvirt.open('lxc:///')
45 print 'Failed to open connection to LXC hypervisor'
51 libvirt.VIR_DOMAIN_NOSTATE: 'no state',
52 libvirt.VIR_DOMAIN_RUNNING: 'running',
53 libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
54 libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
55 libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
56 libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
57 libvirt.VIR_DOMAIN_CRASHED: 'crashed',
61 [state, maxmem, mem, ncpu, cputime] = dom.info()
62 return '%s is %s,\nmaxmem = %s, mem = %s, ncpu = %s, cputime = %s' % (dom.name(), states.get(state, state), maxmem, mem, ncpu, cputime)
64 class Sliver_LXC(accounts.Account):
65 """This class wraps LXC commands"""
68 # Using /bin/bash triggers destroy root/site_admin (?!?)
70 # Need to add a tag at myplc to actually use this account
73 def __init__(self, rec):
74 self.name = rec['name']
75 print "LXC __init__ %s"%(self.name)
76 logger.verbose ('sliver_lxc: %s init'%self.name)
78 self.dir = '/vservers/%s'%(self.name)
80 # Assume the directory with the image and config files
83 self.config = '%s/config'%(self.dir)
84 self.fstab = '%s/fstab'%(self.dir)
85 self.lxc_log = '%s/lxc.log'%(self.dir)
88 self.slice_id = rec['slice_id']
89 self.disk_usage_initialized = False
92 self.connection = createConnection()
95 def create(name, rec = None):
96 ''' Create dirs, copy fs image, lxc_create '''
97 print "LXC create %s"%(name)
98 logger.verbose ('sliver_lxc: %s create'%name)
99 dir = '/vservers/%s'%(name)
100 config = '%s/config'%(dir)
101 lxc_log = '%s/lxc.log'%(dir)
103 if not (os.path.isdir(dir) and
104 os.access(dir, os.R_OK | os.W_OK | os.X_OK)):
105 print 'lxc_create: directory %s does not exist or wrong perms'%(dir)
107 # Assume for now that the directory is there and with a FS
110 command += ['/bin/bash','-x',]
111 command += ['/usr/bin/lxc-create', '-n', name, '-f', config, '&']
113 #subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=False)
114 conn = createConnection()
116 dom0 = conn.lookupByName(name)
118 dom0 = conn.defineXML(test_template())
124 print "LXC destroy %s"%(name)
125 dir = '/vservers/%s'%(name)
126 lxc_log = '%s/lxc.log'%(dir)
128 command += ['/usr/bin/lxc-destroy', '-n', name]
130 subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=False)
131 print "LXC destroy DONE"
133 def configure(self, rec):
134 print "LXC configure %s"%(self.name)
136 def start(self, delay=0):
137 ''' Check existence? lxc_start '''
138 print "LXC start %s"%(self.name)
140 command += ['/usr/bin/lxc-start', '-n', self.name, '-d']
142 subprocess.call(command, stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=False)
146 print "LXC stop %s"%(self.name)
148 def is_running(self):
149 print "LXC is_running %s"%(self.name)
151 command += ['/usr/bin/lxc-info -n %s'%(self.name)]
153 p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
154 state = p.communicate()[0].split(' ')[2]
156 if state == 'RUNNING': return True