Exemple #1
0
def start_vm_with_cdrom(api, vm_id, isoname='ks.iso'):
    """Add CDROM and boot vm."""
    vms_service = api.system_service().vms_service()
    vm_service = vms_service.vm_service(vm_id)
    cdroms_service = vm_service.cdroms_service()
    cdrom = cdroms_service.list()[0]
    cdrom_service = cdroms_service.cdrom_service(cdrom.id)
    cdrom_service.update(
        cdrom=types.Cdrom(file=types.File(id=isoname), ),
        current=False,
    )
    vm_service.start(vm=types.Vm(os=types.OperatingSystem(boot=types.Boot(
        devices=[types.BootDevice.HD, types.BootDevice.CDROM]))))
Exemple #2
0
def start_vm_with_cdrom(api, options):
    """Add CDROM and boot vm."""
    search_name = "name=" + options['vm_name']
    vms_service = api.system_service().vms_service()
    vm = vms_service.list(search=search_name)[0]
    vm_service = vms_service.vm_service(vm.id)
    cdroms_service = vm_service.cdroms_service()
    cdrom = cdroms_service.list()[0]
    cdrom_service = cdroms_service.cdrom_service(cdrom.id)
    cdrom_service.update(
        cdrom=types.Cdrom(file=types.File(id=options['cdrom']), ),
        current=False,
    )
    vm_service.start(vm=types.Vm(os=types.OperatingSystem(boot=types.Boot(
        devices=[types.BootDevice.HD, types.BootDevice.CDROM]))))
Exemple #3
0
    def _attach_cd(self, entity):
        cd_iso = self.param('cd_iso')
        if cd_iso is not None:
            vm_service = self._service.service(entity.id)
            current = vm_service.get().status == otypes.VmStatus.UP
            cdroms_service = vm_service.cdroms_service()
            cdrom_device = cdroms_service.list()[0]
            cdrom_service = cdroms_service.cdrom_service(cdrom_device.id)
            cdrom = cdrom_service.get(current=current)
            if getattr(cdrom.file, 'id', '') != cd_iso:
                if not self._module.check_mode:
                    cdrom_service.update(
                        cdrom=otypes.Cdrom(file=otypes.File(id=cd_iso)),
                        current=current,
                    )
                self.changed = True

        return entity
Exemple #4
0
# Find the virtual machine:
vm = vms_service.list(search='name=myvm')[0]

# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)

# Locate the service that manages the CDROM devices of the VM:
cdroms_service = vm_service.cdroms_service()

# Get the first found CDROM:
cdrom = cdroms_service.list()[0]

# Locate the service that manages the CDROM device found in previous step
# of the VM:
cdrom_service = cdroms_service.cdrom_service(cdrom.id)

# Change the CD of the VM to 'my_iso_file.iso'. By default the below
# operation change permanently the disk that will be visible to the
# virtual machine after the next boot, but they don't have any effect
# on the currently running virtual machine. If you want to change the
# disk that is visible to the current running virtual machine, change
# the `current` parameter's value to `True`.
cdrom_service.update(
    cdrom=types.Cdrom(file=types.File(id='my_iso_file.iso'), ),
    current=False,
)

# Close the connection to the server:
connection.close()
Exemple #5
0
    vm = vms_service.list(search="name={}".format(args.vm_name))[0]

    # Locate the service that manages the virtual machine:
    vm_service = vms_service.vm_service(vm.id)

    # Locate the service that manages the CDROM devices of the VM:
    cdroms_service = vm_service.cdroms_service()

    # Get the first found CDROM:
    cdrom = cdroms_service.list()[0]

    # Locate the service that manages the CDROM device found in previous step
    # of the VM:
    cdrom_service = cdroms_service.cdrom_service(cdrom.id)

    # Change the CD of the VM to file with 'disk-id'. By default the change
    # to the disk is visible to the current running virtual machine immediately,
    # but won't be visible to the virtual machine after the next boot. If you
    # want to change CD permanently, use --now=False. Using this option will
    # change the CD permanently, but it will become visible only after next
    # boot.
    cdrom_service.update(
        cdrom=types.Cdrom(
            file=types.File(
                id=args.disk_id
            ),
        ),
        current=args.current,
    )

Exemple #6
0
def main():

    parser = option_parser()
    args = parser.parse_args()

    if not early_option_check(args):
        sys.exit(-1)

    # Create the connection to the server:
    connection = sdk.Connection(
        url='https://@ENGINE_FQDN@/ovirt-engine/api',
        username='******',
        password=base64.b64decode('@ENGINEPASS_BASE64@'),
        ca_file='@CA_PEM@',
        debug=False,
    )

    vms_service = connection.system_service().vms_service()
    cluster = connection.system_service().clusters_service().list()[0]
    clustername = cluster.name
    dcs_service = connection.system_service().data_centers_service()
    dc = dcs_service.list(search='Clusters.name=%s' % cluster.name)[0]
    networks_service = dcs_service.service(dc.id).networks_service()
    profiles_service = connection.system_service().vnic_profiles_service()

    if not later_option_check(args, connection):
        connection.close()
        sys.exit(-1)

    shorthand = {
        'rhel6': 'rhel_6x64',
        'rhel7': 'rhel_7x64',
        'rhel8': 'rhel_8x64',
        'ubuntu': 'ubuntu_14_04',
        'debian': 'debian_7',
    }

    vmtype = {
        'server': types.VmType.SERVER,
        'desktop': types.VmType.DESKTOP,
        'high_performance': types.VmType.HIGH_PERFORMANCE
    }

    # Creating new virtual machine
    vm = types.Vm()
    vm.name = args.name
    vm.cluster = types.Cluster(name=clustername)
    vm.template = types.Template(name=args.template)
    if args.os in shorthand.keys():
        vm.os = types.OperatingSystem(type=shorthand[args.os])
    else:
        vm.os = types.OperatingSystem(type=args.os)
    vm.memory = args.memory * 1024 * 1024
    if args.balloon == 0:
        vm.memory_policy = types.MemoryPolicy(
            max=args.max_memory * 1024 * 1024,
            guaranteed=args.guaranteed_memory * 1024 * 1024)
    else:
        vm.memory_policy = types.MemoryPolicy(
            max=args.max_memory * 1024 * 1024,
            guaranteed=args.guaranteed_memory * 1024 * 1024,
            ballooning=True if args.balloon == 1 else False)

    vm.cpu = types.Cpu()
    vm.cpu.architecture = types.Architecture.X86_64
    vm.cpu.topology = types.CpuTopology(cores=1, sockets=args.cpu, threads=1)
    if args.sound != 0:
        vm.soundcard_enabled = True if args.sound == 1 else False
    vm.type = vmtype[args.type]

    print("Creating New Virtual Machine:{0}".format(args.name))
    vm = vms_service.add(vm)

    while vms_service.list(search=args.name)[0].status != types.VmStatus.DOWN:
        time.sleep(1)

    # Attach network interface(s)
    nics_service = vms_service.vm_service(vm.id).nics_service()
    nicnum = 0

    for netname in args.vmnet:
        network = next(
            (n for n in networks_service.list() if n.name == netname), None)
        profile_id = None
        for profile in profiles_service.list():
            if profile.name == netname:
                profile_id = profile.id
                break

        if profile_id != None:
            nicnum = nicnum + 1
            print("Attaching nic{0}(Network:{1})".format(nicnum, netname))
            nics_service.add(
                types.Nic(
                    name="nic{0}".format(nicnum),
                    vnic_profile=types.VnicProfile(id=profile_id, ),
                ), )

    # Create and attach disk(s)
    disk_attachments_service = vms_service.vm_service(
        vm.id).disk_attachments_service()
    disks_service = connection.system_service().disks_service()
    disknum = 0
    for d in args.vmdisk:
        disknum += 1
        new_disk = types.DiskAttachment()
        new_disk.disk = types.Disk()
        new_disk.disk.name = "{0}_Disk{1}".format(args.name, disknum)
        new_disk.disk.provisioned_size = int(d.split(':')[1]) * 2**30
        new_disk.disk.storage_domains = [
            types.StorageDomain(name=d.split(':')[0])
        ]
        if d.split(':')[2] == "RAW":
            new_disk.disk.format = types.DiskFormat.RAW
        else:
            new_disk.disk.format = types.DiskFormat.COW

        new_disk.interface = types.DiskInterface.VIRTIO_SCSI
        new_disk.active = True
        if disknum == 1:
            new_disk.bootable = True

        print(
            "Attaching Disk{0}(Domain:{1}, Size:{2}GB, DiskFormat:{3})".format(
                disknum,
                d.split(':')[0],
                d.split(':')[1],
                d.split(':')[2]))
        disk_attachment = disk_attachments_service.add(new_disk)
        disk_service = disks_service.disk_service(disk_attachment.disk.id)
        # wait disk attach finish
        time.sleep(5)
        while disk_service.get().status != types.DiskStatus.OK:
            print("Waiting disk attach complete")
            time.sleep(5)

    if args.ks != None or args.ps != None or args.ai != None:
        # one-shot VM configuration for Kickstart/preseed
        one_vm = types.Vm()
        one_vm.os = types.OperatingSystem()
        one_vm.os.kernel = 'iso://' + args.kernel
        one_vm.os.initrd = 'iso://' + args.initrd
        one_vm.run_once = True
        one_vm.cdroms = list()
        one_vm.cdroms.append(types.Cdrom())
        one_vm.cdroms[0].file = types.File()
        one_vm.cdroms[0].file.id = args.iso
        if args.dns == None:
            args.dns = ""
        elif args.os == 'rhel6':
            args.dns = 'dns=' + args.dns
        else:
            args.dns = 'nameserver=' + args.dns

        if args.os == 'rhel6':
            ksdev = args.network.split(':')[5]
            ksip = args.network.split(':')[0]
            ksnm = calc_netmask(int(args.network.split(':')[3]))
            ksgw = args.network.split(':')[2]
            args.network = "ksdevice={0} ip={1} netmask={2} gateway={3}".format(
                ksdev, ksip, ksnm, ksgw)

        if args.ks != None:
            if args.os == 'rhel6':
                one_vm.os.cmdline = args.network + " " + args.dns + " ks=" + args.ks
            else:
                one_vm.os.cmdline = args.network + " " + args.dns + " inst.ks=" + args.ks
        if args.ps != None:
            one_vm.os.cmdline = "auto=true url=" + args.ps
        if args.ai != None:
            one_vm.os.cmdline = "autoinstall ds=nocloud-net;s=" + args.ai

        vm_service = vms_service.vm_service(vm.id)
        print("Starting automatic OS installation on {0}".format(args.name))
        vm_service.start(vm=one_vm, volatile=True)

    # Close the connection to the server:
    connection.close()