Skip to content

JSON output support#190

Open
pvxe wants to merge 2 commits intorhboot:mainfrom
pvxe:main
Open

JSON output support#190
pvxe wants to merge 2 commits intorhboot:mainfrom
pvxe:main

Conversation

@pvxe
Copy link

@pvxe pvxe commented Jul 28, 2023

(JSON output support has been discussed in the past. #70)

Add --json option to enable JSON output support when compiling efibootmgr with it.
This output is compact and intended to be fed to other programs that want to interface with efibootmgr.

To enable JSON support, simply compile with make ... JSON=1 compilation flag.

Adds a new optional dependency with libjansson.

If the user runs efibootmgr --json without json support being compiled, it will print the following message:

JSON support is not built-in

The rationale behind this change is that I'd like to avoid parsing textual output from efibootmgr.

As additional context for these changes: I'm working on the development of a python program used for remote management of computers. This python program needs to retrieve BootOrder, BootNext and the boot entries of the machine it's running on. The program runs an efibootmgr subprocess for such tasks.


Example:

./src/efibootmgr --json | python -m json.tool
{
    "BootCurrent": "000D",
    "Timeout": 1,
    "BootOrder": [
        "0000",
        "000D",
        "0001",
        "000F",
        "0010",
    ],
    "vars": [
        {
            "name": "Boot0000",
            "active": true,
            "description": "Windows Boot Manager"
        },
        {
            "name": "Boot0001",
            "active": true,
            "description": "UEFI iPXE"
        },
        {
            "name": "Boot000D",
            "active": true,
            "description": "UEFI OS"
        },
        {
            "name": "Boot000F",
            "active": true,
            "description": "UEFI: IP4 Qualcomm Atheros PCIe Network Controller"
        },
        {
            "name": "Boot0010",
            "active": true,
            "description": "UEFI: IP6 Qualcomm Atheros PCIe Network Controller"
        },
    ]
}

@pvxe
Copy link
Author

pvxe commented Feb 27, 2024

I've decided to temporarily revert commit 5ce076c, while issue #204 is still present.

I will rebase my changes as soon as a fix is pushed into efibootmgr tree or a new efivar release is pushed with rhboot/efivar#217 merged.

pvxe added 2 commits March 20, 2024 10:23
Make read_u16 and read_order available to call outside efibootmgr.c.

Make var_entry_t public to be used by other source files.

Prepare efibootmgr for JSON output support by making required utility
functions and types publicly available to other source files.

Signed-off-by: Jose M. Guisado <jguisado@soleta.eu>
Enable JSON output for efibootmgr. This provides an interface for third
party tools so they do not need to scrap the existing textual output.

This feature is optional and is enabled by using JSON=1 when compiling:

	make ... JSON=1

Add parameter '-j'/--json' to use JSON output. If efibootmgr is not
built with JSON output support it will print out this instead:

	"JSON support is not built-in"

This feature adds a new optional dependency with libjansson.

Signed-off-by: Jose M. Guisado <jguisado@soleta.eu>
@stephen-mw
Copy link

stephen-mw commented Jul 31, 2024

Any update on this? Json output will be very useful to all of us who are building complicated regex parsing of the output at the moment.

@lupoDharkael
Copy link

I've been using these changes in a production environment and it works great, would be nice to have it upstream.

@socketpair
Copy link

Bumping PR. I also need it.

void print_json(list_t __unused *entry_list, ebm_mode __unused mode,
char __unused **prefices, char __unused **order_name)
{
printf("JSON support is not built-in\n");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exit with error would be better. Also, all warnings and messages that are not expected to be parsed should go to stderr instead of stdout

printf("\t-g | --gpt Force disk with invalid PMBR to be treated as GPT.\n");
printf("\t-i | --iface name Create a netboot entry for the named interface.\n");
printf("\t-I | --index number When creating an entry, insert it in bootorder at specified position (default: 0).\n");
printf("\t-j | --json Enable JSON output\n");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer #ifdef here instead of stub function that does nothing in case of JSON compile option was not set.

"Could not read variable 'BootNext'");
if (num >= 0) {
snprintf(s, sizeof(s), "%04X", num);
value = json_string(s);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possibly dump here num as number, not as hex string ?

if (num >= 0) {
snprintf(s, sizeof(s), "%04X", num);
value = json_string(s);
json_object_set_new(root, "BootNext", value);
Copy link

@socketpair socketpair Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer (as opposed to golang style) JSON formats where keys in dicts always exists, but refer to null instead of value. It's easier to interpret, actually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants