
from __future__ import absolute_import

from vdsm.common import commands
from vdsm.common.cmdutils import CommandPath

from vdsm.storage import operation

SAFE_WRITE_SAME_SIZE = 2 * 1024**2

OPTIMAL_DISCARD_STEP = 32 * 1024**2

_blkdiscard = CommandPath("blkdiscard", "/sbin/blkdiscard")


def discard(device):
    cmd = [
        _blkdiscard.cmd,
        "--step", "%d" % OPTIMAL_DISCARD_STEP,
    ]
    cmd.append(device)
    commands.run(cmd)


def zeroout_operation(device, size):
    return operation.Command([
        _blkdiscard.cmd,
        "--zeroout",
        "--step", "%d" % SAFE_WRITE_SAME_SIZE,
        "--length", "%d" % size,
        device,
    ])
