mkvm at [d3ca2e329c]
Not logged in

File mkvm artifact c9b2ef6117 part of check-in d3ca2e329c


#!/usr/bin/python
import os,sys
from snack import SnackScreen,GridForm,Listbox,Entry,Label,Checkbox,Button,RadioBar
bridge="lxc0"
secno=3
macaddr="52:54:00:7d:7f:%02x"%secno
options={'qemubinary':'qemu-system-x86_64',
"accel":"-enable-kvm",
"memory":"1024M",
"drive":"-drive media=disk,index=0,if=virtio,file=drive0.qcow2",
"cdrom":"-drive media=cdrom,index=2,if=ide",
"sound":"-soundhw hda",
"net":"-net nic,macaddr=%s -net bridge,br=%s"%(macaddr,bridge),
"usb":"-usb"}

def choose_conf():
    """
    Selects main machine configuration 
    CPU type, memory size, USB and sound hardware
    """
    archs=['i386','x86_64']
    grid = GridForm(screen,"Setting up a virtual machine",2,5)
    archlabel=Label("CPU architecture")
    archlist=Listbox(height=4,width=20)
    for arch in archs:
        archlist.append(arch,arch)
    archlist.setCurrent(options['qemubinary'][12:])
    memlabel=Label("Memory size")
    memline=Entry(20,text=options['memory'])
    checkusb=Checkbox("Enable USB",isOn=len(options["usb"]))
    checksound=Checkbox("Enable sound",isOn=len(options["sound"]))
    checkkvm=Checkbox("Enable KVM",isOn=len(options['accel']))
    grid.add(archlabel,0,0,padding=(3,1,3,0))
    grid.add(archlist,0,1,padding=(3,0,3,1))
    grid.add(memlabel,1,0,padding=(3,1,3,0))
    grid.add(memline,1,1,padding=(3,0,3,1),anchorTop=1)
    grid.add(checkusb,0,2,padding=(3,1,3,0),anchorLeft=1)
    grid.add(checksound,0,3,padding=(3,0,3,1),anchorLeft=1)
    grid.add(checkkvm,1,2,padding=(3,1,3,1),anchorLeft=1)
    prevbtn=Button("<<Back")
    nextbtn=Button("Next>>")
    grid.add(prevbtn,0,4,anchorLeft=1)
    grid.add(nextbtn,1,4,anchorRight=1)
    answer=grid.runOnce()
    if answer == prevbtn or answer == u'ESC':
        return False
    else:
        if checkusb.value():
            options['usb']='-usb'
        else:
            options['usb']=''
        if checksound.value():
            options['sound']='-soundhw hba'
        else :
            options['sound']=''
        if checkkvm.value():
            options['accel'] = '-enable-kvm'
        else:
            options['accel'] = ''
        options['qemubinary']='qemu-system-'+archlist.current()
        options['memory']=memline.value()   
        return True

def choose_drive():
    """
    Selects storage subsystem to use
    import existing drive, use cdrom,
    connect some image on first run
    """
    grid = GridForm(screen,"Setting up a disk drives",1,5)
    
def mkradiogroup(choices, current):
    """
    choises is an array of value,text tuples
    current is value, which have to be selected
    """
    rg = RadioBar(screen,map(lambda x: (x[1],x[0],1 if x[0]==current else 0),choices) )
    return rg

def net_brige_conf():
    """
    Choose net bridge based on /etc/qemu/bridge.conf"
    """


def choose_net():
    """
    Select network type (bridge, tap or user)
    Select parameters of the choosen network type
    """
    grid = GridForm(screen,"Setting up a virtual networking",2,3)
    typlbl = Label("Network type")
    rg = mkradiogroup([('bridge','Attach to bridge'),
        ('tap','Use TAP device'),
        ('user','User mode networking'),
        ('vde','VDE virtual switch')],
        netmode)
    cardlbl = Label("Emulated hardware")    
    cardbox= Listbox(height=6,width=20,scroll=1)
    grid.add(typlbl,0,0,padding=(3,1,3,0))
    grid.add(rg,0,1,padding=(3,0,3,0))
    grid.add(cardlbl,1,0,padding=(3,1,3,0))
    grid.add(cardbox,1,1,padding=(3,0,3,0))
    prevbtn=Button("<<Back")
    nextbtn=Button("Next>>")
    grid.add(prevbtn,0,2,anchorLeft=1)
    grid.add(nextbtn,1,2,anchorRight=1)
name=''
vmtype='private'
def choose_name():
    """
    Selects name of virtual machine and sharing type
    """
    global name,vmtype
    grid = GridForm(screen,"Choosing name of virtual machine",1,5)
    namelbl=Label("Enter a virtual machine name")
    nameline=Entry(40,text=name)
    shlbl = Label("Select virtual machine accessability")
    rg = mkradiogroup([('private','Private VM'),('shared','Shared VM'),
                ('auto','AutoStart VM')],vmtype)

    grid.add(namelbl,0,0,padding=(3,1,3,0),anchorLeft=1)
    grid.add(nameline,0,1,padding=(3,0,3,0),anchorLeft=1)
    grid.add(shlbl,0,2,padding=(3,1,3,0),anchorLeft=1)
    grid.add(rg,0,3)
    #prevbtn=Button("<<Back")
    nextbtn=Button("Next>>")
    #grid.add(prevbtn,0,4,anchorLeft=1)
    grid.add(nextbtn,0,4,anchorRight=1)
    while not nameline.value():
        answer=grid.runOnce()
        if  answer == u'ESC':
            return False
    name = nameline.value()
    vmtype = rg.getSelection()
    return True

wizard=[choose_name,choose_conf,choose_drive,choose_net,None]    

try:
    screen=SnackScreen()
    i=0
    while i<len(wizard):
        if wizard[i]:
            nxt=wizard[i]()
        else:
            nxt=True
        if callable(nxt):
            wizard[i+1] = nxt
        if nxt:
            i+=1
        else :
            i-=1
        if (i<0):
            sys.exit(1)

finally:
    screen.finish()
with open("start.template","r") as f:
	template=f.read()

vmdir = "~/VWs/%s" % name
print repr(ret)
print repr(options)

sys.exit(1)

os.mkdir(vmdir)
with open(vmdir +"/start",w) as f:
	f.write(template.format(options))
os.chmod(0755,vmdir + "/start")
os.system("qemu-img create -f qcow2 "+vmdir+"/drive0.qcow2")