def init(name,
         cpu,
         mem,
         image=None,
         nic='default',
         hypervisor=VIRT_DEFAULT_HYPER,
         start=True,  # pylint: disable=redefined-outer-name
         disk='default',
         saltenv='base',
         seed=True,
         install=True,
         pub_key=None,
         priv_key=None,
         seed_cmd='seed.apply',
         enable_vnc=False,
         **kwargs):
    hypervisor = __salt__['config.get']('libvirt:hypervisor', hypervisor)
    log.debug('Using hyperisor {0}'.format(hypervisor))

    nicp = _nic_profile(nic, hypervisor, **kwargs)
    log.debug('NIC profile is {0}'.format(nicp))

    diskp = None
    seedable = False
    if image:  # with disk template image
        log.debug('Image {0} will be used'.format(image))
        # if image was used, assume only one disk, i.e. the
        # 'default' disk profile
        # TODO: make it possible to use disk profiles and use the
        # template image as the system disk
        diskp = _disk_profile('default', hypervisor, **kwargs)
        log.debug('Disk profile is {0}'.format(diskp))

        # When using a disk profile extract the sole dict key of the first
        # array element as the filename for disk
        disk_name = next(six.iterkeys(diskp[0]))
        disk_type = diskp[0][disk_name]['format']
        disk_file_name = '{0}.{1}'.format(disk_name, disk_type)

        if hypervisor in ['esxi', 'vmware']:
            # TODO: we should be copying the image file onto the ESX host
            raise SaltInvocationError(
                'virt.init does not support image template template in '
                'conjunction with esxi hypervisor'
            )
        elif hypervisor in ['qemu', 'kvm']:
            img_dir = __salt__['config.option']('virt.images')
            img_dest = os.path.join(
                img_dir,
                name,
                disk_file_name
            )
            img_dir = os.path.dirname(img_dest)
            sfn = __salt__['cp.cache_file'](image, saltenv)
            log.debug('Image directory is {0}'.format(img_dir))

            try:
                os.makedirs(img_dir)
            except OSError:
                pass

            try:
                log.debug('Copying {0} to {1}'.format(sfn, img_dest))
                salt.utils.files.copyfile(sfn, img_dest)
                mask = os.umask(0)
                os.umask(mask)
                # Apply umask and remove exec bit
                mode = (0o0777 ^ mask) & 0o0666
                os.chmod(img_dest, mode)
            except (IOError, OSError) as e:
                raise CommandExecutionError('problem copying image. {0} - {1}'.format(image, e))

            seedable = True
        else:
            log.error('Unsupported hypervisor when handling disk image')
    else:
        # no disk template image specified, create disks based on disk profile
        diskp = _disk_profile(disk, hypervisor, **kwargs)
        log.debug('No image specified, disk profile will be used: {0}'.format(diskp))
        if hypervisor in ['qemu', 'kvm']:
            # TODO: we should be creating disks in the local filesystem with
            # qemu-img
            raise SaltInvocationError(
                'virt.init does not support disk profiles in conjunction with '
                'qemu/kvm at this time, use image template instead'
            )
        else:
            # assume libvirt manages disks for us
            for disk in diskp:
                for disk_name, args in six.iteritems(disk):
                    log.debug('Generating libvirt XML for {0}'.format(disk))
                    xml = _gen_vol_xml(
                        name,
                        disk_name,
                        args['size'],
                        hypervisor,
                    )
                    define_vol_xml_str(xml)

    log.debug('Generating VM XML')
    kwargs['enable_vnc'] = enable_vnc
    xml = _gen_xml(name, cpu, mem, diskp, nicp, hypervisor, **kwargs)
    try:
        define_xml_str(xml)
    except libvirtError:
        # This domain already exists
        pass

    if seed and seedable:
        log.debug('Seed command is {0}'.format(seed_cmd))
        __salt__[seed_cmd](
            img_dest,
           id_=name,
           config=kwargs.get('config'),
           install=install,
           pub_key=pub_key,
           priv_key=priv_key,
        )
    if start:
        log.debug('Creating {0}'.format(name))
        _get_domain(name).create()

    return True
Initialize a new vm
dataset/ETHPy150Open saltstack/salt/salt/modules/virt.py/init