def initvm(image, name=None, multi=False, ramsize=None, vramsize=None, cpus=None): handlers = { "winxp": WindowsXP, "win7x86": Windows7x86, "win7x64": Windows7x64, "win81x86": Windows81x86, "win81x64": Windows81x64, "win10x86": Windows10x86, "win10x64": Windows10x64, } h = handlers[image.osversion] m = None if image.vm == "virtualbox": m = VirtualBox(name=name or image.name) m.create_vm() m.os_type(image.osversion) m.cpus(cpus or image.cpus) m.mouse("usbtablet") m.ramsize(ramsize or image.ramsize) m.vramsize(vramsize or image.vramsize) m.attach_hd(image.path, multi=multi) # Ensure the slot is at least allocated for by an empty drive. m.detach_iso() m.hostonly(nictype=h.nictype, adapter=image.adapter) return m, h
def init(name, winxp, win7x86, win7x64, win81x86, win81x64, win10x86, win10x64, product, vm, iso_mount, serial_key, ip, port, adapter, netmask, gateway, dns, cpus, ramsize, vramsize, tempdir, resolution, vm_visible, debug, verbose): if verbose: log.setLevel(logging.INFO) if debug: log.setLevel(logging.DEBUG) session = Session() image = session.query(Image).filter_by(name=name).first() if image: log.error("Image already exists: %s", name) exit(1) if vm not in VMCLOAK_VM_MODES: log.error( "Only VirtualBox Machinery or iso is supported at this point.") exit(1) if winxp: h = WindowsXP() osversion = "winxp" ramsize = ramsize or 1024 elif win7x86: h = Windows7x86() ramsize = ramsize or 1024 osversion = "win7x86" elif win7x64: h = Windows7x64() ramsize = ramsize or 2048 osversion = "win7x64" elif win81x86: h = Windows81x86() ramsize = ramsize or 2048 osversion = "win81x86" elif win81x64: h = Windows81x64() ramsize = ramsize or 2048 osversion = "win81x64" elif win10x86: h = Windows10x86() ramsize = ramsize or 2048 osversion = "win10x86" elif win10x64: h = Windows10x64() ramsize = ramsize or 2048 osversion = "win10x64" else: log.error("Please provide one of --winxp, --win7x86, --win7x64, " "--win81x86, --win81x64, --win10x86, --win10x64.") exit(1) mount = h.pickmount(iso_mount) if not mount: log.error("Please specify --iso-mount to a directory containing the " "mounted Windows Installer ISO image.") log.info("Refer to the documentation on mounting an .iso image.") exit(1) if not h.set_serial_key(serial_key): exit(1) h.configure(tempdir=tempdir, product=product) reso_width, reso_height = resolution.split("x") settings = dict( GUEST_IP=ip, AGENT_PORT=port, GUEST_MASK=netmask, GUEST_GATEWAY=gateway, DNSSERVER=dns, DEBUG="yes" if debug else "no", RESO_WIDTH=reso_width, RESO_HEIGHT=reso_height, INTERFACE=h.interface, ) bootstrap = tempfile.mkdtemp(dir=tempdir) vmcloak_dir = os.path.join(bootstrap, "vmcloak") os.mkdir(vmcloak_dir) # Write the configuration values for bootstrap.bat. with open(os.path.join(vmcloak_dir, "settings.bat"), "wb") as f: for key, value in settings.items(): print >> f, "set %s=%s" % (key, value) # Download the Python dependency and set it up for bootstrapping the VM. d = Python27(i=Image(osversion=osversion)) d.download() shutil.copy(d.filepath, vmcloak_dir) iso_path = os.path.join(tempdir, "%s.iso" % name) hdd_path = os.path.join(image_path, "%s.vdi" % name) m = VirtualBox(name=name) if not h.buildiso(mount, iso_path, bootstrap, tempdir): shutil.rmtree(bootstrap) exit(1) shutil.rmtree(bootstrap) if vm == "virtualbox": m.create_vm() m.os_type(osversion) m.cpus(cpus) m.mouse("usbtablet") m.ramsize(ramsize) m.vramsize(vramsize) m.create_hd(hdd_path) m.attach_iso(iso_path) m.hostonly(nictype=h.nictype, adapter=adapter) log.info("Starting the Virtual Machine %r to install Windows.", name) m.start_vm(visible=vm_visible) m.wait_for_state(shutdown=True) m.detach_iso() os.unlink(iso_path) m.remove_hd() m.compact_hd(hdd_path) m.delete_vm() else: log.info("You can find your deployment ISO image from : %s" % iso_path) log.info("Added image %r to the repository.", name) session.add( Image(name=name, path=hdd_path, osversion=osversion, servicepack="%s" % h.service_pack, mode="normal", ipaddr=ip, port=port, adapter=adapter, netmask=netmask, gateway=gateway, cpus=cpus, ramsize=ramsize, vramsize=vramsize, vm="%s" % vm)) session.commit()
def init(name, winxp, win7x86, win7x64, win81x86, win81x64, win10x86, win10x64, product, vm, iso_mount, serial_key, ip, port, adapter, netmask, gateway, dns, cpus, ramsize, vramsize, tempdir, resolution, vm_visible, debug, verbose): if verbose: log.setLevel(logging.INFO) if debug: log.setLevel(logging.DEBUG) session = Session() image = session.query(Image).filter_by(name=name).first() if image: log.error("Image already exists: %s", name) exit(1) if vm not in VMCLOAK_VM_MODES: log.error("Only VirtualBox Machinery or iso is supported at this point.") exit(1) if winxp: h = WindowsXP() osversion = "winxp" ramsize = ramsize or 1024 elif win7x86: h = Windows7x86() ramsize = ramsize or 1024 osversion = "win7x86" elif win7x64: h = Windows7x64() ramsize = ramsize or 2048 osversion = "win7x64" elif win81x86: h = Windows81x86() ramsize = ramsize or 2048 osversion = "win81x86" elif win81x64: h = Windows81x64() ramsize = ramsize or 2048 osversion = "win81x64" elif win10x86: h = Windows10x86() ramsize = ramsize or 2048 osversion = "win10x86" elif win10x64: h = Windows10x64() ramsize = ramsize or 2048 osversion = "win10x64" else: log.error( "Please provide one of --winxp, --win7x86, --win7x64, " "--win81x86, --win81x64, --win10x86, --win10x64." ) exit(1) mount = h.pickmount(iso_mount) if not mount: log.error("Please specify --iso-mount to a directory containing the " "mounted Windows Installer ISO image.") log.info("Refer to the documentation on mounting an .iso image.") exit(1) if not h.set_serial_key(serial_key): exit(1) h.configure(tempdir=tempdir, product=product) reso_width, reso_height = resolution.split("x") settings = dict( GUEST_IP=ip, AGENT_PORT=port, GUEST_MASK=netmask, GUEST_GATEWAY=gateway, DNSSERVER=dns, DEBUG="yes" if debug else "no", RESO_WIDTH=reso_width, RESO_HEIGHT=reso_height, INTERFACE=h.interface, ) bootstrap = tempfile.mkdtemp(dir=tempdir) vmcloak_dir = os.path.join(bootstrap, "vmcloak") os.mkdir(vmcloak_dir) # Write the configuration values for bootstrap.bat. with open(os.path.join(vmcloak_dir, "settings.bat"), "wb") as f: for key, value in settings.items(): print>>f, "set %s=%s" % (key, value) # Download the Python dependency and set it up for bootstrapping the VM. d = Python27(i=Image(osversion=osversion)) d.download() shutil.copy(d.filepath, vmcloak_dir) iso_path = os.path.join(tempdir, "%s.iso" % name) hdd_path = os.path.join(image_path, "%s.vdi" % name) m = VirtualBox(name=name) if not h.buildiso(mount, iso_path, bootstrap, tempdir): shutil.rmtree(bootstrap) exit(1) shutil.rmtree(bootstrap) if vm == "virtualbox": m.create_vm() m.os_type(osversion) m.cpus(cpus) m.mouse("usbtablet") m.ramsize(ramsize) m.vramsize(vramsize) m.create_hd(hdd_path) m.attach_iso(iso_path) m.hostonly(nictype=h.nictype, adapter=adapter) log.info("Starting the Virtual Machine %r to install Windows.", name) m.start_vm(visible=vm_visible) m.wait_for_state(shutdown=True) m.detach_iso() os.unlink(iso_path) m.remove_hd() m.compact_hd(hdd_path) m.delete_vm() else: log.info("You can find your deployment ISO image from : %s" % iso_path) log.info("Added image %r to the repository.", name) session.add(Image(name=name, path=hdd_path, osversion=osversion, servicepack="%s" % h.service_pack, mode="normal", ipaddr=ip, port=port, adapter=adapter, netmask=netmask, gateway=gateway, cpus=cpus, ramsize=ramsize, vramsize=vramsize, vm="%s" % vm)) session.commit()