Skip to content

Conversation

@nvazquez
Copy link
Contributor

Description

This PR fixes unused convert env variables from PR #11594 on the KVM agents, when agent.properties set:

# Instance conversion TMPDIR env var
#convert.instance.env.tmpdir=

# Instance conversion VIRT_V2V_TMPDIR env var
#convert.instance.env.virtv2v.tmpdir=

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

How did you try to break this feature and the system with this change?

@nvazquez
Copy link
Contributor Author

@blueorangutan package

@blueorangutan
Copy link

@nvazquez a [SL] Jenkins job has been kicked to build packages. It will be bundled with KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.

@codecov
Copy link

codecov bot commented Oct 30, 2025

Codecov Report

❌ Patch coverage is 61.90476% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 17.56%. Comparing base (3d6cafe) to head (f2b6474).

Files with missing lines Patch % Lines
...s/src/main/java/com/cloud/utils/script/Script.java 64.70% 4 Missing and 2 partials ⚠️
.../wrapper/LibvirtConvertInstanceCommandWrapper.java 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##               main   #11947   +/-   ##
=========================================
  Coverage     17.56%   17.56%           
- Complexity    15543    15545    +2     
=========================================
  Files          5909     5909           
  Lines        529056   529070   +14     
  Branches      64617    64620    +3     
=========================================
+ Hits          92941    92946    +5     
- Misses       425661   425667    +6     
- Partials      10454    10457    +3     
Flag Coverage Δ
uitests 3.58% <ø> (ø)
unittests 18.63% <61.90%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@blueorangutan
Copy link

Packaging result [SF]: ✖️ el8 ✖️ el9 ✔️ debian ✖️ suse15. SL-JID 15595

@nvazquez
Copy link
Contributor Author

@blueorangutan package

@blueorangutan
Copy link

@nvazquez a [SL] Jenkins job has been kicked to build packages. It will be bundled with KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.

@blueorangutan
Copy link

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 15598

@nvazquez
Copy link
Contributor Author

@blueorangutan test

@blueorangutan
Copy link

@nvazquez a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests

@blueorangutan
Copy link

[SF] Trillian test result (tid-14752)
Environment: kvm-ol8 (x2), zone: Advanced Networking with Mgmt server ol8
Total time taken: 61259 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr11947-t14752-kvm-ol8.zip
Smoke tests completed. 148 look OK, 0 have errors, 1 did not run
Only failed and skipped tests results shown below:

Test Result Time (s) Test File
all_test_human_readable_logs Skipped --- test_human_readable_logs.py

@DaanHoogland DaanHoogland added this to the 4.22.1 milestone Oct 31, 2025
@shwstppr shwstppr requested a review from Copilot November 3, 2025 10:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for passing custom environment variables when executing scripts via the Script class, specifically to support virt-v2v instance conversions. The changes enable passing environment variables like TMPDIR and VIRT_V2V_TMPDIR to the virt-v2v command, which is necessary for indirect tool invocations (e.g., virt-v2v calling qemu-img).

Key changes:

  • Extended the Script.execute() method to accept an optional environment variables array
  • Modified the script execution logic to use Runtime.exec() with environment variables when provided, falling back to ProcessBuilder when no environment is specified
  • Updated LibvirtConvertInstanceCommandWrapper to pass environment variables retrieved from the resource configuration

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
utils/src/main/java/com/cloud/utils/script/Script.java Added new overloaded execute() methods to accept environment variables and modified execution logic to handle both ProcessBuilder and Runtime.exec() approaches
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConvertInstanceCommandWrapper.java Updated to retrieve and conditionally pass environment variables to the script execution

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

return executeInternal(interpreter, environment);
}

public String executeInternal(OutputInterpreter interpreter, String[] environment) {
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

The executeInternal method is public but appears to be an internal implementation detail. Consider making this method private or package-private to prevent direct external usage, as callers should use the execute() methods instead.

Suggested change
public String executeInternal(OutputInterpreter interpreter, String[] environment) {
private String executeInternal(OutputInterpreter interpreter, String[] environment) {

Copilot uses AI. Check for mistakes.
Comment on lines +266 to +271
// Since Runtime.exec() does not support redirecting the error stream, then append 2>&1 to the command
String[] commands = new String[] {"sh", "-c", String.format("%s 2>&1", commandLine)};
// The PATH variable must be added for indirect calls within the running command
// Example: virt-v2v invokes qemu-img, which cannot be found if PATH is not set
String[] env = ArrayUtils.add(environment, String.format("PATH=%s", System.getenv("PATH")));
_process = Runtime.getRuntime().exec(commands, env, _workDir != null ? new File(_workDir) : null);
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

Wrapping the command in a shell (sh -c) could introduce command injection vulnerabilities if commandLine contains untrusted input. Ensure that all components of _command are properly validated or sanitized before being passed to this method, or consider using ProcessBuilder with proper environment variable support instead.

Suggested change
// Since Runtime.exec() does not support redirecting the error stream, then append 2>&1 to the command
String[] commands = new String[] {"sh", "-c", String.format("%s 2>&1", commandLine)};
// The PATH variable must be added for indirect calls within the running command
// Example: virt-v2v invokes qemu-img, which cannot be found if PATH is not set
String[] env = ArrayUtils.add(environment, String.format("PATH=%s", System.getenv("PATH")));
_process = Runtime.getRuntime().exec(commands, env, _workDir != null ? new File(_workDir) : null);
// Use ProcessBuilder to set environment variables and redirect error stream safely
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
if (_workDir != null)
pb.directory(new File(_workDir));
// Set environment variables
for (String envVar : environment) {
int idx = envVar.indexOf('=');
if (idx > 0) {
String key = envVar.substring(0, idx);
String value = envVar.substring(idx + 1);
pb.environment().put(key, value);
}
}
// Ensure PATH is set
pb.environment().put("PATH", System.getenv("PATH"));
_process = pb.start();

Copilot uses AI. Check for mistakes.
String[] commands = new String[] {"sh", "-c", String.format("%s 2>&1", commandLine)};
// The PATH variable must be added for indirect calls within the running command
// Example: virt-v2v invokes qemu-img, which cannot be found if PATH is not set
String[] env = ArrayUtils.add(environment, String.format("PATH=%s", System.getenv("PATH")));
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

If System.getenv(\"PATH\") returns null (which can happen in some environments), this will add "PATH=null" to the environment variables, potentially breaking command execution. Add a null check and handle the case where PATH is not defined.

Suggested change
String[] env = ArrayUtils.add(environment, String.format("PATH=%s", System.getenv("PATH")));
String pathEnv = System.getenv("PATH");
String[] env = environment;
if (pathEnv != null) {
env = ArrayUtils.add(environment, String.format("PATH=%s", pathEnv));
} else {
_logger.warn("System environment variable PATH is not set; indirect command calls may fail.");
}

Copilot uses AI. Check for mistakes.
Comment on lines +249 to +253
if (ArrayUtils.isEmpty(convertInstanceEnv)) {
script.execute(outputLogger);
} else {
script.execute(outputLogger, convertInstanceEnv);
}
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

The conditional check and branching can be simplified. The execute(outputLogger) method already calls execute(outputLogger, null), and the implementation handles null/empty arrays. You can directly call script.execute(outputLogger, convertInstanceEnv) without the conditional check, simplifying the code.

Suggested change
if (ArrayUtils.isEmpty(convertInstanceEnv)) {
script.execute(outputLogger);
} else {
script.execute(outputLogger, convertInstanceEnv);
}
script.execute(outputLogger, convertInstanceEnv);

Copilot uses AI. Check for mistakes.
_process = pb.start();
} else {
// Since Runtime.exec() does not support redirecting the error stream, then append 2>&1 to the command
String[] commands = new String[] {"sh", "-c", String.format("%s 2>&1", commandLine)};
Copy link
Contributor

Choose a reason for hiding this comment

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

Using shell invocation may be comparatively unsafe versus ProcessBuilder. Not sure if possible to avoid this?
No log is added for this else block, may help

Javadoc for new execute overloaded method may help

Copy link
Contributor

Choose a reason for hiding this comment

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

agree, co-pilots comments are giving some hints.

Comment on lines +243 to +247
public String execute(OutputInterpreter interpreter, String[] environment) {
return executeInternal(interpreter, environment);
}

public String executeInternal(OutputInterpreter interpreter, String[] environment) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public String execute(OutputInterpreter interpreter, String[] environment) {
return executeInternal(interpreter, environment);
}
public String executeInternal(OutputInterpreter interpreter, String[] environment) {
public String execute(OutputInterpreter interpreter, String[] environment) {

no need for the extra call on the stack, that I can see

_process = pb.start();
} else {
// Since Runtime.exec() does not support redirecting the error stream, then append 2>&1 to the command
String[] commands = new String[] {"sh", "-c", String.format("%s 2>&1", commandLine)};
Copy link
Contributor

Choose a reason for hiding this comment

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

agree, co-pilots comments are giving some hints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants