From 83a587b79e9a5a5279f1c7a26918fc8c08523a7f Mon Sep 17 00:00:00 2001 From: Samuel Mendoza-Jonas Date: Tue, 26 Jul 2016 15:45:37 +1000 Subject: discover: Add 'sync' user event Add a user event named 'sync' that causes the discover server to merge the devicemapper snapshots of mounted devices. This is particularly useful as a debug aid (for example, when copying logs to a USB device), as the server will otherwise only sync changes to mounted devices in response to parser actions. The command can be called as pb-event sync@device to sync a particular device, or as pb-event sync@all to sync all devices with snapshots. Signed-off-by: Samuel Mendoza-Jonas --- discover/device-handler.c | 41 +++++++++++++++++++++++++++++++++++++++++ discover/device-handler.h | 1 + discover/event.c | 2 ++ discover/event.h | 1 + discover/user-event.c | 17 +++++++++++++++++ utils/pb-event.c | 2 +- 6 files changed, 63 insertions(+), 1 deletion(-) diff --git a/discover/device-handler.c b/discover/device-handler.c index 682a4e9..c31fcea 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -1581,6 +1581,41 @@ void device_release_write(struct discover_device *dev, bool release) dev->mounted = true; } +void device_sync_snapshots(struct device_handler *handler, const char *device) +{ + struct discover_device *dev = NULL; + unsigned int i; + + if (device) { + /* Find matching device and sync */ + dev = device_lookup_by_name(handler, device); + if (!dev) { + pb_log("%s: device name '%s' unrecognised\n", + __func__, device); + return; + } + if (dev->ramdisk) + device_release_write(dev, true); + else + pb_log("%s has no snapshot to merge, skipping\n", + dev->device->id); + return; + } + + /* Otherwise sync all relevant devices */ + for (i = 0; i < handler->n_devices; i++) { + dev = handler->devices[i]; + if (dev->device->type != DEVICE_TYPE_DISK && + dev->device->type != DEVICE_TYPE_USB) + continue; + if (dev->ramdisk) + device_release_write(dev, true); + else + pb_log("%s has no snapshot to merge, skipping\n", + dev->device->id); + } +} + #else void device_handler_discover_context_commit( @@ -1628,4 +1663,10 @@ void device_release_write(struct discover_device *dev __attribute__((unused)), { } +void device_sync_snapshots( + struct device_handler *handler __attribute__((unused)), + const char *device __attribute__((unused))) +{ +} + #endif diff --git a/discover/device-handler.h b/discover/device-handler.h index 6b1a757..b6f9fd5 100644 --- a/discover/device-handler.h +++ b/discover/device-handler.h @@ -142,5 +142,6 @@ void device_handler_reinit(struct device_handler *handler); int device_request_write(struct discover_device *dev, bool *release); void device_release_write(struct discover_device *dev, bool release); +void device_sync_snapshots(struct device_handler *handler, const char *device); #endif /* _DEVICE_HANDLER_H */ diff --git a/discover/event.c b/discover/event.c index 8b3a1ab..242ae81 100644 --- a/discover/event.c +++ b/discover/event.c @@ -55,6 +55,8 @@ static int event_parse_ad_header(char *buf, int len, enum event_action *action, *action = EVENT_ACTION_DHCP; else if (streq(buf, "boot")) *action = EVENT_ACTION_BOOT; + else if (streq(buf, "sync")) + *action = EVENT_ACTION_SYNC; else { pb_log("%s: unknown action: %s\n", __func__, buf); return -1; diff --git a/discover/event.h b/discover/event.h index 35c0767..a603615 100644 --- a/discover/event.h +++ b/discover/event.h @@ -14,6 +14,7 @@ enum event_action { EVENT_ACTION_CONF, EVENT_ACTION_DHCP, EVENT_ACTION_BOOT, + EVENT_ACTION_SYNC, EVENT_ACTION_MAX, }; diff --git a/discover/user-event.c b/discover/user-event.c index 15d9c87..7350b6c 100644 --- a/discover/user-event.c +++ b/discover/user-event.c @@ -60,6 +60,8 @@ static const char *event_action_name(enum event_action action) return "dhcp"; case EVENT_ACTION_BOOT: return "boot"; + case EVENT_ACTION_SYNC: + return "sync"; default: break; } @@ -464,6 +466,18 @@ static int user_event_boot(struct user_event *uev, struct event *event) return 0; } +static int user_event_sync(struct user_event *uev, struct event *event) +{ + struct device_handler *handler = uev->handler; + + if (strncasecmp(event->device, "all", strlen("all")) != 0) + device_sync_snapshots(handler, event->device); + else + device_sync_snapshots(handler, NULL); + + return 0; +} + static void user_event_handle_message(struct user_event *uev, char *buf, int len) { @@ -499,6 +513,9 @@ static void user_event_handle_message(struct user_event *uev, char *buf, case EVENT_ACTION_BOOT: result = user_event_boot(uev, event); break; + case EVENT_ACTION_SYNC: + result = user_event_sync(uev, event); + break; default: break; } diff --git a/utils/pb-event.c b/utils/pb-event.c index 572eb16..10bf4f5 100644 --- a/utils/pb-event.c +++ b/utils/pb-event.c @@ -55,7 +55,7 @@ static void print_usage(void) " Events can be read from stdin, or provided on the command line.\n" " User events must have the following format:\n" "\n" -" (add|remove|boot)@device-id [name=value] [image=value] [args=value]\n" +" (add|remove|boot|sync)@device-id [name=value] [image=value] [args=value]\n" "\n" " When read from stdin, components are separated by NUL chars\n" "\n" -- cgit v1.1