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

Have a real fixed HostID on Linux #603

Merged
merged 2 commits into from
Nov 7, 2018

Conversation

pierresouchay
Copy link
Contributor

On Linux, most golang programs do not run as root (or at least, they should not),
by default, the kernels uses strict permissions, so most userland programs cannot
read /sys/class/dmi/id/product_uuid. However, programs such as Consul are relying
on it to get fixed IDs, instead they have a different ID on each boot.

We propose to use /etc/machine-id as fallback https://www.freedesktop.org/software/systemd/man/machine-id.html

In order to fix this, this patch does the following:

  • if /sys/class/dmi/id/product_uuid can be read, use it for HostID
  • else if /etc/machine-id exists and has 32 chars, use it and add '-' to have the same format as product_uuid
  • finally, if notthing works, use the kernel.random.boot_id

This will greatly increase the number of programs having correct behaviour when
those rely on having a fixed HostID.

This will fix the following issues:

On Linux, most golang programs do not run as root (or at least, they should not),
by default, the kernels uses strict permissions, so most userland programs cannot
read `/sys/class/dmi/id/product_uuid`. However, programs such as Consul are relying
on it to get fixed IDs, instead they have a different ID on each boot.

We propose to use `/etc/machine-id` as fallback https://www.freedesktop.org/software/systemd/man/machine-id.html

In order to fix this, this patch does the following:
 - if `/sys/class/dmi/id/product_uuid` can be read, use it for HostID
 - else if `/etc/machine-id` exists and has 32 chars, use it and add '-' to have the same format as product_uuid
 - finally, if notthing works, use the `kernel.random.boot_id`

This will greatly increase the number of programs having correct behaviour when
those rely on having a fixed HostID.

This will fix the following issues:
 - shirou#350
 - hashicorp/consul#4741
host/host_linux.go Outdated Show resolved Hide resolved
@pierresouchay
Copy link
Contributor Author

pierresouchay commented Nov 6, 2018

@Lomanic yes, but this case is already properly handled with error management below.

What I mean is, this case (file not being readable is not the exception: some weird kernel config or unusual arch), but the rule (I mean, only programs running as root can use it, which in 2018 should be the exception, not the rule)

Copy link
Collaborator

@Lomanic Lomanic left a comment

Choose a reason for hiding this comment

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

Thanks 👍

@pierresouchay
Copy link
Contributor Author

@Lomanic Thank you for the review

@ShimmerGlass
Copy link

LGTM

@shirou
Copy link
Owner

shirou commented Nov 7, 2018

Wow, Great! Your are thinking backward compatibility so much!

@shirou shirou merged commit 48177ef into shirou:master Nov 7, 2018
@pierresouchay
Copy link
Contributor Author

@shirou Thank you! Do you think you might release a new version, so I can fix the bug in Consul by updating this library? Kind Regards

@Lomanic
Copy link
Collaborator

Lomanic commented Nov 7, 2018

Releases are once per month, following calver like Ubuntu, see Tag semantics

pierresouchay added a commit to pierresouchay/consul that referenced this pull request Nov 8, 2018
Bump `shirou/gopsutil` to include shirou/gopsutil#603

This will allow to have consistent node-id even when machine is reinstalled
when using `"disable_host_node_id": false`

It will fix hashicorp#4914 and allow having
the same node-id even when reinstalling a node from scratch. However,
it is only compatible with a single OS (installing to Windows will change
the node-id, but it seems acceptable).
@linydquantil
Copy link

+1

mkeeler pushed a commit to hashicorp/consul that referenced this pull request Jan 10, 2019
…4926)

Bump `shirou/gopsutil` to include shirou/gopsutil#603

This will allow to have consistent node-id even when machine is reinstalled
when using `"disable_host_node_id": false`

It will fix #4914 and allow having
the same node-id even when reinstalling a node from scratch. However,
it is only compatible with a single OS (installing to Windows will change
the node-id, but it seems acceptable).
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.

5 participants