def createLV(self, vgName, lvName, size, activate=True, contiguous=False, initialTags=()): try: vg_md = self.vgmd[vgName] except KeyError: raise se.CannotCreateLogicalVolume(vgName, lvName) # Size is received as a string in MB. We need to convert it to bytes # and round it up to a multiple of the VG extent size. try: size = int(size) << 20 except ValueError: raise se.CannotCreateLogicalVolume(vgName, lvName) size = utils.round(size, int(vg_md['extent_size'])) # devices is hard to emulate properly (must have a PE allocator that # works the same as for LVM). Since we don't need this value, use None devices = None state = 'a' if activate else '-' lv_attr = dict(voltype='-', permission='w', allocations='i', fixedminor='-', state=state, devopen='-', target='-', zero='-') lv_md = dict(uuid=fake_lvm_uuid(), name=lvName, vg_name=vgName, attr=lv_attr, size=str(size), seg_start_pe='0', devices=devices, tags=initialTags, writeable=True, opened=False, active=bool(activate)) lv_count = int(vg_md['lv_count']) + 1 if (vgName, lvName) in self.lvmd: raise se.CannotCreateLogicalVolume(vgName, lvName) self.lvmd[(vgName, lvName)] = lv_md self.vgmd[vgName]['lv_count'] = str(lv_count) # Create an LV as a regular file so we have a place to write data if activate: lv_path = self.lvPath(vgName, lvName) else: lv_path = self._lvPathInactive(vgName, lvName) make_file(lv_path, size)
def createLV(self, vgName, lvName, size, activate=True, contiguous=False, initialTags=()): try: vg_md = self.vgmd[vgName] except KeyError: raise se.CannotCreateLogicalVolume(['Fake cmd'], 5, ['Fake out'], ['Fake error']) size = self._size_param_to_bytes(size) # devices is hard to emulate properly (must have a PE allocator that # works the same as for LVM). Since we don't need this value, use None devices = None state = 'a' if activate else '-' lv_attr = dict(voltype='-', permission='w', allocations='i', fixedminor='-', state=state, devopen='-', target='-', zero='-') lv_md = dict(uuid=fake_lvm_uuid(), name=lvName, vg_name=vgName, attr=lv_attr, size=str(size), seg_start_pe='0', devices=devices, tags=initialTags, writeable=True, opened=False, active=bool(activate)) lv_count = int(vg_md['lv_count']) + 1 if (vgName, lvName) in self.lvmd: raise se.CannotCreateLogicalVolume(['Fake cmd'], 5, ['Fake out'], ['Fake error']) self.lvmd[(vgName, lvName)] = lv_md self.vgmd[vgName]['lv_count'] = str(lv_count) self._create_lv_file(vgName, lvName, activate, size)
def createLV(vgName, lvName, size, activate=True, contiguous=False, initialTags=()): """ Size units: MB (1024 ** 2 = 2 ** 20)B. """ # WARNING! From man vgs: # All sizes are output in these units: (h)uman-readable, (b)ytes, # (s)ectors, (k)ilobytes, (m)egabytes, (g)igabytes, (t)erabytes, # (p)etabytes, (e)xabytes. # Capitalise to use multiples of 1000 (S.I.) instead of 1024. log.info( "Creating LV (vg=%s, lv=%s, size=%sm, activate=%s, " "contiguous=%s, initialTags=%s)", vgName, lvName, size, activate, contiguous, initialTags) cont = {True: "y", False: "n"}[contiguous] cmd = ["lvcreate"] cmd.extend(LVM_NOBACKUP) cmd.extend(("--contiguous", cont, "--size", "%sm" % size)) for tag in initialTags: cmd.extend(("--addtag", tag)) cmd.extend(("--name", lvName, vgName)) rc, out, err = _lvminfo.cmd(cmd, _lvminfo._getVGDevs((vgName, ))) if rc == 0: _lvminfo._invalidatevgs(vgName) _lvminfo._invalidatelvs(vgName, lvName) else: raise se.CannotCreateLogicalVolume(vgName, lvName, err) # TBD: Need to explore the option of running lvcreate w/o devmapper # so that if activation is not needed it would be skipped in the # first place if activate: lv_path = lvPath(vgName, lvName) st = os.stat(lv_path) uName = pwd.getpwuid(st.st_uid).pw_name gName = grp.getgrgid(st.st_gid).gr_name if ":".join((uName, gName)) != USER_GROUP: cmd = [constants.EXT_CHOWN, USER_GROUP, lv_path] if misc.execCmd(cmd, sudo=True)[0] != 0: log.warning( "Could not change ownership of one or more " "volumes in vg (%s) - %s", vgName, lvName) else: _setLVAvailability(vgName, lvName, "n")