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 struct discover_boot_option *last_boot_option;
85 int default_boot_option_priority;
87 struct list unresolved_boot_options;
89 struct boot_task *pending_boot;
90 bool pending_boot_is_default;
93 unsigned int n_progress;
95 struct plugin_option **plugins;
96 unsigned int n_plugins;
97 bool plugin_installing;
100 static int mount_device(struct discover_device *dev);
101 static int umount_device(struct discover_device *dev);
103 static int device_handler_init_sources(struct device_handler *handler);
104 static void device_handler_reinit_sources(struct device_handler *handler);
106 static void device_handler_update_lang(const char *lang);
108 void discover_context_add_boot_option(struct discover_context *ctx,
109 struct discover_boot_option *boot_option)
111 boot_option->source = ctx->parser;
112 list_add_tail(&ctx->boot_options, &boot_option->list);
113 talloc_steal(ctx, boot_option);
117 * device_handler_get_device_count - Get the count of current handler devices.
120 int device_handler_get_device_count(const struct device_handler *handler)
122 return handler->n_devices;
126 * device_handler_get_device - Get a handler device by index.
129 const struct discover_device *device_handler_get_device(
130 const struct device_handler *handler, unsigned int index)
132 if (index >= handler->n_devices) {
133 assert(0 && "bad index");
137 return handler->devices[index];
141 * device_handler_get_plugin_count - Get the count of current handler plugins.
143 int device_handler_get_plugin_count(const struct device_handler *handler)
145 return handler->n_plugins;
149 * discover_handler_get_plugin - Get a handler plugin by index.
151 const struct plugin_option *device_handler_get_plugin(
152 const struct device_handler *handler, unsigned int index)
154 if (index >= handler->n_plugins) {
155 assert(0 && "bad index");
159 return handler->plugins[index];
162 struct network *device_handler_get_network(
163 const struct device_handler *handler)
165 return handler->network;
168 struct discover_boot_option *discover_boot_option_create(
169 struct discover_context *ctx,
170 struct discover_device *device)
172 struct discover_boot_option *opt;
174 opt = talloc_zero(ctx, struct discover_boot_option);
175 opt->option = talloc_zero(opt, struct boot_option);
176 opt->device = device;
181 static int device_match_uuid(struct discover_device *dev, const char *uuid)
183 return dev->uuid && !strcmp(dev->uuid, uuid);
186 static int device_match_label(struct discover_device *dev, const char *label)
188 return dev->label && !strcmp(dev->label, label);
191 static int device_match_id(struct discover_device *dev, const char *id)
193 return !strcmp(dev->device->id, id);
196 static int device_match_serial(struct discover_device *dev, const char *serial)
198 const char *val = discover_device_get_param(dev, "ID_SERIAL");
199 return val && !strcmp(val, serial);
202 static struct discover_device *device_lookup(
203 struct device_handler *device_handler,
204 int (match_fn)(struct discover_device *, const char *),
207 struct discover_device *dev;
213 for (i = 0; i < device_handler->n_devices; i++) {
214 dev = device_handler->devices[i];
216 if (match_fn(dev, str))
223 struct discover_device *device_lookup_by_name(struct device_handler *handler,
226 if (!strncmp(name, "/dev/", strlen("/dev/")))
227 name += strlen("/dev/");
229 return device_lookup_by_id(handler, name);
232 struct discover_device *device_lookup_by_uuid(
233 struct device_handler *device_handler,
236 return device_lookup(device_handler, device_match_uuid, uuid);
239 struct discover_device *device_lookup_by_label(
240 struct device_handler *device_handler,
243 return device_lookup(device_handler, device_match_label, label);
246 struct discover_device *device_lookup_by_id(
247 struct device_handler *device_handler,
250 return device_lookup(device_handler, device_match_id, id);
253 struct discover_device *device_lookup_by_serial(
254 struct device_handler *device_handler,
257 return device_lookup(device_handler, device_match_serial, serial);
260 void device_handler_destroy(struct device_handler *handler)
262 talloc_free(handler);
265 static int destroy_device(void *arg)
267 struct discover_device *dev = arg;
274 struct discover_device *discover_device_create(struct device_handler *handler,
275 const char *uuid, const char *id)
277 struct discover_device *dev;
280 dev = device_lookup_by_uuid(handler, uuid);
282 dev = device_lookup_by_id(handler, id);
287 dev = talloc_zero(handler, struct discover_device);
288 dev->device = talloc_zero(dev, struct device);
289 dev->device->id = talloc_strdup(dev->device, id);
290 dev->uuid = talloc_strdup(dev, uuid);
291 list_init(&dev->params);
292 list_init(&dev->boot_options);
294 talloc_set_destructor(dev, destroy_device);
299 struct discover_device_param {
302 struct list_item list;
305 void discover_device_set_param(struct discover_device *device,
306 const char *name, const char *value)
308 struct discover_device_param *param;
311 list_for_each_entry(&device->params, param, list) {
312 if (!strcmp(param->name, name)) {
321 param = talloc(device, struct discover_device_param);
322 param->name = talloc_strdup(param, name);
323 list_add(&device->params, ¶m->list);
326 list_remove(¶m->list);
330 talloc_free(param->value);
333 param->value = talloc_strdup(param, value);
336 const char *discover_device_get_param(struct discover_device *device,
339 struct discover_device_param *param;
341 list_for_each_entry(&device->params, param, list) {
342 if (!strcmp(param->name, name))
348 static void set_env_variables(const struct config *config)
350 if (config->http_proxy)
351 setenv("http_proxy", config->http_proxy, 1);
353 unsetenv("http_proxy");
355 if (config->https_proxy)
356 setenv("https_proxy", config->https_proxy, 1);
358 unsetenv("https_proxy");
360 /* Reduce noise in the log from LVM listing open file descriptors */
361 setenv("LVM_SUPPRESS_FD_WARNINGS", "1", 1);
364 struct device_handler *device_handler_init(struct discover_server *server,
365 struct waitset *waitset, int dry_run)
367 struct device_handler *handler;
370 handler = talloc_zero(NULL, struct device_handler);
371 handler->server = server;
372 handler->waitset = waitset;
373 handler->dry_run = dry_run;
374 handler->autoboot_enabled = config_autoboot_active(config_get());
376 list_init(&handler->unresolved_boot_options);
378 list_init(&handler->progress);
380 /* set up our mount point base */
381 pb_mkdir_recursive(mount_base());
385 if (config_get()->safe_mode)
388 set_env_variables(config_get());
390 rc = device_handler_init_sources(handler);
392 talloc_free(handler);
399 void device_handler_reinit(struct device_handler *handler)
401 struct discover_boot_option *opt, *tmp;
402 struct ramdisk_device *ramdisk;
403 struct config *config;
406 device_handler_cancel_default(handler);
407 /* Cancel any pending non-default boot */
408 if (handler->pending_boot) {
409 boot_cancel(handler->pending_boot);
410 handler->pending_boot = NULL;
411 handler->pending_boot_is_default = false;
414 /* Cancel any remaining async jobs */
415 process_stop_async_all();
416 pending_network_jobs_cancel();
418 /* free unresolved boot options */
419 list_for_each_entry_safe(&handler->unresolved_boot_options,
422 list_init(&handler->unresolved_boot_options);
424 /* drop all devices */
425 for (i = 0; i < handler->n_devices; i++) {
426 struct discover_device *device = handler->devices[i];
427 discover_server_notify_device_remove(handler->server,
429 ramdisk = device->ramdisk;
430 if (device->requery_waiter)
431 waiter_remove(device->requery_waiter);
433 talloc_free(ramdisk);
436 talloc_free(handler->devices);
437 handler->devices = NULL;
438 handler->n_devices = 0;
439 talloc_free(handler->ramdisks);
440 handler->ramdisks = NULL;
441 handler->n_ramdisks = 0;
443 /* drop any known plugins */
444 for (i = 0; i < handler->n_plugins; i++)
445 talloc_free(handler->plugins[i]);
446 talloc_free(handler->plugins);
447 handler->plugins = NULL;
448 handler->n_plugins = 0;
450 discover_server_notify_plugins_remove(handler->server);
452 set_env_variables(config_get());
454 /* If the safe mode warning was active disable it now */
455 if (config_get()->safe_mode) {
456 config = config_copy(handler, config_get());
457 config->safe_mode = false;
459 discover_server_notify_config(handler->server, config);
462 /* Force rediscovery on SCSI devices */
463 process_run_simple(handler, pb_system_apps.scsi_rescan, NULL);
465 device_handler_reinit_sources(handler);
468 void device_handler_remove(struct device_handler *handler,
469 struct discover_device *device)
471 struct discover_boot_option *opt, *tmp;
474 if (device->requery_waiter)
475 waiter_remove(device->requery_waiter);
477 list_for_each_entry_safe(&device->boot_options, opt, tmp, list) {
478 if (opt == handler->default_boot_option) {
479 pb_log("Default option %s cancelled since device removed",
481 device_handler_cancel_default(handler);
486 for (i = 0; i < handler->n_devices; i++)
487 if (handler->devices[i] == device)
490 if (i == handler->n_devices) {
495 /* Free any unresolved options, as they're currently allocated
496 * against the handler */
497 list_for_each_entry_safe(&handler->unresolved_boot_options,
499 if (opt->device != device)
501 list_remove(&opt->list);
505 /* if this is a network device, we have to unregister it from the
507 if (device->device->type == DEVICE_TYPE_NETWORK)
508 network_unregister_device(handler->network, device);
510 handler->n_devices--;
511 memmove(&handler->devices[i], &handler->devices[i + 1],
512 (handler->n_devices - i) * sizeof(handler->devices[0]));
513 handler->devices = talloc_realloc(handler, handler->devices,
514 struct discover_device *, handler->n_devices);
516 if (device->notified)
517 discover_server_notify_device_remove(handler->server,
523 void device_handler_status(struct device_handler *handler,
524 struct status *status)
526 pb_debug("%s: %s\n", __func__, status->message);
527 discover_server_notify_boot_status(handler->server, status);
530 static void _device_handler_vstatus(struct device_handler *handler,
531 enum status_type type, const char *fmt, va_list ap)
533 struct status status;
536 status.message = talloc_vasprintf(handler, fmt, ap);
537 status.backlog = false;
538 status.boot_active = false;
540 device_handler_status(handler, &status);
542 talloc_free(status.message);
545 static void _device_handler_vdevstatus(struct device_handler *handler,
546 struct discover_device *device, enum status_type type,
547 const char *fmt, va_list ap)
551 msg = talloc_asprintf(handler, "[%s] %s",
552 device ? device->device->id : "unknown", fmt);
553 _device_handler_vstatus(handler, type, msg, ap);
557 void device_handler_status_dev_info(struct device_handler *handler,
558 struct discover_device *dev, const char *fmt, ...)
563 _device_handler_vdevstatus(handler, dev, STATUS_INFO, fmt, ap);
567 void device_handler_status_dev_err(struct device_handler *handler,
568 struct discover_device *dev, const char *fmt, ...)
573 _device_handler_vdevstatus(handler, dev, STATUS_ERROR, fmt, ap);
577 void device_handler_status_info(struct device_handler *handler,
578 const char *fmt, ...)
583 _device_handler_vstatus(handler, STATUS_INFO, fmt, ap);
587 void device_handler_status_err(struct device_handler *handler,
588 const char *fmt, ...)
593 _device_handler_vstatus(handler, STATUS_ERROR, fmt, ap);
597 void device_handler_status_download(struct device_handler *handler,
598 const struct process_info *procinfo,
599 unsigned int percentage, unsigned int size, char suffix)
601 struct progress_info *p, *progress = NULL;
602 uint64_t current_converted, current = 0;
603 const char *units = " kMGTP";
604 unsigned long size_bytes;
610 list_for_each_entry(&handler->progress, p, list)
611 if (p->procinfo == procinfo)
615 pb_log("Registering new progress struct\n");
616 progress = talloc_zero(handler, struct progress_info);
618 pb_log("Failed to allocate room for progress struct\n");
621 progress->procinfo = procinfo;
622 list_add(&handler->progress, &progress->list);
623 handler->n_progress++;
627 for (i = 0; i < strlen(units); i++) {
628 if (units[i] == suffix)
632 if (i >= strlen(units)) {
633 pb_log("Couldn't recognise suffix '%c'\n", suffix);
640 progress->percentage = percentage;
641 progress->size = size_bytes;
644 * Aggregate the info we have and update status. If a progress struct
645 * has zero for both percentage and size we assume progress information
646 * is unavailable and fall back to a generic progress message.
648 list_for_each_entry(&handler->progress, p, list) {
651 if (!p->percentage || !p->size) {
652 update = talloc_asprintf(handler,
653 _("%u downloads in progress..."),
654 handler->n_progress);
660 t = (100 * c) / p->percentage;
667 current_converted = current;
668 while (current_converted >= 1000) {
669 current_converted >>= 10;
672 update = talloc_asprintf(handler,
673 _("%u %s downloading: %.0f%% - %" PRIu64 "%cB"),
675 ngettext("item", "items", handler->n_progress),
676 (current / total) * 100, current_converted,
681 pb_log_fn("failed to allocate new status\n");
683 device_handler_status_info(handler, "%s\n", update);
688 static void device_handler_plugin_scan_device(struct device_handler *handler,
689 struct discover_device *dev)
693 pb_debug("Scanning %s for plugin files\n", dev->device->id);
695 rc = process_run_simple(handler, pb_system_apps.pb_plugin,
696 "scan", dev->mount_path,
699 pb_log("Error from pb-plugin scan %s\n",
703 void device_handler_status_download_remove(struct device_handler *handler,
704 struct process_info *procinfo)
706 struct progress_info *p, *tmp;
708 list_for_each_entry_safe(&handler->progress, p, tmp, list)
709 if (p->procinfo == procinfo) {
710 list_remove(&p->list);
712 handler->n_progress--;
716 static void device_handler_boot_status_cb(void *arg, struct status *status)
718 struct device_handler *handler = arg;
720 /* boot had failed; update handler state to allow a new default if one
723 if (status->type == STATUS_ERROR) {
724 handler->pending_boot = NULL;
725 handler->default_boot_option = NULL;
728 device_handler_status(handler, status);
731 static void countdown_status(struct device_handler *handler,
732 struct discover_boot_option *opt, unsigned int sec)
734 struct status status;
736 status.type = STATUS_INFO;
737 status.message = talloc_asprintf(handler,
738 _("Booting in %d sec: [%s] %s"), sec,
739 opt->device->device->id, opt->option->name);
740 status.backlog = false;
742 device_handler_status(handler, &status);
744 talloc_free(status.message);
747 static int default_timeout(void *arg)
749 struct device_handler *handler = arg;
750 struct discover_boot_option *opt;
752 if (!handler->default_boot_option)
755 if (handler->pending_boot)
758 opt = handler->default_boot_option;
760 handler->last_boot_option = opt;
762 if (handler->sec_to_boot) {
763 countdown_status(handler, opt, handler->sec_to_boot);
764 handler->sec_to_boot--;
765 handler->timeout_waiter = waiter_register_timeout(
766 handler->waitset, 1000,
767 default_timeout, handler);
771 handler->timeout_waiter = NULL;
773 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
777 handler->pending_boot = boot(handler, handler->default_boot_option,
778 NULL, handler->dry_run, device_handler_boot_status_cb,
780 handler->pending_boot_is_default = true;
785 enum ipmi_bootdev ipmi_type;
786 enum device_type device_type;
787 } device_type_map[] = {
788 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
789 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
790 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
791 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
794 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
795 enum device_type device_type)
799 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
800 if (device_type_map[i].device_type == device_type)
801 return device_type_map[i].ipmi_type == ipmi_type;
807 static bool autoboot_option_matches(struct autoboot_option *opt,
808 struct discover_device *dev)
810 if (opt->boot_type == BOOT_DEVICE_UUID)
811 if (!strcmp(opt->uuid, dev->uuid))
814 if (opt->boot_type == BOOT_DEVICE_TYPE)
815 if (opt->type == dev->device->type ||
816 opt->type == DEVICE_TYPE_ANY)
822 static int autoboot_option_priority(const struct config *config,
823 struct discover_boot_option *opt)
825 struct autoboot_option *auto_opt;
828 for (i = 0; i < config->n_autoboot_opts; i++) {
829 auto_opt = &config->autoboot_opts[i];
830 if (autoboot_option_matches(auto_opt, opt->device))
831 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
838 * We have different priorities to resolve conflicts between boot options that
839 * report to be the default for their device. This function assigns a priority
842 static enum default_priority default_option_priority(
843 struct device_handler *handler,
844 struct discover_boot_option *opt)
846 const struct config *config;
848 /* Temporary user-provided autoboot options have highest priority */
849 if (handler->temp_autoboot) {
850 if (autoboot_option_matches(handler->temp_autoboot,
852 return DEFAULT_PRIORITY_TEMP_USER;
854 pb_debug("handler: disabled default priority due to "
855 "temporary user override\n");
856 return DEFAULT_PRIORITY_DISABLED;
859 config = config_get();
861 /* Next highest priority to IPMI-configured boot options. If we have an
862 * IPMI bootdev configuration set, then we don't allow any other
864 if (config->ipmi_bootdev) {
865 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
866 opt->device->device->type);
868 return DEFAULT_PRIORITY_REMOTE;
870 pb_debug("handler: disabled default priority due to "
871 "non-matching IPMI type %x\n",
872 config->ipmi_bootdev);
873 return DEFAULT_PRIORITY_DISABLED;
876 /* Next, try to match the option against the user-defined autoboot
877 * options, either by device UUID or type. */
878 if (config->n_autoboot_opts) {
879 int boot_match = autoboot_option_priority(config, opt);
883 /* If there is no specific boot order, boot any device */
884 return DEFAULT_PRIORITY_LOCAL_FIRST;
887 /* If the option didn't match any entry in the array, it is disabled */
888 pb_debug("handler: disabled default priority due to "
889 "non-matching UUID or type\n");
890 return DEFAULT_PRIORITY_DISABLED;
893 static void set_default(struct device_handler *handler,
894 struct discover_boot_option *opt)
896 enum default_priority cur_prio, new_prio;
898 if (!handler->autoboot_enabled)
901 pb_debug("handler: new default option: %s\n", opt->option->id);
903 new_prio = default_option_priority(handler, opt);
905 /* Anything outside our range prevents a default boot */
906 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
909 pb_debug("handler: calculated priority %d\n", new_prio);
911 /* Resolve any conflicts: if we have a new default option, it only
912 * replaces the current if it has a higher priority. */
913 if (handler->default_boot_option) {
915 cur_prio = handler->default_boot_option_priority;
917 if (new_prio < cur_prio) {
918 pb_log("handler: new prio %d beats "
919 "old prio %d for %s\n",
921 handler->default_boot_option
923 handler->default_boot_option = opt;
924 handler->default_boot_option_priority = new_prio;
925 /* extend the timeout a little, so the user sees some
926 * indication of the change */
927 handler->sec_to_boot += 2;
933 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
934 handler->default_boot_option = opt;
935 handler->default_boot_option_priority = new_prio;
937 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
938 opt->option->id, handler->sec_to_boot);
940 default_timeout(handler);
943 static char *autoboot_option_desc(void *ctx, const struct autoboot_option *opt)
945 const char *type, *val;
947 if (opt->boot_type == BOOT_DEVICE_TYPE) {
948 type = _("device type");
949 val = device_type_display_name(opt->type);
950 } else if (opt->boot_type == BOOT_DEVICE_UUID) {
951 type = _("device UUID");
954 type = _("unknown specifier");
958 return talloc_asprintf(ctx, "%s = %s", type, val);
961 void device_handler_apply_temp_autoboot(struct device_handler *handler,
962 struct autoboot_option *opt)
967 handler->temp_autoboot = talloc_steal(handler, opt);
969 desc = autoboot_option_desc(handler, opt);
970 device_handler_status_info(handler,
971 _("Applying temporary autoboot override: %s"),
975 if (!handler->autoboot_enabled)
978 if (!handler->default_boot_option)
981 if (autoboot_option_matches(opt, handler->default_boot_option->device))
984 /* cancel the default, and rescan available options */
985 device_handler_cancel_default(handler);
987 handler->autoboot_enabled = true;
989 for (i = 0; i < handler->n_devices; i++) {
990 struct discover_device *dev = handler->devices[i];
991 struct discover_boot_option *boot_opt;
993 if (!autoboot_option_matches(opt, dev))
996 list_for_each_entry(&dev->boot_options, boot_opt, list) {
997 if (boot_opt->option->is_default) {
998 set_default(handler, boot_opt);
1005 static bool resource_is_resolved(struct resource *res)
1007 return !res || res->resolved;
1010 /* We only use this in an assert, which will disappear if we're compiling
1011 * with NDEBUG, so we need the 'used' attribute for these builds */
1012 static bool __attribute__((used)) boot_option_is_resolved(
1013 struct discover_boot_option *opt)
1015 return resource_is_resolved(opt->boot_image) &&
1016 resource_is_resolved(opt->initrd) &&
1017 resource_is_resolved(opt->dtb) &&
1018 resource_is_resolved(opt->args_sig_file) &&
1019 resource_is_resolved(opt->icon);
1022 static bool resource_resolve(struct resource *res, const char *name,
1023 struct discover_boot_option *opt,
1024 struct device_handler *handler)
1026 struct parser *parser = opt->source;
1028 if (resource_is_resolved(res))
1031 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
1032 opt->option->id, name, parser->name);
1033 parser->resolve_resource(handler, res);
1035 return res->resolved;
1038 static bool boot_option_resolve(struct discover_boot_option *opt,
1039 struct device_handler *handler)
1041 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
1042 resource_resolve(opt->initrd, "initrd", opt, handler) &&
1043 resource_resolve(opt->dtb, "dtb", opt, handler) &&
1044 resource_resolve(opt->args_sig_file, "args_sig_file", opt,
1046 resource_resolve(opt->icon, "icon", opt, handler);
1049 static void boot_option_finalise(struct device_handler *handler,
1050 struct discover_boot_option *opt)
1052 assert(boot_option_is_resolved(opt));
1054 /* check that the parsers haven't set any of the final data */
1055 assert(!opt->option->boot_image_file);
1056 assert(!opt->option->initrd_file);
1057 assert(!opt->option->dtb_file);
1058 assert(!opt->option->icon_file);
1059 assert(!opt->option->device_id);
1060 assert(!opt->option->args_sig_file);
1062 if (opt->boot_image)
1063 opt->option->boot_image_file = opt->boot_image->url->full;
1065 opt->option->initrd_file = opt->initrd->url->full;
1067 opt->option->dtb_file = opt->dtb->url->full;
1069 opt->option->icon_file = opt->icon->url->full;
1070 if (opt->args_sig_file)
1071 opt->option->args_sig_file = opt->args_sig_file->url->full;
1073 opt->option->device_id = opt->device->device->id;
1075 if (opt->option->is_default)
1076 set_default(handler, opt);
1079 static void notify_boot_option(struct device_handler *handler,
1080 struct discover_boot_option *opt)
1082 struct discover_device *dev = opt->device;
1085 discover_server_notify_device_add(handler->server,
1086 opt->device->device);
1087 dev->notified = true;
1088 discover_server_notify_boot_option_add(handler->server, opt->option);
1091 static void process_boot_option_queue(struct device_handler *handler)
1093 struct discover_boot_option *opt, *tmp;
1095 list_for_each_entry_safe(&handler->unresolved_boot_options,
1098 pb_debug("queue: attempting resolution for %s\n",
1101 if (!boot_option_resolve(opt, handler))
1104 pb_debug("\tresolved!\n");
1106 list_remove(&opt->list);
1107 list_add_tail(&opt->device->boot_options, &opt->list);
1108 talloc_steal(opt->device, opt);
1109 boot_option_finalise(handler, opt);
1110 notify_boot_option(handler, opt);
1114 struct discover_context *device_handler_discover_context_create(
1115 struct device_handler *handler,
1116 struct discover_device *device)
1118 struct discover_context *ctx;
1120 ctx = talloc_zero(handler, struct discover_context);
1121 ctx->handler = handler;
1122 ctx->device = device;
1123 list_init(&ctx->boot_options);
1128 void device_handler_add_device(struct device_handler *handler,
1129 struct discover_device *device)
1131 handler->n_devices++;
1132 handler->devices = talloc_realloc(handler, handler->devices,
1133 struct discover_device *, handler->n_devices);
1134 handler->devices[handler->n_devices - 1] = device;
1136 if (device->device->type == DEVICE_TYPE_NETWORK)
1137 network_register_device(handler->network, device);
1140 void device_handler_add_ramdisk(struct device_handler *handler,
1143 struct ramdisk_device *dev;
1149 for (i = 0; i < handler->n_ramdisks; i++)
1150 if (!strcmp(handler->ramdisks[i]->path, path))
1153 dev = talloc_zero(handler, struct ramdisk_device);
1155 pb_log("Failed to allocate memory to track %s\n", path);
1159 dev->path = talloc_strdup(handler, path);
1161 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
1162 struct ramdisk_device *,
1163 handler->n_ramdisks + 1);
1164 if (!handler->ramdisks) {
1165 pb_log("Failed to reallocate memory"
1166 "- ramdisk tracking inconsistent!\n");
1170 handler->ramdisks[i] = dev;
1171 i = handler->n_ramdisks++;
1174 struct ramdisk_device *device_handler_get_ramdisk(
1175 struct device_handler *handler)
1181 /* Check if free ramdisk exists */
1182 for (i = 0; i < handler->n_ramdisks; i++)
1183 if (!handler->ramdisks[i]->snapshot &&
1184 !handler->ramdisks[i]->origin &&
1185 !handler->ramdisks[i]->base)
1186 return handler->ramdisks[i];
1188 /* Otherwise create a new one */
1189 name = talloc_asprintf(handler, "/dev/ram%d",
1190 handler->n_ramdisks);
1192 pb_debug("Failed to allocate memory to name /dev/ram%d",
1193 handler->n_ramdisks);
1197 id = makedev(1, handler->n_ramdisks);
1198 if (mknod(name, S_IFBLK, id)) {
1199 if (errno == EEXIST) {
1200 /* We haven't yet received updates for existing
1201 * ramdisks - add and use this one */
1202 pb_debug("Using untracked ramdisk %s\n", name);
1204 pb_log("Failed to create new ramdisk %s: %s\n",
1205 name, strerror(errno));
1209 device_handler_add_ramdisk(handler, name);
1212 return handler->ramdisks[i];
1215 void device_handler_release_ramdisk(struct discover_device *device)
1217 struct ramdisk_device *ramdisk = device->ramdisk;
1219 talloc_free(ramdisk->snapshot);
1220 talloc_free(ramdisk->origin);
1221 talloc_free(ramdisk->base);
1223 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
1224 ramdisk->sectors = 0;
1226 device->ramdisk = NULL;
1229 /* Start discovery on a hotplugged device. The device will be in our devices
1230 * array, but has only just been initialised by the hotplug source.
1232 int device_handler_discover(struct device_handler *handler,
1233 struct discover_device *dev)
1235 struct discover_context *ctx;
1238 device_handler_status_dev_info(handler, dev,
1240 * TRANSLATORS: this string will be passed the type of the
1241 * device (eg "disk" or "network"), which will be translated
1244 _("Processing new %s device"),
1245 device_type_display_name(dev->device->type));
1247 /* create our context */
1248 ctx = device_handler_discover_context_create(handler, dev);
1250 rc = mount_device(dev);
1254 /* add this device to our system info */
1255 system_info_register_blockdev(dev->device->id, dev->uuid,
1258 /* run the parsers. This will populate the ctx's boot_option list. */
1259 iterate_parsers(ctx);
1261 /* add discovered stuff to the handler */
1262 device_handler_discover_context_commit(handler, ctx);
1264 process_boot_option_queue(handler);
1266 /* Check this device for pb-plugins */
1267 device_handler_plugin_scan_device(handler, dev);
1269 talloc_unlink(handler, ctx);
1274 struct requery_data {
1275 struct device_handler *handler;
1276 struct discover_device *device;
1279 static int device_handler_requery_timeout_fn(void *data)
1281 struct discover_boot_option *opt, *tmp;
1282 struct requery_data *rqd = data;
1283 struct device_handler *handler;
1284 struct discover_device *device;
1287 handler = rqd->handler;
1288 device = rqd->device;
1292 /* network_requery_device may re-add a timeout, so clear the device
1293 * waiter here, so we can potentially start a new one. */
1294 device->requery_waiter = NULL;
1296 /* We keep the device around, but get rid of the parsed boot
1297 * options on that device. That involves delaring out the lists,
1298 * and potentially cancelling a default.
1300 list_for_each_entry_safe(&handler->unresolved_boot_options,
1302 if (opt->device != device)
1304 list_remove(&opt->list);
1308 /* Track whether autoboot was enabled, if we cancel a default option
1309 * it will be switched off.
1311 autoboot = handler->autoboot_enabled;
1313 list_for_each_entry_safe(&device->boot_options, opt, tmp, list) {
1314 if (opt == handler->default_boot_option) {
1315 pb_log("Default option %s cancelled since device is being requeried\n",
1317 device_handler_cancel_default(handler);
1319 list_remove(&opt->list);
1323 handler->autoboot_enabled = autoboot;
1325 discover_server_notify_device_remove(handler->server, device->device);
1326 device->notified = false;
1328 network_requery_device(handler->network, device);
1333 /* Schedule a requery in timeout (seconds).
1335 * Special values of timeout:
1339 void device_handler_start_requery_timeout( struct device_handler *handler,
1340 struct discover_device *dev, int timeout)
1342 struct requery_data *rqd;
1344 if (dev->requery_waiter)
1348 timeout = default_rescan_timeout;
1349 else if (timeout == 0)
1352 rqd = talloc(dev, struct requery_data);
1353 rqd->handler = handler;
1356 pb_debug("starting requery timeout for device %s, in %d sec\n",
1357 dev->device->id, timeout);
1359 dev->requery_waiter = waiter_register_timeout(handler->waitset,
1360 timeout * 1000, device_handler_requery_timeout_fn, rqd);
1363 static int event_requery_timeout(struct event *event)
1373 str = event_get_param(event, "reboottime");
1377 x = strtoul(str, &endp, 0);
1385 /* Incoming dhcp event */
1386 int device_handler_dhcp(struct device_handler *handler,
1387 struct discover_device *dev, struct event *event)
1389 struct discover_context *ctx;
1392 if (event_get_param(event, "ipv6"))
1393 ip = event_get_param(event, "ipv6");
1395 ip = event_get_param(event, "ip");
1397 device_handler_status_dev_info(handler, dev,
1398 _("Processing DHCP lease response (ip: %s)"), ip);
1400 pending_network_jobs_start();
1402 /* create our context */
1403 ctx = device_handler_discover_context_create(handler, dev);
1404 talloc_steal(ctx, event);
1407 device_handler_start_requery_timeout(handler, dev,
1408 event_requery_timeout(event));
1410 iterate_parsers(ctx);
1412 device_handler_discover_context_commit(handler, ctx);
1414 talloc_unlink(handler, ctx);
1419 struct discover_boot_option *device_handler_find_option_by_name(
1420 struct device_handler *handler, const char *device,
1423 size_t len = strlen(name);
1426 for (i = 0; i < handler->n_devices; i++) {
1427 struct discover_device *dev = handler->devices[i];
1428 struct discover_boot_option *opt;
1430 list_for_each_entry(&dev->boot_options, opt, list)
1431 /* Match exactly, partial matches can be quite common */
1432 if (strlen(opt->option->name) == len &&
1433 !strcmp(opt->option->name, name))
1434 if (!dev || !strcmp(opt->option->device_id, device))
1441 static struct discover_boot_option *find_boot_option_by_id(
1442 struct device_handler *handler, const char *id)
1446 for (i = 0; i < handler->n_devices; i++) {
1447 struct discover_device *dev = handler->devices[i];
1448 struct discover_boot_option *opt;
1450 list_for_each_entry(&dev->boot_options, opt, list)
1451 if (!strcmp(opt->option->id, id))
1458 void device_handler_boot(struct device_handler *handler,
1459 bool change_default, struct boot_command *cmd)
1461 struct discover_boot_option *opt = NULL;
1463 if (cmd->option_id && strlen(cmd->option_id))
1464 opt = find_boot_option_by_id(handler, cmd->option_id);
1466 /* Don't allow a normal client to change the default */
1467 if (!change_default && handler->last_boot_option &&
1468 opt != handler->last_boot_option) {
1469 pb_log("Non-root user tried to change boot option\n");
1470 device_handler_status_err(handler,
1471 "Must be root to change default boot option\n");
1475 if (handler->pending_boot)
1476 boot_cancel(handler->pending_boot);
1478 platform_pre_boot();
1480 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
1481 device_handler_boot_status_cb, handler);
1482 handler->pending_boot_is_default = false;
1485 void device_handler_cancel_default(struct device_handler *handler)
1487 if (handler->timeout_waiter)
1488 waiter_remove(handler->timeout_waiter);
1490 handler->timeout_waiter = NULL;
1491 handler->autoboot_enabled = false;
1493 /* we only send status if we had a default boot option queued */
1494 if (!handler->default_boot_option)
1497 pb_log("Cancelling default boot option\n");
1499 if (handler->pending_boot && handler->pending_boot_is_default) {
1500 boot_cancel(handler->pending_boot);
1501 handler->pending_boot = NULL;
1502 handler->pending_boot_is_default = false;
1505 handler->default_boot_option = NULL;
1507 device_handler_status_info(handler, _("Default boot cancelled"));
1510 void device_handler_update_config(struct device_handler *handler,
1511 struct config *config)
1515 rc = config_set(config);
1517 device_handler_status_err(handler,
1518 "Failed to update configuration!");
1522 discover_server_notify_config(handler->server, config);
1523 device_handler_update_lang(config->lang);
1524 device_handler_reinit(handler);
1527 static char *device_from_addr(void *ctx, struct pb_url *url)
1529 char *ipaddr, *buf, *tok, *dev = NULL;
1531 const char *delim = " ";
1532 struct sockaddr_in *ipv4;
1533 struct sockaddr_in6 *ipv6;
1534 struct addrinfo *res;
1538 /* Confirm url->host is either a valid hostname, or a
1539 * valid IPv4 or IPv6 address */
1540 rc = getaddrinfo(url->host, NULL, NULL, &res);
1542 pb_debug("%s: Invalid URL\n",__func__);
1546 switch (res->ai_family) {
1547 case AF_INET: /* ipv4 */
1548 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1549 ipv4 = (struct sockaddr_in *) res->ai_addr;
1550 inet_ntop(AF_INET, &(ipv4->sin_addr), ipaddr, INET_ADDRSTRLEN);
1553 case AF_INET6: /* ipv6 */
1554 ipaddr = talloc_array(ctx,char,INET6_ADDRSTRLEN);
1555 ipv6 = (struct sockaddr_in6 *) res->ai_addr;
1556 inet_ntop(AF_INET6, &(ipv6->sin6_addr), ipaddr, INET6_ADDRSTRLEN);
1559 default: /* error */
1565 const char *argv[] = {
1567 ipv6_route ? "-6" : "-4",
1568 "route", "show", "to", "match",
1573 p = process_create(ctx);
1575 p->path = pb_system_apps.ip;
1577 p->keep_stdout = true;
1579 rc = process_run_sync(p);
1581 if (rc || p->exit_status) {
1582 /* ip has complained for some reason; most likely
1583 * there is no route to the host - bail out */
1584 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1585 pb_debug("ip buf: %s\n", p->stdout_buf);
1590 buf = p->stdout_buf;
1591 /* If a route is found, ip-route output will be of the form
1592 * "... dev DEVNAME ... " */
1593 tok = strtok(buf, delim);
1595 if (!strcmp(tok, "dev")) {
1596 tok = strtok(NULL, delim);
1597 dev = talloc_strdup(ctx, tok);
1600 tok = strtok(NULL, delim);
1605 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1609 static void process_url_cb(struct load_url_result *result, void *data)
1611 struct device_handler *handler;
1612 struct discover_context *ctx;
1613 struct discover_device *dev;
1614 struct event *event = data;
1617 if (result->status != LOAD_OK) {
1618 pb_log_fn("Load failed for %s\n", result->url->full);
1625 handler = talloc_parent(event);
1629 event->device = device_from_addr(event, result->url);
1630 if (!event->device) {
1631 pb_log("Downloaded a file but can't find its interface - pretending it was local\n");
1632 event->device = talloc_asprintf(event, "local");
1635 mac = event_get_param(event, "mac");
1636 char *url = talloc_asprintf(event, "file://%s", result->local);
1637 event_set_param(event, "pxeconffile-local", url);
1639 dev = discover_device_create(handler, mac, event->device);
1640 ctx = device_handler_discover_context_create(handler, dev);
1641 talloc_steal(ctx, event);
1644 iterate_parsers(ctx);
1646 device_handler_discover_context_commit(handler, ctx);
1648 talloc_unlink(handler, ctx);
1651 void device_handler_process_url(struct device_handler *handler,
1652 const char *url, const char *mac, const char *ip)
1654 struct discover_context *ctx;
1655 struct discover_device *dev;
1656 bool allow_async = false;
1657 struct pb_url *pb_url;
1658 struct event *event;
1660 event = talloc_zero(handler, struct event);
1661 event->type = EVENT_TYPE_USER;
1662 event->action = EVENT_ACTION_URL;
1664 pb_url = pb_url_parse(event, url);
1665 if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1666 device_handler_status_err(handler, _("Invalid config URL!"));
1671 if (url[strlen(url) - 1] == '/') {
1672 event_set_param(event, "pxepathprefix", url);
1673 event_set_param(event, "mac", mac);
1674 event_set_param(event, "ip", ip);
1675 event->device = device_from_addr(event, pb_url);
1676 if (!event->device) {
1677 device_handler_status_err(handler,
1678 _("Unable to route to host %s"),
1684 event_set_param(event, "pxeconffile", url);
1688 if (pb_url->scheme == pb_url_file)
1689 event->device = talloc_asprintf(event, "local");
1690 else if (allow_async) {
1691 /* If file is remote load asynchronously before passing to
1692 * parser. This allows us to wait for network to be available */
1693 if (!load_url_async(handler, pb_url, process_url_cb, event,
1695 pb_log("Failed to load url %s\n", pb_url->full);
1696 device_handler_status_err(handler, _("Failed to load URL!"));
1702 /* If path is local we can parse straight away */
1704 dev = discover_device_create(handler, mac, event->device);
1705 if (pb_url->scheme == pb_url_file)
1706 dev->device->type = DEVICE_TYPE_ANY;
1707 ctx = device_handler_discover_context_create(handler, dev);
1708 talloc_steal(ctx, event);
1711 iterate_parsers(ctx);
1713 device_handler_discover_context_commit(handler, ctx);
1715 talloc_unlink(handler, ctx);
1718 static void plugin_install_cb(struct process *process)
1720 struct device_handler *handler = process->data;
1723 pb_log_fn("Missing data!\n");
1727 handler->plugin_installing = false;
1728 if (process->exit_status) {
1729 device_handler_status_err(handler, "Plugin failed to install!");
1730 pb_log("Failed to install plugin:\n%s\n", process->stdout_buf);
1734 void device_handler_install_plugin(struct device_handler *handler,
1735 const char *plugin_file)
1740 if (handler->plugin_installing) {
1741 pb_log("Plugin install cancelled - install already running");
1745 p = process_create(handler);
1747 pb_log("install_plugin: Failed to create process\n");
1751 const char *argv[] = {
1752 pb_system_apps.pb_plugin,
1759 p->path = pb_system_apps.pb_plugin;
1761 p->exit_cb = plugin_install_cb;
1763 p->keep_stdout = true;
1765 result = process_run_async(p);
1768 device_handler_status_err(handler, "Could not install plugin");
1770 handler->plugin_installing = true;
1773 #ifndef PETITBOOT_TEST
1776 * context_commit - Commit a temporary discovery context to the handler,
1777 * and notify the clients about any new options / devices
1779 void device_handler_discover_context_commit(struct device_handler *handler,
1780 struct discover_context *ctx)
1782 struct discover_device *dev = ctx->device;
1783 struct discover_boot_option *opt, *tmp;
1785 if (!device_lookup_by_uuid(handler, dev->uuid))
1786 device_handler_add_device(handler, dev);
1788 /* move boot options from the context to the device */
1789 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
1790 list_remove(&opt->list);
1792 /* All boot options need at least a kernel image */
1793 if (!opt->boot_image || !opt->boot_image->url) {
1794 pb_log("boot option %s is missing boot image, ignoring\n",
1800 if (boot_option_resolve(opt, handler)) {
1801 pb_log("boot option %s is resolved, "
1802 "sending to clients\n",
1804 list_add_tail(&dev->boot_options, &opt->list);
1805 talloc_steal(dev, opt);
1806 boot_option_finalise(handler, opt);
1807 notify_boot_option(handler, opt);
1809 if (!opt->source->resolve_resource) {
1810 pb_log("parser %s gave us an unresolved "
1811 "resource (%s), but no way to "
1813 opt->source->name, opt->option->id);
1816 pb_log("boot option %s is unresolved, "
1817 "adding to queue\n",
1819 list_add(&handler->unresolved_boot_options,
1821 talloc_steal(handler, opt);
1827 void device_handler_add_plugin_option(struct device_handler *handler,
1828 struct plugin_option *opt)
1830 struct plugin_option *tmp;
1833 for (i = 0; i < handler->n_plugins; i++) {
1834 tmp = handler->plugins[i];
1835 /* If both id and version match, ignore */
1836 if (strncmp(opt->id, tmp->id, strlen(opt->id)) == 0 &&
1837 strcmp(opt->version, tmp->version) == 0) {
1838 pb_log("discover: Plugin '%s' already exists, ignoring\n",
1844 handler->plugins = talloc_realloc(handler, handler->plugins,
1845 struct plugin_option *, handler->n_plugins + 1);
1846 if (!handler->plugins) {
1847 pb_log("Failed to allocate memory for new plugin\n");
1848 handler->n_plugins = 0;
1852 handler->plugins[handler->n_plugins++] = opt;
1853 discover_server_notify_plugin_option_add(handler->server, opt);
1856 static void device_handler_update_lang(const char *lang)
1858 const char *cur_lang;
1863 cur_lang = setlocale(LC_ALL, NULL);
1864 if (cur_lang && !strcmp(cur_lang, lang))
1867 setlocale(LC_ALL, lang);
1870 static int device_handler_init_sources(struct device_handler *handler)
1872 /* init our device sources: udev, network and user events */
1873 handler->user_event = user_event_init(handler, handler->waitset);
1874 if (!handler->user_event)
1877 handler->network = network_init(handler, handler->waitset,
1879 if (!handler->network)
1882 handler->udev = udev_init(handler, handler->waitset);
1889 static void device_handler_reinit_sources(struct device_handler *handler)
1891 /* if we haven't initialised sources previously (becuase we started in
1892 * safe mode), then init once here. */
1893 if (!(handler->udev || handler->network || handler->user_event)) {
1894 device_handler_init_sources(handler);
1898 system_info_reinit();
1900 network_shutdown(handler->network);
1901 handler->network = network_init(handler, handler->waitset,
1904 udev_reinit(handler->udev);
1907 static inline const char *get_device_path(struct discover_device *dev)
1909 return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1912 static char *check_subvols(struct discover_device *dev)
1914 const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1919 if (strncmp(fstype, "btrfs", strlen("btrfs")))
1920 return dev->mount_path;
1922 /* On btrfs a device's root may be under a subvolume path */
1923 path = join_paths(dev, dev->mount_path, "@");
1924 rc = stat(path, &sb);
1925 if (!rc && S_ISDIR(sb.st_mode)) {
1926 pb_debug("Using '%s' for btrfs root path\n", path);
1931 return dev->mount_path;
1934 static bool check_existing_mount(struct discover_device *dev)
1936 struct stat devstat, mntstat;
1937 const char *device_path;
1942 device_path = get_device_path(dev);
1944 rc = stat(device_path, &devstat);
1946 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1950 if (!S_ISBLK(devstat.st_mode)) {
1951 pb_debug("%s: %s isn't a block device?\n", __func__,
1956 fp = fopen("/proc/self/mounts", "r");
1959 mnt = getmntent(fp);
1963 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1966 rc = stat(mnt->mnt_fsname, &mntstat);
1970 if (!S_ISBLK(mntstat.st_mode))
1973 if (mntstat.st_rdev == devstat.st_rdev) {
1974 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1975 dev->root_path = check_subvols(dev);
1976 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1977 dev->mounted = true;
1978 dev->unmount = false;
1980 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1981 __func__, dev->device_path,
1982 dev->mounted_rw ? 'w' : 'o',
1994 * Attempt to mount a filesystem safely, while handling certain filesytem-
1997 static int try_mount(const char *device_path, const char *mount_path,
1998 const char *fstype, unsigned long flags,
2001 const char *fs, *safe_opts;
2004 /* Mount ext3 as ext4 instead so 'norecovery' can be used */
2005 if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
2006 pb_debug("Mounting ext3 filesystem as ext4\n");
2011 if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
2012 strncmp(fs, "ext4", strlen("ext4")) == 0)
2013 safe_opts = "norecovery";
2018 /* If no snapshot is available don't attempt recovery */
2020 return mount(device_path, mount_path, fs, flags, safe_opts);
2022 rc = mount(device_path, mount_path, fs, flags, NULL);
2027 /* Mounting failed; some filesystems will fail to mount if a recovery
2028 * journal exists (eg. cross-endian XFS), so try again with norecovery
2029 * where that option is available.
2030 * If mounting read-write just return the error as norecovery is not a
2032 if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
2036 return mount(device_path, mount_path, fs, flags, safe_opts);
2039 static int mount_device(struct discover_device *dev)
2041 const char *fstype, *device_path;
2044 if (!dev->device_path)
2050 if (check_existing_mount(dev))
2053 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
2057 dev->mount_path = join_paths(dev, mount_base(),
2060 if (pb_mkdir_recursive(dev->mount_path)) {
2061 pb_log("couldn't create mount directory %s: %s\n",
2062 dev->mount_path, strerror(errno));
2066 device_path = get_device_path(dev);
2068 pb_log("mounting device %s read-only\n", dev->device_path);
2069 rc = try_mount(device_path, dev->mount_path, fstype,
2070 MS_RDONLY | MS_SILENT, dev->ramdisk);
2072 /* If mount fails clean up any snapshot and try again */
2073 if (rc && dev->ramdisk) {
2074 pb_log("couldn't mount snapshot for %s: mount failed: %s\n",
2075 device_path, strerror(errno));
2076 pb_log("falling back to actual device\n");
2078 devmapper_destroy_snapshot(dev);
2080 device_path = get_device_path(dev);
2081 pb_log("mounting device %s read-only\n", dev->device_path);
2082 rc = try_mount(device_path, dev->mount_path, fstype,
2083 MS_RDONLY | MS_SILENT, dev->ramdisk);
2087 dev->mounted = true;
2088 dev->mounted_rw = false;
2089 dev->unmount = true;
2090 dev->root_path = check_subvols(dev);
2094 pb_log("couldn't mount device %s: mount failed: %s\n",
2095 device_path, strerror(errno));
2097 pb_rmdir_recursive(mount_base(), dev->mount_path);
2099 talloc_free(dev->mount_path);
2100 dev->mount_path = NULL;
2104 static int umount_device(struct discover_device *dev)
2106 const char *device_path;
2109 if (!dev->mounted || !dev->unmount)
2112 device_path = get_device_path(dev);
2114 pb_log("unmounting device %s\n", device_path);
2115 rc = umount(dev->mount_path);
2119 dev->mounted = false;
2120 devmapper_destroy_snapshot(dev);
2122 pb_rmdir_recursive(mount_base(), dev->mount_path);
2124 talloc_free(dev->mount_path);
2125 dev->mount_path = NULL;
2126 dev->root_path = NULL;
2131 int device_request_write(struct discover_device *dev, bool *release)
2133 const char *fstype, *device_path;
2134 const struct config *config;
2139 config = config_get();
2140 if (!config->allow_writes)
2146 if (dev->mounted_rw)
2149 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
2151 device_path = get_device_path(dev);
2153 pb_log("remounting device %s read-write\n", device_path);
2155 rc = umount(dev->mount_path);
2157 pb_log("Failed to unmount %s: %s\n",
2158 dev->mount_path, strerror(errno));
2162 rc = try_mount(device_path, dev->mount_path, fstype,
2163 MS_SILENT, dev->ramdisk);
2167 dev->mounted_rw = true;
2172 pb_log("Unable to remount device %s read-write: %s\n",
2173 device_path, strerror(errno));
2174 rc = try_mount(device_path, dev->mount_path, fstype,
2175 MS_RDONLY | MS_SILENT, dev->ramdisk);
2177 pb_log("Unable to recover mount for %s: %s\n",
2178 device_path, strerror(errno));
2182 void device_release_write(struct discover_device *dev, bool release)
2184 const char *fstype, *device_path;
2189 device_path = get_device_path(dev);
2191 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
2193 pb_log("remounting device %s read-only\n", device_path);
2195 if (umount(dev->mount_path)) {
2196 pb_log("Failed to unmount %s\n", dev->mount_path);
2199 dev->mounted_rw = dev->mounted = false;
2202 devmapper_merge_snapshot(dev);
2203 /* device_path becomes stale after merge */
2204 device_path = get_device_path(dev);
2207 if (try_mount(device_path, dev->mount_path, fstype,
2208 MS_RDONLY | MS_SILENT, dev->ramdisk))
2209 pb_log("Failed to remount %s read-only: %s\n",
2210 device_path, strerror(errno));
2212 dev->mounted = true;
2215 void device_sync_snapshots(struct device_handler *handler, const char *device)
2217 struct discover_device *dev = NULL;
2221 /* Find matching device and sync */
2222 dev = device_lookup_by_name(handler, device);
2224 pb_log("%s: device name '%s' unrecognised\n",
2229 device_release_write(dev, true);
2231 pb_log("%s has no snapshot to merge, skipping\n",
2236 /* Otherwise sync all relevant devices */
2237 for (i = 0; i < handler->n_devices; i++) {
2238 dev = handler->devices[i];
2239 if (dev->device->type != DEVICE_TYPE_DISK &&
2240 dev->device->type != DEVICE_TYPE_USB)
2243 device_release_write(dev, true);
2245 pb_log("%s has no snapshot to merge, skipping\n",
2252 void device_handler_discover_context_commit(
2253 struct device_handler *handler __attribute__((unused)),
2254 struct discover_context *ctx __attribute__((unused)))
2256 pb_log_fn("stubbed out for test cases\n");
2259 static void device_handler_update_lang(const char *lang __attribute__((unused)))
2263 static int device_handler_init_sources(
2264 struct device_handler *handler __attribute__((unused)))
2269 static void device_handler_reinit_sources(
2270 struct device_handler *handler __attribute__((unused)))
2274 static int umount_device(struct discover_device *dev __attribute__((unused)))
2279 static int __attribute__((unused)) mount_device(
2280 struct discover_device *dev __attribute__((unused)))
2285 int device_request_write(struct discover_device *dev __attribute__((unused)),
2292 void device_release_write(struct discover_device *dev __attribute__((unused)),
2293 bool release __attribute__((unused)))
2297 void device_sync_snapshots(
2298 struct device_handler *handler __attribute__((unused)),
2299 const char *device __attribute__((unused)))