From 10f9c75519671e7c7ab8993b54fe22da7c2d0c38 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 25 Dec 2014 03:19:19 +0100 Subject: machined: beef up machined image listing with creation/modification times of subvolumes We make use of the btrfs subvol crtime for this, and for gpt images of a manually managed xattr, if we can. --- src/machine/image-dbus.c | 64 ++++++++++++++++++++++++++++++++++++++++++--- src/machine/image.c | 33 +++++++++++------------ src/machine/image.h | 2 +- src/machine/machinectl.c | 53 ++++++++++++++++++++++++++++--------- src/machine/machined-dbus.c | 8 +++--- 5 files changed, 124 insertions(+), 36 deletions(-) (limited to 'src/machine') diff --git a/src/machine/image-dbus.c b/src/machine/image-dbus.c index afb849b41a..8d9ad5589e 100644 --- a/src/machine/image-dbus.c +++ b/src/machine/image-dbus.c @@ -159,12 +159,68 @@ static int property_get_read_only( return 1; } +static int property_get_crtime( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + + _cleanup_(image_unrefp) Image *image = NULL; + int r; + + assert(bus); + assert(reply); + + r = image_find_by_bus_path_with_error(path, &image, error); + if (r < 0) + return r; + + r = sd_bus_message_append(reply, "t", image->crtime); + if (r < 0) + return r; + + return 1; +} + +static int property_get_mtime( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + + _cleanup_(image_unrefp) Image *image = NULL; + int r; + + assert(bus); + assert(reply); + + r = image_find_by_bus_path_with_error(path, &image, error); + if (r < 0) + return r; + + r = sd_bus_message_append(reply, "t", image->mtime); + if (r < 0) + return r; + + return 1; +} + const sd_bus_vtable image_vtable[] = { SD_BUS_VTABLE_START(0), - SD_BUS_PROPERTY("Name", "s", property_get_name, 0, 0), - SD_BUS_PROPERTY("Path", "s", property_get_path, 0, 0), - SD_BUS_PROPERTY("Type", "s", property_get_type, 0, 0), - SD_BUS_PROPERTY("ReadOnly", "b", property_get_read_only, 0, 0), + SD_BUS_PROPERTY("Name", "s", property_get_name, 0, 0), + SD_BUS_PROPERTY("Path", "s", property_get_path, 0, 0), + SD_BUS_PROPERTY("Type", "s", property_get_type, 0, 0), + SD_BUS_PROPERTY("ReadOnly", "b", property_get_read_only, 0, 0), + SD_BUS_PROPERTY("CreationTimestamp", "t", property_get_crtime, 0, 0), + SD_BUS_PROPERTY("ModificationTimestamp", "t", property_get_mtime, 0, 0), SD_BUS_VTABLE_END }; diff --git a/src/machine/image.c b/src/machine/image.c index 2ffe9444e3..9f88b0551f 100644 --- a/src/machine/image.c +++ b/src/machine/image.c @@ -46,8 +46,8 @@ static int image_new( const char *name, const char *path, bool read_only, + usec_t crtime, usec_t mtime, - usec_t btime, Image **ret) { _cleanup_(image_unrefp) Image *i = NULL; @@ -63,8 +63,8 @@ static int image_new( i->type = t; i->read_only = read_only; + i->crtime = crtime; i->mtime = mtime; - i->btime = btime; i->name = strdup(name); if (!i->name) @@ -116,25 +116,20 @@ static int image_make(int dfd, const char *name, const char *path, Image **ret) return -errno; if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) { - usec_t btime = 0; - int ro; + BtrfsSubvolInfo info; /* It's a btrfs subvolume */ - ro = btrfs_subvol_is_read_only_fd(fd); - if (ro < 0) - return ro; - - /* r = btrfs_subvol_get_btime(fd, &btime); */ - /* if (r < 0) */ - /* return r; */ + r = btrfs_subvol_get_info_fd(fd, &info); + if (r < 0) + return r; r = image_new(IMAGE_SUBVOLUME, name, path, - ro, + info.read_only, + info.otime, 0, - btime, ret); if (r < 0) return r; @@ -158,18 +153,24 @@ static int image_make(int dfd, const char *name, const char *path, Image **ret) return 1; } else if (S_ISREG(st.st_mode) && endswith(name, ".gpt")) { + const char *truncated; + usec_t crtime = 0; /* It's a GPT block device */ if (!ret) return 1; + fd_getcrtime_at(dfd, name, &crtime, 0); + + truncated = strndupa(name, strlen(name) - 4); + r = image_new(IMAGE_GPT, - name, + truncated, path, - !!(st.st_mode & 0222), + !(st.st_mode & 0222), + crtime, timespec_load(&st.st_mtim), - 0, ret); if (r < 0) return r; diff --git a/src/machine/image.h b/src/machine/image.h index f04be239d3..2e8f78147c 100644 --- a/src/machine/image.h +++ b/src/machine/image.h @@ -39,8 +39,8 @@ typedef struct Image { char *path; bool read_only; + usec_t crtime; usec_t mtime; - usec_t btime; } Image; Image *image_unref(Image *i); diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index d223e7895a..1cc5dfc68e 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -129,6 +129,8 @@ typedef struct ImageInfo { const char *name; const char *type; bool read_only; + usec_t crtime; + usec_t mtime; } ImageInfo; static int compare_image_info(const void *a, const void *b) { @@ -140,14 +142,14 @@ static int compare_image_info(const void *a, const void *b) { static int list_images(int argc, char *argv[], void *userdata) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - size_t max_name = strlen("NAME"), max_type = strlen("TYPE"); + size_t max_name = strlen("NAME"), max_type = strlen("TYPE"), max_crtime = strlen("CREATED"), max_mtime = strlen("MODIFIED"); _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_free_ ImageInfo *images = NULL; size_t n_images = 0, n_allocated = 0, j; const char *name, *type, *object; sd_bus *bus = userdata; - int read_only; - int r; + uint64_t crtime, mtime; + int read_only, r; assert(bus); @@ -167,11 +169,13 @@ static int list_images(int argc, char *argv[], void *userdata) { return r; } - r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbo)"); + r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbtto)"); if (r < 0) return bus_log_parse_error(r); - while ((r = sd_bus_message_read(reply, "(ssbo)", &name, &type, &read_only, &object)) > 0) { + while ((r = sd_bus_message_read(reply, "(ssbtto)", &name, &type, &read_only, &crtime, &mtime, &object)) > 0) { + char buf[FORMAT_TIMESTAMP_MAX]; + size_t l; if (name[0] == '.' && !arg_all) continue; @@ -182,12 +186,28 @@ static int list_images(int argc, char *argv[], void *userdata) { images[n_images].name = name; images[n_images].type = type; images[n_images].read_only = read_only; + images[n_images].crtime = crtime; + images[n_images].mtime = mtime; + + l = strlen(name); + if (l > max_name) + max_name = l; + + l = strlen(type); + if (l > max_type) + max_type = l; - if (strlen(name) > max_name) - max_name = strlen(name); + if (crtime != 0) { + l = strlen(format_timestamp(buf, sizeof(buf), crtime)); + if (l > max_crtime) + max_crtime = l; + } - if (strlen(type) > max_type) - max_type = strlen(type); + if (mtime != 0) { + l = strlen(format_timestamp(buf, sizeof(buf), mtime)); + if (l > max_mtime) + max_mtime = l; + } n_images++; } @@ -201,13 +221,22 @@ static int list_images(int argc, char *argv[], void *userdata) { qsort_safe(images, n_images, sizeof(ImageInfo), compare_image_info); if (arg_legend) - printf("%-*s %-*s %-3s\n", (int) max_name, "NAME", (int) max_type, "TYPE", "RO"); + printf("%-*s %-*s %-3s %*s %*s\n", + (int) max_name, "NAME", + (int) max_type, "TYPE", + "RO", + (int) max_crtime, "CREATED", + (int) max_mtime, "MODIFIED"); for (j = 0; j < n_images; j++) { - printf("%-*s %-*s %-3s\n", + char crtime_buf[FORMAT_TIMESTAMP_MAX], mtime_buf[FORMAT_TIMESTAMP_MAX]; + + printf("%-*s %-*s %-3s %*s %*s\n", (int) max_name, images[j].name, (int) max_type, images[j].type, - yes_no(images[j].read_only)); + yes_no(images[j].read_only), + (int) max_crtime, images[j].crtime != 0 ? format_timestamp(crtime_buf, sizeof(crtime_buf), images[j].crtime) : "-", + (int) max_mtime, images[j].mtime != 0 ? format_timestamp(mtime_buf, sizeof(mtime_buf), images[j].mtime) : "-"); } if (r < 0) diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index 0bf97e1ebb..e264dacbe6 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -488,7 +488,7 @@ static int method_list_images(sd_bus *bus, sd_bus_message *message, void *userda if (r < 0) return r; - r = sd_bus_message_open_container(reply, 'a', "(ssbo)"); + r = sd_bus_message_open_container(reply, 'a', "(ssbtto)"); if (r < 0) return r; @@ -499,10 +499,12 @@ static int method_list_images(sd_bus *bus, sd_bus_message *message, void *userda if (!p) return -ENOMEM; - r = sd_bus_message_append(reply, "(ssbo)", + r = sd_bus_message_append(reply, "(ssbtto)", image->name, image_type_to_string(image->type), image->read_only, + image->crtime, + image->mtime, p); if (r < 0) return r; @@ -563,7 +565,7 @@ const sd_bus_vtable manager_vtable[] = { SD_BUS_METHOD("GetImage", "s", "o", method_get_image, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("ListMachines", NULL, "a(ssso)", method_list_machines, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("ListImages", NULL, "a(ssbo)", method_list_images, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("ListImages", NULL, "a(ssbtto)", method_list_images, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine, 0), SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network, 0), SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine, 0), -- cgit v1.2.3-54-g00ecf