Beispiel #1
0
def build_snapshot(config, logical_volume, suppress_tmpdir=False):
    """Create a snapshot process for running through the various steps
    of creating, mounting, unmounting and removing a snapshot
    """
    snapshot_name = config['snapshot-name'] or \
                    logical_volume.lv_name + '_snapshot'
    extent_size = int(logical_volume.vg_extent_size)
    snapshot_size = config['snapshot-size']
    if not snapshot_size:
        snapshot_size = min(
            int(logical_volume.vg_free_count),
            (int(logical_volume.lv_size) * 0.2) / extent_size,
            (15 * 1024**3) / extent_size,
        )
        LOG.info("Auto-sizing snapshot-size to %s (%d extents)",
                 format_bytes(snapshot_size * extent_size), snapshot_size)
        if snapshot_size < 1:
            raise BackupError(
                "Insufficient free extents on %s "
                "to create snapshot (free extents = %s)" %
                (logical_volume.device_name(), logical_volume.vg_free_count))
    else:
        try:
            _snapshot_size = snapshot_size
            snapshot_size = parse_bytes(snapshot_size) / extent_size
            LOG.info(
                "Using requested snapshot-size %s "
                "rounded by extent-size %s to %s.", _snapshot_size,
                format_bytes(extent_size),
                format_bytes(snapshot_size * extent_size))
            if snapshot_size < 1:
                raise BackupError("Requested snapshot-size (%s) is "
                                  "less than 1 extent" % _snapshot_size)
            if snapshot_size > int(logical_volume.vg_free_count):
                LOG.info(
                    "Snapshot size requested %s, but only %s available.",
                    config['snapshot-size'],
                    format_bytes(int(logical_volume.vg_free_count) *
                                 extent_size,
                                 precision=4))
                LOG.info(
                    "Truncating snapshot-size to %d extents (%s)",
                    int(logical_volume.vg_free_count),
                    format_bytes(int(logical_volume.vg_free_count) *
                                 extent_size,
                                 precision=4))
                snapshot_size = int(logical_volume.vg_free_count)
        except ValueError, exc:
            raise BackupError("Problem parsing snapshot-size %s" % exc)
Beispiel #2
0
def build_snapshot(config, logical_volume):
    """Create a snapshot process for running through the various steps
    of creating, mounting, unmounting and removing a snapshot
    """
    snapshot_name = config["snapshot-name"] or logical_volume.lv_name + "_snapshot"
    extent_size = int(logical_volume.vg_extent_size)
    snapshot_size = config["snapshot-size"]
    if not snapshot_size:
        snapshot_size = min(
            int(logical_volume.vg_free_count),
            (int(logical_volume.lv_size) * 0.2) / extent_size,
            (15 * 1024 ** 3) / extent_size,
        )
        LOG.info(
            "Auto-sizing snapshot-size to %s (%d extents)", format_bytes(snapshot_size * extent_size), snapshot_size
        )
        if snapshot_size < 1:
            raise BackupError(
                "Insufficient free extents on %s "
                "to create snapshot (free extents = %s)" % (logical_volume.device_name(), logical_volume.vg_free_count)
            )
    else:
        try:
            _snapshot_size = snapshot_size
            snapshot_size = parse_bytes(snapshot_size) / extent_size
            LOG.info(
                "Using requested snapshot-size %s " "rounded by extent-size %s to %s.",
                _snapshot_size,
                format_bytes(extent_size),
                format_bytes(snapshot_size * extent_size),
            )
            if snapshot_size < 1:
                raise BackupError("Requested snapshot-size (%s) is " "less than 1 extent" % _snapshot_size)
            if snapshot_size > int(logical_volume.vg_free_count):
                LOG.info(
                    "Snapshot size requested %s, but only %s available.",
                    config["snapshot-size"],
                    format_bytes(int(logical_volume.vg_free_count) * extent_size, precision=4),
                )
                LOG.info(
                    "Truncating snapshot-size to %d extents (%s)",
                    int(logical_volume.vg_free_count),
                    format_bytes(int(logical_volume.vg_free_count) * extent_size, precision=4),
                )
                snapshot_size = int(logical_volume.vg_free_count)
        except ValueError, exc:
            raise BackupError("Problem parsing snapshot-size %s" % exc)
Beispiel #3
0
def build_snapshot(config, logical_volume, suppress_tmpdir=False):
    """Create a snapshot process for running through the various steps
    of creating, mounting, unmounting and removing a snapshot
    """
    snapshot_name = config['snapshot-name'] or \
                    logical_volume.lv_name + '_snapshot'
    extent_size = int(logical_volume.vg_extent_size)
    snapshot_size = config['snapshot-size']
    if not snapshot_size:
        snapshot_size = min(
            int(logical_volume.vg_free_count),
            (int(logical_volume.lv_size) * 0.2) / extent_size,
            (15 * 1024**3) / extent_size,
        )
        LOG.info("Auto-sizing snapshot-size to %s (%d extents)",
                 format_bytes(snapshot_size * extent_size), snapshot_size)
        if snapshot_size < 1:
            raise BackupError(
                "Insufficient free extents on %s "
                "to create snapshot (free extents = %s)" %
                (logical_volume.device_name(), logical_volume.vg_free_count))
    else:
        try:
            _snapshot_size = snapshot_size
            snapshot_size = parse_bytes(snapshot_size) / extent_size
            LOG.info(
                "Using requested snapshot-size %s "
                "rounded by extent-size %s to %s.", _snapshot_size,
                format_bytes(extent_size),
                format_bytes(snapshot_size * extent_size))
            if snapshot_size < 1:
                raise BackupError("Requested snapshot-size (%s) is "
                                  "less than 1 extent" % _snapshot_size)
            if snapshot_size > int(logical_volume.vg_free_count):
                LOG.info(
                    "Snapshot size requested %s, but only %s available.",
                    config['snapshot-size'],
                    format_bytes(int(logical_volume.vg_free_count) *
                                 extent_size,
                                 precision=4))
                LOG.info(
                    "Truncating snapshot-size to %d extents (%s)",
                    int(logical_volume.vg_free_count),
                    format_bytes(int(logical_volume.vg_free_count) *
                                 extent_size,
                                 precision=4))
                snapshot_size = int(logical_volume.vg_free_count)
        except ValueError as exc:
            raise BackupError("Problem parsing snapshot-size %s" % exc)

    mountpoint = config['snapshot-mountpoint']
    tempdir = False
    if not mountpoint:
        tempdir = True
        if not suppress_tmpdir:
            mountpoint = tempfile.mkdtemp()
    else:
        try:
            os.makedirs(mountpoint)
            LOG.info("Created mountpoint %s", mountpoint)
        except OSError as exc:
            # silently ignore if the mountpoint already exists
            if exc.errno != errno.EEXIST:
                raise BackupError("Failure creating snapshot mountpoint: %s" %
                                  str(exc))
    snapshot = Snapshot(snapshot_name, int(snapshot_size), mountpoint)
    if tempdir:
        snapshot.register('finish',
                          lambda *args, **kwargs: cleanup_tempdir(mountpoint))
    return snapshot
Beispiel #4
0
def build_snapshot(config, logical_volume, suppress_tmpdir=False):
    """Create a snapshot process for running through the various steps
    of creating, mounting, unmounting and removing a snapshot
    """
    snapshot_name = config["snapshot-name"] or logical_volume.lv_name + "_snapshot"
    extent_size = int(logical_volume.vg_extent_size)
    snapshot_size = config["snapshot-size"]
    if not snapshot_size:
        snapshot_size = min(
            int(logical_volume.vg_free_count),
            (int(logical_volume.lv_size) * 0.2) / extent_size,
            (15 * 1024 ** 3) / extent_size,
        )
        LOG.info(
            "Auto-sizing snapshot-size to %s (%d extents)",
            format_bytes(snapshot_size * extent_size),
            snapshot_size,
        )
        if snapshot_size < 1:
            raise BackupError(
                "Insufficient free extents on %s "
                "to create snapshot (free extents = %s)"
                % (logical_volume.device_name(), logical_volume.vg_free_count)
            )
    else:
        try:
            _snapshot_size = snapshot_size
            snapshot_size = parse_bytes(snapshot_size) / extent_size
            LOG.info(
                "Using requested snapshot-size %s " "rounded by extent-size %s to %s.",
                _snapshot_size,
                format_bytes(extent_size),
                format_bytes(snapshot_size * extent_size),
            )
            if snapshot_size < 1:
                raise BackupError(
                    "Requested snapshot-size (%s) is " "less than 1 extent" % _snapshot_size
                )
            if snapshot_size > int(logical_volume.vg_free_count):
                LOG.info(
                    "Snapshot size requested %s, but only %s available.",
                    config["snapshot-size"],
                    format_bytes(int(logical_volume.vg_free_count) * extent_size, precision=4),
                )
                LOG.info(
                    "Truncating snapshot-size to %d extents (%s)",
                    int(logical_volume.vg_free_count),
                    format_bytes(int(logical_volume.vg_free_count) * extent_size, precision=4),
                )
                snapshot_size = int(logical_volume.vg_free_count)
        except ValueError as exc:
            raise BackupError("Problem parsing snapshot-size %s" % exc)

    mountpoint = config["snapshot-mountpoint"]
    tempdir = False
    if not mountpoint:
        tempdir = True
        if not suppress_tmpdir:
            mountpoint = tempfile.mkdtemp()
    else:
        try:
            os.makedirs(mountpoint)
            LOG.info("Created mountpoint %s", mountpoint)
        except OSError as exc:
            # silently ignore if the mountpoint already exists
            if exc.errno != errno.EEXIST:  # pylint: disable=no-member
                raise BackupError("Failure creating snapshot mountpoint: %s" % str(exc))
    snapshot = Snapshot(snapshot_name, int(snapshot_size), mountpoint)
    if tempdir:
        snapshot.register("finish", lambda *args, **kwargs: cleanup_tempdir(mountpoint))
    return snapshot