Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fabrics: Use /etc/machine-id as a fallback source for system UUID #956

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/nvme/fabrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#define NVMF_HOSTNQN_FILE SYSCONFDIR "/nvme/hostnqn"
#define NVMF_HOSTID_FILE SYSCONFDIR "/nvme/hostid"
#define MACHINE_ID_FILE SYSCONFDIR "/machine-id"

const char *nvmf_dev = "/dev/nvme-fabrics";

Expand Down Expand Up @@ -1427,6 +1428,40 @@
return ret;
}

/**
* uuid_from_machine_id() - read local machine ID config file
* @system_uuid: buffer for the UUID
*
* Reads and parses local machine ID config file located in $SYSCONFDIR,
* typically /etc/machine-id.
*
* https://www.freedesktop.org/software/systemd/man/latest/machine-id.html
*
* Return: 0 on success, negative errno otherwise.
*/
static int uuid_from_machine_id(char *system_uuid)
{
uint8_t uuid[NVME_UUID_LEN] = {0,};
_cleanup_file_ FILE *f = NULL;

Check failure on line 1445 in src/nvme/fabrics.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Missing a blank line after declarations
int n;

f = fopen(MACHINE_ID_FILE, "re");
if (!f)
return -errno;

n = fscanf(f,
"%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
"%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",

Check failure on line 1454 in src/nvme/fabrics.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: quoted string split across lines
&uuid[0], &uuid[1], &uuid[2], &uuid[3],
&uuid[4], &uuid[5], &uuid[6], &uuid[7],
&uuid[8], &uuid[9], &uuid[10], &uuid[11],
&uuid[12], &uuid[13], &uuid[14], &uuid[15]);
if (n < NVME_UUID_LEN)
return -EINVAL;

return nvme_uuid_to_string(uuid, system_uuid);
}

char *nvmf_hostid_generate()
{
int ret;
Expand All @@ -1436,6 +1471,8 @@
ret = uuid_from_dmi(uuid_str);
if (ret < 0)
ret = uuid_from_device_tree(uuid_str);
if (ret < 0)
ret = uuid_from_machine_id(uuid_str);
if (ret < 0) {
if (nvme_uuid_random(uuid) < 0)
memset(uuid, 0, NVME_UUID_LEN);
Expand Down
2 changes: 1 addition & 1 deletion src/nvme/fabrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ char *nvmf_hostid_generate();
* nvmf_hostnqn_from_file() - Reads the host nvm qualified name from the config
* default location
*
* Retrieve the qualified name from the config file located in $SYSCONFIDR/nvme.
* Retrieve the qualified name from the config file located in $SYSCONFDIR/nvme.
* $SYSCONFDIR is usually /etc.
*
* Return: The host nqn, or NULL if unsuccessful. If found, the caller
Expand Down
Loading