admin管理员组

文章数量:1125492

I want to be able to identify if code is running within Qemu, there is a custom OS image running on QEMU so there are not many third-party utilities available eg. dmidecode to verify the same.

Is there a reliable way to check the above using any linux commands?

pmap/dmidecode both were not identified.

ps -aux did not have any qemu process running as suggested by some users. Currently, I'm out of options.

I want to be able to identify if code is running within Qemu, there is a custom OS image running on QEMU so there are not many third-party utilities available eg. dmidecode to verify the same.

Is there a reliable way to check the above using any linux commands?

pmap/dmidecode both were not identified.

ps -aux did not have any qemu process running as suggested by some users. Currently, I'm out of options.

Share Improve this question asked 2 days ago Tanisha SaxenaTanisha Saxena 112 bronze badges New contributor Tanisha Saxena is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3
  • Try grep -q QEMU /proc/cpuinfo && echo QEMU || echo Not QEMU – Philippe Commented yesterday
  • This is not reliable, I just tried and it gives response as Not QEMU even if it's inside QEMU @Philippe – Tanisha Saxena Commented yesterday
  • Can you run this : grep -qi "QEMU" /sys/class/dmi/id/product_name ? – Philippe Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

Using the C language you could try with:

#include <stdio.h>
#include <string.h>

int isRunningInQemu() {
    FILE *cpuinfo = fopen("/proc/cpuinfo", "r");

    if (cpuinfo == NULL) {
        perror("fopen failure.");
        return 0;
    }

    char line[256];

    while (fgets(line, sizeof(line), cpuinfo)) {
        if (strstr(line, "QEMU")) {
            fclose(cpuinfo);
            return 1;
        }
    }

    fclose(cpuinfo);

    return 0;
}

int main() {

    if (isRunningInQemu()) {
        printf("Running inside QEMU emulator.\n");
    } else {
        printf("Not running inside QEMU emulator.\n");
    }

    return 0;
}

本文标签: linuxHow can I identify if the code is running inside QEMU EmulatorStack Overflow