12 #include <sys/mount.h>
14 #include <talloc/talloc.h>
15 #include <list/list.h>
17 #include <types/types.h>
18 #include <system/system.h>
19 #include <process/process.h>
21 #include <i18n/i18n.h>
22 #include <pb-config/pb-config.h>
24 #include <sys/sysmacros.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <arpa/inet.h>
30 #include "device-handler.h"
31 #include "discover-server.h"
32 #include "devmapper.h"
33 #include "user-event.h"
45 enum default_priority {
46 DEFAULT_PRIORITY_TEMP_USER = 1,
47 DEFAULT_PRIORITY_REMOTE = 2,
48 DEFAULT_PRIORITY_LOCAL_FIRST = 3,
49 DEFAULT_PRIORITY_LOCAL_LAST = 0xfe,
50 DEFAULT_PRIORITY_DISABLED = 0xff,
53 static int default_rescan_timeout = 5 * 60; /* seconds */
55 struct progress_info {
56 unsigned int percentage;
57 unsigned long size; /* size in bytes */
59 const struct process_info *procinfo;
60 struct list_item list;
63 struct device_handler {
64 struct discover_server *server;
68 struct network *network;
69 struct user_event *user_event;
71 struct discover_device **devices;
72 unsigned int n_devices;
74 struct ramdisk_device **ramdisks;
75 unsigned int n_ramdisks;
77 struct waitset *waitset;
78 struct waiter *timeout_waiter;
79 bool autoboot_enabled;
80 unsigned int sec_to_boot;
81 struct autoboot_option *temp_autoboot;
83 struct discover_boot_option *default_boot_option;
84 int default_boot_option_priority;
86 struct list unresolved_boot_options;
88 struct boot_task *pending_boot;
89 bool pending_boot_is_default;
92 unsigned int n_progress;
94 struct plugin_option **plugins;
95 unsigned int n_plugins;
96 bool plugin_installing;
99 static int mount_device(struct discover_device *dev);
100 static int umount_device(struct discover_device *dev);
102 static int device_handler_init_sources(struct device_handler *handler);
103 static void device_handler_reinit_sources(struct device_handler *handler);
105 static void device_handler_update_lang(const char *lang);
107 void discover_context_add_boot_option(struct discover_context *ctx,
108 struct discover_boot_option *boot_option)
110 boot_option->source = ctx->parser;
111 list_add_tail(&ctx->boot_options, &boot_option->list);
112 talloc_steal(ctx, boot_option);
116 * device_handler_get_device_count - Get the count of current handler devices.
119 int device_handler_get_device_count(const struct device_handler *handler)
121 return handler->n_devices;
125 * device_handler_get_device - Get a handler device by index.
128 const struct discover_device *device_handler_get_device(
129 const struct device_handler *handler, unsigned int index)
131 if (index >= handler->n_devices) {
132 assert(0 && "bad index");
136 return handler->devices[index];
140 * device_handler_get_plugin_count - Get the count of current handler plugins.
142 int device_handler_get_plugin_count(const struct device_handler *handler)
144 return handler->n_plugins;
148 * discover_handler_get_plugin - Get a handler plugin by index.
150 const struct plugin_option *device_handler_get_plugin(
151 const struct device_handler *handler, unsigned int index)
153 if (index >= handler->n_plugins) {
154 assert(0 && "bad index");
158 return handler->plugins[index];
161 struct network *device_handler_get_network(
162 const struct device_handler *handler)
164 return handler->network;
167 struct discover_boot_option *discover_boot_option_create(
168 struct discover_context *ctx,
169 struct discover_device *device)
171 struct discover_boot_option *opt;
173 opt = talloc_zero(ctx, struct discover_boot_option);
174 opt->option = talloc_zero(opt, struct boot_option);
175 opt->device = device;
180 static int device_match_uuid(struct discover_device *dev, const char *uuid)
182 return dev->uuid && !strcmp(dev->uuid, uuid);
185 static int device_match_label(struct discover_device *dev, const char *label)
187 return dev->label && !strcmp(dev->label, label);
190 static int device_match_id(struct discover_device *dev, const char *id)
192 return !strcmp(dev->device->id, id);
195 static int device_match_serial(struct discover_device *dev, const char *serial)
197 const char *val = discover_device_get_param(dev, "ID_SERIAL");
198 return val && !strcmp(val, serial);
201 static struct discover_device *device_lookup(
202 struct device_handler *device_handler,
203 int (match_fn)(struct discover_device *, const char *),
206 struct discover_device *dev;
212 for (i = 0; i < device_handler->n_devices; i++) {
213 dev = device_handler->devices[i];
215 if (match_fn(dev, str))
222 struct discover_device *device_lookup_by_name(struct device_handler *handler,
225 if (!strncmp(name, "/dev/", strlen("/dev/")))
226 name += strlen("/dev/");
228 return device_lookup_by_id(handler, name);
231 struct discover_device *device_lookup_by_uuid(
232 struct device_handler *device_handler,
235 return device_lookup(device_handler, device_match_uuid, uuid);
238 struct discover_device *device_lookup_by_label(
239 struct device_handler *device_handler,
242 return device_lookup(device_handler, device_match_label, label);
245 struct discover_device *device_lookup_by_id(
246 struct device_handler *device_handler,
249 return device_lookup(device_handler, device_match_id, id);
252 struct discover_device *device_lookup_by_serial(
253 struct device_handler *device_handler,
256 return device_lookup(device_handler, device_match_serial, serial);
259 void device_handler_destroy(struct device_handler *handler)
261 talloc_free(handler);
264 static int destroy_device(void *arg)
266 struct discover_device *dev = arg;
273 struct discover_device *discover_device_create(struct device_handler *handler,
274 const char *uuid, const char *id)
276 struct discover_device *dev;
279 dev = device_lookup_by_uuid(handler, uuid);
281 dev = device_lookup_by_id(handler, id);
286 dev = talloc_zero(handler, struct discover_device);
287 dev->device = talloc_zero(dev, struct device);
288 dev->device->id = talloc_strdup(dev->device, id);
289 dev->uuid = talloc_strdup(dev, uuid);
290 list_init(&dev->params);
291 list_init(&dev->boot_options);
293 talloc_set_destructor(dev, destroy_device);
298 struct discover_device_param {
301 struct list_item list;
304 void discover_device_set_param(struct discover_device *device,
305 const char *name, const char *value)
307 struct discover_device_param *param;
310 list_for_each_entry(&device->params, param, list) {
311 if (!strcmp(param->name, name)) {
320 param = talloc(device, struct discover_device_param);
321 param->name = talloc_strdup(param, name);
322 list_add(&device->params, ¶m->list);
325 list_remove(¶m->list);
329 talloc_free(param->value);
332 param->value = talloc_strdup(param, value);
335 const char *discover_device_get_param(struct discover_device *device,
338 struct discover_device_param *param;
340 list_for_each_entry(&device->params, param, list) {
341 if (!strcmp(param->name, name))
347 static void set_env_variables(const struct config *config)
349 if (config->http_proxy)
350 setenv("http_proxy", config->http_proxy, 1);
352 unsetenv("http_proxy");
354 if (config->https_proxy)
355 setenv("https_proxy", config->https_proxy, 1);
357 unsetenv("https_proxy");
359 /* Reduce noise in the log from LVM listing open file descriptors */
360 setenv("LVM_SUPPRESS_FD_WARNINGS", "1", 1);
363 struct device_handler *device_handler_init(struct discover_server *server,
364 struct waitset *waitset, int dry_run)
366 struct device_handler *handler;
369 handler = talloc_zero(NULL, struct device_handler);
370 handler->server = server;
371 handler->waitset = waitset;
372 handler->dry_run = dry_run;
373 handler->autoboot_enabled = config_autoboot_active(config_get());
375 list_init(&handler->unresolved_boot_options);
377 list_init(&handler->progress);
379 /* set up our mount point base */
380 pb_mkdir_recursive(mount_base());
384 if (config_get()->safe_mode)
387 set_env_variables(config_get());
389 rc = device_handler_init_sources(handler);
391 talloc_free(handler);
398 void device_handler_reinit(struct device_handler *handler)
400 struct discover_boot_option *opt, *tmp;
401 struct ramdisk_device *ramdisk;
402 struct config *config;
405 device_handler_cancel_default(handler);
406 /* Cancel any pending non-default boot */
407 if (handler->pending_boot) {
408 boot_cancel(handler->pending_boot);
409 handler->pending_boot = NULL;
410 handler->pending_boot_is_default = false;
413 /* Cancel any remaining async jobs */
414 process_stop_async_all();
415 pending_network_jobs_cancel();
417 /* free unresolved boot options */
418 list_for_each_entry_safe(&handler->unresolved_boot_options,
421 list_init(&handler->unresolved_boot_options);
423 /* drop all devices */
424 for (i = 0; i < handler->n_devices; i++) {
425 struct discover_device *device = handler->devices[i];
426 discover_server_notify_device_remove(handler->server,
428 ramdisk = device->ramdisk;
429 if (device->requery_waiter)
430 waiter_remove(device->requery_waiter);
432 talloc_free(ramdisk);
435 talloc_free(handler->devices);
436 handler->devices = NULL;
437 handler->n_devices = 0;
438 talloc_free(handler->ramdisks);
439 handler->ramdisks = NULL;
440 handler->n_ramdisks = 0;
442 /* drop any known plugins */
443 for (i = 0; i < handler->n_plugins; i++)
444 talloc_free(handler->plugins[i]);
445 talloc_free(handler->plugins);
446 handler->plugins = NULL;
447 handler->n_plugins = 0;
449 discover_server_notify_plugins_remove(handler->server);
451 set_env_variables(config_get());
453 /* If the safe mode warning was active disable it now */
454 if (config_get()->safe_mode) {
455 config = config_copy(handler, config_get());
456 config->safe_mode = false;
458 discover_server_notify_config(handler->server, config);
461 /* Force rediscovery on SCSI devices */
462 process_run_simple(handler, pb_system_apps.scsi_rescan, NULL);
464 device_handler_reinit_sources(handler);
467 void device_handler_remove(struct device_handler *handler,
468 struct discover_device *device)
470 struct discover_boot_option *opt, *tmp;
473 if (device->requery_waiter)
474 waiter_remove(device->requery_waiter);
476 list_for_each_entry_safe(&device->boot_options, opt, tmp, list) {
477 if (opt == handler->default_boot_option) {
478 pb_log("Default option %s cancelled since device removed",
480 device_handler_cancel_default(handler);
485 for (i = 0; i < handler->n_devices; i++)
486 if (handler->devices[i] == device)
489 if (i == handler->n_devices) {
494 /* Free any unresolved options, as they're currently allocated
495 * against the handler */
496 list_for_each_entry_safe(&handler->unresolved_boot_options,
498 if (opt->device != device)
500 list_remove(&opt->list);
504 /* if this is a network device, we have to unregister it from the
506 if (device->device->type == DEVICE_TYPE_NETWORK)
507 network_unregister_device(handler->network, device);
509 handler->n_devices--;
510 memmove(&handler->devices[i], &handler->devices[i + 1],
511 (handler->n_devices - i) * sizeof(handler->devices[0]));
512 handler->devices = talloc_realloc(handler, handler->devices,
513 struct discover_device *, handler->n_devices);
515 if (device->notified)
516 discover_server_notify_device_remove(handler->server,
522 void device_handler_status(struct device_handler *handler,
523 struct status *status)
525 discover_server_notify_boot_status(handler->server, status);
528 static void _device_handler_vstatus(struct device_handler *handler,
529 enum status_type type, const char *fmt, va_list ap)
531 struct status status;
534 status.message = talloc_vasprintf(handler, fmt, ap);
535 status.backlog = false;
537 device_handler_status(handler, &status);
539 talloc_free(status.message);
542 static void _device_handler_vdevstatus(struct device_handler *handler,
543 struct discover_device *device, enum status_type type,
544 const char *fmt, va_list ap)
548 msg = talloc_asprintf(handler, "[%s] %s",
549 device ? device->device->id : "unknown", fmt);
550 _device_handler_vstatus(handler, type, msg, ap);
554 void device_handler_status_dev_info(struct device_handler *handler,
555 struct discover_device *dev, const char *fmt, ...)
560 _device_handler_vdevstatus(handler, dev, STATUS_INFO, fmt, ap);
564 void device_handler_status_dev_err(struct device_handler *handler,
565 struct discover_device *dev, const char *fmt, ...)
570 _device_handler_vdevstatus(handler, dev, STATUS_ERROR, fmt, ap);
574 void device_handler_status_info(struct device_handler *handler,
575 const char *fmt, ...)
580 _device_handler_vstatus(handler, STATUS_INFO, fmt, ap);
584 void device_handler_status_err(struct device_handler *handler,
585 const char *fmt, ...)
590 _device_handler_vstatus(handler, STATUS_ERROR, fmt, ap);
594 void device_handler_status_download(struct device_handler *handler,
595 const struct process_info *procinfo,
596 unsigned int percentage, unsigned int size, char suffix)
598 struct progress_info *p, *progress = NULL;
599 uint64_t current_converted, current = 0;
600 const char *units = " kMGTP";
601 unsigned long size_bytes;
607 list_for_each_entry(&handler->progress, p, list)
608 if (p->procinfo == procinfo)
612 pb_log("Registering new progress struct\n");
613 progress = talloc_zero(handler, struct progress_info);
615 pb_log("Failed to allocate room for progress struct\n");
618 progress->procinfo = procinfo;
619 list_add(&handler->progress, &progress->list);
620 handler->n_progress++;
624 for (i = 0; i < strlen(units); i++) {
625 if (units[i] == suffix)
629 if (i >= strlen(units)) {
630 pb_log("Couldn't recognise suffix '%c'\n", suffix);
637 progress->percentage = percentage;
638 progress->size = size_bytes;
641 * Aggregate the info we have and update status. If a progress struct
642 * has zero for both percentage and size we assume progress information
643 * is unavailable and fall back to a generic progress message.
645 list_for_each_entry(&handler->progress, p, list) {
648 if (!p->percentage || !p->size) {
649 update = talloc_asprintf(handler,
650 _("%u downloads in progress..."),
651 handler->n_progress);
657 t = (100 * c) / p->percentage;
664 current_converted = current;
665 while (current_converted >= 1000) {
666 current_converted >>= 10;
669 update = talloc_asprintf(handler,
670 _("%u %s downloading: %.0f%% - %" PRIu64 "%cB"),
672 ngettext("item", "items", handler->n_progress),
673 (current / total) * 100, current_converted,
678 pb_log("%s: failed to allocate new status\n", __func__);
680 device_handler_status_info(handler, "%s\n", update);
685 static void device_handler_plugin_scan_device(struct device_handler *handler,
686 struct discover_device *dev)
690 pb_debug("Scanning %s for plugin files\n", dev->device->id);
692 rc = process_run_simple(handler, pb_system_apps.pb_plugin,
693 "scan", dev->mount_path,
696 pb_log("Error from pb-plugin scan %s\n",
700 void device_handler_status_download_remove(struct device_handler *handler,
701 struct process_info *procinfo)
703 struct progress_info *p, *tmp;
705 list_for_each_entry_safe(&handler->progress, p, tmp, list)
706 if (p->procinfo == procinfo) {
707 list_remove(&p->list);
709 handler->n_progress--;
713 static void device_handler_boot_status_cb(void *arg, struct status *status)
715 struct device_handler *handler = arg;
717 /* boot had failed; update handler state to allow a new default if one
720 if (status->type == STATUS_ERROR) {
721 handler->pending_boot = NULL;
722 handler->default_boot_option = NULL;
725 device_handler_status(handler, status);
728 static void countdown_status(struct device_handler *handler,
729 struct discover_boot_option *opt, unsigned int sec)
731 struct status status;
733 status.type = STATUS_INFO;
734 status.message = talloc_asprintf(handler,
735 _("Booting in %d sec: [%s] %s"), sec,
736 opt->device->device->id, opt->option->name);
737 status.backlog = false;
739 device_handler_status(handler, &status);
741 talloc_free(status.message);
744 static int default_timeout(void *arg)
746 struct device_handler *handler = arg;
747 struct discover_boot_option *opt;
749 if (!handler->default_boot_option)
752 if (handler->pending_boot)
755 opt = handler->default_boot_option;
757 if (handler->sec_to_boot) {
758 countdown_status(handler, opt, handler->sec_to_boot);
759 handler->sec_to_boot--;
760 handler->timeout_waiter = waiter_register_timeout(
761 handler->waitset, 1000,
762 default_timeout, handler);
766 handler->timeout_waiter = NULL;
768 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
772 handler->pending_boot = boot(handler, handler->default_boot_option,
773 NULL, handler->dry_run, device_handler_boot_status_cb,
775 handler->pending_boot_is_default = true;
780 enum ipmi_bootdev ipmi_type;
781 enum device_type device_type;
782 } device_type_map[] = {
783 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
784 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
785 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
786 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
789 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
790 enum device_type device_type)
794 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
795 if (device_type_map[i].device_type == device_type)
796 return device_type_map[i].ipmi_type == ipmi_type;
802 static bool autoboot_option_matches(struct autoboot_option *opt,
803 struct discover_device *dev)
805 if (opt->boot_type == BOOT_DEVICE_UUID)
806 if (!strcmp(opt->uuid, dev->uuid))
809 if (opt->boot_type == BOOT_DEVICE_TYPE)
810 if (opt->type == dev->device->type ||
811 opt->type == DEVICE_TYPE_ANY)
817 static int autoboot_option_priority(const struct config *config,
818 struct discover_boot_option *opt)
820 struct autoboot_option *auto_opt;
823 for (i = 0; i < config->n_autoboot_opts; i++) {
824 auto_opt = &config->autoboot_opts[i];
825 if (autoboot_option_matches(auto_opt, opt->device))
826 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
833 * We have different priorities to resolve conflicts between boot options that
834 * report to be the default for their device. This function assigns a priority
837 static enum default_priority default_option_priority(
838 struct device_handler *handler,
839 struct discover_boot_option *opt)
841 const struct config *config;
843 /* Temporary user-provided autoboot options have highest priority */
844 if (handler->temp_autoboot) {
845 if (autoboot_option_matches(handler->temp_autoboot,
847 return DEFAULT_PRIORITY_TEMP_USER;
849 pb_debug("handler: disabled default priority due to "
850 "temporary user override\n");
851 return DEFAULT_PRIORITY_DISABLED;
854 config = config_get();
856 /* Next highest priority to IPMI-configured boot options. If we have an
857 * IPMI bootdev configuration set, then we don't allow any other
859 if (config->ipmi_bootdev) {
860 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
861 opt->device->device->type);
863 return DEFAULT_PRIORITY_REMOTE;
865 pb_debug("handler: disabled default priority due to "
866 "non-matching IPMI type %x\n",
867 config->ipmi_bootdev);
868 return DEFAULT_PRIORITY_DISABLED;
871 /* Next, try to match the option against the user-defined autoboot
872 * options, either by device UUID or type. */
873 if (config->n_autoboot_opts) {
874 int boot_match = autoboot_option_priority(config, opt);
878 /* If there is no specific boot order, boot any device */
879 return DEFAULT_PRIORITY_LOCAL_FIRST;
882 /* If the option didn't match any entry in the array, it is disabled */
883 pb_debug("handler: disabled default priority due to "
884 "non-matching UUID or type\n");
885 return DEFAULT_PRIORITY_DISABLED;
888 static void set_default(struct device_handler *handler,
889 struct discover_boot_option *opt)
891 enum default_priority cur_prio, new_prio;
893 if (!handler->autoboot_enabled)
896 pb_debug("handler: new default option: %s\n", opt->option->id);
898 new_prio = default_option_priority(handler, opt);
900 /* Anything outside our range prevents a default boot */
901 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
904 pb_debug("handler: calculated priority %d\n", new_prio);
906 /* Resolve any conflicts: if we have a new default option, it only
907 * replaces the current if it has a higher priority. */
908 if (handler->default_boot_option) {
910 cur_prio = handler->default_boot_option_priority;
912 if (new_prio < cur_prio) {
913 pb_log("handler: new prio %d beats "
914 "old prio %d for %s\n",
916 handler->default_boot_option
918 handler->default_boot_option = opt;
919 handler->default_boot_option_priority = new_prio;
920 /* extend the timeout a little, so the user sees some
921 * indication of the change */
922 handler->sec_to_boot += 2;
928 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
929 handler->default_boot_option = opt;
930 handler->default_boot_option_priority = new_prio;
932 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
933 opt->option->id, handler->sec_to_boot);
935 default_timeout(handler);
938 static char *autoboot_option_desc(void *ctx, const struct autoboot_option *opt)
940 const char *type, *val;
942 if (opt->boot_type == BOOT_DEVICE_TYPE) {
943 type = _("device type");
944 val = device_type_display_name(opt->type);
945 } else if (opt->boot_type == BOOT_DEVICE_UUID) {
946 type = _("device UUID");
949 type = _("unknown specifier");
953 return talloc_asprintf(ctx, "%s = %s", type, val);
956 void device_handler_apply_temp_autoboot(struct device_handler *handler,
957 struct autoboot_option *opt)
962 handler->temp_autoboot = talloc_steal(handler, opt);
964 desc = autoboot_option_desc(handler, opt);
965 device_handler_status_info(handler,
966 _("Applying temporary autoboot override: %s"),
970 if (!handler->autoboot_enabled)
973 if (!handler->default_boot_option)
976 if (autoboot_option_matches(opt, handler->default_boot_option->device))
979 /* cancel the default, and rescan available options */
980 device_handler_cancel_default(handler);
982 handler->autoboot_enabled = true;
984 for (i = 0; i < handler->n_devices; i++) {
985 struct discover_device *dev = handler->devices[i];
986 struct discover_boot_option *boot_opt;
988 if (!autoboot_option_matches(opt, dev))
991 list_for_each_entry(&dev->boot_options, boot_opt, list) {
992 if (boot_opt->option->is_default) {
993 set_default(handler, boot_opt);
1000 static bool resource_is_resolved(struct resource *res)
1002 return !res || res->resolved;
1005 /* We only use this in an assert, which will disappear if we're compiling
1006 * with NDEBUG, so we need the 'used' attribute for these builds */
1007 static bool __attribute__((used)) boot_option_is_resolved(
1008 struct discover_boot_option *opt)
1010 return resource_is_resolved(opt->boot_image) &&
1011 resource_is_resolved(opt->initrd) &&
1012 resource_is_resolved(opt->dtb) &&
1013 resource_is_resolved(opt->args_sig_file) &&
1014 resource_is_resolved(opt->icon);
1017 static bool resource_resolve(struct resource *res, const char *name,
1018 struct discover_boot_option *opt,
1019 struct device_handler *handler)
1021 struct parser *parser = opt->source;
1023 if (resource_is_resolved(res))
1026 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
1027 opt->option->id, name, parser->name);
1028 parser->resolve_resource(handler, res);
1030 return res->resolved;
1033 static bool boot_option_resolve(struct discover_boot_option *opt,
1034 struct device_handler *handler)
1036 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
1037 resource_resolve(opt->initrd, "initrd", opt, handler) &&
1038 resource_resolve(opt->dtb, "dtb", opt, handler) &&
1039 resource_resolve(opt->args_sig_file, "args_sig_file", opt,
1041 resource_resolve(opt->icon, "icon", opt, handler);
1044 static void boot_option_finalise(struct device_handler *handler,
1045 struct discover_boot_option *opt)
1047 assert(boot_option_is_resolved(opt));
1049 /* check that the parsers haven't set any of the final data */
1050 assert(!opt->option->boot_image_file);
1051 assert(!opt->option->initrd_file);
1052 assert(!opt->option->dtb_file);
1053 assert(!opt->option->icon_file);
1054 assert(!opt->option->device_id);
1055 assert(!opt->option->args_sig_file);
1057 if (opt->boot_image)
1058 opt->option->boot_image_file = opt->boot_image->url->full;
1060 opt->option->initrd_file = opt->initrd->url->full;
1062 opt->option->dtb_file = opt->dtb->url->full;
1064 opt->option->icon_file = opt->icon->url->full;
1065 if (opt->args_sig_file)
1066 opt->option->args_sig_file = opt->args_sig_file->url->full;
1068 opt->option->device_id = opt->device->device->id;
1070 if (opt->option->is_default)
1071 set_default(handler, opt);
1074 static void notify_boot_option(struct device_handler *handler,
1075 struct discover_boot_option *opt)
1077 struct discover_device *dev = opt->device;
1080 discover_server_notify_device_add(handler->server,
1081 opt->device->device);
1082 dev->notified = true;
1083 discover_server_notify_boot_option_add(handler->server, opt->option);
1086 static void process_boot_option_queue(struct device_handler *handler)
1088 struct discover_boot_option *opt, *tmp;
1090 list_for_each_entry_safe(&handler->unresolved_boot_options,
1093 pb_debug("queue: attempting resolution for %s\n",
1096 if (!boot_option_resolve(opt, handler))
1099 pb_debug("\tresolved!\n");
1101 list_remove(&opt->list);
1102 list_add_tail(&opt->device->boot_options, &opt->list);
1103 talloc_steal(opt->device, opt);
1104 boot_option_finalise(handler, opt);
1105 notify_boot_option(handler, opt);
1109 struct discover_context *device_handler_discover_context_create(
1110 struct device_handler *handler,
1111 struct discover_device *device)
1113 struct discover_context *ctx;
1115 ctx = talloc_zero(handler, struct discover_context);
1116 ctx->handler = handler;
1117 ctx->device = device;
1118 list_init(&ctx->boot_options);
1123 void device_handler_add_device(struct device_handler *handler,
1124 struct discover_device *device)
1126 handler->n_devices++;
1127 handler->devices = talloc_realloc(handler, handler->devices,
1128 struct discover_device *, handler->n_devices);
1129 handler->devices[handler->n_devices - 1] = device;
1131 if (device->device->type == DEVICE_TYPE_NETWORK)
1132 network_register_device(handler->network, device);
1135 void device_handler_add_ramdisk(struct device_handler *handler,
1138 struct ramdisk_device *dev;
1144 for (i = 0; i < handler->n_ramdisks; i++)
1145 if (!strcmp(handler->ramdisks[i]->path, path))
1148 dev = talloc_zero(handler, struct ramdisk_device);
1150 pb_log("Failed to allocate memory to track %s\n", path);
1154 dev->path = talloc_strdup(handler, path);
1156 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
1157 struct ramdisk_device *,
1158 handler->n_ramdisks + 1);
1159 if (!handler->ramdisks) {
1160 pb_log("Failed to reallocate memory"
1161 "- ramdisk tracking inconsistent!\n");
1165 handler->ramdisks[i] = dev;
1166 i = handler->n_ramdisks++;
1169 struct ramdisk_device *device_handler_get_ramdisk(
1170 struct device_handler *handler)
1176 /* Check if free ramdisk exists */
1177 for (i = 0; i < handler->n_ramdisks; i++)
1178 if (!handler->ramdisks[i]->snapshot &&
1179 !handler->ramdisks[i]->origin &&
1180 !handler->ramdisks[i]->base)
1181 return handler->ramdisks[i];
1183 /* Otherwise create a new one */
1184 name = talloc_asprintf(handler, "/dev/ram%d",
1185 handler->n_ramdisks);
1187 pb_debug("Failed to allocate memory to name /dev/ram%d",
1188 handler->n_ramdisks);
1192 id = makedev(1, handler->n_ramdisks);
1193 if (mknod(name, S_IFBLK, id)) {
1194 if (errno == EEXIST) {
1195 /* We haven't yet received updates for existing
1196 * ramdisks - add and use this one */
1197 pb_debug("Using untracked ramdisk %s\n", name);
1199 pb_log("Failed to create new ramdisk %s: %s\n",
1200 name, strerror(errno));
1204 device_handler_add_ramdisk(handler, name);
1207 return handler->ramdisks[i];
1210 void device_handler_release_ramdisk(struct discover_device *device)
1212 struct ramdisk_device *ramdisk = device->ramdisk;
1214 talloc_free(ramdisk->snapshot);
1215 talloc_free(ramdisk->origin);
1216 talloc_free(ramdisk->base);
1218 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
1219 ramdisk->sectors = 0;
1221 device->ramdisk = NULL;
1224 /* Start discovery on a hotplugged device. The device will be in our devices
1225 * array, but has only just been initialised by the hotplug source.
1227 int device_handler_discover(struct device_handler *handler,
1228 struct discover_device *dev)
1230 struct discover_context *ctx;
1233 device_handler_status_dev_info(handler, dev,
1235 * TRANSLATORS: this string will be passed the type of the
1236 * device (eg "disk" or "network"), which will be translated
1239 _("Processing new %s device"),
1240 device_type_display_name(dev->device->type));
1242 /* create our context */
1243 ctx = device_handler_discover_context_create(handler, dev);
1245 rc = mount_device(dev);
1249 /* add this device to our system info */
1250 system_info_register_blockdev(dev->device->id, dev->uuid,
1253 /* run the parsers. This will populate the ctx's boot_option list. */
1254 iterate_parsers(ctx);
1256 /* add discovered stuff to the handler */
1257 device_handler_discover_context_commit(handler, ctx);
1259 process_boot_option_queue(handler);
1261 /* Check this device for pb-plugins */
1262 device_handler_plugin_scan_device(handler, dev);
1264 talloc_unlink(handler, ctx);
1269 struct requery_data {
1270 struct device_handler *handler;
1271 struct discover_device *device;
1274 static int device_handler_requery_timeout_fn(void *data)
1276 struct discover_boot_option *opt, *tmp;
1277 struct requery_data *rqd = data;
1278 struct device_handler *handler;
1279 struct discover_device *device;
1281 handler = rqd->handler;
1282 device = rqd->device;
1286 /* network_requery_device may re-add a timeout, so clear the device
1287 * waiter here, so we can potentially start a new one. */
1288 device->requery_waiter = NULL;
1290 /* We keep the device around, but get rid of the parsed boot
1291 * options on that device. That involves delaring out the lists,
1292 * and potentially cancelling a default.
1294 list_for_each_entry_safe(&handler->unresolved_boot_options,
1296 if (opt->device != device)
1298 list_remove(&opt->list);
1302 list_for_each_entry_safe(&device->boot_options, opt, tmp, list) {
1303 if (opt == handler->default_boot_option) {
1304 pb_log("Default option %s cancelled since device is being requeried",
1306 device_handler_cancel_default(handler);
1308 list_remove(&opt->list);
1312 discover_server_notify_device_remove(handler->server, device->device);
1313 device->notified = false;
1315 network_requery_device(handler->network, device);
1320 /* Schedule a requery in timeout (seconds).
1322 * Special values of timeout:
1326 void device_handler_start_requery_timeout( struct device_handler *handler,
1327 struct discover_device *dev, int timeout)
1329 struct requery_data *rqd;
1331 if (dev->requery_waiter)
1335 timeout = default_rescan_timeout;
1336 else if (timeout == 0)
1339 rqd = talloc(dev, struct requery_data);
1340 rqd->handler = handler;
1343 pb_debug("starting requery timeout for device %s, in %d sec\n",
1344 dev->device->id, timeout);
1346 dev->requery_waiter = waiter_register_timeout(handler->waitset,
1347 timeout * 1000, device_handler_requery_timeout_fn, rqd);
1350 static int event_requery_timeout(struct event *event)
1360 str = event_get_param(event, "reboottime");
1364 x = strtoul(str, &endp, 0);
1372 /* Incoming dhcp event */
1373 int device_handler_dhcp(struct device_handler *handler,
1374 struct discover_device *dev, struct event *event)
1376 struct discover_context *ctx;
1378 device_handler_status_dev_info(handler, dev,
1379 _("Processing DHCP lease response (ip: %s)"),
1380 event_get_param(event, "ip"));
1382 pending_network_jobs_start();
1384 /* create our context */
1385 ctx = device_handler_discover_context_create(handler, dev);
1386 talloc_steal(ctx, event);
1389 device_handler_start_requery_timeout(handler, dev,
1390 event_requery_timeout(event));
1392 iterate_parsers(ctx);
1394 device_handler_discover_context_commit(handler, ctx);
1396 talloc_unlink(handler, ctx);
1401 static struct discover_boot_option *find_boot_option_by_id(
1402 struct device_handler *handler, const char *id)
1406 for (i = 0; i < handler->n_devices; i++) {
1407 struct discover_device *dev = handler->devices[i];
1408 struct discover_boot_option *opt;
1410 list_for_each_entry(&dev->boot_options, opt, list)
1411 if (!strcmp(opt->option->id, id))
1418 void device_handler_boot(struct device_handler *handler,
1419 struct boot_command *cmd)
1421 struct discover_boot_option *opt = NULL;
1423 if (cmd->option_id && strlen(cmd->option_id))
1424 opt = find_boot_option_by_id(handler, cmd->option_id);
1426 if (handler->pending_boot)
1427 boot_cancel(handler->pending_boot);
1429 platform_pre_boot();
1431 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
1432 device_handler_boot_status_cb, handler);
1433 handler->pending_boot_is_default = false;
1436 void device_handler_cancel_default(struct device_handler *handler)
1438 if (handler->timeout_waiter)
1439 waiter_remove(handler->timeout_waiter);
1441 handler->timeout_waiter = NULL;
1442 handler->autoboot_enabled = false;
1444 /* we only send status if we had a default boot option queued */
1445 if (!handler->default_boot_option)
1448 pb_log("Cancelling default boot option\n");
1450 if (handler->pending_boot && handler->pending_boot_is_default) {
1451 boot_cancel(handler->pending_boot);
1452 handler->pending_boot = NULL;
1453 handler->pending_boot_is_default = false;
1456 handler->default_boot_option = NULL;
1458 device_handler_status_info(handler, _("Default boot cancelled"));
1461 void device_handler_update_config(struct device_handler *handler,
1462 struct config *config)
1466 rc = config_set(config);
1470 discover_server_notify_config(handler->server, config);
1471 device_handler_update_lang(config->lang);
1472 device_handler_reinit(handler);
1475 static char *device_from_addr(void *ctx, struct pb_url *url)
1477 char *ipaddr, *buf, *tok, *dev = NULL;
1478 const char *delim = " ";
1479 struct sockaddr_in *ip;
1480 struct sockaddr_in si;
1481 struct addrinfo *res;
1485 /* Note: IPv4 only */
1486 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1490 /* need to turn hostname into a valid IP */
1491 rc = getaddrinfo(url->host, NULL, NULL, &res);
1493 pb_debug("%s: Invalid URL\n",__func__);
1496 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1497 ip = (struct sockaddr_in *) res->ai_addr;
1498 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1502 const char *argv[] = {
1504 "route", "show", "to", "match",
1509 p = process_create(ctx);
1511 p->path = pb_system_apps.ip;
1513 p->keep_stdout = true;
1515 rc = process_run_sync(p);
1517 if (rc || p->exit_status) {
1518 /* ip has complained for some reason; most likely
1519 * there is no route to the host - bail out */
1520 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1521 pb_debug("ip buf: %s\n", p->stdout_buf);
1526 buf = p->stdout_buf;
1527 /* If a route is found, ip-route output will be of the form
1528 * "... dev DEVNAME ... " */
1529 tok = strtok(buf, delim);
1531 if (!strcmp(tok, "dev")) {
1532 tok = strtok(NULL, delim);
1533 dev = talloc_strdup(ctx, tok);
1536 tok = strtok(NULL, delim);
1541 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1545 static void process_url_cb(struct load_url_result *result, void *data)
1547 struct device_handler *handler;
1548 struct discover_context *ctx;
1549 struct discover_device *dev;
1550 struct event *event = data;
1553 if (result->status != LOAD_OK) {
1554 pb_log("%s: Load failed for %s\n", __func__, result->url->full);
1561 handler = talloc_parent(event);
1565 event->device = device_from_addr(event, result->url);
1566 if (!event->device) {
1567 pb_log("Downloaded a file but can't find its interface - pretending it was local\n");
1568 event->device = talloc_asprintf(event, "local");
1571 mac = event_get_param(event, "mac");
1572 char *url = talloc_asprintf(event, "file://%s", result->local);
1573 event_set_param(event, "pxeconffile-local", url);
1575 dev = discover_device_create(handler, mac, event->device);
1576 ctx = device_handler_discover_context_create(handler, dev);
1577 talloc_steal(ctx, event);
1580 iterate_parsers(ctx);
1582 device_handler_discover_context_commit(handler, ctx);
1584 talloc_unlink(handler, ctx);
1587 void device_handler_process_url(struct device_handler *handler,
1588 const char *url, const char *mac, const char *ip)
1590 struct discover_context *ctx;
1591 struct discover_device *dev;
1592 bool allow_async = false;
1593 struct pb_url *pb_url;
1594 struct event *event;
1596 event = talloc_zero(handler, struct event);
1597 event->type = EVENT_TYPE_USER;
1598 event->action = EVENT_ACTION_URL;
1600 pb_url = pb_url_parse(event, url);
1601 if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1602 device_handler_status_err(handler, _("Invalid config URL!"));
1607 if (url[strlen(url) - 1] == '/') {
1608 event_set_param(event, "pxepathprefix", url);
1609 event_set_param(event, "mac", mac);
1610 event_set_param(event, "ip", ip);
1611 event->device = device_from_addr(event, pb_url);
1612 if (!event->device) {
1613 device_handler_status_err(handler,
1614 _("Unable to route to host %s"),
1620 event_set_param(event, "pxeconffile", url);
1624 if (pb_url->scheme == pb_url_file)
1625 event->device = talloc_asprintf(event, "local");
1626 else if (allow_async) {
1627 /* If file is remote load asynchronously before passing to
1628 * parser. This allows us to wait for network to be available */
1629 if (!load_url_async(handler, pb_url, process_url_cb, event,
1631 pb_log("Failed to load url %s\n", pb_url->full);
1632 device_handler_status_err(handler, _("Failed to load URL!"));
1638 /* If path is local we can parse straight away */
1640 dev = discover_device_create(handler, mac, event->device);
1641 if (pb_url->scheme == pb_url_file)
1642 dev->device->type = DEVICE_TYPE_ANY;
1643 ctx = device_handler_discover_context_create(handler, dev);
1644 talloc_steal(ctx, event);
1647 iterate_parsers(ctx);
1649 device_handler_discover_context_commit(handler, ctx);
1651 talloc_unlink(handler, ctx);
1654 static void plugin_install_cb(struct process *process)
1656 struct device_handler *handler = process->data;
1659 pb_log("%s: Missing data!\n", __func__);
1663 handler->plugin_installing = false;
1664 if (process->exit_status) {
1665 device_handler_status_err(handler, "Plugin failed to install!");
1666 pb_log("Failed to install plugin:\n%s\n", process->stdout_buf);
1670 void device_handler_install_plugin(struct device_handler *handler,
1671 const char *plugin_file)
1676 if (handler->plugin_installing) {
1677 pb_log("Plugin install cancelled - install already running");
1681 p = process_create(handler);
1683 pb_log("install_plugin: Failed to create process\n");
1687 const char *argv[] = {
1688 pb_system_apps.pb_plugin,
1695 p->path = pb_system_apps.pb_plugin;
1697 p->exit_cb = plugin_install_cb;
1699 p->keep_stdout = true;
1701 result = process_run_async(p);
1704 device_handler_status_err(handler, "Could not install plugin");
1706 handler->plugin_installing = true;
1709 #ifndef PETITBOOT_TEST
1712 * context_commit - Commit a temporary discovery context to the handler,
1713 * and notify the clients about any new options / devices
1715 void device_handler_discover_context_commit(struct device_handler *handler,
1716 struct discover_context *ctx)
1718 struct discover_device *dev = ctx->device;
1719 struct discover_boot_option *opt, *tmp;
1721 if (!device_lookup_by_uuid(handler, dev->uuid))
1722 device_handler_add_device(handler, dev);
1724 /* move boot options from the context to the device */
1725 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
1726 list_remove(&opt->list);
1728 /* All boot options need at least a kernel image */
1729 if (!opt->boot_image || !opt->boot_image->url) {
1730 pb_log("boot option %s is missing boot image, ignoring\n",
1736 if (boot_option_resolve(opt, handler)) {
1737 pb_log("boot option %s is resolved, "
1738 "sending to clients\n",
1740 list_add_tail(&dev->boot_options, &opt->list);
1741 talloc_steal(dev, opt);
1742 boot_option_finalise(handler, opt);
1743 notify_boot_option(handler, opt);
1745 if (!opt->source->resolve_resource) {
1746 pb_log("parser %s gave us an unresolved "
1747 "resource (%s), but no way to "
1749 opt->source->name, opt->option->id);
1752 pb_log("boot option %s is unresolved, "
1753 "adding to queue\n",
1755 list_add(&handler->unresolved_boot_options,
1757 talloc_steal(handler, opt);
1763 void device_handler_add_plugin_option(struct device_handler *handler,
1764 struct plugin_option *opt)
1766 struct plugin_option *tmp;
1769 for (i = 0; i < handler->n_plugins; i++) {
1770 tmp = handler->plugins[i];
1771 /* If both id and version match, ignore */
1772 if (strncmp(opt->id, tmp->id, strlen(opt->id)) == 0 &&
1773 strcmp(opt->version, tmp->version) == 0) {
1774 pb_log("discover: Plugin '%s' already exists, ignoring\n",
1780 handler->plugins = talloc_realloc(handler, handler->plugins,
1781 struct plugin_option *, handler->n_plugins + 1);
1782 if (!handler->plugins) {
1783 pb_log("Failed to allocate memory for new plugin\n");
1784 handler->n_plugins = 0;
1788 handler->plugins[handler->n_plugins++] = opt;
1789 discover_server_notify_plugin_option_add(handler->server, opt);
1792 static void device_handler_update_lang(const char *lang)
1794 const char *cur_lang;
1799 cur_lang = setlocale(LC_ALL, NULL);
1800 if (cur_lang && !strcmp(cur_lang, lang))
1803 setlocale(LC_ALL, lang);
1806 static int device_handler_init_sources(struct device_handler *handler)
1808 /* init our device sources: udev, network and user events */
1809 handler->user_event = user_event_init(handler, handler->waitset);
1810 if (!handler->user_event)
1813 handler->network = network_init(handler, handler->waitset,
1815 if (!handler->network)
1818 handler->udev = udev_init(handler, handler->waitset);
1825 static void device_handler_reinit_sources(struct device_handler *handler)
1827 /* if we haven't initialised sources previously (becuase we started in
1828 * safe mode), then init once here. */
1829 if (!(handler->udev || handler->network || handler->user_event)) {
1830 device_handler_init_sources(handler);
1834 system_info_reinit();
1836 network_shutdown(handler->network);
1837 handler->network = network_init(handler, handler->waitset,
1840 udev_reinit(handler->udev);
1843 static inline const char *get_device_path(struct discover_device *dev)
1845 return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1848 static char *check_subvols(struct discover_device *dev)
1850 const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1855 if (strncmp(fstype, "btrfs", strlen("btrfs")))
1856 return dev->mount_path;
1858 /* On btrfs a device's root may be under a subvolume path */
1859 path = join_paths(dev, dev->mount_path, "@");
1860 rc = stat(path, &sb);
1861 if (!rc && S_ISDIR(sb.st_mode)) {
1862 pb_debug("Using '%s' for btrfs root path\n", path);
1867 return dev->mount_path;
1870 static bool check_existing_mount(struct discover_device *dev)
1872 struct stat devstat, mntstat;
1873 const char *device_path;
1878 device_path = get_device_path(dev);
1880 rc = stat(device_path, &devstat);
1882 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1886 if (!S_ISBLK(devstat.st_mode)) {
1887 pb_debug("%s: %s isn't a block device?\n", __func__,
1892 fp = fopen("/proc/self/mounts", "r");
1895 mnt = getmntent(fp);
1899 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1902 rc = stat(mnt->mnt_fsname, &mntstat);
1906 if (!S_ISBLK(mntstat.st_mode))
1909 if (mntstat.st_rdev == devstat.st_rdev) {
1910 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1911 dev->root_path = check_subvols(dev);
1912 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1913 dev->mounted = true;
1914 dev->unmount = false;
1916 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1917 __func__, dev->device_path,
1918 dev->mounted_rw ? 'w' : 'o',
1930 * Attempt to mount a filesystem safely, while handling certain filesytem-
1933 static int try_mount(const char *device_path, const char *mount_path,
1934 const char *fstype, unsigned long flags,
1937 const char *fs, *safe_opts;
1940 /* Mount ext3 as ext4 instead so 'norecovery' can be used */
1941 if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
1942 pb_debug("Mounting ext3 filesystem as ext4\n");
1947 if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
1948 strncmp(fs, "ext4", strlen("ext4")) == 0)
1949 safe_opts = "norecovery";
1954 /* If no snapshot is available don't attempt recovery */
1956 return mount(device_path, mount_path, fs, flags, safe_opts);
1958 rc = mount(device_path, mount_path, fs, flags, NULL);
1963 /* Mounting failed; some filesystems will fail to mount if a recovery
1964 * journal exists (eg. cross-endian XFS), so try again with norecovery
1965 * where that option is available.
1966 * If mounting read-write just return the error as norecovery is not a
1968 if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
1972 return mount(device_path, mount_path, fs, flags, safe_opts);
1975 static int mount_device(struct discover_device *dev)
1977 const char *fstype, *device_path;
1980 if (!dev->device_path)
1986 if (check_existing_mount(dev))
1989 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1993 dev->mount_path = join_paths(dev, mount_base(),
1996 if (pb_mkdir_recursive(dev->mount_path)) {
1997 pb_log("couldn't create mount directory %s: %s\n",
1998 dev->mount_path, strerror(errno));
2002 device_path = get_device_path(dev);
2004 pb_log("mounting device %s read-only\n", dev->device_path);
2005 rc = try_mount(device_path, dev->mount_path, fstype,
2006 MS_RDONLY | MS_SILENT, dev->ramdisk);
2008 /* If mount fails clean up any snapshot and try again */
2009 if (rc && dev->ramdisk) {
2010 pb_log("couldn't mount snapshot for %s: mount failed: %s\n",
2011 device_path, strerror(errno));
2012 pb_log("falling back to actual device\n");
2014 devmapper_destroy_snapshot(dev);
2016 device_path = get_device_path(dev);
2017 pb_log("mounting device %s read-only\n", dev->device_path);
2018 rc = try_mount(device_path, dev->mount_path, fstype,
2019 MS_RDONLY | MS_SILENT, dev->ramdisk);
2023 dev->mounted = true;
2024 dev->mounted_rw = false;
2025 dev->unmount = true;
2026 dev->root_path = check_subvols(dev);
2030 pb_log("couldn't mount device %s: mount failed: %s\n",
2031 device_path, strerror(errno));
2033 pb_rmdir_recursive(mount_base(), dev->mount_path);
2035 talloc_free(dev->mount_path);
2036 dev->mount_path = NULL;
2040 static int umount_device(struct discover_device *dev)
2042 const char *device_path;
2045 if (!dev->mounted || !dev->unmount)
2048 device_path = get_device_path(dev);
2050 pb_log("unmounting device %s\n", device_path);
2051 rc = umount(dev->mount_path);
2055 dev->mounted = false;
2056 devmapper_destroy_snapshot(dev);
2058 pb_rmdir_recursive(mount_base(), dev->mount_path);
2060 talloc_free(dev->mount_path);
2061 dev->mount_path = NULL;
2062 dev->root_path = NULL;
2067 int device_request_write(struct discover_device *dev, bool *release)
2069 const char *fstype, *device_path;
2070 const struct config *config;
2075 config = config_get();
2076 if (!config->allow_writes)
2082 if (dev->mounted_rw)
2085 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
2087 device_path = get_device_path(dev);
2089 pb_log("remounting device %s read-write\n", device_path);
2091 rc = umount(dev->mount_path);
2093 pb_log("Failed to unmount %s: %s\n",
2094 dev->mount_path, strerror(errno));
2098 rc = try_mount(device_path, dev->mount_path, fstype,
2099 MS_SILENT, dev->ramdisk);
2103 dev->mounted_rw = true;
2108 pb_log("Unable to remount device %s read-write: %s\n",
2109 device_path, strerror(errno));
2110 rc = try_mount(device_path, dev->mount_path, fstype,
2111 MS_RDONLY | MS_SILENT, dev->ramdisk);
2113 pb_log("Unable to recover mount for %s: %s\n",
2114 device_path, strerror(errno));
2118 void device_release_write(struct discover_device *dev, bool release)
2120 const char *fstype, *device_path;
2125 device_path = get_device_path(dev);
2127 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
2129 pb_log("remounting device %s read-only\n", device_path);
2131 if (umount(dev->mount_path)) {
2132 pb_log("Failed to unmount %s\n", dev->mount_path);
2135 dev->mounted_rw = dev->mounted = false;
2138 devmapper_merge_snapshot(dev);
2139 /* device_path becomes stale after merge */
2140 device_path = get_device_path(dev);
2143 if (try_mount(device_path, dev->mount_path, fstype,
2144 MS_RDONLY | MS_SILENT, dev->ramdisk))
2145 pb_log("Failed to remount %s read-only: %s\n",
2146 device_path, strerror(errno));
2148 dev->mounted = true;
2151 void device_sync_snapshots(struct device_handler *handler, const char *device)
2153 struct discover_device *dev = NULL;
2157 /* Find matching device and sync */
2158 dev = device_lookup_by_name(handler, device);
2160 pb_log("%s: device name '%s' unrecognised\n",
2165 device_release_write(dev, true);
2167 pb_log("%s has no snapshot to merge, skipping\n",
2172 /* Otherwise sync all relevant devices */
2173 for (i = 0; i < handler->n_devices; i++) {
2174 dev = handler->devices[i];
2175 if (dev->device->type != DEVICE_TYPE_DISK &&
2176 dev->device->type != DEVICE_TYPE_USB)
2179 device_release_write(dev, true);
2181 pb_log("%s has no snapshot to merge, skipping\n",
2188 void device_handler_discover_context_commit(
2189 struct device_handler *handler __attribute__((unused)),
2190 struct discover_context *ctx __attribute__((unused)))
2192 pb_log("%s stubbed out for test cases\n", __func__);
2195 static void device_handler_update_lang(const char *lang __attribute__((unused)))
2199 static int device_handler_init_sources(
2200 struct device_handler *handler __attribute__((unused)))
2205 static void device_handler_reinit_sources(
2206 struct device_handler *handler __attribute__((unused)))
2210 static int umount_device(struct discover_device *dev __attribute__((unused)))
2215 static int __attribute__((unused)) mount_device(
2216 struct discover_device *dev __attribute__((unused)))
2221 int device_request_write(struct discover_device *dev __attribute__((unused)),
2228 void device_release_write(struct discover_device *dev __attribute__((unused)),
2229 bool release __attribute__((unused)))
2233 void device_sync_snapshots(
2234 struct device_handler *handler __attribute__((unused)),
2235 const char *device __attribute__((unused)))