]> git.ozlabs.org Git - petitboot/blob - discover/device-handler.c
discover: Pass UUID to discover_device_create()
[petitboot] / discover / device-handler.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <mntent.h>
8 #include <locale.h>
9 #include <sys/stat.h>
10 #include <sys/wait.h>
11 #include <sys/mount.h>
12
13 #include <talloc/talloc.h>
14 #include <list/list.h>
15 #include <log/log.h>
16 #include <types/types.h>
17 #include <system/system.h>
18 #include <process/process.h>
19 #include <url/url.h>
20 #include <i18n/i18n.h>
21
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netdb.h>
25 #include <arpa/inet.h>
26
27 #include "device-handler.h"
28 #include "discover-server.h"
29 #include "devmapper.h"
30 #include "user-event.h"
31 #include "platform.h"
32 #include "event.h"
33 #include "parser.h"
34 #include "resource.h"
35 #include "paths.h"
36 #include "sysinfo.h"
37 #include "boot.h"
38 #include "udev.h"
39 #include "network.h"
40 #include "ipmi.h"
41
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,
47 };
48
49 struct device_handler {
50         struct discover_server  *server;
51         int                     dry_run;
52
53         struct pb_udev          *udev;
54         struct network          *network;
55         struct user_event       *user_event;
56
57         struct discover_device  **devices;
58         unsigned int            n_devices;
59
60         struct ramdisk_device   **ramdisks;
61         unsigned int            n_ramdisks;
62
63         struct waitset          *waitset;
64         struct waiter           *timeout_waiter;
65         bool                    autoboot_enabled;
66         unsigned int            sec_to_boot;
67
68         struct discover_boot_option *default_boot_option;
69         int                     default_boot_option_priority;
70
71         struct list             unresolved_boot_options;
72
73         struct boot_task        *pending_boot;
74         bool                    pending_boot_is_default;
75 };
76
77 static int mount_device(struct discover_device *dev);
78 static int umount_device(struct discover_device *dev);
79
80 static int device_handler_init_sources(struct device_handler *handler);
81 static void device_handler_reinit_sources(struct device_handler *handler);
82
83 static void device_handler_update_lang(const char *lang);
84
85 void discover_context_add_boot_option(struct discover_context *ctx,
86                 struct discover_boot_option *boot_option)
87 {
88         boot_option->source = ctx->parser;
89         list_add_tail(&ctx->boot_options, &boot_option->list);
90         talloc_steal(ctx, boot_option);
91 }
92
93 /**
94  * device_handler_get_device_count - Get the count of current handler devices.
95  */
96
97 int device_handler_get_device_count(const struct device_handler *handler)
98 {
99         return handler->n_devices;
100 }
101
102 /**
103  * device_handler_get_device - Get a handler device by index.
104  */
105
106 const struct discover_device *device_handler_get_device(
107         const struct device_handler *handler, unsigned int index)
108 {
109         if (index >= handler->n_devices) {
110                 assert(0 && "bad index");
111                 return NULL;
112         }
113
114         return handler->devices[index];
115 }
116
117 struct discover_boot_option *discover_boot_option_create(
118                 struct discover_context *ctx,
119                 struct discover_device *device)
120 {
121         struct discover_boot_option *opt;
122
123         opt = talloc_zero(ctx, struct discover_boot_option);
124         opt->option = talloc_zero(opt, struct boot_option);
125         opt->device = device;
126
127         return opt;
128 }
129
130 static int device_match_uuid(struct discover_device *dev, const char *uuid)
131 {
132         return dev->uuid && !strcmp(dev->uuid, uuid);
133 }
134
135 static int device_match_label(struct discover_device *dev, const char *label)
136 {
137         return dev->label && !strcmp(dev->label, label);
138 }
139
140 static int device_match_id(struct discover_device *dev, const char *id)
141 {
142         return !strcmp(dev->device->id, id);
143 }
144
145 static int device_match_serial(struct discover_device *dev, const char *serial)
146 {
147         const char *val = discover_device_get_param(dev, "ID_SERIAL");
148         return val && !strcmp(val, serial);
149 }
150
151 static struct discover_device *device_lookup(
152                 struct device_handler *device_handler,
153                 int (match_fn)(struct discover_device *, const char *),
154                 const char *str)
155 {
156         struct discover_device *dev;
157         unsigned int i;
158
159         if (!str)
160                 return NULL;
161
162         for (i = 0; i < device_handler->n_devices; i++) {
163                 dev = device_handler->devices[i];
164
165                 if (match_fn(dev, str))
166                         return dev;
167         }
168
169         return NULL;
170 }
171
172 struct discover_device *device_lookup_by_name(struct device_handler *handler,
173                 const char *name)
174 {
175         if (!strncmp(name, "/dev/", strlen("/dev/")))
176                 name += strlen("/dev/");
177
178         return device_lookup_by_id(handler, name);
179 }
180
181 struct discover_device *device_lookup_by_uuid(
182                 struct device_handler *device_handler,
183                 const char *uuid)
184 {
185         return device_lookup(device_handler, device_match_uuid, uuid);
186 }
187
188 struct discover_device *device_lookup_by_label(
189                 struct device_handler *device_handler,
190                 const char *label)
191 {
192         return device_lookup(device_handler, device_match_label, label);
193 }
194
195 struct discover_device *device_lookup_by_id(
196                 struct device_handler *device_handler,
197                 const char *id)
198 {
199         return device_lookup(device_handler, device_match_id, id);
200 }
201
202 struct discover_device *device_lookup_by_serial(
203                 struct device_handler *device_handler,
204                 const char *serial)
205 {
206         return device_lookup(device_handler, device_match_serial, serial);
207 }
208
209 void device_handler_destroy(struct device_handler *handler)
210 {
211         talloc_free(handler);
212 }
213
214 static int destroy_device(void *arg)
215 {
216         struct discover_device *dev = arg;
217
218         umount_device(dev);
219
220         return 0;
221 }
222
223 struct discover_device *discover_device_create(struct device_handler *handler,
224                 const char *uuid, const char *id)
225 {
226         struct discover_device *dev;
227
228         if (uuid)
229                 dev = device_lookup_by_uuid(handler, uuid);
230         else
231                 dev = device_lookup_by_id(handler, id);
232
233         if (dev)
234                 return dev;
235
236         dev = talloc_zero(handler, struct discover_device);
237         dev->device = talloc_zero(dev, struct device);
238         dev->device->id = talloc_strdup(dev->device, id);
239         dev->uuid = talloc_strdup(dev, uuid);
240         list_init(&dev->params);
241         list_init(&dev->boot_options);
242
243         talloc_set_destructor(dev, destroy_device);
244
245         return dev;
246 }
247
248 struct discover_device_param {
249         char                    *name;
250         char                    *value;
251         struct list_item        list;
252 };
253
254 void discover_device_set_param(struct discover_device *device,
255                 const char *name, const char *value)
256 {
257         struct discover_device_param *param;
258         bool found = false;
259
260         list_for_each_entry(&device->params, param, list) {
261                 if (!strcmp(param->name, name)) {
262                         found = true;
263                         break;
264                 }
265         }
266
267         if (!found) {
268                 if (!value)
269                         return;
270                 param = talloc(device, struct discover_device_param);
271                 param->name = talloc_strdup(param, name);
272                 list_add(&device->params, &param->list);
273         } else {
274                 if (!value) {
275                         list_remove(&param->list);
276                         talloc_free(param);
277                         return;
278                 }
279                 talloc_free(param->value);
280         }
281
282         param->value = talloc_strdup(param, value);
283 }
284
285 const char *discover_device_get_param(struct discover_device *device,
286                 const char *name)
287 {
288         struct discover_device_param *param;
289
290         list_for_each_entry(&device->params, param, list) {
291                 if (!strcmp(param->name, name))
292                         return param->value;
293         }
294         return NULL;
295 }
296
297 struct device_handler *device_handler_init(struct discover_server *server,
298                 struct waitset *waitset, int dry_run)
299 {
300         struct device_handler *handler;
301         int rc;
302
303         handler = talloc_zero(NULL, struct device_handler);
304         handler->server = server;
305         handler->waitset = waitset;
306         handler->dry_run = dry_run;
307         handler->autoboot_enabled = config_get()->autoboot_enabled;
308
309         list_init(&handler->unresolved_boot_options);
310
311         /* set up our mount point base */
312         pb_mkdir_recursive(mount_base());
313
314         parser_init();
315
316         if (config_get()->safe_mode)
317                 return handler;
318
319         rc = device_handler_init_sources(handler);
320         if (rc) {
321                 talloc_free(handler);
322                 return NULL;
323         }
324
325         return handler;
326 }
327
328 void device_handler_reinit(struct device_handler *handler)
329 {
330         struct discover_boot_option *opt, *tmp;
331         struct ramdisk_device *ramdisk;
332         unsigned int i;
333
334         device_handler_cancel_default(handler);
335
336         /* free unresolved boot options */
337         list_for_each_entry_safe(&handler->unresolved_boot_options,
338                         opt, tmp, list)
339                 talloc_free(opt);
340         list_init(&handler->unresolved_boot_options);
341
342         /* drop all devices */
343         for (i = 0; i < handler->n_devices; i++) {
344                 discover_server_notify_device_remove(handler->server,
345                                 handler->devices[i]->device);
346                 ramdisk = handler->devices[i]->ramdisk;
347                 talloc_free(handler->devices[i]);
348                 talloc_free(ramdisk);
349         }
350
351         talloc_free(handler->devices);
352         handler->devices = NULL;
353         handler->n_devices = 0;
354         talloc_free(handler->ramdisks);
355         handler->ramdisks = NULL;
356         handler->n_ramdisks = 0;
357
358         device_handler_reinit_sources(handler);
359 }
360
361 void device_handler_remove(struct device_handler *handler,
362                 struct discover_device *device)
363 {
364         struct discover_boot_option *opt, *tmp;
365         unsigned int i;
366
367         list_for_each_entry_safe(&device->boot_options, opt, tmp, list) {
368                 if (opt == handler->default_boot_option) {
369                         pb_log("Default option %s cancelled since device removed",
370                                         opt->option->name);
371                         device_handler_cancel_default(handler);
372                         break;
373                 }
374         }
375
376         for (i = 0; i < handler->n_devices; i++)
377                 if (handler->devices[i] == device)
378                         break;
379
380         if (i == handler->n_devices) {
381                 talloc_free(device);
382                 return;
383         }
384
385         /* Free any unresolved options, as they're currently allocated
386          * against the handler */
387         list_for_each_entry_safe(&handler->unresolved_boot_options,
388                         opt, tmp, list) {
389                 if (opt->device != device)
390                         continue;
391                 list_remove(&opt->list);
392                 talloc_free(opt);
393         }
394
395         /* if this is a network device, we have to unregister it from the
396          * network code */
397         if (device->device->type == DEVICE_TYPE_NETWORK)
398                 network_unregister_device(handler->network, device);
399
400         handler->n_devices--;
401         memmove(&handler->devices[i], &handler->devices[i + 1],
402                 (handler->n_devices - i) * sizeof(handler->devices[0]));
403         handler->devices = talloc_realloc(handler, handler->devices,
404                 struct discover_device *, handler->n_devices);
405
406         if (device->notified)
407                 discover_server_notify_device_remove(handler->server,
408                                                         device->device);
409
410         talloc_free(device);
411 }
412
413 void device_handler_boot_status(void *arg, struct boot_status *status)
414 {
415         struct device_handler *handler = arg;
416
417         discover_server_notify_boot_status(handler->server, status);
418 }
419
420 static void countdown_status(struct device_handler *handler,
421                 struct discover_boot_option *opt, unsigned int sec)
422 {
423         struct boot_status status;
424
425         status.type = BOOT_STATUS_INFO;
426         status.progress = -1;
427         status.detail = NULL;
428         status.message = talloc_asprintf(handler,
429                         _("Booting in %d sec: %s"), sec, opt->option->name);
430
431         discover_server_notify_boot_status(handler->server, &status);
432
433         talloc_free(status.message);
434 }
435
436 static int default_timeout(void *arg)
437 {
438         struct device_handler *handler = arg;
439         struct discover_boot_option *opt;
440
441         if (!handler->default_boot_option)
442                 return 0;
443
444         if (handler->pending_boot)
445                 return 0;
446
447         opt = handler->default_boot_option;
448
449         if (handler->sec_to_boot) {
450                 countdown_status(handler, opt, handler->sec_to_boot);
451                 handler->sec_to_boot--;
452                 handler->timeout_waiter = waiter_register_timeout(
453                                                 handler->waitset, 1000,
454                                                 default_timeout, handler);
455                 return 0;
456         }
457
458         handler->timeout_waiter = NULL;
459
460         pb_log("Timeout expired, booting default option %s\n", opt->option->id);
461
462         platform_pre_boot();
463
464         handler->pending_boot = boot(handler, handler->default_boot_option,
465                         NULL, handler->dry_run, device_handler_boot_status,
466                         handler);
467         handler->pending_boot_is_default = true;
468         return 0;
469 }
470
471 struct {
472         enum ipmi_bootdev       ipmi_type;
473         enum device_type        device_type;
474 } device_type_map[] = {
475         { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
476         { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
477         { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
478         { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
479 };
480
481 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
482                 enum device_type device_type)
483 {
484         unsigned int i;
485
486         for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
487                 if (device_type_map[i].device_type == device_type)
488                         return device_type_map[i].ipmi_type == ipmi_type;
489         }
490
491         return false;
492 }
493
494 static int autoboot_option_priority(const struct config *config,
495                                 struct discover_boot_option *opt)
496 {
497         enum device_type type = opt->device->device->type;
498         const char *uuid = opt->device->uuid;
499         struct autoboot_option *auto_opt;
500         unsigned int i;
501
502         for (i = 0; i < config->n_autoboot_opts; i++) {
503                 auto_opt = &config->autoboot_opts[i];
504                 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
505                         if (!strcmp(auto_opt->uuid, uuid))
506                                 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
507
508                 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
509                         if (auto_opt->type == type ||
510                             auto_opt->type == DEVICE_TYPE_ANY)
511                                 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
512         }
513
514         return -1;
515 }
516
517 /*
518  * We have different priorities to resolve conflicts between boot options that
519  * report to be the default for their device. This function assigns a priority
520  * for these options.
521  */
522 static enum default_priority default_option_priority(
523                 struct discover_boot_option *opt)
524 {
525         const struct config *config;
526
527         config = config_get();
528
529         /* We give highest priority to IPMI-configured boot options. If
530          * we have an IPMI bootdev configuration set, then we don't allow
531          * any other defaults */
532         if (config->ipmi_bootdev) {
533                 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
534                                 opt->device->device->type);
535                 if (ipmi_match)
536                         return DEFAULT_PRIORITY_REMOTE;
537
538                 pb_debug("handler: disabled default priority due to "
539                                 "non-matching IPMI type %x\n",
540                                 config->ipmi_bootdev);
541                 return DEFAULT_PRIORITY_DISABLED;
542         }
543
544         /* Next, try to match the option against the user-defined autoboot
545          * options, either by device UUID or type. */
546         if (config->n_autoboot_opts) {
547                 int boot_match = autoboot_option_priority(config, opt);
548                 if (boot_match > 0)
549                         return boot_match;
550         }
551
552         /* If the option didn't match any entry in the array, it is disabled */
553         pb_debug("handler: disabled default priority due to "
554                         "non-matching UUID or type\n");
555         return DEFAULT_PRIORITY_DISABLED;
556 }
557
558 static void set_default(struct device_handler *handler,
559                 struct discover_boot_option *opt)
560 {
561         enum default_priority cur_prio, new_prio;
562
563         if (!handler->autoboot_enabled)
564                 return;
565
566         pb_debug("handler: new default option: %s\n", opt->option->id);
567
568         new_prio = default_option_priority(opt);
569
570         /* Anything outside our range prevents a default boot */
571         if (new_prio >= DEFAULT_PRIORITY_DISABLED)
572                 return;
573
574         pb_debug("handler: calculated priority %d\n", new_prio);
575
576         /* Resolve any conflicts: if we have a new default option, it only
577          * replaces the current if it has a higher priority. */
578         if (handler->default_boot_option) {
579
580                 cur_prio = handler->default_boot_option_priority;
581
582                 if (new_prio < cur_prio) {
583                         pb_log("handler: new prio %d beats "
584                                         "old prio %d for %s\n",
585                                         new_prio, cur_prio,
586                                         handler->default_boot_option
587                                                 ->option->id);
588                         handler->default_boot_option = opt;
589                         handler->default_boot_option_priority = new_prio;
590                         /* extend the timeout a little, so the user sees some
591                          * indication of the change */
592                         handler->sec_to_boot += 2;
593                 }
594
595                 return;
596         }
597
598         handler->sec_to_boot = config_get()->autoboot_timeout_sec;
599         handler->default_boot_option = opt;
600         handler->default_boot_option_priority = new_prio;
601
602         pb_log("handler: boot option %s set as default, timeout %u sec.\n",
603                opt->option->id, handler->sec_to_boot);
604
605         default_timeout(handler);
606 }
607
608 static bool resource_is_resolved(struct resource *res)
609 {
610         return !res || res->resolved;
611 }
612
613 /* We only use this in an assert, which will disappear if we're compiling
614  * with NDEBUG, so we need the 'used' attribute for these builds */
615 static bool __attribute__((used)) boot_option_is_resolved(
616                 struct discover_boot_option *opt)
617 {
618         return resource_is_resolved(opt->boot_image) &&
619                 resource_is_resolved(opt->initrd) &&
620                 resource_is_resolved(opt->dtb) &&
621                 resource_is_resolved(opt->args_sig_file) &&
622                 resource_is_resolved(opt->icon);
623 }
624
625 static bool resource_resolve(struct resource *res, const char *name,
626                 struct discover_boot_option *opt,
627                 struct device_handler *handler)
628 {
629         struct parser *parser = opt->source;
630
631         if (resource_is_resolved(res))
632                 return true;
633
634         pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
635                         opt->option->id, name, parser->name);
636         parser->resolve_resource(handler, res);
637
638         return res->resolved;
639 }
640
641 static bool boot_option_resolve(struct discover_boot_option *opt,
642                 struct device_handler *handler)
643 {
644         return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
645                 resource_resolve(opt->initrd, "initrd", opt, handler) &&
646                 resource_resolve(opt->dtb, "dtb", opt, handler) &&
647                 resource_resolve(opt->args_sig_file, "args_sig_file", opt,
648                         handler) &&
649                 resource_resolve(opt->icon, "icon", opt, handler);
650 }
651
652 static void boot_option_finalise(struct device_handler *handler,
653                 struct discover_boot_option *opt)
654 {
655         assert(boot_option_is_resolved(opt));
656
657         /* check that the parsers haven't set any of the final data */
658         assert(!opt->option->boot_image_file);
659         assert(!opt->option->initrd_file);
660         assert(!opt->option->dtb_file);
661         assert(!opt->option->icon_file);
662         assert(!opt->option->device_id);
663         assert(!opt->option->args_sig_file);
664
665         if (opt->boot_image)
666                 opt->option->boot_image_file = opt->boot_image->url->full;
667         if (opt->initrd)
668                 opt->option->initrd_file = opt->initrd->url->full;
669         if (opt->dtb)
670                 opt->option->dtb_file = opt->dtb->url->full;
671         if (opt->icon)
672                 opt->option->icon_file = opt->icon->url->full;
673         if (opt->args_sig_file)
674                 opt->option->args_sig_file = opt->args_sig_file->url->full;
675
676         opt->option->device_id = opt->device->device->id;
677
678         if (opt->option->is_default)
679                 set_default(handler, opt);
680 }
681
682 static void notify_boot_option(struct device_handler *handler,
683                 struct discover_boot_option *opt)
684 {
685         struct discover_device *dev = opt->device;
686
687         if (!dev->notified)
688                 discover_server_notify_device_add(handler->server,
689                                                   opt->device->device);
690         dev->notified = true;
691         discover_server_notify_boot_option_add(handler->server, opt->option);
692 }
693
694 static void process_boot_option_queue(struct device_handler *handler)
695 {
696         struct discover_boot_option *opt, *tmp;
697
698         list_for_each_entry_safe(&handler->unresolved_boot_options,
699                         opt, tmp, list) {
700
701                 pb_debug("queue: attempting resolution for %s\n",
702                                 opt->option->id);
703
704                 if (!boot_option_resolve(opt, handler))
705                         continue;
706
707                 pb_debug("\tresolved!\n");
708
709                 list_remove(&opt->list);
710                 list_add_tail(&opt->device->boot_options, &opt->list);
711                 talloc_steal(opt->device, opt);
712                 boot_option_finalise(handler, opt);
713                 notify_boot_option(handler, opt);
714         }
715 }
716
717 struct discover_context *device_handler_discover_context_create(
718                 struct device_handler *handler,
719                 struct discover_device *device)
720 {
721         struct discover_context *ctx;
722
723         ctx = talloc_zero(handler, struct discover_context);
724         ctx->device = device;
725         ctx->network = handler->network;
726         list_init(&ctx->boot_options);
727
728         return ctx;
729 }
730
731 void device_handler_add_device(struct device_handler *handler,
732                 struct discover_device *device)
733 {
734         handler->n_devices++;
735         handler->devices = talloc_realloc(handler, handler->devices,
736                                 struct discover_device *, handler->n_devices);
737         handler->devices[handler->n_devices - 1] = device;
738
739         if (device->device->type == DEVICE_TYPE_NETWORK)
740                 network_register_device(handler->network, device);
741 }
742
743 void device_handler_add_ramdisk(struct device_handler *handler,
744                 const char *path)
745 {
746         struct ramdisk_device *dev;
747         unsigned int i;
748
749         if (!path)
750                 return;
751
752         for (i = 0; i < handler->n_ramdisks; i++)
753                 if (!strcmp(handler->ramdisks[i]->path, path))
754                         return;
755
756         dev = talloc_zero(handler, struct ramdisk_device);
757         if (!dev) {
758                 pb_log("Failed to allocate memory to track %s\n", path);
759                 return;
760         }
761
762         dev->path = talloc_strdup(handler, path);
763
764         handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
765                                 struct ramdisk_device *,
766                                 handler->n_ramdisks + 1);
767         if (!handler->ramdisks) {
768                 pb_log("Failed to reallocate memory"
769                        "- ramdisk tracking inconsistent!\n");
770                 return;
771         }
772
773         handler->ramdisks[i] = dev;
774         i = handler->n_ramdisks++;
775 }
776
777 struct ramdisk_device *device_handler_get_ramdisk(
778                 struct device_handler *handler)
779 {
780         unsigned int i;
781         char *name;
782         dev_t id;
783
784         /* Check if free ramdisk exists */
785         for (i = 0; i < handler->n_ramdisks; i++)
786                 if (!handler->ramdisks[i]->snapshot &&
787                     !handler->ramdisks[i]->origin &&
788                     !handler->ramdisks[i]->base)
789                         return handler->ramdisks[i];
790
791         /* Otherwise create a new one */
792         name = talloc_asprintf(handler, "/dev/ram%d",
793                         handler->n_ramdisks);
794         if (!name) {
795                 pb_debug("Failed to allocate memory to name /dev/ram%d",
796                         handler->n_ramdisks);
797                 return NULL;
798         }
799
800         id = makedev(1, handler->n_ramdisks);
801         if (mknod(name, S_IFBLK, id)) {
802                 if (errno == EEXIST) {
803                         /* We haven't yet received updates for existing
804                          * ramdisks - add and use this one */
805                         pb_debug("Using untracked ramdisk %s\n", name);
806                 } else {
807                         pb_log("Failed to create new ramdisk %s: %s\n",
808                                name, strerror(errno));
809                         return NULL;
810                 }
811         }
812         device_handler_add_ramdisk(handler, name);
813         talloc_free(name);
814
815         return handler->ramdisks[i];
816 }
817
818 void device_handler_release_ramdisk(struct discover_device *device)
819 {
820         struct ramdisk_device *ramdisk = device->ramdisk;
821
822         talloc_free(ramdisk->snapshot);
823         talloc_free(ramdisk->origin);
824         talloc_free(ramdisk->base);
825
826         ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
827         ramdisk->sectors = 0;
828
829         device->ramdisk = NULL;
830 }
831
832 /* Start discovery on a hotplugged device. The device will be in our devices
833  * array, but has only just been initialised by the hotplug source.
834  */
835 int device_handler_discover(struct device_handler *handler,
836                 struct discover_device *dev)
837 {
838         struct discover_context *ctx;
839         struct boot_status *status;
840         int rc;
841
842         status = talloc_zero(handler, struct boot_status);
843         status->type = BOOT_STATUS_INFO;
844         /*
845          * TRANSLATORS: this string will be passed the type and identifier
846          * of the device. For example, the first parameter could be "Disk",
847          * (which will be translated accordingly) and the second a Linux device
848          * identifier like 'sda1' (which will not be translated)
849          */
850         status->message = talloc_asprintf(status, _("Processing %s device %s"),
851                                 device_type_display_name(dev->device->type),
852                                 dev->device->id);
853         device_handler_boot_status(handler, status);
854
855         process_boot_option_queue(handler);
856
857         /* create our context */
858         ctx = device_handler_discover_context_create(handler, dev);
859
860         rc = mount_device(dev);
861         if (rc)
862                 goto out;
863
864         /* add this device to our system info */
865         system_info_register_blockdev(dev->device->id, dev->uuid,
866                         dev->mount_path);
867
868         /* run the parsers. This will populate the ctx's boot_option list. */
869         iterate_parsers(ctx);
870
871         /* add discovered stuff to the handler */
872         device_handler_discover_context_commit(handler, ctx);
873
874 out:
875         /*
876          * TRANSLATORS: the format specifier in this string is a Linux
877          * device identifier, like 'sda1'
878          */
879         status->message = talloc_asprintf(status,_("Processing %s complete"),
880                                 dev->device->id);
881         device_handler_boot_status(handler, status);
882
883         talloc_free(status);
884         talloc_unlink(handler, ctx);
885
886         return 0;
887 }
888
889 /* Incoming dhcp event */
890 int device_handler_dhcp(struct device_handler *handler,
891                 struct discover_device *dev, struct event *event)
892 {
893         struct discover_context *ctx;
894         struct boot_status *status;
895
896         status = talloc_zero(handler, struct boot_status);
897         status->type = BOOT_STATUS_INFO;
898         /*
899          * TRANSLATORS: this format specifier will be the name of a network
900          * device, like 'eth0'.
901          */
902         status->message = talloc_asprintf(status, _("Processing dhcp event on %s"),
903                                 dev->device->id);
904         device_handler_boot_status(handler, status);
905
906         /* create our context */
907         ctx = device_handler_discover_context_create(handler, dev);
908         talloc_steal(ctx, event);
909         ctx->event = event;
910
911         iterate_parsers(ctx);
912
913         device_handler_discover_context_commit(handler, ctx);
914
915         /*
916          * TRANSLATORS: this format specifier will be the name of a network
917          * device, like 'eth0'.
918          */
919         status->message = talloc_asprintf(status,_("Processing %s complete"),
920                                 dev->device->id);
921         device_handler_boot_status(handler, status);
922
923         talloc_free(status);
924         talloc_unlink(handler, ctx);
925
926         return 0;
927 }
928
929 static struct discover_boot_option *find_boot_option_by_id(
930                 struct device_handler *handler, const char *id)
931 {
932         unsigned int i;
933
934         for (i = 0; i < handler->n_devices; i++) {
935                 struct discover_device *dev = handler->devices[i];
936                 struct discover_boot_option *opt;
937
938                 list_for_each_entry(&dev->boot_options, opt, list)
939                         if (!strcmp(opt->option->id, id))
940                                 return opt;
941         }
942
943         return NULL;
944 }
945
946 void device_handler_boot(struct device_handler *handler,
947                 struct boot_command *cmd)
948 {
949         struct discover_boot_option *opt = NULL;
950
951         if (cmd->option_id && strlen(cmd->option_id))
952                 opt = find_boot_option_by_id(handler, cmd->option_id);
953
954         if (handler->pending_boot)
955                 boot_cancel(handler->pending_boot);
956
957         platform_pre_boot();
958
959         handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
960                         device_handler_boot_status, handler);
961         handler->pending_boot_is_default = false;
962 }
963
964 void device_handler_cancel_default(struct device_handler *handler)
965 {
966         struct boot_status status;
967
968         if (handler->timeout_waiter)
969                 waiter_remove(handler->timeout_waiter);
970
971         handler->timeout_waiter = NULL;
972         handler->autoboot_enabled = false;
973
974         /* we only send status if we had a default boot option queued */
975         if (!handler->default_boot_option)
976                 return;
977
978         pb_log("Cancelling default boot option\n");
979
980         if (handler->pending_boot && handler->pending_boot_is_default) {
981                 boot_cancel(handler->pending_boot);
982                 handler->pending_boot = NULL;
983                 handler->pending_boot_is_default = false;
984         }
985
986         handler->default_boot_option = NULL;
987
988         status.type = BOOT_STATUS_INFO;
989         status.progress = -1;
990         status.detail = NULL;
991         status.message = _("Default boot cancelled");
992
993         discover_server_notify_boot_status(handler->server, &status);
994 }
995
996 void device_handler_update_config(struct device_handler *handler,
997                 struct config *config)
998 {
999         int rc;
1000
1001         rc = config_set(config);
1002         if (rc)
1003                 return;
1004
1005         discover_server_notify_config(handler->server, config);
1006         device_handler_update_lang(config->lang);
1007         device_handler_reinit(handler);
1008 }
1009
1010 static char *device_from_addr(void *ctx, struct pb_url *url)
1011 {
1012         char *ipaddr, *buf, *tok, *dev = NULL;
1013         const char *delim = " ";
1014         struct sockaddr_in *ip;
1015         struct sockaddr_in si;
1016         struct addrinfo *res;
1017         struct process *p;
1018         int rc;
1019
1020         /* Note: IPv4 only */
1021         rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1022         if (rc > 0) {
1023                 ipaddr = url->host;
1024         } else {
1025                 /* need to turn hostname into a valid IP */
1026                 rc = getaddrinfo(url->host, NULL, NULL, &res);
1027                 if (rc) {
1028                         pb_debug("%s: Invalid URL\n",__func__);
1029                         return NULL;
1030                 }
1031                 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1032                 ip = (struct sockaddr_in *) res->ai_addr;
1033                 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1034                 freeaddrinfo(res);
1035         }
1036
1037         const char *argv[] = {
1038                 pb_system_apps.ip,
1039                 "route", "show", "to", "match",
1040                 ipaddr,
1041                 NULL
1042         };
1043
1044         p = process_create(ctx);
1045
1046         p->path = pb_system_apps.ip;
1047         p->argv = argv;
1048         p->keep_stdout = true;
1049
1050         rc = process_run_sync(p);
1051
1052         if (rc || p->exit_status) {
1053                 /* ip has complained for some reason; most likely
1054                  * there is no route to the host - bail out */
1055                 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1056                 pb_debug("ip buf: %s\n", p->stdout_buf);
1057                 process_release(p);
1058                 return NULL;
1059         }
1060
1061         buf = p->stdout_buf;
1062         /* If a route is found, ip-route output will be of the form
1063          * "... dev DEVNAME ... " */
1064         tok = strtok(buf, delim);
1065         while (tok) {
1066                 if (!strcmp(tok, "dev")) {
1067                         tok = strtok(NULL, delim);
1068                         dev = talloc_strdup(ctx, tok);
1069                         break;
1070                 }
1071                 tok = strtok(NULL, delim);
1072         }
1073
1074         process_release(p);
1075         if (dev)
1076                 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1077         return dev;
1078 }
1079
1080 void device_handler_process_url(struct device_handler *handler,
1081                 const char *url, const char *mac, const char *ip)
1082 {
1083         struct discover_context *ctx;
1084         struct discover_device *dev;
1085         struct boot_status *status;
1086         struct pb_url *pb_url;
1087         struct event *event;
1088         struct param *param;
1089
1090         status = talloc(handler, struct boot_status);
1091
1092         status->type = BOOT_STATUS_ERROR;
1093         status->progress = 0;
1094         status->detail = talloc_asprintf(status,
1095                         _("Received config URL %s"), url);
1096
1097         if (!handler->network) {
1098                 status->message = talloc_asprintf(handler,
1099                                         _("No network configured"));
1100                 goto msg;
1101         }
1102
1103         event = talloc(handler, struct event);
1104         event->type = EVENT_TYPE_USER;
1105         event->action = EVENT_ACTION_URL;
1106
1107         if (url[strlen(url) - 1] == '/') {
1108                 event->params = talloc_array(event, struct param, 3);
1109                 param = &event->params[0];
1110                 param->name = talloc_strdup(event, "pxepathprefix");
1111                 param->value = talloc_strdup(event, url);
1112                 param = &event->params[1];
1113                 param->name = talloc_strdup(event, "mac");
1114                 param->value = talloc_strdup(event, mac);
1115                 param = &event->params[2];
1116                 param->name = talloc_strdup(event, "ip");
1117                 param->value = talloc_strdup(event, ip);
1118                 event->n_params = 3;
1119         } else {
1120                 event->params = talloc_array(event, struct param, 1);
1121                 param = &event->params[0];
1122                 param->name = talloc_strdup(event, "pxeconffile");
1123                 param->value = talloc_strdup(event, url);
1124                 event->n_params = 1;
1125         }
1126
1127         pb_url = pb_url_parse(event, event->params->value);
1128         if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1129                 status->message = talloc_asprintf(handler,
1130                                         _("Invalid config URL!"));
1131                 goto msg;
1132         }
1133
1134         if (pb_url->scheme == pb_url_file)
1135                 event->device = talloc_asprintf(event, "local");
1136         else
1137                 event->device = device_from_addr(event, pb_url);
1138
1139         if (!event->device) {
1140                 status->message = talloc_asprintf(status,
1141                                         _("Unable to route to host %s"),
1142                                         pb_url->host);
1143                 goto msg;
1144         }
1145
1146         dev = discover_device_create(handler, mac, event->device);
1147         if (pb_url->scheme == pb_url_file)
1148                 dev->device->type = DEVICE_TYPE_ANY;
1149         ctx = device_handler_discover_context_create(handler, dev);
1150         talloc_steal(ctx, event);
1151         ctx->event = event;
1152
1153         iterate_parsers(ctx);
1154
1155         device_handler_discover_context_commit(handler, ctx);
1156
1157         talloc_unlink(handler, ctx);
1158
1159         status->type = BOOT_STATUS_INFO;
1160         status->message = talloc_asprintf(status, _("Config file %s parsed"),
1161                                         pb_url->file);
1162 msg:
1163         device_handler_boot_status(handler, status);
1164         talloc_free(status);
1165 }
1166
1167 #ifndef PETITBOOT_TEST
1168
1169 /**
1170  * context_commit - Commit a temporary discovery context to the handler,
1171  * and notify the clients about any new options / devices
1172  */
1173 void device_handler_discover_context_commit(struct device_handler *handler,
1174                 struct discover_context *ctx)
1175 {
1176         struct discover_device *dev = ctx->device;
1177         struct discover_boot_option *opt, *tmp;
1178
1179         if (!device_lookup_by_uuid(handler, dev->uuid))
1180                 device_handler_add_device(handler, dev);
1181
1182         /* move boot options from the context to the device */
1183         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
1184                 list_remove(&opt->list);
1185
1186                 /* All boot options need at least a kernel image */
1187                 if (!opt->boot_image || !opt->boot_image->url) {
1188                         pb_log("boot option %s is missing boot image, ignoring\n",
1189                                 opt->option->id);
1190                         talloc_free(opt);
1191                         continue;
1192                 }
1193
1194                 if (boot_option_resolve(opt, handler)) {
1195                         pb_log("boot option %s is resolved, "
1196                                         "sending to clients\n",
1197                                         opt->option->id);
1198                         list_add_tail(&dev->boot_options, &opt->list);
1199                         talloc_steal(dev, opt);
1200                         boot_option_finalise(handler, opt);
1201                         notify_boot_option(handler, opt);
1202                 } else {
1203                         if (!opt->source->resolve_resource) {
1204                                 pb_log("parser %s gave us an unresolved "
1205                                         "resource (%s), but no way to "
1206                                         "resolve it\n",
1207                                         opt->source->name, opt->option->id);
1208                                 talloc_free(opt);
1209                         } else {
1210                                 pb_log("boot option %s is unresolved, "
1211                                                 "adding to queue\n",
1212                                                 opt->option->id);
1213                                 list_add(&handler->unresolved_boot_options,
1214                                                 &opt->list);
1215                                 talloc_steal(handler, opt);
1216                         }
1217                 }
1218         }
1219 }
1220
1221 static void device_handler_update_lang(const char *lang)
1222 {
1223         const char *cur_lang;
1224
1225         if (!lang)
1226                 return;
1227
1228         cur_lang = setlocale(LC_ALL, NULL);
1229         if (cur_lang && !strcmp(cur_lang, lang))
1230                 return;
1231
1232         setlocale(LC_ALL, lang);
1233 }
1234
1235 static int device_handler_init_sources(struct device_handler *handler)
1236 {
1237         /* init our device sources: udev, network and user events */
1238         handler->udev = udev_init(handler, handler->waitset);
1239         if (!handler->udev)
1240                 return -1;
1241
1242         handler->network = network_init(handler, handler->waitset,
1243                         handler->dry_run);
1244         if (!handler->network)
1245                 return -1;
1246
1247         handler->user_event = user_event_init(handler, handler->waitset);
1248         if (!handler->user_event)
1249                 return -1;
1250
1251         return 0;
1252 }
1253
1254 static void device_handler_reinit_sources(struct device_handler *handler)
1255 {
1256         /* if we haven't initialised sources previously (becuase we started in
1257          * safe mode), then init once here. */
1258         if (!(handler->udev || handler->network || handler->user_event)) {
1259                 device_handler_init_sources(handler);
1260                 return;
1261         }
1262
1263         udev_reinit(handler->udev);
1264
1265         network_shutdown(handler->network);
1266         handler->network = network_init(handler, handler->waitset,
1267                         handler->dry_run);
1268 }
1269
1270 static inline const char *get_device_path(struct discover_device *dev)
1271 {
1272         return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1273 }
1274
1275 static char *check_subvols(struct discover_device *dev)
1276 {
1277         const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1278         struct stat sb;
1279         char *path;
1280         int rc;
1281
1282         if (strncmp(fstype, "btrfs", strlen("btrfs")))
1283                 return dev->mount_path;
1284
1285         /* On btrfs a device's root may be under a subvolume path */
1286         path = join_paths(dev, dev->mount_path, "@");
1287         rc = stat(path, &sb);
1288         if (!rc && S_ISDIR(sb.st_mode)) {
1289                 pb_debug("Using '%s' for btrfs root path\n", path);
1290                 return path;
1291         }
1292
1293         talloc_free(path);
1294         return dev->mount_path;
1295 }
1296
1297 static bool check_existing_mount(struct discover_device *dev)
1298 {
1299         struct stat devstat, mntstat;
1300         const char *device_path;
1301         struct mntent *mnt;
1302         FILE *fp;
1303         int rc;
1304
1305         device_path = get_device_path(dev);
1306
1307         rc = stat(device_path, &devstat);
1308         if (rc) {
1309                 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1310                 return false;
1311         }
1312
1313         if (!S_ISBLK(devstat.st_mode)) {
1314                 pb_debug("%s: %s isn't a block device?\n", __func__,
1315                                 dev->device_path);
1316                 return false;
1317         }
1318
1319         fp = fopen("/proc/self/mounts", "r");
1320
1321         for (;;) {
1322                 mnt = getmntent(fp);
1323                 if (!mnt)
1324                         break;
1325
1326                 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1327                         continue;
1328
1329                 rc = stat(mnt->mnt_fsname, &mntstat);
1330                 if (rc)
1331                         continue;
1332
1333                 if (!S_ISBLK(mntstat.st_mode))
1334                         continue;
1335
1336                 if (mntstat.st_rdev == devstat.st_rdev) {
1337                         dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1338                         dev->root_path = check_subvols(dev);
1339                         dev->mounted_rw = !!hasmntopt(mnt, "rw");
1340                         dev->mounted = true;
1341                         dev->unmount = false;
1342
1343                         pb_debug("%s: %s is already mounted (r%c) at %s\n",
1344                                         __func__, dev->device_path,
1345                                         dev->mounted_rw ? 'w' : 'o',
1346                                         mnt->mnt_dir);
1347                         break;
1348                 }
1349         }
1350
1351         fclose(fp);
1352
1353         return mnt != NULL;
1354 }
1355
1356 /*
1357  * Attempt to mount a filesystem safely, while handling certain filesytem-
1358  * specific options
1359  */
1360 static int try_mount(const char *device_path, const char *mount_path,
1361                              const char *fstype, unsigned long flags,
1362                              bool have_snapshot)
1363 {
1364         const char *fs, *safe_opts;
1365         int rc;
1366
1367         /* Mount ext3 as ext4 instead so 'norecovery' can be used */
1368         if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
1369                 pb_debug("Mounting ext3 filesystem as ext4\n");
1370                 fs = "ext4";
1371         } else
1372                 fs = fstype;
1373
1374         if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
1375             strncmp(fs, "ext4", strlen("ext4")) == 0)
1376                 safe_opts = "norecovery";
1377         else
1378                 safe_opts = NULL;
1379
1380         errno = 0;
1381         /* If no snapshot is available don't attempt recovery */
1382         if (!have_snapshot)
1383                 return mount(device_path, mount_path, fs, flags, safe_opts);
1384
1385         rc = mount(device_path, mount_path, fs, flags, NULL);
1386
1387         if (!rc)
1388                 return rc;
1389
1390         /* Mounting failed; some filesystems will fail to mount if a recovery
1391          * journal exists (eg. cross-endian XFS), so try again with norecovery
1392          * where that option is available.
1393          * If mounting read-write just return the error as norecovery is not a
1394          * valid option */
1395         if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
1396                 return rc;
1397
1398         errno = 0;
1399         return mount(device_path, mount_path, fs, flags, safe_opts);
1400 }
1401
1402 static int mount_device(struct discover_device *dev)
1403 {
1404         const char *fstype, *device_path;
1405         int rc;
1406
1407         if (!dev->device_path)
1408                 return -1;
1409
1410         if (dev->mounted)
1411                 return 0;
1412
1413         if (check_existing_mount(dev))
1414                 return 0;
1415
1416         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1417         if (!fstype)
1418                 return 0;
1419
1420         dev->mount_path = join_paths(dev, mount_base(),
1421                                         dev->device_path);
1422
1423         if (pb_mkdir_recursive(dev->mount_path)) {
1424                 pb_log("couldn't create mount directory %s: %s\n",
1425                                 dev->mount_path, strerror(errno));
1426                 goto err_free;
1427         }
1428
1429         device_path = get_device_path(dev);
1430
1431         pb_log("mounting device %s read-only\n", dev->device_path);
1432         rc = try_mount(device_path, dev->mount_path, fstype,
1433                        MS_RDONLY | MS_SILENT, dev->ramdisk);
1434
1435         if (!rc) {
1436                 dev->mounted = true;
1437                 dev->mounted_rw = false;
1438                 dev->unmount = true;
1439                 dev->root_path = check_subvols(dev);
1440                 return 0;
1441         }
1442
1443         pb_log("couldn't mount device %s: mount failed: %s\n",
1444                         device_path, strerror(errno));
1445
1446         /* If mount fails clean up any snapshot */
1447         devmapper_destroy_snapshot(dev);
1448
1449         pb_rmdir_recursive(mount_base(), dev->mount_path);
1450 err_free:
1451         talloc_free(dev->mount_path);
1452         dev->mount_path = NULL;
1453         return -1;
1454 }
1455
1456 static int umount_device(struct discover_device *dev)
1457 {
1458         const char *device_path;
1459         int rc;
1460
1461         if (!dev->mounted || !dev->unmount)
1462                 return 0;
1463
1464         device_path = get_device_path(dev);
1465
1466         pb_log("unmounting device %s\n", device_path);
1467         rc = umount(dev->mount_path);
1468         if (rc)
1469                 return -1;
1470
1471         dev->mounted = false;
1472         devmapper_destroy_snapshot(dev);
1473
1474         pb_rmdir_recursive(mount_base(), dev->mount_path);
1475
1476         talloc_free(dev->mount_path);
1477         dev->mount_path = NULL;
1478         dev->root_path = NULL;
1479
1480         return 0;
1481 }
1482
1483 int device_request_write(struct discover_device *dev, bool *release)
1484 {
1485         const char *fstype, *device_path;
1486         const struct config *config;
1487         int rc;
1488
1489         *release = false;
1490
1491         config = config_get();
1492         if (!config->allow_writes)
1493                 return -1;
1494
1495         if (!dev->mounted)
1496                 return -1;
1497
1498         if (dev->mounted_rw)
1499                 return 0;
1500
1501         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1502
1503         device_path = get_device_path(dev);
1504
1505         pb_log("remounting device %s read-write\n", device_path);
1506
1507         rc = umount(dev->mount_path);
1508         if (rc) {
1509                 pb_log("Failed to unmount %s: %s\n",
1510                        dev->mount_path, strerror(errno));
1511                 return -1;
1512         }
1513
1514         rc = try_mount(device_path, dev->mount_path, fstype,
1515                        MS_SILENT, dev->ramdisk);
1516         if (rc)
1517                 goto mount_ro;
1518
1519         dev->mounted_rw = true;
1520         *release = true;
1521         return 0;
1522
1523 mount_ro:
1524         pb_log("Unable to remount device %s read-write: %s\n",
1525                device_path, strerror(errno));
1526         rc = try_mount(device_path, dev->mount_path, fstype,
1527                        MS_RDONLY | MS_SILENT, dev->ramdisk);
1528         if (rc)
1529                 pb_log("Unable to recover mount for %s: %s\n",
1530                        device_path, strerror(errno));
1531         return -1;
1532 }
1533
1534 void device_release_write(struct discover_device *dev, bool release)
1535 {
1536         const char *fstype, *device_path;
1537
1538         if (!release)
1539                 return;
1540
1541         device_path = get_device_path(dev);
1542
1543         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1544
1545         pb_log("remounting device %s read-only\n", device_path);
1546
1547         if (umount(dev->mount_path)) {
1548                 pb_log("Failed to unmount %s\n", dev->mount_path);
1549                 return;
1550         }
1551         dev->mounted_rw = dev->mounted = false;
1552
1553         if (dev->ramdisk) {
1554                 devmapper_merge_snapshot(dev);
1555                 /* device_path becomes stale after merge */
1556                 device_path = get_device_path(dev);
1557         }
1558
1559         if (try_mount(device_path, dev->mount_path, fstype,
1560                        MS_RDONLY | MS_SILENT, dev->ramdisk))
1561                 pb_log("Failed to remount %s read-only: %s\n",
1562                        device_path, strerror(errno));
1563         else
1564                 dev->mounted = true;
1565 }
1566
1567 void device_sync_snapshots(struct device_handler *handler, const char *device)
1568 {
1569         struct discover_device *dev = NULL;
1570         unsigned int i;
1571
1572         if (device) {
1573                 /* Find matching device and sync */
1574                 dev = device_lookup_by_name(handler, device);
1575                 if (!dev) {
1576                         pb_log("%s: device name '%s' unrecognised\n",
1577                                 __func__, device);
1578                         return;
1579                 }
1580                 if (dev->ramdisk)
1581                         device_release_write(dev, true);
1582                 else
1583                         pb_log("%s has no snapshot to merge, skipping\n",
1584                                 dev->device->id);
1585                 return;
1586         }
1587
1588         /* Otherwise sync all relevant devices */
1589         for (i = 0; i < handler->n_devices; i++) {
1590                 dev = handler->devices[i];
1591                 if (dev->device->type != DEVICE_TYPE_DISK &&
1592                         dev->device->type != DEVICE_TYPE_USB)
1593                         continue;
1594                 if (dev->ramdisk)
1595                         device_release_write(dev, true);
1596                 else
1597                         pb_log("%s has no snapshot to merge, skipping\n",
1598                                 dev->device->id);
1599         }
1600 }
1601
1602 #else
1603
1604 void device_handler_discover_context_commit(
1605                 struct device_handler *handler __attribute__((unused)),
1606                 struct discover_context *ctx __attribute__((unused)))
1607 {
1608         pb_log("%s stubbed out for test cases\n", __func__);
1609 }
1610
1611 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1612 {
1613 }
1614
1615 static int device_handler_init_sources(
1616                 struct device_handler *handler __attribute__((unused)))
1617 {
1618         return 0;
1619 }
1620
1621 static void device_handler_reinit_sources(
1622                 struct device_handler *handler __attribute__((unused)))
1623 {
1624 }
1625
1626 static int umount_device(struct discover_device *dev __attribute__((unused)))
1627 {
1628         return 0;
1629 }
1630
1631 static int __attribute__((unused)) mount_device(
1632                 struct discover_device *dev __attribute__((unused)))
1633 {
1634         return 0;
1635 }
1636
1637 int device_request_write(struct discover_device *dev __attribute__((unused)),
1638                 bool *release)
1639 {
1640         *release = true;
1641         return 0;
1642 }
1643
1644 void device_release_write(struct discover_device *dev __attribute__((unused)),
1645         bool release __attribute__((unused)))
1646 {
1647 }
1648
1649 void device_sync_snapshots(
1650                 struct device_handler *handler __attribute__((unused)),
1651                 const char *device __attribute__((unused)))
1652 {
1653 }
1654
1655 #endif