virt-install in python script

1.6k views Asked by At

I have been tasked to create virtual device using virt-install by writing it as a shell script. How can I achieve the same in python script? I'm new to both virt-install and python. Thanks!

virt-install \
   --name centos7 \
   --ram 1024 \
   --disk path=./centos7.qcow2,size=8 \ 
   --vcpus 1 \
   --os-type linux \
   --os-variant centos7 \
   --network bridge=virbr0 \
   --graphics none \
   --console pty,target_type=serial \
   --location 'http://mirror.i3d.net/pub/centos/7/os/x86_64/' \ 
   --extra-args 'console=ttyS0,115200n8 serial'

(virtinstall.sh) and runs fine.

2

There are 2 answers

0
Sharad On BEST ANSWER

You can use subprocess. Here's a sample:

>>> import subprocess
>>> subprocess.call('date')
Wed Jan  4 17:36:58 IST 2017
0
>>> 

libvirt has a python interface. So, if you are planning to use Python - you can directly use the python interface.

http://www.ibm.com/developerworks/library/os-python-kvm-scripting1/

0
Yuriy Zhigulskiy On

Use python os.system()

os.system('virt-install --name centos7 --ram 1024 --disk path=./centos7.qcow2,size=8 --vcpus 1 --os-type linux --os-variant centos7 --network bridge=virbr0 --graphics none --console pty,target_type=serial --location 'http://mirror.i3d.net/pub/centos/7/os/x86_64/' --extra-args 'console=ttyS0,115200n8 serial'')

Or use subprocess

Ex:
from subprocess import call
call('ls')