-
Notifications
You must be signed in to change notification settings - Fork 315
Add optional Virtio memory balloon support #1276
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
Open
TastyHeadphones
wants to merge
2
commits into
openai:main
Choose a base branch
from
TastyHeadphones:claude/distracted-cannon-778ea8
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+325
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import Virtualization | ||
| import XCTest | ||
|
|
||
| @testable import tart | ||
|
|
||
| final class MemoryBalloonTests: XCTestCase { | ||
| // Configurations created by older Tart versions don't have | ||
| // the "memoryBalloon" key and should default to a disabled | ||
| // memory balloon device | ||
| func testDisabledByDefaultWhenDecodingLegacyConfig() throws { | ||
| let legacyConfigJSON = """ | ||
| { | ||
| "version": 1, | ||
| "os": "linux", | ||
| "arch": "arm64", | ||
| "cpuCountMin": 1, | ||
| "cpuCount": 1, | ||
| "memorySizeMin": 536870912, | ||
| "memorySize": 536870912, | ||
| "macAddress": "5a:00:00:00:00:01" | ||
| } | ||
| """ | ||
|
|
||
| let vmConfig = try VMConfig(fromJSON: legacyConfigJSON.data(using: .utf8)!) | ||
|
|
||
| XCTAssertFalse(vmConfig.memoryBalloon) | ||
| } | ||
|
|
||
| func testDisabledByDefaultWhenCreatingNewConfig() throws { | ||
| let vmConfig = VMConfig(platform: Linux(), cpuCountMin: 1, memorySizeMin: 512 * 1024 * 1024) | ||
|
|
||
| XCTAssertFalse(vmConfig.memoryBalloon) | ||
|
|
||
| // The key shouldn't even be present in the resulting JSON to keep | ||
| // the configurations of VMs that don't use this feature identical | ||
| // to those produced by older Tart versions | ||
| let encodedConfig = String(data: try vmConfig.toJSON(), encoding: .utf8)! | ||
| XCTAssertFalse(encodedConfig.contains("memoryBalloon")) | ||
| } | ||
|
|
||
| func testPersistsWhenEnabled() throws { | ||
| var vmConfig = VMConfig(platform: Linux(), cpuCountMin: 1, memorySizeMin: 512 * 1024 * 1024) | ||
| vmConfig.memoryBalloon = true | ||
|
|
||
| let roundtrippedVMConfig = try VMConfig(fromJSON: try vmConfig.toJSON()) | ||
|
|
||
| XCTAssertTrue(roundtrippedVMConfig.memoryBalloon) | ||
| } | ||
|
|
||
| func testMemoryBalloonSetArgumentParsing() throws { | ||
| XCTAssertEqual(try tart.Set.parse(["vm", "--memory-balloon", "true"]).memoryBalloon, true) | ||
| XCTAssertEqual(try tart.Set.parse(["vm", "--memory-balloon", "false"]).memoryBalloon, false) | ||
| XCTAssertNil(try tart.Set.parse(["vm"]).memoryBalloon) | ||
| XCTAssertThrowsError(try tart.Set.parse(["vm", "--memory-balloon", "yes"])) | ||
| } | ||
|
|
||
| func testBalloonTargetMemoryValidation() throws { | ||
| var vmConfig = VMConfig(platform: Linux(), cpuCountMin: 1, memorySizeMin: 4096 * 1024 * 1024) | ||
|
|
||
| // Balloon device is not enabled | ||
| XCTAssertThrowsError(try Run.validateBalloonTargetMemory(2048, vmConfig: vmConfig)) | ||
|
|
||
| vmConfig.memoryBalloon = true | ||
|
|
||
| // Target exceeds the configured memory size | ||
| XCTAssertThrowsError(try Run.validateBalloonTargetMemory(8192, vmConfig: vmConfig)) | ||
|
|
||
| // Target multiplication by 1 MB overflows UInt64 | ||
| XCTAssertThrowsError(try Run.validateBalloonTargetMemory(UInt64.max, vmConfig: vmConfig)) | ||
|
|
||
| // Target is too small to be safe | ||
| XCTAssertThrowsError(try Run.validateBalloonTargetMemory(1, vmConfig: vmConfig)) | ||
|
|
||
| // Sane target | ||
| XCTAssertNoThrow(try Run.validateBalloonTargetMemory(2048, vmConfig: vmConfig)) | ||
|
|
||
| // Target that is exactly the configured memory size (fully deflated balloon) | ||
| XCTAssertNoThrow(try Run.validateBalloonTargetMemory(4096, vmConfig: vmConfig)) | ||
| } | ||
|
|
||
| func testBalloonTargetMemoryValidationRespectsDarwinMinimum() throws { | ||
| // A macOS guest whose restore image requires 4096 MB of memory at minimum | ||
| var vmConfig = VMConfig(platform: Linux(), cpuCountMin: 1, memorySizeMin: 4096 * 1024 * 1024) | ||
| vmConfig.os = .darwin | ||
| vmConfig.memoryBalloon = true | ||
| try vmConfig.setMemory(memorySize: 8192 * 1024 * 1024) | ||
|
|
||
| // Target below the restore image's minimum supported memory size | ||
| XCTAssertThrowsError(try Run.validateBalloonTargetMemory(2048, vmConfig: vmConfig)) | ||
|
|
||
| // Target at the restore image's minimum supported memory size | ||
| XCTAssertNoThrow(try Run.validateBalloonTargetMemory(4096, vmConfig: vmConfig)) | ||
|
|
||
| // The same minimum doesn't apply to Linux guests, similarly | ||
| // to how "tart set --memory" doesn't restrict them | ||
| vmConfig.os = .linux | ||
| XCTAssertNoThrow(try Run.validateBalloonTargetMemory(2048, vmConfig: vmConfig)) | ||
| } | ||
|
|
||
| func testBalloonDeviceOnlyConfiguredWhenEnabled() throws { | ||
| // Disabled by default | ||
| XCTAssertEqual(try craftConfiguration().memoryBalloonDevices.count, 0) | ||
|
|
||
| // Configured when enabled | ||
| let memoryBalloonDevices = try craftConfiguration(memoryBalloon: true).memoryBalloonDevices | ||
| XCTAssertEqual(memoryBalloonDevices.count, 1) | ||
| XCTAssertTrue(memoryBalloonDevices.first is VZVirtioTraditionalMemoryBalloonDeviceConfiguration) | ||
|
|
||
| // Not configured for suspendable VMs, even when enabled | ||
| XCTAssertEqual(try craftConfiguration(memoryBalloon: true, suspendable: true).memoryBalloonDevices.count, 0) | ||
| } | ||
|
|
||
| private func craftConfiguration(memoryBalloon: Bool = false, suspendable: Bool = false) throws -> VZVirtualMachineConfiguration { | ||
| let tmpDir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) | ||
| try FileManager.default.createDirectory(at: tmpDir, withIntermediateDirectories: true) | ||
| defer { try? FileManager.default.removeItem(at: tmpDir) } | ||
|
|
||
| let nvramURL = tmpDir.appendingPathComponent("nvram.bin") | ||
| _ = try VZEFIVariableStore(creatingVariableStoreAt: nvramURL) | ||
|
|
||
| let diskURL = tmpDir.appendingPathComponent("disk.img") | ||
| FileManager.default.createFile(atPath: diskURL.path, contents: nil) | ||
| let diskFileHandle = try FileHandle(forWritingTo: diskURL) | ||
| try diskFileHandle.truncate(atOffset: 512 * 1024 * 1024) | ||
| try diskFileHandle.close() | ||
|
|
||
| var vmConfig = VMConfig(platform: Linux(), cpuCountMin: 1, memorySizeMin: 1024 * 1024 * 1024) | ||
| vmConfig.memoryBalloon = memoryBalloon | ||
|
|
||
| // Note: VM.buildConfiguration() is used here instead of VM.craftConfiguration(), | ||
| // because the latter additionally validates the configuration, which requires | ||
| // the "com.apple.security.virtualization" entitlement that tests don't have | ||
| return try VM.buildConfiguration( | ||
| diskURL: diskURL, | ||
| nvramURL: nvramURL, | ||
| vmConfig: vmConfig, | ||
| additionalStorageDevices: [], | ||
| directorySharingDevices: [], | ||
| serialPorts: [], | ||
| suspendable: suspendable | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.