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

Fix LXC virtualization facts #58881

Closed
wants to merge 4 commits into from
Closed

Conversation

silverwind
Copy link
Contributor

SUMMARY

LXD guests were wrongly detected when running as non-root (making the check for /proc/1/environ fail) and when systemd was absent (making the check for /run/systemd/container fail).

Fixed this by adding a check for the LXD host <-> guest communication socket /dev/lxd/sock which is almost guaranteed to exist, thought it can theoretically be disabled by configuration.

Ref: https://github.com/lxc/lxd/issues/5923#issuecomment-509705074
Ref: https://lxd.readthedocs.io/en/latest/dev-lxd/

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

facts

ADDITIONAL INFORMATION

Before

"ansible_virtualization_role": "host",
"ansible_virtualization_type": "kvm",

After

"ansible_virtualization_role": "lxc",
"ansible_virtualization_type": "guest",

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. core_review In order to be merged, this PR must follow the core review workflow. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Jul 9, 2019
@silverwind
Copy link
Contributor Author

silverwind commented Jul 9, 2019

There's an option to add another check for existance of /var/lib/lxd/devlxd for the host role, but I think generally, hosts do run systemd so they'd be covered via the /run/systemd/container check, so I opted to not do it. If we want to cover ancient LXD systems without systemd, I guess it should be added, thought.

@silverwind
Copy link
Contributor Author

silverwind commented Jul 9, 2019

Decided to add the host role check as well. Testing against a Ubuntu 18.04 LXD node running various LXC guests showed it as

"ansible_virtualization_role": "host",
"ansible_virtualization_type": "kvm",

which is just wrong. Checking on the system, I see /run/systemd/container is absent so that check won't work there as I had initially assumed. After the fix, it now correctly shows the host role:

"ansible_virtualization_role": "host",
"ansible_virtualization_type": "lxc",

@silverwind silverwind changed the title Fix LXD guest detection for non-root/non-systemd Facts: Fix LXD detection for non-root/non-systemd Jul 9, 2019
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Jul 9, 2019
@samdoran
Copy link
Contributor

Please create unit tests and a changelog fragment. See this fragment as an example.

There are tests in test/units/module_utils/facts/test_ansible_collector.py but it may be easier to create newer pytest-style tests for testing this specific case.

@samdoran samdoran added the ci_verified Changes made in this PR are causing tests to fail. label Jul 11, 2019
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jul 11, 2019
@silverwind
Copy link
Contributor Author

Any pointers for writing tests which mock a remote file system and then assert the facts for those file systems? Do such tests exist yet?

@samdoran
Copy link
Contributor

@silverwind I do not believe we have any tests that do this currently. Go ahead and add the changelog fragment and I'll see if I can come up with some tests.

@silverwind
Copy link
Contributor Author

silverwind commented Jul 11, 2019

Will do. Maybe a rudimentary mock like this could suffice for some basic testing:

{
  "/proc/1/cgroup":  "file content",
  "/dev/lxd/sock": "", # no content
  "/var/lib/lxd/devlxd": "" # no content
}

Thought generally, I think the whole concept of ansible_virtualization_role/type is flawed because a system can host multiple virtualization techs, e.g. LXD, Docker and KVM can all co-exist on the same machine, but we can only return one.

The thing with LXD is that if someone has it installed, it's rather likely that it's their virtualization of choice, so that's why I added the check near the top of the file.

@silverwind
Copy link
Contributor Author

silverwind commented Jul 11, 2019

Did a small change to the host detection. Because LXD docs were apparently wrong, I did only check for the directory containing the socket, not the socket file itself. Filed https://github.com/lxc/lxd/pull/5941 to fix their docs.

@silverwind silverwind changed the title Facts: Fix LXD detection for non-root/non-systemd Fix LXC virtualization facts Jul 11, 2019
@silverwind
Copy link
Contributor Author

silverwind commented Jul 11, 2019

Force-pushed with changelog fragment and a new commit message.

@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Jul 11, 2019
@ansibot ansibot added the support:community This issue/PR relates to code supported by the Ansible community. label Jul 11, 2019
@samdoran
Copy link
Contributor

Will do. Maybe a rudimentary mock like this could suffice for some basic testing:

That's what I was thinking as well. Just patch things to behave like files exist.

Thought[sic.] generally, I think the whole concept of ansible_virtualization_role/type is flawed because a system can host multiple virtualization techs, e.g. LXD, Docker and KVM can all co-exist on the same machine, but we can only return one.

Also true. We were discussing this today and it would make more sense for this to be a list since there can be a combination of virtualization going on. This has compatibility implications, but your point is valid.

@silverwind
Copy link
Contributor Author

This has compatibility implications

Yes, of course. One way I see it done could be:

  • Indroduce a new list-based fact virtualization_roles in the format ["lxc-host", "docker-host"], can be empty too. The first item in the list must correspond to what the module currently returns.
  • Deprecate virtualization_role and virtualization_type and set them to use the first item from the new fact.
  • A few versions later, remove the old facts variables.

@bcoca
Copy link
Member

bcoca commented Jul 12, 2019

That has mostly been my plan for a while, but we was waiting for a facility to 'deprecate specific variables' which we have not been able to add yet. Until that happens, we could just go with the alternate keys and document that these are 'more precise' than the old ones.

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. core_review In order to be merged, this PR must follow the core review workflow. and removed core_review In order to be merged, this PR must follow the core review workflow. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jan 14, 2021
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jan 22, 2021
@ansibot ansibot removed the support:community This issue/PR relates to code supported by the Ansible community. label Mar 3, 2021
Copy link
Contributor

@s-hertel s-hertel left a comment

Choose a reason for hiding this comment

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

This fix is still applicable. Adding lxc to the new virtualization_tech_guest and virtualization_tech_host facts lists is good since systems may have multiple. LGTM besides a concern about backwards compatibility for the legacy singular facts.

lib/ansible/module_utils/facts/virtual/linux.py Outdated Show resolved Hide resolved
# https://lxd.readthedocs.io/en/latest/dev-lxd/
if os.path.exists('/dev/lxd/sock'):
guest_tech.add('lxc')
if not found_virt:
Copy link
Contributor

@s-hertel s-hertel Apr 6, 2022

Choose a reason for hiding this comment

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

To keep backwards compatibility for the virtualization_type and virtualization_role facts, this would need to occur at the end of the function. First found wins, so this would take precedence over any of the facts found later. Same for the /var/lib/lxd/devlxd/sock section.

Co-authored-by: Sloane Hertel <19572925+s-hertel@users.noreply.github.com>
@silverwind
Copy link
Contributor Author

@s-hertel maybe you want to take this over and file a new PR? I'm not really using ansible any more, so my motivation to further work on it is pretty much nil.

@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Apr 7, 2022
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Apr 15, 2022
@sivel sivel unassigned relrod Dec 1, 2022
@s-hertel s-hertel self-assigned this May 24, 2023
@ansibot ansibot removed the has_issue label Jul 12, 2023
@webknjaz

This comment was marked as resolved.

@azure-pipelines

This comment was marked as resolved.

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Sep 12, 2023
@webknjaz
Copy link
Member

The branch is too old, the CI can't check it out, needs rebase.

@webknjaz webknjaz added the ci_verified Changes made in this PR are causing tests to fail. label Sep 13, 2023
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Sep 20, 2023
@silverwind
Copy link
Contributor Author

silverwind commented Nov 10, 2023

I don't really need this anymore so I have lost interest.

Anyone still interested, feel free to raise a new PR based on this one.

@silverwind silverwind closed this Nov 10, 2023
@ansible ansible locked and limited conversation to collaborators Dec 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 affects_2.14 affects_2.15 bug This issue/PR relates to a bug. ci_verified Changes made in this PR are causing tests to fail. core_review In order to be merged, this PR must follow the core review workflow. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants