在 python 脚本中安装 virt

virt-install in python script

我的任务是使用 virt-install 创建虚拟设备,方法是将其编写为 shell 脚本。如何在 python 脚本中实现相同的目的?我是 virt-install 和 python 的新手。谢谢!

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) 并且运行良好。

您可以使用子流程。 这是一个示例:

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

libvirt 有一个 python 接口。所以,如果您打算使用 Python - 您可以直接使用 python 界面。

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

使用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'')

或使用subprocess

例如:
from subprocess import call
call('ls')