|
|
5593c8 |
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
5593c8 |
From: Michael Chang <mchang@suse.com>
|
|
|
5593c8 |
Date: Tue, 9 Jul 2019 13:56:16 +0200
|
|
|
5593c8 |
Subject: [PATCH] grub2-btrfs-06-subvol-mount
|
|
|
5593c8 |
|
|
|
5593c8 |
---
|
|
|
5593c8 |
grub-core/fs/btrfs.c | 195 +++++++++++++++++++++++++++++++++++++++-
|
|
|
5593c8 |
grub-core/osdep/linux/getroot.c | 148 +++++++++++++++++++++++++++++-
|
|
|
5593c8 |
util/grub-install.c | 49 ++++++++++
|
|
|
5593c8 |
include/grub/emu/getroot.h | 5 ++
|
|
|
5593c8 |
4 files changed, 392 insertions(+), 5 deletions(-)
|
|
|
5593c8 |
|
|
|
5593c8 |
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
|
|
|
5593c8 |
index 113c1f746c9..d323746ecfa 100644
|
|
|
5593c8 |
--- a/grub-core/fs/btrfs.c
|
|
|
5593c8 |
+++ b/grub-core/fs/btrfs.c
|
|
|
5593c8 |
@@ -41,6 +41,7 @@
|
|
|
5593c8 |
#include <grub/command.h>
|
|
|
5593c8 |
#include <grub/env.h>
|
|
|
5593c8 |
#include <grub/extcmd.h>
|
|
|
5593c8 |
+#include <grub/list.h>
|
|
|
5593c8 |
#include <grub/crypto.h>
|
|
|
5593c8 |
#include <grub/diskfilter.h>
|
|
|
5593c8 |
#include <grub/safemath.h>
|
|
|
5593c8 |
@@ -266,6 +267,12 @@ static grub_err_t
|
|
|
5593c8 |
grub_btrfs_read_logical (struct grub_btrfs_data *data,
|
|
|
5593c8 |
grub_disk_addr_t addr, void *buf, grub_size_t size,
|
|
|
5593c8 |
int recursion_depth);
|
|
|
5593c8 |
+static grub_err_t
|
|
|
5593c8 |
+get_root (struct grub_btrfs_data *data, struct grub_btrfs_key *key,
|
|
|
5593c8 |
+ grub_uint64_t *tree, grub_uint8_t *type);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+grub_uint64_t
|
|
|
5593c8 |
+find_mtab_subvol_tree (const char *path, char **path_in_subvol);
|
|
|
5593c8 |
|
|
|
5593c8 |
static grub_err_t
|
|
|
5593c8 |
read_sblock (grub_disk_t disk, struct grub_btrfs_superblock *sb)
|
|
|
5593c8 |
@@ -1223,9 +1230,26 @@ lookup_root_by_name(struct grub_btrfs_data *data, const char *path)
|
|
|
5593c8 |
grub_err_t err;
|
|
|
5593c8 |
grub_uint64_t tree = 0;
|
|
|
5593c8 |
grub_uint8_t type;
|
|
|
5593c8 |
+ grub_uint64_t saved_tree;
|
|
|
5593c8 |
struct grub_btrfs_key key;
|
|
|
5593c8 |
|
|
|
5593c8 |
+ if (path[0] == '\0')
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ data->fs_tree = 0;
|
|
|
5593c8 |
+ return GRUB_ERR_NONE;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ err = get_root (data, &key, &tree, &type);
|
|
|
5593c8 |
+ if (err)
|
|
|
5593c8 |
+ return err;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ saved_tree = data->fs_tree;
|
|
|
5593c8 |
+ data->fs_tree = tree;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
err = find_path (data, path, &key, &tree, &type);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ data->fs_tree = saved_tree;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
if (err)
|
|
|
5593c8 |
return grub_error(GRUB_ERR_FILE_NOT_FOUND, "couldn't locate %s\n", path);
|
|
|
5593c8 |
|
|
|
5593c8 |
@@ -2199,11 +2223,20 @@ grub_btrfs_dir (grub_device_t device, const char *path,
|
|
|
5593c8 |
int r = 0;
|
|
|
5593c8 |
grub_uint64_t tree;
|
|
|
5593c8 |
grub_uint8_t type;
|
|
|
5593c8 |
+ char *new_path = NULL;
|
|
|
5593c8 |
|
|
|
5593c8 |
if (!data)
|
|
|
5593c8 |
return grub_errno;
|
|
|
5593c8 |
|
|
|
5593c8 |
- err = find_path (data, path, &key_in, &tree, &type);
|
|
|
5593c8 |
+ tree = find_mtab_subvol_tree (path, &new_path);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (tree)
|
|
|
5593c8 |
+ data->fs_tree = tree;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ err = find_path (data, new_path ? new_path : path, &key_in, &tree, &type);
|
|
|
5593c8 |
+ if (new_path)
|
|
|
5593c8 |
+ grub_free (new_path);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
if (err)
|
|
|
5593c8 |
{
|
|
|
5593c8 |
grub_btrfs_unmount (data);
|
|
|
5593c8 |
@@ -2305,11 +2338,21 @@ grub_btrfs_open (struct grub_file *file, const char *name)
|
|
|
5593c8 |
struct grub_btrfs_inode inode;
|
|
|
5593c8 |
grub_uint8_t type;
|
|
|
5593c8 |
struct grub_btrfs_key key_in;
|
|
|
5593c8 |
+ grub_uint64_t tree;
|
|
|
5593c8 |
+ char *new_path = NULL;
|
|
|
5593c8 |
|
|
|
5593c8 |
if (!data)
|
|
|
5593c8 |
return grub_errno;
|
|
|
5593c8 |
|
|
|
5593c8 |
- err = find_path (data, name, &key_in, &data->tree, &type);
|
|
|
5593c8 |
+ tree = find_mtab_subvol_tree (name, &new_path);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (tree)
|
|
|
5593c8 |
+ data->fs_tree = tree;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ err = find_path (data, new_path ? new_path : name, &key_in, &data->tree, &type);
|
|
|
5593c8 |
+ if (new_path)
|
|
|
5593c8 |
+ grub_free (new_path);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
if (err)
|
|
|
5593c8 |
{
|
|
|
5593c8 |
grub_btrfs_unmount (data);
|
|
|
5593c8 |
@@ -2480,6 +2523,150 @@ grub_cmd_btrfs_info (grub_command_t cmd __attribute__ ((unused)), int argc,
|
|
|
5593c8 |
return 0;
|
|
|
5593c8 |
}
|
|
|
5593c8 |
|
|
|
5593c8 |
+struct grub_btrfs_mtab
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ struct grub_btrfs_mtab *next;
|
|
|
5593c8 |
+ struct grub_btrfs_mtab **prev;
|
|
|
5593c8 |
+ char *path;
|
|
|
5593c8 |
+ char *subvol;
|
|
|
5593c8 |
+ grub_uint64_t tree;
|
|
|
5593c8 |
+};
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+typedef struct grub_btrfs_mtab* grub_btrfs_mtab_t;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+static struct grub_btrfs_mtab *btrfs_mtab;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+#define FOR_GRUB_MTAB(var) FOR_LIST_ELEMENTS (var, btrfs_mtab)
|
|
|
5593c8 |
+#define FOR_GRUB_MTAB_SAFE(var, next) FOR_LIST_ELEMENTS_SAFE((var), (next), btrfs_mtab)
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+static void
|
|
|
5593c8 |
+add_mountpoint (const char *path, const char *subvol, grub_uint64_t tree)
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ grub_btrfs_mtab_t m = grub_malloc (sizeof (*m));
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ m->path = grub_strdup (path);
|
|
|
5593c8 |
+ m->subvol = grub_strdup (subvol);
|
|
|
5593c8 |
+ m->tree = tree;
|
|
|
5593c8 |
+ grub_list_push (GRUB_AS_LIST_P (&btrfs_mtab), GRUB_AS_LIST (m));
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+static grub_err_t
|
|
|
5593c8 |
+grub_cmd_btrfs_mount_subvol (grub_command_t cmd __attribute__ ((unused)), int argc,
|
|
|
5593c8 |
+ char **argv)
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ char *devname, *dirname, *subvol;
|
|
|
5593c8 |
+ struct grub_btrfs_key key_in;
|
|
|
5593c8 |
+ grub_uint8_t type;
|
|
|
5593c8 |
+ grub_uint64_t tree;
|
|
|
5593c8 |
+ grub_uint64_t saved_tree;
|
|
|
5593c8 |
+ grub_err_t err;
|
|
|
5593c8 |
+ struct grub_btrfs_data *data = NULL;
|
|
|
5593c8 |
+ grub_device_t dev = NULL;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (argc < 3)
|
|
|
5593c8 |
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "required <dev> <dir> and <subvol>");
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ devname = grub_file_get_device_name(argv[0]);
|
|
|
5593c8 |
+ dev = grub_device_open (devname);
|
|
|
5593c8 |
+ grub_free (devname);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (!dev)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ err = grub_errno;
|
|
|
5593c8 |
+ goto err_out;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ dirname = argv[1];
|
|
|
5593c8 |
+ subvol = argv[2];
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ data = grub_btrfs_mount (dev);
|
|
|
5593c8 |
+ if (!data)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ err = grub_errno;
|
|
|
5593c8 |
+ goto err_out;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ err = find_path (data, dirname, &key_in, &tree, &type);
|
|
|
5593c8 |
+ if (err)
|
|
|
5593c8 |
+ goto err_out;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (type != GRUB_BTRFS_DIR_ITEM_TYPE_DIRECTORY)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ err = grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
|
|
|
5593c8 |
+ goto err_out;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ err = get_root (data, &key_in, &tree, &type);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (err)
|
|
|
5593c8 |
+ goto err_out;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ saved_tree = data->fs_tree;
|
|
|
5593c8 |
+ data->fs_tree = tree;
|
|
|
5593c8 |
+ err = find_path (data, subvol, &key_in, &tree, &type);
|
|
|
5593c8 |
+ data->fs_tree = saved_tree;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (err)
|
|
|
5593c8 |
+ goto err_out;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (key_in.object_id != grub_cpu_to_le64_compile_time (GRUB_BTRFS_OBJECT_ID_CHUNK) || tree == 0)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ err = grub_error (GRUB_ERR_BAD_FILE_TYPE, "%s: not a subvolume\n", subvol);
|
|
|
5593c8 |
+ goto err_out;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ grub_btrfs_unmount (data);
|
|
|
5593c8 |
+ grub_device_close (dev);
|
|
|
5593c8 |
+ add_mountpoint (dirname, subvol, tree);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ return GRUB_ERR_NONE;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+err_out:
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (data)
|
|
|
5593c8 |
+ grub_btrfs_unmount (data);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (dev)
|
|
|
5593c8 |
+ grub_device_close (dev);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ return err;
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+grub_uint64_t
|
|
|
5593c8 |
+find_mtab_subvol_tree (const char *path, char **path_in_subvol)
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ grub_btrfs_mtab_t m, cm;
|
|
|
5593c8 |
+ grub_uint64_t tree;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (!path || !path_in_subvol)
|
|
|
5593c8 |
+ return 0;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ *path_in_subvol = NULL;
|
|
|
5593c8 |
+ tree = 0;
|
|
|
5593c8 |
+ cm = NULL;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ FOR_GRUB_MTAB (m)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ if (grub_strncmp (path, m->path, grub_strlen (m->path)) == 0)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ if (!cm)
|
|
|
5593c8 |
+ cm = m;
|
|
|
5593c8 |
+ else
|
|
|
5593c8 |
+ if (grub_strcmp (m->path, cm->path) > 0)
|
|
|
5593c8 |
+ cm = m;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (cm)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ const char *s = path + grub_strlen (cm->path);
|
|
|
5593c8 |
+ *path_in_subvol = (s[0] == '\0') ? grub_strdup ("/") : grub_strdup (s);
|
|
|
5593c8 |
+ tree = cm->tree;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ return tree;
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
+
|
|
|
5593c8 |
static grub_err_t
|
|
|
5593c8 |
get_fs_root(struct grub_btrfs_data *data, grub_uint64_t tree,
|
|
|
5593c8 |
grub_uint64_t objectid, grub_uint64_t offset,
|
|
|
5593c8 |
@@ -2686,6 +2873,7 @@ static struct grub_fs grub_btrfs_fs = {
|
|
|
5593c8 |
};
|
|
|
5593c8 |
|
|
|
5593c8 |
static grub_command_t cmd_info;
|
|
|
5593c8 |
+static grub_command_t cmd_mount_subvol;
|
|
|
5593c8 |
static grub_extcmd_t cmd_list_subvols;
|
|
|
5593c8 |
|
|
|
5593c8 |
static char *
|
|
|
5593c8 |
@@ -2749,6 +2937,9 @@ GRUB_MOD_INIT (btrfs)
|
|
|
5593c8 |
cmd_info = grub_register_command("btrfs-info", grub_cmd_btrfs_info,
|
|
|
5593c8 |
"DEVICE",
|
|
|
5593c8 |
"Print BtrFS info about DEVICE.");
|
|
|
5593c8 |
+ cmd_mount_subvol = grub_register_command("btrfs-mount-subvol", grub_cmd_btrfs_mount_subvol,
|
|
|
5593c8 |
+ "DEVICE DIRECTORY SUBVOL",
|
|
|
5593c8 |
+ "Set btrfs DEVICE the DIRECTORY a mountpoint of SUBVOL.");
|
|
|
5593c8 |
cmd_list_subvols = grub_register_extcmd("btrfs-list-subvols",
|
|
|
5593c8 |
grub_cmd_btrfs_list_subvols, 0,
|
|
|
5593c8 |
"[-p|-n] [-o var] DEVICE",
|
|
|
5593c8 |
diff --git a/grub-core/osdep/linux/getroot.c b/grub-core/osdep/linux/getroot.c
|
|
|
5593c8 |
index caf9b1ccd3f..28790307e00 100644
|
|
|
5593c8 |
--- a/grub-core/osdep/linux/getroot.c
|
|
|
5593c8 |
+++ b/grub-core/osdep/linux/getroot.c
|
|
|
5593c8 |
@@ -107,6 +107,14 @@ struct btrfs_ioctl_search_key
|
|
|
5593c8 |
grub_uint32_t unused[9];
|
|
|
5593c8 |
};
|
|
|
5593c8 |
|
|
|
5593c8 |
+struct btrfs_ioctl_search_header {
|
|
|
5593c8 |
+ grub_uint64_t transid;
|
|
|
5593c8 |
+ grub_uint64_t objectid;
|
|
|
5593c8 |
+ grub_uint64_t offset;
|
|
|
5593c8 |
+ grub_uint32_t type;
|
|
|
5593c8 |
+ grub_uint32_t len;
|
|
|
5593c8 |
+};
|
|
|
5593c8 |
+
|
|
|
5593c8 |
struct btrfs_ioctl_search_args {
|
|
|
5593c8 |
struct btrfs_ioctl_search_key key;
|
|
|
5593c8 |
grub_uint64_t buf[(4096 - sizeof(struct btrfs_ioctl_search_key))
|
|
|
5593c8 |
@@ -378,6 +386,109 @@ get_btrfs_fs_prefix (const char *mount_path)
|
|
|
5593c8 |
|
|
|
5593c8 |
int use_relative_path_on_btrfs = 0;
|
|
|
5593c8 |
|
|
|
5593c8 |
+static char *
|
|
|
5593c8 |
+get_btrfs_subvol (const char *path)
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ struct btrfs_ioctl_ino_lookup_args args;
|
|
|
5593c8 |
+ grub_uint64_t tree_id;
|
|
|
5593c8 |
+ int fd = -1;
|
|
|
5593c8 |
+ char *ret = NULL;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ fd = open (path, O_RDONLY);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (fd < 0)
|
|
|
5593c8 |
+ return NULL;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ memset (&args, 0, sizeof(args));
|
|
|
5593c8 |
+ args.objectid = GRUB_BTRFS_TREE_ROOT_OBJECTID;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (ioctl (fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
|
|
|
5593c8 |
+ goto error;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ tree_id = args.treeid;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ while (tree_id != GRUB_BTRFS_ROOT_VOL_OBJECTID)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ struct btrfs_ioctl_search_args sargs;
|
|
|
5593c8 |
+ struct grub_btrfs_root_backref *br;
|
|
|
5593c8 |
+ struct btrfs_ioctl_search_header *search_header;
|
|
|
5593c8 |
+ char *old;
|
|
|
5593c8 |
+ grub_uint16_t len;
|
|
|
5593c8 |
+ grub_uint64_t inode_id;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ memset (&sargs, 0, sizeof(sargs));
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ sargs.key.tree_id = 1;
|
|
|
5593c8 |
+ sargs.key.min_objectid = tree_id;
|
|
|
5593c8 |
+ sargs.key.max_objectid = tree_id;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ sargs.key.min_offset = 0;
|
|
|
5593c8 |
+ sargs.key.max_offset = ~0ULL;
|
|
|
5593c8 |
+ sargs.key.min_transid = 0;
|
|
|
5593c8 |
+ sargs.key.max_transid = ~0ULL;
|
|
|
5593c8 |
+ sargs.key.min_type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF;
|
|
|
5593c8 |
+ sargs.key.max_type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ sargs.key.nr_items = 1;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (ioctl (fd, BTRFS_IOC_TREE_SEARCH, &sargs) < 0)
|
|
|
5593c8 |
+ goto error;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (sargs.key.nr_items == 0)
|
|
|
5593c8 |
+ goto error;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ search_header = (struct btrfs_ioctl_search_header *)sargs.buf;
|
|
|
5593c8 |
+ br = (struct grub_btrfs_root_backref *) (search_header + 1);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ len = grub_le_to_cpu16 (br->n);
|
|
|
5593c8 |
+ inode_id = grub_le_to_cpu64 (br->inode_id);
|
|
|
5593c8 |
+ tree_id = search_header->offset;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ old = ret;
|
|
|
5593c8 |
+ ret = malloc (len + 1);
|
|
|
5593c8 |
+ memcpy (ret, br->name, len);
|
|
|
5593c8 |
+ ret[len] = '\0';
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (inode_id != GRUB_BTRFS_TREE_ROOT_OBJECTID)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ char *s;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ memset(&args, 0, sizeof(args));
|
|
|
5593c8 |
+ args.treeid = search_header->offset;
|
|
|
5593c8 |
+ args.objectid = inode_id;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (ioctl (fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
|
|
|
5593c8 |
+ goto error;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ s = xasprintf ("%s%s", args.name, ret);
|
|
|
5593c8 |
+ free (ret);
|
|
|
5593c8 |
+ ret = s;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (old)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ char *s = xasprintf ("%s/%s", ret, old);
|
|
|
5593c8 |
+ free (ret);
|
|
|
5593c8 |
+ free (old);
|
|
|
5593c8 |
+ ret = s;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ close (fd);
|
|
|
5593c8 |
+ return ret;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+error:
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (fd >= 0)
|
|
|
5593c8 |
+ close (fd);
|
|
|
5593c8 |
+ if (ret)
|
|
|
5593c8 |
+ free (ret);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ return NULL;
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+void (*grub_find_root_btrfs_mount_path_hook)(const char *mount_path);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
char **
|
|
|
5593c8 |
grub_find_root_devices_from_mountinfo (const char *dir, char **relroot)
|
|
|
5593c8 |
{
|
|
|
5593c8 |
@@ -519,12 +630,15 @@ again:
|
|
|
5593c8 |
else if (grub_strcmp (entries[i].fstype, "btrfs") == 0)
|
|
|
5593c8 |
{
|
|
|
5593c8 |
ret = grub_find_root_devices_from_btrfs (dir);
|
|
|
5593c8 |
- fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
|
|
|
5593c8 |
if (use_relative_path_on_btrfs)
|
|
|
5593c8 |
{
|
|
|
5593c8 |
- if (fs_prefix)
|
|
|
5593c8 |
- free (fs_prefix);
|
|
|
5593c8 |
fs_prefix = xstrdup ("/");
|
|
|
5593c8 |
+ if (grub_find_root_btrfs_mount_path_hook)
|
|
|
5593c8 |
+ grub_find_root_btrfs_mount_path_hook (entries[i].enc_path);
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+ else
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path);
|
|
|
5593c8 |
}
|
|
|
5593c8 |
}
|
|
|
5593c8 |
else if (!retry && grub_strcmp (entries[i].fstype, "autofs") == 0)
|
|
|
5593c8 |
@@ -1150,6 +1264,34 @@ grub_util_get_grub_dev_os (const char *os_dev)
|
|
|
5593c8 |
return grub_dev;
|
|
|
5593c8 |
}
|
|
|
5593c8 |
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+char *
|
|
|
5593c8 |
+grub_util_get_btrfs_subvol (const char *path, char **mount_path)
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ char *mp = NULL;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (mount_path)
|
|
|
5593c8 |
+ *mount_path = NULL;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ auto void
|
|
|
5593c8 |
+ mount_path_hook (const char *m)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ mp = strdup (m);
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ grub_find_root_btrfs_mount_path_hook = mount_path_hook;
|
|
|
5593c8 |
+ grub_free (grub_find_root_devices_from_mountinfo (path, NULL));
|
|
|
5593c8 |
+ grub_find_root_btrfs_mount_path_hook = NULL;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (!mp)
|
|
|
5593c8 |
+ return NULL;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (mount_path)
|
|
|
5593c8 |
+ *mount_path = mp;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ return get_btrfs_subvol (mp);
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
+
|
|
|
5593c8 |
char *
|
|
|
5593c8 |
grub_make_system_path_relative_to_its_root_os (const char *path)
|
|
|
5593c8 |
{
|
|
|
5593c8 |
diff --git a/util/grub-install.c b/util/grub-install.c
|
|
|
5593c8 |
index 0f66f36d23a..84ed6e88ecb 100644
|
|
|
5593c8 |
--- a/util/grub-install.c
|
|
|
5593c8 |
+++ b/util/grub-install.c
|
|
|
5593c8 |
@@ -1569,6 +1569,55 @@ main (int argc, char *argv[])
|
|
|
5593c8 |
prefix_drive = xasprintf ("(%s)", grub_drives[0]);
|
|
|
5593c8 |
}
|
|
|
5593c8 |
|
|
|
5593c8 |
+#ifdef __linux__
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (config.is_suse_btrfs_snapshot_enabled
|
|
|
5593c8 |
+ && grub_strncmp(grub_fs->name, "btrfs", sizeof ("btrfs") - 1) == 0)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ char *subvol = NULL;
|
|
|
5593c8 |
+ char *mount_path = NULL;
|
|
|
5593c8 |
+ char **rootdir_devices = NULL;
|
|
|
5593c8 |
+ char *rootdir_path = grub_util_path_concat (2, "/", rootdir);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (grub_util_is_directory (rootdir_path))
|
|
|
5593c8 |
+ rootdir_devices = grub_guess_root_devices (rootdir_path);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ free (rootdir_path);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (rootdir_devices && rootdir_devices[0])
|
|
|
5593c8 |
+ if (grub_strcmp (rootdir_devices[0], grub_devices[0]) == 0)
|
|
|
5593c8 |
+ subvol = grub_util_get_btrfs_subvol (platdir, &mount_path);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (subvol && mount_path)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ char *def_subvol;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ def_subvol = grub_util_get_btrfs_subvol ("/", NULL);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (def_subvol)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ if (!load_cfg_f)
|
|
|
5593c8 |
+ load_cfg_f = grub_util_fopen (load_cfg, "wb");
|
|
|
5593c8 |
+ have_load_cfg = 1;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (grub_strcmp (subvol, def_subvol) != 0)
|
|
|
5593c8 |
+ fprintf (load_cfg_f, "btrfs-mount-subvol ($root) %s %s\n", mount_path, subvol);
|
|
|
5593c8 |
+ free (def_subvol);
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ for (curdev = rootdir_devices; *curdev; curdev++)
|
|
|
5593c8 |
+ free (*curdev);
|
|
|
5593c8 |
+ if (rootdir_devices)
|
|
|
5593c8 |
+ free (rootdir_devices);
|
|
|
5593c8 |
+ if (subvol)
|
|
|
5593c8 |
+ free (subvol);
|
|
|
5593c8 |
+ if (mount_path)
|
|
|
5593c8 |
+ free (mount_path);
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+#endif
|
|
|
5593c8 |
+
|
|
|
5593c8 |
char mkimage_target[200];
|
|
|
5593c8 |
const char *core_name = NULL;
|
|
|
5593c8 |
|
|
|
5593c8 |
diff --git a/include/grub/emu/getroot.h b/include/grub/emu/getroot.h
|
|
|
5593c8 |
index 73fa2d34abb..9c642ae3fe3 100644
|
|
|
5593c8 |
--- a/include/grub/emu/getroot.h
|
|
|
5593c8 |
+++ b/include/grub/emu/getroot.h
|
|
|
5593c8 |
@@ -53,6 +53,11 @@ char **
|
|
|
5593c8 |
grub_find_root_devices_from_mountinfo (const char *dir, char **relroot);
|
|
|
5593c8 |
#endif
|
|
|
5593c8 |
|
|
|
5593c8 |
+#ifdef __linux__
|
|
|
5593c8 |
+char *
|
|
|
5593c8 |
+grub_util_get_btrfs_subvol (const char *path, char **mount_path);
|
|
|
5593c8 |
+#endif
|
|
|
5593c8 |
+
|
|
|
5593c8 |
/* Devmapper functions provided by getroot_devmapper.c. */
|
|
|
5593c8 |
void
|
|
|
5593c8 |
grub_util_pull_devmapper (const char *os_dev);
|