def __untarimage(self, imagename, destDir): logging.info("Untar image") untarimage = "sudo tar -zxvf " + imagename + " -C " + destDir try: execute.osExecute(untarimage) except: logging.info('Failed to uncompress image') raise
def __removeMountpoint(self, mountpoint): logging.info('removing mount point') unmountlvm = "sudo umount " + mountpoint try: os.chdir("/") execute.osExecute(unmountlvm) os.rmdir(mountpoint) except: logging.info('Failed to unmount image/delete mount point') raise
def __startVirtualMachine(self, machinename, configFile): logging.info('starting the virtual machine') newConfigFile = "/etc/xen/auto/vnode_" + machinename + ".cfg" moveConfigFile = "sudo mv " + configFile + " " + newConfigFile startup = "sudo xm create -f " + newConfigFile try: execute.osExecute(moveConfigFile) execute.osExecute(startup) except: logging.info('cannot start the domain with config file: ' + configFile) raise
def __createMountpoint(self, machinename, mountpoint): logging.info('creating mount point') mountlvm = "sudo mount /dev/" + self.__vgName + "/xen-root-" + machinename + " " + mountpoint try: os.mkdir(mountpoint) execute.osExecute(mountlvm) os.chdir(mountpoint) except: logging.info('Failed to mount vm root partition') raise
def __saveImage(self, machinename, imagename): tempMountpoint = "/tmp/vnode_" + machinename tarimage = "sudo tar -cszvf " + imagename + " -C " + tempMountpoint + " ." try: self.__createMountpoint(machinename, tempMountpoint) execute.osExecute(tarimage) except: logging.info('failed in saving the image -> ' + imagename) self.__removeMountpoint(tempMountpoint) raise self.__removeMountpoint(tempMountpoint)
def __removeLVMPartition(self, machinename): rootPartName = "xen-root-" + machinename swapPartName = "xen-swap-" + machinename lvrootremove = "sudo lvremove -f /dev/" + self.__vgName + "/" + rootPartName lvswapremove = "sudo lvremove -f /dev/" + self.__vgName + "/" + swapPartName try: try: if self.__checkPartitionExists(rootPartName): execute.osExecute(lvrootremove) except: logging.info('LVM Root Partition /dev/' + self.__vgName + '/xen-root-' + machinename + ' is NOT removed') raise finally: try: if self.__checkPartitionExists(swapPartName): execute.osExecute(lvswapremove) except: logging.info('LVM Swap Partition /dev/' + self.__vgName + '/xen-swap-' + machinename + ' is NOT removed') raise
def __createLVMPartition(self, machinename, partitionSize, mkfsCommand, imageType): try: swapSize = self.__config.getVMSwapSize(imageType) lvrootcreate = "sudo lvcreate --size " + partitionSize + "G --name xen-root-" + machinename + " " + self.__vgName lvswapcreate = "sudo lvcreate --size " + swapSize + "M --name xen-swap-" + machinename + " " + self.__vgName makeswap = "sudo mkswap /dev/" + self.__vgName + "/xen-swap-" + machinename filesystem = "sudo " + mkfsCommand + " /dev/" + self.__vgName + "/xen-root-" + machinename execute.osExecute(lvrootcreate) execute.osExecute(lvswapcreate) execute.osExecute(filesystem) execute.osExecute(makeswap) except: logging.info("Failed to create LVM partition") raise
def __untarimage(self, imagename, destDir): logging.info("Untar image") untarimage = "sudo tar -zxvf " + imagename + " -C " + destDir try: execute.osExecute(untarimage) except: logging.info('Failed to uncompress image' + imagename) raise """ optionally - untar configuration files (if archive exists) (just to have additional small archive with configs to modify) """ # config filename for image: imagename.tar.gz -> imagename-config.tar.gz configFileName = re.sub (".tar.gz$", "-config.tar.gz", imagename ) if os.path.isfile(configFileName): logging.info("Untar config") untarConfig = "sudo tar -zxvf " + configFileName + " -C " + destDir try: execute.osExecute(untarConfig) except: logging.info('Failed to uncompress config ' + configFileName) raise
def __shutdownVirtualMachine(self, machinename): logging.info('shutting down virtual machine') configFile = "/etc/xen/auto/vnode_" + machinename + ".cfg" removeConfigFile = "sudo rm -f " + configFile try: #domainId = self.__getDomainId(machinename) #if domainId != -1: # shutdown = "sudo xm destroy " + str(domainId) shutdown = "sudo xm destroy " + machinename execute.osExecute(shutdown) except: logging.info('cannot shutdown "' + machinename + '"') execute.osExecute(removeConfigFile) raise execute.osExecute(removeConfigFile)