mount_root.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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[3])
  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. if (argc < 3)
  38. ULOG_NOTE("mounting /dev/root\n");
  39. else
  40. ULOG_NOTE("mounting /dev/root with options %s\n", argv[2]);
  41. /*
  42. * If present, mount rootfs with passed options.
  43. * Example F2FS filesystem with compress_algorithm option.
  44. */
  45. mount("/dev/root", "/", NULL, MS_NOATIME | MS_REMOUNT,
  46. argc < 3 ? 0 : argv[2]);
  47. }
  48. /* Check for extroot config in rootfs before even trying rootfs_data */
  49. if (!mount_extroot("")) {
  50. ULOG_NOTE("switched to extroot\n");
  51. return 0;
  52. }
  53. /* There isn't extroot, so just try to mount "rootfs_data" */
  54. volume_init(data);
  55. switch (volume_identify(data)) {
  56. case FS_NONE:
  57. ULOG_WARN("no usable overlay filesystem found, using tmpfs overlay\n");
  58. return ramoverlay();
  59. case FS_DEADCODE:
  60. /*
  61. * Filesystem isn't ready yet and we are in the preinit, so we
  62. * can't afford waiting for it. Use tmpfs for now and handle it
  63. * properly in the "done" call.
  64. */
  65. ULOG_NOTE("jffs2 not ready yet, using temporary tmpfs overlay\n");
  66. return ramoverlay();
  67. case FS_EXT4:
  68. case FS_F2FS:
  69. case FS_JFFS2:
  70. case FS_UBIFS:
  71. mount_overlay(data);
  72. break;
  73. case FS_SNAPSHOT:
  74. mount_snapshot(data);
  75. break;
  76. }
  77. return 0;
  78. }
  79. static int
  80. stop(int argc, char *argv[1])
  81. {
  82. if (!getenv("SHUTDOWN"))
  83. return -1;
  84. return 0;
  85. }
  86. /*
  87. * Called at the end of init, it can wait for filesystem if needed.
  88. */
  89. static int
  90. done(int argc, char *argv[1])
  91. {
  92. struct volume *v = volume_find("rootfs_data");
  93. if (!v)
  94. return -1;
  95. switch (volume_identify(v)) {
  96. case FS_NONE:
  97. case FS_DEADCODE:
  98. return jffs2_switch(v);
  99. case FS_EXT4:
  100. case FS_F2FS:
  101. case FS_JFFS2:
  102. case FS_UBIFS:
  103. fs_state_set("/overlay", FS_STATE_READY);
  104. break;
  105. }
  106. return 0;
  107. }
  108. int main(int argc, char **argv)
  109. {
  110. if (argc < 2 || !strcmp(argv[1], "start"))
  111. return start(argc, argv);
  112. if (!strcmp(argv[1], "ram"))
  113. return ramoverlay();
  114. if (!strcmp(argv[1], "stop"))
  115. return stop(argc, argv);
  116. if (!strcmp(argv[1], "done"))
  117. return done(argc, argv);
  118. return -1;
  119. }