* [PROCFS 1/5] loopback: Add procfs entry 'loopbacks' to output configured loopback devices
2021-03-19 23:17 [PROCFS 0/5] Add and improve (proc) entries Glenn Washburn
@ 2021-03-19 23:17 ` Glenn Washburn
2021-03-19 23:17 ` [PROCFS 2/5] luks2: Add support for LUKS2 in (proc)/luks_script Glenn Washburn
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Glenn Washburn @ 2021-03-19 23:17 UTC (permalink / raw)
To: grub-devel, Daniel Kiper; +Cc: Glenn Washburn
Signed-off-by: Glenn Washburn <development@efficientek.com>
---
grub-core/disk/loopback.c | 57 +++++++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git a/grub-core/disk/loopback.c b/grub-core/disk/loopback.c
index 41bebd14f..5957f58df 100644
--- a/grub-core/disk/loopback.c
+++ b/grub-core/disk/loopback.c
@@ -21,6 +21,7 @@
#include <grub/misc.h>
#include <grub/file.h>
#include <grub/disk.h>
+#include <grub/procfs.h>
#include <grub/mm.h>
#include <grub/extcmd.h>
#include <grub/i18n.h>
@@ -221,6 +222,60 @@ static struct grub_disk_dev grub_loopback_dev =
.next = 0
};
+
+/* Open a file named NAME and initialize FILE. */
+#define STR(s) #s
+#define MAX_ID_PRINT 10000
+static char *
+loopbacks_get (grub_size_t *sz)
+{
+ struct grub_loopback *i;
+ grub_size_t size = 0;
+ char *ptr, *ret;
+ static const char header[] = N_("<id> <devname> <filename>\n");
+ static const char errmsg[] = N_("Can not list more than " STR(MAX_ID_PRINT)
+ " loopback devices.\n");
+
+ for (i = loopback_list; i != NULL; i = i->next)
+ if (i->id < MAX_ID_PRINT)
+ {
+ /* id + spaces + '\n' */
+ size += sizeof (STR(MAX_ID_PRINT)) + 2 + 1;
+ size += grub_strlen (i->devname);
+ size += grub_strlen (i->file->name);
+ }
+ else
+ {
+ *sz = sizeof (errmsg);
+ return grub_strdup (errmsg);
+ }
+
+ ret = grub_malloc (sizeof (header) + size + 1);
+ if (!ret)
+ return 0;
+
+ ptr = grub_stpcpy (ret, header);
+
+ for (i = loopback_list; i != NULL; i = i->next)
+ {
+ ptr += grub_snprintf (ptr, 21, "%lu ", i->id);
+ ptr = grub_stpcpy (ptr, i->devname);
+ *ptr++ = ' ';
+ ptr = grub_stpcpy (ptr, i->file->name);
+ *ptr++ = '\n';
+ }
+ *ptr = '\0';
+ *sz = ptr - ret;
+ return ret;
+}
+
+struct grub_procfs_entry loopbacks =
+{
+ .name = "loopbacks",
+ .get_contents = loopbacks_get
+};
+
+
static grub_extcmd_t cmd;
GRUB_MOD_INIT(loopback)
@@ -231,10 +286,12 @@ GRUB_MOD_INIT(loopback)
or transformed into drive. */
N_("Make a virtual drive from a file."), options);
grub_disk_dev_register (&grub_loopback_dev);
+ grub_procfs_register ("loopbacks", &loopbacks);
}
GRUB_MOD_FINI(loopback)
{
grub_unregister_extcmd (cmd);
grub_disk_dev_unregister (&grub_loopback_dev);
+ grub_procfs_unregister (&loopbacks);
}
--
2.27.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PROCFS 2/5] luks2: Add support for LUKS2 in (proc)/luks_script
2021-03-19 23:17 [PROCFS 0/5] Add and improve (proc) entries Glenn Washburn
2021-03-19 23:17 ` [PROCFS 1/5] loopback: Add procfs entry 'loopbacks' to output configured loopback devices Glenn Washburn
@ 2021-03-19 23:17 ` Glenn Washburn
2021-03-19 23:17 ` [PROCFS 3/5] cryptodisk: Add crypto disk name and source device names to (proc)/luks_script Glenn Washburn
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Glenn Washburn @ 2021-03-19 23:17 UTC (permalink / raw)
To: grub-devel, Daniel Kiper; +Cc: Glenn Washburn
In addition, the sector size in bytes is added to each line and it is
allowed to be 4 decimal digits long, which covers the most common cases of
512 and 4096 byte sectors. The size allocation is updated to reflect this
additional field, allow up to 4 characters and 1 space added.
Signed-off-by: Glenn Washburn <development@efficientek.com>
---
grub-core/disk/cryptodisk.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
index 90f82b2d3..bb9e1a946 100644
--- a/grub-core/disk/cryptodisk.c
+++ b/grub-core/disk/cryptodisk.c
@@ -1221,12 +1221,15 @@ luks_script_get (grub_size_t *sz)
*sz = 0;
for (i = cryptodisk_list; i != NULL; i = i->next)
- if (grub_strcmp (i->modname, "luks") == 0)
+ if (grub_strcmp (i->modname, "luks") == 0 ||
+ grub_strcmp (i->modname, "luks2") == 0)
{
- size += sizeof ("luks_mount ");
+ size += grub_strlen (i->modname);
+ size += sizeof ("_mount");
size += grub_strlen (i->uuid);
size += grub_strlen (i->cipher->cipher->name);
- size += 54;
+ /* mode + mode_iv + spaces + offset + sector size + ??? + '\n' */
+ size += 5 + 8 + 5 + 20 + 4 + 16 + 1;
if (i->essiv_hash)
size += grub_strlen (i->essiv_hash->name);
size += i->keysize * 2;
@@ -1239,16 +1242,18 @@ luks_script_get (grub_size_t *sz)
ptr = ret;
for (i = cryptodisk_list; i != NULL; i = i->next)
- if (grub_strcmp (i->modname, "luks") == 0)
+ if (grub_strcmp (i->modname, "luks") == 0 ||
+ grub_strcmp (i->modname, "luks2") == 0)
{
unsigned j;
const char *iptr;
- ptr = grub_stpcpy (ptr, "luks_mount ");
+ ptr = grub_stpcpy (ptr, i->modname);
+ ptr = grub_stpcpy (ptr, "_mount ");
ptr = grub_stpcpy (ptr, i->uuid);
*ptr++ = ' ';
- grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset_sectors);
- while (*ptr)
- ptr++;
+ ptr += grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ",
+ i->offset_sectors);
+ ptr += grub_snprintf (ptr, 6, "%d ", 1 << i->log_sector_size);
for (iptr = i->cipher->cipher->name; *iptr; iptr++)
*ptr++ = grub_tolower (*iptr);
switch (i->mode)
--
2.27.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PROCFS 3/5] cryptodisk: Add crypto disk name and source device names to (proc)/luks_script
2021-03-19 23:17 [PROCFS 0/5] Add and improve (proc) entries Glenn Washburn
2021-03-19 23:17 ` [PROCFS 1/5] loopback: Add procfs entry 'loopbacks' to output configured loopback devices Glenn Washburn
2021-03-19 23:17 ` [PROCFS 2/5] luks2: Add support for LUKS2 in (proc)/luks_script Glenn Washburn
@ 2021-03-19 23:17 ` Glenn Washburn
2021-03-19 23:17 ` [PROCFS 4/5] cryptodisk: Add header line of field names to (procfs)/luks_script Glenn Washburn
2021-03-19 23:17 ` [PROCFS 5/5] procfs: Add (proc)/devices to get info on grub devices Glenn Washburn
4 siblings, 0 replies; 6+ messages in thread
From: Glenn Washburn @ 2021-03-19 23:17 UTC (permalink / raw)
To: grub-devel, Daniel Kiper; +Cc: Glenn Washburn
It can be difficult to determine what device backs a particular (cryptoN)
device because they are automatically generated. This allows users to see
in the grub shell which devices back which crypto devices.
Signed-off-by: Glenn Washburn <development@efficientek.com>
---
grub-core/disk/cryptodisk.c | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
index bb9e1a946..be97dfab9 100644
--- a/grub-core/disk/cryptodisk.c
+++ b/grub-core/disk/cryptodisk.c
@@ -1211,25 +1211,36 @@ hex (grub_uint8_t val)
}
/* Open a file named NAME and initialize FILE. */
+#define STR(s) #s
+#define MAX_ID_PRINT 10000
static char *
luks_script_get (grub_size_t *sz)
{
grub_cryptodisk_t i;
grub_size_t size = 0;
char *ptr, *ret;
+ static char errmsg[] = N_("Can not list more than " STR(MAX_ID_PRINT)
+ " crypto devices.\n");
*sz = 0;
for (i = cryptodisk_list; i != NULL; i = i->next)
- if (grub_strcmp (i->modname, "luks") == 0 ||
- grub_strcmp (i->modname, "luks2") == 0)
+ if (i->id >= MAX_ID_PRINT)
+ {
+ *sz = sizeof (errmsg);
+ return errmsg;
+ }
+ else if (grub_strcmp (i->modname, "luks") == 0 ||
+ grub_strcmp (i->modname, "luks2") == 0)
{
size += grub_strlen (i->modname);
size += sizeof ("_mount");
+ size += sizeof ("crypto");
+ size += grub_strlen (i->source);
size += grub_strlen (i->uuid);
size += grub_strlen (i->cipher->cipher->name);
- /* mode + mode_iv + spaces + offset + sector size + ??? + '\n' */
- size += 5 + 8 + 5 + 20 + 4 + 16 + 1;
+ /* spaces + maxidlen + mode + mode_iv + offset + sector size + ??? + '\n' */
+ size += 7 + sizeof (STR(MAX_ID_PRINT)) + 5 + 8 + 20 + 4 + 16 + 1;
if (i->essiv_hash)
size += grub_strlen (i->essiv_hash->name);
size += i->keysize * 2;
@@ -1249,6 +1260,9 @@ luks_script_get (grub_size_t *sz)
const char *iptr;
ptr = grub_stpcpy (ptr, i->modname);
ptr = grub_stpcpy (ptr, "_mount ");
+ ptr += grub_snprintf (ptr, 8 + sizeof (STR(MAX_ID_PRINT)), "crypto%lu ", i->id);
+ ptr = grub_stpcpy (ptr, i->source);
+ *ptr++ = ' ';
ptr = grub_stpcpy (ptr, i->uuid);
*ptr++ = ' ';
ptr += grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ",
--
2.27.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PROCFS 4/5] cryptodisk: Add header line of field names to (procfs)/luks_script
2021-03-19 23:17 [PROCFS 0/5] Add and improve (proc) entries Glenn Washburn
` (2 preceding siblings ...)
2021-03-19 23:17 ` [PROCFS 3/5] cryptodisk: Add crypto disk name and source device names to (proc)/luks_script Glenn Washburn
@ 2021-03-19 23:17 ` Glenn Washburn
2021-03-19 23:17 ` [PROCFS 5/5] procfs: Add (proc)/devices to get info on grub devices Glenn Washburn
4 siblings, 0 replies; 6+ messages in thread
From: Glenn Washburn @ 2021-03-19 23:17 UTC (permalink / raw)
To: grub-devel, Daniel Kiper; +Cc: Glenn Washburn
Its not obvious from the grub shell, nor is it documented anywhere, what
the fields in each line of (procfs)/luks_script represent. This makes it
easier for the user to comprehend the meaning of fields directly from the
shell.
Signed-off-by: Glenn Washburn <development@efficientek.com>
---
grub-core/disk/cryptodisk.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
index be97dfab9..6b1d73fd3 100644
--- a/grub-core/disk/cryptodisk.c
+++ b/grub-core/disk/cryptodisk.c
@@ -1219,6 +1219,8 @@ luks_script_get (grub_size_t *sz)
grub_cryptodisk_t i;
grub_size_t size = 0;
char *ptr, *ret;
+ const char header[] = N_("<type> <devname> <source disk> <uuid> <sector "
+ "offset> <sector size> <cipher> <key> <options>\n");
static char errmsg[] = N_("Can not list more than " STR(MAX_ID_PRINT)
" crypto devices.\n");
@@ -1246,11 +1248,11 @@ luks_script_get (grub_size_t *sz)
size += i->keysize * 2;
}
- ret = grub_malloc (size + 1);
+ ret = grub_malloc (sizeof (header) + size + 1);
if (!ret)
return 0;
- ptr = ret;
+ ptr = grub_stpcpy (ret, header);
for (i = cryptodisk_list; i != NULL; i = i->next)
if (grub_strcmp (i->modname, "luks") == 0 ||
--
2.27.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PROCFS 5/5] procfs: Add (proc)/devices to get info on grub devices.
2021-03-19 23:17 [PROCFS 0/5] Add and improve (proc) entries Glenn Washburn
` (3 preceding siblings ...)
2021-03-19 23:17 ` [PROCFS 4/5] cryptodisk: Add header line of field names to (procfs)/luks_script Glenn Washburn
@ 2021-03-19 23:17 ` Glenn Washburn
4 siblings, 0 replies; 6+ messages in thread
From: Glenn Washburn @ 2021-03-19 23:17 UTC (permalink / raw)
To: grub-devel, Daniel Kiper; +Cc: Glenn Washburn
Signed-off-by: Glenn Washburn <development@efficientek.com>
---
grub-core/fs/proc.c | 91 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 91 insertions(+)
diff --git a/grub-core/fs/proc.c b/grub-core/fs/proc.c
index 5f516502d..9baabb7d4 100644
--- a/grub-core/fs/proc.c
+++ b/grub-core/fs/proc.c
@@ -190,10 +190,101 @@ static struct grub_fs grub_procfs_fs =
.next = 0
};
+/* Context for grub_device_iterate. */
+struct grub_device_iterate_ctx
+{
+ char *str;
+ grub_size_t len, size;
+};
+
+#define ALLOC_SIZE 512
+static int
+iterate_disk (const char *disk_name, void *data)
+{
+ struct grub_device_iterate_ctx *ctx = data;
+ grub_device_t dev;
+ int size = 0, ret = 0;
+ char *buf;
+
+ dev = grub_device_open (disk_name);
+ if (! dev)
+ {
+ grub_errno = GRUB_ERR_NONE;
+ return 0;
+ }
+
+ if (!dev->disk || dev->disk->partition)
+ goto err;
+
+ do {
+ grub_disk_t disk = dev->disk;
+
+ size = grub_snprintf(ctx->str + ctx->len, ctx->size - ctx->len,
+ "\n%s %s %"PRIuGRUB_UINT64_T" %u",
+ disk->name, (disk->dev) ? disk->dev->name : "none",
+ disk->total_sectors, 1 << disk->log_sector_size);
+
+ if ((size + ctx->len) <= ctx->size)
+ {
+ ctx->len += size;
+ break;
+ }
+
+ /* Not enough space left in string, reallocate more and try again. */
+ buf = grub_realloc(ctx->str, ctx->size + ALLOC_SIZE);
+ if (!buf)
+ goto err;
+
+ ctx->str = buf;
+ ctx->size += ALLOC_SIZE;
+ } while (1);
+
+err:
+ grub_device_close (dev);
+ return ret;
+}
+
+static char *
+devices_get (grub_size_t *sz)
+{
+ struct grub_device_iterate_ctx ctx = { NULL, 0, 0 };
+ static const char header[] = N_("<name> <type> <nsectors> <sector size>");
+
+ *sz = 0;
+ ctx.str = grub_malloc(ALLOC_SIZE);
+ ctx.size = ALLOC_SIZE;
+ if (ctx.str == NULL)
+ {
+ grub_print_error();
+ return NULL;
+ }
+
+ ctx.len = grub_stpcpy (ctx.str, header) - ctx.str;
+
+ if (grub_disk_dev_iterate (iterate_disk, &ctx))
+ grub_print_error();
+
+ *sz = ctx.len;
+ return ctx.str;
+}
+
+struct grub_procfs_entry procfs[] =
+{
+ {
+ .name = "devices",
+ .get_contents = devices_get
+ }
+};
+
GRUB_MOD_INIT (procfs)
{
+ struct grub_procfs_entry *pe;
+
grub_disk_dev_register (&grub_procfs_dev);
grub_fs_register (&grub_procfs_fs);
+
+ for (pe = procfs + (sizeof (procfs)/sizeof (*procfs)) - 1; (pe + 1) != procfs; pe--)
+ grub_procfs_register (pe->name, pe);
}
GRUB_MOD_FINI (procfs)
--
2.27.0
^ permalink raw reply related [flat|nested] 6+ messages in thread