def hitgen(self): ''' Generate a HIP config for the given node. First check for cached config under HIP_CFG_CACHE. ''' cfgbase = os.path.expanduser(HIP_CFG_CACHE) if not os.path.exists(cfgbase): os.mkdir(cfgbase) cfgpath = os.path.join(cfgbase, self.name) print "checking for cached config in %s..." % cfgpath, if not os.path.exists(cfgpath): os.mkdir(cfgpath) myhosts = os.path.join(cfgpath, "my_host_identities.xml") if not os.path.exists(myhosts): print "generating host ID..." mutecheck_call(["./hitgen", "-noinput", "-name", self.name, "-file", myhosts]) else: print "cached." hipconf = os.path.join(cfgpath, "hip.conf") if not os.path.exists(hipconf): mutecheck_call(["./hitgen", "-file", hipconf, "-conf"]) self.mount(cfgpath, HIP_CFG_DIR) pub = os.path.join(cfgpath, "%s_host_identities.pub.xml" % self.name) if not os.path.exists(pub): self.cmd([os.path.join(self.hip_path, "hitgen"), "-publish"])
def hitgen(self): ''' Generate a HIP config for the given node. First check for cached config under HIP_CFG_CACHE. ''' cfgbase = os.path.expanduser(HIP_CFG_CACHE) if not os.path.exists(cfgbase): os.mkdir(cfgbase) cfgpath = os.path.join(cfgbase, self.name) print "checking for cached config in %s..." % cfgpath, if not os.path.exists(cfgpath): os.mkdir(cfgpath) myhosts = os.path.join(cfgpath, "my_host_identities.xml") if not os.path.exists(myhosts): print "generating host ID..." mutecheck_call( ["./hitgen", "-noinput", "-name", self.name, "-file", myhosts]) else: print "cached." hipconf = os.path.join(cfgpath, "hip.conf") if not os.path.exists(hipconf): mutecheck_call(["./hitgen", "-file", hipconf, "-conf"]) self.mount(cfgpath, HIP_CFG_DIR) pub = os.path.join(cfgpath, "%s_host_identities.pub.xml" % self.name) if not os.path.exists(pub): self.cmd([os.path.join(self.hip_path, "hitgen"), "-publish"])
def hipconfval(self, key, value): ''' Update the specified key in the hip.conf file with the given value. Copy hip.conf to hip.conf.default first. ''' hipconf = self.hipconfpath() if not os.path.exists(hipconf): mutecheck_call(["./hitgen", "-file", hipconf, "-conf"]) hipconfdef = hipconf + ".default" # make backup copy of hip.conf if it doesn't exist yet if not os.path.exists(hipconfdef): shutil.copy2(hipconf, hipconfdef) # modify the value f = open(hipconf, 'r') lines = f.readlines() f.close() f = open(hipconf, 'w') changed = False for line in lines: if line.startswith(" <%s>" % key): line = " <%s>%s</%s>\n" % (key, value, key) changed = True if line.startswith("</hip_configuration>"): if (not changed): newline = " <%s>%s</%s>\n" % (key, value, key) f.write(newline) f.write(line) f.close()
def boot(self): logger.warn("XEN PVM boot() called") self.lock.acquire() if not self.up: raise Exception("Can't boot VM without initialized disk") if self.booted: self.lock.release() return self.session.services.bootnodeservices(self) tarname = self.getconfigitem("persist_tar") if tarname: self.untarpersistent(tarname=tarname, iso=False) try: subprocess.check_call([constants.UMOUNT_BIN, self.mountdir]) self.unmount_all(self.mountdir) subprocess.check_call([UDEVADM_PATH, "settle"]) subprocess.check_call([KPARTX_PATH, "-d", self.lvpath]) # time.sleep(5) # time.sleep(1) # unpause VM logger.warn("XEN PVM boot() unpause domU %s", self.vmname) utils.mutecheck_call([XM_PATH, "unpause", self.vmname]) self.booted = True finally: self.lock.release()
def addtap(self, name): ''' Install a TAP device into the namespace for use by the HIP daemon. ''' localname = "%s.%s" % (self.name, name) try: mutecheck_call(["tunctl", "-t", localname]) except OSError, e: print "error creating TUN/TAP device '%s':\n\t%s" % (localname, e) print "make sure the 'tunctl' utility is installed" sys.exit(1)
def createlogicalvolume(self): """ Create a logical volume for this Xen domU. Called from startup(). """ if os.path.exists(self.lvpath): raise Exception, "LVM volume already exists" utils.mutecheck_call([ LVCREATE_PATH, "--size", self.disksize, "--name", self.lvname, self.vgname ])
def createfilesystems(self): """ Make an ext4 filesystem and swap space. Return the device name for the persistent partition so we can mount it. """ output = subprocess.Popen([KPARTX_PATH, "-l", self.lvpath], stdout=subprocess.PIPE).communicate()[0] lines = output.splitlines() persistdev = "/dev/mapper/" + lines[0].strip().split(" ")[0].strip() swapdev = "/dev/mapper/" + lines[1].strip().split(" ")[0].strip() subprocess.check_call([KPARTX_PATH, "-a", self.lvpath]) utils.mutecheck_call([MKFSEXT4_PATH, "-L", "persist", persistdev]) utils.mutecheck_call([MKSWAP_PATH, "-f", "-L", "swap", swapdev]) return persistdev
def createvm(self): """ Instantiate a *paused* domU VM Instantiate it now, so we can add network interfaces, pause it so we can have the filesystem open for configuration. """ args = [XM_PATH, "create", os.devnull, "--paused"] args.extend(["name=" + self.vmname, "memory=" + str(self.ramsize)]) args.append("disk=tap:aio:" + self.isofile + ",hda,r") args.append("disk=phy:" + self.lvpath + ",hdb,w") args.append("bootloader=pygrub") bootargs = "--kernel=/isolinux/vmlinuz --ramdisk=/isolinux/initrd" args.append("bootargs=" + bootargs) for action in ("poweroff", "reboot", "suspend", "crash", "halt"): args.append("on_%s=destroy" % action) args.append("extra=" + self.getconfigitem("xm_create_extra")) utils.mutecheck_call(args)
def shutdown(self): logger.warn("XEN PVM shutdown() called") if not self.up: return self.lock.acquire() try: if self.up: # sketch from SimpleLxcNode for netif in self.netifs(): netif.shutdown() try: # RJE XXX what to do here if self.booted: utils.mutecheck_call([XM_PATH, "destroy", self.vmname]) self.booted = False except (OSError, subprocess.CalledProcessError): # ignore this error too, the VM may have exited already logger.exception("error during shutdown") # discard LVM volume lvm_remove_count = 0 while os.path.exists(self.lvpath): try: subprocess.check_call([UDEVADM_PATH, "settle"]) utils.mutecall([LVCHANGE_PATH, "-an", self.lvpath]) lvm_remove_count += 1 utils.mutecall([LVREMOVE_PATH, "-f", self.lvpath]) except OSError: logger.exception("error during shutdown") if lvm_remove_count > 1: logger.warn( "XEN PVM shutdown() required %d lvremove executions.", lvm_remove_count) self._netif.clear() del self.session self.up = False finally: self.rmnodedir() self.lock.release()