mount_root.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <sys/mount.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <unistd.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <libubox/ulog.h>
  20. #include "libfstools/libfstools.h"
  21. #include "libfstools/volume.h"
  22. /*
  23. * Called in the early (PREINIT) stage, when we immediately need some writable
  24. * filesystem.
  25. */
  26. static int
  27. start(int argc, char *argv[1])
  28. {
  29. struct volume *root;
  30. struct volume *data = volume_find("rootfs_data");
  31. struct stat s;
  32. if (!getenv("PREINIT") && stat("/tmp/.preinit", &s))
  33. return -1;
  34. if (!data) {
  35. root = volume_find("rootfs");
  36. volume_init(root);
  37. ULOG_NOTE("mounting /dev/root\n");
  38. mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT, 0);
  39. }
  40. /* Check for extroot config in rootfs before even trying rootfs_data */
  41. if (!mount_extroot("")) {
  42. ULOG_NOTE("switched to extroot\n");
  43. return 0;
  44. }
  45. /* There isn't extroot, so just try to mount "rootfs_data" */
  46. volume_init(data);
  47. switch (volume_identify(data)) {
  48. case FS_NONE:
  49. ULOG_WARN("no usable overlay filesystem found, using tmpfs overlay\n");
  50. return ramoverlay();
  51. case FS_DEADCODE:
  52. /*
  53. * Filesystem isn't ready yet and we are in the preinit, so we
  54. * can't afford waiting for it. Use tmpfs for now and handle it
  55. * properly in the "done" call.
  56. */
  57. ULOG_NOTE("jffs2 not ready yet, using temporary tmpfs overlay\n");
  58. return ramoverlay();
  59. case FS_EXT4:
  60. case FS_F2FS:
  61. case FS_JFFS2:
  62. case FS_UBIFS:
  63. mount_overlay(data);
  64. break;
  65. case FS_SNAPSHOT:
  66. mount_snapshot(data);
  67. break;
  68. }
  69. return 0;
  70. }
  71. static int
  72. stop(int argc, char *argv[1])
  73. {
  74. if (!getenv("SHUTDOWN"))
  75. return -1;
  76. return 0;
  77. }
  78. /*
  79. * Called at the end of init, it can wait for filesystem if needed.
  80. */
  81. static int
  82. done(int argc, char *argv[1])
  83. {
  84. struct volume *v = volume_find("rootfs_data");
  85. if (!v)
  86. return -1;
  87. switch (volume_identify(v)) {
  88. case FS_NONE:
  89. case FS_DEADCODE:
  90. return jffs2_switch(v);
  91. case FS_EXT4:
  92. case FS_F2FS:
  93. case FS_JFFS2:
  94. case FS_UBIFS:
  95. fs_state_set("/overlay", FS_STATE_READY);
  96. break;
  97. }
  98. return 0;
  99. }
  100. int main(int argc, char **argv)
  101. {
  102. if (argc < 2)
  103. return start(argc, argv);
  104. if (!strcmp(argv[1], "ram"))
  105. return ramoverlay();
  106. if (!strcmp(argv[1], "stop"))
  107. return stop(argc, argv);
  108. if (!strcmp(argv[1], "done"))
  109. return done(argc, argv);
  110. return -1;
  111. }