-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_virtualization.sh
More file actions
executable file
·146 lines (134 loc) · 4.67 KB
/
Copy pathdetect_virtualization.sh
File metadata and controls
executable file
·146 lines (134 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
# Color codes
GREEN="\e[1;32m"
BLUE="\e[1;34m"
YELLOW="\e[1;33m"
RED="\e[1;31m"
RESET="\e[0m"
detect_environment() {
local virt=""
local virt_type=""
# --- Check for container environments first ---
if grep -qa 'docker\|lxc\|containerd' /proc/1/cgroup; then
if grep -qa 'docker' /proc/1/cgroup || [ -f /.dockerenv ]; then
echo -e "${YELLOW}Running inside a Docker container${RESET}"
echo "TYPE=container:docker"
return
elif grep -qa 'lxc' /proc/1/cgroup || grep -qa 'container=lxc' /proc/1/environ; then
echo -e "${YELLOW}Running inside an LXC container${RESET}"
echo "TYPE=container:lxc"
return
fi
fi
if [ -f /run/systemd/container ]; then
virt_type=$(cat /run/systemd/container)
echo -e "${YELLOW}Running inside a container: $virt_type${RESET}"
echo "TYPE=container:$virt_type"
return
fi
# --- Check for systemd-detect-virt ---
if command -v systemd-detect-virt &>/dev/null; then
virt=$(systemd-detect-virt)
case "$virt" in
"none")
echo -e "${GREEN}Physical machine${RESET}"
echo "TYPE=physical"
return
;;
"kvm" | "qemu")
echo -e "${BLUE}Virtual machine: KVM/QEMU${RESET}"
echo "TYPE=virtual:kvm"
return
;;
"vmware")
echo -e "${BLUE}Virtual machine: VMware ESXi${RESET}"
echo "TYPE=virtual:vmware"
return
;;
"oracle")
echo -e "${BLUE}Virtual machine: Oracle VirtualBox${RESET}"
echo "TYPE=virtual:virtualbox"
return
;;
"microsoft")
echo -e "${BLUE}Virtual machine: Microsoft Hyper-V${RESET}"
echo "TYPE=virtual:hyperv"
return
;;
"xen")
echo -e "${BLUE}Virtual machine: Xen${RESET}"
echo "TYPE=virtual:xen"
return
;;
"lxc" | "lxc-libvirt")
echo -e "${YELLOW}Running inside an LXC container${RESET}"
echo "TYPE=container:lxc"
return
;;
"docker")
echo -e "${YELLOW}Running inside a Docker container${RESET}"
echo "TYPE=container:docker"
return
;;
*)
echo -e "${BLUE}Virtual/Container detected: $virt${RESET}"
echo "TYPE=virtual:$virt"
return
;;
esac
fi
# --- Fallback: dmidecode detection ---
if command -v dmidecode &>/dev/null; then
local sys_manuf
local sys_product
sys_manuf=$(sudo dmidecode -s system-manufacturer 2>/dev/null)
sys_product=$(sudo dmidecode -s system-product-name 2>/dev/null)
case "$sys_manuf $sys_product" in
*"VMware"*)
echo -e "${BLUE}Virtual machine: VMware ESXi${RESET}"
echo "TYPE=virtual:vmware"
return
;;
*"Microsoft Corporation"*)
echo -e "${BLUE}Virtual machine: Microsoft Hyper-V${RESET}"
echo "TYPE=virtual:hyperv"
return
;;
*"QEMU"*)
echo -e "${BLUE}Virtual machine: QEMU/KVM${RESET}"
echo "TYPE=virtual:kvm"
return
;;
*"Xen"*)
echo -e "${BLUE}Virtual machine: Xen${RESET}"
echo "TYPE=virtual:xen"
return
;;
*"Bochs"*)
echo -e "${BLUE}Virtual machine: Bochs${RESET}"
echo "TYPE=virtual:bochs"
return
;;
esac
# More flexible fallback match
if echo "$sys_manuf $sys_product" | grep -qiE 'ovirt|red hat'; then
echo -e "${BLUE}Virtual machine: oVirt/KVM${RESET}"
echo "TYPE=virtual:ovirt"
return
fi
# Bonus: scan full dmidecode output for known virt strings
local dmidata
dmidata=$(sudo dmidecode 2>/dev/null)
if echo "$dmidata" | grep -qiE 'oVirt|KVM|QEMU'; then
echo -e "${BLUE}Virtual machine: Detected via full DMI scan${RESET}"
echo "TYPE=virtual:ovirt"
return
fi
echo -e "${GREEN}Physical machine${RESET}"
echo "TYPE=physical"
return
fi
echo -e "${RED}Unable to determine environment${RESET}"
echo "TYPE=unknown"
}
detect_environment