10 from string import Template
16 class Sliver_LXC(sliver_libvirt.Sliver_Libvirt):
17 """This class wraps LXC commands"""
21 # Need to add a tag at myplc to actually use this account
24 REF_IMG_BASE_DIR = '/vservers/.lvref'
25 CON_BASE_DIR = '/vservers'
28 def create(name, rec=None):
29 ''' Create dirs, copy fs image, lxc_create '''
30 logger.verbose ('sliver_lxc: %s create'%(name))
31 conn = sliver_libvirt.getConnection(Sliver_LXC.TYPE)
33 # Get the type of image from vref myplc tags specified as:
39 logger.log('sliver_libvirt: %s: WARNING - no vref attached defaults to lxc-f14' % (name))
40 vref = "lxc-f14-x86_64"
42 refImgDir = os.path.join(Sliver_LXC.REF_IMG_BASE_DIR, vref)
43 containerDir = os.path.join(Sliver_LXC.CON_BASE_DIR, name)
45 # check the template exists -- there's probably a better way..
46 if not os.path.isdir(refImgDir):
47 logger.log('sliver_lxc: %s: ERROR Could not create sliver - reference image %s not found' % (name,vref))
48 logger.log('sliver_lxc: %s: ERROR Expected reference image in %s'%(name,refImgDir))
51 # Snapshot the reference image fs (assume the reference image is in its own
53 command = ['btrfs', 'subvolume', 'snapshot', refImgDir, containerDir]
54 if not logger.log_call(command, timeout=15*60):
55 logger.log('sliver_lxc: ERROR Could not create BTRFS snapshot at', containDir)
57 command = ['chmod', '755', containerDir]
58 logger.log_call(command, timeout=15*60)
60 # customize prompt for slice owner
61 dot_profile=os.path.join(containerDir,"root/.profile")
62 with open(dot_profile,'w') as f:
63 f.write("export PS1='%s@\H \$ '\n"%(name))
67 # Set hostname. A valid hostname cannot have '_'
68 #with open(os.path.join(containerDir, 'etc/hostname'), 'w') as f:
69 # print >>f, name.replace('_', '-')
71 # Add slices group if not already present
73 group = grp.getgrnam('slices')
75 command = ['/usr/sbin/groupadd', 'slices']
76 logger.log_call(command, timeout=15*60)
78 # Add unix account (TYPE is specified in the subclass)
79 command = ['/usr/sbin/useradd', '-g', 'slices', '-s', Sliver_LXC.SHELL, name, '-p', '*']
80 logger.log_call(command, timeout=15*60)
81 command = ['mkdir', '/home/%s/.ssh'%name]
82 logger.log_call(command, timeout=15*60)
84 # Create PK pair keys to connect from the host to the guest without
85 # password... maybe remove the need for authentication inside the
87 command = ['su', '-s', '/bin/bash', '-c', 'ssh-keygen -t rsa -N "" -f /home/%s/.ssh/id_rsa'%(name)]
88 logger.log_call(command, timeout=15*60)
90 command = ['chown', '-R', '%s.slices'%name, '/home/%s/.ssh'%name]
91 logger.log_call(command, timeout=15*60)
93 command = ['mkdir', '%s/root/.ssh'%containerDir]
94 logger.log_call(command, timeout=15*60)
96 command = ['cp', '/home/%s/.ssh/id_rsa.pub'%name, '%s/root/.ssh/authorized_keys'%containerDir]
97 logger.log_call(command, timeout=15*60)
99 # Lookup for xid and create template after the user is created so we
100 # can get the correct xid based on the name of the slice
101 xid = bwlimit.get_xid(name)
103 # Template for libvirt sliver configuration
104 template_filename_sliceimage = os.path.join(Sliver_LXC.REF_IMG_BASE_DIR,'lxc_template.xml')
105 if os.path.isfile (template_filename_sliceimage):
106 logger.log("WARNING: using compat template %s"%template_filename_sliceimage)
107 template_filename=template_filename_sliceimage
109 logger.log("Cannot find XML template %s"%template_filename_sliceimage)
112 with open(template_filename) as f:
113 template = Template(f.read())
114 xml = template.substitute(name=name, xid=xid)
116 logger.log('Failed to parse or use XML template file %s'%template_filename)
119 # Lookup for the sliver before actually
120 # defining it, just in case it was already defined.
122 dom = conn.lookupByName(name)
124 dom = conn.defineXML(xml)
125 logger.verbose('lxc_create: %s -> %s'%(name, sliver_libvirt.debuginfo(dom)))
130 logger.verbose ('sliver_lxc: %s destroy'%(name))
131 conn = sliver_libvirt.getConnection(Sliver_LXC.TYPE)
133 containerDir = Sliver_LXC.CON_BASE_DIR + '/%s'%(name)
136 # Destroy libvirt domain
137 dom = conn.lookupByName(name)
139 logger.verbose('sliver_lxc: Domain %s does not exist!' % name)
144 logger.verbose('sliver_lxc: Domain %s not running... continuing.' % name)
149 logger.verbose('sliver_lxc: Domain %s is not defined... continuing.' % name)
151 # Remove user after destroy domain to force logout
152 command = ['/usr/sbin/userdel', '-f', '-r', name]
153 logger.log_call(command, timeout=15*60)
155 # Remove rootfs of destroyed domain
156 command = ['btrfs', 'subvolume', 'delete', containerDir]
157 logger.log_call(command, timeout=15*60)
159 logger.verbose('sliver_libvirt: %s destroyed.'%name)