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 device_handler {
50 struct discover_server *server;
54 struct network *network;
55 struct user_event *user_event;
57 struct discover_device **devices;
58 unsigned int n_devices;
60 struct ramdisk_device **ramdisks;
61 unsigned int n_ramdisks;
63 struct waitset *waitset;
64 struct waiter *timeout_waiter;
65 bool autoboot_enabled;
66 unsigned int sec_to_boot;
68 struct discover_boot_option *default_boot_option;
69 int default_boot_option_priority;
71 struct list unresolved_boot_options;
73 struct boot_task *pending_boot;
74 bool pending_boot_is_default;
77 static int mount_device(struct discover_device *dev);
78 static int umount_device(struct discover_device *dev);
80 static int device_handler_init_sources(struct device_handler *handler);
81 static void device_handler_reinit_sources(struct device_handler *handler);
83 static void device_handler_update_lang(const char *lang);
85 void discover_context_add_boot_option(struct discover_context *ctx,
86 struct discover_boot_option *boot_option)
88 boot_option->source = ctx->parser;
89 list_add_tail(&ctx->boot_options, &boot_option->list);
90 talloc_steal(ctx, boot_option);
94 * device_handler_get_device_count - Get the count of current handler devices.
97 int device_handler_get_device_count(const struct device_handler *handler)
99 return handler->n_devices;
103 * device_handler_get_device - Get a handler device by index.
106 const struct discover_device *device_handler_get_device(
107 const struct device_handler *handler, unsigned int index)
109 if (index >= handler->n_devices) {
110 assert(0 && "bad index");
114 return handler->devices[index];
117 struct discover_boot_option *discover_boot_option_create(
118 struct discover_context *ctx,
119 struct discover_device *device)
121 struct discover_boot_option *opt;
123 opt = talloc_zero(ctx, struct discover_boot_option);
124 opt->option = talloc_zero(opt, struct boot_option);
125 opt->device = device;
130 static int device_match_uuid(struct discover_device *dev, const char *uuid)
132 return dev->uuid && !strcmp(dev->uuid, uuid);
135 static int device_match_label(struct discover_device *dev, const char *label)
137 return dev->label && !strcmp(dev->label, label);
140 static int device_match_id(struct discover_device *dev, const char *id)
142 return !strcmp(dev->device->id, id);
145 static int device_match_serial(struct discover_device *dev, const char *serial)
147 const char *val = discover_device_get_param(dev, "ID_SERIAL");
148 return val && !strcmp(val, serial);
151 static struct discover_device *device_lookup(
152 struct device_handler *device_handler,
153 int (match_fn)(struct discover_device *, const char *),
156 struct discover_device *dev;
162 for (i = 0; i < device_handler->n_devices; i++) {
163 dev = device_handler->devices[i];
165 if (match_fn(dev, str))
172 struct discover_device *device_lookup_by_name(struct device_handler *handler,
175 if (!strncmp(name, "/dev/", strlen("/dev/")))
176 name += strlen("/dev/");
178 return device_lookup_by_id(handler, name);
181 struct discover_device *device_lookup_by_uuid(
182 struct device_handler *device_handler,
185 return device_lookup(device_handler, device_match_uuid, uuid);
188 struct discover_device *device_lookup_by_label(
189 struct device_handler *device_handler,
192 return device_lookup(device_handler, device_match_label, label);
195 struct discover_device *device_lookup_by_id(
196 struct device_handler *device_handler,
199 return device_lookup(device_handler, device_match_id, id);
202 struct discover_device *device_lookup_by_serial(
203 struct device_handler *device_handler,
206 return device_lookup(device_handler, device_match_serial, serial);
209 void device_handler_destroy(struct device_handler *handler)
211 talloc_free(handler);
214 static int destroy_device(void *arg)
216 struct discover_device *dev = arg;
223 struct discover_device *discover_device_create(struct device_handler *handler,
226 struct discover_device *dev;
228 dev = device_lookup_by_id(handler, id);
232 dev = talloc_zero(handler, struct discover_device);
233 dev->device = talloc_zero(dev, struct device);
234 dev->device->id = talloc_strdup(dev->device, id);
235 list_init(&dev->params);
236 list_init(&dev->boot_options);
238 talloc_set_destructor(dev, destroy_device);
243 struct discover_device_param {
246 struct list_item list;
249 void discover_device_set_param(struct discover_device *device,
250 const char *name, const char *value)
252 struct discover_device_param *param;
255 list_for_each_entry(&device->params, param, list) {
256 if (!strcmp(param->name, name)) {
265 param = talloc(device, struct discover_device_param);
266 param->name = talloc_strdup(param, name);
267 list_add(&device->params, ¶m->list);
270 list_remove(¶m->list);
274 talloc_free(param->value);
277 param->value = talloc_strdup(param, value);
280 const char *discover_device_get_param(struct discover_device *device,
283 struct discover_device_param *param;
285 list_for_each_entry(&device->params, param, list) {
286 if (!strcmp(param->name, name))
292 struct device_handler *device_handler_init(struct discover_server *server,
293 struct waitset *waitset, int dry_run)
295 struct device_handler *handler;
298 handler = talloc_zero(NULL, struct device_handler);
299 handler->server = server;
300 handler->waitset = waitset;
301 handler->dry_run = dry_run;
302 handler->autoboot_enabled = config_get()->autoboot_enabled;
304 list_init(&handler->unresolved_boot_options);
306 /* set up our mount point base */
307 pb_mkdir_recursive(mount_base());
311 if (config_get()->safe_mode)
314 rc = device_handler_init_sources(handler);
316 talloc_free(handler);
323 void device_handler_reinit(struct device_handler *handler)
325 struct discover_boot_option *opt, *tmp;
326 struct ramdisk_device *ramdisk;
329 device_handler_cancel_default(handler);
331 /* free unresolved boot options */
332 list_for_each_entry_safe(&handler->unresolved_boot_options,
335 list_init(&handler->unresolved_boot_options);
337 /* drop all devices */
338 for (i = 0; i < handler->n_devices; i++) {
339 discover_server_notify_device_remove(handler->server,
340 handler->devices[i]->device);
341 ramdisk = handler->devices[i]->ramdisk;
342 talloc_free(handler->devices[i]);
343 talloc_free(ramdisk);
346 talloc_free(handler->devices);
347 handler->devices = NULL;
348 handler->n_devices = 0;
349 talloc_free(handler->ramdisks);
350 handler->ramdisks = NULL;
351 handler->n_ramdisks = 0;
353 device_handler_reinit_sources(handler);
356 void device_handler_remove(struct device_handler *handler,
357 struct discover_device *device)
359 struct discover_boot_option *opt, *tmp;
362 for (i = 0; i < handler->n_devices; i++)
363 if (handler->devices[i] == device)
366 if (i == handler->n_devices) {
371 /* Free any unresolved options, as they're currently allocated
372 * against the handler */
373 list_for_each_entry_safe(&handler->unresolved_boot_options,
375 if (opt->device != device)
377 list_remove(&opt->list);
381 /* if this is a network device, we have to unregister it from the
383 if (device->device->type == DEVICE_TYPE_NETWORK)
384 network_unregister_device(handler->network, device);
386 handler->n_devices--;
387 memmove(&handler->devices[i], &handler->devices[i + 1],
388 (handler->n_devices - i) * sizeof(handler->devices[0]));
389 handler->devices = talloc_realloc(handler, handler->devices,
390 struct discover_device *, handler->n_devices);
392 if (device->notified)
393 discover_server_notify_device_remove(handler->server,
399 static void boot_status(void *arg, struct boot_status *status)
401 struct device_handler *handler = arg;
403 discover_server_notify_boot_status(handler->server, status);
406 static void countdown_status(struct device_handler *handler,
407 struct discover_boot_option *opt, unsigned int sec)
409 struct boot_status status;
411 status.type = BOOT_STATUS_INFO;
412 status.progress = -1;
413 status.detail = NULL;
414 status.message = talloc_asprintf(handler,
415 _("Booting in %d sec: %s"), sec, opt->option->name);
417 discover_server_notify_boot_status(handler->server, &status);
419 talloc_free(status.message);
422 static int default_timeout(void *arg)
424 struct device_handler *handler = arg;
425 struct discover_boot_option *opt;
427 if (!handler->default_boot_option)
430 if (handler->pending_boot)
433 opt = handler->default_boot_option;
435 if (handler->sec_to_boot) {
436 countdown_status(handler, opt, handler->sec_to_boot);
437 handler->sec_to_boot--;
438 handler->timeout_waiter = waiter_register_timeout(
439 handler->waitset, 1000,
440 default_timeout, handler);
444 handler->timeout_waiter = NULL;
446 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
448 handler->pending_boot = boot(handler, handler->default_boot_option,
449 NULL, handler->dry_run, boot_status, handler);
450 handler->pending_boot_is_default = true;
455 enum ipmi_bootdev ipmi_type;
456 enum device_type device_type;
457 } device_type_map[] = {
458 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
459 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
460 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
461 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
464 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
465 enum device_type device_type)
469 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
470 if (device_type_map[i].device_type == device_type)
471 return device_type_map[i].ipmi_type == ipmi_type;
477 static int autoboot_option_priority(const struct config *config,
478 struct discover_boot_option *opt)
480 enum device_type type = opt->device->device->type;
481 const char *uuid = opt->device->uuid;
482 struct autoboot_option *auto_opt;
485 for (i = 0; i < config->n_autoboot_opts; i++) {
486 auto_opt = &config->autoboot_opts[i];
487 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
488 if (!strcmp(auto_opt->uuid, uuid))
489 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
491 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
492 if (auto_opt->type == type ||
493 auto_opt->type == DEVICE_TYPE_ANY)
494 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
501 * We have different priorities to resolve conflicts between boot options that
502 * report to be the default for their device. This function assigns a priority
505 static enum default_priority default_option_priority(
506 struct discover_boot_option *opt)
508 const struct config *config;
510 config = config_get();
512 /* We give highest priority to IPMI-configured boot options. If
513 * we have an IPMI bootdev configuration set, then we don't allow
514 * any other defaults */
515 if (config->ipmi_bootdev) {
516 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
517 opt->device->device->type);
519 return DEFAULT_PRIORITY_REMOTE;
521 pb_debug("handler: disabled default priority due to "
522 "non-matching IPMI type %x\n",
523 config->ipmi_bootdev);
524 return DEFAULT_PRIORITY_DISABLED;
527 /* Next, try to match the option against the user-defined autoboot
528 * options, either by device UUID or type. */
529 if (config->n_autoboot_opts) {
530 int boot_match = autoboot_option_priority(config, opt);
535 /* If the option didn't match any entry in the array, it is disabled */
536 pb_debug("handler: disabled default priority due to "
537 "non-matching UUID or type\n");
538 return DEFAULT_PRIORITY_DISABLED;
541 static void set_default(struct device_handler *handler,
542 struct discover_boot_option *opt)
544 enum default_priority cur_prio, new_prio;
546 if (!handler->autoboot_enabled)
549 pb_debug("handler: new default option: %s\n", opt->option->id);
551 new_prio = default_option_priority(opt);
553 /* Anything outside our range prevents a default boot */
554 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
557 pb_debug("handler: calculated priority %d\n", new_prio);
559 /* Resolve any conflicts: if we have a new default option, it only
560 * replaces the current if it has a higher priority. */
561 if (handler->default_boot_option) {
563 cur_prio = handler->default_boot_option_priority;
565 if (new_prio < cur_prio) {
566 pb_log("handler: new prio %d beats "
567 "old prio %d for %s\n",
569 handler->default_boot_option
571 handler->default_boot_option = opt;
572 handler->default_boot_option_priority = new_prio;
573 /* extend the timeout a little, so the user sees some
574 * indication of the change */
575 handler->sec_to_boot += 2;
581 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
582 handler->default_boot_option = opt;
583 handler->default_boot_option_priority = new_prio;
585 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
586 opt->option->id, handler->sec_to_boot);
588 default_timeout(handler);
591 static bool resource_is_resolved(struct resource *res)
593 return !res || res->resolved;
596 /* We only use this in an assert, which will disappear if we're compiling
597 * with NDEBUG, so we need the 'used' attribute for these builds */
598 static bool __attribute__((used)) boot_option_is_resolved(
599 struct discover_boot_option *opt)
601 return resource_is_resolved(opt->boot_image) &&
602 resource_is_resolved(opt->initrd) &&
603 resource_is_resolved(opt->dtb) &&
604 resource_is_resolved(opt->icon);
607 static bool resource_resolve(struct resource *res, const char *name,
608 struct discover_boot_option *opt,
609 struct device_handler *handler)
611 struct parser *parser = opt->source;
613 if (resource_is_resolved(res))
616 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
617 opt->option->id, name, parser->name);
618 parser->resolve_resource(handler, res);
620 return res->resolved;
623 static bool boot_option_resolve(struct discover_boot_option *opt,
624 struct device_handler *handler)
626 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
627 resource_resolve(opt->initrd, "initrd", opt, handler) &&
628 resource_resolve(opt->dtb, "dtb", opt, handler) &&
629 resource_resolve(opt->icon, "icon", opt, handler);
632 static void boot_option_finalise(struct device_handler *handler,
633 struct discover_boot_option *opt)
635 assert(boot_option_is_resolved(opt));
637 /* check that the parsers haven't set any of the final data */
638 assert(!opt->option->boot_image_file);
639 assert(!opt->option->initrd_file);
640 assert(!opt->option->dtb_file);
641 assert(!opt->option->icon_file);
642 assert(!opt->option->device_id);
645 opt->option->boot_image_file = opt->boot_image->url->full;
647 opt->option->initrd_file = opt->initrd->url->full;
649 opt->option->dtb_file = opt->dtb->url->full;
651 opt->option->icon_file = opt->icon->url->full;
653 opt->option->device_id = opt->device->device->id;
655 if (opt->option->is_default)
656 set_default(handler, opt);
659 static void notify_boot_option(struct device_handler *handler,
660 struct discover_boot_option *opt)
662 struct discover_device *dev = opt->device;
665 discover_server_notify_device_add(handler->server,
666 opt->device->device);
667 dev->notified = true;
668 discover_server_notify_boot_option_add(handler->server, opt->option);
671 static void process_boot_option_queue(struct device_handler *handler)
673 struct discover_boot_option *opt, *tmp;
675 list_for_each_entry_safe(&handler->unresolved_boot_options,
678 pb_debug("queue: attempting resolution for %s\n",
681 if (!boot_option_resolve(opt, handler))
684 pb_debug("\tresolved!\n");
686 list_remove(&opt->list);
687 list_add_tail(&opt->device->boot_options, &opt->list);
688 talloc_steal(opt->device, opt);
689 boot_option_finalise(handler, opt);
690 notify_boot_option(handler, opt);
694 struct discover_context *device_handler_discover_context_create(
695 struct device_handler *handler,
696 struct discover_device *device)
698 struct discover_context *ctx;
700 ctx = talloc_zero(handler, struct discover_context);
701 ctx->device = device;
702 ctx->network = handler->network;
703 list_init(&ctx->boot_options);
709 * context_commit - Commit a temporary discovery context to the handler,
710 * and notify the clients about any new options / devices
712 void device_handler_discover_context_commit(struct device_handler *handler,
713 struct discover_context *ctx)
715 struct discover_device *dev = ctx->device;
716 struct discover_boot_option *opt, *tmp;
718 if (!device_lookup_by_id(handler, dev->device->id))
719 device_handler_add_device(handler, dev);
721 /* move boot options from the context to the device */
722 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
723 list_remove(&opt->list);
725 if (boot_option_resolve(opt, handler)) {
726 pb_log("boot option %s is resolved, "
727 "sending to clients\n",
729 list_add_tail(&dev->boot_options, &opt->list);
730 talloc_steal(dev, opt);
731 boot_option_finalise(handler, opt);
732 notify_boot_option(handler, opt);
734 if (!opt->source->resolve_resource) {
735 pb_log("parser %s gave us an unresolved "
736 "resource (%s), but no way to "
738 opt->source->name, opt->option->id);
741 pb_log("boot option %s is unresolved, "
744 list_add(&handler->unresolved_boot_options,
746 talloc_steal(handler, opt);
752 void device_handler_add_device(struct device_handler *handler,
753 struct discover_device *device)
755 handler->n_devices++;
756 handler->devices = talloc_realloc(handler, handler->devices,
757 struct discover_device *, handler->n_devices);
758 handler->devices[handler->n_devices - 1] = device;
760 if (device->device->type == DEVICE_TYPE_NETWORK)
761 network_register_device(handler->network, device);
764 void device_handler_add_ramdisk(struct device_handler *handler,
767 struct ramdisk_device *dev;
773 for (i = 0; i < handler->n_ramdisks; i++)
774 if (!strcmp(handler->ramdisks[i]->path, path))
777 dev = talloc_zero(handler, struct ramdisk_device);
779 pb_log("Failed to allocate memory to track %s\n", path);
783 dev->path = talloc_strdup(handler, path);
785 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
786 struct ramdisk_device *,
787 handler->n_ramdisks + 1);
788 if (!handler->ramdisks) {
789 pb_log("Failed to reallocate memory"
790 "- ramdisk tracking inconsistent!\n");
794 handler->ramdisks[i] = dev;
795 i = handler->n_ramdisks++;
798 struct ramdisk_device *device_handler_get_ramdisk(
799 struct device_handler *handler)
805 /* Check if free ramdisk exists */
806 for (i = 0; i < handler->n_ramdisks; i++)
807 if (!handler->ramdisks[i]->snapshot &&
808 !handler->ramdisks[i]->origin &&
809 !handler->ramdisks[i]->base)
810 return handler->ramdisks[i];
812 /* Otherwise create a new one */
813 name = talloc_asprintf(handler, "/dev/ram%d",
814 handler->n_ramdisks);
816 pb_debug("Failed to allocate memory to name /dev/ram%d",
817 handler->n_ramdisks);
821 id = makedev(1, handler->n_ramdisks);
822 if (mknod(name, S_IFBLK, id)) {
823 if (errno == EEXIST) {
824 /* We haven't yet received updates for existing
825 * ramdisks - add and use this one */
826 pb_debug("Using untracked ramdisk %s\n", name);
828 pb_log("Failed to create new ramdisk %s: %s\n",
829 name, strerror(errno));
833 device_handler_add_ramdisk(handler, name);
836 return handler->ramdisks[i];
839 void device_handler_release_ramdisk(struct discover_device *device)
841 struct ramdisk_device *ramdisk = device->ramdisk;
843 talloc_free(ramdisk->snapshot);
844 talloc_free(ramdisk->origin);
845 talloc_free(ramdisk->base);
847 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
848 ramdisk->sectors = 0;
850 device->ramdisk = NULL;
853 /* Start discovery on a hotplugged device. The device will be in our devices
854 * array, but has only just been initialised by the hotplug source.
856 int device_handler_discover(struct device_handler *handler,
857 struct discover_device *dev)
859 struct discover_context *ctx;
860 struct boot_status *status;
863 status = talloc_zero(handler, struct boot_status);
864 status->type = BOOT_STATUS_INFO;
865 status->message = talloc_asprintf(status, "Processing %s device %s",
866 device_type_display_name(dev->device->type),
868 boot_status(handler, status);
870 process_boot_option_queue(handler);
872 /* create our context */
873 ctx = device_handler_discover_context_create(handler, dev);
875 rc = mount_device(dev);
879 /* add this device to our system info */
880 system_info_register_blockdev(dev->device->id, dev->uuid,
883 /* run the parsers. This will populate the ctx's boot_option list. */
884 iterate_parsers(ctx);
886 /* add discovered stuff to the handler */
887 device_handler_discover_context_commit(handler, ctx);
890 status->message = talloc_asprintf(status,"Processing %s complete\n",
892 boot_status(handler, status);
900 /* Incoming dhcp event */
901 int device_handler_dhcp(struct device_handler *handler,
902 struct discover_device *dev, struct event *event)
904 struct discover_context *ctx;
905 struct boot_status *status;
907 status = talloc_zero(handler, struct boot_status);
908 status->type = BOOT_STATUS_INFO;
909 status->message = talloc_asprintf(status, "Processing dhcp event on %s",
911 boot_status(handler, status);
913 /* create our context */
914 ctx = device_handler_discover_context_create(handler, dev);
917 iterate_parsers(ctx);
919 device_handler_discover_context_commit(handler, ctx);
921 status->message = talloc_asprintf(status,"Processing %s complete\n",
923 boot_status(handler, status);
931 /* incoming conf event */
932 int device_handler_conf(struct device_handler *handler,
933 struct discover_device *dev, struct pb_url *url)
935 struct discover_context *ctx;
936 struct boot_status *status;
938 status = talloc_zero(handler, struct boot_status);
939 status->type = BOOT_STATUS_INFO;
940 status->message = talloc_asprintf(status, "Processing user config");
941 boot_status(handler, status);
943 /* create our context */
944 ctx = device_handler_discover_context_create(handler, dev);
947 iterate_parsers(ctx);
949 device_handler_discover_context_commit(handler, ctx);
951 status->message = talloc_asprintf(status,
952 "Processing user config complete");
953 boot_status(handler, status);
961 static struct discover_boot_option *find_boot_option_by_id(
962 struct device_handler *handler, const char *id)
966 for (i = 0; i < handler->n_devices; i++) {
967 struct discover_device *dev = handler->devices[i];
968 struct discover_boot_option *opt;
970 list_for_each_entry(&dev->boot_options, opt, list)
971 if (!strcmp(opt->option->id, id))
978 void device_handler_boot(struct device_handler *handler,
979 struct boot_command *cmd)
981 struct discover_boot_option *opt = NULL;
983 if (cmd->option_id && strlen(cmd->option_id))
984 opt = find_boot_option_by_id(handler, cmd->option_id);
986 if (handler->pending_boot)
987 boot_cancel(handler->pending_boot);
991 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
992 boot_status, handler);
993 handler->pending_boot_is_default = false;
996 void device_handler_cancel_default(struct device_handler *handler)
998 struct boot_status status;
1000 if (handler->timeout_waiter)
1001 waiter_remove(handler->timeout_waiter);
1003 handler->timeout_waiter = NULL;
1004 handler->autoboot_enabled = false;
1006 /* we only send status if we had a default boot option queued */
1007 if (!handler->default_boot_option)
1010 pb_log("Cancelling default boot option\n");
1012 if (handler->pending_boot && handler->pending_boot_is_default) {
1013 boot_cancel(handler->pending_boot);
1014 handler->pending_boot = NULL;
1015 handler->pending_boot_is_default = false;
1018 handler->default_boot_option = NULL;
1020 status.type = BOOT_STATUS_INFO;
1021 status.progress = -1;
1022 status.detail = NULL;
1023 status.message = _("Default boot cancelled");
1025 discover_server_notify_boot_status(handler->server, &status);
1028 void device_handler_update_config(struct device_handler *handler,
1029 struct config *config)
1033 rc = config_set(config);
1037 discover_server_notify_config(handler->server, config);
1038 device_handler_update_lang(config->lang);
1039 device_handler_reinit(handler);
1042 static char *device_from_addr(void *ctx, struct pb_url *url)
1044 char *ipaddr, *buf, *tok, *dev = NULL;
1045 const char *delim = " ";
1046 struct sockaddr_in *ip;
1047 struct sockaddr_in si;
1048 struct addrinfo *res;
1052 /* Note: IPv4 only */
1053 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1057 /* need to turn hostname into a valid IP */
1058 rc = getaddrinfo(url->host, NULL, NULL, &res);
1060 pb_debug("%s: Invalid URL\n",__func__);
1063 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1064 ip = (struct sockaddr_in *) res->ai_addr;
1065 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1069 const char *argv[] = {
1071 "route", "show", "to", "match",
1076 p = process_create(ctx);
1078 p->path = pb_system_apps.ip;
1080 p->keep_stdout = true;
1082 rc = process_run_sync(p);
1085 /* ip has complained for some reason; most likely
1086 * there is no route to the host - bail out */
1087 pb_debug("%s: No route to %s\n",__func__,url->host);
1091 buf = p->stdout_buf;
1092 /* If a route is found, ip-route output will be of the form
1093 * "... dev DEVNAME ... " */
1094 tok = strtok(buf, delim);
1096 if (!strcmp(tok, "dev")) {
1097 tok = strtok(NULL, delim);
1098 dev = talloc_strdup(ctx, tok);
1101 tok = strtok(NULL, delim);
1106 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1111 void device_handler_process_url(struct device_handler *handler,
1114 struct discover_context *ctx;
1115 struct discover_device *dev;
1116 struct boot_status *status;
1117 struct pb_url *pb_url;
1118 struct event *event;
1119 struct param *param;
1121 status = talloc(handler, struct boot_status);
1123 status->type = BOOT_STATUS_ERROR;
1124 status->progress = 0;
1125 status->detail = talloc_asprintf(status,
1126 _("Received config URL %s"), url);
1128 if (!handler->network) {
1129 status->message = talloc_asprintf(handler,
1130 _("No network configured"));
1134 event = talloc(handler, struct event);
1135 event->type = EVENT_TYPE_USER;
1136 event->action = EVENT_ACTION_CONF;
1138 event->params = talloc_array(event, struct param, 1);
1139 param = &event->params[0];
1140 param->name = talloc_strdup(event, "pxeconffile");
1141 param->value = talloc_strdup(event, url);
1142 event->n_params = 1;
1144 pb_url = pb_url_parse(event, event->params->value);
1145 if (!pb_url || !pb_url->host) {
1146 status->message = talloc_asprintf(handler,
1147 _("Invalid config URL!"));
1151 event->device = device_from_addr(event, pb_url);
1152 if (!event->device) {
1153 status->message = talloc_asprintf(status,
1154 _("Unable to route to host %s"),
1159 dev = discover_device_create(handler, event->device);
1160 ctx = device_handler_discover_context_create(handler, dev);
1163 iterate_parsers(ctx);
1165 device_handler_discover_context_commit(handler, ctx);
1169 status->type = BOOT_STATUS_INFO;
1170 status->message = talloc_asprintf(status, _("Config file %s parsed"),
1173 boot_status(handler, status);
1174 talloc_free(status);
1177 #ifndef PETITBOOT_TEST
1179 static void device_handler_update_lang(const char *lang)
1181 const char *cur_lang;
1186 cur_lang = setlocale(LC_ALL, NULL);
1187 if (cur_lang && !strcmp(cur_lang, lang))
1190 setlocale(LC_ALL, lang);
1193 static int device_handler_init_sources(struct device_handler *handler)
1195 /* init our device sources: udev, network and user events */
1196 handler->udev = udev_init(handler, handler->waitset);
1200 handler->network = network_init(handler, handler->waitset,
1202 if (!handler->network)
1205 handler->user_event = user_event_init(handler, handler->waitset);
1206 if (!handler->user_event)
1212 static void device_handler_reinit_sources(struct device_handler *handler)
1214 /* if we haven't initialised sources previously (becuase we started in
1215 * safe mode), then init once here. */
1216 if (!(handler->udev || handler->network || handler->user_event)) {
1217 device_handler_init_sources(handler);
1221 udev_reinit(handler->udev);
1223 network_shutdown(handler->network);
1224 handler->network = network_init(handler, handler->waitset,
1228 static const char *fs_parameters(unsigned int rw_flags, const char *fstype)
1230 if ((rw_flags | MS_RDONLY) != MS_RDONLY)
1233 /* Avoid writing back to the disk on journaled filesystems */
1234 if (!strncmp(fstype, "ext4", strlen("ext4")))
1235 return "norecovery";
1236 if (!strncmp(fstype, "xfs", strlen("xfs")))
1237 return "norecovery";
1242 static inline const char *get_device_path(struct discover_device *dev)
1244 return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1247 static bool check_existing_mount(struct discover_device *dev)
1249 struct stat devstat, mntstat;
1250 const char *device_path;
1255 device_path = get_device_path(dev);
1257 rc = stat(device_path, &devstat);
1259 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1263 if (!S_ISBLK(devstat.st_mode)) {
1264 pb_debug("%s: %s isn't a block device?\n", __func__,
1269 fp = fopen("/proc/self/mounts", "r");
1272 mnt = getmntent(fp);
1276 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1279 rc = stat(mnt->mnt_fsname, &mntstat);
1283 if (!S_ISBLK(mntstat.st_mode))
1286 if (mntstat.st_rdev == devstat.st_rdev) {
1287 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1288 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1289 dev->mounted = true;
1290 dev->unmount = false;
1292 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1293 __func__, dev->device_path,
1294 dev->mounted_rw ? 'w' : 'o',
1305 static int mount_device(struct discover_device *dev)
1307 const char *fstype, *device_path;
1310 if (!dev->device_path)
1316 if (check_existing_mount(dev))
1319 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1323 /* ext3 treats the norecovery option as an error, so mount the device
1324 * as an ext4 filesystem instead */
1325 if (!strncmp(fstype, "ext3", strlen("ext3"))) {
1326 pb_debug("Mounting ext3 filesystem as ext4\n");
1327 fstype = talloc_asprintf(dev, "ext4");
1330 dev->mount_path = join_paths(dev, mount_base(),
1333 if (pb_mkdir_recursive(dev->mount_path)) {
1334 pb_log("couldn't create mount directory %s: %s\n",
1335 dev->mount_path, strerror(errno));
1339 device_path = get_device_path(dev);
1341 pb_log("mounting device %s read-only\n", dev->device_path);
1343 rc = mount(device_path, dev->mount_path, fstype,
1344 MS_RDONLY | MS_SILENT,
1345 fs_parameters(MS_RDONLY, fstype));
1347 dev->mounted = true;
1348 dev->mounted_rw = false;
1349 dev->unmount = true;
1353 pb_log("couldn't mount device %s: mount failed: %s\n",
1354 device_path, strerror(errno));
1356 /* If mount fails clean up any snapshot */
1357 devmapper_destroy_snapshot(dev);
1359 pb_rmdir_recursive(mount_base(), dev->mount_path);
1361 talloc_free(dev->mount_path);
1362 dev->mount_path = NULL;
1366 static int umount_device(struct discover_device *dev)
1368 const char *device_path;
1371 if (!dev->mounted || !dev->unmount)
1374 device_path = get_device_path(dev);
1376 pb_log("unmounting device %s\n", device_path);
1377 rc = umount(dev->mount_path);
1381 dev->mounted = false;
1382 devmapper_destroy_snapshot(dev);
1384 pb_rmdir_recursive(mount_base(), dev->mount_path);
1386 talloc_free(dev->mount_path);
1387 dev->mount_path = NULL;
1392 int device_request_write(struct discover_device *dev, bool *release)
1394 const char *fstype, *device_path;
1395 const struct config *config;
1400 config = config_get();
1401 if (!config->allow_writes)
1407 if (dev->mounted_rw)
1410 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1412 device_path = get_device_path(dev);
1414 pb_log("remounting device %s read-write\n", device_path);
1416 rc = umount(dev->mount_path);
1418 pb_log("Failed to unmount %s: %s\n",
1419 dev->mount_path, strerror(errno));
1423 rc = mount(device_path, dev->mount_path, fstype,
1425 fs_parameters(MS_REMOUNT, fstype));
1429 dev->mounted_rw = true;
1434 pb_log("Unable to remount device %s read-write: %s\n",
1435 device_path, strerror(errno));
1436 if (mount(device_path, dev->mount_path, fstype,
1437 MS_RDONLY | MS_SILENT,
1438 fs_parameters(MS_RDONLY, fstype)))
1439 pb_log("Unable to recover mount for %s: %s\n",
1440 device_path, strerror(errno));
1444 void device_release_write(struct discover_device *dev, bool release)
1446 const char *fstype, *device_path;
1452 device_path = get_device_path(dev);
1454 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1456 pb_log("remounting device %s read-only\n", device_path);
1458 if (umount(dev->mount_path)) {
1459 pb_log("Failed to unmount %s\n", dev->mount_path);
1462 dev->mounted_rw = dev->mounted = false;
1465 devmapper_merge_snapshot(dev);
1466 /* device_path becomes stale after merge */
1467 device_path = get_device_path(dev);
1470 mount(device_path, dev->mount_path, fstype,
1471 MS_RDONLY | MS_SILENT,
1472 fs_parameters(MS_RDONLY, fstype));
1474 pb_log("Failed to remount %s read-only: %s\n",
1475 device_path, strerror(errno));
1477 dev->mounted = true;
1482 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1486 static int device_handler_init_sources(
1487 struct device_handler *handler __attribute__((unused)))
1492 static void device_handler_reinit_sources(
1493 struct device_handler *handler __attribute__((unused)))
1497 static int umount_device(struct discover_device *dev __attribute__((unused)))
1502 static int __attribute__((unused)) mount_device(
1503 struct discover_device *dev __attribute__((unused)))
1508 int device_request_write(struct discover_device *dev __attribute__((unused)),
1515 void device_release_write(struct discover_device *dev __attribute__((unused)),
1516 bool release __attribute__((unused)))