grub-mount.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /* grub-mount.c - FUSE driver for filesystems that GRUB understands */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define FUSE_USE_VERSION 26
  20. #include <config.h>
  21. #include <grub/types.h>
  22. #include <grub/emu/misc.h>
  23. #include <grub/util/misc.h>
  24. #include <grub/misc.h>
  25. #include <grub/device.h>
  26. #include <grub/disk.h>
  27. #include <grub/file.h>
  28. #include <grub/fs.h>
  29. #include <grub/env.h>
  30. #include <grub/term.h>
  31. #include <grub/mm.h>
  32. #include <grub/lib/hexdump.h>
  33. #include <grub/crypto.h>
  34. #include <grub/command.h>
  35. #include <grub/zfs/zfs.h>
  36. #include <grub/i18n.h>
  37. #include <fuse/fuse.h>
  38. #include <stdio.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. #include <stdlib.h>
  42. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  43. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  44. #include <argp.h>
  45. #pragma GCC diagnostic error "-Wmissing-prototypes"
  46. #pragma GCC diagnostic error "-Wmissing-declarations"
  47. #include "progname.h"
  48. static const char *root = NULL;
  49. grub_device_t dev = NULL;
  50. grub_fs_t fs = NULL;
  51. static char **images = NULL;
  52. static char *debug_str = NULL;
  53. static char **fuse_args = NULL;
  54. static int fuse_argc = 0;
  55. static int num_disks = 0;
  56. static int mount_crypt = 0;
  57. static grub_err_t
  58. execute_command (const char *name, int n, char **args)
  59. {
  60. grub_command_t cmd;
  61. cmd = grub_command_find (name);
  62. if (! cmd)
  63. grub_util_error (_("can't find command `%s'"), name);
  64. return (cmd->func) (cmd, n, args);
  65. }
  66. /* Translate GRUB error numbers into OS error numbers. Print any unexpected
  67. errors. */
  68. static int
  69. translate_error (void)
  70. {
  71. int ret;
  72. switch (grub_errno)
  73. {
  74. case GRUB_ERR_NONE:
  75. ret = 0;
  76. break;
  77. case GRUB_ERR_OUT_OF_MEMORY:
  78. grub_print_error ();
  79. ret = -ENOMEM;
  80. break;
  81. case GRUB_ERR_BAD_FILE_TYPE:
  82. /* This could also be EISDIR. Take a guess. */
  83. ret = -ENOTDIR;
  84. break;
  85. case GRUB_ERR_FILE_NOT_FOUND:
  86. ret = -ENOENT;
  87. break;
  88. case GRUB_ERR_FILE_READ_ERROR:
  89. case GRUB_ERR_READ_ERROR:
  90. case GRUB_ERR_IO:
  91. grub_print_error ();
  92. ret = -EIO;
  93. break;
  94. case GRUB_ERR_SYMLINK_LOOP:
  95. ret = -ELOOP;
  96. break;
  97. default:
  98. grub_print_error ();
  99. ret = -EINVAL;
  100. break;
  101. }
  102. /* Any previous errors were handled. */
  103. grub_errno = GRUB_ERR_NONE;
  104. return ret;
  105. }
  106. /* Context for fuse_getattr. */
  107. struct fuse_getattr_ctx
  108. {
  109. char *filename;
  110. struct grub_dirhook_info file_info;
  111. int file_exists;
  112. };
  113. /* A hook for iterating directories. */
  114. static int
  115. fuse_getattr_find_file (const char *cur_filename,
  116. const struct grub_dirhook_info *info, void *data)
  117. {
  118. struct fuse_getattr_ctx *ctx = data;
  119. if ((info->case_insensitive ? grub_strcasecmp (cur_filename, ctx->filename)
  120. : grub_strcmp (cur_filename, ctx->filename)) == 0)
  121. {
  122. ctx->file_info = *info;
  123. ctx->file_exists = 1;
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. static int
  129. fuse_getattr (const char *path, struct stat *st)
  130. {
  131. struct fuse_getattr_ctx ctx;
  132. char *pathname, *path2;
  133. if (path[0] == '/' && path[1] == 0)
  134. {
  135. st->st_dev = 0;
  136. st->st_ino = 0;
  137. st->st_mode = 0555 | S_IFDIR;
  138. st->st_uid = 0;
  139. st->st_gid = 0;
  140. st->st_rdev = 0;
  141. st->st_size = 0;
  142. st->st_blksize = 512;
  143. st->st_blocks = (st->st_blksize + 511) >> 9;
  144. st->st_atime = st->st_mtime = st->st_ctime = 0;
  145. return 0;
  146. }
  147. ctx.file_exists = 0;
  148. pathname = xstrdup (path);
  149. /* Remove trailing '/'. */
  150. while (*pathname && pathname[grub_strlen (pathname) - 1] == '/')
  151. pathname[grub_strlen (pathname) - 1] = 0;
  152. /* Split into path and filename. */
  153. ctx.filename = grub_strrchr (pathname, '/');
  154. if (! ctx.filename)
  155. {
  156. path2 = grub_strdup ("/");
  157. ctx.filename = pathname;
  158. }
  159. else
  160. {
  161. ctx.filename++;
  162. path2 = grub_strdup (pathname);
  163. path2[ctx.filename - pathname] = 0;
  164. }
  165. /* It's the whole device. */
  166. (fs->fs_dir) (dev, path2, fuse_getattr_find_file, &ctx);
  167. grub_free (path2);
  168. if (!ctx.file_exists)
  169. {
  170. grub_errno = GRUB_ERR_NONE;
  171. return -ENOENT;
  172. }
  173. st->st_dev = 0;
  174. st->st_ino = 0;
  175. st->st_mode = ctx.file_info.dir ? (0555 | S_IFDIR) : (0444 | S_IFREG);
  176. st->st_uid = 0;
  177. st->st_gid = 0;
  178. st->st_rdev = 0;
  179. st->st_size = 0;
  180. if (!ctx.file_info.dir)
  181. {
  182. grub_file_t file;
  183. file = grub_file_open (path, GRUB_FILE_TYPE_GET_SIZE);
  184. if (! file && grub_errno == GRUB_ERR_BAD_FILE_TYPE)
  185. {
  186. grub_errno = GRUB_ERR_NONE;
  187. st->st_mode = (0555 | S_IFDIR);
  188. }
  189. else if (! file)
  190. return translate_error ();
  191. else
  192. {
  193. st->st_size = file->size;
  194. grub_file_close (file);
  195. }
  196. }
  197. st->st_blksize = 512;
  198. st->st_blocks = (st->st_size + 511) >> 9;
  199. st->st_atime = st->st_mtime = st->st_ctime = ctx.file_info.mtimeset
  200. ? ctx.file_info.mtime : 0;
  201. grub_errno = GRUB_ERR_NONE;
  202. return 0;
  203. }
  204. static int
  205. fuse_opendir (const char *path, struct fuse_file_info *fi)
  206. {
  207. return 0;
  208. }
  209. /* FIXME */
  210. static grub_file_t files[65536];
  211. static int first_fd = 1;
  212. static int
  213. fuse_open (const char *path, struct fuse_file_info *fi __attribute__ ((unused)))
  214. {
  215. grub_file_t file;
  216. file = grub_file_open (path, GRUB_FILE_TYPE_MOUNT);
  217. if (! file)
  218. return translate_error ();
  219. files[first_fd++] = file;
  220. fi->fh = first_fd;
  221. files[first_fd++] = file;
  222. grub_errno = GRUB_ERR_NONE;
  223. return 0;
  224. }
  225. static int
  226. fuse_read (const char *path, char *buf, size_t sz, off_t off,
  227. struct fuse_file_info *fi)
  228. {
  229. grub_file_t file = files[fi->fh];
  230. grub_ssize_t size;
  231. if (off > file->size)
  232. return -EINVAL;
  233. file->offset = off;
  234. size = grub_file_read (file, buf, sz);
  235. if (size < 0)
  236. return translate_error ();
  237. else
  238. {
  239. grub_errno = GRUB_ERR_NONE;
  240. return size;
  241. }
  242. }
  243. static int
  244. fuse_release (const char *path, struct fuse_file_info *fi)
  245. {
  246. grub_file_close (files[fi->fh]);
  247. files[fi->fh] = NULL;
  248. grub_errno = GRUB_ERR_NONE;
  249. return 0;
  250. }
  251. /* Context for fuse_readdir. */
  252. struct fuse_readdir_ctx
  253. {
  254. const char *path;
  255. void *buf;
  256. fuse_fill_dir_t fill;
  257. };
  258. /* Helper for fuse_readdir. */
  259. static int
  260. fuse_readdir_call_fill (const char *filename,
  261. const struct grub_dirhook_info *info, void *data)
  262. {
  263. struct fuse_readdir_ctx *ctx = data;
  264. struct stat st;
  265. grub_memset (&st, 0, sizeof (st));
  266. st.st_mode = info->dir ? (0555 | S_IFDIR) : (0444 | S_IFREG);
  267. if (!info->dir)
  268. {
  269. grub_file_t file;
  270. char *tmp;
  271. tmp = xasprintf ("%s/%s", ctx->path, filename);
  272. file = grub_file_open (tmp, GRUB_FILE_TYPE_GET_SIZE);
  273. free (tmp);
  274. /* Symlink to directory. */
  275. if (! file && grub_errno == GRUB_ERR_BAD_FILE_TYPE)
  276. {
  277. grub_errno = GRUB_ERR_NONE;
  278. st.st_mode = (0555 | S_IFDIR);
  279. }
  280. else if (!file)
  281. {
  282. grub_errno = GRUB_ERR_NONE;
  283. }
  284. else
  285. {
  286. st.st_size = file->size;
  287. grub_file_close (file);
  288. }
  289. }
  290. st.st_blksize = 512;
  291. st.st_blocks = (st.st_size + 511) >> 9;
  292. st.st_atime = st.st_mtime = st.st_ctime
  293. = info->mtimeset ? info->mtime : 0;
  294. ctx->fill (ctx->buf, filename, &st, 0);
  295. return 0;
  296. }
  297. static int
  298. fuse_readdir (const char *path, void *buf,
  299. fuse_fill_dir_t fill, off_t off, struct fuse_file_info *fi)
  300. {
  301. struct fuse_readdir_ctx ctx = {
  302. .path = path,
  303. .buf = buf,
  304. .fill = fill
  305. };
  306. char *pathname;
  307. pathname = xstrdup (path);
  308. /* Remove trailing '/'. */
  309. while (pathname [0] && pathname[1]
  310. && pathname[grub_strlen (pathname) - 1] == '/')
  311. pathname[grub_strlen (pathname) - 1] = 0;
  312. (fs->fs_dir) (dev, pathname, fuse_readdir_call_fill, &ctx);
  313. free (pathname);
  314. grub_errno = GRUB_ERR_NONE;
  315. return 0;
  316. }
  317. struct fuse_operations grub_opers = {
  318. .getattr = fuse_getattr,
  319. .open = fuse_open,
  320. .release = fuse_release,
  321. .opendir = fuse_opendir,
  322. .readdir = fuse_readdir,
  323. .read = fuse_read
  324. };
  325. static grub_err_t
  326. fuse_init (void)
  327. {
  328. int i;
  329. for (i = 0; i < num_disks; i++)
  330. {
  331. char *argv[2];
  332. char *host_file;
  333. char *loop_name;
  334. loop_name = grub_xasprintf ("loop%d", i);
  335. if (!loop_name)
  336. grub_util_error ("%s", grub_errmsg);
  337. host_file = grub_xasprintf ("(host)%s", images[i]);
  338. if (!host_file)
  339. grub_util_error ("%s", grub_errmsg);
  340. argv[0] = loop_name;
  341. argv[1] = host_file;
  342. if (execute_command ("loopback", 2, argv))
  343. grub_util_error (_("`loopback' command fails: %s"), grub_errmsg);
  344. grub_free (loop_name);
  345. grub_free (host_file);
  346. }
  347. if (mount_crypt)
  348. {
  349. char *argv[2] = { xstrdup ("-a"), NULL};
  350. if (execute_command ("cryptomount", 1, argv))
  351. grub_util_error (_("`cryptomount' command fails: %s"),
  352. grub_errmsg);
  353. free (argv[0]);
  354. }
  355. grub_lvm_fini ();
  356. grub_mdraid09_fini ();
  357. grub_mdraid1x_fini ();
  358. grub_diskfilter_fini ();
  359. grub_diskfilter_init ();
  360. grub_mdraid09_init ();
  361. grub_mdraid1x_init ();
  362. grub_lvm_init ();
  363. dev = grub_device_open (0);
  364. if (! dev)
  365. return grub_errno;
  366. fs = grub_fs_probe (dev);
  367. if (! fs)
  368. {
  369. grub_device_close (dev);
  370. return grub_errno;
  371. }
  372. if (fuse_main (fuse_argc, fuse_args, &grub_opers, NULL))
  373. grub_error (GRUB_ERR_IO, "fuse_main failed");
  374. for (i = 0; i < num_disks; i++)
  375. {
  376. char *argv[2];
  377. char *loop_name;
  378. loop_name = grub_xasprintf ("loop%d", i);
  379. if (!loop_name)
  380. grub_util_error ("%s", grub_errmsg);
  381. argv[0] = xstrdup ("-d");
  382. argv[1] = loop_name;
  383. execute_command ("loopback", 2, argv);
  384. grub_free (argv[0]);
  385. grub_free (loop_name);
  386. }
  387. return grub_errno;
  388. }
  389. static struct argp_option options[] = {
  390. {"root", 'r', N_("DEVICE_NAME"), 0, N_("Set root device."), 2},
  391. {"debug", 'd', N_("STRING"), 0, N_("Set debug environment variable."), 2},
  392. {"crypto", 'C', NULL, 0, N_("Mount crypto devices."), 2},
  393. {"zfs-key", 'K',
  394. /* TRANSLATORS: "prompt" is a keyword. */
  395. N_("FILE|prompt"), 0, N_("Load zfs crypto key."), 2},
  396. {"verbose", 'v', NULL, 0, N_("print verbose messages."), 2},
  397. {0, 0, 0, 0, 0, 0}
  398. };
  399. /* Print the version information. */
  400. static void
  401. print_version (FILE *stream, struct argp_state *state)
  402. {
  403. fprintf (stream, "%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
  404. }
  405. void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
  406. static error_t
  407. argp_parser (int key, char *arg, struct argp_state *state)
  408. {
  409. switch (key)
  410. {
  411. case 'r':
  412. root = arg;
  413. return 0;
  414. case 'K':
  415. if (strcmp (arg, "prompt") == 0)
  416. {
  417. char buf[1024];
  418. grub_printf ("%s", _("Enter ZFS password: "));
  419. if (grub_password_get (buf, 1023))
  420. {
  421. grub_zfs_add_key ((grub_uint8_t *) buf, grub_strlen (buf), 1);
  422. }
  423. }
  424. else
  425. {
  426. FILE *f;
  427. ssize_t real_size;
  428. grub_uint8_t buf[1024];
  429. f = grub_util_fopen (arg, "rb");
  430. if (!f)
  431. {
  432. printf (_("%s: error:"), program_name);
  433. printf (_("cannot open `%s': %s"), arg, strerror (errno));
  434. printf ("\n");
  435. return 0;
  436. }
  437. real_size = fread (buf, 1, 1024, f);
  438. if (real_size < 0)
  439. {
  440. printf (_("%s: error:"), program_name);
  441. printf (_("cannot read `%s': %s"), arg,
  442. strerror (errno));
  443. printf ("\n");
  444. fclose (f);
  445. return 0;
  446. }
  447. grub_zfs_add_key (buf, real_size, 0);
  448. fclose (f);
  449. }
  450. return 0;
  451. case 'C':
  452. mount_crypt = 1;
  453. return 0;
  454. case 'd':
  455. debug_str = arg;
  456. return 0;
  457. case 'v':
  458. verbosity++;
  459. return 0;
  460. case ARGP_KEY_ARG:
  461. if (arg[0] != '-')
  462. break;
  463. /* FALLTHROUGH */
  464. default:
  465. if (!arg)
  466. return 0;
  467. fuse_args = xrealloc (fuse_args, (fuse_argc + 1) * sizeof (fuse_args[0]));
  468. fuse_args[fuse_argc] = xstrdup (arg);
  469. fuse_argc++;
  470. return 0;
  471. }
  472. images = xrealloc (images, (num_disks + 1) * sizeof (images[0]));
  473. images[num_disks] = grub_canonicalize_file_name (arg);
  474. num_disks++;
  475. return 0;
  476. }
  477. struct argp argp = {
  478. options, argp_parser, N_("IMAGE1 [IMAGE2 ...] MOUNTPOINT"),
  479. N_("Debug tool for filesystem driver."),
  480. NULL, NULL, NULL
  481. };
  482. int
  483. main (int argc, char *argv[])
  484. {
  485. const char *default_root;
  486. char *alloc_root;
  487. grub_util_host_init (&argc, &argv);
  488. fuse_args = xrealloc (fuse_args, (fuse_argc + 2) * sizeof (fuse_args[0]));
  489. fuse_args[fuse_argc] = xstrdup (argv[0]);
  490. fuse_argc++;
  491. /* Run single-threaded. */
  492. fuse_args[fuse_argc] = xstrdup ("-s");
  493. fuse_argc++;
  494. argp_parse (&argp, argc, argv, 0, 0, 0);
  495. if (num_disks < 2)
  496. grub_util_error ("%s", _("need an image and mountpoint"));
  497. fuse_args = xrealloc (fuse_args, (fuse_argc + 2) * sizeof (fuse_args[0]));
  498. fuse_args[fuse_argc] = images[num_disks - 1];
  499. fuse_argc++;
  500. num_disks--;
  501. fuse_args[fuse_argc] = NULL;
  502. /* Initialize all modules. */
  503. grub_init_all ();
  504. if (debug_str)
  505. grub_env_set ("debug", debug_str);
  506. default_root = (num_disks == 1) ? "loop0" : "md0";
  507. alloc_root = 0;
  508. if (root)
  509. {
  510. if ((*root >= '0') && (*root <= '9'))
  511. {
  512. alloc_root = xmalloc (strlen (default_root) + strlen (root) + 2);
  513. sprintf (alloc_root, "%s,%s", default_root, root);
  514. root = alloc_root;
  515. }
  516. }
  517. else
  518. root = default_root;
  519. grub_env_set ("root", root);
  520. if (alloc_root)
  521. free (alloc_root);
  522. /* Do it. */
  523. fuse_init ();
  524. if (grub_errno)
  525. {
  526. grub_print_error ();
  527. return 1;
  528. }
  529. /* Free resources. */
  530. grub_fini_all ();
  531. return 0;
  532. }