From 4634be306f0a4d97c177b917eff889a373ced183 Mon Sep 17 00:00:00 2001 From: Seth Grover <13872653+mmguero@users.noreply.github.com> Date: Wed, 30 Oct 2024 10:05:21 -0600 Subject: [PATCH] If 'truncate' fails for /etc/machine-id during 'commit', attempt with 'sudo' before giving up If 'truncate' fails for /etc/machine-id, attempt with 'sudo' before giving up (LINBIT/virter#29). Also, fix a spelling error in the help messages. --- cmd/image_build.go | 2 +- cmd/vm_exec.go | 2 +- cmd/vm_run.go | 2 +- internal/virter/vm.go | 3 +-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/image_build.go b/cmd/image_build.go index 9f41f6a..9da3d70 100644 --- a/cmd/image_build.go +++ b/cmd/image_build.go @@ -280,7 +280,7 @@ func imageBuildCommand() *cobra.Command { buildCmd.Flags().BoolVar(&resetMachineID, "reset-machine-id", true, "Whether or not to clear the /etc/machine-id file after provisioning") buildCmd.Flags().VarP(&vmPullPolicy, "pull-policy", "", "Whether or not to pull the source image.") buildCmd.Flags().VarP(&vmPullPolicy, "vm-pull-policy", "", fmt.Sprintf("Whether or not to pull the source image. Valid values: [%s, %s, %s]", pullpolicy.Always, pullpolicy.IfNotExist, pullpolicy.Never)) - buildCmd.Flags().VarP(&containerPullPolicy, "container-pull-policy", "", fmt.Sprintf("Whether or not to pull container images used durign provisioning. Overrides the `pull` value of every provision step. Valid values: [%s, %s, %s]", pullpolicy.Always, pullpolicy.IfNotExist, pullpolicy.Never)) + buildCmd.Flags().VarP(&containerPullPolicy, "container-pull-policy", "", fmt.Sprintf("Whether or not to pull container images used during provisioning. Overrides the `pull` value of every provision step. Valid values: [%s, %s, %s]", pullpolicy.Always, pullpolicy.IfNotExist, pullpolicy.Never)) buildCmd.Flags().BoolVarP(&push, "push", "", false, "Push the image after building") buildCmd.Flags().BoolVarP(&noCache, "no-cache", "", false, "Disable caching for the image build") buildCmd.Flags().StringVarP(&buildId, "build-id", "", "", "Build ID used to determine if an image needs to be rebuild.") diff --git a/cmd/vm_exec.go b/cmd/vm_exec.go index eeec90a..aec83e6 100644 --- a/cmd/vm_exec.go +++ b/cmd/vm_exec.go @@ -64,7 +64,7 @@ func vmExecCommand() *cobra.Command { execCmd.Flags().VarP(&provFile, "provision", "p", "name of toml file containing provisioning steps") execCmd.Flags().StringArrayVarP(&provisionOverrides, "set", "s", []string{}, "set/override provisioning steps") - execCmd.Flags().VarP(&containerPullPolicy, "container-pull-policy", "", fmt.Sprintf("Whether or not to pull container images used durign provisioning. Overrides the `pull` value of every provision step. Valid values: [%s, %s, %s]", pullpolicy.Always, pullpolicy.IfNotExist, pullpolicy.Never)) + execCmd.Flags().VarP(&containerPullPolicy, "container-pull-policy", "", fmt.Sprintf("Whether or not to pull container images used during provisioning. Overrides the `pull` value of every provision step. Valid values: [%s, %s, %s]", pullpolicy.Always, pullpolicy.IfNotExist, pullpolicy.Never)) return execCmd } diff --git a/cmd/vm_run.go b/cmd/vm_run.go index 783e9bb..a92e2fe 100644 --- a/cmd/vm_run.go +++ b/cmd/vm_run.go @@ -308,7 +308,7 @@ func vmRunCommand() *cobra.Command { runCmd.Flags().VarP(&vmPullPolicy, "pull-policy", "", "Whether or not to pull the source image.") runCmd.Flags().VarP(&vmPullPolicy, "vm-pull-policy", "", fmt.Sprintf("Whether or not to pull the source image. Valid values: [%s, %s, %s]", pullpolicy.Always, pullpolicy.IfNotExist, pullpolicy.Never)) - runCmd.Flags().VarP(&containerPullPolicy, "container-pull-policy", "", fmt.Sprintf("Whether or not to pull container images used durign provisioning. Overrides the `pull` value of every provision step. Valid values: [%s, %s, %s]", pullpolicy.Always, pullpolicy.IfNotExist, pullpolicy.Never)) + runCmd.Flags().VarP(&containerPullPolicy, "container-pull-policy", "", fmt.Sprintf("Whether or not to pull container images used during provisioning. Overrides the `pull` value of every provision step. Valid values: [%s, %s, %s]", pullpolicy.Always, pullpolicy.IfNotExist, pullpolicy.Never)) runCmd.Flags().StringVarP(&user, "user", "u", "root", "Remote user for ssh session") runCmd.Flags().BoolVarP(&vncEnabled, "vnc", "", false, "whether to configure VNC (remote GUI access) for the VM (defaults to false)") runCmd.Flags().IntVar(&vncPort, "vnc-port", 0, "VNC port. Defaults to 6000+id of this VM") diff --git a/internal/virter/vm.go b/internal/virter/vm.go index f505a47..f4ab456 100644 --- a/internal/virter/vm.go +++ b/internal/virter/vm.go @@ -476,8 +476,7 @@ 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 { return err }