salt.modules.virt

Work with virtual machines managed by libvirt

depends:libvirt Python module
salt.modules.virt.cpu_baseline(full=False, migratable=False, out='libvirt')

Return the optimal 'custom' CPU baseline config for VM's on this minion

2016.3.0 新版功能.

参数:
  • full -- Return all CPU features rather than the ones on top of the closest CPU model
  • migratable -- Exclude CPU features that are unmigratable (libvirt 2.13+)
  • out -- 'libvirt' (default) for usable libvirt XML definition, 'salt' for nice dict

CLI 范例:

salt '*' virt.cpu_baseline
salt.modules.virt.create(domain)

2016.3.0 版后已移除: Use start() instead.

Start a defined domain

CLI 范例:

salt '*' virt.create <domain>
salt.modules.virt.create_xml_path(path)

Start a domain based on the XML-file path passed to the function

CLI 范例:

salt '*' virt.create_xml_path <path to XML file on the node>
salt.modules.virt.create_xml_str(xml)

Start a domain based on the XML passed to the function

CLI 范例:

salt '*' virt.create_xml_str <XML in string format>
salt.modules.virt.ctrl_alt_del(vm_)

Sends CTRL+ALT+DEL to a VM

CLI 范例:

salt '*' virt.ctrl_alt_del <domain>
salt.modules.virt.define_vol_xml_path(path)

Define a volume based on the XML-file path passed to the function

CLI 范例:

salt '*' virt.define_vol_xml_path <path to XML file on the node>
salt.modules.virt.define_vol_xml_str(xml)

Define a volume based on the XML passed to the function

CLI 范例:

salt '*' virt.define_vol_xml_str <XML in string format>
salt.modules.virt.define_xml_path(path)

Define a domain based on the XML-file path passed to the function

CLI 范例:

salt '*' virt.define_xml_path <path to XML file on the node>
salt.modules.virt.define_xml_str(xml)

Define a domain based on the XML passed to the function

CLI 范例:

salt '*' virt.define_xml_str <XML in string format>
salt.modules.virt.delete_snapshots(name, *names, **kwargs)

Delete one or more snapshots of the given VM.

Options:

  • all: Remove all snapshots. Values: True or False (default False).

2016.3.0 新版功能.

CLI 范例:

salt '*' virt.delete_snapshots <domain> all=True
salt '*' virt.delete_snapshots <domain> <snapshot>
salt '*' virt.delete_snapshots <domain> <snapshot1> <snapshot2> ...
salt.modules.virt.destroy(domain)

2016.3.0 版后已移除: Use stop() instead.

Power off a defined domain

CLI 范例:

salt '*' virt.destroy <domain>
salt.modules.virt.freecpu()

Return an int representing the number of unallocated cpus on this hypervisor

CLI 范例:

salt '*' virt.freecpu
salt.modules.virt.freemem()

Return an int representing the amount of memory (in MB) that has not been given to virtual machines on this node

CLI 范例:

salt '*' virt.freemem
salt.modules.virt.full_info()

Return the node_info, vm_info and freemem

CLI 范例:

salt '*' virt.full_info
salt.modules.virt.get_disks(vm_)

Return the disks of a named vm

CLI 范例:

salt '*' virt.get_disks <domain>
salt.modules.virt.get_graphics(vm_)

Returns the information on vnc for a given vm

CLI 范例:

salt '*' virt.get_graphics <domain>
salt.modules.virt.get_macs(vm_)

Return a list off MAC addresses from the named vm

CLI 范例:

salt '*' virt.get_macs <domain>
salt.modules.virt.get_nics(vm_)

Return info about the network interfaces of a named vm

CLI 范例:

salt '*' virt.get_nics <domain>
salt.modules.virt.get_profiles(hypervisor=None)

Return the virt profiles for hypervisor.

Currently there are profiles for:

  • nic
  • disk

CLI 范例:

salt '*' virt.get_profiles
salt '*' virt.get_profiles hypervisor=esxi
salt.modules.virt.get_xml(vm_)

Returns the XML for a given vm

CLI 范例:

salt '*' virt.get_xml <domain>
salt.modules.virt.init(name, cpu, mem, image=None, nic='default', hypervisor='kvm', start=True, disk='default', saltenv='base', seed=True, install=True, pub_key=None, priv_key=None, seed_cmd='seed.apply', enable_vnc=False, **kwargs)

Initialize a new vm

CLI 范例:

salt 'hypervisor' virt.init vm_name 4 512 salt://path/to/image.raw
salt 'hypervisor' virt.init vm_name 4 512 nic=profile disk=profile
salt.modules.virt.is_hyper()

Returns a bool whether or not this node is a hypervisor of any kind

CLI 范例:

salt '*' virt.is_hyper
salt.modules.virt.is_kvm_hyper()

Returns a bool whether or not this node is a KVM hypervisor

CLI 范例:

salt '*' virt.is_kvm_hyper
salt.modules.virt.is_xen_hyper()

Returns a bool whether or not this node is a XEN hypervisor

CLI 范例:

salt '*' virt.is_xen_hyper
salt.modules.virt.list_active_vms()

Return a list of names for active virtual machine on the minion

CLI 范例:

salt '*' virt.list_active_vms
salt.modules.virt.list_domains()

Return a list of available domains.

CLI 范例:

salt '*' virt.list_domains
salt.modules.virt.list_inactive_vms()

Return a list of names for inactive virtual machine on the minion

CLI 范例:

salt '*' virt.list_inactive_vms
salt.modules.virt.list_snapshots(domain=None)

List available snapshots for certain vm or for all.

2016.3.0 新版功能.

CLI 范例:

salt '*' virt.list_snapshots
salt '*' virt.list_snapshots <domain>
salt.modules.virt.list_vms()

2016.3.0 版后已移除: Use list_domains() instead.

List all virtual machines.

CLI 范例:

salt '*' virt.list_vms <domain>
salt.modules.virt.migrate(vm_, target, ssh=False)

Shared storage migration

CLI 范例:

salt '*' virt.migrate <domain> <target hypervisor>
salt.modules.virt.migrate_non_shared(vm_, target, ssh=False)

Attempt to execute non-shared storage "all" migration

CLI 范例:

salt '*' virt.migrate_non_shared <vm name> <target hypervisor>
salt.modules.virt.migrate_non_shared_inc(vm_, target, ssh=False)

Attempt to execute non-shared storage "all" migration

CLI 范例:

salt '*' virt.migrate_non_shared_inc <vm name> <target hypervisor>
salt.modules.virt.node_info()

Return a dict with information about this node

CLI 范例:

salt '*' virt.node_info
salt.modules.virt.pause(vm_)

Pause the named vm

CLI 范例:

salt '*' virt.pause <domain>
salt.modules.virt.purge(vm_, dirs=False)

Recursively destroy and delete a virtual machine, pass True for dir's to also delete the directories containing the virtual machine disk images - USE WITH EXTREME CAUTION!

CLI 范例:

salt '*' virt.purge <domain>
salt.modules.virt.reboot(name)

Reboot a domain via ACPI request

CLI 范例:

salt '*' virt.reboot <domain>
salt.modules.virt.reset(vm_)

Reset a VM by emulating the reset button on a physical machine

CLI 范例:

salt '*' virt.reset <domain>
salt.modules.virt.resume(vm_)

Resume the named vm

CLI 范例:

salt '*' virt.resume <domain>
salt.modules.virt.revert_snapshot(name, snapshot=None, cleanup=False)

Revert snapshot to the previous from current (if available) or to the specific.

Options:

  • cleanup: Remove all newer than reverted snapshots. Values: True or False (default False).

2016.3.0 新版功能.

CLI 范例:

salt '*' virt.revert <domain>
salt '*' virt.revert <domain> <snapshot>
salt.modules.virt.seed_non_shared_migrate(disks, force=False)

Non shared migration requires that the disks be present on the migration destination, pass the disks information via this function, to the migration destination before executing the migration.

CLI 范例:

salt '*' virt.seed_non_shared_migrate <disks>
salt.modules.virt.set_autostart(vm_, state='on')

Set the autostart flag on a VM so that the VM will start with the host system on reboot.

CLI 范例:

salt "*" virt.set_autostart <domain> <on | off>
salt.modules.virt.setmem(vm_, memory, config=False)

Changes the amount of memory allocated to VM. The VM must be shutdown for this to work.

memory is to be specified in MB If config is True then we ask libvirt to modify the config as well

CLI 范例:

salt '*' virt.setmem <domain> <size>
salt '*' virt.setmem my_domain 768
salt.modules.virt.setvcpus(vm_, vcpus, config=False)

Changes the amount of vcpus allocated to VM. The VM must be shutdown for this to work.

vcpus is an int representing the number to be assigned If config is True then we ask libvirt to modify the config as well

CLI 范例:

salt '*' virt.setvcpus <domain> <amount>
salt '*' virt.setvcpus my_domain 4
salt.modules.virt.shutdown(vm_)

Send a soft shutdown signal to the named vm

CLI 范例:

salt '*' virt.shutdown <domain>
salt.modules.virt.snapshot(domain, name=None, suffix=None)

Create a snapshot of a VM.

Options:

  • name: Name of the snapshot. If the name is omitted, then will be used original domain name with

    ISO 8601 time as a suffix.

  • suffix: Add suffix for the new name. Useful in states, where such snapshots

    can be distinguished from manually created.

2016.3.0 新版功能.

CLI 范例:

salt '*' virt.snapshot <domain>
salt.modules.virt.start(name)

Start a defined domain

CLI 范例:

salt '*' virt.start <domain>
salt.modules.virt.stop(name)

Hard power down the virtual machine, this is equivalent to pulling the power.

CLI 范例:

salt '*' virt.stop <domain>
salt.modules.virt.undefine(vm_)

Remove a defined vm, this does not purge the virtual machine image, and this only works if the vm is powered down

CLI 范例:

salt '*' virt.undefine <domain>
salt.modules.virt.virt_type()

Returns the virtual machine type as a string

CLI 范例:

salt '*' virt.virt_type
salt.modules.virt.vm_cputime(vm_=None)

Return cputime used by the vms on this hyper in a list of dicts:

[
    'your-vm': {
        'cputime' <int>
        'cputime_percent' <int>
        },
    ...
    ]

If you pass a VM name in as an argument then it will return info for just the named VM, otherwise it will return all VMs.

CLI 范例:

salt '*' virt.vm_cputime
salt.modules.virt.vm_diskstats(vm_=None)

Return disk usage counters used by the vms on this hyper in a list of dicts:

[
    'your-vm': {
        'rd_req'   : 0,
        'rd_bytes' : 0,
        'wr_req'   : 0,
        'wr_bytes' : 0,
        'errs'     : 0
        },
    ...
    ]

If you pass a VM name in as an argument then it will return info for just the named VM, otherwise it will return all VMs.

CLI 范例:

salt '*' virt.vm_blockstats
salt.modules.virt.vm_info(vm_=None)

Return detailed information about the vms on this hyper in a list of dicts:

[
    'your-vm': {
        'cpu': <int>,
        'maxMem': <int>,
        'mem': <int>,
        'state': '<state>',
        'cputime' <int>
        },
    ...
    ]

If you pass a VM name in as an argument then it will return info for just the named VM, otherwise it will return all VMs.

CLI 范例:

salt '*' virt.vm_info
salt.modules.virt.vm_netstats(vm_=None)

Return combined network counters used by the vms on this hyper in a list of dicts:

[
    'your-vm': {
        'rx_bytes'   : 0,
        'rx_packets' : 0,
        'rx_errs'    : 0,
        'rx_drop'    : 0,
        'tx_bytes'   : 0,
        'tx_packets' : 0,
        'tx_errs'    : 0,
        'tx_drop'    : 0
        },
    ...
    ]

If you pass a VM name in as an argument then it will return info for just the named VM, otherwise it will return all VMs.

CLI 范例:

salt '*' virt.vm_netstats
salt.modules.virt.vm_state(vm_=None)

Return list of all the vms and their state.

If you pass a VM name in as an argument then it will return info for just the named VM, otherwise it will return all VMs.

CLI 范例:

salt '*' virt.vm_state <domain>