【问题标题】:virt-install in python script在 python 脚本中安装 virt
【发布时间】:2017-01-04 11:39:02
【问题描述】:

我的任务是通过将 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) 并且运行良好。

【问题讨论】:

    标签: python shell virtual-machine libvirt


    【解决方案1】:

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

    >>> 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/

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-14
        • 1970-01-01
        • 2011-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多