bool VerifyRegistrySettingsForWinGA()

in host/AzureRecoveryLib/win32/RecoveryHelpers.cpp [1816:1978]


    bool VerifyRegistrySettingsForWinGA(
        const std::string& srcOSVolume,
        std::stringstream& errorstream)
    {
        TRACE_FUNC_BEGIN;
        bool isSuccess = true;
        try
        {
            /*
                This is a temporary code avoiding loops in effort to avoid Memory Corruption issue.
                This will be refactored before next release to reduce code duplicacy.
            */
            do
            {
                std::vector<std::string> controlSets;
                DWORD lRetStatus = RegGetControlSets(RegistryConstants::VM_SYSTEM_HIVE_NAME, controlSets, std::string(RegistryConstants::VALUE_NAME_LASTKNOWNGOOD));
                if (ERROR_SUCCESS != lRetStatus)
                {
                    TRACE_ERROR("Could not get ControlSets from Source VM system hive.\n");
                    isSuccess = false;
                    break;
                }

                //ArrayLess Implementation
                std::string lastKnownGoodCS = controlSets[0];

                // BEGIN -- WindowsAzureGuestAgent Service check.
                TRACE_INFO("Checking guest agent registry settings for service : %s\n in OS Volume %s\n",
                    ServiceNames::AZURE_GUEST_AGENT,
                    srcOSVolume.c_str());

                std::string winGASubKey = RegistryConstants::VM_SYSTEM_HIVE_NAME +
                    (std::string) DIRECOTRY_SEPERATOR +
                    lastKnownGoodCS +
                    RegistryConstants::SERVICES +
                    (std::string) ServiceNames::AZURE_GUEST_AGENT; // WindowsAzureGuestAgent

                CRegKey winGACurrKey;
                lRetStatus = winGACurrKey.Open(HKEY_LOCAL_MACHINE, winGASubKey.c_str(), KEY_READ);
                if (ERROR_SUCCESS != lRetStatus)
                {
                    TRACE_ERROR("Could not open registry key %s. Error %ld\n",
                        winGASubKey.c_str(),
                        lRetStatus);

                    isSuccess = false;
                    break;
                }

                std::string imagePath = "";
                lRetStatus = RegGetStringValue(winGACurrKey, RegistryConstants::VALUE_IMAGE_PATH, imagePath);
                if (ERROR_SUCCESS != lRetStatus)
                {
                    TRACE_ERROR("Could not read %s value under the key %s. Error %ld\n",
                        RegistryConstants::VALUE_CURRENT_VERSION,
                        winGASubKey.c_str(),
                        lRetStatus);

                    isSuccess = false;
                    break;
                }

                // Image Path will be in reference to paritions on source VM. Replace SourceOS partitions with volume's name on the hydration vm.
                size_t delimiterLocation = imagePath.find("\\");
                if (delimiterLocation != std::string::npos
                    && delimiterLocation == 2)
                {
                    // Format of srcOSVolume is C:\\ so we replace upto \\ and not just before it.
                    imagePath.replace(0, delimiterLocation + 1, srcOSVolume);

                    if (FileExists(imagePath))
                    {
                        // Both registry settings and guest agent files exist.
                        TRACE_INFO("Windows guest agent files exist on source disk. %s \n", imagePath.c_str());
                    }
                    else
                    {
                        TRACE_INFO("Could not find guest agent files on source disk. %s \n", imagePath.c_str());
                        isSuccess = false;
                        break;
                    }
                }
                else
                {
                    TRACE_INFO("Could not find correct image path format for WindowsAzureGuestAgent service. Image path: %s \n", imagePath.c_str());
                    isSuccess = false;
                    break;
                }
                // END -- WindowsAzureGuestAgent Service check.

                // BEGIN -- RdAgent Service check.
                TRACE_INFO("Checking guest agent registry settings for service : %s\n",
                    ServiceNames::AZURE_RDAGENT_SVC);

                winGASubKey = RegistryConstants::VM_SYSTEM_HIVE_NAME +
                    (std::string) DIRECOTRY_SEPERATOR +
                    lastKnownGoodCS +
                    RegistryConstants::SERVICES +
                    (std::string) ServiceNames::AZURE_RDAGENT_SVC; //RdAgent

                lRetStatus = winGACurrKey.Open(HKEY_LOCAL_MACHINE, winGASubKey.c_str(), KEY_READ);
                if (ERROR_SUCCESS != lRetStatus)
                {
                    TRACE_ERROR("Could not open registry key %s. Error %ld\n",
                        winGASubKey.c_str(),
                        lRetStatus);

                    isSuccess = false;
                    break;
                }

                lRetStatus = RegGetStringValue(winGACurrKey, RegistryConstants::VALUE_IMAGE_PATH, imagePath);
                if (ERROR_SUCCESS != lRetStatus)
                {
                    TRACE_ERROR("Could not read %s value under the key %s. Error %ld\n",
                        RegistryConstants::VALUE_CURRENT_VERSION,
                        winGASubKey.c_str(),
                        lRetStatus);

                    isSuccess = false;
                    break;
                }

                // Image Path will be in reference to paritions on source VM. Replace SourceOS partitions with volume's name on the hydration vm.
                delimiterLocation = imagePath.find("\\");
                if (delimiterLocation != std::string::npos
                    && delimiterLocation == 2)
                {
                    // Format of srcOSVolume is C:\\ so we replace upto \\ and not just before it.
                    imagePath.replace(0, delimiterLocation + 1, srcOSVolume);

                    if (FileExists(imagePath))
                    {
                        // Both registry settings and guest agent files exist.
                        TRACE_INFO("Windows guest agent files exist on source disk. %s \n", imagePath.c_str());
                    }
                    else
                    {
                        TRACE_INFO("Could not find guest agent files on source disk. %s \n", imagePath.c_str());
                        isSuccess = false;
                        break;
                    }
                }
                else
                {
                    TRACE_INFO("Could not find correct image path format for RdAgent service. Image path: %s \n", imagePath.c_str());
                    isSuccess = false;
                    break;
                }
                // END -- RdAgent Service check.
            } while (false);
        }
        catch (const std::exception& exp)
        {
            isSuccess = false;
            errorstream << "Could not verify windows guest agent files and settings." << std::endl
                << "Error details: " << exp.what();
            TRACE_ERROR(errorstream.str().c_str());
        }

        TRACE_FUNC_END;
        return isSuccess;
    }