在python脚本中的virt-install

时间:2017-01-04 11:39:03

标签: python shell virtual-machine libvirt

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

import { Injectable } from '@angular/core';

export class Test {
  public id: number; public name: string; public fid: number
};

export const TESTS: Test[] = [
  {id: 1, name: 'Cat', fid: 1},
  {id: 2, name: 'Dog', fid: 1},
  {id: 3, name: 'Elephant', fid: 1},
  {id: 4, name: 'Bird', fid: 1},
  {id: 5, name: 'Duck', fid: 2},
  {id: 6, name: 'Snake', fid: 2},
  {id: 7, name: 'Rabbit', fid: 2},
  {id: 8, name: 'Dolphin', fid: 2},
  {id: 9, name: 'Sheep', fid: 3},
  {id: 10, name: 'Cow', fid: 3},
  {id: 11, name: 'Fish', fid: 3},
  {id: 12, name: 'Monkey', fid: 3}];

let testsPromise = Promise.resolve(TESTS);

@Injectable()
export class TestService {
  getTests(id) { 
    let items: Test[] = [];
    for(var i = 0; i < TESTS.length; i++) {
        if(TESTS[i].fid == id) { 
        items.push(TESTS[i]) 
        }
    }
    return items;
 }
}

(virtinstall.sh)运行正常。

2 个答案:

答案 0 :(得分:0)

您可以使用子流程。 这是一个样本:

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

答案 1 :(得分:0)

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

< p>例:
from subprocess import call
call('ls')