From caea9c20ec4c11e8eca0a80f2ed5121a65bbce12 Mon Sep 17 00:00:00 2001 From: jiaweien Date: Sat, 27 Jul 2019 14:19:53 +0800 Subject: [PATCH 1/2] Sometimes cloudbase-init reboot after sethostname when windows setup is running This leads to a Windows corrupt --- cloudbaseinit/osutils/windows.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/cloudbaseinit/osutils/windows.py b/cloudbaseinit/osutils/windows.py index 79cfff7f..b8d6d430 100644 --- a/cloudbaseinit/osutils/windows.py +++ b/cloudbaseinit/osutils/windows.py @@ -35,6 +35,7 @@ import win32security import win32service import winerror +import win32com.client from cloudbaseinit import exception from cloudbaseinit.osutils import base @@ -1007,6 +1008,10 @@ def get_config_value(self, name, section=None): def wait_for_boot_completion(self): try: + strComputer = "." + objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator") + mywmi = objWMIService.ConnectServer(strComputer,"root\cimv2") + wait_setup_flag = 0 with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SYSTEM\\Setup\\Status\\SysprepStatus", 0, winreg.KEY_READ) as key: @@ -1014,10 +1019,29 @@ def wait_for_boot_completion(self): gen_state = winreg.QueryValueEx(key, "GeneralizationState")[0] if gen_state == 7: - break + LOG.info('Sysprep completion. ' + 'GeneralizationState: %d', gen_state) + allProcess = mywmi.ExecQuery("select * from Win32_Process where caption='setup.exe'") + if len(allProcess) > 0: + for i in allProcess: + cmd_line = i.Properties_("CommandLine") + if re.search('oobe' , str(cmd_line).lower()) != None: + wait_setup_flag = 1 + break + if wait_setup_flag: + LOG.info('%s is running , wait it completion' , str(cmd_line)) + else: + LOG.info('Finally , windows initialize setup.exe completion , and wait 5 seconds to complete the other steps!') + time.sleep(5) + return + else: + LOG.info('Windows initialize setup.exe completion , and wait 5 seconds to complete the other steps!') + time.sleep(5) + return + else: + LOG.info('Waiting for sysprep completion. ' + 'GeneralizationState: %d', gen_state) time.sleep(1) - LOG.info('Waiting for sysprep completion. ' - 'GeneralizationState: %d', gen_state) except WindowsError as ex: if ex.winerror == 2: LOG.debug('Sysprep data not found in the registry, ' From b092d3ddc186d4cee9daf0154784c491e7c6b766 Mon Sep 17 00:00:00 2001 From: jiaweien Date: Tue, 31 Dec 2019 12:47:05 +0800 Subject: [PATCH 2/2] modify the judgment condition for is it have setup done --- cloudbaseinit/osutils/windows.py | 36 ++++++-------------------------- 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/cloudbaseinit/osutils/windows.py b/cloudbaseinit/osutils/windows.py index b8d6d430..33fd7f05 100644 --- a/cloudbaseinit/osutils/windows.py +++ b/cloudbaseinit/osutils/windows.py @@ -35,7 +35,6 @@ import win32security import win32service import winerror -import win32com.client from cloudbaseinit import exception from cloudbaseinit.osutils import base @@ -1008,40 +1007,17 @@ def get_config_value(self, name, section=None): def wait_for_boot_completion(self): try: - strComputer = "." - objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator") - mywmi = objWMIService.ConnectServer(strComputer,"root\cimv2") - wait_setup_flag = 0 with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, - "SYSTEM\\Setup\\Status\\SysprepStatus", 0, + "SYSTEM\\Setup\\Status\\ChildCompletion", 0, winreg.KEY_READ) as key: while True: gen_state = winreg.QueryValueEx(key, - "GeneralizationState")[0] - if gen_state == 7: - LOG.info('Sysprep completion. ' - 'GeneralizationState: %d', gen_state) - allProcess = mywmi.ExecQuery("select * from Win32_Process where caption='setup.exe'") - if len(allProcess) > 0: - for i in allProcess: - cmd_line = i.Properties_("CommandLine") - if re.search('oobe' , str(cmd_line).lower()) != None: - wait_setup_flag = 1 - break - if wait_setup_flag: - LOG.info('%s is running , wait it completion' , str(cmd_line)) - else: - LOG.info('Finally , windows initialize setup.exe completion , and wait 5 seconds to complete the other steps!') - time.sleep(5) - return - else: - LOG.info('Windows initialize setup.exe completion , and wait 5 seconds to complete the other steps!') - time.sleep(5) - return - else: - LOG.info('Waiting for sysprep completion. ' - 'GeneralizationState: %d', gen_state) + "SetupFinalTasks")[0] + if gen_state == 3: + break time.sleep(1) + LOG.info('Waiting for sysprep completion. ' + 'SetupFinalTasks: %d', gen_state) except WindowsError as ex: if ex.winerror == 2: LOG.debug('Sysprep data not found in the registry, '