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