JBR-6769 Make it possible to get info whether IDE is running in a virtual env

Added system property intellij.os.virtualization with possible values
"none", "Xen", "KVM", "VMWare", "HyperV"

(cherry picked from commit 92e4311f13)
This commit is contained in:
Maxim Kartashev
2024-04-08 14:12:59 +04:00
committed by jbrbot
parent c7f12018fb
commit ce6576a7e7
4 changed files with 79 additions and 0 deletions

View File

@@ -418,6 +418,30 @@ void Arguments::init_system_properties() {
os::init_system_properties_values();
}
static const char * get_virtualization_type() {
VirtualizationType vrt = VM_Version::get_detected_virtualization();
if (vrt == XenHVM || vrt == XenPVHVM) {
return "Xen";
} else if (vrt == KVM) {
return "KVM";
} else if (vrt == VMWare) {
return "VMWare";
} else if (vrt == HyperV) {
return "HyperV";
} else if (vrt == HyperVRole) {
// Enables users to create and manage virtual machines (VMs) on a physical host machine,
// but doesn't mean we are running in virtual.
return "none";
}
return "none";
}
void Arguments::post_init_system_properties() {
const char *virtualization_type = get_virtualization_type();
PropertyList_add(&_system_properties, new SystemProperty("intellij.os.virtualization", virtualization_type, false));
}
// Update/Initialize System properties after JDK version number is known
void Arguments::init_version_specific_system_properties() {
enum { bufsz = 16 };

View File

@@ -445,6 +445,8 @@ class Arguments : AllStatic {
// System properties
static void init_system_properties();
static void post_init_system_properties();
// Update/Initialize System properties after JDK version number is known
static void init_version_specific_system_properties();

View File

@@ -34,6 +34,7 @@
#include "prims/downcallLinker.hpp"
#include "prims/jvmtiExport.hpp"
#include "prims/methodHandles.hpp"
#include "runtime/arguments.hpp"
#include "runtime/atomic.hpp"
#include "runtime/continuation.hpp"
#include "runtime/flags/jvmFlag.hpp"
@@ -134,6 +135,8 @@ jint init_globals() {
if (status != JNI_OK)
return status;
Arguments::post_init_system_properties();
#ifdef LEAK_SANITIZER
{
// Register the Java heap with LSan.

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2022-2024 JetBrains s.r.o.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @summary Verifies that the system property describing virtualization
* is present
* @library /test/lib
* @run main DetectVirtualization
*/
public class DetectVirtualization {
static final String VIRT_PROPERTY_NAME = "intellij.os.virtualization";
public static void main(String args[]) {
String virtualization = System.getProperty(VIRT_PROPERTY_NAME);
System.out.println("Detected virtualization: " + virtualization);
switch (virtualization) {
case "Xen":
case "KVM":
case "VMWare":
case "HyperV":
case "none":
break;
default:
throw new RuntimeException(VIRT_PROPERTY_NAME + " has an unexpected value " + virtualization);
}
}
}