Add and improve (proc) entries
cover Add and improve (proc) entries
This patch series adds proc entries to show the user some of GRUB's internal state on devices, loopback devices, and luks devices. This can be valuable when debugging grub, either as a developer, or as a user trying to determine why their config is not working as expected. Some of the same info can be gotten from the "ls" command with no arguments, however the proc entry is a lot less verbose, which is helpful when output is more than one screen's worth of data.
Patch #1: Add (proc)/loopbacks which shows all configured loopback devices Patch #2-4: Improve the existing luks proc entry by adding LUKS2 support,
add field names for the cryptodisk names and source device names, and adding a header to make the file more easily comprehensible
Patch #5: Add (proc)/devices to show all devices grub knows about.
Glenn
Glenn Washburn (5):
loopback: Add procfs entry 'loopbacks' to output configured loopback
devices
luks2: Add support for LUKS2 in (proc)/luks_script
cryptodisk: Add crypto disk name and source device names to
(proc)/luks_script
cryptodisk: Add header line of field names to (procfs)/luks_script
procfs: Add (proc)/devices to get info on grub devices.
grub-core/disk/cryptodisk.c | 41 +++++++++++++---- grub-core/disk/loopback.c | 57 +++++++++++++++++++++++ grub-core/fs/proc.c | 91 +++++++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+), 10 deletions(-)
Signature · 1 line
2.27.0
PATCH 1/5 loopback: Add procfs entry 'loopbacks' to output configured loopback devices
--- grub-core/disk/loopback.c | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
grub-core/disk/loopback.c +57 -0
@@ -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); }
Signature · 1 line
2.27.0
PATCH 2/5 luks2: Add support for LUKS2 in (proc)/luks_script
In reply to Glenn WashburnIn 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.
--- grub-core/disk/cryptodisk.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
grub-core/disk/cryptodisk.c +13 -8
@@ -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)
Signature · 1 line
2.27.0
PATCH 3/5 cryptodisk: Add crypto disk name and source device names to (proc)/luks_script
In reply to Glenn WashburnIt 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.
--- grub-core/disk/cryptodisk.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)
grub-core/disk/cryptodisk.c +18 -4
@@ -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 10000static 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 " ",
Signature · 1 line
2.27.0
PATCH 4/5 cryptodisk: Add header line of field names to (procfs)/luks_script
In reply to Glenn WashburnIts 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.
--- grub-core/disk/cryptodisk.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
grub-core/disk/cryptodisk.c +4 -2
@@ -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 ||
Signature · 1 line
2.27.0
PATCH 5/5 procfs: Add (proc)/devices to get info on grub devices.
In reply to Glenn Washburn--- grub-core/fs/proc.c | 91 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+)
grub-core/fs/proc.c +91 -0
@@ -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)
Signature · 1 line
2.27.0