1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/python
from ConfigParser import ConfigParser
from argparse import ArgumentParser
import fcntl
import socket,select
import errno
import re
import os,sys,time
VERSION=0.2
def find_vm(name):
|
|
|
1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/python
from ConfigParser import ConfigParser
from argparse import ArgumentParser,Namespace
import fcntl
import socket,select
import errno
import re
import os,sys,time
VERSION=0.2
def find_vm(name):
|
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
"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"
|
|
|
|
<
|
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
"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.machine
dirmode = 0755
else:
machinedir = os.environ["HOME"]+"/VWs/"+parsed_args.machine
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"
|
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
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:
start_opts=Namespace(command='start',cdrom=parsed_args.install,stopped=True)
cmd_start(start_opts)
#
# Utility functions for arg parsing
|
|
|
|
>
>
>
>
|
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
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.machine
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 hasattr(parsed_args,"debug") and 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:
start_opts=Namespace(machine=parsed_args.machine,
command='start', cdrom=[parsed_args.install],
dir=machinedir, stopped=True, snapshot=False,
args="", gui=True
)
cmd_start(start_opts)
#
# Utility functions for arg parsing
|