def __create_storage(self): com1 = "lvcreate -L %s -n %s --addtag %s %s" % ( self.size, self.vm_name, self.vm_name, self.settings.get_item("driver_vserver","volume_group")) com2 = "mkfs.%s /dev/%s/%s" % ( self.settings.get_item("resource","default_storage_filesystem"), self.settings.get_item("driver_vserver","volume_group"), self.vm_name ) com3 = "mkdir -p /tmp/pravega/volume/%s" % self.vm_name com4 = "mount /dev/%s/%s /tmp/pravega/volume/%s" % ( self.settings.get_item("driver_vserver","volume_group"), self.vm_name, self.vm_name ) execute_command(com1) execute_command(com2) execute_command(com3) execute_command(com4) self.__prepare_fstab()
def __check_all_ip(self): ip_list = [] for vm in glob.glob("%s/../*" % self.conf_base): suff = execute_command("cat %s/interfaces/0/ip | cut -d '.' -f4" % vm ) ip_list.append(suff) return ip_list
def __all_volume_group(self): command = "vgdisplay -C --units b --noheadings --aligned \ --separator '|' -o vg_name,vg_size,vg_free" vgs = execute_command(command) pv_list = self.all_pv result = {} mb = 1024*1024 for vg in vgs.split('\n'): if vg : data = vg.strip().split('|') total = int(data[1].split("B")[0]) free = int(data[2].split("B")[0]) used = total - free pvs = [] for pv in pv_list : if pv_list.get(pv).get('volume_group') == data[0] : pvs.append(pv) result[data[0]] = { 'total_space' : total / mb , 'free_space' : free / mb, 'used_space' : used / mb, 'pv_list' : pvs } return result
def __prepare_fstab(self): lvs = "/dev/%s/%s" % ( self.settings.get_item("driver_vserver","volume_group"), self.vm_name) mnt = os.path.join(self.repo_base, self.vm_name) lv = execute_command("cat /etc/fstab | grep -c %s " % lvs) if lv != None : pass else : execute_command("echo '%s %s ext4 defaults 1 1' >> /etc/fstab" % (lvs,mnt)) cmd = "echo 'aufs-%s %s aufs br:%s=rw:%s=ro 1' 1 >> /etc/fstab" % ( self.vm_name, mnt, mnt, self.buildroot ) execute_command(cmd)
def __all_disk(self): tank_list = {} raw_comm = "cat /proc/partitions | grep -E -w '[s|h]d[a-z]' | awk '{ print $4 , $3}'" comm = execute_command(raw_comm) for tank in comm.strip().split("\n"): content = tank.split() tank_list[content[0]] = { "size" : int(content[1]) / 1024 } return tank_list
def __all_partition(self): part_list = {} for disk in self.__all_disk(): raw_comm = "fdisk -l /dev/sda | grep -E '/dev/sda[1-9]' | awk '{ print $1, $4, $6}'" comm = execute_command(raw_comm) part = {} for p in comm.strip().split("\n"): content = p.split() part[content[0].split('/dev/')[1]] = { "size" : int(content[1].replace('+','0')) / 1024, "system" : content[2] } part_list[disk] = part return part_list
def __all_logical_volume(self): command = "lvdisplay -C --aligned --units b --separator '|' \ --noheadings -o lv_name,vg_name,lv_size,lv_path" pvs = execute_command(command) result = {} mb = 1024*1024 for pv in pvs.split('\n'): if pv : data = pv.strip().split('|') total = int(data[2].split("B")[0]) result[data[0]] = { 'total_space' : total / mb , 'volume_group' : data[1], 'device_path' : data[3] } return result
def __all_physical_volume(self): command = "pvdisplay -C --units b --aligned --separator '|' \ --noheadings -o pv_name,pv_size,pv_used,pv_free,vg_name" pvs = execute_command(command) result = {} mb = 1024*1024 for pv in pvs.split('\n'): if pv : data = pv.strip().split('|') total = int(data[1].split("B")[0]) used = int(data[2].split("B")[0]) free = int(data[3].split("B")[0]) result[data[0].strip()] = { 'total_space' : total / mb , 'used_space' : used / mb, 'free_space' : free / mb, 'volume_group' : data[4] } return result
def __create_vm(self): conf_dir = os.path.join(self.conf_base,self.vm_name) home = os.path.join(self.repo_base, self.vm_name) interface = "dummy0:%s/%s" % (self.ip, self.settings.get_item( "driver_vserver","network_netmask_cidr")) init_command = """ vserver %s build -m skeleton \ --hostname %s \ --interface %s \ --flags lock,virt_mem,virt_uptime,virt_cpu,virt_load,hide_netif\ --initstyle sysv """ % (self.vm_name,self.vm_name,interface) execute_command(init_command) execute_command("rsync -arv %s /tmp/pravega/volume/%s" % (home, self.vm_name)) execute_command("umount /tmp/pravega/volume/%s" % self.vm_name) execute_command("rm -rf %s/*" % home) execute_command("mount /dev/%s/%s" % ( self.settings.get_item("driver_vserver","volume_group"), self.vm_name, )) execute_command("mount aufs-%s" % self.vm_name) os.mkdir(os.path.join(self.conf_base,'cgroup')) mem = open(os.path.join(self.conf_base,'cgroup/memory.limit_in_bytes'),'w') mem.write(""+str(self.memory)+"M\n") mem.close() swap = open(os.path.join(self.conf_base,'cgroup/memory.memsw.limit_in_bytes'),'w') swap.write(""+str(int(self.memory)+2*int(self.memory))+"M\n") swap.close() old_password = execute_command("openssl passwd "+self.passwd+"").strip() execute_command("chroot "+home+" /usr/sbin/usermod -p \""+old_password+"\" root")
def make_physical_volume(self,block): if "/dev/" + block in self.all_pv : return False else : execute_command("pvcreate /dev/%s" % block) return True