2011-04-08 09:31:32 +00:00
|
|
|
/* grub-mount.c - FUSE driver for filesystems that GRUB understands */
|
2011-01-08 18:51:08 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2008,2009,2010 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#define FUSE_USE_VERSION 26
|
|
|
|
#include <config.h>
|
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/emu/misc.h>
|
|
|
|
#include <grub/util/misc.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/device.h>
|
|
|
|
#include <grub/disk.h>
|
|
|
|
#include <grub/file.h>
|
|
|
|
#include <grub/fs.h>
|
|
|
|
#include <grub/env.h>
|
|
|
|
#include <grub/term.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/lib/hexdump.h>
|
|
|
|
#include <grub/crypto.h>
|
|
|
|
#include <grub/command.h>
|
2011-11-11 23:56:20 +00:00
|
|
|
#include <grub/zfs/zfs.h>
|
2011-01-08 18:51:08 +00:00
|
|
|
#include <grub/i18n.h>
|
|
|
|
#include <fuse/fuse.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-01-18 15:43:29 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
|
|
|
|
#pragma GCC diagnostic ignored "-Wmissing-declarations"
|
|
|
|
#include <argp.h>
|
|
|
|
#pragma GCC diagnostic error "-Wmissing-prototypes"
|
|
|
|
#pragma GCC diagnostic error "-Wmissing-declarations"
|
|
|
|
|
2011-01-08 18:51:08 +00:00
|
|
|
#include "progname.h"
|
|
|
|
|
2012-02-10 12:03:21 +00:00
|
|
|
static const char *root = NULL;
|
2011-04-10 20:41:55 +00:00
|
|
|
grub_device_t dev = NULL;
|
|
|
|
grub_fs_t fs = NULL;
|
2011-01-08 18:51:08 +00:00
|
|
|
static char **images = NULL;
|
|
|
|
static char *debug_str = NULL;
|
|
|
|
static char **fuse_args = NULL;
|
|
|
|
static int fuse_argc = 0;
|
|
|
|
static int num_disks = 0;
|
2011-11-11 23:56:20 +00:00
|
|
|
static int mount_crypt = 0;
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
static grub_err_t
|
2012-02-10 12:03:21 +00:00
|
|
|
execute_command (const char *name, int n, char **args)
|
2011-01-08 18:51:08 +00:00
|
|
|
{
|
|
|
|
grub_command_t cmd;
|
|
|
|
|
|
|
|
cmd = grub_command_find (name);
|
|
|
|
if (! cmd)
|
2012-02-12 14:25:25 +00:00
|
|
|
grub_util_error (_("can't find command `%s'"), name);
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
return (cmd->func) (cmd, n, args);
|
|
|
|
}
|
|
|
|
|
2011-04-10 20:41:55 +00:00
|
|
|
/* Translate GRUB error numbers into OS error numbers. Print any unexpected
|
|
|
|
errors. */
|
|
|
|
static int
|
|
|
|
translate_error (void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (grub_errno)
|
|
|
|
{
|
|
|
|
case GRUB_ERR_NONE:
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRUB_ERR_OUT_OF_MEMORY:
|
|
|
|
grub_print_error ();
|
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRUB_ERR_BAD_FILE_TYPE:
|
|
|
|
/* This could also be EISDIR. Take a guess. */
|
|
|
|
ret = -ENOTDIR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRUB_ERR_FILE_NOT_FOUND:
|
|
|
|
ret = -ENOENT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRUB_ERR_FILE_READ_ERROR:
|
|
|
|
case GRUB_ERR_READ_ERROR:
|
|
|
|
case GRUB_ERR_IO:
|
|
|
|
grub_print_error ();
|
|
|
|
ret = -EIO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GRUB_ERR_SYMLINK_LOOP:
|
|
|
|
ret = -ELOOP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
grub_print_error ();
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Any previous errors were handled. */
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Context for fuse_getattr. */
|
|
|
|
struct fuse_getattr_ctx
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
struct grub_dirhook_info file_info;
|
|
|
|
int file_exists;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* A hook for iterating directories. */
|
|
|
|
static int
|
|
|
|
fuse_getattr_find_file (const char *cur_filename,
|
|
|
|
const struct grub_dirhook_info *info, void *data)
|
|
|
|
{
|
|
|
|
struct fuse_getattr_ctx *ctx = data;
|
|
|
|
|
|
|
|
if ((info->case_insensitive ? grub_strcasecmp (cur_filename, ctx->filename)
|
|
|
|
: grub_strcmp (cur_filename, ctx->filename)) == 0)
|
|
|
|
{
|
|
|
|
ctx->file_info = *info;
|
|
|
|
ctx->file_exists = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-08 18:51:08 +00:00
|
|
|
static int
|
|
|
|
fuse_getattr (const char *path, struct stat *st)
|
|
|
|
{
|
2013-01-21 01:33:46 +00:00
|
|
|
struct fuse_getattr_ctx ctx;
|
|
|
|
char *pathname, *path2;
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
if (path[0] == '/' && path[1] == 0)
|
|
|
|
{
|
|
|
|
st->st_dev = 0;
|
|
|
|
st->st_ino = 0;
|
|
|
|
st->st_mode = 0555 | S_IFDIR;
|
|
|
|
st->st_uid = 0;
|
|
|
|
st->st_gid = 0;
|
|
|
|
st->st_rdev = 0;
|
|
|
|
st->st_size = 0;
|
|
|
|
st->st_blksize = 512;
|
|
|
|
st->st_blocks = (st->st_blksize + 511) >> 9;
|
|
|
|
st->st_atime = st->st_mtime = st->st_ctime = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
ctx.file_exists = 0;
|
2011-01-08 18:51:08 +00:00
|
|
|
|
2015-12-29 16:43:05 +00:00
|
|
|
pathname = xstrdup (path);
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
/* Remove trailing '/'. */
|
|
|
|
while (*pathname && pathname[grub_strlen (pathname) - 1] == '/')
|
|
|
|
pathname[grub_strlen (pathname) - 1] = 0;
|
|
|
|
|
|
|
|
/* Split into path and filename. */
|
2013-01-21 01:33:46 +00:00
|
|
|
ctx.filename = grub_strrchr (pathname, '/');
|
|
|
|
if (! ctx.filename)
|
2011-01-08 18:51:08 +00:00
|
|
|
{
|
|
|
|
path2 = grub_strdup ("/");
|
2013-01-21 01:33:46 +00:00
|
|
|
ctx.filename = pathname;
|
2011-01-08 18:51:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-21 01:33:46 +00:00
|
|
|
ctx.filename++;
|
2011-01-08 18:51:08 +00:00
|
|
|
path2 = grub_strdup (pathname);
|
2013-01-21 01:33:46 +00:00
|
|
|
path2[ctx.filename - pathname] = 0;
|
2011-01-08 18:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* It's the whole device. */
|
2013-01-21 01:33:46 +00:00
|
|
|
(fs->dir) (dev, path2, fuse_getattr_find_file, &ctx);
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
grub_free (path2);
|
2013-01-21 01:33:46 +00:00
|
|
|
if (!ctx.file_exists)
|
2011-04-10 20:41:55 +00:00
|
|
|
{
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2011-01-08 18:51:08 +00:00
|
|
|
st->st_dev = 0;
|
|
|
|
st->st_ino = 0;
|
2013-01-21 01:33:46 +00:00
|
|
|
st->st_mode = ctx.file_info.dir ? (0555 | S_IFDIR) : (0444 | S_IFREG);
|
2011-01-08 18:51:08 +00:00
|
|
|
st->st_uid = 0;
|
|
|
|
st->st_gid = 0;
|
|
|
|
st->st_rdev = 0;
|
2013-11-02 19:30:39 +00:00
|
|
|
st->st_size = 0;
|
2013-01-21 01:33:46 +00:00
|
|
|
if (!ctx.file_info.dir)
|
2011-01-08 18:51:08 +00:00
|
|
|
{
|
|
|
|
grub_file_t file;
|
|
|
|
file = grub_file_open (path);
|
2013-11-02 19:30:39 +00:00
|
|
|
if (! file && grub_errno == GRUB_ERR_BAD_FILE_TYPE)
|
|
|
|
{
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
st->st_mode = (0555 | S_IFDIR);
|
|
|
|
}
|
|
|
|
else if (! file)
|
2011-04-10 20:41:55 +00:00
|
|
|
return translate_error ();
|
2013-11-02 19:30:39 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
st->st_size = file->size;
|
|
|
|
grub_file_close (file);
|
|
|
|
}
|
2011-01-08 18:51:08 +00:00
|
|
|
}
|
|
|
|
st->st_blksize = 512;
|
|
|
|
st->st_blocks = (st->st_size + 511) >> 9;
|
2013-01-21 01:33:46 +00:00
|
|
|
st->st_atime = st->st_mtime = st->st_ctime = ctx.file_info.mtimeset
|
|
|
|
? ctx.file_info.mtime : 0;
|
2011-04-10 20:41:55 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
2011-01-08 18:51:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fuse_opendir (const char *path, struct fuse_file_info *fi)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME */
|
|
|
|
static grub_file_t files[65536];
|
|
|
|
static int first_fd = 1;
|
|
|
|
|
|
|
|
static int
|
|
|
|
fuse_open (const char *path, struct fuse_file_info *fi __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
grub_file_t file;
|
|
|
|
file = grub_file_open (path);
|
|
|
|
if (! file)
|
2011-04-10 20:41:55 +00:00
|
|
|
return translate_error ();
|
2011-01-08 18:51:08 +00:00
|
|
|
files[first_fd++] = file;
|
|
|
|
fi->fh = first_fd;
|
|
|
|
files[first_fd++] = file;
|
2011-04-10 20:41:55 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
2011-01-08 18:51:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fuse_read (const char *path, char *buf, size_t sz, off_t off,
|
|
|
|
struct fuse_file_info *fi)
|
|
|
|
{
|
|
|
|
grub_file_t file = files[fi->fh];
|
2011-04-10 20:41:55 +00:00
|
|
|
grub_ssize_t size;
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
if (off > file->size)
|
2011-04-10 20:41:55 +00:00
|
|
|
return -EINVAL;
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
file->offset = off;
|
|
|
|
|
2011-04-10 20:41:55 +00:00
|
|
|
size = grub_file_read (file, buf, sz);
|
|
|
|
if (size < 0)
|
|
|
|
return translate_error ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
return size;
|
|
|
|
}
|
2011-01-08 18:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fuse_release (const char *path, struct fuse_file_info *fi)
|
|
|
|
{
|
|
|
|
grub_file_close (files[fi->fh]);
|
|
|
|
files[fi->fh] = NULL;
|
2011-04-10 20:41:55 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
2011-01-08 18:51:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
/* Context for fuse_readdir. */
|
|
|
|
struct fuse_readdir_ctx
|
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
void *buf;
|
|
|
|
fuse_fill_dir_t fill;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Helper for fuse_readdir. */
|
|
|
|
static int
|
|
|
|
fuse_readdir_call_fill (const char *filename,
|
|
|
|
const struct grub_dirhook_info *info, void *data)
|
|
|
|
{
|
|
|
|
struct fuse_readdir_ctx *ctx = data;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
grub_memset (&st, 0, sizeof (st));
|
|
|
|
st.st_mode = info->dir ? (0555 | S_IFDIR) : (0444 | S_IFREG);
|
|
|
|
if (!info->dir)
|
|
|
|
{
|
|
|
|
grub_file_t file;
|
|
|
|
char *tmp;
|
|
|
|
tmp = xasprintf ("%s/%s", ctx->path, filename);
|
|
|
|
file = grub_file_open (tmp);
|
|
|
|
free (tmp);
|
2013-11-02 19:30:39 +00:00
|
|
|
/* Symlink to directory. */
|
|
|
|
if (! file && grub_errno == GRUB_ERR_BAD_FILE_TYPE)
|
|
|
|
{
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
st.st_mode = (0555 | S_IFDIR);
|
|
|
|
}
|
|
|
|
else if (!file)
|
|
|
|
{
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
st.st_size = file->size;
|
|
|
|
grub_file_close (file);
|
|
|
|
}
|
2013-01-21 01:33:46 +00:00
|
|
|
}
|
|
|
|
st.st_blksize = 512;
|
|
|
|
st.st_blocks = (st.st_size + 511) >> 9;
|
|
|
|
st.st_atime = st.st_mtime = st.st_ctime
|
|
|
|
= info->mtimeset ? info->mtime : 0;
|
|
|
|
ctx->fill (ctx->buf, filename, &st, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-08 18:51:08 +00:00
|
|
|
static int
|
|
|
|
fuse_readdir (const char *path, void *buf,
|
|
|
|
fuse_fill_dir_t fill, off_t off, struct fuse_file_info *fi)
|
|
|
|
{
|
2013-01-21 01:33:46 +00:00
|
|
|
struct fuse_readdir_ctx ctx = {
|
|
|
|
.path = path,
|
|
|
|
.buf = buf,
|
|
|
|
.fill = fill
|
|
|
|
};
|
2011-01-08 18:51:08 +00:00
|
|
|
char *pathname;
|
|
|
|
|
|
|
|
pathname = xstrdup (path);
|
|
|
|
|
|
|
|
/* Remove trailing '/'. */
|
|
|
|
while (pathname [0] && pathname[1]
|
|
|
|
&& pathname[grub_strlen (pathname) - 1] == '/')
|
|
|
|
pathname[grub_strlen (pathname) - 1] = 0;
|
|
|
|
|
2013-01-21 01:33:46 +00:00
|
|
|
(fs->dir) (dev, pathname, fuse_readdir_call_fill, &ctx);
|
2011-01-08 18:51:08 +00:00
|
|
|
free (pathname);
|
2011-04-10 20:41:55 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
2011-01-08 18:51:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fuse_operations grub_opers = {
|
|
|
|
.getattr = fuse_getattr,
|
|
|
|
.open = fuse_open,
|
|
|
|
.release = fuse_release,
|
|
|
|
.opendir = fuse_opendir,
|
|
|
|
.readdir = fuse_readdir,
|
|
|
|
.read = fuse_read
|
|
|
|
};
|
|
|
|
|
2011-04-10 20:41:55 +00:00
|
|
|
static grub_err_t
|
2011-01-08 18:51:08 +00:00
|
|
|
fuse_init (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_disks; i++)
|
|
|
|
{
|
|
|
|
char *argv[2];
|
|
|
|
char *host_file;
|
|
|
|
char *loop_name;
|
|
|
|
loop_name = grub_xasprintf ("loop%d", i);
|
|
|
|
if (!loop_name)
|
2012-02-10 12:03:21 +00:00
|
|
|
grub_util_error ("%s", grub_errmsg);
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
host_file = grub_xasprintf ("(host)%s", images[i]);
|
|
|
|
if (!host_file)
|
2012-02-10 12:03:21 +00:00
|
|
|
grub_util_error ("%s", grub_errmsg);
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
argv[0] = loop_name;
|
|
|
|
argv[1] = host_file;
|
|
|
|
|
|
|
|
if (execute_command ("loopback", 2, argv))
|
2012-03-05 15:39:16 +00:00
|
|
|
grub_util_error (_("`loopback' command fails: %s"), grub_errmsg);
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
grub_free (loop_name);
|
|
|
|
grub_free (host_file);
|
|
|
|
}
|
|
|
|
|
2011-11-11 23:56:20 +00:00
|
|
|
if (mount_crypt)
|
|
|
|
{
|
2012-02-10 13:12:43 +00:00
|
|
|
char *argv[2] = { xstrdup ("-a"), NULL};
|
2011-11-11 23:56:20 +00:00
|
|
|
if (execute_command ("cryptomount", 1, argv))
|
2012-03-05 15:39:16 +00:00
|
|
|
grub_util_error (_("`cryptomount' command fails: %s"),
|
|
|
|
grub_errmsg);
|
2012-02-10 13:12:43 +00:00
|
|
|
free (argv[0]);
|
2011-11-11 23:56:20 +00:00
|
|
|
}
|
|
|
|
|
2011-01-08 18:51:08 +00:00
|
|
|
grub_lvm_fini ();
|
|
|
|
grub_mdraid09_fini ();
|
|
|
|
grub_mdraid1x_fini ();
|
2012-01-29 13:28:01 +00:00
|
|
|
grub_diskfilter_fini ();
|
|
|
|
grub_diskfilter_init ();
|
2011-01-08 18:51:08 +00:00
|
|
|
grub_mdraid09_init ();
|
|
|
|
grub_mdraid1x_init ();
|
|
|
|
grub_lvm_init ();
|
|
|
|
|
2011-04-10 20:41:55 +00:00
|
|
|
dev = grub_device_open (0);
|
|
|
|
if (! dev)
|
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
fs = grub_fs_probe (dev);
|
|
|
|
if (! fs)
|
|
|
|
{
|
|
|
|
grub_device_close (dev);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
2013-04-03 09:28:16 +00:00
|
|
|
if (fuse_main (fuse_argc, fuse_args, &grub_opers, NULL))
|
|
|
|
grub_error (GRUB_ERR_IO, "fuse_main failed");
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
for (i = 0; i < num_disks; i++)
|
|
|
|
{
|
|
|
|
char *argv[2];
|
|
|
|
char *loop_name;
|
|
|
|
|
|
|
|
loop_name = grub_xasprintf ("loop%d", i);
|
|
|
|
if (!loop_name)
|
2012-02-10 12:03:21 +00:00
|
|
|
grub_util_error ("%s", grub_errmsg);
|
2011-01-08 18:51:08 +00:00
|
|
|
|
2012-02-10 13:12:43 +00:00
|
|
|
argv[0] = xstrdup ("-d");
|
2011-01-08 18:51:08 +00:00
|
|
|
argv[1] = loop_name;
|
|
|
|
|
|
|
|
execute_command ("loopback", 2, argv);
|
|
|
|
|
2012-02-10 13:12:43 +00:00
|
|
|
grub_free (argv[0]);
|
2011-01-08 18:51:08 +00:00
|
|
|
grub_free (loop_name);
|
|
|
|
}
|
2011-04-10 20:41:55 +00:00
|
|
|
|
2013-04-03 09:28:16 +00:00
|
|
|
return grub_errno;
|
2011-01-08 18:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct argp_option options[] = {
|
|
|
|
{"root", 'r', N_("DEVICE_NAME"), 0, N_("Set root device."), 2},
|
2012-03-08 18:09:05 +00:00
|
|
|
{"debug", 'd', N_("STRING"), 0, N_("Set debug environment variable."), 2},
|
2012-03-06 10:38:50 +00:00
|
|
|
{"crypto", 'C', NULL, 0, N_("Mount crypto devices."), 2},
|
2012-02-08 18:26:01 +00:00
|
|
|
{"zfs-key", 'K',
|
|
|
|
/* TRANSLATORS: "prompt" is a keyword. */
|
|
|
|
N_("FILE|prompt"), 0, N_("Load zfs crypto key."), 2},
|
2012-03-06 10:38:50 +00:00
|
|
|
{"verbose", 'v', NULL, 0, N_("print verbose messages."), 2},
|
2011-01-08 18:51:08 +00:00
|
|
|
{0, 0, 0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Print the version information. */
|
|
|
|
static void
|
|
|
|
print_version (FILE *stream, struct argp_state *state)
|
|
|
|
{
|
|
|
|
fprintf (stream, "%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
|
|
|
|
}
|
|
|
|
void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
|
|
|
|
|
2012-02-10 12:03:21 +00:00
|
|
|
static error_t
|
2011-01-08 18:51:08 +00:00
|
|
|
argp_parser (int key, char *arg, struct argp_state *state)
|
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case 'r':
|
|
|
|
root = arg;
|
|
|
|
return 0;
|
|
|
|
|
2011-11-11 23:56:20 +00:00
|
|
|
case 'K':
|
|
|
|
if (strcmp (arg, "prompt") == 0)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
2011-11-12 00:19:06 +00:00
|
|
|
grub_printf ("%s", _("Enter ZFS password: "));
|
2011-11-11 23:56:20 +00:00
|
|
|
if (grub_password_get (buf, 1023))
|
|
|
|
{
|
|
|
|
grub_zfs_add_key ((grub_uint8_t *) buf, grub_strlen (buf), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
ssize_t real_size;
|
|
|
|
grub_uint8_t buf[1024];
|
2013-10-13 18:36:28 +00:00
|
|
|
f = grub_util_fopen (arg, "rb");
|
2011-11-11 23:56:20 +00:00
|
|
|
if (!f)
|
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
printf (_("%s: error:"), program_name);
|
|
|
|
printf (_("cannot open `%s': %s"), arg, strerror (errno));
|
|
|
|
printf ("\n");
|
2011-11-11 23:56:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
real_size = fread (buf, 1, 1024, f);
|
|
|
|
if (real_size < 0)
|
|
|
|
{
|
2012-02-08 18:26:01 +00:00
|
|
|
printf (_("%s: error:"), program_name);
|
|
|
|
printf (_("cannot read `%s': %s"), arg,
|
|
|
|
strerror (errno));
|
|
|
|
printf ("\n");
|
2011-11-11 23:56:20 +00:00
|
|
|
fclose (f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
grub_zfs_add_key (buf, real_size, 0);
|
2015-01-26 19:24:28 +00:00
|
|
|
fclose (f);
|
2011-11-11 23:56:20 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
mount_crypt = 1;
|
|
|
|
return 0;
|
|
|
|
|
2011-01-08 18:51:08 +00:00
|
|
|
case 'd':
|
|
|
|
debug_str = arg;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
verbosity++;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ARGP_KEY_ARG:
|
|
|
|
if (arg[0] != '-')
|
|
|
|
break;
|
|
|
|
|
2017-04-04 16:23:55 +00:00
|
|
|
/* FALLTHROUGH */
|
2011-01-08 18:51:08 +00:00
|
|
|
default:
|
|
|
|
if (!arg)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fuse_args = xrealloc (fuse_args, (fuse_argc + 1) * sizeof (fuse_args[0]));
|
|
|
|
fuse_args[fuse_argc] = xstrdup (arg);
|
|
|
|
fuse_argc++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
images = xrealloc (images, (num_disks + 1) * sizeof (images[0]));
|
2015-03-04 00:00:19 +00:00
|
|
|
images[num_disks] = grub_canonicalize_file_name (arg);
|
2011-01-08 18:51:08 +00:00
|
|
|
num_disks++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct argp argp = {
|
|
|
|
options, argp_parser, N_("IMAGE1 [IMAGE2 ...] MOUNTPOINT"),
|
|
|
|
N_("Debug tool for filesystem driver."),
|
|
|
|
NULL, NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2012-02-10 12:03:21 +00:00
|
|
|
const char *default_root;
|
|
|
|
char *alloc_root;
|
2011-01-08 18:51:08 +00:00
|
|
|
|
2013-10-13 18:03:42 +00:00
|
|
|
grub_util_host_init (&argc, &argv);
|
2011-01-08 18:51:08 +00:00
|
|
|
|
2011-04-14 19:27:27 +00:00
|
|
|
fuse_args = xrealloc (fuse_args, (fuse_argc + 2) * sizeof (fuse_args[0]));
|
2011-01-08 18:51:08 +00:00
|
|
|
fuse_args[fuse_argc] = xstrdup (argv[0]);
|
|
|
|
fuse_argc++;
|
2011-04-14 19:27:27 +00:00
|
|
|
/* Run single-threaded. */
|
|
|
|
fuse_args[fuse_argc] = xstrdup ("-s");
|
|
|
|
fuse_argc++;
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
argp_parse (&argp, argc, argv, 0, 0, 0);
|
|
|
|
|
|
|
|
if (num_disks < 2)
|
2012-02-10 12:03:21 +00:00
|
|
|
grub_util_error ("%s", _("need an image and mountpoint"));
|
2011-01-08 18:51:08 +00:00
|
|
|
fuse_args = xrealloc (fuse_args, (fuse_argc + 2) * sizeof (fuse_args[0]));
|
|
|
|
fuse_args[fuse_argc] = images[num_disks - 1];
|
|
|
|
fuse_argc++;
|
|
|
|
num_disks--;
|
|
|
|
fuse_args[fuse_argc] = NULL;
|
|
|
|
|
|
|
|
/* Initialize all modules. */
|
|
|
|
grub_init_all ();
|
|
|
|
|
|
|
|
if (debug_str)
|
|
|
|
grub_env_set ("debug", debug_str);
|
|
|
|
|
|
|
|
default_root = (num_disks == 1) ? "loop0" : "md0";
|
|
|
|
alloc_root = 0;
|
|
|
|
if (root)
|
|
|
|
{
|
|
|
|
if ((*root >= '0') && (*root <= '9'))
|
|
|
|
{
|
|
|
|
alloc_root = xmalloc (strlen (default_root) + strlen (root) + 2);
|
|
|
|
|
|
|
|
sprintf (alloc_root, "%s,%s", default_root, root);
|
|
|
|
root = alloc_root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
root = default_root;
|
|
|
|
|
|
|
|
grub_env_set ("root", root);
|
|
|
|
|
|
|
|
if (alloc_root)
|
|
|
|
free (alloc_root);
|
|
|
|
|
|
|
|
/* Do it. */
|
|
|
|
fuse_init ();
|
2011-04-10 20:41:55 +00:00
|
|
|
if (grub_errno)
|
|
|
|
{
|
|
|
|
grub_print_error ();
|
|
|
|
return 1;
|
|
|
|
}
|
2011-01-08 18:51:08 +00:00
|
|
|
|
|
|
|
/* Free resources. */
|
|
|
|
grub_fini_all ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|