Skip to content

Commit

Permalink
revisions for #31 to address suggestions by @JoelColledge: rather tha…
Browse files Browse the repository at this point in the history
…n 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
  • Loading branch information
mmguero committed Oct 30, 2024
1 parent b482c83 commit 2ccdcb2
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions internal/virter/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,16 +476,9 @@ func (v *Virter) VMCommit(ctx context.Context, afterNotifier AfterNotifier, vmNa
// We want these IDs to be unique, so reset to empty.
err = v.VMExecShell(
ctx, []string{vmName},
&ProvisionShellStep{Script: "truncate -c -s 0 /etc/machine-id"})
&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 {
// In case the command failed because of an unprivileged user,
// retry with sudo before giving up.
err = v.VMExecShell(
ctx, []string{vmName},
&ProvisionShellStep{Script: "sudo truncate -c -s 0 /etc/machine-id"})
if err != nil {
return err
}
return err
}
}

Expand Down

0 comments on commit 2ccdcb2

Please sign in to comment.