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/sysmacros.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include <arpa/inet.h>
28 #include "device-handler.h"
29 #include "discover-server.h"
30 #include "devmapper.h"
31 #include "user-event.h"
43 enum default_priority {
44 DEFAULT_PRIORITY_REMOTE = 1,
45 DEFAULT_PRIORITY_LOCAL_FIRST = 2,
46 DEFAULT_PRIORITY_LOCAL_LAST = 0xfe,
47 DEFAULT_PRIORITY_DISABLED = 0xff,
50 struct progress_info {
51 unsigned int percentage;
52 unsigned long size; /* size in bytes */
54 const struct process_info *procinfo;
55 struct list_item list;
58 struct device_handler {
59 struct discover_server *server;
63 struct network *network;
64 struct user_event *user_event;
66 struct discover_device **devices;
67 unsigned int n_devices;
69 struct ramdisk_device **ramdisks;
70 unsigned int n_ramdisks;
72 struct waitset *waitset;
73 struct waiter *timeout_waiter;
74 bool autoboot_enabled;
75 unsigned int sec_to_boot;
77 struct discover_boot_option *default_boot_option;
78 int default_boot_option_priority;
80 struct list unresolved_boot_options;
82 struct boot_task *pending_boot;
83 bool pending_boot_is_default;
86 unsigned int n_progress;
89 static int mount_device(struct discover_device *dev);
90 static int umount_device(struct discover_device *dev);
92 static int device_handler_init_sources(struct device_handler *handler);
93 static void device_handler_reinit_sources(struct device_handler *handler);
95 static void device_handler_update_lang(const char *lang);
97 void discover_context_add_boot_option(struct discover_context *ctx,
98 struct discover_boot_option *boot_option)
100 boot_option->source = ctx->parser;
101 list_add_tail(&ctx->boot_options, &boot_option->list);
102 talloc_steal(ctx, boot_option);
106 * device_handler_get_device_count - Get the count of current handler devices.
109 int device_handler_get_device_count(const struct device_handler *handler)
111 return handler->n_devices;
115 * device_handler_get_device - Get a handler device by index.
118 const struct discover_device *device_handler_get_device(
119 const struct device_handler *handler, unsigned int index)
121 if (index >= handler->n_devices) {
122 assert(0 && "bad index");
126 return handler->devices[index];
129 struct network *device_handler_get_network(
130 const struct device_handler *handler)
132 return handler->network;
135 struct discover_boot_option *discover_boot_option_create(
136 struct discover_context *ctx,
137 struct discover_device *device)
139 struct discover_boot_option *opt;
141 opt = talloc_zero(ctx, struct discover_boot_option);
142 opt->option = talloc_zero(opt, struct boot_option);
143 opt->device = device;
148 static int device_match_uuid(struct discover_device *dev, const char *uuid)
150 return dev->uuid && !strcmp(dev->uuid, uuid);
153 static int device_match_label(struct discover_device *dev, const char *label)
155 return dev->label && !strcmp(dev->label, label);
158 static int device_match_id(struct discover_device *dev, const char *id)
160 return !strcmp(dev->device->id, id);
163 static int device_match_serial(struct discover_device *dev, const char *serial)
165 const char *val = discover_device_get_param(dev, "ID_SERIAL");
166 return val && !strcmp(val, serial);
169 static struct discover_device *device_lookup(
170 struct device_handler *device_handler,
171 int (match_fn)(struct discover_device *, const char *),
174 struct discover_device *dev;
180 for (i = 0; i < device_handler->n_devices; i++) {
181 dev = device_handler->devices[i];
183 if (match_fn(dev, str))
190 struct discover_device *device_lookup_by_name(struct device_handler *handler,
193 if (!strncmp(name, "/dev/", strlen("/dev/")))
194 name += strlen("/dev/");
196 return device_lookup_by_id(handler, name);
199 struct discover_device *device_lookup_by_uuid(
200 struct device_handler *device_handler,
203 return device_lookup(device_handler, device_match_uuid, uuid);
206 struct discover_device *device_lookup_by_label(
207 struct device_handler *device_handler,
210 return device_lookup(device_handler, device_match_label, label);
213 struct discover_device *device_lookup_by_id(
214 struct device_handler *device_handler,
217 return device_lookup(device_handler, device_match_id, id);
220 struct discover_device *device_lookup_by_serial(
221 struct device_handler *device_handler,
224 return device_lookup(device_handler, device_match_serial, serial);
227 void device_handler_destroy(struct device_handler *handler)
229 talloc_free(handler);
232 static int destroy_device(void *arg)
234 struct discover_device *dev = arg;
241 struct discover_device *discover_device_create(struct device_handler *handler,
242 const char *uuid, const char *id)
244 struct discover_device *dev;
247 dev = device_lookup_by_uuid(handler, uuid);
249 dev = device_lookup_by_id(handler, id);
254 dev = talloc_zero(handler, struct discover_device);
255 dev->device = talloc_zero(dev, struct device);
256 dev->device->id = talloc_strdup(dev->device, id);
257 dev->uuid = talloc_strdup(dev, uuid);
258 list_init(&dev->params);
259 list_init(&dev->boot_options);
261 talloc_set_destructor(dev, destroy_device);
266 struct discover_device_param {
269 struct list_item list;
272 void discover_device_set_param(struct discover_device *device,
273 const char *name, const char *value)
275 struct discover_device_param *param;
278 list_for_each_entry(&device->params, param, list) {
279 if (!strcmp(param->name, name)) {
288 param = talloc(device, struct discover_device_param);
289 param->name = talloc_strdup(param, name);
290 list_add(&device->params, ¶m->list);
293 list_remove(¶m->list);
297 talloc_free(param->value);
300 param->value = talloc_strdup(param, value);
303 const char *discover_device_get_param(struct discover_device *device,
306 struct discover_device_param *param;
308 list_for_each_entry(&device->params, param, list) {
309 if (!strcmp(param->name, name))
315 struct device_handler *device_handler_init(struct discover_server *server,
316 struct waitset *waitset, int dry_run)
318 struct device_handler *handler;
321 handler = talloc_zero(NULL, struct device_handler);
322 handler->server = server;
323 handler->waitset = waitset;
324 handler->dry_run = dry_run;
325 handler->autoboot_enabled = config_autoboot_active(config_get());
327 list_init(&handler->unresolved_boot_options);
329 list_init(&handler->progress);
331 /* set up our mount point base */
332 pb_mkdir_recursive(mount_base());
336 if (config_get()->safe_mode)
339 rc = device_handler_init_sources(handler);
341 talloc_free(handler);
348 void device_handler_reinit(struct device_handler *handler)
350 struct discover_boot_option *opt, *tmp;
351 struct ramdisk_device *ramdisk;
354 device_handler_cancel_default(handler);
355 /* Cancel any pending non-default boot */
356 if (handler->pending_boot) {
357 boot_cancel(handler->pending_boot);
358 handler->pending_boot = NULL;
359 handler->pending_boot_is_default = false;
362 /* Cancel any remaining async jobs */
363 process_stop_async_all();
365 /* free unresolved boot options */
366 list_for_each_entry_safe(&handler->unresolved_boot_options,
369 list_init(&handler->unresolved_boot_options);
371 /* drop all devices */
372 for (i = 0; i < handler->n_devices; i++) {
373 discover_server_notify_device_remove(handler->server,
374 handler->devices[i]->device);
375 ramdisk = handler->devices[i]->ramdisk;
376 talloc_free(handler->devices[i]);
377 talloc_free(ramdisk);
380 talloc_free(handler->devices);
381 handler->devices = NULL;
382 handler->n_devices = 0;
383 talloc_free(handler->ramdisks);
384 handler->ramdisks = NULL;
385 handler->n_ramdisks = 0;
387 device_handler_reinit_sources(handler);
390 void device_handler_remove(struct device_handler *handler,
391 struct discover_device *device)
393 struct discover_boot_option *opt, *tmp;
396 list_for_each_entry_safe(&device->boot_options, opt, tmp, list) {
397 if (opt == handler->default_boot_option) {
398 pb_log("Default option %s cancelled since device removed",
400 device_handler_cancel_default(handler);
405 for (i = 0; i < handler->n_devices; i++)
406 if (handler->devices[i] == device)
409 if (i == handler->n_devices) {
414 /* Free any unresolved options, as they're currently allocated
415 * against the handler */
416 list_for_each_entry_safe(&handler->unresolved_boot_options,
418 if (opt->device != device)
420 list_remove(&opt->list);
424 /* if this is a network device, we have to unregister it from the
426 if (device->device->type == DEVICE_TYPE_NETWORK)
427 network_unregister_device(handler->network, device);
429 handler->n_devices--;
430 memmove(&handler->devices[i], &handler->devices[i + 1],
431 (handler->n_devices - i) * sizeof(handler->devices[0]));
432 handler->devices = talloc_realloc(handler, handler->devices,
433 struct discover_device *, handler->n_devices);
435 if (device->notified)
436 discover_server_notify_device_remove(handler->server,
442 void device_handler_status(struct device_handler *handler,
443 struct status *status)
445 discover_server_notify_boot_status(handler->server, status);
448 static void _device_handler_vstatus(struct device_handler *handler,
449 enum status_type type, const char *fmt, va_list ap)
451 struct status status;
454 status.message = talloc_vasprintf(handler, fmt, ap);
455 status.backlog = false;
457 device_handler_status(handler, &status);
459 talloc_free(status.message);
462 static void _device_handler_vdevstatus(struct device_handler *handler,
463 struct discover_device *device, enum status_type type,
464 const char *fmt, va_list ap)
468 msg = talloc_asprintf(handler, "[%s] %s",
469 device ? device->device->id : "unknown", fmt);
470 _device_handler_vstatus(handler, type, msg, ap);
474 void device_handler_status_dev_info(struct device_handler *handler,
475 struct discover_device *dev, const char *fmt, ...)
480 _device_handler_vdevstatus(handler, dev, STATUS_INFO, fmt, ap);
484 void device_handler_status_dev_err(struct device_handler *handler,
485 struct discover_device *dev, const char *fmt, ...)
490 _device_handler_vdevstatus(handler, dev, STATUS_ERROR, fmt, ap);
494 void device_handler_status_info(struct device_handler *handler,
495 const char *fmt, ...)
500 _device_handler_vstatus(handler, STATUS_INFO, fmt, ap);
504 void device_handler_status_err(struct device_handler *handler,
505 const char *fmt, ...)
510 _device_handler_vstatus(handler, STATUS_ERROR, fmt, ap);
514 void device_handler_status_download(struct device_handler *handler,
515 const struct process_info *procinfo,
516 unsigned int percentage, unsigned int size, char suffix)
518 struct progress_info *p, *progress = NULL;
519 uint64_t current_converted, current = 0;
520 const char *units = " kMGTP";
521 unsigned long size_bytes;
527 list_for_each_entry(&handler->progress, p, list)
528 if (p->procinfo == procinfo)
532 pb_log("Registering new progress struct\n");
533 progress = talloc_zero(handler, struct progress_info);
535 pb_log("Failed to allocate room for progress struct\n");
538 progress->procinfo = procinfo;
539 list_add(&handler->progress, &progress->list);
540 handler->n_progress++;
544 for (i = 0; i < strlen(units); i++) {
545 if (units[i] == suffix)
549 if (i >= strlen(units)) {
550 pb_log("Couldn't recognise suffix '%c'\n", suffix);
557 progress->percentage = percentage;
558 progress->size = size_bytes;
561 * Aggregate the info we have and update status. If a progress struct
562 * has zero for both percentage and size we assume progress information
563 * is unavailable and fall back to a generic progress message.
565 list_for_each_entry(&handler->progress, p, list) {
568 if (!p->percentage || !p->size) {
569 update = talloc_asprintf(handler,
570 _("%u downloads in progress..."),
571 handler->n_progress);
577 t = (100 * c) / p->percentage;
584 current_converted = current;
585 while (current_converted >= 1000) {
586 current_converted >>= 10;
589 update = talloc_asprintf(handler,
590 _("%u %s downloading: %.0f%% - %lu%cB"),
592 ngettext("item", "items", handler->n_progress),
593 (current / total) * 100, current_converted,
598 pb_log("%s: failed to allocate new status\n", __func__);
600 device_handler_status_info(handler, "%s\n", update);
605 void device_handler_status_download_remove(struct device_handler *handler,
606 struct process_info *procinfo)
608 struct progress_info *p, *tmp;
610 list_for_each_entry_safe(&handler->progress, p, tmp, list)
611 if (p->procinfo == procinfo) {
612 list_remove(&p->list);
614 handler->n_progress--;
618 static void device_handler_boot_status_cb(void *arg, struct status *status)
620 device_handler_status(arg, status);
623 static void countdown_status(struct device_handler *handler,
624 struct discover_boot_option *opt, unsigned int sec)
626 struct status status;
628 status.type = STATUS_INFO;
629 status.message = talloc_asprintf(handler,
630 _("Booting in %d sec: %s"), sec, opt->option->name);
631 status.backlog = false;
633 device_handler_status(handler, &status);
635 talloc_free(status.message);
638 static int default_timeout(void *arg)
640 struct device_handler *handler = arg;
641 struct discover_boot_option *opt;
643 if (!handler->default_boot_option)
646 if (handler->pending_boot)
649 opt = handler->default_boot_option;
651 if (handler->sec_to_boot) {
652 countdown_status(handler, opt, handler->sec_to_boot);
653 handler->sec_to_boot--;
654 handler->timeout_waiter = waiter_register_timeout(
655 handler->waitset, 1000,
656 default_timeout, handler);
660 handler->timeout_waiter = NULL;
662 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
666 handler->pending_boot = boot(handler, handler->default_boot_option,
667 NULL, handler->dry_run, device_handler_boot_status_cb,
669 handler->pending_boot_is_default = true;
674 enum ipmi_bootdev ipmi_type;
675 enum device_type device_type;
676 } device_type_map[] = {
677 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
678 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
679 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
680 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
683 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
684 enum device_type device_type)
688 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
689 if (device_type_map[i].device_type == device_type)
690 return device_type_map[i].ipmi_type == ipmi_type;
696 static int autoboot_option_priority(const struct config *config,
697 struct discover_boot_option *opt)
699 enum device_type type = opt->device->device->type;
700 const char *uuid = opt->device->uuid;
701 struct autoboot_option *auto_opt;
704 for (i = 0; i < config->n_autoboot_opts; i++) {
705 auto_opt = &config->autoboot_opts[i];
706 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
707 if (!strcmp(auto_opt->uuid, uuid))
708 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
710 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
711 if (auto_opt->type == type ||
712 auto_opt->type == DEVICE_TYPE_ANY)
713 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
720 * We have different priorities to resolve conflicts between boot options that
721 * report to be the default for their device. This function assigns a priority
724 static enum default_priority default_option_priority(
725 struct discover_boot_option *opt)
727 const struct config *config;
729 config = config_get();
731 /* We give highest priority to IPMI-configured boot options. If
732 * we have an IPMI bootdev configuration set, then we don't allow
733 * any other defaults */
734 if (config->ipmi_bootdev) {
735 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
736 opt->device->device->type);
738 return DEFAULT_PRIORITY_REMOTE;
740 pb_debug("handler: disabled default priority due to "
741 "non-matching IPMI type %x\n",
742 config->ipmi_bootdev);
743 return DEFAULT_PRIORITY_DISABLED;
746 /* Next, try to match the option against the user-defined autoboot
747 * options, either by device UUID or type. */
748 if (config->n_autoboot_opts) {
749 int boot_match = autoboot_option_priority(config, opt);
754 /* If the option didn't match any entry in the array, it is disabled */
755 pb_debug("handler: disabled default priority due to "
756 "non-matching UUID or type\n");
757 return DEFAULT_PRIORITY_DISABLED;
760 static void set_default(struct device_handler *handler,
761 struct discover_boot_option *opt)
763 enum default_priority cur_prio, new_prio;
765 if (!handler->autoboot_enabled)
768 pb_debug("handler: new default option: %s\n", opt->option->id);
770 new_prio = default_option_priority(opt);
772 /* Anything outside our range prevents a default boot */
773 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
776 pb_debug("handler: calculated priority %d\n", new_prio);
778 /* Resolve any conflicts: if we have a new default option, it only
779 * replaces the current if it has a higher priority. */
780 if (handler->default_boot_option) {
782 cur_prio = handler->default_boot_option_priority;
784 if (new_prio < cur_prio) {
785 pb_log("handler: new prio %d beats "
786 "old prio %d for %s\n",
788 handler->default_boot_option
790 handler->default_boot_option = opt;
791 handler->default_boot_option_priority = new_prio;
792 /* extend the timeout a little, so the user sees some
793 * indication of the change */
794 handler->sec_to_boot += 2;
800 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
801 handler->default_boot_option = opt;
802 handler->default_boot_option_priority = new_prio;
804 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
805 opt->option->id, handler->sec_to_boot);
807 default_timeout(handler);
810 static bool resource_is_resolved(struct resource *res)
812 return !res || res->resolved;
815 /* We only use this in an assert, which will disappear if we're compiling
816 * with NDEBUG, so we need the 'used' attribute for these builds */
817 static bool __attribute__((used)) boot_option_is_resolved(
818 struct discover_boot_option *opt)
820 return resource_is_resolved(opt->boot_image) &&
821 resource_is_resolved(opt->initrd) &&
822 resource_is_resolved(opt->dtb) &&
823 resource_is_resolved(opt->args_sig_file) &&
824 resource_is_resolved(opt->icon);
827 static bool resource_resolve(struct resource *res, const char *name,
828 struct discover_boot_option *opt,
829 struct device_handler *handler)
831 struct parser *parser = opt->source;
833 if (resource_is_resolved(res))
836 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
837 opt->option->id, name, parser->name);
838 parser->resolve_resource(handler, res);
840 return res->resolved;
843 static bool boot_option_resolve(struct discover_boot_option *opt,
844 struct device_handler *handler)
846 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
847 resource_resolve(opt->initrd, "initrd", opt, handler) &&
848 resource_resolve(opt->dtb, "dtb", opt, handler) &&
849 resource_resolve(opt->args_sig_file, "args_sig_file", opt,
851 resource_resolve(opt->icon, "icon", opt, handler);
854 static void boot_option_finalise(struct device_handler *handler,
855 struct discover_boot_option *opt)
857 assert(boot_option_is_resolved(opt));
859 /* check that the parsers haven't set any of the final data */
860 assert(!opt->option->boot_image_file);
861 assert(!opt->option->initrd_file);
862 assert(!opt->option->dtb_file);
863 assert(!opt->option->icon_file);
864 assert(!opt->option->device_id);
865 assert(!opt->option->args_sig_file);
868 opt->option->boot_image_file = opt->boot_image->url->full;
870 opt->option->initrd_file = opt->initrd->url->full;
872 opt->option->dtb_file = opt->dtb->url->full;
874 opt->option->icon_file = opt->icon->url->full;
875 if (opt->args_sig_file)
876 opt->option->args_sig_file = opt->args_sig_file->url->full;
878 opt->option->device_id = opt->device->device->id;
880 if (opt->option->is_default)
881 set_default(handler, opt);
884 static void notify_boot_option(struct device_handler *handler,
885 struct discover_boot_option *opt)
887 struct discover_device *dev = opt->device;
890 discover_server_notify_device_add(handler->server,
891 opt->device->device);
892 dev->notified = true;
893 discover_server_notify_boot_option_add(handler->server, opt->option);
896 static void process_boot_option_queue(struct device_handler *handler)
898 struct discover_boot_option *opt, *tmp;
900 list_for_each_entry_safe(&handler->unresolved_boot_options,
903 pb_debug("queue: attempting resolution for %s\n",
906 if (!boot_option_resolve(opt, handler))
909 pb_debug("\tresolved!\n");
911 list_remove(&opt->list);
912 list_add_tail(&opt->device->boot_options, &opt->list);
913 talloc_steal(opt->device, opt);
914 boot_option_finalise(handler, opt);
915 notify_boot_option(handler, opt);
919 struct discover_context *device_handler_discover_context_create(
920 struct device_handler *handler,
921 struct discover_device *device)
923 struct discover_context *ctx;
925 ctx = talloc_zero(handler, struct discover_context);
926 ctx->handler = handler;
927 ctx->device = device;
928 list_init(&ctx->boot_options);
933 void device_handler_add_device(struct device_handler *handler,
934 struct discover_device *device)
936 handler->n_devices++;
937 handler->devices = talloc_realloc(handler, handler->devices,
938 struct discover_device *, handler->n_devices);
939 handler->devices[handler->n_devices - 1] = device;
941 if (device->device->type == DEVICE_TYPE_NETWORK)
942 network_register_device(handler->network, device);
945 void device_handler_add_ramdisk(struct device_handler *handler,
948 struct ramdisk_device *dev;
954 for (i = 0; i < handler->n_ramdisks; i++)
955 if (!strcmp(handler->ramdisks[i]->path, path))
958 dev = talloc_zero(handler, struct ramdisk_device);
960 pb_log("Failed to allocate memory to track %s\n", path);
964 dev->path = talloc_strdup(handler, path);
966 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
967 struct ramdisk_device *,
968 handler->n_ramdisks + 1);
969 if (!handler->ramdisks) {
970 pb_log("Failed to reallocate memory"
971 "- ramdisk tracking inconsistent!\n");
975 handler->ramdisks[i] = dev;
976 i = handler->n_ramdisks++;
979 struct ramdisk_device *device_handler_get_ramdisk(
980 struct device_handler *handler)
986 /* Check if free ramdisk exists */
987 for (i = 0; i < handler->n_ramdisks; i++)
988 if (!handler->ramdisks[i]->snapshot &&
989 !handler->ramdisks[i]->origin &&
990 !handler->ramdisks[i]->base)
991 return handler->ramdisks[i];
993 /* Otherwise create a new one */
994 name = talloc_asprintf(handler, "/dev/ram%d",
995 handler->n_ramdisks);
997 pb_debug("Failed to allocate memory to name /dev/ram%d",
998 handler->n_ramdisks);
1002 id = makedev(1, handler->n_ramdisks);
1003 if (mknod(name, S_IFBLK, id)) {
1004 if (errno == EEXIST) {
1005 /* We haven't yet received updates for existing
1006 * ramdisks - add and use this one */
1007 pb_debug("Using untracked ramdisk %s\n", name);
1009 pb_log("Failed to create new ramdisk %s: %s\n",
1010 name, strerror(errno));
1014 device_handler_add_ramdisk(handler, name);
1017 return handler->ramdisks[i];
1020 void device_handler_release_ramdisk(struct discover_device *device)
1022 struct ramdisk_device *ramdisk = device->ramdisk;
1024 talloc_free(ramdisk->snapshot);
1025 talloc_free(ramdisk->origin);
1026 talloc_free(ramdisk->base);
1028 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
1029 ramdisk->sectors = 0;
1031 device->ramdisk = NULL;
1034 /* Start discovery on a hotplugged device. The device will be in our devices
1035 * array, but has only just been initialised by the hotplug source.
1037 int device_handler_discover(struct device_handler *handler,
1038 struct discover_device *dev)
1040 struct discover_context *ctx;
1043 device_handler_status_dev_info(handler, dev,
1045 * TRANSLATORS: this string will be passed the type of the
1046 * device (eg "disk" or "network"), which will be translated
1049 _("Processing new %s device"),
1050 device_type_display_name(dev->device->type));
1052 /* create our context */
1053 ctx = device_handler_discover_context_create(handler, dev);
1055 rc = mount_device(dev);
1059 /* add this device to our system info */
1060 system_info_register_blockdev(dev->device->id, dev->uuid,
1063 /* run the parsers. This will populate the ctx's boot_option list. */
1064 iterate_parsers(ctx);
1066 /* add discovered stuff to the handler */
1067 device_handler_discover_context_commit(handler, ctx);
1069 process_boot_option_queue(handler);
1071 talloc_unlink(handler, ctx);
1076 /* Incoming dhcp event */
1077 int device_handler_dhcp(struct device_handler *handler,
1078 struct discover_device *dev, struct event *event)
1080 struct discover_context *ctx;
1082 device_handler_status_dev_info(handler, dev,
1083 _("Processing DHCP lease response (ip: %s)"),
1084 event_get_param(event, "ip"));
1086 /* create our context */
1087 ctx = device_handler_discover_context_create(handler, dev);
1088 talloc_steal(ctx, event);
1091 iterate_parsers(ctx);
1093 device_handler_discover_context_commit(handler, ctx);
1095 talloc_unlink(handler, ctx);
1100 static struct discover_boot_option *find_boot_option_by_id(
1101 struct device_handler *handler, const char *id)
1105 for (i = 0; i < handler->n_devices; i++) {
1106 struct discover_device *dev = handler->devices[i];
1107 struct discover_boot_option *opt;
1109 list_for_each_entry(&dev->boot_options, opt, list)
1110 if (!strcmp(opt->option->id, id))
1117 void device_handler_boot(struct device_handler *handler,
1118 struct boot_command *cmd)
1120 struct discover_boot_option *opt = NULL;
1122 if (cmd->option_id && strlen(cmd->option_id))
1123 opt = find_boot_option_by_id(handler, cmd->option_id);
1125 if (handler->pending_boot)
1126 boot_cancel(handler->pending_boot);
1128 platform_pre_boot();
1130 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
1131 device_handler_boot_status_cb, handler);
1132 handler->pending_boot_is_default = false;
1135 void device_handler_cancel_default(struct device_handler *handler)
1137 if (handler->timeout_waiter)
1138 waiter_remove(handler->timeout_waiter);
1140 handler->timeout_waiter = NULL;
1141 handler->autoboot_enabled = false;
1143 /* we only send status if we had a default boot option queued */
1144 if (!handler->default_boot_option)
1147 pb_log("Cancelling default boot option\n");
1149 if (handler->pending_boot && handler->pending_boot_is_default) {
1150 boot_cancel(handler->pending_boot);
1151 handler->pending_boot = NULL;
1152 handler->pending_boot_is_default = false;
1155 handler->default_boot_option = NULL;
1157 device_handler_status_info(handler, _("Default boot cancelled"));
1160 void device_handler_update_config(struct device_handler *handler,
1161 struct config *config)
1165 rc = config_set(config);
1169 discover_server_notify_config(handler->server, config);
1170 device_handler_update_lang(config->lang);
1171 device_handler_reinit(handler);
1174 static char *device_from_addr(void *ctx, struct pb_url *url)
1176 char *ipaddr, *buf, *tok, *dev = NULL;
1177 const char *delim = " ";
1178 struct sockaddr_in *ip;
1179 struct sockaddr_in si;
1180 struct addrinfo *res;
1184 /* Note: IPv4 only */
1185 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1189 /* need to turn hostname into a valid IP */
1190 rc = getaddrinfo(url->host, NULL, NULL, &res);
1192 pb_debug("%s: Invalid URL\n",__func__);
1195 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1196 ip = (struct sockaddr_in *) res->ai_addr;
1197 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1201 const char *argv[] = {
1203 "route", "show", "to", "match",
1208 p = process_create(ctx);
1210 p->path = pb_system_apps.ip;
1212 p->keep_stdout = true;
1214 rc = process_run_sync(p);
1216 if (rc || p->exit_status) {
1217 /* ip has complained for some reason; most likely
1218 * there is no route to the host - bail out */
1219 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1220 pb_debug("ip buf: %s\n", p->stdout_buf);
1225 buf = p->stdout_buf;
1226 /* If a route is found, ip-route output will be of the form
1227 * "... dev DEVNAME ... " */
1228 tok = strtok(buf, delim);
1230 if (!strcmp(tok, "dev")) {
1231 tok = strtok(NULL, delim);
1232 dev = talloc_strdup(ctx, tok);
1235 tok = strtok(NULL, delim);
1240 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1244 void device_handler_process_url(struct device_handler *handler,
1245 const char *url, const char *mac, const char *ip)
1247 struct discover_context *ctx;
1248 struct discover_device *dev;
1249 struct pb_url *pb_url;
1250 struct event *event;
1251 struct param *param;
1253 if (!handler->network) {
1254 device_handler_status_err(handler, _("No network configured"));
1258 event = talloc(handler, struct event);
1259 event->type = EVENT_TYPE_USER;
1260 event->action = EVENT_ACTION_URL;
1262 if (url[strlen(url) - 1] == '/') {
1263 event->params = talloc_array(event, struct param, 3);
1264 param = &event->params[0];
1265 param->name = talloc_strdup(event, "pxepathprefix");
1266 param->value = talloc_strdup(event, url);
1267 param = &event->params[1];
1268 param->name = talloc_strdup(event, "mac");
1269 param->value = talloc_strdup(event, mac);
1270 param = &event->params[2];
1271 param->name = talloc_strdup(event, "ip");
1272 param->value = talloc_strdup(event, ip);
1273 event->n_params = 3;
1275 event->params = talloc_array(event, struct param, 1);
1276 param = &event->params[0];
1277 param->name = talloc_strdup(event, "pxeconffile");
1278 param->value = talloc_strdup(event, url);
1279 event->n_params = 1;
1282 pb_url = pb_url_parse(event, event->params->value);
1283 if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1284 device_handler_status_err(handler, _("Invalid config URL!"));
1288 if (pb_url->scheme == pb_url_file)
1289 event->device = talloc_asprintf(event, "local");
1291 event->device = device_from_addr(event, pb_url);
1293 if (!event->device) {
1294 device_handler_status_err(handler,
1295 _("Unable to route to host %s"),
1300 dev = discover_device_create(handler, mac, event->device);
1301 if (pb_url->scheme == pb_url_file)
1302 dev->device->type = DEVICE_TYPE_ANY;
1303 ctx = device_handler_discover_context_create(handler, dev);
1304 talloc_steal(ctx, event);
1307 iterate_parsers(ctx);
1309 device_handler_discover_context_commit(handler, ctx);
1311 talloc_unlink(handler, ctx);
1314 #ifndef PETITBOOT_TEST
1317 * context_commit - Commit a temporary discovery context to the handler,
1318 * and notify the clients about any new options / devices
1320 void device_handler_discover_context_commit(struct device_handler *handler,
1321 struct discover_context *ctx)
1323 struct discover_device *dev = ctx->device;
1324 struct discover_boot_option *opt, *tmp;
1326 if (!device_lookup_by_uuid(handler, dev->uuid))
1327 device_handler_add_device(handler, dev);
1329 /* move boot options from the context to the device */
1330 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
1331 list_remove(&opt->list);
1333 /* All boot options need at least a kernel image */
1334 if (!opt->boot_image || !opt->boot_image->url) {
1335 pb_log("boot option %s is missing boot image, ignoring\n",
1341 if (boot_option_resolve(opt, handler)) {
1342 pb_log("boot option %s is resolved, "
1343 "sending to clients\n",
1345 list_add_tail(&dev->boot_options, &opt->list);
1346 talloc_steal(dev, opt);
1347 boot_option_finalise(handler, opt);
1348 notify_boot_option(handler, opt);
1350 if (!opt->source->resolve_resource) {
1351 pb_log("parser %s gave us an unresolved "
1352 "resource (%s), but no way to "
1354 opt->source->name, opt->option->id);
1357 pb_log("boot option %s is unresolved, "
1358 "adding to queue\n",
1360 list_add(&handler->unresolved_boot_options,
1362 talloc_steal(handler, opt);
1368 static void device_handler_update_lang(const char *lang)
1370 const char *cur_lang;
1375 cur_lang = setlocale(LC_ALL, NULL);
1376 if (cur_lang && !strcmp(cur_lang, lang))
1379 setlocale(LC_ALL, lang);
1382 static int device_handler_init_sources(struct device_handler *handler)
1384 /* init our device sources: udev, network and user events */
1385 handler->udev = udev_init(handler, handler->waitset);
1389 handler->network = network_init(handler, handler->waitset,
1391 if (!handler->network)
1394 handler->user_event = user_event_init(handler, handler->waitset);
1395 if (!handler->user_event)
1401 static void device_handler_reinit_sources(struct device_handler *handler)
1403 /* if we haven't initialised sources previously (becuase we started in
1404 * safe mode), then init once here. */
1405 if (!(handler->udev || handler->network || handler->user_event)) {
1406 device_handler_init_sources(handler);
1410 udev_reinit(handler->udev);
1412 network_shutdown(handler->network);
1413 handler->network = network_init(handler, handler->waitset,
1417 static inline const char *get_device_path(struct discover_device *dev)
1419 return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1422 static char *check_subvols(struct discover_device *dev)
1424 const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1429 if (strncmp(fstype, "btrfs", strlen("btrfs")))
1430 return dev->mount_path;
1432 /* On btrfs a device's root may be under a subvolume path */
1433 path = join_paths(dev, dev->mount_path, "@");
1434 rc = stat(path, &sb);
1435 if (!rc && S_ISDIR(sb.st_mode)) {
1436 pb_debug("Using '%s' for btrfs root path\n", path);
1441 return dev->mount_path;
1444 static bool check_existing_mount(struct discover_device *dev)
1446 struct stat devstat, mntstat;
1447 const char *device_path;
1452 device_path = get_device_path(dev);
1454 rc = stat(device_path, &devstat);
1456 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1460 if (!S_ISBLK(devstat.st_mode)) {
1461 pb_debug("%s: %s isn't a block device?\n", __func__,
1466 fp = fopen("/proc/self/mounts", "r");
1469 mnt = getmntent(fp);
1473 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1476 rc = stat(mnt->mnt_fsname, &mntstat);
1480 if (!S_ISBLK(mntstat.st_mode))
1483 if (mntstat.st_rdev == devstat.st_rdev) {
1484 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1485 dev->root_path = check_subvols(dev);
1486 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1487 dev->mounted = true;
1488 dev->unmount = false;
1490 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1491 __func__, dev->device_path,
1492 dev->mounted_rw ? 'w' : 'o',
1504 * Attempt to mount a filesystem safely, while handling certain filesytem-
1507 static int try_mount(const char *device_path, const char *mount_path,
1508 const char *fstype, unsigned long flags,
1511 const char *fs, *safe_opts;
1514 /* Mount ext3 as ext4 instead so 'norecovery' can be used */
1515 if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
1516 pb_debug("Mounting ext3 filesystem as ext4\n");
1521 if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
1522 strncmp(fs, "ext4", strlen("ext4")) == 0)
1523 safe_opts = "norecovery";
1528 /* If no snapshot is available don't attempt recovery */
1530 return mount(device_path, mount_path, fs, flags, safe_opts);
1532 rc = mount(device_path, mount_path, fs, flags, NULL);
1537 /* Mounting failed; some filesystems will fail to mount if a recovery
1538 * journal exists (eg. cross-endian XFS), so try again with norecovery
1539 * where that option is available.
1540 * If mounting read-write just return the error as norecovery is not a
1542 if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
1546 return mount(device_path, mount_path, fs, flags, safe_opts);
1549 static int mount_device(struct discover_device *dev)
1551 const char *fstype, *device_path;
1554 if (!dev->device_path)
1560 if (check_existing_mount(dev))
1563 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1567 dev->mount_path = join_paths(dev, mount_base(),
1570 if (pb_mkdir_recursive(dev->mount_path)) {
1571 pb_log("couldn't create mount directory %s: %s\n",
1572 dev->mount_path, strerror(errno));
1576 device_path = get_device_path(dev);
1578 pb_log("mounting device %s read-only\n", dev->device_path);
1579 rc = try_mount(device_path, dev->mount_path, fstype,
1580 MS_RDONLY | MS_SILENT, dev->ramdisk);
1583 dev->mounted = true;
1584 dev->mounted_rw = false;
1585 dev->unmount = true;
1586 dev->root_path = check_subvols(dev);
1590 pb_log("couldn't mount device %s: mount failed: %s\n",
1591 device_path, strerror(errno));
1593 /* If mount fails clean up any snapshot */
1594 devmapper_destroy_snapshot(dev);
1596 pb_rmdir_recursive(mount_base(), dev->mount_path);
1598 talloc_free(dev->mount_path);
1599 dev->mount_path = NULL;
1603 static int umount_device(struct discover_device *dev)
1605 const char *device_path;
1608 if (!dev->mounted || !dev->unmount)
1611 device_path = get_device_path(dev);
1613 pb_log("unmounting device %s\n", device_path);
1614 rc = umount(dev->mount_path);
1618 dev->mounted = false;
1619 devmapper_destroy_snapshot(dev);
1621 pb_rmdir_recursive(mount_base(), dev->mount_path);
1623 talloc_free(dev->mount_path);
1624 dev->mount_path = NULL;
1625 dev->root_path = NULL;
1630 int device_request_write(struct discover_device *dev, bool *release)
1632 const char *fstype, *device_path;
1633 const struct config *config;
1638 config = config_get();
1639 if (!config->allow_writes)
1645 if (dev->mounted_rw)
1648 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1650 device_path = get_device_path(dev);
1652 pb_log("remounting device %s read-write\n", device_path);
1654 rc = umount(dev->mount_path);
1656 pb_log("Failed to unmount %s: %s\n",
1657 dev->mount_path, strerror(errno));
1661 rc = try_mount(device_path, dev->mount_path, fstype,
1662 MS_SILENT, dev->ramdisk);
1666 dev->mounted_rw = true;
1671 pb_log("Unable to remount device %s read-write: %s\n",
1672 device_path, strerror(errno));
1673 rc = try_mount(device_path, dev->mount_path, fstype,
1674 MS_RDONLY | MS_SILENT, dev->ramdisk);
1676 pb_log("Unable to recover mount for %s: %s\n",
1677 device_path, strerror(errno));
1681 void device_release_write(struct discover_device *dev, bool release)
1683 const char *fstype, *device_path;
1688 device_path = get_device_path(dev);
1690 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1692 pb_log("remounting device %s read-only\n", device_path);
1694 if (umount(dev->mount_path)) {
1695 pb_log("Failed to unmount %s\n", dev->mount_path);
1698 dev->mounted_rw = dev->mounted = false;
1701 devmapper_merge_snapshot(dev);
1702 /* device_path becomes stale after merge */
1703 device_path = get_device_path(dev);
1706 if (try_mount(device_path, dev->mount_path, fstype,
1707 MS_RDONLY | MS_SILENT, dev->ramdisk))
1708 pb_log("Failed to remount %s read-only: %s\n",
1709 device_path, strerror(errno));
1711 dev->mounted = true;
1714 void device_sync_snapshots(struct device_handler *handler, const char *device)
1716 struct discover_device *dev = NULL;
1720 /* Find matching device and sync */
1721 dev = device_lookup_by_name(handler, device);
1723 pb_log("%s: device name '%s' unrecognised\n",
1728 device_release_write(dev, true);
1730 pb_log("%s has no snapshot to merge, skipping\n",
1735 /* Otherwise sync all relevant devices */
1736 for (i = 0; i < handler->n_devices; i++) {
1737 dev = handler->devices[i];
1738 if (dev->device->type != DEVICE_TYPE_DISK &&
1739 dev->device->type != DEVICE_TYPE_USB)
1742 device_release_write(dev, true);
1744 pb_log("%s has no snapshot to merge, skipping\n",
1751 void device_handler_discover_context_commit(
1752 struct device_handler *handler __attribute__((unused)),
1753 struct discover_context *ctx __attribute__((unused)))
1755 pb_log("%s stubbed out for test cases\n", __func__);
1758 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1762 static int device_handler_init_sources(
1763 struct device_handler *handler __attribute__((unused)))
1768 static void device_handler_reinit_sources(
1769 struct device_handler *handler __attribute__((unused)))
1773 static int umount_device(struct discover_device *dev __attribute__((unused)))
1778 static int __attribute__((unused)) mount_device(
1779 struct discover_device *dev __attribute__((unused)))
1784 int device_request_write(struct discover_device *dev __attribute__((unused)),
1791 void device_release_write(struct discover_device *dev __attribute__((unused)),
1792 bool release __attribute__((unused)))
1796 void device_sync_snapshots(
1797 struct device_handler *handler __attribute__((unused)),
1798 const char *device __attribute__((unused)))