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

If 'truncate' fails for /etc/machine-id during 'commit', attempt with 'sudo' before giving up #31

Merged
merged 2 commits into from
Oct 30, 2024

Conversation

mmguero
Copy link
Contributor

@mmguero mmguero commented Oct 28, 2024

If 'truncate' fails for /etc/machine-id during 'commit', attempt with 'sudo' before giving up.

Addresses #29. Tested locally with virter vm commit as well as virter image build.

Also, fixed a typo in some help messages (s/durign/during/).

…iving up (LINBIT#29). Also, fix a spelling error in the help messages.
@JoelColledge
Copy link
Member

Thanks for the contribution!

There are alternatives to sudo that can be used to run a command with different privileges. ec768ad shows that this is a real concern. In that case the command is being run on the host rather than in the guest.

Could we change this so that, instead of doing a new provisioning step when the first truncate fails, we send a script that uses the appropriate tool? The script could use the same logic as ec768ad, just written as a shell script.

@mmguero
Copy link
Contributor Author

mmguero commented Oct 30, 2024

Sure, I'll modify the PR to follow the pattern in the commit you linked.

@mmguero
Copy link
Contributor Author

mmguero commented Oct 30, 2024

How would you feel about this being executed in the provisionshellstep:

		// Starting the VM creates a machine ID.
		// We want these IDs to be unique, so reset to empty.
		err = v.VMExecShell(
			ctx, []string{vmName},
			&ProvisionShellStep{Script: "CMD='truncate -c -s 0 /etc/machine-id'; [ \"$(id -u)\" -eq 0 ] && $CMD || { ESC=$(command -v sudo || command -v doas || command -v please || echo ''); $ESC $CMD; }"})
		if err != nil {
			return err
		}

@JoelColledge
Copy link
Member

How would you feel about this being executed in the provisionshellstep:

Neat. I'm happy with that 👍

It breaks on a system where sudo is on a path containing spaces 🙈 I can't imagine anyone actually doing that. That could be fixed by quoting "$ESC", but then the fallback ESC='' doesn't work. The best I ended up with was:

CMD='truncate -c -s 0 /etc/machine-id'; [ "$(id -u)" -eq 0 ] && $CMD || { ESC=$(command -v sudo || command -v doas || command -v please); [ -n "$ESC" ] && "$ESC" $CMD || $CMD; }

But that said, I think your suggestion is better. It is easier to understand and I really don't think anyone has /usr/bin with spaces/sudo on their PATH.

…er than retrying on a failed execution of truncating /etc/machine-id, determine if elevated privileges are needed in the shell script and use them from the get-go
@mmguero
Copy link
Contributor Author

mmguero commented Oct 30, 2024

Sounds good. I've committed an update. I've tested it locally (I will admit only on images that do have sudo available, but I think the logic is sound) by building an image (both with the --user flag set and without it) and then spinning it up and down and checking /etc/machine-id and it seems to be doing what we'd expect.

@JoelColledge JoelColledge merged commit 4634be3 into LINBIT:master Oct 30, 2024
1 check passed
@mmguero
Copy link
Contributor Author

mmguero commented Nov 4, 2024

@JoelColledge not to be a pest, but what's y'all's release cadence like? I've got some coworkers I want to get this PR to. I could just build the binary for them myself, but if an official release is forthcoming I'll wait so they can just grab it from the official repo here.

@JoelColledge
Copy link
Member

We don't have a regular release cadence. We release a new version when there is need. v0.28.0 released.

@mmguero
Copy link
Contributor Author

mmguero commented Nov 5, 2024

Thanks, I appreciate it!

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.

2 participants