[Engine-devel] [Users] Related to python script for Attach Cd (.iso) to a vm
Michael Pasternak
mpastern at redhat.com
Mon Nov 5 05:51:49 UTC 2012
Hi Romil,
In general you can use api guide [1] for this, anyway here is example:
1. create vm
============
...
2. insert cdrom
===============
vm = api.vms.get(name="myvm")
cdrom = vm.cdroms.get(id="00000000-0000-0000-0000-000000000000")
isofile = params.File(id="myiso.iso")
cdrom.set_file(isofile)
cdrom.update()
3. change boot order (to boot from cdrom and then from hd)
==========================================================
os=params.OperatingSystem(boot=[params.Boot(dev='cdrom'), params.Boot(dev='hd')], ...)
api.vms.add(params.VM(name='myvm2',
os=os,
cluster=api.clusters.get('mycluster'),
template=api.templates.get('mytemplate')))
4. run vm
=========
...
[1] https://access.redhat.com/knowledge/docs/en-US/Red_Hat_Enterprise_Virtualization/3.1-Beta/html-single/Developer_Guide/index.html#sect-REST_API_Guide-Example-Attach_CDROM
On 11/02/2012 09:56 AM, Romil Gupta wrote:
>
> Hello ,
>
> *I want to write a script to attach a '.iso' image to a vm *
> so ,can you please help me or tell me some api so that i can continue my work !!!
> I have some '.iso ' file in my storage .
>
> script *rhevm_test.py*
>
> from ovirtsdk.api import API
> from ovirtsdk.xml import params
> import time
>
> rhevm_uri = "https://rhevm301.xxx.xx.com:8443/api"
> rhevm_username = "admin at rhevm301.xxx.xx.com <mailto:admin at rhevm301.xxx.xx.com>"
> rhevm_password = "iso*help"
>
> api = API(url=rhevm_uri, username=rhevm_username, password=rhevm_password)
> print "Connected to RHEVM Successful"
>
> MB = 1024*1024
> GB = 1024*MB
> VM_NAME = 'test_vm'
> DESCRIP = 'testing vm'
> CLUSTER_NAME = 'Default'
> STORAGE_NAME = 'rhevmVMdata'
>
> domain_name= 'rhevmiso'
> domain_type = 'ISO'
>
>
> try:
> api.vms.add(params.VM(name=VM_NAME,description=DESCRIP, memory=2*GB,cluster=api.clusters.get(CLUSTER_NAME), template=api.templates.get('Blank') , cdroms =
> 'CentOS-6.2-x86_64-LiveCD.iso '))
>
> print 'VM created'
>
> api.vms.get(VM_NAME).nics.add(params.NIC(name='nic1', network=params.Network(name='rhevm'), interface='Red Hat VirtIO'))
> print 'NIC added to VM'
>
>
> api.vms.get(VM_NAME).disks.add(params.Disk(storage_domains=params.StorageDomains(storage_domain=[api.storagedomains.get(STORAGE_NAME)]),size=512*MB,status=None,interface='VirtIO',format='Preallocated',sparse=True,bootable=True))
>
> print 'Disk added to VM'
> print 'Waiting for VM to reach Down status'
> while api.vms.get(VM_NAME).status.state != 'down':
> time.sleep(1)
> except Exception as e:
> print 'Failed to create VM with disk and NIC\n%s' % str(e)
>
> time.sleep(10)
>
> try:
> if api.vms.get(VM_NAME).status.state != 'up':
> print 'Starting VM'
> api.vms.get(VM_NAME).start()
> print 'Waiting for VM to reach Up status'
> while api.vms.get(VM_NAME).status.state != 'up':
> time.sleep(1)
> else:
> print 'VM already up'
> except Exception as e:
> print 'Failed to Start VM:\n%s' % str(e)
>
>
> and got some exceptions
>
> Connected to RHEVM Successful
> Failed to create VM with disk and NIC
> 'str' object has no attribute 'export'
> Failed to Start VM:
> 'NoneType' object has no attribute 'status'
>
> if i remove this cdroms = 'CentOS-6.2-x86_64-LiveCD.iso ' the code will works fine and it will create a vm with blank template w/o any '.iso' attached !!
>
>
> *
> *
> *
> *
> *
> *
> *
> *
> *With Regards,*
> *Romil*
>
>
>
>
>
> _______________________________________________
> Users mailing list
> Users at ovirt.org
> http://lists.ovirt.org/mailman/listinfo/users
--
Michael Pasternak
RedHat, ENG-Virtualization R&D
More information about the Devel
mailing list