]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
machine: introduce io.systemd.MachineImage.List varlink method
authorIvan Kruglov <mail@ikruglov.com>
Tue, 8 Oct 2024 14:58:22 +0000 (16:58 +0200)
committerIvan Kruglov <mail@ikruglov.com>
Mon, 14 Oct 2024 08:57:36 +0000 (10:57 +0200)
io.systemd.MachineImage.List implements the following dbus org.freedesktop.machine1.Manager interfaces:

- GetImage
- ListImages
- GetImageHostname
- GetImageMachineID
- GetImageMachineInfo
- GetImageOSRelease

src/machine/machined-varlink.c
src/shared/varlink-io.systemd.MachineImage.c [new file with mode: 0644]
src/shared/varlink-io.systemd.MachineImage.h [new file with mode: 0644]

index 45e71266ed38df85075082bd7aa72d410ef46857..4329f74886a3034bf60a0db114dcf9295fca7251 100644 (file)
@@ -3,6 +3,7 @@
 #include "sd-varlink.h"
 
 #include "bus-polkit.h"
+#include "discover-image.h"
 #include "format-util.h"
 #include "hostname-util.h"
 #include "json-util.h"
@@ -508,6 +509,119 @@ static int vl_method_terminate(sd_varlink *link, sd_json_variant *parameters, sd
         return lookup_machine_and_call_method(link, parameters, flags, userdata, vl_method_terminate_internal);
 }
 
+static int list_image_one_and_maybe_read_metadata(sd_varlink *link, Image *image, bool more, bool read_metadata) {
+        int r;
+
+        assert(link);
+        assert(image);
+
+        if (read_metadata && !image->metadata_valid) {
+                r = image_read_metadata(image, &image_policy_container);
+                if (r < 0)
+                        return log_debug_errno(r, "Failed to read image metadata: %m");
+        }
+
+        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
+
+        r = sd_json_buildo(
+                        &v,
+                        SD_JSON_BUILD_PAIR_STRING("name", image->name),
+                        JSON_BUILD_PAIR_STRING_NON_EMPTY("path", image->path),
+                        SD_JSON_BUILD_PAIR_STRING("type", image_type_to_string(image->type)),
+                        SD_JSON_BUILD_PAIR_STRING("class", image_class_to_string(image->class)),
+                        SD_JSON_BUILD_PAIR_BOOLEAN("readOnly", image->read_only),
+                        JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("creationTimestamp", image->crtime),
+                        JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("modificationTimestamp", image->mtime),
+                        JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("usage", image->usage, UINT64_MAX),
+                        JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("usageExclusive", image->usage_exclusive, UINT64_MAX),
+                        JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("limit", image->limit, UINT64_MAX),
+                        JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("limitExclusive", image->limit_exclusive, UINT64_MAX));
+        if (r < 0)
+                return r;
+
+        if (image->metadata_valid) {
+                r = sd_json_variant_merge_objectbo(
+                                &v,
+                                JSON_BUILD_PAIR_STRING_NON_EMPTY("hostname", image->hostname),
+                                SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(image->machine_id), "machineId", SD_JSON_BUILD_ID128(image->machine_id)),
+                                SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(image->machine_info), "machineInfo", JSON_BUILD_STRV_ENV_PAIR(image->machine_info)),
+                                SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(image->os_release), "OSRelease", JSON_BUILD_STRV_ENV_PAIR(image->os_release)));
+                if (r < 0)
+                        return r;
+        }
+
+        if (more)
+                return sd_varlink_notify(link, v);
+
+        return sd_varlink_reply(link, v);
+}
+
+static int vl_method_list_images(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
+        struct params {
+                const char *image_name;
+                bool acquire_metadata;
+        };
+
+        static const sd_json_dispatch_field dispatch_table[] = {
+                { "name",            SD_JSON_VARIANT_STRING,    sd_json_dispatch_const_string, offsetof(struct params, image_name),        0 },
+                { "acquireMetadata", SD_JSON_VARIANT_BOOLEAN,   sd_json_dispatch_stdbool,      offsetof(struct params, acquire_metadata),  0 },
+                VARLINK_DISPATCH_POLKIT_FIELD,
+                {}
+        };
+
+        _cleanup_hashmap_free_ Hashmap *images = NULL;
+        struct params p = {};
+        Image *image;
+        int r;
+
+        assert(link);
+        assert(parameters);
+
+        r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
+        if (r != 0)
+                return r;
+
+        if (p.image_name) {
+                if (!image_name_is_valid(p.image_name))
+                        return sd_varlink_error_invalid_parameter_name(link, "name");
+
+                r = image_find(IMAGE_MACHINE, p.image_name, /* root = */ NULL, &image);
+                if (r == -ENOENT)
+                        return sd_varlink_error(link, "io.systemd.MachineImage.NoSuchImage", NULL);
+                if (r < 0)
+                        return log_debug_errno(r, "Failed to find image: %m");
+
+                return list_image_one_and_maybe_read_metadata(link, image, /* more = */ false, p.acquire_metadata);
+        }
+
+        if (!FLAGS_SET(flags, SD_VARLINK_METHOD_MORE))
+                return sd_varlink_error(link, SD_VARLINK_ERROR_EXPECTED_MORE, NULL);
+
+        images = hashmap_new(&image_hash_ops);
+        if (!images)
+                return -ENOMEM;
+
+        r = image_discover(IMAGE_MACHINE, /* root = */ NULL, images);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to discover images: %m");
+
+        Image *previous = NULL;
+        HASHMAP_FOREACH(image, images) {
+                if (previous) {
+                        r = list_image_one_and_maybe_read_metadata(link, previous, /* more = */ true, p.acquire_metadata);
+                        if (r < 0)
+                                return r;
+                }
+
+                previous = image;
+        }
+
+        if (previous)
+                return list_image_one_and_maybe_read_metadata(link, previous, /* more = */ false, p.acquire_metadata);
+
+        return sd_varlink_error(link, "io.systemd.MachineImage.NoSuchImage", NULL);
+}
+
 static int manager_varlink_init_userdb(Manager *m) {
         _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *s = NULL;
         int r;
@@ -566,15 +680,16 @@ static int manager_varlink_init_machine(Manager *m) {
 
         r = sd_varlink_server_add_interface(s, &vl_interface_io_systemd_Machine);
         if (r < 0)
-                return log_error_errno(r, "Failed to add UserDatabase interface to varlink server: %m");
+                return log_error_errno(r, "Failed to add Machine interface to varlink server: %m");
 
         r = sd_varlink_server_bind_method_many(
                         s,
-                        "io.systemd.Machine.Register",   vl_method_register,
-                        "io.systemd.Machine.List",       vl_method_list,
-                        "io.systemd.Machine.Unregister", vl_method_unregister,
-                        "io.systemd.Machine.Terminate",  vl_method_terminate,
-                        "io.systemd.Machine.Kill",       vl_method_kill);
+                        "io.systemd.Machine.Register",    vl_method_register,
+                        "io.systemd.Machine.List",        vl_method_list,
+                        "io.systemd.Machine.Unregister",  vl_method_unregister,
+                        "io.systemd.Machine.Terminate",   vl_method_terminate,
+                        "io.systemd.Machine.Kill",        vl_method_kill,
+                        "io.systemd.MachineImage.List",   vl_method_list_images);
         if (r < 0)
                 return log_error_errno(r, "Failed to register varlink methods: %m");
 
diff --git a/src/shared/varlink-io.systemd.MachineImage.c b/src/shared/varlink-io.systemd.MachineImage.c
new file mode 100644 (file)
index 0000000..e79b340
--- /dev/null
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "sd-varlink-idl.h"
+
+#include "varlink-io.systemd.MachineImage.h"
+
+static SD_VARLINK_DEFINE_METHOD_FULL(
+                List,
+                SD_VARLINK_SUPPORTS_MORE,
+                SD_VARLINK_FIELD_COMMENT("If non-null the name of a image to report details on."),
+                SD_VARLINK_DEFINE_INPUT(name, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
+                SD_VARLINK_FIELD_COMMENT("If true the output will include image metadata fields such as 'machineInfo' and 'OSRelease'."),
+                SD_VARLINK_DEFINE_INPUT(acquireMetadata, SD_VARLINK_BOOL, SD_VARLINK_NULLABLE),
+                VARLINK_DEFINE_POLKIT_INPUT,
+                SD_VARLINK_FIELD_COMMENT("Name of the image"),
+                SD_VARLINK_DEFINE_OUTPUT(name, SD_VARLINK_STRING, 0),
+                SD_VARLINK_FIELD_COMMENT("The file system path where image is stored"),
+                SD_VARLINK_DEFINE_OUTPUT(path, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
+                SD_VARLINK_FIELD_COMMENT("The type of this image"),
+                SD_VARLINK_DEFINE_OUTPUT(type, SD_VARLINK_STRING, 0),
+                SD_VARLINK_FIELD_COMMENT("The class of this image"),
+                SD_VARLINK_DEFINE_OUTPUT(class, SD_VARLINK_STRING, 0),
+                SD_VARLINK_FIELD_COMMENT("Whether the image is read-only"),
+                SD_VARLINK_DEFINE_OUTPUT(readOnly, SD_VARLINK_BOOL, 0),
+                SD_VARLINK_FIELD_COMMENT("The image creation timestamp"),
+                SD_VARLINK_DEFINE_OUTPUT(creationTimestamp, SD_VARLINK_INT, SD_VARLINK_NULLABLE),
+                SD_VARLINK_FIELD_COMMENT("The image creation timestamp"),
+                SD_VARLINK_DEFINE_OUTPUT(modificationTimestamp, SD_VARLINK_INT, SD_VARLINK_NULLABLE),
+                SD_VARLINK_FIELD_COMMENT("The image creation timestamp"),
+                SD_VARLINK_DEFINE_OUTPUT(usage, SD_VARLINK_INT, SD_VARLINK_NULLABLE),
+                SD_VARLINK_FIELD_COMMENT("The image disk usage (exclusive)"),
+                SD_VARLINK_DEFINE_OUTPUT(usageExclusive, SD_VARLINK_INT, SD_VARLINK_NULLABLE),
+                SD_VARLINK_FIELD_COMMENT("The image disk usage (exclusive)"),
+                SD_VARLINK_DEFINE_OUTPUT(limit, SD_VARLINK_INT, SD_VARLINK_NULLABLE),
+                SD_VARLINK_FIELD_COMMENT("The image disk usage limit (exclusive)"),
+                SD_VARLINK_DEFINE_OUTPUT(limitExclusive, SD_VARLINK_INT, SD_VARLINK_NULLABLE),
+                SD_VARLINK_FIELD_COMMENT("The hostname of the image"),
+                SD_VARLINK_DEFINE_OUTPUT(hostname, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
+                SD_VARLINK_FIELD_COMMENT("The machine ID of the image"),
+                SD_VARLINK_DEFINE_OUTPUT(machineId, SD_VARLINK_STRING, SD_VARLINK_NULLABLE),
+                SD_VARLINK_FIELD_COMMENT("Machine info information of an image. It contains an array of key value pairs read from the machine-info(5) file in the image."),
+                SD_VARLINK_DEFINE_OUTPUT(machineInfo, SD_VARLINK_STRING, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY),
+                SD_VARLINK_FIELD_COMMENT("OS release information of an image. It contains an array of key value pairs read from the os-release(5) file in the image."),
+                SD_VARLINK_DEFINE_OUTPUT(OSRelease, SD_VARLINK_STRING, SD_VARLINK_NULLABLE|SD_VARLINK_ARRAY));
+
+static SD_VARLINK_DEFINE_ERROR(NoSuchImage);
+
+SD_VARLINK_DEFINE_INTERFACE(
+                io_systemd_MachineImage,
+                "io.systemd.MachineImage",
+                SD_VARLINK_SYMBOL_COMMENT("List images"),
+                &vl_method_List,
+                SD_VARLINK_SYMBOL_COMMENT("No matching image exists"),
+                &vl_error_NoSuchImage);
diff --git a/src/shared/varlink-io.systemd.MachineImage.h b/src/shared/varlink-io.systemd.MachineImage.h
new file mode 100644 (file)
index 0000000..91e21f6
--- /dev/null
@@ -0,0 +1,6 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include "sd-varlink-idl.h"
+
+extern const sd_varlink_interface vl_interface_io_systemd_MachineImage;