Instantly share code, notes, and snippets.

Embed
What would you like to do?
#! /usr/bin/python3
import os
import sys
from subprocess import Popen, PIPE
def command(cmd):
p = Popen(cmd.split(), stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
return stdout if stdout else stderr
if __name__ == '__main__':
print('Image Generator, Empty for default value')
name = input('Input VM name: ')
base = input('Input base Image(trusty/xenial): ')
size = input('Input VM image size(10, GB for unit): ')
if not name:
raise Exception('Name should not be empty')
if not base:
base = 'trusty'
if not size:
size = 10
else:
size = int(size)
variant = 'ubuntu14.04' if base == 'trusty' else 'ubuntu16.04'
create_img_cmd = 'qemu-img create -f qcow2 {name}.qcow2 {size}G'
virt_install_cmd = 'virt-install --virt-type kvm --name {name} --ram 1024 '\
'--cdrom=base/{base}.iso '\
'--disk={name}.qcow2,format=qcow2 '\
'--network network=default '\
'--graphics vnc,listen=0.0.0.0 --noautoconsole '\
'--os-type=linux --os-variant={variant}'
os.system(create_img_cmd.format(name=name, size=size))
os.system(virt_install_cmd.format(name=name, base=base, variant=variant))
#print(command(create_img_cmd.format(name=name, size=size)))
#print(command(virt_install_cmd.format(name=name, base=base, variant)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment