Esempio n. 1
0
static int condition_test_virtualization(Condition *c) {
        int b, v;
        const char *id;

        assert(c);
        assert(c->parameter);
        assert(c->type == CONDITION_VIRTUALIZATION);

        v = detect_virtualization(&id);
        if (v < 0)
                return v;

        /* First, compare with yes/no */
        b = parse_boolean(c->parameter);

        if (v > 0 && b > 0)
                return true;

        if (v == 0 && b == 0)
                return true;

        /* Then, compare categorization */
        if (v == VIRTUALIZATION_VM && streq(c->parameter, "vm"))
                return true;

        if (v == VIRTUALIZATION_CONTAINER && streq(c->parameter, "container"))
                return true;

        /* Finally compare id */
        return v > 0 && streq(c->parameter, id);
}
Esempio n. 2
0
static int condition_test_virtualization(Condition *c) {
        int b, v;

        assert(c);
        assert(c->parameter);
        assert(c->type == CONDITION_VIRTUALIZATION);

        if (streq(c->parameter, "private-users"))
                return running_in_userns();

        v = detect_virtualization();
        if (v < 0)
                return v;

        /* First, compare with yes/no */
        b = parse_boolean(c->parameter);
        if (b >= 0)
                return b == !!v;

        /* Then, compare categorization */
        if (streq(c->parameter, "vm"))
                return VIRTUALIZATION_IS_VM(v);

        if (streq(c->parameter, "container"))
                return VIRTUALIZATION_IS_CONTAINER(v);

        /* Finally compare id */
        return v != VIRTUALIZATION_NONE && streq(c->parameter, virtualization_to_string(v));
}
Esempio n. 3
0
static int condition_test_virtualization(Condition *c) {
        int b, v;

        assert(c);
        assert(c->parameter);
        assert(c->type == CONDITION_VIRTUALIZATION);

        v = detect_virtualization();
        if (v < 0)
                return v;

        /* First, compare with yes/no */
        b = parse_boolean(c->parameter);

        if (v > 0 && b > 0)
                return true;

        if (v == 0 && b == 0)
                return true;

        /* Then, compare categorization */
        if (VIRTUALIZATION_IS_VM(v) && streq(c->parameter, "vm"))
                return true;

        if (VIRTUALIZATION_IS_CONTAINER(v) && streq(c->parameter, "container"))
                return true;

        /* Finally compare id */
        return v != VIRTUALIZATION_NONE && streq(c->parameter, virtualization_to_string(v));
}
Esempio n. 4
0
int main(int argc, char *argv[]) {
        int a, v;

        v = detect_virtualization();
        if (v == -EPERM || v == -EACCES)
                return EXIT_TEST_SKIP;

        assert_se(v >= 0);

        log_info("virtualization=%s id=%s",
                 VIRTUALIZATION_IS_CONTAINER(v) ? "container" :
                 VIRTUALIZATION_IS_VM(v)        ? "vm" : "n/a",
                 virtualization_to_string(v));

        a = uname_architecture();
        assert_se(a >= 0);

        log_info("uname architecture=%s", architecture_to_string(a));

        a = native_architecture();
        assert_se(a >= 0);

        log_info("native architecture=%s", architecture_to_string(a));

        log_info("primary library architecture=" LIB_ARCH_TUPLE);

        return 0;
}
Esempio n. 5
0
int main(int argc, char *argv[]) {
        const char *id = NULL;
        int retval = EXIT_SUCCESS;
        int r;

        /* This is mostly intended to be used for scripts which want
         * to detect whether we are being run in a virtualized
         * environment or not */

        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r <= 0)
                return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;

        switch (arg_mode) {

        case ANY_VIRTUALIZATION: {
                int v;

                v = detect_virtualization(&id);
                if (v < 0) {
                        log_error("Failed to check for virtualization: %s", strerror(-v));
                        return EXIT_FAILURE;
                }

                retval = v != VIRTUALIZATION_NONE ? EXIT_SUCCESS : EXIT_FAILURE;
                break;
        }

        case ONLY_CONTAINER:
                r = detect_container(&id);
                if (r < 0) {
                        log_error("Failed to check for container: %s", strerror(-r));
                        return EXIT_FAILURE;
                }

                retval = r > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
                break;

        case ONLY_VM:
                r = detect_vm(&id);
                if (r < 0) {
                        log_error("Failed to check for vm: %s", strerror(-r));
                        return EXIT_FAILURE;
                }

                retval = r > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
                break;
        }

        if (!arg_quiet)
                puts(id ? id : "none");

        return retval;
}
Esempio n. 6
0
static void print_status_info(StatusInfo *i) {
        sd_id128_t mid, bid;
        int r;
        const char *id = NULL;
        _cleanup_free_ char *pretty_name = NULL, *cpe_name = NULL;
        struct utsname u;

        assert(i);

        printf("   Static hostname: %s\n",
               strna(i->static_hostname));

        if (!isempty(i->pretty_hostname) &&
            !streq_ptr(i->pretty_hostname, i->static_hostname))
                printf("   Pretty hostname: %s\n",
                       strna(i->pretty_hostname));

        if (!isempty(i->hostname) &&
            !streq_ptr(i->hostname, i->static_hostname))
                printf("Transient hostname: %s\n",
                       strna(i->hostname));

        printf("         Icon name: %s\n"
               "           Chassis: %s\n",
               strna(i->icon_name),
               strna(i->chassis));

        r = sd_id128_get_machine(&mid);
        if (r >= 0)
                printf("        Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(mid));

        r = sd_id128_get_boot(&bid);
        if (r >= 0)
                printf("           Boot ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(bid));

        if (detect_virtualization(&id) > 0)
                printf("    Virtualization: %s\n", id);

        r = parse_env_file("/etc/os-release", NEWLINE,
                           "PRETTY_NAME", &pretty_name,
                           "CPE_NAME", &cpe_name,
                           NULL);

        if (!isempty(pretty_name))
                printf("  Operating System: %s\n", pretty_name);

        if (!isempty(cpe_name))
                printf("       CPE OS Name: %s\n", cpe_name);

        assert_se(uname(&u) >= 0);
        printf("            Kernel: %s %s\n"
               "      Architecture: %s\n", u.sysname, u.release, u.machine);

}
Esempio n. 7
0
int main(int argc, char *argv[]) {
        int a, v;
        const char *p;

        assert_se(architecture_from_string("") < 0);
        assert_se(architecture_from_string(NULL) < 0);
        assert_se(architecture_from_string("hoge") < 0);
        assert_se(architecture_to_string(-1) == NULL);
        assert_se(architecture_from_string(architecture_to_string(0)) == 0);
        assert_se(architecture_from_string(architecture_to_string(1)) == 1);

        v = detect_virtualization();
        if (IN_SET(v, -EPERM, -EACCES))
                return EXIT_TEST_SKIP;

        assert_se(v >= 0);

        log_info("virtualization=%s id=%s",
                 VIRTUALIZATION_IS_CONTAINER(v) ? "container" :
                 VIRTUALIZATION_IS_VM(v)        ? "vm" : "n/a",
                 virtualization_to_string(v));

        a = uname_architecture();
        assert_se(a >= 0);

        p = architecture_to_string(a);
        assert_se(p);
        log_info("uname architecture=%s", p);
        assert_se(architecture_from_string(p) == a);

        a = native_architecture();
        assert_se(a >= 0);

        p = architecture_to_string(a);
        assert_se(p);
        log_info("native architecture=%s", p);
        assert_se(architecture_from_string(p) == a);

        log_info("primary library architecture=" LIB_ARCH_TUPLE);

        return 0;
}
Esempio n. 8
0
static bool test_kernel_command_line(const char *parameter) {
        char *line, *w, *state, *word = NULL;
        bool equal;
        int r;
        size_t l, pl;
        bool found = false;

        assert(parameter);

        if (detect_virtualization(NULL) > 0)
                return false;

        if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
                log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
                return false;
        }

        equal = !!strchr(parameter, '=');
        pl = strlen(parameter);

        FOREACH_WORD_QUOTED(w, l, line, state) {

                free(word);
                if (!(word = strndup(w, l)))
                        break;

                if (equal) {
                        if (streq(word, parameter)) {
                                found = true;
                                break;
                        }
                } else {
                        if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
                                found = true;
                                break;
                        }
                }

        }
Esempio n. 9
0
static const char* fallback_chassis(void) {
    int r;
    char *type;
    unsigned t;
    int v;

    v = detect_virtualization();

    if (VIRTUALIZATION_IS_VM(v))
        return "vm";
    if (VIRTUALIZATION_IS_CONTAINER(v))
        return "container";

    r = read_one_line_file("/sys/firmware/acpi/pm_profile", &type);
    if (r < 0)
        goto try_dmi;

    r = safe_atou(type, &t);
    free(type);
    if (r < 0)
        goto try_dmi;

    /* We only list the really obvious cases here as the ACPI data
     * is not really super reliable.
     *
     * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
     *
     * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
     */

    switch(t) {

    case 1:
    case 3:
    case 6:
        return "desktop";

    case 2:
        return "laptop";

    case 4:
    case 5:
    case 7:
        return "server";

    case 8:
        return "tablet";
    }

try_dmi:
    r = read_one_line_file("/sys/class/dmi/id/chassis_type", &type);
    if (r < 0)
        return NULL;

    r = safe_atou(type, &t);
    free(type);
    if (r < 0)
        return NULL;

    /* We only list the really obvious cases here. The DMI data is
       unreliable enough, so let's not do any additional guesswork
       on top of that.

       See the SMBIOS Specification 3.0 section 7.4.1 for
       details about the values listed here:

       https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.0.0.pdf
     */

    switch (t) {

    case 0x3:
    case 0x4:
    case 0x6:
    case 0x7:
        return "desktop";

    case 0x8:
    case 0x9:
    case 0xA:
    case 0xE:
        return "laptop";

    case 0xB:
        return "handset";

    case 0x11:
    case 0x1C:
    case 0x1D:
        return "server";

    case 0x1E:
        return "tablet";
    }

    return NULL;
}
Esempio n. 10
0
static const char* fallback_chassis(void) {
        char *type;
        unsigned t;
        int v, r;

        v = detect_virtualization();
        if (VIRTUALIZATION_IS_VM(v))
                return "vm";
        if (VIRTUALIZATION_IS_CONTAINER(v))
                return "container";

        r = read_one_line_file("/sys/class/dmi/id/chassis_type", &type);
        if (r < 0)
                goto try_acpi;

        r = safe_atou(type, &t);
        free(type);
        if (r < 0)
                goto try_acpi;

        /* We only list the really obvious cases here. The DMI data is unreliable enough, so let's not do any
           additional guesswork on top of that.

           See the SMBIOS Specification 3.0 section 7.4.1 for details about the values listed here:

           https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.0.0.pdf
         */

        switch (t) {

        case 0x3: /* Desktop */
        case 0x4: /* Low Profile Desktop */
        case 0x6: /* Mini Tower */
        case 0x7: /* Tower */
                return "desktop";

        case 0x8: /* Portable */
        case 0x9: /* Laptop */
        case 0xA: /* Notebook */
        case 0xE: /* Sub Notebook */
                return "laptop";

        case 0xB: /* Hand Held */
                return "handset";

        case 0x11: /* Main Server Chassis */
        case 0x1C: /* Blade */
        case 0x1D: /* Blade Enclosure */
                return "server";

        case 0x1E: /* Tablet */
                return "tablet";

        case 0x1F: /* Convertible */
        case 0x20: /* Detachable */
                return "convertible";
        }

try_acpi:
        r = read_one_line_file("/sys/firmware/acpi/pm_profile", &type);
        if (r < 0)
                return NULL;

        r = safe_atou(type, &t);
        free(type);
        if (r < 0)
                return NULL;

        /* We only list the really obvious cases here as the ACPI data is not really super reliable.
         *
         * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
         *
         * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
         */

        switch(t) {

        case 1: /* Desktop */
        case 3: /* Workstation */
        case 6: /* Appliance PC */
                return "desktop";

        case 2: /* Mobile */
                return "laptop";

        case 4: /* Enterprise Server */
        case 5: /* SOHO Server */
        case 7: /* Performance Server */
                return "server";

        case 8: /* Tablet */
                return "tablet";
        }

        return NULL;
}