Fred808 commited on
Commit
2f582aa
·
verified ·
1 Parent(s): 741d4db

Upload 2 files

Browse files
Files changed (2) hide show
  1. Dockerfile +30 -0
  2. detect_hypervisor.sh +70 -0
Dockerfile ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a minimal base image for a small footprint
2
+ FROM alpine:latest
3
+
4
+ # Install necessary tools. 'dmesg' is usually available, but 'grep' is essential.
5
+ # We also install 'util-linux' for 'dmesg' in case it's not in the base image,
6
+ # and 'coreutils' for general shell utilities.
7
+ RUN apk update && apk add --no-cache bash grep coreutils util-linux
8
+
9
+ # Set the working directory
10
+ WORKDIR /app
11
+
12
+ # Copy the detection script into the container
13
+ COPY detect_hypervisor.sh .
14
+
15
+ # Make the script executable
16
+ RUN chmod +x detect_hypervisor.sh
17
+
18
+ # Command to run the script when the container starts
19
+ # The script needs to be run with the necessary privileges to access /sys and dmesg.
20
+ # When running the container, the user will need to use '--privileged' or
21
+ # map the necessary devices/directories, but for a simple check, we'll
22
+ # rely on the host's environment when the container is run with the right flags.
23
+ # For the purpose of this Dockerfile, we just set the entrypoint.
24
+ ENTRYPOINT ["/app/detect_hypervisor.sh"]
25
+
26
+ # Note: The script relies on reading system files like /sys/hypervisor/microsoft
27
+ # and the output of 'dmesg'. To make this work inside a Docker container,
28
+ # the container must be run with specific flags, typically:
29
+ # docker run --rm -v /sys:/sys:ro -v /dev/kmsg:/dev/kmsg:ro --privileged <image_name>
30
+ # I will include these instructions in the final delivery.
detect_hypervisor.sh ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Function to check for Hyper-V
4
+ check_hyperv() {
5
+ # Check for the presence of the Hyper-V specific file system entry
6
+ if [ -d "/sys/hypervisor/microsoft" ]; then
7
+ echo "Hypervisor: Hyper-V"
8
+ return 0
9
+ fi
10
+
11
+ # Fallback check using dmesg for "Hyper-V"
12
+ if dmesg | grep -qi "Hyper-V"; then
13
+ echo "Hypervisor: Hyper-V (via dmesg)"
14
+ return 0
15
+ fi
16
+
17
+ # Fallback check using dmidecode (requires dmidecode to be installed and run with sudo/root)
18
+ # Since we are in a Docker container, we'll assume a minimal environment,
19
+ # so we'll rely on the /sys check first.
20
+ # if sudo dmidecode -s system-product-name | grep -qi "Virtual Machine"; then
21
+ # if sudo dmidecode -s system-manufacturer | grep -qi "Microsoft Corporation"; then
22
+ # echo "Hypervisor: Hyper-V (via dmidecode)"
23
+ # return 0
24
+ # fi
25
+ # fi
26
+
27
+ return 1
28
+ }
29
+
30
+ # Function to check for KVM
31
+ check_kvm() {
32
+ # Check for the presence of the KVM device file
33
+ if [ -c "/dev/kvm" ]; then
34
+ echo "Hypervisor: KVM (via /dev/kvm)"
35
+ return 0
36
+ fi
37
+
38
+ # Check for KVM in the system product name
39
+ if [ -f "/sys/class/dmi/id/product_name" ] && grep -qi "KVM" "/sys/class/dmi/id/product_name"; then
40
+ echo "Hypervisor: KVM"
41
+ return 0
42
+ fi
43
+
44
+ # Fallback check using dmesg for "KVM"
45
+ if dmesg | grep -qi "KVM"; then
46
+ echo "Hypervisor: KVM (via dmesg)"
47
+ return 0
48
+ fi
49
+
50
+ return 1
51
+ }
52
+
53
+ # Main detection logic
54
+ if check_hyperv; then
55
+ exit 0
56
+ elif check_kvm; then
57
+ exit 0
58
+ else
59
+ # Check for other common hypervisors as a bonus
60
+ if [ -f "/sys/class/dmi/id/product_name" ] && grep -qi "VMware" "/sys/class/dmi/id/product_name"; then
61
+ echo "Hypervisor: VMware"
62
+ elif [ -f "/sys/class/dmi/id/product_name" ] && grep -qi "VirtualBox" "/sys/class/dmi/id/product_name"; then
63
+ echo "Hypervisor: VirtualBox"
64
+ elif [ -f "/sys/class/dmi/id/product_name" ] && grep -qi "QEMU" "/sys/class/dmi/id/product_name"; then
65
+ echo "Hypervisor: QEMU"
66
+ else
67
+ echo "Hypervisor: Unknown or Bare Metal"
68
+ fi
69
+ exit 1
70
+ fi