From 75f86906c52735c98dc0aa7e24b773edb42ee814 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 7 Sep 2015 13:42:47 +0200 Subject: basic: rework virtualization detection API Introduce a proper enum, and don't pass around string ids anymore. This simplifies things quite a bit, and makes virtualization detection more similar to architecture detection. --- src/core/dbus-manager.c | 6 +----- src/core/job.c | 2 +- src/core/locale-setup.c | 2 +- src/core/machine-id-setup.c | 30 ++++++++++++++---------------- src/core/main.c | 14 +++++++------- src/core/manager.c | 6 +++--- src/core/mount-setup.c | 4 ++-- src/core/shutdown.c | 2 +- src/core/swap.c | 8 ++++---- src/core/umount.c | 2 +- src/core/unit.c | 2 +- 11 files changed, 36 insertions(+), 42 deletions(-) (limited to 'src/core') diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c index 0b365391ec..4e5d67fc19 100644 --- a/src/core/dbus-manager.c +++ b/src/core/dbus-manager.c @@ -81,14 +81,10 @@ static int property_get_virtualization( void *userdata, sd_bus_error *error) { - const char *id = NULL; - assert(bus); assert(reply); - detect_virtualization(&id); - - return sd_bus_message_append(reply, "s", id); + return sd_bus_message_append(reply, "s", virtualization_to_string(detect_virtualization())); } static int property_get_architecture( diff --git a/src/core/job.c b/src/core/job.c index 15f5cc0cc9..2a35d1e2de 100644 --- a/src/core/job.c +++ b/src/core/job.c @@ -1137,7 +1137,7 @@ void job_shutdown_magic(Job *j) { /* In case messages on console has been disabled on boot */ j->unit->manager->no_console_output = false; - if (detect_container(NULL) > 0) + if (detect_container() > 0) return; asynchronous_sync(); diff --git a/src/core/locale-setup.c b/src/core/locale-setup.c index 108072ca9f..6961c26674 100644 --- a/src/core/locale-setup.c +++ b/src/core/locale-setup.c @@ -35,7 +35,7 @@ int locale_setup(char ***environment) { char *variables[_VARIABLE_LC_MAX] = {}; int r = 0, i; - if (detect_container(NULL) <= 0) { + if (detect_container() <= 0) { r = parse_env_file("/proc/cmdline", WHITESPACE, "locale.LANG", &variables[VARIABLE_LANG], "locale.LANGUAGE", &variables[VARIABLE_LANGUAGE], diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c index 2d5ae3b3b9..8f682c6d10 100644 --- a/src/core/machine-id-setup.c +++ b/src/core/machine-id-setup.c @@ -108,7 +108,7 @@ static int generate_machine_id(char id[34], const char *root) { unsigned char *p; sd_id128_t buf; char *q; - const char *vm_id, *dbus_machine_id; + const char *dbus_machine_id; assert(id); @@ -133,8 +133,8 @@ static int generate_machine_id(char id[34], const char *root) { /* If that didn't work, see if we are running in a container, * and a machine ID was passed in via $container_uuid the way * libvirt/LXC does it */ - r = detect_container(NULL); - if (r > 0) { + + if (detect_container() > 0) { _cleanup_free_ char *e = NULL; r = getenv_for_pid(1, "container_uuid", &e); @@ -146,26 +146,24 @@ static int generate_machine_id(char id[34], const char *root) { } } - } else { + } else if (detect_vm() == VIRTUALIZATION_KVM) { + /* If we are not running in a container, see if we are * running in qemu/kvm and a machine ID was passed in * via -uuid on the qemu/kvm command line */ - r = detect_vm(&vm_id); - if (r > 0 && streq(vm_id, "kvm")) { - char uuid[36]; + char uuid[36]; - fd = open("/sys/class/dmi/id/product_uuid", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); - if (fd >= 0) { - r = loop_read_exact(fd, uuid, 36, false); - safe_close(fd); + fd = open("/sys/class/dmi/id/product_uuid", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); + if (fd >= 0) { + r = loop_read_exact(fd, uuid, 36, false); + safe_close(fd); + if (r >= 0) { + r = shorten_uuid(id, uuid); if (r >= 0) { - r = shorten_uuid(id, uuid); - if (r >= 0) { - log_info("Initializing machine ID from KVM UUID."); - return 0; - } + log_info("Initializing machine ID from KVM UUID."); + return 0; } } } diff --git a/src/core/main.c b/src/core/main.c index 4cd2b08c38..fe8f1924bd 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -374,7 +374,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value) { /* Note that log_parse_environment() handles 'debug' * too, and sets the log level to LOG_DEBUG. */ - if (detect_container(NULL) > 0) + if (detect_container() > 0) log_set_target(LOG_TARGET_CONSOLE); } else if (!in_initrd() && !value) { @@ -1297,7 +1297,7 @@ int main(int argc, char *argv[]) { if (getpid() == 1) umask(0); - if (getpid() == 1 && detect_container(NULL) <= 0) { + if (getpid() == 1 && detect_container() <= 0) { /* Running outside of a container as PID 1 */ arg_running_as = MANAGER_SYSTEM; @@ -1551,14 +1551,14 @@ int main(int argc, char *argv[]) { } if (arg_running_as == MANAGER_SYSTEM) { - const char *virtualization = NULL; + int v; log_info(PACKAGE_STRING " running in %ssystem mode. (" SYSTEMD_FEATURES ")", arg_action == ACTION_TEST ? "test " : "" ); - detect_virtualization(&virtualization); - if (virtualization) - log_info("Detected virtualization %s.", virtualization); + v = detect_virtualization(); + if (v > 0) + log_info("Detected virtualization %s.", virtualization_to_string(v)); write_container_id(); @@ -2046,7 +2046,7 @@ finish: /* Avoid the creation of new processes forked by the * kernel; at this point, we will not listen to the * signals anyway */ - if (detect_container(NULL) <= 0) + if (detect_container() <= 0) (void) cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER); execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block); diff --git a/src/core/manager.c b/src/core/manager.c index fc10ddb5d9..56f2c92feb 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -554,7 +554,7 @@ int manager_new(ManagerRunningAs running_as, bool test_run, Manager **_m) { return -ENOMEM; #ifdef ENABLE_EFI - if (running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0) + if (running_as == MANAGER_SYSTEM && detect_container() <= 0) boot_timestamps(&m->userspace_timestamp, &m->firmware_timestamp, &m->loader_timestamp); #endif @@ -2156,7 +2156,7 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) { if (m->running_as != MANAGER_SYSTEM) return; - if (detect_container(NULL) > 0) + if (detect_container() > 0) return; if (u->type != UNIT_SERVICE && @@ -2613,7 +2613,7 @@ static void manager_notify_finished(Manager *m) { if (m->test_run) return; - if (m->running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0) { + if (m->running_as == MANAGER_SYSTEM && detect_container() <= 0) { /* Note that m->kernel_usec.monotonic is always at 0, * and m->firmware_usec.monotonic and diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c index c6f3569915..e84f80b61b 100644 --- a/src/core/mount-setup.c +++ b/src/core/mount-setup.c @@ -164,7 +164,7 @@ static int mount_one(const MountPoint *p, bool relabel) { return 0; /* Skip securityfs in a container */ - if (!(p->mode & MNT_IN_CONTAINER) && detect_container(NULL) > 0) + if (!(p->mode & MNT_IN_CONTAINER) && detect_container() > 0) return 0; /* The access mode here doesn't really matter too much, since @@ -385,7 +385,7 @@ int mount_setup(bool loaded_policy) { * nspawn and the container tools work out of the box. If * specific setups need other settings they can reset the * propagation mode to private if needed. */ - if (detect_container(NULL) <= 0) + if (detect_container() <= 0) if (mount(NULL, "/", NULL, MS_REC|MS_SHARED, NULL) < 0) log_warning_errno(errno, "Failed to set up the root directory for shared mount propagation: %m"); diff --git a/src/core/shutdown.c b/src/core/shutdown.c index aba16b4689..8a6fd25f31 100644 --- a/src/core/shutdown.c +++ b/src/core/shutdown.c @@ -202,7 +202,7 @@ int main(int argc, char *argv[]) { log_info("Sending SIGKILL to remaining processes..."); broadcast_signal(SIGKILL, true, false); - in_container = detect_container(NULL) > 0; + in_container = detect_container() > 0; need_umount = !in_container; need_swapoff = !in_container; diff --git a/src/core/swap.c b/src/core/swap.c index 4f3ddc9f04..2f462e339d 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -215,7 +215,7 @@ static int swap_add_default_dependencies(Swap *s) { if (UNIT(s)->manager->running_as != MANAGER_SYSTEM) return 0; - if (detect_container(NULL) > 0) + if (detect_container() > 0) return 0; return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true); @@ -824,7 +824,7 @@ static int swap_start(Unit *u) { assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED); - if (detect_container(NULL) > 0) + if (detect_container() > 0) return -EPERM; /* If there's a job for another swap unit for the same node @@ -857,7 +857,7 @@ static int swap_stop(Unit *u) { s->state == SWAP_ACTIVATING_DONE || s->state == SWAP_ACTIVE); - if (detect_container(NULL) > 0) + if (detect_container() > 0) return -EPERM; swap_enter_deactivating(s); @@ -1404,7 +1404,7 @@ static bool swap_supported(void) { if (supported < 0) supported = access("/proc/swaps", F_OK) >= 0 && - detect_container(NULL) <= 0; + detect_container() <= 0; return supported; } diff --git a/src/core/umount.c b/src/core/umount.c index d59b5d0ffb..22dbe67259 100644 --- a/src/core/umount.c +++ b/src/core/umount.c @@ -368,7 +368,7 @@ static int mount_points_list_umount(MountPoint **head, bool *changed, bool log_e read-only mount anything as that brings no real benefits, but might confuse the host, as we remount the superblock here, not the bind mound. */ - if (detect_container(NULL) <= 0) { + if (detect_container() <= 0) { /* We always try to remount directories * read-only first, before we go on and umount * them. diff --git a/src/core/unit.c b/src/core/unit.c index 45ce1b172d..be38f1fa27 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -3505,7 +3505,7 @@ int unit_kill_context( * them.*/ if (cg_unified() > 0 || - (detect_container(NULL) == 0 && !unit_cgroup_delegate(u))) + (detect_container() == 0 && !unit_cgroup_delegate(u))) wait_for_exit = true; if (c->send_sighup && k != KILL_KILL) { -- cgit v1.2.3-54-g00ecf