def extendLV(vgName, lvName, size_mb): log.info("Extending LV %s/%s to %s megabytes", vgName, lvName, size_mb) cmd = ("lvextend", ) + LVM_NOBACKUP cmd += ("--size", "%sm" % (size_mb, ), "%s/%s" % (vgName, lvName)) rc, out, err = _lvminfo.cmd(cmd, _lvminfo._getVGDevs((vgName, ))) if rc != 0: # Since this runs only on the SPM, assume that cached vg and lv # metadata is correct. vg = getVG(vgName) lv = getLV(vgName, lvName) # Convert sizes to extents to match lvm behavior. extent_size = int(vg.extent_size) lv_extents = int(lv.size) // extent_size requested_size = size_mb * constants.MEGAB requested_extents = (requested_size + extent_size - 1) // extent_size if lv_extents >= requested_extents: log.debug("LV %s/%s already extended (extents=%d, requested=%d)", vgName, lvName, lv_extents, requested_extents) return needed_extents = requested_extents - lv_extents free_extents = int(vg.free_count) if free_extents < needed_extents: raise se.VolumeGroupSizeError( "Not enough free extents for extending LV %s/%s (free=%d, " "needed=%d)" % (vgName, lvName, free_extents, needed_extents)) raise se.LogicalVolumeExtendError(vgName, lvName, "%sm" % (size_mb, )) _lvminfo._invalidatevgs(vgName) _lvminfo._invalidatelvs(vgName, lvName)
def _resizeLV(op, vgName, lvName, size): """ 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. cmd = (op, ) + LVM_NOBACKUP cmd += ("--size", "%sm" % (size, ), "%s/%s" % (vgName, lvName)) rc, out, err = _lvminfo.cmd(cmd, _lvminfo._getVGDevs((vgName, ))) if rc == 0: _lvminfo._invalidatevgs(vgName) _lvminfo._invalidatelvs(vgName, lvName) elif rc == 3: # In LVM we trust. Hope that 3 is only for this. log.debug("New size (in extents) matches existing size (in extents).") elif rc != 0: # get the free extents size # YaRC vg = getVG(vgName) free_size = int(vg.extent_size) * int(vg.free_count) # in B if free_size < int(size) * constants.MEGAB: raise se.VolumeGroupSizeError( "%s/%s %d > %d (MiB)" % (vgName, lvName, int(size), free_size / constants.MEGAB)) raise se.LogicalVolumeExtendError(vgName, lvName, "%sM" % (size, ))