1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
#!/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")
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|