block: remove duplicate block device wildcard
[project/ubox.git] / block.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #define _GNU_SOURCE
16 #include <getopt.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <syslog.h>
20 #include <libgen.h>
21 #include <glob.h>
22
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/swap.h>
26 #include <sys/mount.h>
27 #include <sys/wait.h>
28
29 #include <uci.h>
30 #include <uci_blob.h>
31
32 #include <libubox/list.h>
33 #include <libubox/vlist.h>
34 #include <libubox/blobmsg_json.h>
35 #include <libubox/avl-cmp.h>
36
37 #include "libblkid-tiny/libblkid-tiny.h"
38
39 enum {
40 TYPE_MOUNT,
41 TYPE_SWAP,
42 };
43
44 struct mount {
45 struct vlist_node node;
46 int type;
47
48 char *target;
49 char *path;
50 char *options;
51 uint32_t flags;
52 char *uuid;
53 char *label;
54 char *device;
55 int extroot;
56 int overlay;
57 int disabled_fsck;
58 unsigned int prio;
59 };
60
61 static struct vlist_tree mounts;
62 static struct blob_buf b;
63 static LIST_HEAD(devices);
64 static int anon_mount, anon_swap, auto_mount, auto_swap, check_fs;
65 static unsigned int delay_root;
66
67 enum {
68 CFG_ANON_MOUNT,
69 CFG_ANON_SWAP,
70 CFG_AUTO_MOUNT,
71 CFG_AUTO_SWAP,
72 CFG_DELAY_ROOT,
73 CFG_CHECK_FS,
74 __CFG_MAX
75 };
76
77 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
78 [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
79 [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
80 [CFG_AUTO_SWAP] = { .name = "auto_swap", .type = BLOBMSG_TYPE_INT32 },
81 [CFG_AUTO_MOUNT] = { .name = "auto_mount", .type = BLOBMSG_TYPE_INT32 },
82 [CFG_DELAY_ROOT] = { .name = "delay_root", .type = BLOBMSG_TYPE_INT32 },
83 [CFG_CHECK_FS] = { .name = "check_fs", .type = BLOBMSG_TYPE_INT32 },
84 };
85
86 enum {
87 MOUNT_UUID,
88 MOUNT_LABEL,
89 MOUNT_ENABLE,
90 MOUNT_TARGET,
91 MOUNT_DEVICE,
92 MOUNT_OPTIONS,
93 __MOUNT_MAX
94 };
95
96 static const struct uci_blob_param_list config_attr_list = {
97 .n_params = __CFG_MAX,
98 .params = config_policy,
99 };
100
101 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
102 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
103 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
104 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
105 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
106 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
107 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
108 };
109
110 static const struct uci_blob_param_list mount_attr_list = {
111 .n_params = __MOUNT_MAX,
112 .params = mount_policy,
113 };
114
115 enum {
116 SWAP_ENABLE,
117 SWAP_UUID,
118 SWAP_LABEL,
119 SWAP_DEVICE,
120 SWAP_PRIO,
121 __SWAP_MAX
122 };
123
124 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
125 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
126 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
127 [SWAP_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
128 [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
129 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
130 };
131
132 static const struct uci_blob_param_list swap_attr_list = {
133 .n_params = __SWAP_MAX,
134 .params = swap_policy,
135 };
136
137 struct mount_flag {
138 const char *name;
139 int32_t flag;
140 };
141
142 #ifndef MS_DIRSYNC
143 # define MS_DIRSYNC (1 << 7)
144 #endif
145
146 #ifndef MS_RELATIME
147 # define MS_RELATIME (1 << 21)
148 #endif
149
150 #ifndef MS_STRICTATIME
151 # define MS_STRICTATIME (1 << 24)
152 #endif
153
154 static const struct mount_flag mount_flags[] = {
155 { "sync", MS_SYNCHRONOUS },
156 { "async", ~MS_SYNCHRONOUS },
157 { "dirsync", MS_DIRSYNC },
158 { "mand", MS_MANDLOCK },
159 { "nomand", ~MS_MANDLOCK },
160 { "atime", ~MS_NOATIME },
161 { "noatime", MS_NOATIME },
162 { "dev", ~MS_NODEV },
163 { "nodev", MS_NODEV },
164 { "diratime", ~MS_NODIRATIME },
165 { "nodiratime", MS_NODIRATIME },
166 { "exec", ~MS_NOEXEC },
167 { "noexec", MS_NOEXEC },
168 { "suid", ~MS_NOSUID },
169 { "nosuid", MS_NOSUID },
170 { "rw", ~MS_RDONLY },
171 { "ro", MS_RDONLY },
172 { "relatime", MS_RELATIME },
173 { "norelatime", ~MS_RELATIME },
174 { "strictatime", MS_STRICTATIME },
175 };
176
177 static char *blobmsg_get_strdup(struct blob_attr *attr)
178 {
179 if (!attr)
180 return NULL;
181
182 return strdup(blobmsg_get_string(attr));
183 }
184
185 static char *blobmsg_get_basename(struct blob_attr *attr)
186 {
187 if (!attr)
188 return NULL;
189
190 return strdup(basename(blobmsg_get_string(attr)));
191 }
192
193 static void parse_mount_options(struct mount *m, char *optstr)
194 {
195 int i;
196 bool is_flag;
197 char *p, *opts, *last;
198
199 m->flags = 0;
200 m->options = NULL;
201
202 if (!optstr || !*optstr)
203 return;
204
205 m->options = opts = calloc(1, strlen(optstr) + 1);
206
207 if (!m->options)
208 return;
209
210 p = last = optstr;
211
212 do {
213 p = strchr(p, ',');
214
215 if (p)
216 *p++ = 0;
217
218 for (i = 0, is_flag = false; i < ARRAY_SIZE(mount_flags); i++) {
219 if (!strcmp(last, mount_flags[i].name)) {
220 if (mount_flags[i].flag < 0)
221 m->flags &= (uint32_t)mount_flags[i].flag;
222 else
223 m->flags |= (uint32_t)mount_flags[i].flag;
224 is_flag = true;
225 break;
226 }
227 }
228
229 if (!is_flag)
230 opts += sprintf(opts, "%s%s", (opts > m->options) ? "," : "", last);
231
232 last = p;
233
234 } while (p);
235
236 free(optstr);
237 }
238
239 static int mount_add(struct uci_section *s)
240 {
241 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
242 struct mount *m;
243
244 blob_buf_init(&b, 0);
245 uci_to_blob(&b, s, &mount_attr_list);
246 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
247
248 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
249 return -1;
250
251 if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
252 return -1;
253
254 m = malloc(sizeof(struct mount));
255 m->type = TYPE_MOUNT;
256 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
257 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
258 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
259 m->device = blobmsg_get_basename(tb[MOUNT_DEVICE]);
260
261 parse_mount_options(m, blobmsg_get_strdup(tb[MOUNT_OPTIONS]));
262
263 m->overlay = m->extroot = 0;
264 if (m->target && !strcmp(m->target, "/"))
265 m->extroot = 1;
266 if (m->target && !strcmp(m->target, "/overlay"))
267 m->extroot = m->overlay = 1;
268
269 if (m->uuid)
270 vlist_add(&mounts, &m->node, m->uuid);
271 else if (m->label)
272 vlist_add(&mounts, &m->node, m->label);
273 else if (m->device)
274 vlist_add(&mounts, &m->node, m->device);
275
276 return 0;
277 }
278
279 static int swap_add(struct uci_section *s)
280 {
281 struct blob_attr *tb[__SWAP_MAX] = { 0 };
282 struct mount *m;
283
284 blob_buf_init(&b, 0);
285 uci_to_blob(&b, s, &swap_attr_list);
286 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
287
288 if (!tb[SWAP_UUID] && !tb[SWAP_LABEL] && !tb[SWAP_DEVICE])
289 return -1;
290
291 m = malloc(sizeof(struct mount));
292 memset(m, 0, sizeof(struct mount));
293 m->type = TYPE_SWAP;
294 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
295 m->label = blobmsg_get_strdup(tb[SWAP_LABEL]);
296 m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
297 if (tb[SWAP_PRIO])
298 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
299 if (m->prio)
300 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
301
302 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE])) {
303 /* store complete swap path */
304 if (tb[SWAP_DEVICE])
305 m->target = blobmsg_get_strdup(tb[SWAP_DEVICE]);
306
307 if (m->uuid)
308 vlist_add(&mounts, &m->node, m->uuid);
309 else if (m->label)
310 vlist_add(&mounts, &m->node, m->label);
311 else if (m->device)
312 vlist_add(&mounts, &m->node, m->device);
313 }
314
315 return 0;
316 }
317
318 static int global_add(struct uci_section *s)
319 {
320 struct blob_attr *tb[__CFG_MAX] = { 0 };
321
322 blob_buf_init(&b, 0);
323 uci_to_blob(&b, s, &config_attr_list);
324 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
325
326 if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
327 anon_mount = 1;
328 if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
329 anon_swap = 1;
330
331 if ((tb[CFG_AUTO_MOUNT]) && blobmsg_get_u32(tb[CFG_AUTO_MOUNT]))
332 auto_mount = 1;
333 if ((tb[CFG_AUTO_SWAP]) && blobmsg_get_u32(tb[CFG_AUTO_SWAP]))
334 auto_swap = 1;
335
336 if (tb[CFG_DELAY_ROOT])
337 delay_root = blobmsg_get_u32(tb[CFG_DELAY_ROOT]);
338
339 if ((tb[CFG_CHECK_FS]) && blobmsg_get_u32(tb[CFG_CHECK_FS]))
340 check_fs = 1;
341
342 return 0;
343 }
344
345 static struct mount* find_swap(const char *uuid, const char *label, const char *device)
346 {
347 struct mount *m;
348
349 vlist_for_each_element(&mounts, m, node) {
350 if (m->type != TYPE_SWAP)
351 continue;
352 if (uuid && m->uuid && !strcmp(m->uuid, uuid))
353 return m;
354 if (label && m->label && !strcmp(m->label, label))
355 return m;
356 if (device && m->device && !strcmp(m->device, device))
357 return m;
358 }
359
360 return NULL;
361 }
362
363 static struct mount* find_block(const char *uuid, const char *label, const char *device,
364 const char *target)
365 {
366 struct mount *m;
367
368 vlist_for_each_element(&mounts, m, node) {
369 if (m->type != TYPE_MOUNT)
370 continue;
371 if (m->uuid && uuid && !strcmp(m->uuid, uuid))
372 return m;
373 if (m->label && label && !strcmp(m->label, label))
374 return m;
375 if (m->target && target && !strcmp(m->target, target))
376 return m;
377 if (m->device && device && !strcmp(m->device, device))
378 return m;
379 }
380
381 return NULL;
382 }
383
384 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
385 struct vlist_node *node_old)
386 {
387 }
388
389 static int config_load(char *cfg)
390 {
391 struct uci_context *ctx;
392 struct uci_package *pkg;
393 struct uci_element *e;
394
395 vlist_init(&mounts, avl_strcmp, mounts_update);
396
397 ctx = uci_alloc_context();
398 if (cfg) {
399 char path[32];
400 snprintf(path, 32, "%s/etc/config", cfg);
401 uci_set_confdir(ctx, path);
402 }
403
404 if (uci_load(ctx, "fstab", &pkg))
405 return -1;
406
407 vlist_update(&mounts);
408 uci_foreach_element(&pkg->sections, e) {
409 struct uci_section *s = uci_to_section(e);
410
411 if (!strcmp(s->type, "mount"))
412 mount_add(s);
413 if (!strcmp(s->type, "swap"))
414 swap_add(s);
415 if (!strcmp(s->type, "global"))
416 global_add(s);
417 }
418 vlist_flush(&mounts);
419
420 return 0;
421 }
422
423 static struct blkid_struct_probe* _probe_path(char *path)
424 {
425 struct blkid_struct_probe *pr;
426
427 pr = malloc(sizeof(*pr));
428
429 if (!pr)
430 return NULL;
431
432 memset(pr, 0, sizeof(*pr));
433 probe_block(path, pr);
434
435 if (pr->err || !pr->id) {
436 free(pr);
437 return NULL;
438 }
439
440 return pr;
441 }
442
443 static int _cache_load(const char *path)
444 {
445 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
446 int j;
447 glob_t gl;
448
449 if (glob(path, gl_flags, NULL, &gl) < 0)
450 return -1;
451
452 for (j = 0; j < gl.gl_pathc; j++) {
453 struct blkid_struct_probe *pr = _probe_path(gl.gl_pathv[j]);
454 if (pr)
455 list_add_tail(&pr->list, &devices);
456 }
457
458 globfree(&gl);
459
460 return 0;
461 }
462
463 static void cache_load(int mtd)
464 {
465 if (mtd)
466 _cache_load("/dev/mtdblock*");
467 _cache_load("/dev/mmcblk*");
468 _cache_load("/dev/sd*");
469 _cache_load("/dev/hd*");
470 _cache_load("/dev/md*");
471 _cache_load("/dev/mapper/*");
472 }
473
474 static int print_block_info(struct blkid_struct_probe *pr)
475 {
476 printf("%s:", pr->dev);
477 if (pr->uuid[0])
478 printf(" UUID=\"%s\"", pr->uuid);
479
480 if (pr->label[0])
481 printf(" LABEL=\"%s\"", pr->label);
482
483 if (pr->name[0])
484 printf(" NAME=\"%s\"", pr->name);
485
486 if (pr->version[0])
487 printf(" VERSION=\"%s\"", pr->version);
488
489 printf(" TYPE=\"%s\"\n", pr->id->name);
490
491 return 0;
492 }
493
494 static int print_block_uci(struct blkid_struct_probe *pr)
495 {
496 if (!strcmp(pr->id->name, "swap")) {
497 printf("config 'swap'\n");
498 } else {
499 printf("config 'mount'\n");
500 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
501 }
502 if (pr->uuid[0])
503 printf("\toption\tuuid\t'%s'\n", pr->uuid);
504 else
505 printf("\toption\tdevice\t'%s'\n", pr->dev);
506 printf("\toption\tenabled\t'0'\n\n");
507
508 return 0;
509 }
510
511 static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char *path)
512 {
513 struct blkid_struct_probe *pr = NULL;
514
515 if (uuid)
516 list_for_each_entry(pr, &devices, list)
517 if (!strcmp(pr->uuid, uuid))
518 return pr;
519
520 if (label)
521 list_for_each_entry(pr, &devices, list)
522 if (!strcmp(pr->label, label))
523 return pr;
524
525 if (path)
526 list_for_each_entry(pr, &devices, list)
527 if (!strcmp(basename(pr->dev), basename(path)))
528 return pr;
529
530 return NULL;
531 }
532
533 static char* find_mount_point(char *block)
534 {
535 FILE *fp = fopen("/proc/mounts", "r");
536 static char line[256];
537 int len = strlen(block);
538 char *point = NULL;
539
540 if(!fp)
541 return NULL;
542
543 while (fgets(line, sizeof(line), fp)) {
544 if (!strncmp(line, block, len)) {
545 char *p = &line[len + 1];
546 char *t = strstr(p, " ");
547
548 if (!t) {
549 fclose(fp);
550 return NULL;
551 }
552 *t = '\0';
553 point = p;
554 break;
555 }
556 }
557
558 fclose(fp);
559
560 return point;
561 }
562
563 static void mkdir_p(char *dir)
564 {
565 char *l = strrchr(dir, '/');
566
567 if (l) {
568 *l = '\0';
569 mkdir_p(dir);
570 *l = '/';
571 mkdir(dir, 0755);
572 }
573 }
574
575 static void check_filesystem(struct blkid_struct_probe *pr)
576 {
577 pid_t pid;
578 struct stat statbuf;
579 char *e2fsck = "/usr/sbin/e2fsck";
580
581 if (strncmp(pr->id->name, "ext", 3)) {
582 fprintf(stderr, "check_filesystem: %s is not supported\n", pr->id->name);
583 return;
584 }
585
586 if (stat(e2fsck, &statbuf) < 0) {
587 fprintf(stderr, "check_filesystem: %s not found\n", e2fsck);
588 return;
589 }
590
591 pid = fork();
592 if (!pid) {
593 execl(e2fsck, e2fsck, "-p", pr->dev, NULL);
594 exit(-1);
595 } else if (pid > 0) {
596 int status;
597
598 waitpid(pid, &status, 0);
599 if (WEXITSTATUS(status))
600 fprintf(stderr, "check_filesystem: %s returned %d\n", e2fsck, WEXITSTATUS(status));
601 }
602 }
603
604 static void handle_swapfiles(bool on)
605 {
606 struct stat s;
607 struct mount *m;
608 struct blkid_struct_probe *pr;
609
610 vlist_for_each_element(&mounts, m, node)
611 {
612 if (m->type != TYPE_SWAP || !m->target)
613 continue;
614
615 if (stat(m->target, &s) || !S_ISREG(s.st_mode))
616 continue;
617
618 pr = _probe_path(m->target);
619
620 if (!pr)
621 continue;
622
623 if (!strcmp(pr->id->name, "swap")) {
624 if (on)
625 swapon(pr->dev, m->prio);
626 else
627 swapoff(pr->dev);
628 }
629
630 free(pr);
631 }
632 }
633
634 static int mount_device(struct blkid_struct_probe *pr, int hotplug)
635 {
636 struct mount *m;
637 char *device;
638
639 if (!pr)
640 return -1;
641
642 device = basename(pr->dev);
643
644 if (!strcmp(pr->id->name, "swap")) {
645 if (hotplug && !auto_swap)
646 return -1;
647 m = find_swap(pr->uuid, pr->label, device);
648 if (m || anon_swap)
649 swapon(pr->dev, (m) ? (m->prio) : (0));
650
651 return 0;
652 }
653
654 if (hotplug && !auto_mount)
655 return -1;
656
657 if (find_mount_point(pr->dev)) {
658 fprintf(stderr, "%s is already mounted\n", pr->dev);
659 return -1;
660 }
661
662 m = find_block(pr->uuid, pr->label, device, NULL);
663 if (m && m->extroot)
664 return -1;
665
666 if (m) {
667 char *target = m->target;
668 char _target[32];
669 int err = 0;
670
671 if (!target) {
672 snprintf(_target, sizeof(_target), "/mnt/%s", device);
673 target = _target;
674 }
675 mkdir_p(target);
676
677 if (check_fs)
678 check_filesystem(pr);
679
680 err = mount(pr->dev, target, pr->id->name, m->flags,
681 (m->options) ? (m->options) : (""));
682 if (err)
683 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
684 pr->dev, pr->id->name, target, err, strerror(err));
685 else
686 handle_swapfiles(true);
687 return err;
688 }
689
690 if (anon_mount) {
691 char target[] = "/mnt/mmcblk123";
692 int err = 0;
693
694 snprintf(target, sizeof(target), "/mnt/%s", device);
695 mkdir_p(target);
696
697 if (check_fs)
698 check_filesystem(pr);
699
700 err = mount(pr->dev, target, pr->id->name, 0, "");
701 if (err)
702 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
703 pr->dev, pr->id->name, target, err, strerror(err));
704 else
705 handle_swapfiles(true);
706 return err;
707 }
708
709 return 0;
710 }
711
712 static int umount_device(struct blkid_struct_probe *pr)
713 {
714 struct mount *m;
715 char *device = basename(pr->dev);
716 char *mp;
717 int err;
718
719 if (!pr)
720 return -1;
721
722 if (!strcmp(pr->id->name, "swap"))
723 return -1;
724
725 mp = find_mount_point(pr->dev);
726 if (!mp)
727 return -1;
728
729 m = find_block(pr->uuid, pr->label, device, NULL);
730 if (m && m->extroot)
731 return -1;
732
733 err = umount2(mp, MNT_DETACH);
734 if (err)
735 fprintf(stderr, "unmounting %s (%s) failed (%d) - %s\n",
736 pr->dev, mp, err, strerror(err));
737 else
738 fprintf(stderr, "unmounted %s (%s)\n",
739 pr->dev, mp);
740
741 return err;
742 }
743
744 static int main_hotplug(int argc, char **argv)
745 {
746 char path[32];
747 char *action, *device, *mount_point;
748
749 action = getenv("ACTION");
750 device = getenv("DEVNAME");
751
752 if (!action || !device)
753 return -1;
754 snprintf(path, sizeof(path), "/dev/%s", device);
755
756 if (!strcmp(action, "remove")) {
757 int err = 0;
758 mount_point = find_mount_point(path);
759 if (mount_point)
760 err = umount2(mount_point, MNT_DETACH);
761
762 if (err)
763 fprintf(stderr, "umount of %s failed (%d) - %s\n",
764 mount_point, err, strerror(err));
765
766 return 0;
767 } else if (strcmp(action, "add")) {
768 fprintf(stderr, "Unkown action %s\n", action);
769
770 return -1;
771 }
772
773 if (config_load(NULL))
774 return -1;
775 cache_load(0);
776
777 return mount_device(find_block_info(NULL, NULL, path), 1);
778 }
779
780 static int find_block_mtd(char *name, char *part, int plen)
781 {
782 FILE *fp = fopen("/proc/mtd", "r");
783 static char line[256];
784 char *index = NULL;
785
786 if(!fp)
787 return -1;
788
789 while (!index && fgets(line, sizeof(line), fp)) {
790 if (strstr(line, name)) {
791 char *eol = strstr(line, ":");
792
793 if (!eol)
794 continue;
795
796 *eol = '\0';
797 index = &line[3];
798 }
799 }
800
801 fclose(fp);
802
803 if (!index)
804 return -1;
805
806 snprintf(part, plen, "/dev/mtdblock%s", index);
807
808 return 0;
809 }
810
811 static int check_extroot(char *path)
812 {
813 struct blkid_struct_probe *pr = NULL;
814 char fs[32];
815
816 if (find_block_mtd("rootfs", fs, sizeof(fs)))
817 return -1;
818
819 list_for_each_entry(pr, &devices, list) {
820 if (!strcmp(pr->dev, fs)) {
821 struct stat s;
822 FILE *fp = NULL;
823 char tag[64];
824 char uuid[64] = { 0 };
825
826 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
827 if (stat(tag, &s)) {
828 fp = fopen(tag, "w+");
829 if (!fp) {
830 fprintf(stderr, "extroot: failed to write uuid tag file\n");
831 /* return 0 to continue boot regardless of error */
832 return 0;
833 }
834 fputs(pr->uuid, fp);
835 fclose(fp);
836 return 0;
837 }
838
839 fp = fopen(tag, "r");
840 if (!fp) {
841 fprintf(stderr, "extroot: failed to open uuid tag file\n");
842 return -1;
843 }
844
845 fgets(uuid, sizeof(uuid), fp);
846 fclose(fp);
847 if (!strcmp(uuid, pr->uuid))
848 return 0;
849 fprintf(stderr, "extroot: uuid tag does not match rom uuid\n");
850 }
851 }
852 return -1;
853 }
854
855 static int mount_extroot(char *cfg)
856 {
857 char overlay[] = "/tmp/extroot/overlay";
858 char mnt[] = "/tmp/extroot/mnt";
859 char *path = mnt;
860 struct blkid_struct_probe *pr;
861 struct mount *m;
862 int err = -1;
863
864 if (config_load(cfg))
865 return -2;
866
867 m = find_block(NULL, NULL, NULL, "/");
868 if (!m)
869 m = find_block(NULL, NULL, NULL, "/overlay");
870
871 if (!m || !m->extroot)
872 return -1;
873
874 pr = find_block_info(m->uuid, m->label, m->device);
875
876 if (!pr && delay_root){
877 fprintf(stderr, "extroot: is not ready yet, retrying in %u seconds\n", delay_root);
878 sleep(delay_root);
879 mkblkdev();
880 cache_load(0);
881 pr = find_block_info(m->uuid, m->label, m->device);
882 }
883 if (pr) {
884 if (strncmp(pr->id->name, "ext", 3)) {
885 fprintf(stderr, "extroot: %s is not supported, try ext4\n", pr->id->name);
886 return -1;
887 }
888 if (m->overlay)
889 path = overlay;
890 mkdir_p(path);
891
892 if (check_fs)
893 check_filesystem(pr);
894
895 err = mount(pr->dev, path, pr->id->name, 0, (m->options) ? (m->options) : (""));
896
897 if (err) {
898 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
899 pr->dev, pr->id->name, path, err, strerror(err));
900 } else if (m->overlay) {
901 err = check_extroot(path);
902 if (err)
903 umount(path);
904 }
905 }
906
907 return err;
908 }
909
910 static int main_extroot(int argc, char **argv)
911 {
912 struct blkid_struct_probe *pr;
913 char fs[32] = { 0 };
914 char fs_data[32] = { 0 };
915 int err = -1;
916
917 if (!getenv("PREINIT"))
918 return -1;
919
920 if (argc != 2) {
921 fprintf(stderr, "Usage: block extroot mountpoint\n");
922 return -1;
923 }
924
925 mkblkdev();
926 cache_load(1);
927
928 find_block_mtd("rootfs", fs, sizeof(fs));
929 if (!fs[0])
930 return -2;
931
932 pr = find_block_info(NULL, NULL, fs);
933 if (!pr)
934 return -3;
935
936 find_block_mtd("rootfs_data", fs_data, sizeof(fs_data));
937 if (fs_data[0]) {
938 pr = find_block_info(NULL, NULL, fs_data);
939 if (pr && !strcmp(pr->id->name, "jffs2")) {
940 char cfg[] = "/tmp/jffs_cfg";
941
942 mkdir_p(cfg);
943 if (!mount(fs_data, cfg, "jffs2", MS_NOATIME, NULL)) {
944 err = mount_extroot(cfg);
945 umount2(cfg, MNT_DETACH);
946 }
947 if (err < 0)
948 rmdir("/tmp/overlay");
949 rmdir(cfg);
950 return err;
951 }
952 }
953
954 return mount_extroot(NULL);
955 }
956
957 static int main_mount(int argc, char **argv)
958 {
959 struct blkid_struct_probe *pr;
960
961 if (config_load(NULL))
962 return -1;
963
964 cache_load(1);
965 list_for_each_entry(pr, &devices, list)
966 mount_device(pr, 0);
967
968 handle_swapfiles(true);
969
970 return 0;
971 }
972
973 static int main_umount(int argc, char **argv)
974 {
975 struct blkid_struct_probe *pr;
976
977 if (config_load(NULL))
978 return -1;
979
980 handle_swapfiles(false);
981
982 cache_load(0);
983 list_for_each_entry(pr, &devices, list)
984 umount_device(pr);
985
986 return 0;
987 }
988
989 static int main_detect(int argc, char **argv)
990 {
991 struct blkid_struct_probe *pr;
992
993 cache_load(0);
994 printf("config 'global'\n");
995 printf("\toption\tanon_swap\t'0'\n");
996 printf("\toption\tanon_mount\t'0'\n");
997 printf("\toption\tauto_swap\t'1'\n");
998 printf("\toption\tauto_mount\t'1'\n");
999 printf("\toption\tdelay_root\t'5'\n");
1000 printf("\toption\tcheck_fs\t'0'\n\n");
1001 list_for_each_entry(pr, &devices, list)
1002 print_block_uci(pr);
1003
1004 return 0;
1005 }
1006
1007 static int main_info(int argc, char **argv)
1008 {
1009 int i;
1010 struct blkid_struct_probe *pr;
1011
1012 cache_load(1);
1013 if (argc == 2) {
1014 list_for_each_entry(pr, &devices, list)
1015 print_block_info(pr);
1016
1017 return 0;
1018 };
1019
1020 for (i = 2; i < argc; i++) {
1021 struct stat s;
1022
1023 if (stat(argv[i], &s)) {
1024 fprintf(stderr, "failed to stat %s\n", argv[i]);
1025 continue;
1026 }
1027 if (!S_ISBLK(s.st_mode)) {
1028 fprintf(stderr, "%s is not a block device\n", argv[i]);
1029 continue;
1030 }
1031 pr = find_block_info(NULL, NULL, argv[i]);
1032 if (pr)
1033 print_block_info(pr);
1034 }
1035
1036 return 0;
1037 }
1038
1039 static int swapon_usage(void)
1040 {
1041 fprintf(stderr, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1042 "\tStart swapping on [DEVICE]\n"
1043 " -a\tStart swapping on all swap devices\n"
1044 " -p pri\tSet priority of swap device\n"
1045 " -s\tShow summary\n");
1046 return -1;
1047 }
1048
1049 static int main_swapon(int argc, char **argv)
1050 {
1051 int ch;
1052 FILE *fp;
1053 char *lineptr;
1054 size_t s;
1055 struct blkid_struct_probe *pr;
1056 int flags = 0;
1057 int pri;
1058 struct stat st;
1059 int err;
1060
1061 while ((ch = getopt(argc, argv, "ap:s")) != -1) {
1062 switch(ch) {
1063 case 's':
1064 fp = fopen("/proc/swaps", "r");
1065 lineptr = NULL;
1066
1067 if (!fp) {
1068 fprintf(stderr, "failed to open /proc/swaps\n");
1069 return -1;
1070 }
1071 while (getline(&lineptr, &s, fp) > 0)
1072 printf(lineptr);
1073 if (lineptr)
1074 free(lineptr);
1075 fclose(fp);
1076 return 0;
1077 case 'a':
1078 cache_load(0);
1079 list_for_each_entry(pr, &devices, list) {
1080 if (strcmp(pr->id->name, "swap"))
1081 continue;
1082 if (swapon(pr->dev, 0))
1083 fprintf(stderr, "failed to swapon %s\n", pr->dev);
1084 }
1085 return 0;
1086 case 'p':
1087 pri = atoi(optarg);
1088 if (pri >= 0)
1089 flags = ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
1090 break;
1091 default:
1092 return swapon_usage();
1093 }
1094
1095 }
1096
1097 if (optind != (argc - 1))
1098 return swapon_usage();
1099
1100 if (stat(argv[optind], &st) || (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode))) {
1101 fprintf(stderr, "%s is not a block device or file\n", argv[optind]);
1102 return -1;
1103 }
1104 err = swapon(argv[optind], flags);
1105 if (err) {
1106 fprintf(stderr, "failed to swapon %s (%d)\n", argv[optind], err);
1107 return err;
1108 }
1109
1110 return 0;
1111 }
1112
1113 static int main_swapoff(int argc, char **argv)
1114 {
1115 if (argc != 2) {
1116 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\n"
1117 "\tStop swapping on DEVICE\n"
1118 " -a\tStop swapping on all swap devices\n");
1119 return -1;
1120 }
1121
1122 if (!strcmp(argv[1], "-a")) {
1123 FILE *fp = fopen("/proc/swaps", "r");
1124 char line[256];
1125
1126 if (!fp) {
1127 fprintf(stderr, "failed to open /proc/swaps\n");
1128 return -1;
1129 }
1130 fgets(line, sizeof(line), fp);
1131 while (fgets(line, sizeof(line), fp)) {
1132 char *end = strchr(line, ' ');
1133 int err;
1134
1135 if (!end)
1136 continue;
1137 *end = '\0';
1138 err = swapoff(line);
1139 if (err)
1140 fprintf(stderr, "failed to swapoff %s (%d)\n", line, err);
1141 }
1142 fclose(fp);
1143 } else {
1144 struct stat s;
1145 int err;
1146
1147 if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
1148 fprintf(stderr, "%s is not a block device or file\n", argv[1]);
1149 return -1;
1150 }
1151 err = swapoff(argv[1]);
1152 if (err) {
1153 fprintf(stderr, "fsiled to swapoff %s (%d)\n", argv[1], err);
1154 return err;
1155 }
1156 }
1157
1158 return 0;
1159 }
1160
1161 int main(int argc, char **argv)
1162 {
1163 char *base = basename(*argv);
1164
1165 umask(0);
1166
1167 if (!strcmp(base, "swapon"))
1168 return main_swapon(argc, argv);
1169
1170 if (!strcmp(base, "swapoff"))
1171 return main_swapoff(argc, argv);
1172
1173 if ((argc > 1) && !strcmp(base, "block")) {
1174 if (!strcmp(argv[1], "info"))
1175 return main_info(argc, argv);
1176
1177 if (!strcmp(argv[1], "detect"))
1178 return main_detect(argc, argv);
1179
1180 if (!strcmp(argv[1], "hotplug"))
1181 return main_hotplug(argc, argv);
1182
1183 if (!strcmp(argv[1], "extroot"))
1184 return main_extroot(argc, argv);
1185
1186 if (!strcmp(argv[1], "mount"))
1187 return main_mount(argc, argv);
1188
1189 if (!strcmp(argv[1], "umount"))
1190 return main_umount(argc, argv);
1191 }
1192
1193 fprintf(stderr, "Usage: block <info|mount|umount|detect>\n");
1194
1195 return -1;
1196 }