def _generateTmpDoWorkBatFile(self, execFile, argList): """execFile and argList should be windows path""" assert not FvmUtil.isWinAbsPath(execFile) buf = '' buf += '@echo off\n' buf += 'setlocal enabledelayedexpansion\n' buf += '\n' if execFile.endswith(".au3"): buf += '"..\\autoit\\autoit3.exe" "%s"' % (execFile) elif execFile.endswith(".bat"): buf += 'call "%s"' % (execFile) elif execFile.endswith(".exe"): buf += '"%s"' % (execFile) else: assert False for a in argList: buf += ' "%s"' % (a) buf += '\n' buf += 'goto :eof\n' buf += '\n' tmpf = os.path.join(self.param.tmpDir, "_do_work.bat") FvmUtil.writeFile(tmpf, buf) return tmpf
def _generateTmpDoWorkBatFile(self, execFile, argList): """execFile and argList should be windows path""" assert not FvmUtil.isWinAbsPath(execFile) buf = '' buf += '@echo off\n' buf += 'setlocal enabledelayedexpansion\n' buf += '\n' if execFile.endswith(".au3"): buf += '"..\\autoit\\autoit3.exe" "%s"' % (execFile) elif execFile.endswith(".bat"): buf += 'call "%s"' % (execFile) elif execFile.endswith(".exe"): buf += '"%s"' % (execFile) else: assert False for a in argList: buf += ' "%s"' % (a) buf += '\n' buf += 'goto :eof\n' buf += '\n' tmpf = os.path.join(self.param.tmpDir, "_do_work.bat") FvmUtil.writeFile(tmpf, buf) return tmpf
def _addQxlDriver(self, mptObj, dstDir): drvDir = os.path.join(self.param.tmpDir, "qxl") FvmUtil.shell('/bin/mkdir "%s"' % (drvDir), "stdout") FvmUtil.shell('/usr/bin/unzip "%s" -d "%s"' % (os.path.join(self.dataDir, "qxl_xp_x86.zip"), drvDir), "stdout") drvSubDir = os.path.join(drvDir, "xp", "x86") for f in os.listdir(drvSubDir): mptObj.addFile(os.path.join(drvSubDir, f), dstDir, True)
def _resetVmEnvironment(self): self.vmObj.setLocalFakeHarddisk("") self.vmObj.setLocalFloppyImage("") self.vmObj.setLocalUsbImage("") self.vmObj.setLocalCdromImage("") self.vmObj.setBootOrder(None) self.vmObj.setNetworkStatus("") FvmUtil.deleteDirContent(self.param.tmpDir)
def _resetVmEnvironment(self): self.vmObj.setLocalFakeHarddisk("") self.vmObj.setLocalFloppyImage("") self.vmObj.setLocalUsbImage("") self.vmObj.setLocalCdromImage("") self.vmObj.setBootOrder(None) self.vmObj.setNetworkStatus("") FvmUtil.deleteDirContent(self.param.tmpDir)
def _getTimezone(self): if FvmUtil.getWinLang(self.osName) == "en_US": return "85" elif FvmUtil.getWinLang(self.osName) == "zh_CN": return "85" elif FvmUtil.getWinLang(self.osName) == "zh_TW": return "85" else: assert False
def _createAssistantFloppy(self): # create floppy file floppyFile = os.path.join(self.param.tmpDir, "floppy.img") FvmUtil.createFormattedFloppy(floppyFile) # add autounattend script uatFile = os.path.join(self.param.tmpDir, "winnt.sif") self._generateUnattendXmlScript(uatFile) FvmUtil.copyToFloppy(floppyFile, uatFile) return floppyFile
def getVmCfgHw(self, param, optList): """minimal requirement: 128M mem, 1.5G disk pci slot allcation: slot 0x04: graphics adapter slot 0x05: sound adapter slot 0x06: network adapter slot 0x07: balloon device slot 0x08: vdi-port device""" osName = optList[0][3:] vmCfgHw = FvmVmFqemuConfigHardware() vmCfgHw.qemuVmType = "pc" vmCfgHw.cpuArch = FvmUtil.getWinArch(osName) vmCfgHw.cpuNumber = 1 vmCfgHw.memorySize = 1024 # fixme if FvmUtil.getWinArch(osName) == "x86": vmCfgHw.mainDiskInterface = "virtio-blk" elif FvmUtil.getWinArch(osName) == "amd64": vmCfgHw.mainDiskInterface = "ide" # winxp amd64 has no virtio block device driver else: assert False vmCfgHw.mainDiskFormat = "raw-sparse" vmCfgHw.mainDiskSize = 30 * 1024 # fixme vmCfgHw.graphicsAdapterInterface = "qxl" # fixme vmCfgHw.graphicsAdapterPciSlot = 0x04 vmCfgHw.soundAdapterInterface = "ac97" # fixme vmCfgHw.soundAdapterPciSlot = 0x05 vmCfgHw.networkAdapterInterface = "virtio" # fixme vmCfgHw.networkAdapterPciSlot = 0x06 vmCfgHw.balloonDeviceSupport = True # fixme vmCfgHw.balloonDevicePciSlot = 0x07 vmCfgHw.vdiPortDeviceSupport = True # fixme vmCfgHw.vdiPortDevicePciSlot = 0x08 vmCfgHw.shareDirectoryNumber = 0 vmCfgHw.shareDirectoryHotplugSupport = False vmCfgHw.shareUsbNumber = 0 vmCfgHw.shareUsbHotplugSupport = False vmCfgHw.shareScsiNumber = 0 vmCfgHw.shareScsiHotplugSupport = False return vmCfgHw
def injectTo(self, mainDiskImage): # write to tmp file tmpf = os.path.join(self.param.tmpDir, "startup.bat") FvmUtil.writeFile(tmpf, self.buf) # inject operation mptObj = WinDiskMountPoint(self.param, mainDiskImage, FvmUtil.getWinLang(self.osName)) try: startupDir = FvmUtil.getWinDir("startup", FvmUtil.getWinLang(self.osName), FvmUtil.getWinUser()) mptObj.addFile(tmpf, startupDir, False) finally: mptObj.umount() os.remove(tmpf)
def doWork(self, param, vmObj, infoPrinter): """do driver install operation""" # prepare parameter self.param = param self.vmObj = vmObj mptObj = WinDiskMountPoint(self.param, self.vmObj.getMainDiskImage(), FvmUtil.getWinLang(self.osName)) try: winreg = WinRegistry(self.param, mptObj.getMountDir()) # Add drivers mptObj.mkdir("Drivers") self._addParaDriver(mptObj, "Drivers") self._addQxlDriver(mptObj, "Drivers") self._addVdagent(mptObj, "Drivers") # Configure driver installation options winreg.addOrModify("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion", "DevicePath", "REG_EXPAND_SZ", "%SystemRoot%\\inf;C:\\Drivers") winreg.addOrModify("HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Windows\\DriverSearching", "DontPromptForWindowsUpdate", "REG_DWORD", 1) winreg.addOrModify("HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Windows\\DriverSearching", "DontSearchWindowsUpdate", "REG_DWORD", 1) winreg.addOrModify("HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Windows\\DriverSearching", "DontSearchFloppies", "REG_DWORD", 1) winreg.addOrModify("HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies\\Microsoft\\Windows\\DriverSearching", "DontSearchCD", "REG_DWORD", 1) finally: mptObj.umount()
def _checkVmCfg(self, vmInfo, osName): if FvmUtil.getWinArch(osName) != vmInfo.vmCfgHw.cpuArch: raise Exception("unmatch Windows architecture and CPU architecture") if vmInfo.vmCfgHw.memorySize < 128: raise Exception("require at least 128MB memory") if vmInfo.vmCfgHw.mainDiskSize < 1500: raise Exception("require at least 1.5GB main disk size")
def _generateUnattendXmlScript(self, uatFile): # read template uatTemplateFile = self._getFile(self.osName, "autounattend") buf = FvmUtil.readFile(uatTemplateFile) # replace content buf = buf.replace("@@timezone@@", self._getTimezone()) buf = buf.replace("@@serial_id@@", self._getSerial()) buf = buf.replace("@@x_resolution@@", "1024") buf = buf.replace("@@y_resolution@@", "768") buf = buf.replace("@@country_code@@", "86") buf = buf.replace("@@area_code@@", "00") buf = buf.replace("@@dialing@@", "Tone") buf = buf.replace("@@language_group@@", "10") buf = buf.replace("@@language@@", "00000804") # write file FvmUtil.writeFile(uatFile, buf)
def _generateTmpConfigIniFile(self, reqList): shutdownFlag = "true" if "rebootAndShutdown" in reqList: shutdownFlag = "false" buf = '' buf += '[config]\n' buf += 'reqList=%s\n' % (" ".join(reqList)) buf += 'winLang=%s\n' % (self._getWinLang()) buf += '\n' buf += '[boot]\n' buf += 'bootNo=0\n' # startup.bat will increase it in every boot process buf += 'bootInfo=\n' buf += 'shutdownFlag=%s\n' % (shutdownFlag) buf += '\n' tmpf = os.path.join(self.param.tmpDir, "config.ini") FvmUtil.writeFile(tmpf, buf) return tmpf
def _generateTmpConfigIniFile(self, reqList): shutdownFlag = "true" if "rebootAndShutdown" in reqList: shutdownFlag = "false" buf = '' buf += '[config]\n' buf += 'reqList=%s\n' % (" ".join(reqList)) buf += 'winLang=%s\n' % (self._getWinLang()) buf += '\n' buf += '[boot]\n' buf += 'bootNo=0\n' # startup.bat will increase it in every boot process buf += 'bootInfo=\n' buf += 'shutdownFlag=%s\n' % (shutdownFlag) buf += '\n' tmpf = os.path.join(self.param.tmpDir, "config.ini") FvmUtil.writeFile(tmpf, buf) return tmpf
def _doJobSetExplorerOption(self, value): optList = ["file-ext:show", "file-ext:hide", "hidden-file:show", "hidden-file:hide", "extra-tip:show", "extra-tip:hide"] valueList = value.split(";") o = FvmUtil.notInList(valueList, optList) if o is not None: raise Exception("invalid option \"%s\"" % (o)) work = FvmWorkOnLineExec() work.setWorkName("Configure explorer option") work.setExecFileInfo(os.path.join(self.dataDir, "cfg-set-explorer-option.au3"), valueList) return work
def _doJobSetDesktopOption(self, value): optList = ["sort-icon:on", "sort-icon:off"] valueList = value.split(";") o = FvmUtil.notInList(valueList, optList) if o is not None: raise Exception("invalid option \"%s\"" % (o)) work = FvmWorkOnLineExec() work.setWorkName("Configure desktop option") work.setExecFileInfo(os.path.join(self.dataDir, "cfg-set-desktop-option.au3"), valueList) return work
def _doJobSetWmpOption(self, value): optList = ["minimize-to-taskbar:on", "minimize-to-taskbar:off"] valueList = value.split(";") o = FvmUtil.notInList(valueList, optList) if o is not None: raise Exception("invalid option \"%s\"" % (o)) work = FvmWorkOnLineExec() work.setWorkName("Configure Windows Media Player") work.setExecFileInfo(os.path.join(self.dataDir, "cfg-set-wmp-option.au3"), [value]) return work
def _doJobSetTaskmgrOption(self, value): optList = ["auto-start:on", "auto-start:off", "hide-when-minimized:on", "hide-when-minimized:off"] valueList = value.split(";") o = FvmUtil.notInList(valueList, optList) if o is not None: raise Exception("invalid option \"%s\"" % (o)) work = FvmWorkOnLineExec() work.setWorkName("Configure Task Manager") work.setExecFileInfo(os.path.join(self.dataDir, "cfg-set-taskmgr-option.au3"), valueList) return work
def _createAndInjectStartupFile(self, batFileList, hasCdrom): """windows path elements in batFileList is relative to usb disk drive root directory""" # create startup file in tmpDir tmpf = os.path.join(self.param.tmpDir, "startup.bat") nbuf = "" if True: lineList = FvmUtil.readFile(os.path.join(self.param.dataDir, "startup.bat.in")).split("\n") tmplBegin = lineList.index("@@execute_template@@") tmplEnd = lineList.index("@@execute_template_end@@") nbuf += "\n".join(lineList[:tmplBegin]) + "\n" tmplBuf = "\n".join(lineList[tmplBegin + 1:tmplEnd]) + "\n" for vsi in batFileList: ntbuf = tmplBuf ntbuf = ntbuf.replace("@@execName@@", vsi) ntbuf = ntbuf.replace("@@execWorkDir@@", FvmUtil.winDirname(vsi)) ntbuf = ntbuf.replace("@@execFile@@", vsi) nbuf += ntbuf nbuf += "\n".join(lineList[tmplEnd + 1:]) if hasCdrom: nbuf = nbuf.replace("@@driverLetter@@", "E:") else: nbuf = nbuf.replace("@@driverLetter@@", "D:") FvmUtil.writeFile(tmpf, nbuf) # inject startup file mptObj = WinDiskMountPoint(self.param, self.vmObj.getMainDiskImage(), self._getWinLang()) try: startupDir = FvmUtil.getWinDir("startup", self._getWinLang(), FvmUtil.getWinUser()) mptObj.addTmpFile(tmpf, startupDir, False) finally: mptObj.umount()
def _doJobSetExplorerSearchOption(self, value): optList = ["balloon-prompt:on", "balloon-prompt:off", "auto-completion:on", "auto-completion:off", "animation:on", "animation:off", "indexing:on", "indexing:off"] valueList = value.split(";") o = FvmUtil.notInList(valueList, optList) if o is not None: raise Exception("invalid option \"%s\"" % (o)) work = FvmWorkOnLineExec() work.setWorkName("Configure explorer search option") work.setExecFileInfo(os.path.join(self.dataDir, "cfg-set-explorer-search-option.au3"), valueList) return work
def _doJobSetStartMenuOption(self, value): optList = ["style:winxp", "style:classic", "auto-hide:on", "auto-hide:off", "inactive-tray-icon:show", "inactive-tray-icon:hide", "group-task:on", "group-task:off", "quick-bar:on", "quick-bar:off", "balloon-tips:on", "balloon-tips:off"] valueList = value.split(";") o = FvmUtil.notInList(valueList, optList) if o is not None: raise Exception("invalid option \"%s\"" % (o)) work = FvmWorkOnLineExec() work.setWorkName("Configure start menu option") work.setExecFileInfo(os.path.join(self.dataDir, "cfg-set-start-menu-option.au3"), valueList) return work
def _addVdagent(self, mptObj, dstDir): drvDir = os.path.join(self.param.tmpDir, "vdagent") FvmUtil.shell('/bin/mkdir "%s"' % (drvDir), "stdout") FvmUtil.shell('/usr/bin/unzip "%s" -d "%s"' % (os.path.join(self.dataDir, "vdagent-win32_20111124.zip"), drvDir), "stdout") if FvmUtil.getWinArch(self.osName) == "x86": drvSubDir = os.path.join(drvDir, "vdagent_x86") elif FvmUtil.getWinArch(self.osName) == "amd64": drvSubDir = os.path.join(drvDir, "vdagent_x64") else: assert False for f in os.listdir(drvSubDir): mptObj.addFile(os.path.join(drvSubDir, f), dstDir, True)
def doInitialConfigure(self, param, vmInfo): self._checkOsPlugin(vmInfo) osName = FvmUtil.getVmOsName(vmInfo) workList = [] work = FvmWorkOnLineExec() work.setWorkName("Enable auto update") work.setExecFileInfo(os.path.join(self.dataDir, "autocfg-enable-auto-update.au3")) workList.append(work) work = FvmWorkOnLineExec() work.setWorkName("Configure input method") work.setExecFileInfo(os.path.join(self.dataDir, "autocfg-init-input-method.au3")) workList.append(work) work = FvmWorkOnLineExec() work.setWorkName("Update windows installer") work.addFile(os.path.join(self.dataDir, "WindowsXP-KB942288-v3-x86.exe"), True) work.setExecFileInfo(os.path.join(self.dataDir, "autocfg-update-windows-installer.au3")) workList.append(work) work = FvmWorkOnLineExec() work.setWorkName("Configure Windows Media Player 9") work.setExecFileInfo(os.path.join(self.dataDir, "autocfg-init-wmp9.au3")) work.setReqList(["noNetwork"]) workList.append(work) work = FvmWorkOnLineExec() work.setWorkName("Install Internet Explorer 8") if osName.endswith(".X86.zh_CN"): work.addFile(os.path.join(self.dataDir, "IE8-WindowsXP-x86-CHS.exe"), True) else: assert False work.setExecFileInfo(os.path.join(self.dataDir, "autocfg-init-ie8.au3")) work.setReqList(["noNetwork", "rebootAndShutdown"]) # noNetwork can quicken the installation # IE8 setup program needs rebooting workList.append(work) return workList
def _addParaDriver(self, mptObj, dstDir): drvDir = os.path.join(self.param.tmpDir, "virtio") FvmUtil.shell('/bin/mkdir "%s"' % (drvDir), "stdout") FvmUtil.shell('/usr/bin/7z x "%s" -o"%s"' % (os.path.join(self.dataDir, "virtio-win-0.1-52.iso"), drvDir), "stdout") if FvmUtil.getWinArch(self.osName) == "x86": drvSubDir = os.path.join(drvDir, "XP", "X86") for f in os.listdir(drvSubDir): mptObj.addFile(os.path.join(drvSubDir, f), dstDir, True) drvSubDir = os.path.join(drvDir, "WXP", "X86") for f in os.listdir(drvSubDir): mptObj.addFile(os.path.join(drvSubDir, f), dstDir, True) elif FvmUtil.getWinArch(self.osName) == "amd64": drvSubDir = os.path.join(drvDir, "XP", "AMD64") for f in os.listdir(drvSubDir): mptObj.addFile(os.path.join(drvSubDir, f), dstDir, True) else: assert False
def _createAndInjectStartupFile(self, batFileList, hasCdrom): """windows path elements in batFileList is relative to usb disk drive root directory""" # create startup file in tmpDir tmpf = os.path.join(self.param.tmpDir, "startup.bat") nbuf = "" if True: lineList = FvmUtil.readFile( os.path.join(self.param.dataDir, "startup.bat.in")).split("\n") tmplBegin = lineList.index("@@execute_template@@") tmplEnd = lineList.index("@@execute_template_end@@") nbuf += "\n".join(lineList[:tmplBegin]) + "\n" tmplBuf = "\n".join(lineList[tmplBegin + 1:tmplEnd]) + "\n" for vsi in batFileList: ntbuf = tmplBuf ntbuf = ntbuf.replace("@@execName@@", vsi) ntbuf = ntbuf.replace("@@execWorkDir@@", FvmUtil.winDirname(vsi)) ntbuf = ntbuf.replace("@@execFile@@", vsi) nbuf += ntbuf nbuf += "\n".join(lineList[tmplEnd + 1:]) if hasCdrom: nbuf = nbuf.replace("@@driverLetter@@", "E:") else: nbuf = nbuf.replace("@@driverLetter@@", "D:") FvmUtil.writeFile(tmpf, nbuf) # inject startup file mptObj = WinDiskMountPoint(self.param, self.vmObj.getMainDiskImage(), self._getWinLang()) try: startupDir = FvmUtil.getWinDir("startup", self._getWinLang(), FvmUtil.getWinUser()) mptObj.addTmpFile(tmpf, startupDir, False) finally: mptObj.umount()
def main(self): FvmUtil.createWinUsbImg("abc.img", 1024, "ntfs")
def _getSerial(self): serialFile = self._getFile(self.osName, "serial") buf = FvmUtil.readFile(serialFile) return buf.split("\n")[0]
def _getWinLang(self): for o in self.vmObj.getVmInfo().vmCfgWin.os.setupOptList: if o.startswith("os="): osName = o[3:] return FvmUtil.getWinLang(osName) assert False
def doWork(self, param, vmObj, infoPrinter): """do driver update operation""" # prepare parameter self.param = param self.vmObj = vmObj # do preparation configuration in main disk image mptObj = WinDiskMountPoint(self.param, self.vmObj.getMainDiskImage(), FvmUtil.getWinLang(self.osName)) try: winreg = WinRegistry(self.param, mptObj.getMountDir()) # add sleep.exe, needed by delay code in startup.bat mptObj.addFile(os.path.join(self.dataDir, "sleep.exe"), "WINDOWS/system32", True) # Dismiss screen check, so it won't disturb us winreg.addOrModify("HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\DontShowMeThisDialogAgain", "ScreenCheck", "REG_SZ", "no") # Dismiss balloon tips, so it won't disturb us winreg.addOrModify("HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced", "EnableBalloonTips", "REG_DWORD", 0) # Disable "AutoPlay & AutoRun", they will disturb some config's autoit script winreg.addOrModify("HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\Explorer", "NoDriveTypeAutoRun", "REG_DWORD", 0xff) finally: mptObj.umount() # boot with usb disk, so it can be the first hardware device recognized usbFile = os.path.join(self.param.tmpDir, "usb.img") if True: FvmUtil.createWinUsbImg(usbFile, 300, "ntfs") mptObj = WinDiskMountPoint(self.param, usbFile, FvmUtil.getWinLang(self.osName)) try: mptObj.addAutoIt() # dismiss start menu show, so it won't disturb us mptObj.addFile(os.path.join(self.dataDir, "autocfg-dismiss-start-menu-show.au3"), "", False) # disable swapfile and snapshot as early as possible mptObj.addFile(os.path.join(self.dataDir, "autocfg-disable-swapfile.au3"), "", False) mptObj.addFile(os.path.join(self.dataDir, "autocfg-disable-snapshot.au3"), "", False) # disable anti-virus check, so it won't disturb us mptObj.addFile(os.path.join(self.dataDir, "autocfg-disable-antivirues-check.au3"), "", False) # disable screen saver, so it won't disturb us in future mptObj.addFile(os.path.join(self.dataDir, "autocfg-disable-screensaver.au3"), "", False) # disable desktop cleanup wizard mptObj.addFile(os.path.join(self.dataDir, "autocfg-disable-desktop-cleanup-wizard.au3"), "", False) # dismiss explore xp, so it won't disturb us # it can't be dismissed by directly modify the registry, don't know why mptObj.addFile(os.path.join(self.dataDir, "autocfg-dismiss-explore-winxp.au3"), "", False) finally: mptObj.umount() self.vmObj.setLocalUsbImage(usbFile) # create and inject startup file sFileList = ["autocfg-dismiss-start-menu-show.au3", "autocfg-disable-swapfile.au3", "autocfg-disable-snapshot.au3", "autocfg-disable-antivirues-check.au3", "autocfg-disable-screensaver.au3", "autocfg-disable-desktop-cleanup-wizard.au3", "autocfg-dismiss-explore-winxp.au3"] msf = MyStartupFile(self.param, self.osName, sFileList) msf.injectTo(self.vmObj.getMainDiskImage()) # run virtual machine # this boot will also dimiss the start menu show, so it won't disturb us in future self.vmObj.run()
def doWork(self, param, vmObj, infoPrinter): """do driver update operation""" # prepare parameter self.param = param self.vmObj = vmObj # create assistant usb disk usbFile = os.path.join(self.param.tmpDir, "usb.img") if True: FvmUtil.createWinUsbImg(usbFile, 300, "ntfs") mptObj = WinDiskMountPoint(self.param, usbFile, FvmUtil.getWinLang(self.osName)) try: mptObj.addAutoIt() mptObj.addFile(os.path.join(self.dataDir, "autocfg-update-driver.au3"), "", False) mptObj.addFile(os.path.join(self.dataDir, "autocfg-enable-vdagent.au3"), "", False) finally: mptObj.umount() self.vmObj.setLocalUsbImage(usbFile) # update virtio harddisk driver if self.vmObj.getVmInfo().vmCfgHw.mainDiskInterface != "ide": # create and inject startup file sFileList = ["autocfg-update-driver.au3", "autocfg-enable-vdagent.au3"] msf = MyStartupFile(self.param, self.osName, sFileList) msf.injectTo(self.vmObj.getMainDiskImage()) # run virtual machine self.vmObj.setLocalFakeHarddisk(self.vmObj.getVmInfo().vmCfgHw.mainDiskInterface) self.vmObj.run() self.vmObj.setLocalFakeHarddisk("") # update drivers if True: # create and inject startup.bat to disk-main.img sFileList = ["autocfg-update-driver.au3", # VirtIO SCSI driver "autocfg-update-driver.au3", # VirtIO Balloon driver "autocfg-update-driver.au3", # VirtIO Serial driver "autocfg-update-driver.au3"] # Red Hat QXL GPU driver msf = MyStartupFile(self.param, self.osName, sFileList) msf.injectTo(self.vmObj.getMainDiskImage()) # run virtual machine self.vmObj.setSetupMode(False) self.vmObj.setNetworkStatus("isolate") self.vmObj.run() self.vmObj.setNetworkStatus("") # update virtio network driver if self.vmObj.getVmInfo().vmCfgHw.networkAdapterInterface != "user": # create and inject startup.bat to disk-main.img sFileList = ["autocfg-update-driver.au3"] # VirtIO Network driver msf = MyStartupFile(self.param, self.osName, sFileList) msf.injectTo(self.vmObj.getMainDiskImage()) # run virtual machine self.vmObj.setNetworkStatus("virtio-dummy") self.vmObj.run() self.vmObj.setNetworkStatus("")
def _executeWorkListOnLine(self, workList, startIndex): """usb.img directory structure: [usb.img] |--config.ini |--autoit |--autoit files |--work1 |--_do_work.bat |--xxxx.au3 |--work2 |--_do_work.bat |--xxxx.au3 |--other files |--work3 |--_do_work.bat |--xxxx.bat |--other files |--work4 |--_do_work.bat |--xxxx.exe config.ini format: [config] winLang = "xxxx" reqList = "xx1 xx2" [boot] bootNo = 1 bootInfo = "xxxx" shutdownFlag = false""" self.infoPrinter.printInfo(">> Doing on-line operation...") self.infoPrinter.incIndent() # prepare usb disk usbFile = os.path.join(self.param.tmpDir, "usb.img") FvmUtil.createWinUsbImg(usbFile, 1024, "ntfs") mptObj = WinDiskMountPoint(self.param, usbFile, self._getWinLang()) # copy files to usb disk batFileList = [] reqList = None workCount = 0 try: mptObj.addAutoIt() mptObj.addSevenZip() # process work i = 0 while (startIndex + i) < len(workList): w = workList[startIndex + i] if not self._isWorkOnLine(w): break if self._mergeReqList(reqList, w) is None: break # create work-dir workDir = "work%d" % (i) mptObj.mkdir(workDir) if isinstance(w, FvmWorkOnLineFree): # fill usb disk absWorkDir = os.path.join(mptObj.getMountDir(), workDir) w.fillUsbDataDir(self.param, absWorkDir) # generate _do_work.bat (execFile, argList) = w.getCmdLine() tmpf = self._generateTmpDoWorkBatFile(execFile, argList) mptObj.addTmpFile(tmpf, workDir, False) elif isinstance(w, FvmWorkOnLineExec): # fill usb disk for fname, isBinary in w.fileList: mptObj.addFile(fname, workDir, isBinary) for fname, isBinary in w.tmpFileList: mptObj.addTmpFile(fname, workDir, isBinary) for fname in w.zipFileList: d = os.path.join(workDir, os.path.splitext(os.path.basename(fname))[0]) mptObj.mkdir(d) mptObj.addZipFile(fname, d) if True: isBinary = w.execFile.endswith(".exe") mptObj.addFile(w.execFile, workDir, isBinary) # generate _do_work.bat tmpf = self._generateTmpDoWorkBatFile(os.path.basename(w.execFile), w.argList) mptObj.addTmpFile(tmpf, workDir, False) else: assert False reqList = self._mergeReqList(reqList, w) batFileList.append("%s\\_do_work.bat" % (os.path.basename(workDir))) i = i + 1 # get workCount workCount = i assert workCount > 0 # add config.ini tmpf = self._generateTmpConfigIniFile(reqList) mptObj.addTmpFile(tmpf, "", False) finally: mptObj.umount() for i in range(0, workCount): self.infoPrinter.printInfo(">> %s" % (workList[startIndex + i].workName)) # pre-run virtual machine cdromf = self._getCdromFileFromReqList(reqList) if cdromf is not None: self.vmObj.setLocalCdromImage(cdromf) self.vmObj.setLocalUsbImage(usbFile) if "noNetwork" in reqList: self.vmObj.setNetworkStatus("none") elif "network" not in reqList: self.vmObj.setNetworkStatus("isolate") # run virtual machine FvmUtil.winPauseAutoActivity(self.param, self.vmObj.getMainDiskImage(), self._getWinLang()) self._createAndInjectStartupFile(batFileList, (cdromf is not None)) self.vmObj.run() FvmUtil.winResumeAutoActivity(self.param, self.vmObj.getMainDiskImage(), self._getWinLang()) self.infoPrinter.decIndent() self._resetVmEnvironment() return workCount
def doCreate(self): """create new virtual machine and do an unattended os setup""" self.args.vmdir = os.path.abspath(self.args.path) if os.path.exists(self.args.vmdir): raise Exception("target virtual machine directory already exists") pObj = self.getPluginByOsName(self.pluginList, self.args.os) if pObj is None: raise Exception("the specified operating system is not supported") try: # create virt-machine self.infoPrinter.printInfo("Creating virtual machine in directory \"%s\"..." % (self.args.vmdir)) self.infoPrinter.incIndent() vmCfgBasic = self._getVmCfgBasicByArgs(self.args) vmCfgHw = pObj.getVmCfgHw(self.param, self.args.popt) # fixme: should return hardware spec vmCfgBasic.checkValid() vmCfgHw.checkValid() vb = FvmVmFqemuBuilder(self.param) vb.createVm(self.args.vmdir, vmCfgBasic, vmCfgHw) self.infoPrinter.decIndent() self.infoPrinter.printInfo("Complete!") # open vmObj and run os setup self.infoPrinter.printInfo("Doing \"%s\" setup..." % (pObj.getOsName())) self.infoPrinter.incIndent() vmObj = FvmVmFqemuObject(self.param, self.args.vmdir) self.param.tmpDir = os.path.join(self.args.vmdir, "temp") try: vmObj.lock() vmObj.setShowUi(self.args.showui) FvmUtil.mkDirAndClear(self.param.tmpDir) # execute os setup work workList = pObj.doSetup(self.param, vmObj.getVmInfo(), self.args.popt) for w in workList: assert isinstance(w, FvmWorkFullControl) wlExec = FvmWorkListExecutor(self.param, vmObj, self.infoPrinter) wlExec.executeWorkList(workList) assert not vmObj.getSetupMode() self._changeVmCfgWinForOsSetup(vmObj, self.args.plugin, self.args.popt) # execute initial configure work workList = pObj.doInitialConfigure(self.param, vmObj.getVmInfo()) wlExec = FvmWorkListExecutor(self.param, vmObj, self.infoPrinter) wlExec.executeWorkList(workList) finally: if not self.args.keep: FvmUtil.forceDelete(self.param.tmpDir) self.param.tmpDir = "" vmObj.unlock() self.infoPrinter.decIndent() self.infoPrinter.printInfo("Complete!") except: if not self.args.keep: if os.path.exists(self.args.vmdir): FvmUtil.forceDelete(self.args.vmdir) raise
def main(self): FvmUtil.createWinUsbImg("abc.img", 1024, "ntfs")
def _executeWorkListOnLine(self, workList, startIndex): """usb.img directory structure: [usb.img] |--config.ini |--autoit |--autoit files |--work1 |--_do_work.bat |--xxxx.au3 |--work2 |--_do_work.bat |--xxxx.au3 |--other files |--work3 |--_do_work.bat |--xxxx.bat |--other files |--work4 |--_do_work.bat |--xxxx.exe config.ini format: [config] winLang = "xxxx" reqList = "xx1 xx2" [boot] bootNo = 1 bootInfo = "xxxx" shutdownFlag = false""" self.infoPrinter.printInfo(">> Doing on-line operation...") self.infoPrinter.incIndent() # prepare usb disk usbFile = os.path.join(self.param.tmpDir, "usb.img") FvmUtil.createWinUsbImg(usbFile, 1024, "ntfs") mptObj = WinDiskMountPoint(self.param, usbFile, self._getWinLang()) # copy files to usb disk batFileList = [] reqList = None workCount = 0 try: mptObj.addAutoIt() mptObj.addSevenZip() # process work i = 0 while (startIndex + i) < len(workList): w = workList[startIndex + i] if not self._isWorkOnLine(w): break if self._mergeReqList(reqList, w) is None: break # create work-dir workDir = "work%d" % (i) mptObj.mkdir(workDir) if isinstance(w, FvmWorkOnLineFree): # fill usb disk absWorkDir = os.path.join(mptObj.getMountDir(), workDir) w.fillUsbDataDir(self.param, absWorkDir) # generate _do_work.bat (execFile, argList) = w.getCmdLine() tmpf = self._generateTmpDoWorkBatFile(execFile, argList) mptObj.addTmpFile(tmpf, workDir, False) elif isinstance(w, FvmWorkOnLineExec): # fill usb disk for fname, isBinary in w.fileList: mptObj.addFile(fname, workDir, isBinary) for fname, isBinary in w.tmpFileList: mptObj.addTmpFile(fname, workDir, isBinary) for fname in w.zipFileList: d = os.path.join( workDir, os.path.splitext(os.path.basename(fname))[0]) mptObj.mkdir(d) mptObj.addZipFile(fname, d) if True: isBinary = w.execFile.endswith(".exe") mptObj.addFile(w.execFile, workDir, isBinary) # generate _do_work.bat tmpf = self._generateTmpDoWorkBatFile( os.path.basename(w.execFile), w.argList) mptObj.addTmpFile(tmpf, workDir, False) else: assert False reqList = self._mergeReqList(reqList, w) batFileList.append("%s\\_do_work.bat" % (os.path.basename(workDir))) i = i + 1 # get workCount workCount = i assert workCount > 0 # add config.ini tmpf = self._generateTmpConfigIniFile(reqList) mptObj.addTmpFile(tmpf, "", False) finally: mptObj.umount() for i in range(0, workCount): self.infoPrinter.printInfo(">> %s" % (workList[startIndex + i].workName)) # pre-run virtual machine cdromf = self._getCdromFileFromReqList(reqList) if cdromf is not None: self.vmObj.setLocalCdromImage(cdromf) self.vmObj.setLocalUsbImage(usbFile) if "noNetwork" in reqList: self.vmObj.setNetworkStatus("none") elif "network" not in reqList: self.vmObj.setNetworkStatus("isolate") # run virtual machine FvmUtil.winPauseAutoActivity(self.param, self.vmObj.getMainDiskImage(), self._getWinLang()) self._createAndInjectStartupFile(batFileList, (cdromf is not None)) self.vmObj.run() FvmUtil.winResumeAutoActivity(self.param, self.vmObj.getMainDiskImage(), self._getWinLang()) self.infoPrinter.decIndent() self._resetVmEnvironment() return workCount
def _getWinLang(self): for o in self.vmObj.getVmInfo().vmCfgWin.os.setupOptList: if o.startswith("os="): osName = o[3:] return FvmUtil.getWinLang(osName) assert False