HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/vmspawn/vmspawn.c

198,981 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 1258b35c7ded8fefe9ea7f21e6147dfd8810fc39

Lines 1001–2000 of 4,443

        int r;
 
        assert(d);
        assert(fd >= 0);
 
        for (;;) {
                assert(d->full < sizeof(d->buffer));
 
                ssize_t n = read(fd, d->buffer + d->full, sizeof(d->buffer) - d->full);
                if (n < 0) {
                        if (ERRNO_IS_TRANSIENT(errno))
                                return 0;
 
                        return log_error_errno(errno, "Failed to read notification message: %m");
                }
                if (n == 0) /* We hit EOF! Let's parse this */
                        break;
 
                if ((size_t) n >= sizeof(d->buffer) - d->full)
                        return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Received notify message exceeded maximum size.");
 
                d->full += n;
        }
 
        /* We reached EOF, now parse the thing */
        assert(d->full < sizeof(d->buffer));
        d->buffer[d->full] = 0;
 
        _cleanup_strv_free_ char **tags = strv_split(d->buffer, "\n\r");
        if (!tags)
                return log_oom();
 
        if (DEBUG_LOGGING) {
                _cleanup_free_ char *j = strv_join(tags, " ");
                log_debug("Received notification message with tags: %s", strnull(j));
        }
 
        const char *status = strv_find_startswith(tags, "STATUS=");
        if (status)
                (void) sd_notifyf(/* unset_environment= */ false, "STATUS=VM running: %s", status);
 
        if (strv_contains(tags, "READY=1")) {
                r = sd_notify(/* unset_environment= */ false, "READY=1");
                if (r < 0)
                        log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
 
                if (!status)
                        (void) sd_notifyf(/* unset_environment= */ false, "STATUS=VM running.");
        }
 
        const char *p = strv_find_startswith(tags, "EXIT_STATUS=");
        if (p) {
                uint8_t k = 0;
                r = safe_atou8(p, &k);
                if (r < 0)
                        log_warning_errno(r, "Failed to parse exit status from %s, ignoring: %m", p);
                else
                        *d->exit_status = k;
        }
 
        return 1; /* done */
}
 
static int vmspawn_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
        NotifyConnectionData *d = ASSERT_PTR(userdata);
        int r;
 
        assert(source);
        assert(fd >= 0);
 
        r = read_vsock_notify(d, fd);
        if (r != 0) {
                int q;
 
                /* If we are done or are seeing an error we'll turn off floating mode, which means the event
                 * loop itself won't keep the event source pinned anymore, and since no one else (hopefully!)
                 * keeps a reference anymore the whole thing will be released once we exit from this handler
                 * here. */
 
                q = sd_event_source_set_floating(source, false);
                if (q < 0)
                        log_warning_errno(q, "Failed to disable floating mode of event source, ignoring: %m");
 
                return r;
        }
 
        return 0;
}
 
static int vmspawn_dispatch_vsock_connections(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
        _cleanup_close_ int conn_fd = -EBADF;
        sd_event *event;
        int r;
 
        assert(source);
        assert(fd >= 0);
        assert(userdata);
 
        if (revents != EPOLLIN) {
                log_warning("Got unexpected poll event for VSOCK fd.");
                return 0;
        }
 
        conn_fd = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
        if (conn_fd < 0) {
                if (ERRNO_IS_TRANSIENT(errno))
                        return 0;
 
                log_warning_errno(errno, "Failed to accept connection from VSOCK connection, ignoring: %m");
                return 0;
        }
 
        event = sd_event_source_get_event(source);
        if (!event)
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Failed to retrieve event from event source, exiting task");
 
        _cleanup_free_ NotifyConnectionData *d = new(NotifyConnectionData, 1);
        if (!d)
                return log_oom();
 
        *d = (NotifyConnectionData) {
                .exit_status = userdata,
        };
 
        /* add a new floating task to read from the connection */
        _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
        r = sd_event_add_io(event, &s, conn_fd, EPOLLIN, vmspawn_dispatch_notify_fd, d);
        if (r < 0)
                return log_error_errno(r, "Failed to allocate notify connection event source: %m");
 
        r = sd_event_source_set_io_fd_own(s, true);
        if (r < 0)
                return log_error_errno(r, "Failed to pass ownership of notify to event source: %m");
        TAKE_FD(conn_fd); /* conn_fd is now owned by the event loop so don't clean it up */
 
        r = sd_event_source_set_destroy_callback(s, free);
        if (r < 0)
                return log_error_errno(r, "Failed to set destroy callback on event source: %m");
        TAKE_PTR(d); /* The data object will now automatically be freed by the event source when it goes away */
 
        /* Finally, make sure the event loop pins the event source */
        r = sd_event_source_set_floating(s, true);
        if (r < 0)
                return log_error_errno(r, "Failed to set event source to floating mode: %m");
 
        (void) sd_event_source_set_description(s, "vmspawn-notify-socket-connection");
 
        return 0;
}
 
static int setup_notify_parent(sd_event *event, int fd, int *exit_status, sd_event_source **ret_notify_event_source) {
        int r;
 
        assert(event);
        assert(fd >= 0);
        assert(exit_status);
        assert(ret_notify_event_source);
 
        r = sd_event_add_io(event, ret_notify_event_source, fd, EPOLLIN, vmspawn_dispatch_vsock_connections, exit_status);
        if (r < 0)
                return log_error_errno(r, "Failed to allocate notify socket event source: %m");
 
        (void) sd_event_source_set_description(*ret_notify_event_source, "vmspawn-notify-socket-listen");
 
        return 0;
}
 
static int bus_open_in_machine(sd_bus **ret, unsigned cid, unsigned port, const char *private_key_path) {
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        _cleanup_free_ char *ssh_escaped = NULL, *bus_address = NULL;
        char port_str[DECIMAL_STR_MAX(unsigned)], cid_str[DECIMAL_STR_MAX(unsigned)];
        int r;
 
        assert(ret);
        assert(private_key_path);
 
        r = sd_bus_new(&bus);
        if (r < 0)
                return r;
 
        const char *ssh = secure_getenv("SYSTEMD_SSH") ?: "ssh";
        ssh_escaped = bus_address_escape(ssh);
        if (!ssh_escaped)
                return -ENOMEM;
 
        xsprintf(port_str, "%u", port);
        xsprintf(cid_str, "%u", cid);
 
        bus_address = strjoin(
                "unixexec:path=", ssh_escaped,
                /* -x: Disable X11 forwarding
                 * -T: Disable PTY allocation */
                ",argv1=-xT",
                ",argv2=-o,argv3=IdentitiesOnly yes",
                ",argv4=-o,argv5=IdentityFile=", private_key_path,
                ",argv6=-p,argv7=", port_str,
                ",argv8=--",
                ",argv9=root@vsock/", cid_str,
                ",argv10=systemd-stdio-bridge"
        );
        if (!bus_address)
                return -ENOMEM;
 
        free_and_replace(bus->address, bus_address);
        bus->bus_client = true;
        bus->trusted = true;
        bus->runtime_scope = RUNTIME_SCOPE_SYSTEM;
        bus->is_local = false;
 
        r = sd_bus_start(bus);
        if (r < 0)
                return r;
 
        *ret = TAKE_PTR(bus);
        return 0;
}
 
static int shutdown_vm_graceful(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        ShutdownInfo *shutdown_info = ASSERT_PTR(userdata);
        SSHInfo *ssh_info = ASSERT_PTR(shutdown_info->ssh_info);
        int r;
 
        assert(s);
        assert(si);
 
        /* If we don't have the vsock address and the SSH key, go to fallback */
        if (ssh_info->cid == VMADDR_CID_ANY || !ssh_info->private_key_path)
                goto fallback;
 
        /*
         * In order we should try:
         * 1. PowerOff from logind respects inhibitors but might not be available
         * 2. PowerOff from systemd heavy handed but should always be available
         * 3. SIGKILL qemu (this waits for qemu to die still)
         * 4. kill ourselves by shutting down our event loop (this does not wait for qemu)
         */
 
        r = bus_open_in_machine(&bus, ssh_info->cid, ssh_info->port, ssh_info->private_key_path);
        if (r < 0) {
                log_warning_errno(r, "Failed to connect to VM to forward signal, ignoring: %m");
                goto fallback;
        }
 
        r = bus_call_method(bus, bus_login_mgr, "PowerOff", &error, /* ret_reply= */ NULL, "b", false);
        if (r >= 0) {
                log_info("Requested powering off VM through D-Bus.");
                return 0;
        }
 
        log_warning_errno(r, "Failed to shutdown VM via logind, ignoring: %s", bus_error_message(&error, r));
        sd_bus_error_free(&error);
 
        r = bus_call_method(bus, bus_systemd_mgr, "PowerOff", &error, /* ret_reply= */ NULL, /* types= */ NULL);
        if (r >= 0) {
                log_info("Requested powering off VM through D-Bus.");
                return 0;
        }
 
        log_warning_errno(r, "Failed to shutdown VM via systemd, ignoring: %s", bus_error_message(&error, r));
 
fallback:
        /* at this point SSH clearly isn't working so don't try it again */
        TAKE_STRUCT(*ssh_info);
 
        /* Backup method to shut down the VM when D-BUS access over SSH is not available */
        if (shutdown_info->pidref) {
                r = pidref_kill(shutdown_info->pidref, SIGKILL);
                if (r < 0)
                        log_warning_errno(r, "Failed to kill qemu, terminating: %m");
                else {
                        TAKE_PTR(shutdown_info->pidref);
                        log_info("Trying to halt qemu. Send SIGTERM again to trigger vmspawn to immediately terminate.");
                        return 0;
                }
        }
 
        return sd_event_exit(sd_event_source_get_event(s), 0);
}
 
/* QEMU's stdio is the console PTY, which nothing reads before the PTY forwarder starts. When we fail before
 * the guest runs, copy what QEMU wrote (e.g. why it refused to start) to stderr, so that error messages
 * aren't lost. */
static void flush_console_to_stderrp(int *master) {
        char buf[4096];
        ssize_t n;
 
        assert(master);
 
        if (*master < 0)
                return;
 
        /* The PTY master is non-blocking. It fails with EIO once QEMU has exited and the buffer is empty. */
        while ((n = read(*master, buf, sizeof(buf))) > 0)
                (void) loop_write(STDERR_FILENO, buf, n);
        if (n < 0 && !IN_SET(errno, EAGAIN, EIO))
                log_debug_errno(errno, "Failed to read QEMU console output, ignoring: %m");
}
 
static int on_child_exit(sd_event_source *s, const siginfo_t *si, void *userdata) {
        assert(s);
        assert(si);
 
        /* Let's first do some logging about the exit status of the child. */
 
        int ret;
        if (si->si_code == CLD_EXITED) {
                if (si->si_status == EXIT_SUCCESS)
                        log_debug("Child process " PID_FMT " exited successfully.", si->si_pid);
                else
                        log_error("Child process " PID_FMT " died with a failure exit status %i.", si->si_pid, si->si_status);
 
                ret = si->si_status;
        } else if (si->si_code == CLD_KILLED)
                ret = log_error_errno(SYNTHETIC_ERRNO(EPROTO),
                                      "Child process " PID_FMT " was killed by signal %s.",
                                      si->si_pid, signal_to_string(si->si_status));
        else if (si->si_code == CLD_DUMPED)
                ret = log_error_errno(SYNTHETIC_ERRNO(EPROTO),
                                      "Child process " PID_FMT " dumped core by signal %s.",
                                      si->si_pid, signal_to_string(si->si_status));
        else
                ret = log_error_errno(SYNTHETIC_ERRNO(EPROTO),
                                      "Got unexpected exit code %i from child.",
                                      si->si_code);
 
        /* Regardless of whether the main qemu process or an auxiliary process died, let's exit either way
         * as it's very likely that the main qemu process won't be able to operate properly anymore if one
         * of the auxiliary processes died. */
 
        sd_event_exit(sd_event_source_get_event(s), ret);
        return 0;
}
 
static bool smbios_supported(void) {
        /* SMBIOS is always available on x86 (via SeaBIOS fallback), but on
         * other architectures it requires UEFI firmware to be loaded. */
        return ARCHITECTURE_SUPPORTS_SMBIOS &&
               (IN_SET(native_architecture(), ARCHITECTURE_X86, ARCHITECTURE_X86_64) || arg_firmware_type == FIRMWARE_UEFI);
}
 
static int add_vsock_credential(int vsock_fd) {
        assert(vsock_fd >= 0);
 
        union sockaddr_union addr;
        socklen_t addr_len = sizeof addr.vm;
        if (getsockname(vsock_fd, &addr.sa, &addr_len) < 0)
                return -errno;
 
        assert(addr_len >= sizeof addr.vm);
        assert(addr.vm.svm_family == AF_VSOCK);
 
        _cleanup_free_ char *value = NULL;
        if (asprintf(&value, "vsock-stream:%u:%u", (unsigned) VMADDR_CID_HOST, addr.vm.svm_port) < 0)
                return -ENOMEM;
 
        return machine_credential_add(&arg_credentials, "vmm.notify_socket", value, SIZE_MAX);
}
 
static int append_credential(const char *id, const char *data) {
        int r;
 
        assert(id);
        assert(data);
 
        MachineCredential *existing = machine_credential_find(&arg_credentials, id);
        if (!existing) {
                r = machine_credential_add(&arg_credentials, id, data, SIZE_MAX);
                if (r < 0)
                        return log_error_errno(r, "Failed to add %s credential: %m", id);
 
                return 0;
        }
 
        if (existing->size >= INT_MAX)
                return log_error_errno(SYNTHETIC_ERRNO(EFBIG),
                                       "Existing %s credential is too large (%zu bytes).",
                                       id, existing->size);
 
        _cleanup_free_ char *combined = NULL;
        if (existing->size > 0 && existing->data[existing->size - 1] != '\n')
                r = asprintf(&combined, "%.*s\n%s", (int) existing->size, existing->data, data);
        else
                r = asprintf(&combined, "%.*s%s", (int) existing->size, existing->data, data);
        if (r < 0)
                return log_oom();
 
        free_and_replace_full(existing->data, combined, erase_and_free);
        existing->size = r;
 
        return 0;
}
 
static int cmdline_add_kernel_cmdline(char ***cmdline, int smbios_dir_fd, const char *smbios_dir) {
        int r;
 
        assert(cmdline);
        assert(smbios_dir_fd >= 0);
        assert(smbios_dir);
 
        if (strv_isempty(arg_kernel_cmdline_extra))
                return 0;
 
        _cleanup_free_ char *kcl = strv_join(arg_kernel_cmdline_extra, " ");
        if (!kcl)
                return log_oom();
 
        size_t kcl_len = strlen(kcl);
        if (kcl_len >= KERNEL_CMDLINE_SIZE)
                return log_error_errno(SYNTHETIC_ERRNO(E2BIG),
                                       "Kernel command line length (%zu) exceeds the kernel's COMMAND_LINE_SIZE (%d).",
                                       kcl_len, KERNEL_CMDLINE_SIZE);
 
        if (arg_linux_image_type >= 0 && arg_linux_image_type != KERNEL_IMAGE_TYPE_UKI) {
                if (strv_extend_many(cmdline, "-append", kcl) < 0)
                        return log_oom();
        } else {
                if (!smbios_supported()) {
                        log_warning("Cannot append extra args to kernel cmdline, native architecture doesn't support SMBIOS, ignoring.");
                        return 0;
                }
 
                FOREACH_STRING(id, "io.systemd.stub.kernel-cmdline-extra", "io.systemd.boot.kernel-cmdline-extra") {
                        _cleanup_free_ char *content = strjoin(id, "=", kcl);
                        if (!content)
                                return log_oom();
 
                        r = write_string_file_at(
                                        smbios_dir_fd, id, content,
                                        WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_AVOID_NEWLINE|WRITE_STRING_FILE_MODE_0600);
                        if (r < 0)
                                return log_error_errno(r, "Failed to write smbios kernel command line to file: %m");
 
                        _cleanup_free_ char *p = path_join(smbios_dir, id);
                        if (!p)
                                return log_oom();
 
                        if (strv_extend(cmdline, "-smbios") < 0)
                                return log_oom();
 
                        if (strv_extend_joined(cmdline, "type=11,path=", p) < 0)
                                return log_oom();
                }
        }
 
        return 0;
}
 
static int cmdline_add_credentials(char ***cmdline, int smbios_dir_fd, const char *smbios_dir) {
        int r;
 
        assert(cmdline);
        assert(smbios_dir_fd >= 0);
        assert(smbios_dir);
 
        FOREACH_ARRAY(cred, arg_credentials.credentials, arg_credentials.n_credentials) {
                _cleanup_free_ char *cred_data_b64 = NULL;
                ssize_t n;
 
                n = base64mem(cred->data, cred->size, &cred_data_b64);
                if (n < 0)
                        return log_oom();
 
                if (smbios_supported()) {
                        _cleanup_free_ char *content = NULL;
                        if (asprintf(&content, "io.systemd.credential.binary:%s=%s", cred->id, cred_data_b64) < 0)
                                return log_oom();
 
                        r = write_string_file_at(
                                        smbios_dir_fd, cred->id, content,
                                        WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_AVOID_NEWLINE|WRITE_STRING_FILE_MODE_0600);
                        if (r < 0)
                                return log_error_errno(r, "Failed to write smbios credential file: %m");
 
                        _cleanup_free_ char *p = path_join(smbios_dir, cred->id);
                        if (!p)
                                return log_oom();
 
                        if (strv_extend(cmdline, "-smbios") < 0)
                                return log_oom();
 
                        if (strv_extend_joined(cmdline, "type=11,path=", p) < 0)
                                return log_oom();
 
                } else if (ARCHITECTURE_SUPPORTS_FW_CFG) {
                        /* fw_cfg keys are limited to 55 characters */
                        _cleanup_free_ char *key = strjoin("opt/io.systemd.credentials/", cred->id);
                        if (!key)
                                return log_oom();
 
                        if (strlen(key) <= QEMU_FW_CFG_MAX_KEY_LEN) {
                                r = write_data_file_atomic_at(
                                                smbios_dir_fd, cred->id,
                                                &IOVEC_MAKE(cred->data, cred->size),
                                                WRITE_DATA_FILE_MODE_0400);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to write fw_cfg credential file: %m");
 
                                _cleanup_free_ char *p = path_join(smbios_dir, cred->id);
                                if (!p)
                                        return log_oom();
 
                                if (strv_extend(cmdline, "-fw_cfg") < 0)
                                        return log_oom();
 
                                if (strv_extendf(cmdline, "name=%s,file=%s", key, p) < 0)
                                        return log_oom();
 
                                continue;
                        }
 
                        /* Fall through to kernel command line if key is too long */
                        log_notice("fw_cfg key '%s' exceeds %d character limit, passing credential via kernel command line. "
                                   "Note that this will make literal credentials readable to unprivileged userspace.",
                                   key, QEMU_FW_CFG_MAX_KEY_LEN);
 
                        if (arg_linux_image_type < 0)
                                return log_error_errno(
                                                SYNTHETIC_ERRNO(E2BIG),
                                                "Cannot pass credential '%s' to VM, fw_cfg key exceeds %d character limit and no kernel for direct boot specified.",
                                                cred->id,
                                                QEMU_FW_CFG_MAX_KEY_LEN);
 
                        if (strv_extendf(&arg_kernel_cmdline_extra,
                                         "systemd.set_credential_binary=%s:%s", cred->id, cred_data_b64) < 0)
                                return log_oom();
 
                } else if (arg_linux_image_type >= 0) {
                        log_notice("Both SMBIOS and fw_cfg are not supported, passing credential via kernel command line. "
                                   "Note that this will make literal credentials readable to unprivileged userspace.");
                        if (strv_extendf(&arg_kernel_cmdline_extra,
                                         "systemd.set_credential_binary=%s:%s", cred->id, cred_data_b64) < 0)
                                return log_oom();
                } else
                        return log_error_errno(
                                        SYNTHETIC_ERRNO(EOPNOTSUPP),
                                        "Cannot pass credential '%s' to VM, native architecture doesn't support SMBIOS or fw_cfg and no kernel for direct boot specified.",
                                        cred->id);
        }
 
        return 0;
}
 
static int cmdline_add_smbios11(char ***cmdline, int smbios_dir_fd, const char *smbios_dir) {
        int r;
 
        assert(cmdline);
        assert(smbios_dir_fd >= 0);
        assert(smbios_dir);
 
        if (strv_isempty(arg_smbios11))
                return 0;
 
        if (!smbios_supported()) {
                log_warning("Cannot issue SMBIOS Type #11 strings, native architecture doesn't support SMBIOS, ignoring.");
                return 0;
        }
 
        STRV_FOREACH(i, arg_smbios11) {
                _cleanup_(unlink_and_freep) char *p = NULL;
 
                r = tempfn_random_child(smbios_dir, "smbios11", &p);
                if (r < 0)
                        return r;
 
                _cleanup_free_ char *fn = NULL;
                r = path_extract_filename(p, &fn);
                if (r < 0)
                        return r;
 
                r = write_string_file_at(
                                smbios_dir_fd, fn, *i,
                                WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_AVOID_NEWLINE|WRITE_STRING_FILE_MODE_0600);
                if (r < 0)
                        return log_error_errno(r, "Failed to write smbios data to smbios file %s: %m", p);
 
                if (strv_extend(cmdline, "-smbios") < 0)
                        return log_oom();
 
                if (strv_extend_joined(cmdline, "type=11,path=", p) < 0)
                        return log_oom();
 
                p = mfree(p);
        }
 
        return 0;
}
 
static int start_tpm(
                const char *swtpm,
                const char *runtime_dir,
                const char *sd_socket_activate,
                char **ret_listen_address,
                PidRef *ret_pidref) {
 
        int r;
 
        assert(swtpm);
        assert(runtime_dir);
        assert(sd_socket_activate);
 
        _cleanup_free_ char *listen_address = path_join(runtime_dir, "tpm.sock");
        if (!listen_address)
                return log_oom();
 
        /* Validate socket path length up front instead of a failure in swtpm */
        union sockaddr_union sa;
        r = sockaddr_un_set_path(&sa.un, listen_address);
        if (r < 0)
                return log_error_errno(r, "TPM socket path '%s' is too long: %m", listen_address);
 
        _cleanup_free_ char *transient_state_dir = NULL;
        const char *state_dir;
        if (arg_tpm_state_path)
                state_dir = arg_tpm_state_path;
        else {
                transient_state_dir = path_join(runtime_dir, "tpm");
                if (!transient_state_dir)
                        return log_oom();
 
                state_dir = transient_state_dir;
        }
 
        r = mkdir_p(state_dir, 0700);
        if (r < 0)
                return log_error_errno(r, "Failed to create TPM state directory '%s': %m", state_dir);
 
        r = manufacture_swtpm(state_dir, /* secret= */ NULL);
        if (r < 0)
                return r;
 
        _cleanup_strv_free_ char **argv = NULL;
        argv = strv_new(sd_socket_activate, "--listen", listen_address, swtpm, "socket", "--tpm2", "--tpmstate");
        if (!argv)
                return log_oom();
 
        r = strv_extend_joined(&argv, "dir=", state_dir);
        if (r < 0)
                return log_oom();
 
        r = strv_extend_many(&argv, "--ctrl", "type=unixio,fd=3");
        if (r < 0)
                return log_oom();
 
        r = fork_notify(argv, /* child_handler= */ NULL, /* child_userdata= */ NULL, ret_pidref);
        if (r < 0)
                return r;
 
        if (ret_listen_address)
                *ret_listen_address = TAKE_PTR(listen_address);
 
        return 0;
}
 
static int discover_root(char **ret) {
        int r;
        _cleanup_(dissected_image_unrefp) DissectedImage *image = NULL;
        _cleanup_free_ char *root = NULL;
 
        assert(ret);
 
        r = dissect_image_file_and_warn(
                        arg_image,
                        /* verity= */ NULL,
                        /* mount_options= */ NULL,
                        /* image_policy= */ NULL,
                        /* image_filter= */ NULL,
                        /* flags= */ 0,
                        &image);
        if (r < 0)
                return r;
 
        if (image->partitions[PARTITION_ROOT].found)
                root = strjoin("root=PARTUUID=", SD_ID128_TO_UUID_STRING(image->partitions[PARTITION_ROOT].uuid));
        else if (image->partitions[PARTITION_USR].found)
                root = strjoin("mount.usr=PARTUUID=", SD_ID128_TO_UUID_STRING(image->partitions[PARTITION_USR].uuid));
        else
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Cannot perform a direct kernel boot without a root or usr partition, refusing");
 
        if (!root)
                return log_oom();
 
        *ret = TAKE_PTR(root);
        return 0;
}
 
static int find_virtiofsd(char **ret) {
        int r;
 
        assert(ret);
 
        r = find_executable("virtiofsd", ret);
        if (r >= 0)
                return 0;
        if (r != -ENOENT)
                return log_error_errno(r, "Error while searching for virtiofsd: %m");
 
        FOREACH_STRING(file, "/usr/libexec/virtiofsd", "/usr/lib/virtiofsd") {
                if (access(file, X_OK) >= 0) {
                        _cleanup_free_ char *copy = strdup(file);
                        if (!copy)
                                return log_oom();
 
                        *ret = TAKE_PTR(copy);
                        return 0;
                }
 
                if (!IN_SET(errno, ENOENT, EACCES))
                        return log_error_errno(errno, "Error while searching for virtiofsd: %m");
        }
 
        return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Failed to find virtiofsd binary.");
}
 
static int start_virtiofsd(
                const char *directory,
                uid_t source_uid,
                uid_t target_uid,
                uid_t uid_range,
                const char *runtime_dir,
                char **ret_listen_address,
                PidRef *ret_pidref) {
 
        int r;
 
        assert(directory);
        assert(runtime_dir);
 
        _cleanup_free_ char *virtiofsd = NULL;
        r = find_virtiofsd(&virtiofsd);
        if (r < 0)
                return r;
 
        /* A VM may run several virtiofsd instances (one per shared dir), so the random suffix keeps their
         * sockets distinct within the machine's runtime directory. */
        _cleanup_free_ char *listen_address = NULL;
        if (asprintf(&listen_address, "%s/sock-%"PRIx64, runtime_dir, random_u64()) < 0)
                return log_oom();
 
        union sockaddr_union su;
        r = sockaddr_un_set_path(&su.un, listen_address);
        if (r < 0)
                return log_error_errno(r, "Failed to prepare unix socket '%s': %m", listen_address);
 
        _cleanup_close_ int sock = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
        if (sock < 0)
                return log_error_errno(errno, "Failed to create unix socket: %m");
 
        if (bind(sock, &su.sa, r) < 0)
                return log_error_errno(errno, "Failed to bind unix socket to '%s': %m", listen_address);
 
        if (listen(sock, SOMAXCONN_DELUXE) < 0)
                return log_error_errno(errno, "Failed to listen on unix socket '%s': %m", listen_address);
 
        _cleanup_free_ char *sockstr = NULL;
        if (asprintf(&sockstr, "%i", sock) < 0)
                return log_oom();
 
        /* QEMU doesn't support submounts so don't announce them */
        _cleanup_strv_free_ char **argv = strv_new(
                        virtiofsd,
                        "--shared-dir", source_uid == FOREIGN_UID_MIN ? "/run/systemd/mount-rootfs" : directory,
                        "--xattr",
                        "--fd", sockstr,
                        "--no-announce-submounts",
                        "--log-level=error",
                        "--modcaps=-mknod");
        if (!argv)
                return log_oom();
 
        _cleanup_close_ int userns_fd = -EBADF, mapped_fd = -EBADF;
 
        if (source_uid == FOREIGN_UID_MIN) {
                assert(target_uid == 0);
                assert(uid_range == 0x10000);
 
                userns_fd = nsresource_allocate_userns(/* vl= */ NULL, /* name= */ NULL, NSRESOURCE_UIDS_64K);
                if (userns_fd < 0)
                        return log_error_errno(userns_fd, "Failed to allocate user namespace for virtiofsd: %m");
 
                _cleanup_close_ int directory_fd = open(directory, O_DIRECTORY|O_CLOEXEC|O_PATH);
                if (directory_fd < 0)
                        return log_error_errno(directory_fd, "Failed to open '%s': %m", directory);
 
                r = mountfsd_mount_directory_fd(/* vl= */ NULL, directory_fd, userns_fd, DISSECT_IMAGE_FOREIGN_UID, &mapped_fd);
                if (r < 0)
                        return r;
 
        } else if (!IN_SET(source_uid, FOREIGN_UID_MIN, UID_INVALID) && target_uid != UID_INVALID && uid_range != UID_INVALID &&
                   source_uid != target_uid) {
                r = strv_extend(&argv, "--translate-uid");
                if (r < 0)
                        return log_oom();
 
                r = strv_extendf(&argv, "map:" UID_FMT ":" UID_FMT ":" UID_FMT, target_uid, source_uid, uid_range);
                if (r < 0)
                        return log_oom();
 
                r = strv_extend(&argv, "--translate-gid");
                if (r < 0)
                        return log_oom();
 
                r = strv_extendf(&argv, "map:" GID_FMT ":" GID_FMT ":" GID_FMT, target_uid, source_uid, uid_range);
                if (r < 0)
                        return log_oom();
        }
 
        r = pidref_safe_fork_full(
                        "(virtiofsd)",
                        (const int[3]) { -EBADF, STDOUT_FILENO, STDERR_FILENO },
                        (int[]) { sock, userns_fd, mapped_fd },
                        source_uid == FOREIGN_UID_MIN ? 3 : 1,
                        FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_REOPEN_LOG|FORK_REARRANGE_STDIO,
                        ret_pidref);
        if (r < 0)
                return r;
        if (r == 0) {
                /* Child */
 
                r = namespace_enter(
                                /* pidns_fd= */ -EBADF,
                                /* mntns_fd= */ -EBADF,
                                /* netns_fd= */ -EBADF,
                                userns_fd,
                                /* root_fd= */ -EBADF);
                if (r < 0) {
                        log_error_errno(r, "Failed to enter user namespace for virtiofsd: %m");
                        _exit(EXIT_FAILURE);
                }
 
                if (userns_fd >= 0 && unshare(CLONE_NEWNS) < 0) {
                        log_error_errno(errno, "Failed to unshare mount namespace %m");
                        _exit(EXIT_FAILURE);
                }
 
                if (mapped_fd >= 0 && move_mount(mapped_fd, "", AT_FDCWD, "/run/systemd/mount-rootfs", MOVE_MOUNT_F_EMPTY_PATH) < 0) {
                        log_error_errno(errno, "Failed to move mount file descriptor to '/run/systemd/mount-rootfs': %m");
                        _exit(EXIT_FAILURE);
                }
 
                r = fd_cloexec(sock, false);
                if (r < 0) {
                        log_error_errno(r, "Failed to disable cloexec on socket: %m");
                        _exit(EXIT_FAILURE);
                }
 
                r = invoke_callout_binary(argv[0], argv);
                log_error_errno(r, "Failed to execute '%s': %m", argv[0]);
                _exit(EXIT_FAILURE);
        }
 
        if (ret_listen_address)
                *ret_listen_address = TAKE_PTR(listen_address);
 
        return 0;
}
 
static int bind_user_setup(
                const MachineBindUserContext *context,
                MachineCredentialContext *credentials,
                RuntimeMountContext *mounts) {
 
        int r;
 
        assert(credentials);
        assert(mounts);
 
        if (!context)
                return 0;
 
        FOREACH_ARRAY(bind_user, context->data, context->n_data) {
                _cleanup_free_ char *formatted = NULL;
                r = sd_json_variant_format(bind_user->payload_user->json, SD_JSON_FORMAT_NEWLINE, &formatted);
                if (r < 0)
                        return log_error_errno(r, "Failed to format JSON user record: %m");
 
                _cleanup_free_ char *cred = strjoin("userdb.transient.user.", bind_user->payload_user->user_name);
                if (!cred)
                        return log_oom();
 
                r = machine_credential_add(credentials, cred, formatted, SIZE_MAX);
                if (r < 0)
                        return r;
 
                formatted = mfree(formatted);
                r = sd_json_variant_format(bind_user->payload_group->json, SD_JSON_FORMAT_NEWLINE, &formatted);
                if (r < 0)
                        return log_error_errno(r, "Failed to format JSON group record: %m");
 
                free(cred);
                cred = strjoin("userdb.transient.group.", bind_user->payload_group->group_name);
                if (!cred)
                        return log_oom();
 
                r = machine_credential_add(credentials, cred, formatted, SIZE_MAX);
                if (r < 0)
                        return r;
 
                _cleanup_(runtime_mount_done) RuntimeMount mount = {
                        .source = strdup(user_record_home_directory(bind_user->host_user)),
                        .source_uid = bind_user->host_user->uid,
                        .target = strdup(user_record_home_directory(bind_user->payload_user)),
                        .target_uid = bind_user->payload_user->uid,
                };
                if (!mount.source || !mount.target)
                        return log_oom();
 
                if (!GREEDY_REALLOC(mounts->mounts, mounts->n_mounts + 1))
                        return log_oom();
 
                mounts->mounts[mounts->n_mounts++] = TAKE_STRUCT(mount);
        }
 
        return 0;
}
 
static int kernel_cmdline_maybe_append_root(void) {
        int r;
 
        if (strv_find_startswith(arg_kernel_cmdline_extra, "root=") ||
            strv_find_startswith(arg_kernel_cmdline_extra, "mount.usr="))
                return 0;
 
        _cleanup_free_ char *root = NULL;
        r = discover_root(&root);
        if (r < 0)
                return r;
 
        log_debug("Determined root file system '%s' from dissected image", root);
 
        if (strv_consume(&arg_kernel_cmdline_extra, TAKE_PTR(root)) < 0)
                return log_oom();
 
        return 0;
}
 
static int discover_boot_entry(const char *root, char **ret_linux, char ***ret_initrds) {
        _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL;
        _cleanup_free_ char *esp_path = NULL, *xbootldr_path = NULL;
        int r;
 
        assert(root);
        assert(ret_linux);
        assert(ret_initrds);
 
        esp_path = path_join(root, "efi");
        if (!esp_path)
                return log_oom();
 
        xbootldr_path = path_join(root, "boot");
        if (!xbootldr_path)
                return log_oom();
 
        r = boot_config_load(&config, esp_path, xbootldr_path);
        if (r < 0)
                return r;
 
        r = boot_config_select_special_entries(&config, /* skip_efivars= */ true);
        if (r < 0)
                return log_error_errno(r, "Failed to find special boot config entries: %m");
 
        const BootEntry *boot_entry = boot_config_default_entry(&config);
 
        if (boot_entry && !IN_SET(boot_entry->type, BOOT_ENTRY_TYPE1, BOOT_ENTRY_TYPE2))
                boot_entry = NULL;
 
        /* If we cannot determine a default entry search for UKIs (Type #2 EFI Unified Kernel Images)
         * then .conf files (Type #1 Boot Loader Specification Entries).
         * https://uapi-group.org/specifications/specs/boot_loader_specification */
        if (!boot_entry)
                FOREACH_ARRAY(entry, config.entries, config.n_entries)
                        if (entry->type == BOOT_ENTRY_TYPE2) { /* UKI */
                                boot_entry = entry;
                                break;
                        }
 
        if (!boot_entry)
                FOREACH_ARRAY(entry, config.entries, config.n_entries)
                        if (entry->type == BOOT_ENTRY_TYPE1) { /* .conf */
                                boot_entry = entry;
                                break;
                        }
 
        if (!boot_entry)
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Failed to discover any boot entries.");
 
        log_debug("Discovered boot entry %s (%s)", boot_entry->id, boot_entry_type_description_to_string(boot_entry->type));
 
        _cleanup_free_ char *linux_kernel = NULL;
        _cleanup_strv_free_ char **initrds = NULL;
        if (boot_entry->type == BOOT_ENTRY_TYPE2) { /* UKI */
                linux_kernel = path_join(boot_entry->root, boot_entry->kernel);
                if (!linux_kernel)
                        return log_oom();
        } else if (boot_entry->type == BOOT_ENTRY_TYPE1) { /* .conf */
                linux_kernel = path_join(boot_entry->root, boot_entry->kernel);
                if (!linux_kernel)
                        return log_oom();