Is there a Salt state to manage volumes? (qvm.volume)

I am describing an AppVM using qvm.present and qvm.prefs (docs). I would like to be able to describe something equivalent to the result of:

qvm-volume extend vm-name:root 20g

I have performed an organisation-level search on GitHub for qvm.volume that didn’t yield any Salt-related results. Am I missing something? Is there any Salt state associated with the qvm-volume cli?

2 Likes

No - you need to run volume related calls manually with (e.g)
cmd.run.

1 Like

I have code for that. This is a Salt state module, so you would place it in whichever _states directory your setup uses.

import subprocess


def size_to_bytes(size):
    size = str(size)
    if size.endswith("G"):
        size = str(int(size[:-1]) * 1024 * 1024 * 1024)
    elif size.endswith("M"):
        size = str(int(size[:-1]) * 1024 * 1024)
    elif size.endswith("K"):
        size = str(int(size[:-1]) * 1024)
    return size


def set_size(name, volume, size):
    size = size_to_bytes(size)
    ret = {
        "name": name,
        "comment": "",
        "changes": {},
        "result": False,
    }
    try:
        current = subprocess.check_output(
            ["qvm-volume", "info", "{name}:{volume}".format(**locals()), "size"],
            universal_newlines=True,
        ).rstrip()
    except (subprocess.CalledProcessError, OSError) as e:
        ret["comment"] = str(e)
        return ret

    if size == current:
        ret['result'] = True
        return ret
    
    if __opts__['test']:
        ret['comment'] = "Volume %s of VM %s would be resized from %s to %s" % (name, volume, current, size)
        ret['changes'] = {volume: size}
        ret['result'] = None
        return ret

    p = subprocess.Popen(
        ["qvm-volume", "resize", "{name}:{volume}".format(**locals()), size],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        universal_newlines=True,
    )
    output, _ = p.communicate()
    output = output.rstrip()
    returncode = p.wait()
    if returncode == 0:
        ret['comment'] = "Volume %s of VM %s resized from %s to %s" % (name, volume, current, size) + "\n" + output
        ret['changes'] = {volume: size}
        ret['result'] = True
    else:
        ret["comment"] = "Failed executing qvm-volume resize\n" + str(output)
    return ret

1 Like

Hi @Rudd-O! Thank you for sharing this!

I am not very familiar with using modules, so I’ll do some reading. :wink:

2 posts were split to a new topic: Why does qvm-volume resize seem inconsistent?