11 #include <sys/mount.h>
13 #include <talloc/talloc.h>
14 #include <list/list.h>
16 #include <types/types.h>
17 #include <system/system.h>
18 #include <process/process.h>
20 #include <i18n/i18n.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <arpa/inet.h>
27 #include "device-handler.h"
28 #include "discover-server.h"
29 #include "devmapper.h"
30 #include "user-event.h"
42 enum default_priority {
43 DEFAULT_PRIORITY_REMOTE = 1,
44 DEFAULT_PRIORITY_LOCAL_FIRST = 2,
45 DEFAULT_PRIORITY_LOCAL_LAST = 0xfe,
46 DEFAULT_PRIORITY_DISABLED = 0xff,
49 struct progress_info {
50 unsigned int percentage;
51 unsigned long size; /* size in bytes */
53 const struct process_info *procinfo;
54 struct list_item list;
57 struct device_handler {
58 struct discover_server *server;
62 struct network *network;
63 struct user_event *user_event;
65 struct discover_device **devices;
66 unsigned int n_devices;
68 struct ramdisk_device **ramdisks;
69 unsigned int n_ramdisks;
71 struct waitset *waitset;
72 struct waiter *timeout_waiter;
73 bool autoboot_enabled;
74 unsigned int sec_to_boot;
76 struct discover_boot_option *default_boot_option;
77 int default_boot_option_priority;
79 struct list unresolved_boot_options;
81 struct boot_task *pending_boot;
82 bool pending_boot_is_default;
85 unsigned int n_progress;
88 static int mount_device(struct discover_device *dev);
89 static int umount_device(struct discover_device *dev);
91 static int device_handler_init_sources(struct device_handler *handler);
92 static void device_handler_reinit_sources(struct device_handler *handler);
94 static void device_handler_update_lang(const char *lang);
96 void discover_context_add_boot_option(struct discover_context *ctx,
97 struct discover_boot_option *boot_option)
99 boot_option->source = ctx->parser;
100 list_add_tail(&ctx->boot_options, &boot_option->list);
101 talloc_steal(ctx, boot_option);
105 * device_handler_get_device_count - Get the count of current handler devices.
108 int device_handler_get_device_count(const struct device_handler *handler)
110 return handler->n_devices;
114 * device_handler_get_device - Get a handler device by index.
117 const struct discover_device *device_handler_get_device(
118 const struct device_handler *handler, unsigned int index)
120 if (index >= handler->n_devices) {
121 assert(0 && "bad index");
125 return handler->devices[index];
128 struct network *device_handler_get_network(
129 const struct device_handler *handler)
131 return handler->network;
134 struct discover_boot_option *discover_boot_option_create(
135 struct discover_context *ctx,
136 struct discover_device *device)
138 struct discover_boot_option *opt;
140 opt = talloc_zero(ctx, struct discover_boot_option);
141 opt->option = talloc_zero(opt, struct boot_option);
142 opt->device = device;
147 static int device_match_uuid(struct discover_device *dev, const char *uuid)
149 return dev->uuid && !strcmp(dev->uuid, uuid);
152 static int device_match_label(struct discover_device *dev, const char *label)
154 return dev->label && !strcmp(dev->label, label);
157 static int device_match_id(struct discover_device *dev, const char *id)
159 return !strcmp(dev->device->id, id);
162 static int device_match_serial(struct discover_device *dev, const char *serial)
164 const char *val = discover_device_get_param(dev, "ID_SERIAL");
165 return val && !strcmp(val, serial);
168 static struct discover_device *device_lookup(
169 struct device_handler *device_handler,
170 int (match_fn)(struct discover_device *, const char *),
173 struct discover_device *dev;
179 for (i = 0; i < device_handler->n_devices; i++) {
180 dev = device_handler->devices[i];
182 if (match_fn(dev, str))
189 struct discover_device *device_lookup_by_name(struct device_handler *handler,
192 if (!strncmp(name, "/dev/", strlen("/dev/")))
193 name += strlen("/dev/");
195 return device_lookup_by_id(handler, name);
198 struct discover_device *device_lookup_by_uuid(
199 struct device_handler *device_handler,
202 return device_lookup(device_handler, device_match_uuid, uuid);
205 struct discover_device *device_lookup_by_label(
206 struct device_handler *device_handler,
209 return device_lookup(device_handler, device_match_label, label);
212 struct discover_device *device_lookup_by_id(
213 struct device_handler *device_handler,
216 return device_lookup(device_handler, device_match_id, id);
219 struct discover_device *device_lookup_by_serial(
220 struct device_handler *device_handler,
223 return device_lookup(device_handler, device_match_serial, serial);
226 void device_handler_destroy(struct device_handler *handler)
228 talloc_free(handler);
231 static int destroy_device(void *arg)
233 struct discover_device *dev = arg;
240 struct discover_device *discover_device_create(struct device_handler *handler,
241 const char *uuid, const char *id)
243 struct discover_device *dev;
246 dev = device_lookup_by_uuid(handler, uuid);
248 dev = device_lookup_by_id(handler, id);
253 dev = talloc_zero(handler, struct discover_device);
254 dev->device = talloc_zero(dev, struct device);
255 dev->device->id = talloc_strdup(dev->device, id);
256 dev->uuid = talloc_strdup(dev, uuid);
257 list_init(&dev->params);
258 list_init(&dev->boot_options);
260 talloc_set_destructor(dev, destroy_device);
265 struct discover_device_param {
268 struct list_item list;
271 void discover_device_set_param(struct discover_device *device,
272 const char *name, const char *value)
274 struct discover_device_param *param;
277 list_for_each_entry(&device->params, param, list) {
278 if (!strcmp(param->name, name)) {
287 param = talloc(device, struct discover_device_param);
288 param->name = talloc_strdup(param, name);
289 list_add(&device->params, ¶m->list);
292 list_remove(¶m->list);
296 talloc_free(param->value);
299 param->value = talloc_strdup(param, value);
302 const char *discover_device_get_param(struct discover_device *device,
305 struct discover_device_param *param;
307 list_for_each_entry(&device->params, param, list) {
308 if (!strcmp(param->name, name))
314 struct device_handler *device_handler_init(struct discover_server *server,
315 struct waitset *waitset, int dry_run)
317 struct device_handler *handler;
320 handler = talloc_zero(NULL, struct device_handler);
321 handler->server = server;
322 handler->waitset = waitset;
323 handler->dry_run = dry_run;
324 handler->autoboot_enabled = config_get()->autoboot_enabled;
326 list_init(&handler->unresolved_boot_options);
328 list_init(&handler->progress);
330 /* set up our mount point base */
331 pb_mkdir_recursive(mount_base());
335 if (config_get()->safe_mode)
338 rc = device_handler_init_sources(handler);
340 talloc_free(handler);
347 void device_handler_reinit(struct device_handler *handler)
349 struct discover_boot_option *opt, *tmp;
350 struct ramdisk_device *ramdisk;
353 device_handler_cancel_default(handler);
355 /* free unresolved boot options */
356 list_for_each_entry_safe(&handler->unresolved_boot_options,
359 list_init(&handler->unresolved_boot_options);
361 /* drop all devices */
362 for (i = 0; i < handler->n_devices; i++) {
363 discover_server_notify_device_remove(handler->server,
364 handler->devices[i]->device);
365 ramdisk = handler->devices[i]->ramdisk;
366 talloc_free(handler->devices[i]);
367 talloc_free(ramdisk);
370 talloc_free(handler->devices);
371 handler->devices = NULL;
372 handler->n_devices = 0;
373 talloc_free(handler->ramdisks);
374 handler->ramdisks = NULL;
375 handler->n_ramdisks = 0;
377 device_handler_reinit_sources(handler);
380 void device_handler_remove(struct device_handler *handler,
381 struct discover_device *device)
383 struct discover_boot_option *opt, *tmp;
386 list_for_each_entry_safe(&device->boot_options, opt, tmp, list) {
387 if (opt == handler->default_boot_option) {
388 pb_log("Default option %s cancelled since device removed",
390 device_handler_cancel_default(handler);
395 for (i = 0; i < handler->n_devices; i++)
396 if (handler->devices[i] == device)
399 if (i == handler->n_devices) {
404 /* Free any unresolved options, as they're currently allocated
405 * against the handler */
406 list_for_each_entry_safe(&handler->unresolved_boot_options,
408 if (opt->device != device)
410 list_remove(&opt->list);
414 /* if this is a network device, we have to unregister it from the
416 if (device->device->type == DEVICE_TYPE_NETWORK)
417 network_unregister_device(handler->network, device);
419 handler->n_devices--;
420 memmove(&handler->devices[i], &handler->devices[i + 1],
421 (handler->n_devices - i) * sizeof(handler->devices[0]));
422 handler->devices = talloc_realloc(handler, handler->devices,
423 struct discover_device *, handler->n_devices);
425 if (device->notified)
426 discover_server_notify_device_remove(handler->server,
432 void device_handler_status(struct device_handler *handler,
433 struct status *status)
435 discover_server_notify_boot_status(handler->server, status);
438 static void _device_handler_vstatus(struct device_handler *handler,
439 enum status_type type, const char *fmt, va_list ap)
441 struct status status;
444 status.message = talloc_vasprintf(handler, fmt, ap);
445 status.backlog = false;
447 device_handler_status(handler, &status);
449 talloc_free(status.message);
452 static void _device_handler_vdevstatus(struct device_handler *handler,
453 struct discover_device *device, enum status_type type,
454 const char *fmt, va_list ap)
458 msg = talloc_asprintf(handler, "[%s] %s",
459 device ? device->device->id : "unknown", fmt);
460 _device_handler_vstatus(handler, type, msg, ap);
464 void device_handler_status_dev_info(struct device_handler *handler,
465 struct discover_device *dev, const char *fmt, ...)
470 _device_handler_vdevstatus(handler, dev, STATUS_INFO, fmt, ap);
474 void device_handler_status_dev_err(struct device_handler *handler,
475 struct discover_device *dev, const char *fmt, ...)
480 _device_handler_vdevstatus(handler, dev, STATUS_ERROR, fmt, ap);
484 void device_handler_status_info(struct device_handler *handler,
485 const char *fmt, ...)
490 _device_handler_vstatus(handler, STATUS_INFO, fmt, ap);
494 void device_handler_status_err(struct device_handler *handler,
495 const char *fmt, ...)
500 _device_handler_vstatus(handler, STATUS_ERROR, fmt, ap);
504 void device_handler_status_download(struct device_handler *handler,
505 const struct process_info *procinfo,
506 unsigned int percentage, unsigned int size, char suffix)
508 struct progress_info *p, *progress = NULL;
509 uint64_t current_converted, current = 0;
510 const char *units = " kMGTP";
511 unsigned long size_bytes;
517 list_for_each_entry(&handler->progress, p, list)
518 if (p->procinfo == procinfo)
522 pb_log("Registering new progress struct\n");
523 progress = talloc_zero(handler, struct progress_info);
525 pb_log("Failed to allocate room for progress struct\n");
528 progress->procinfo = procinfo;
529 list_add(&handler->progress, &progress->list);
530 handler->n_progress++;
534 for (i = 0; i < strlen(units); i++) {
535 if (units[i] == suffix)
539 if (i >= strlen(units)) {
540 pb_log("Couldn't recognise suffix '%c'\n", suffix);
547 progress->percentage = percentage;
548 progress->size = size_bytes;
551 * Aggregate the info we have and update status. If a progress struct
552 * has zero for both percentage and size we assume progress information
553 * is unavailable and fall back to a generic progress message.
555 list_for_each_entry(&handler->progress, p, list) {
558 if (!p->percentage || !p->size) {
559 update = talloc_asprintf(handler,
560 _("%u downloads in progress..."),
561 handler->n_progress);
567 t = (100 * c) / p->percentage;
574 current_converted = current;
575 while (current_converted >= 1000) {
576 current_converted >>= 10;
579 update = talloc_asprintf(handler,
580 _("%u %s downloading: %.0f%% - %lu%cB"),
582 ngettext("item", "items", handler->n_progress),
583 (current / total) * 100, current_converted,
588 pb_log("%s: failed to allocate new status\n", __func__);
590 device_handler_status_info(handler, "%s\n", update);
595 void device_handler_status_download_remove(struct device_handler *handler,
596 struct process_info *procinfo)
598 struct progress_info *p, *tmp;
600 list_for_each_entry_safe(&handler->progress, p, tmp, list)
601 if (p->procinfo == procinfo) {
602 list_remove(&p->list);
604 handler->n_progress--;
608 static void device_handler_boot_status_cb(void *arg, struct status *status)
610 device_handler_status(arg, status);
613 static void countdown_status(struct device_handler *handler,
614 struct discover_boot_option *opt, unsigned int sec)
616 struct status status;
618 status.type = STATUS_INFO;
619 status.message = talloc_asprintf(handler,
620 _("Booting in %d sec: %s"), sec, opt->option->name);
621 status.backlog = false;
623 device_handler_status(handler, &status);
625 talloc_free(status.message);
628 static int default_timeout(void *arg)
630 struct device_handler *handler = arg;
631 struct discover_boot_option *opt;
633 if (!handler->default_boot_option)
636 if (handler->pending_boot)
639 opt = handler->default_boot_option;
641 if (handler->sec_to_boot) {
642 countdown_status(handler, opt, handler->sec_to_boot);
643 handler->sec_to_boot--;
644 handler->timeout_waiter = waiter_register_timeout(
645 handler->waitset, 1000,
646 default_timeout, handler);
650 handler->timeout_waiter = NULL;
652 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
656 handler->pending_boot = boot(handler, handler->default_boot_option,
657 NULL, handler->dry_run, device_handler_boot_status_cb,
659 handler->pending_boot_is_default = true;
664 enum ipmi_bootdev ipmi_type;
665 enum device_type device_type;
666 } device_type_map[] = {
667 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
668 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
669 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
670 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
673 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
674 enum device_type device_type)
678 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
679 if (device_type_map[i].device_type == device_type)
680 return device_type_map[i].ipmi_type == ipmi_type;
686 static int autoboot_option_priority(const struct config *config,
687 struct discover_boot_option *opt)
689 enum device_type type = opt->device->device->type;
690 const char *uuid = opt->device->uuid;
691 struct autoboot_option *auto_opt;
694 for (i = 0; i < config->n_autoboot_opts; i++) {
695 auto_opt = &config->autoboot_opts[i];
696 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
697 if (!strcmp(auto_opt->uuid, uuid))
698 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
700 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
701 if (auto_opt->type == type ||
702 auto_opt->type == DEVICE_TYPE_ANY)
703 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
710 * We have different priorities to resolve conflicts between boot options that
711 * report to be the default for their device. This function assigns a priority
714 static enum default_priority default_option_priority(
715 struct discover_boot_option *opt)
717 const struct config *config;
719 config = config_get();
721 /* We give highest priority to IPMI-configured boot options. If
722 * we have an IPMI bootdev configuration set, then we don't allow
723 * any other defaults */
724 if (config->ipmi_bootdev) {
725 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
726 opt->device->device->type);
728 return DEFAULT_PRIORITY_REMOTE;
730 pb_debug("handler: disabled default priority due to "
731 "non-matching IPMI type %x\n",
732 config->ipmi_bootdev);
733 return DEFAULT_PRIORITY_DISABLED;
736 /* Next, try to match the option against the user-defined autoboot
737 * options, either by device UUID or type. */
738 if (config->n_autoboot_opts) {
739 int boot_match = autoboot_option_priority(config, opt);
744 /* If the option didn't match any entry in the array, it is disabled */
745 pb_debug("handler: disabled default priority due to "
746 "non-matching UUID or type\n");
747 return DEFAULT_PRIORITY_DISABLED;
750 static void set_default(struct device_handler *handler,
751 struct discover_boot_option *opt)
753 enum default_priority cur_prio, new_prio;
755 if (!handler->autoboot_enabled)
758 pb_debug("handler: new default option: %s\n", opt->option->id);
760 new_prio = default_option_priority(opt);
762 /* Anything outside our range prevents a default boot */
763 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
766 pb_debug("handler: calculated priority %d\n", new_prio);
768 /* Resolve any conflicts: if we have a new default option, it only
769 * replaces the current if it has a higher priority. */
770 if (handler->default_boot_option) {
772 cur_prio = handler->default_boot_option_priority;
774 if (new_prio < cur_prio) {
775 pb_log("handler: new prio %d beats "
776 "old prio %d for %s\n",
778 handler->default_boot_option
780 handler->default_boot_option = opt;
781 handler->default_boot_option_priority = new_prio;
782 /* extend the timeout a little, so the user sees some
783 * indication of the change */
784 handler->sec_to_boot += 2;
790 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
791 handler->default_boot_option = opt;
792 handler->default_boot_option_priority = new_prio;
794 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
795 opt->option->id, handler->sec_to_boot);
797 default_timeout(handler);
800 static bool resource_is_resolved(struct resource *res)
802 return !res || res->resolved;
805 /* We only use this in an assert, which will disappear if we're compiling
806 * with NDEBUG, so we need the 'used' attribute for these builds */
807 static bool __attribute__((used)) boot_option_is_resolved(
808 struct discover_boot_option *opt)
810 return resource_is_resolved(opt->boot_image) &&
811 resource_is_resolved(opt->initrd) &&
812 resource_is_resolved(opt->dtb) &&
813 resource_is_resolved(opt->args_sig_file) &&
814 resource_is_resolved(opt->icon);
817 static bool resource_resolve(struct resource *res, const char *name,
818 struct discover_boot_option *opt,
819 struct device_handler *handler)
821 struct parser *parser = opt->source;
823 if (resource_is_resolved(res))
826 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
827 opt->option->id, name, parser->name);
828 parser->resolve_resource(handler, res);
830 return res->resolved;
833 static bool boot_option_resolve(struct discover_boot_option *opt,
834 struct device_handler *handler)
836 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
837 resource_resolve(opt->initrd, "initrd", opt, handler) &&
838 resource_resolve(opt->dtb, "dtb", opt, handler) &&
839 resource_resolve(opt->args_sig_file, "args_sig_file", opt,
841 resource_resolve(opt->icon, "icon", opt, handler);
844 static void boot_option_finalise(struct device_handler *handler,
845 struct discover_boot_option *opt)
847 assert(boot_option_is_resolved(opt));
849 /* check that the parsers haven't set any of the final data */
850 assert(!opt->option->boot_image_file);
851 assert(!opt->option->initrd_file);
852 assert(!opt->option->dtb_file);
853 assert(!opt->option->icon_file);
854 assert(!opt->option->device_id);
855 assert(!opt->option->args_sig_file);
858 opt->option->boot_image_file = opt->boot_image->url->full;
860 opt->option->initrd_file = opt->initrd->url->full;
862 opt->option->dtb_file = opt->dtb->url->full;
864 opt->option->icon_file = opt->icon->url->full;
865 if (opt->args_sig_file)
866 opt->option->args_sig_file = opt->args_sig_file->url->full;
868 opt->option->device_id = opt->device->device->id;
870 if (opt->option->is_default)
871 set_default(handler, opt);
874 static void notify_boot_option(struct device_handler *handler,
875 struct discover_boot_option *opt)
877 struct discover_device *dev = opt->device;
880 discover_server_notify_device_add(handler->server,
881 opt->device->device);
882 dev->notified = true;
883 discover_server_notify_boot_option_add(handler->server, opt->option);
886 static void process_boot_option_queue(struct device_handler *handler)
888 struct discover_boot_option *opt, *tmp;
890 list_for_each_entry_safe(&handler->unresolved_boot_options,
893 pb_debug("queue: attempting resolution for %s\n",
896 if (!boot_option_resolve(opt, handler))
899 pb_debug("\tresolved!\n");
901 list_remove(&opt->list);
902 list_add_tail(&opt->device->boot_options, &opt->list);
903 talloc_steal(opt->device, opt);
904 boot_option_finalise(handler, opt);
905 notify_boot_option(handler, opt);
909 struct discover_context *device_handler_discover_context_create(
910 struct device_handler *handler,
911 struct discover_device *device)
913 struct discover_context *ctx;
915 ctx = talloc_zero(handler, struct discover_context);
916 ctx->handler = handler;
917 ctx->device = device;
918 list_init(&ctx->boot_options);
923 void device_handler_add_device(struct device_handler *handler,
924 struct discover_device *device)
926 handler->n_devices++;
927 handler->devices = talloc_realloc(handler, handler->devices,
928 struct discover_device *, handler->n_devices);
929 handler->devices[handler->n_devices - 1] = device;
931 if (device->device->type == DEVICE_TYPE_NETWORK)
932 network_register_device(handler->network, device);
935 void device_handler_add_ramdisk(struct device_handler *handler,
938 struct ramdisk_device *dev;
944 for (i = 0; i < handler->n_ramdisks; i++)
945 if (!strcmp(handler->ramdisks[i]->path, path))
948 dev = talloc_zero(handler, struct ramdisk_device);
950 pb_log("Failed to allocate memory to track %s\n", path);
954 dev->path = talloc_strdup(handler, path);
956 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
957 struct ramdisk_device *,
958 handler->n_ramdisks + 1);
959 if (!handler->ramdisks) {
960 pb_log("Failed to reallocate memory"
961 "- ramdisk tracking inconsistent!\n");
965 handler->ramdisks[i] = dev;
966 i = handler->n_ramdisks++;
969 struct ramdisk_device *device_handler_get_ramdisk(
970 struct device_handler *handler)
976 /* Check if free ramdisk exists */
977 for (i = 0; i < handler->n_ramdisks; i++)
978 if (!handler->ramdisks[i]->snapshot &&
979 !handler->ramdisks[i]->origin &&
980 !handler->ramdisks[i]->base)
981 return handler->ramdisks[i];
983 /* Otherwise create a new one */
984 name = talloc_asprintf(handler, "/dev/ram%d",
985 handler->n_ramdisks);
987 pb_debug("Failed to allocate memory to name /dev/ram%d",
988 handler->n_ramdisks);
992 id = makedev(1, handler->n_ramdisks);
993 if (mknod(name, S_IFBLK, id)) {
994 if (errno == EEXIST) {
995 /* We haven't yet received updates for existing
996 * ramdisks - add and use this one */
997 pb_debug("Using untracked ramdisk %s\n", name);
999 pb_log("Failed to create new ramdisk %s: %s\n",
1000 name, strerror(errno));
1004 device_handler_add_ramdisk(handler, name);
1007 return handler->ramdisks[i];
1010 void device_handler_release_ramdisk(struct discover_device *device)
1012 struct ramdisk_device *ramdisk = device->ramdisk;
1014 talloc_free(ramdisk->snapshot);
1015 talloc_free(ramdisk->origin);
1016 talloc_free(ramdisk->base);
1018 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
1019 ramdisk->sectors = 0;
1021 device->ramdisk = NULL;
1024 /* Start discovery on a hotplugged device. The device will be in our devices
1025 * array, but has only just been initialised by the hotplug source.
1027 int device_handler_discover(struct device_handler *handler,
1028 struct discover_device *dev)
1030 struct discover_context *ctx;
1033 device_handler_status_dev_info(handler, dev,
1035 * TRANSLATORS: this string will be passed the type of the
1036 * device (eg "disk" or "network"), which will be translated
1039 _("Processing new %s device"),
1040 device_type_display_name(dev->device->type));
1042 process_boot_option_queue(handler);
1044 /* create our context */
1045 ctx = device_handler_discover_context_create(handler, dev);
1047 rc = mount_device(dev);
1051 /* add this device to our system info */
1052 system_info_register_blockdev(dev->device->id, dev->uuid,
1055 /* run the parsers. This will populate the ctx's boot_option list. */
1056 iterate_parsers(ctx);
1058 /* add discovered stuff to the handler */
1059 device_handler_discover_context_commit(handler, ctx);
1062 talloc_unlink(handler, ctx);
1067 /* Incoming dhcp event */
1068 int device_handler_dhcp(struct device_handler *handler,
1069 struct discover_device *dev, struct event *event)
1071 struct discover_context *ctx;
1073 device_handler_status_dev_info(handler, dev,
1074 _("Processing DHCP lease response (ip: %s)"),
1075 event_get_param(event, "ip"));
1077 /* create our context */
1078 ctx = device_handler_discover_context_create(handler, dev);
1079 talloc_steal(ctx, event);
1082 iterate_parsers(ctx);
1084 device_handler_discover_context_commit(handler, ctx);
1086 talloc_unlink(handler, ctx);
1091 static struct discover_boot_option *find_boot_option_by_id(
1092 struct device_handler *handler, const char *id)
1096 for (i = 0; i < handler->n_devices; i++) {
1097 struct discover_device *dev = handler->devices[i];
1098 struct discover_boot_option *opt;
1100 list_for_each_entry(&dev->boot_options, opt, list)
1101 if (!strcmp(opt->option->id, id))
1108 void device_handler_boot(struct device_handler *handler,
1109 struct boot_command *cmd)
1111 struct discover_boot_option *opt = NULL;
1113 if (cmd->option_id && strlen(cmd->option_id))
1114 opt = find_boot_option_by_id(handler, cmd->option_id);
1116 if (handler->pending_boot)
1117 boot_cancel(handler->pending_boot);
1119 platform_pre_boot();
1121 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
1122 device_handler_boot_status_cb, handler);
1123 handler->pending_boot_is_default = false;
1126 void device_handler_cancel_default(struct device_handler *handler)
1128 if (handler->timeout_waiter)
1129 waiter_remove(handler->timeout_waiter);
1131 handler->timeout_waiter = NULL;
1132 handler->autoboot_enabled = false;
1134 /* we only send status if we had a default boot option queued */
1135 if (!handler->default_boot_option)
1138 pb_log("Cancelling default boot option\n");
1140 if (handler->pending_boot && handler->pending_boot_is_default) {
1141 boot_cancel(handler->pending_boot);
1142 handler->pending_boot = NULL;
1143 handler->pending_boot_is_default = false;
1146 handler->default_boot_option = NULL;
1148 device_handler_status_info(handler, _("Default boot cancelled"));
1151 void device_handler_update_config(struct device_handler *handler,
1152 struct config *config)
1156 rc = config_set(config);
1160 discover_server_notify_config(handler->server, config);
1161 device_handler_update_lang(config->lang);
1162 device_handler_reinit(handler);
1165 static char *device_from_addr(void *ctx, struct pb_url *url)
1167 char *ipaddr, *buf, *tok, *dev = NULL;
1168 const char *delim = " ";
1169 struct sockaddr_in *ip;
1170 struct sockaddr_in si;
1171 struct addrinfo *res;
1175 /* Note: IPv4 only */
1176 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1180 /* need to turn hostname into a valid IP */
1181 rc = getaddrinfo(url->host, NULL, NULL, &res);
1183 pb_debug("%s: Invalid URL\n",__func__);
1186 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1187 ip = (struct sockaddr_in *) res->ai_addr;
1188 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1192 const char *argv[] = {
1194 "route", "show", "to", "match",
1199 p = process_create(ctx);
1201 p->path = pb_system_apps.ip;
1203 p->keep_stdout = true;
1205 rc = process_run_sync(p);
1207 if (rc || p->exit_status) {
1208 /* ip has complained for some reason; most likely
1209 * there is no route to the host - bail out */
1210 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1211 pb_debug("ip buf: %s\n", p->stdout_buf);
1216 buf = p->stdout_buf;
1217 /* If a route is found, ip-route output will be of the form
1218 * "... dev DEVNAME ... " */
1219 tok = strtok(buf, delim);
1221 if (!strcmp(tok, "dev")) {
1222 tok = strtok(NULL, delim);
1223 dev = talloc_strdup(ctx, tok);
1226 tok = strtok(NULL, delim);
1231 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1235 void device_handler_process_url(struct device_handler *handler,
1236 const char *url, const char *mac, const char *ip)
1238 struct discover_context *ctx;
1239 struct discover_device *dev;
1240 struct pb_url *pb_url;
1241 struct event *event;
1242 struct param *param;
1244 if (!handler->network) {
1245 device_handler_status_err(handler, _("No network configured"));
1249 event = talloc(handler, struct event);
1250 event->type = EVENT_TYPE_USER;
1251 event->action = EVENT_ACTION_URL;
1253 if (url[strlen(url) - 1] == '/') {
1254 event->params = talloc_array(event, struct param, 3);
1255 param = &event->params[0];
1256 param->name = talloc_strdup(event, "pxepathprefix");
1257 param->value = talloc_strdup(event, url);
1258 param = &event->params[1];
1259 param->name = talloc_strdup(event, "mac");
1260 param->value = talloc_strdup(event, mac);
1261 param = &event->params[2];
1262 param->name = talloc_strdup(event, "ip");
1263 param->value = talloc_strdup(event, ip);
1264 event->n_params = 3;
1266 event->params = talloc_array(event, struct param, 1);
1267 param = &event->params[0];
1268 param->name = talloc_strdup(event, "pxeconffile");
1269 param->value = talloc_strdup(event, url);
1270 event->n_params = 1;
1273 pb_url = pb_url_parse(event, event->params->value);
1274 if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1275 device_handler_status_err(handler, _("Invalid config URL!"));
1279 if (pb_url->scheme == pb_url_file)
1280 event->device = talloc_asprintf(event, "local");
1282 event->device = device_from_addr(event, pb_url);
1284 if (!event->device) {
1285 device_handler_status_err(handler,
1286 _("Unable to route to host %s"),
1291 dev = discover_device_create(handler, mac, event->device);
1292 if (pb_url->scheme == pb_url_file)
1293 dev->device->type = DEVICE_TYPE_ANY;
1294 ctx = device_handler_discover_context_create(handler, dev);
1295 talloc_steal(ctx, event);
1298 iterate_parsers(ctx);
1300 device_handler_discover_context_commit(handler, ctx);
1302 talloc_unlink(handler, ctx);
1305 #ifndef PETITBOOT_TEST
1308 * context_commit - Commit a temporary discovery context to the handler,
1309 * and notify the clients about any new options / devices
1311 void device_handler_discover_context_commit(struct device_handler *handler,
1312 struct discover_context *ctx)
1314 struct discover_device *dev = ctx->device;
1315 struct discover_boot_option *opt, *tmp;
1317 if (!device_lookup_by_uuid(handler, dev->uuid))
1318 device_handler_add_device(handler, dev);
1320 /* move boot options from the context to the device */
1321 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
1322 list_remove(&opt->list);
1324 /* All boot options need at least a kernel image */
1325 if (!opt->boot_image || !opt->boot_image->url) {
1326 pb_log("boot option %s is missing boot image, ignoring\n",
1332 if (boot_option_resolve(opt, handler)) {
1333 pb_log("boot option %s is resolved, "
1334 "sending to clients\n",
1336 list_add_tail(&dev->boot_options, &opt->list);
1337 talloc_steal(dev, opt);
1338 boot_option_finalise(handler, opt);
1339 notify_boot_option(handler, opt);
1341 if (!opt->source->resolve_resource) {
1342 pb_log("parser %s gave us an unresolved "
1343 "resource (%s), but no way to "
1345 opt->source->name, opt->option->id);
1348 pb_log("boot option %s is unresolved, "
1349 "adding to queue\n",
1351 list_add(&handler->unresolved_boot_options,
1353 talloc_steal(handler, opt);
1359 static void device_handler_update_lang(const char *lang)
1361 const char *cur_lang;
1366 cur_lang = setlocale(LC_ALL, NULL);
1367 if (cur_lang && !strcmp(cur_lang, lang))
1370 setlocale(LC_ALL, lang);
1373 static int device_handler_init_sources(struct device_handler *handler)
1375 /* init our device sources: udev, network and user events */
1376 handler->udev = udev_init(handler, handler->waitset);
1380 handler->network = network_init(handler, handler->waitset,
1382 if (!handler->network)
1385 handler->user_event = user_event_init(handler, handler->waitset);
1386 if (!handler->user_event)
1392 static void device_handler_reinit_sources(struct device_handler *handler)
1394 /* if we haven't initialised sources previously (becuase we started in
1395 * safe mode), then init once here. */
1396 if (!(handler->udev || handler->network || handler->user_event)) {
1397 device_handler_init_sources(handler);
1401 udev_reinit(handler->udev);
1403 network_shutdown(handler->network);
1404 handler->network = network_init(handler, handler->waitset,
1408 static inline const char *get_device_path(struct discover_device *dev)
1410 return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1413 static char *check_subvols(struct discover_device *dev)
1415 const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1420 if (strncmp(fstype, "btrfs", strlen("btrfs")))
1421 return dev->mount_path;
1423 /* On btrfs a device's root may be under a subvolume path */
1424 path = join_paths(dev, dev->mount_path, "@");
1425 rc = stat(path, &sb);
1426 if (!rc && S_ISDIR(sb.st_mode)) {
1427 pb_debug("Using '%s' for btrfs root path\n", path);
1432 return dev->mount_path;
1435 static bool check_existing_mount(struct discover_device *dev)
1437 struct stat devstat, mntstat;
1438 const char *device_path;
1443 device_path = get_device_path(dev);
1445 rc = stat(device_path, &devstat);
1447 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1451 if (!S_ISBLK(devstat.st_mode)) {
1452 pb_debug("%s: %s isn't a block device?\n", __func__,
1457 fp = fopen("/proc/self/mounts", "r");
1460 mnt = getmntent(fp);
1464 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1467 rc = stat(mnt->mnt_fsname, &mntstat);
1471 if (!S_ISBLK(mntstat.st_mode))
1474 if (mntstat.st_rdev == devstat.st_rdev) {
1475 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1476 dev->root_path = check_subvols(dev);
1477 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1478 dev->mounted = true;
1479 dev->unmount = false;
1481 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1482 __func__, dev->device_path,
1483 dev->mounted_rw ? 'w' : 'o',
1495 * Attempt to mount a filesystem safely, while handling certain filesytem-
1498 static int try_mount(const char *device_path, const char *mount_path,
1499 const char *fstype, unsigned long flags,
1502 const char *fs, *safe_opts;
1505 /* Mount ext3 as ext4 instead so 'norecovery' can be used */
1506 if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
1507 pb_debug("Mounting ext3 filesystem as ext4\n");
1512 if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
1513 strncmp(fs, "ext4", strlen("ext4")) == 0)
1514 safe_opts = "norecovery";
1519 /* If no snapshot is available don't attempt recovery */
1521 return mount(device_path, mount_path, fs, flags, safe_opts);
1523 rc = mount(device_path, mount_path, fs, flags, NULL);
1528 /* Mounting failed; some filesystems will fail to mount if a recovery
1529 * journal exists (eg. cross-endian XFS), so try again with norecovery
1530 * where that option is available.
1531 * If mounting read-write just return the error as norecovery is not a
1533 if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
1537 return mount(device_path, mount_path, fs, flags, safe_opts);
1540 static int mount_device(struct discover_device *dev)
1542 const char *fstype, *device_path;
1545 if (!dev->device_path)
1551 if (check_existing_mount(dev))
1554 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1558 dev->mount_path = join_paths(dev, mount_base(),
1561 if (pb_mkdir_recursive(dev->mount_path)) {
1562 pb_log("couldn't create mount directory %s: %s\n",
1563 dev->mount_path, strerror(errno));
1567 device_path = get_device_path(dev);
1569 pb_log("mounting device %s read-only\n", dev->device_path);
1570 rc = try_mount(device_path, dev->mount_path, fstype,
1571 MS_RDONLY | MS_SILENT, dev->ramdisk);
1574 dev->mounted = true;
1575 dev->mounted_rw = false;
1576 dev->unmount = true;
1577 dev->root_path = check_subvols(dev);
1581 pb_log("couldn't mount device %s: mount failed: %s\n",
1582 device_path, strerror(errno));
1584 /* If mount fails clean up any snapshot */
1585 devmapper_destroy_snapshot(dev);
1587 pb_rmdir_recursive(mount_base(), dev->mount_path);
1589 talloc_free(dev->mount_path);
1590 dev->mount_path = NULL;
1594 static int umount_device(struct discover_device *dev)
1596 const char *device_path;
1599 if (!dev->mounted || !dev->unmount)
1602 device_path = get_device_path(dev);
1604 pb_log("unmounting device %s\n", device_path);
1605 rc = umount(dev->mount_path);
1609 dev->mounted = false;
1610 devmapper_destroy_snapshot(dev);
1612 pb_rmdir_recursive(mount_base(), dev->mount_path);
1614 talloc_free(dev->mount_path);
1615 dev->mount_path = NULL;
1616 dev->root_path = NULL;
1621 int device_request_write(struct discover_device *dev, bool *release)
1623 const char *fstype, *device_path;
1624 const struct config *config;
1629 config = config_get();
1630 if (!config->allow_writes)
1636 if (dev->mounted_rw)
1639 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1641 device_path = get_device_path(dev);
1643 pb_log("remounting device %s read-write\n", device_path);
1645 rc = umount(dev->mount_path);
1647 pb_log("Failed to unmount %s: %s\n",
1648 dev->mount_path, strerror(errno));
1652 rc = try_mount(device_path, dev->mount_path, fstype,
1653 MS_SILENT, dev->ramdisk);
1657 dev->mounted_rw = true;
1662 pb_log("Unable to remount device %s read-write: %s\n",
1663 device_path, strerror(errno));
1664 rc = try_mount(device_path, dev->mount_path, fstype,
1665 MS_RDONLY | MS_SILENT, dev->ramdisk);
1667 pb_log("Unable to recover mount for %s: %s\n",
1668 device_path, strerror(errno));
1672 void device_release_write(struct discover_device *dev, bool release)
1674 const char *fstype, *device_path;
1679 device_path = get_device_path(dev);
1681 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1683 pb_log("remounting device %s read-only\n", device_path);
1685 if (umount(dev->mount_path)) {
1686 pb_log("Failed to unmount %s\n", dev->mount_path);
1689 dev->mounted_rw = dev->mounted = false;
1692 devmapper_merge_snapshot(dev);
1693 /* device_path becomes stale after merge */
1694 device_path = get_device_path(dev);
1697 if (try_mount(device_path, dev->mount_path, fstype,
1698 MS_RDONLY | MS_SILENT, dev->ramdisk))
1699 pb_log("Failed to remount %s read-only: %s\n",
1700 device_path, strerror(errno));
1702 dev->mounted = true;
1705 void device_sync_snapshots(struct device_handler *handler, const char *device)
1707 struct discover_device *dev = NULL;
1711 /* Find matching device and sync */
1712 dev = device_lookup_by_name(handler, device);
1714 pb_log("%s: device name '%s' unrecognised\n",
1719 device_release_write(dev, true);
1721 pb_log("%s has no snapshot to merge, skipping\n",
1726 /* Otherwise sync all relevant devices */
1727 for (i = 0; i < handler->n_devices; i++) {
1728 dev = handler->devices[i];
1729 if (dev->device->type != DEVICE_TYPE_DISK &&
1730 dev->device->type != DEVICE_TYPE_USB)
1733 device_release_write(dev, true);
1735 pb_log("%s has no snapshot to merge, skipping\n",
1742 void device_handler_discover_context_commit(
1743 struct device_handler *handler __attribute__((unused)),
1744 struct discover_context *ctx __attribute__((unused)))
1746 pb_log("%s stubbed out for test cases\n", __func__);
1749 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1753 static int device_handler_init_sources(
1754 struct device_handler *handler __attribute__((unused)))
1759 static void device_handler_reinit_sources(
1760 struct device_handler *handler __attribute__((unused)))
1764 static int umount_device(struct discover_device *dev __attribute__((unused)))
1769 static int __attribute__((unused)) mount_device(
1770 struct discover_device *dev __attribute__((unused)))
1775 int device_request_write(struct discover_device *dev __attribute__((unused)),
1782 void device_release_write(struct discover_device *dev __attribute__((unused)),
1783 bool release __attribute__((unused)))
1787 void device_sync_snapshots(
1788 struct device_handler *handler __attribute__((unused)),
1789 const char *device __attribute__((unused)))