Пример #1
0
        private static void SetInstallStatus(InstallStatus status)
        // Writes value 'status' to 'rootRegKeyName\InstallStatus'
        // For backwards compatibility with 3rd party applications,
        // the same value is also written under the 'XenToolsInstaller'
        // registry keys (both x64/x86)
        {
            const string SOFTWARE      = @"SOFTWARE\";
            const string XTINSTALLER   = @"XCP-ng\XenToolsInstaller";
            const string INSTALLSTATUS = "InstallStatus";
            string       xenToolsKey;

            Trace.WriteLine(
                "Setting \'" + INSTALLSTATUS + "\': \'" + status + "\'"
                );

            installStatus = status;

            using (RegistryKey rootRK =
                       Registry.LocalMachine.CreateSubKey(rootRegKeyName))
            {
                rootRK.SetValue(
                    INSTALLSTATUS,
                    status,
                    RegistryValueKind.String
                    );
            }

            xenToolsKey = SOFTWARE + XTINSTALLER;

            using (RegistryKey rk =
                       Registry.LocalMachine.CreateSubKey(xenToolsKey))
            {
                rk.SetValue(
                    INSTALLSTATUS,
                    status,
                    RegistryValueKind.String
                    );
            }

            if (!WinVersion.Is64BitOS())
            {
                return;
            }

            xenToolsKey = SOFTWARE + @"Wow6432Node\" + XTINSTALLER;

            using (RegistryKey rk =
                       Registry.LocalMachine.CreateSubKey(xenToolsKey))
            {
                rk.SetValue(
                    INSTALLSTATUS,
                    status,
                    RegistryValueKind.String
                    );
            }
        }
Пример #2
0
        public static void Shutdown()
        {
            AcquireSystemPrivilege(SE_SHUTDOWN_NAME);
            WinVersion vers = new WinVersion();

            bool res = InitiateSystemShutdownEx("", "", 0, true, false,
                                                ShutdownReason.SHTDN_REASON_MAJOR_OTHER |
                                                ShutdownReason.SHTDN_REASON_MINOR_ENVIRONMENT |
                                                ShutdownReason.SHTDN_REASON_FLAG_PLANNED);
        }
Пример #3
0
        public static void CleanUpXenLegacy()
        // Cleans up leftover Xen Legacy registry entries and files
        {
            const string SOFTWARE    = "SOFTWARE";
            const string WOW6432NODE = @"\Wow6432Node";
            const string UNINSTALL   =
                @"\Microsoft\Windows\CurrentVersion\Uninstall";
            const string CITRIX_XENTOOLS = @"\XCP-ng\XenTools";
            const string INSTALL_DIR     = @"\Install_Dir";

            string rk1Path;
            string rk2Path;
            string tmpPath;

            if (WinVersion.Is64BitOS())
            {
                rk1Path = SOFTWARE + WOW6432NODE + UNINSTALL;
                tmpPath = SOFTWARE + WOW6432NODE + CITRIX_XENTOOLS;
            }
            else
            {
                rk1Path = SOFTWARE + UNINSTALL;
                tmpPath = SOFTWARE + CITRIX_XENTOOLS;
            }

            rk2Path = tmpPath + INSTALL_DIR;

            try
            {
                Registry.LocalMachine.OpenSubKey(
                    rk1Path,
                    true
                    ).DeleteSubKeyTree("XCP-ng XenTools");
            }
            catch (ArgumentException) { }

            string installDir = (string)Registry.LocalMachine.GetValue(
                rk2Path
                );

            try
            {
                Directory.Delete(installDir, true);
            }
            catch (DirectoryNotFoundException) { }
            catch (ArgumentNullException) { }
        }
Пример #4
0
        private static void PVDriversInstall(out bool needReboot)
        // Installs the set of PV drivers provided
        // by the Management Agent
        {
            string build = WinVersion.Is64BitOS() ? @"\x64\" : @"\x86\";

            string driverRootDir = Path.Combine(
                InstallAgent.exeDir,
                "Drivers"
                );

            var drivers = new[] {
                new { name      = "xennet",
                      installed = Installer.States.XenNetInstalled },
                new { name      = "xenvif",
                      installed = Installer.States.XenVifInstalled },
                new { name      = "xenvbd",
                      installed = Installer.States.XenVbdInstalled },
                new { name      = "xeniface",
                      installed = Installer.States.XenIfaceInstalled },
                new { name      = "xenbus",
                      installed = Installer.States.XenBusInstalled }
            };

            needReboot = false;

            foreach (var driver in drivers)
            {
                if (!Installer.GetFlag(driver.installed))
                {
                    bool   reboot;
                    string infPath = Path.Combine(
                        driverRootDir,
                        driver.name + build + driver.name + ".inf"
                        );

                    Helpers.InstallDriver(driver.name, infPath, out reboot);

                    needReboot |= reboot;

                    Installer.SetFlag(driver.installed);
                }
            }
        }
Пример #5
0
        public string GetRelativeInfPath(WinVersion ver, bool x64)
        {
            if (ver == WinVersion.Win7)
            {
                if (x64)
                {
                    return(String.Format("drivers{0}w7{0}x64{0}fsprot.inf", Path.DirectorySeparatorChar));
                }
                else
                {
                    return(String.Format("drivers{0}w7{0}x86{0}fsprot.inf", Path.DirectorySeparatorChar));
                }
            }

            if (ver == WinVersion.Win8)
            {
                if (x64)
                {
                    return(String.Format("drivers{0}w8{0}x64{0}fsprot.inf", Path.DirectorySeparatorChar));
                }
                else
                {
                    return(String.Format("drivers{0}w8{0}x86{0}fsprot.inf", Path.DirectorySeparatorChar));
                }
            }

            if (ver == WinVersion.Win81)
            {
                if (x64)
                {
                    return(String.Format("drivers{0}w81{0}x64{0}fsprot.inf", Path.DirectorySeparatorChar));
                }
                else
                {
                    return(String.Format("drivers{0}w81{0}x86{0}fsprot.inf", Path.DirectorySeparatorChar));
                }
            }

            return("");
        }
Пример #6
0
        /// <summary>
        /// 返回WinVersion对应的字符串
        /// </summary>
        /// <param name="version">enum WinVersion</param>
        /// <returns></returns>
        public static string ToVersionString(this WinVersion version)
        {
            switch (version)
            {
            case WinVersion.Unknown: return(@"Unknown");

            case WinVersion.Win3: return(@"Win 3.1");

            case WinVersion.WinCE: return(@"Win CE");

            case WinVersion.Win95: return(@"Win95");

            case WinVersion.Win98: return(@"Win98");

            case WinVersion.WinMe: return(@"WinMe");

            case WinVersion.NT3: return(@"NT 3.51");

            case WinVersion.NT4: return(@"NT 4.0");

            case WinVersion.Win2000: return(@"Win2000");

            case WinVersion.WinXP: return(@"WinXP");

            case WinVersion.Win2003: return(@"Win2003");

            case WinVersion.Vista: return(@"Vista/Win2008Server");

            case WinVersion.Win7: return(@"Win7/Win2008Server R2");

            case WinVersion.Win8: return(@"Win8/Win2012Server");

            case WinVersion.Win8point1: return(@"Win8.1/Win2012Server R2");

            case WinVersion.Win10: return(@"Windows 10");

            default:
                throw new ArgumentOutOfRangeException(nameof(version), version, null);
            }
        }
Пример #7
0
        public static void Reboot()
        {
            Trace.WriteLine("OK - shutting down");
            AcquireSystemPrivilege(AdvApi32.SE_SHUTDOWN_NAME);

            if (WinVersion.GetMajorVersion() >= 5 &&
                WinVersion.GetMajorVersion() < 6)
            {
                User32.ExitWindowsEx(
                    User32.ExitFlags.EWX_REBOOT |
                    User32.ExitFlags.EWX_FORCE,
                    0
                    );
            }
            else
            {
                AdvApi32.InitiateSystemShutdownEx(
                    "", "", 0, true, true,
                    AdvApi32.ShtdnReason.MAJOR_OTHER |
                    AdvApi32.ShtdnReason.MINOR_ENVIRONMENT |
                    AdvApi32.ShtdnReason.FLAG_PLANNED
                    );
            }
        }
Пример #8
0
 public static bool isServerSKU()
 {
     WinVersion vers = new WinVersion();
     return (ProductType)vers.GetProductType() != ProductType.NT_WORKSTATION;
 }
Пример #9
0
 public static void winReboot()
 {
     Trace.WriteLine("One last attempt not to shutdown if something is still installing");
     if (!InstallerState.ExistingDriverInstalling)
     {
         setupapi.CMP_WaitNoPendingInstallEvents(setupapi.INFINITE);
     }
     Trace.WriteLine("OK - shutting down");
     AcquireSystemPrivilege(SE_SHUTDOWN_NAME);
     WinVersion vers = new WinVersion();
     if (vers.GetVersionValue() >= 0x500 &&
         vers.GetVersionValue() < 0x600) {
         bool res = ExitWindowsEx(ExitFlags.EWX_REBOOT|
                                  ExitFlags.EWX_FORCE,0);
     }
     else {
         bool res = InitiateSystemShutdownEx("","", 0, true, true,
             ShutdownReason.SHTDN_REASON_MAJOR_OTHER |
             ShutdownReason.SHTDN_REASON_MINOR_ENVIRONMENT |
             ShutdownReason.SHTDN_REASON_FLAG_PLANNED);
     }
 }
Пример #10
0
        private void __InstallThreadHandler()
        {
            if (WinVersion.IsWOW64())
            {
                throw new Exception("WOW64: Do not do that.");
            }

            SetInstallStatus("Installing");

            if (!Installer.GetFlag(Installer.States.NetworkSettingsSaved))
            {
                Trace.WriteLine("NetSettings not saved..");
                XenVif.NetworkSettingsSaveRestore(true);
                Installer.SetFlag(Installer.States.NetworkSettingsSaved);
            }

            while (!Installer.SystemCleaned())
            // use 'while' to abuse the 'break' statement
            {
                if (VM.GetPVToolsVersionOnFirstRun() !=
                    VM.PVToolsVersion.LessThanEight)
                // System clean not needed; flip all relevant flags
                {
                    Installer.SetFlag(Installer.States.RemovedFromFilters);
                    Installer.SetFlag(Installer.States.BootStartDisabled);
                    Installer.SetFlag(Installer.States.MSIsUninstalled);
                    Installer.SetFlag(Installer.States.DrvsAndDevsUninstalled);
                    Installer.SetFlag(Installer.States.CleanedUp);
                    break;
                }

                Trace.WriteLine("PV Tools < 8.x detected; cleaning..");

                if (!PVDriversWipe())
                // Regardless of the 'rebootOption' value, the VM has to
                // reboot after the first 2 actions in PVDriversWipe(),
                // before the new drivers can be installed
                {
                    Trace.WriteLine(
                        "Prevented old drivers from being used after " +
                        "the system reboots. Install Agent will " +
                        "continue after the reboot"
                        );

                    if (rebootOption == RebootType.AUTOREBOOT)
                    {
                        TryReboot();
                    }
                    else // NOREBOOT
                    {
                        SetInstallStatus("NeedsReboot");
                    }

                    return;
                }

                // Enumerate the PCI Bus after
                // cleaning the system
                Device.Enumerate(@"ACPI\PNP0A03", true);

                Trace.WriteLine("Old PV Tools removal complete!");
            }

            if (!Installer.EverythingInstalled())
            {
                InstallCertificates();
                PVDriversInstall();
            }

            if (PVDevice.PVDevice.AllFunctioning())
            {
                Helpers.EnsureBootStartServicesStartAtBoot();

                SetInstallStatus("Installed");
            }
            else
            {
                Helpers.EnsureBootStartServicesStartAtBoot();

                if (rebootOption == RebootType.AUTOREBOOT)
                {
                    TryReboot();
                }
                else
                {
                    SetInstallStatus("NeedsReboot");
                }
            }

            string installStatus = GetInstallStatus();

            if (!installStatus.Equals("Installed") &&
                !installStatus.Equals("Failed"))
            {
                return;
            }

            for (;;)
            {
                if (InformInstallerInitiator(installStatus))
                {
                    Helpers.ChangeServiceStartMode(
                        this.ServiceName,
                        Helpers.ExpandedServiceStartMode.Manual
                        );

                    break;
                }
                Thread.Sleep(15000); // 15 seconds
            }
        }
Пример #11
0
        private void __InstallThreadHandler()
        {
            if (WinVersion.IsWOW64())
            {
                throw new Exception("WOW64: Do not do that.");
            }

            SetInstallStatus(InstallStatus.Installing);

            if (!Installer.GetFlag(Installer.States.DriversRequired))
            {
                if (VM.DriversRequired())
                {
                    Installer.SetFlag(Installer.States.DriversRequired);
                }
            }

            if (!Installer.GetFlag(Installer.States.NetworkSettingsSaved))
            {
                Trace.WriteLine("NetSettings not saved..");
                XenVif.NetworkSettingsSaveRestore(true);
                Installer.SetFlag(Installer.States.NetworkSettingsSaved);
            }

            while (!Installer.SystemCleaned())
            // use 'while' to abuse the 'break' statement
            {
                if (VM.GetPVToolsVersionOnFirstRun() !=
                    VM.PVToolsVersion.LessThanEight)
                // System clean not needed; flip all relevant flags
                {
                    Installer.SetFlag(Installer.States.RemovedFromFilters);
                    Installer.SetFlag(Installer.States.BootStartDisabled);
                    Installer.SetFlag(Installer.States.MSIsUninstalled);
                    Installer.SetFlag(Installer.States.DrvsAndDevsUninstalled);
                    Installer.SetFlag(Installer.States.CleanedUp);
                    break;
                }

                Trace.WriteLine("PV Tools < 8.x detected; cleaning..");

                if (!PVDriversWipe())
                // Regardless of the 'rebootOption' value, the VM has to
                // reboot after the first 2 actions in PVDriversWipe(),
                // before the new drivers can be installed
                {
                    Trace.WriteLine(
                        "Prevented old drivers from being used after " +
                        "the system reboots. Install Agent will " +
                        "continue after the reboot"
                        );

                    goto ExitReboot;
                }

                // Enumerate the PCI Bus after
                // cleaning the system
                Device.Enumerate(@"ACPI\PNP0A03", true);

                Trace.WriteLine("Old PV Tools removal complete!");
            }

            if (!Installer.GetFlag(Installer.States.DriversRequired))
            {
                SetInstallStatus(InstallStatus.Installed);
                goto ExitDone;
            }

            if (!Installer.EverythingInstalled())
            {
                bool needReboot;
                InstallCertificates();
                PVDriversInstall(out needReboot);

                if (needReboot)
                {
                    goto ExitReboot;
                }
            }

            if (PVDevice.PVDevice.AllFunctioning())
            {
                if (!Helpers.BlockUntilNoDriversInstalling(0))
                {
                    Trace.WriteLine("A driver is still installing");
                    while (!(Helpers.BlockUntilNoDriversInstalling(5) || VM.GetOtherDriverInstallingOnFirstRun()))
                    {
                        Trace.WriteLine("Waiting to see if drivers request reboot");
                        if (!PVDevice.PVDevice.AllFunctioning())
                        {
                            Trace.WriteLine("Reboot needed");
                            goto ExitReboot;
                        }
                    }
                    Trace.WriteLine("No reboot needed");
                }
                goto ExitDone;
            }
            else
            {
                goto ExitReboot;
            }

ExitReboot:
            Helpers.EnsureBootStartServicesStartAtBoot();
            this.InstallerReboot();
            return;

ExitDone:
            Helpers.EnsureBootStartServicesStartAtBoot();
            SetInstallStatus(InstallStatus.Installed);
            this.InstallerDone();
        }
Пример #12
0
 public static void winReboot()
 {
     AcquireSystemPrivilege(SE_SHUTDOWN_NAME);
     WinVersion vers = new WinVersion();
     if (vers.GetVersionValue() >= 0x500 &&
         vers.GetVersionValue() < 0x600) {
         bool res = ExitWindowsEx(ExitFlags.EWX_REBOOT|
                                  ExitFlags.EWX_FORCE,0);
     }
     else {
         bool res = InitiateSystemShutdownEx("","", 0, true, true,
             ShutdownReason.SHTDN_REASON_MAJOR_OTHER |
             ShutdownReason.SHTDN_REASON_MINOR_ENVIRONMENT |
             ShutdownReason.SHTDN_REASON_FLAG_PLANNED);
     }
 }
Пример #13
0
 public static void ShutDown()
 {
     Privileges.AcquireSystemPrivilege(SE_SHUTDOWN_NAME);
     WinVersion vers = new WinVersion();
     if (vers.GetVersionValue() >= 0x500 &&
         vers.GetVersionValue() < 0x600)
     {
         bool res = ExitWindowsEx(ExitFlags.EWX_SHUTDOWN |
                                  ExitFlags.EWX_FORCE, 0);
         if (res != true)
         {
             throw new Exception("ExitWindowsEx Failed");
         }
     }
     else
     {
         bool res = InitiateSystemShutdownEx("", "", 0, true, false,
             ShutdownReason.SHTDN_REASON_MAJOR_OTHER |
             ShutdownReason.SHTDN_REASON_MINOR_ENVIRONMENT |
             ShutdownReason.SHTDN_REASON_FLAG_PLANNED);
         if (res != true)
         {
             throw new Exception("InitiateSystemShutdownEx Failed");
         }
     }
 }
Пример #14
0
 public WinInfo(WinVersion windowsVersion)
 {
     this.WindowsVersion = windowsVersion;
 }