184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
print "%*s %s" % (-maxlen,f[0],f[1])
else:
print f[0]
if not count:
sys.exit(1)
def cmd_version(options):
print VERSION
def cmd_create(parsed_args):
libdir="/usr/local/lib/vws"
drivename="drive0.qcow2"
options={'qemubinary':'qemu-system-x86_64',
"accel":"-enable-kvm",
"memory":"1024M",
"vga":'qxl',
"drive":"-drive media=disk,index=0,if={interface},file={image}",
"cdrom":"-drive media=cdrom,index=2,if=ide",
"sound":"-soundhw hda",
"usb":"-usb"}
macaddr=":".join(map(lambda x: "%02x"%ord(x),chr(0x52)+os.urandom(5)))
if parsed_args.shared:
machinedir = config.get("directories","SharedVMs")+"/"+parsed_args.name
dirmode = 0755
else:
machinedir = os.environ["HOME"]+"/VWs/"+parsed_args.name
dirmode = 0775
if parsed_args.net != 'user':
bridges = list_bridges()
if not parsed_args.net in bridges:
print >>sys.stderr,"No such bridge %s. Available ones %s"%(parsed_args.net,", ".join(bridges))
sys.exit(1)
options["net"]="-net nic,macaddr=%s -net bridge,br=%s"%(macaddr,parsed_args.net),
else:
options["net"]="-net nic,macaddr=%s -net user"%(macaddr,)
options["qemubinary"] = 'qemu-system-'+parsed_args.arch
options["vga"]=parsed_args.vga
if not parsed_args.arch in ('i386','x86_64'):
print >>sys.stderr,"KVM acceleration disabled due to target architecture"
options.accel = ''
elif not os.access("/dev/kvm",os.W_OK):
print >>sys.stderr,"KVM acceleration disabled due to unavailability on the host system"
options.accel = ''
if not parsed_args.usb:
options["usb"]=''
if not parsed_args.sound:
options["sound"]=''
else:
options["sound"]='-soundhw '+parsed_args.sound
options["memory"]=parsed_args.mem
# Creating directory for VM
os.makedirs(machinedir,dirmode)
driveopts={"interface":parsed_args.diskif,"image":drivename}
if parsed_args.image:
# Copying image file
print >>sys.stderr,"Copying %s to %s"%(parsed_args.image,machinedir+"/"+drivename)
os.system("qemu-img convert -O qcow2 -p %s %s/%s"%(parsed_args.image,machinedir+"/"+drivename))
os.chdir(machinedir)
else:
print >>sys.stderr,"Creating new image file"
os.chdir(machinedir)
os.system("qemu-img create -f qcow2 %s %s"%(drivename,parsed_args.size))
options["drive"]=options["drive"].format(**driveopts)
if parsed_args.debug:
print repr(driveopts),repr(options["drive"])
print repr(options)
with open(libdir+"/start.template","r") as f:
template=f.read()
with open("start","w") as script:
script.write(template.format(**options))
os.chmod('start',0755)
# If installation media is specified vws start for new vm
if parsed_args.install:
start_opts=Namespace(command='start',cdrom=parsed_args.install,stopped=True)
cmd_start(start_opts)
#
# Utility functions for arg parsing
#
def new_command(cmds,name,**kwargs):
"""
Adds a subparser and adds a machine name argument to it
"""
|
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
p=new_command(cmds,'snapshots',help='List existing snapshots')
# Screenshoits and recording
p=new_command(cmds,'screenshoot',help='take a screenshot')
p.add_argument('filename',help='image filename to write screenshot to')
p=new_command(cmds,'record',help='Record audio output from VM')
p.add_argument('filename',help='wav file to record autdio to')
new_command(cmds,'stoprecord',help='stop recording audio')
# Create new VM
p=new_command(cmds,'create',help="Create new VM")
p.add_argument("--no-usb",help="Disable USB controller",action='store_const', const = False, default=True, dest="usb")
p.add_argument("--size",help="Size of primary disk images",dest="size",default="20G");
p.add_argument("--arch",help="Emulated architecture",dest="arch",default='x86_64');
p.add_argument("--no-sound",help="Disable sound card",action='store_const',const = None,default='hda', dest="sound")
p.add_argument("--sound",help="Specify sound card type",dest='sound',default='hda')
p.add_argument("--vga",help="specify video card type (cirrus,std,vmwae,qxl) default qxl",dest="vga",default="qxl")
p.add_argument("--net",help="Network - 'user' or bridge name",dest='net',default="user")
p.add_argument("--mem",help="Size of memory",dest="mem",default="1024M")
p.add_argument("--diskif",help="Disk interface",dest="diskif",default="virtio")
p.add_argument('--shared',help='Create shared VM instead of private one',action='store_const',const= True,dest='shared',default=False)
p.add_argument('--image',help='Existing disk image to import',dest='image',default=None)
p.add_argument('--install',help='ISO image to install OS from',dest='install',default=None)
# Miscellenia
p=new_command(cmds,'monitor',help='connect stdin/stdout to monitor of VM')
p=new_command(cmds,'spiceuri',help='Output spice URI of machine')
parsed_args=args.parse_args(sys.argv[1:])
# Create command is totally different, so it is handled separately
if parsed_args.command == 'create':
cmd_create(parsed_args)
sys.exit(0)
parsed_args.stopped = False
stopped_vm_commands = ['start','snapshot','revert','commit','snapshots']
if hasattr(parsed_args,'machine'):
parsed_args.dir=find_vm(parsed_args.machine)
parsed_args.sock=connect_vm(parsed_args.dir)
if parsed_args.sock is None:
|