grub-mount.c 14 KB

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