176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
answer=send_command(options.sock,"usb_del %s" % address)
print answer
def cmd_usb_attached(options):
for t in get_host_devices(options.sock):
print "Address %s : %s"%(t[0],t[1])
print answer
def cmd_list(options):
count = 0
search_path=[os.environ['HOME']+"/VWs",
config.get("directories","SharedVMs"),
config.get("directories","AutostartVMs")]
for dirname in search_path:
if not os.access(dirname+"/.",os.X_OK):
|
>
|
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
answer=send_command(options.sock,"usb_del %s" % address)
print answer
def cmd_usb_attached(options):
for t in get_host_devices(options.sock):
print "Address %s : %s"%(t[0],t[1])
print answer
def cmd_list(options):
count = 0
search_path=[os.environ['HOME']+"/VWs",
config.get("directories","SharedVMs"),
config.get("directories","AutostartVMs")]
for dirname in search_path:
if not os.access(dirname+"/.",os.X_OK):
|
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
for f in sorted(vms):
if len(f)==2:
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 validate_size(size):
return re.match('\d+[KMG]',size) is not None
def cmd_create(parsed_args):
if not parsed_args.image and not validate_size(parsed_args.size):
print >>sys.stderr,"Invalid size of disk specifed %s. Should have K, M or G suffix"%parsed_args.size
sys.exit(1)
if not validate_size(parsed_args.mem):
print >>sys.stderr,"Invalid size of memory specifed %s. Should have K, M or G suffix"%parsed_args.size
sys.exit(1)
libdir="/usr/local/lib/vws"
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
281
282
283
284
285
286
287
288
|
for f in sorted(vms):
if len(f)==2:
print "%*s %s" % (-maxlen,f[0],f[1])
else:
print f[0]
if not count:
sys.exit(1)
def cmd_screenshot(options):
from os.path import abspath
filename = abspath(options.filename)
print send_command(options.sock,"screendump "+filename)
def cmd_record(options):
from os.path import abspath
filename = abspath(options.filename)
print send_command(options.sock,"wavcapture "+filename)
def cmd_stoprecord(options):
answer = send_command(options.sock,"info capture")
m=re.search('\[(\d+)\]: ',answer)
if not m:
print >>sys.stderr,"No sound recording in progress"
else:
print send_command(options.sock,"stopcapture "+m.group(1))
def cmd_version(options):
print VERSION
def validate_size(size):
return re.match('\d+[KMG]',size) is not None
template="""#!/bin/sh
# Get machine name from current directory name
NAME=$(basename $(pwd))
# if remote access is enabled, then there should be
# SPICE_PASSWORD=password
QEMU_AUDIO_DRV=spice
export QEMU_AUDIO_DRV
if [ -n "$SPICE_PASSWORD" ]; then
SPICE_AUTH="password=$SPICE_PASSWORD"
else
SPICE_AUTH="disable-ticketing,addr=127.0.0.1"
fi
SPICE_PORT=$(find_free_port 5900)
if [ "$1" = '-cdrom' ]; then
shift
CDROM=",file=$1"
shift
fi
{qemubinary} -name $NAME {accel} \\
-m {memory} \\
{drive} \\
{cdrom}$CDROM \\
{net} \\
{usb} \\
{sound} \\
-chardev socket,server,nowait,path=monitor,id=monitor \\
-mon chardev=monitor,mode=readline \\
-vga {vga} \\
-spice port=$SPICE_PORT,$SPICE_AUTH \\
-device virtio-serial -chardev spicevmc,id=vdagent,name=vdagent \\
-device virtserialport,chardev=vdagent,name=com.redhat.spice.0 \\
-device ich9-usb-ehci1,id=usb \\
-device ich9-usb-uhci1,masterbus=usb.0,firstport=0,multifunction=on \\
-chardev spicevmc,name=usbredir,id=usbredirchardev1 \\
-device usb-redir,chardev=usbredirchardev1,id=usbredirdev1 \\
-daemonize -pidfile pid
"""
def cmd_create(parsed_args):
import os.path
global template
if not parsed_args.image and not validate_size(parsed_args.size):
print >>sys.stderr,"Invalid size of disk specifed %s. Should have K, M or G suffix"%parsed_args.size
sys.exit(1)
if not validate_size(parsed_args.mem):
print >>sys.stderr,"Invalid size of memory specifed %s. Should have K, M or G suffix"%parsed_args.size
sys.exit(1)
libdir="/usr/local/lib/vws"
|
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
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:
|
|
>
>
>
>
>
<
<
|
329
330
331
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
|
if not parsed_args.sound:
options["sound"]=''
else:
options["sound"]='-soundhw '+parsed_args.sound
options["memory"]=parsed_args.mem
if os.path.exists(machinedir):
if os.path.exists(machinedir+"/start"):
print >> sys.stderr,"Virtual Worstation %s already exists"%parsed_args.name
else:
print >> sys.stderr,"Cannot create VW directory, something on the way"
sys.exit(1)
# 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("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:
|
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
return p
#
# arg parsing
#
config=ConfigParser({'SharedVMs':'/var/cache/vws/shared',
'AutoStartVMs':'/var/cache/vws/autostart'})
config.add_section('directories')
config.read(['/etc/vws.conf',os.environ['HOME']+'/.vwsrc'])
args=ArgumentParser()
cmds=args.add_subparsers(dest='command',help="sub-command help")
p=cmds.add_parser("list",help="List existing VWs",description="List existing VWs")
|
|
|
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
return p
#
# arg parsing
#
config=ConfigParser({'SharedVMs':'/var/cache/vws/shared',
'AutoStartVMs':'/var/cache/vws/autostart'})
config.add_section('directories')
config.read(['/etc/vws.conf',os.environ['HOME']+'/.vwsrc'])
args=ArgumentParser()
cmds=args.add_subparsers(dest='command',help="sub-command help")
p=cmds.add_parser("list",help="List existing VWs",description="List existing VWs")
|
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
p=new_command(usb,'remove',help='detach connected usb device')
p.add_argument('pattern',help='Pattern of device name to look up in lsusb')
p.add_argument('--address',type=str,dest='address',nargs=1,help='exact address bus:device')
p=new_command(usb,'attached',help='list devices attached to vm')
usb.add_parser('list',help='list devices available in the host system')
# Snapshot management
p=new_command(cmds,'snapshot',help='Create new snapshot')
p=new_command(cmds,'revert',help='Revert to last snapshot')
p=new_command(cmds,'commit',help='Commit snapshot changes into backing file')
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",metavar='size',help="Size of primary disk images",dest="size",default="20G");
|
>
|
>
|
|
|
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
p=new_command(usb,'remove',help='detach connected usb device')
p.add_argument('pattern',help='Pattern of device name to look up in lsusb')
p.add_argument('--address',type=str,dest='address',nargs=1,help='exact address bus:device')
p=new_command(usb,'attached',help='list devices attached to vm')
usb.add_parser('list',help='list devices available in the host system')
# Snapshot management
p=new_command(cmds,'snapshot',help='Create new snapshot')
p.add_argument('snapname',help='snapshot name')
p=new_command(cmds,'revert',help='Revert to snapshot')
p.add_argument('snapname',help='name of snapshot to revert to')
p=new_command(cmds,'commit',help='Commit snapshot changes into backing file')
p=new_command(cmds,'snapshots',help='List existing snapshots')
# Screenshoits and recording
p=new_command(cmds,'screenshot',help='take a screenshot')
p.add_argument('filename',help='PPM 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",metavar='size',help="Size of primary disk images",dest="size",default="20G");
|