Repository navigation
SHARD-213: drive Virtualization.framework through a per-VM shim the daemon re-adopts by socket #177
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
055b78b
SHARD-213: drive Virtualization.framework through a per-VM shim the d…
presmihaylov 92b601e
SHARD-213: answer the review: a 512 MiB default, retained device file…
presmihaylov e3c7b27
SHARD-213: answer the second review: untrack the shim binary, settle …
presmihaylov 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>com.apple.security.virtualization</key> | ||
| <true/> | ||
| </dict> | ||
| </plist> |
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,86 @@ | ||
| //go:build darwin | ||
|
|
||
| // shard-vz-shim holds one Virtualization.framework VM behind a unix socket, so it outlives the daemon; the shape follows hypeman's cmd/vz-shim/main.go (561e34fd), see NOTICE. | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "os/signal" | ||
| "runtime" | ||
| "runtime/debug" | ||
| "syscall" | ||
|
|
||
| vzfw "github.com/Code-Hex/vz/v3" | ||
|
|
||
| "github.com/presmihaylov/shard/pkg/vz" | ||
| ) | ||
|
|
||
| func main() { | ||
| if err := run(); err != nil { | ||
| fmt.Fprintln(os.Stderr, "shard-vz-shim:", err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func run() error { | ||
| encoded := flag.String("config", "", "the VM configuration as JSON") | ||
| flag.Parse() | ||
|
|
||
| var cfg vz.Config | ||
| if err := json.Unmarshal([]byte(*encoded), &cfg); err != nil { | ||
| return fmt.Errorf("decode -config: %w", err) | ||
| } | ||
|
|
||
| // The socket is claimed before the boot, so a second shim for the same VM refuses instead of orphaning the first. | ||
| listener, err := vz.Listen(cfg.Socket) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| machine, err := vz.NewMachine(&cfg) | ||
| if err != nil { | ||
| return errors.Join(err, listener.Close()) | ||
| } | ||
| if err := machine.Boot(&cfg); err != nil { | ||
| return errors.Join(err, listener.Close(), machine.Close()) | ||
| } | ||
|
|
||
| logger := log.New(os.Stderr, "", log.LstdFlags) | ||
| served := make(chan error, 1) | ||
| go func() { served <- vz.Serve(listener, machine, logger) }() | ||
|
|
||
| signals := make(chan os.Signal, 1) | ||
| signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1) | ||
| changed := machine.Changed() | ||
| for { | ||
| select { | ||
| case sig := <-signals: | ||
| // SIGUSR1 forces a collection, so a test can prove the device files outlive one. | ||
| if sig == syscall.SIGUSR1 { | ||
| runtime.GC() | ||
| debug.FreeOSMemory() | ||
| logger.Printf("%s: collected", sig) | ||
|
|
||
| continue | ||
| } | ||
| logger.Printf("%s: stopping the vm", sig) | ||
| if err := machine.Stop(); err != nil { | ||
| return err | ||
| } | ||
| case state := <-changed: | ||
| if state != vzfw.VirtualMachineStateStopped && state != vzfw.VirtualMachineStateError { | ||
| continue | ||
| } | ||
| logger.Printf("vm %s: exiting", vz.State(state.String())) | ||
| if err := listener.Close(); err != nil { | ||
| return fmt.Errorf("close the shim socket: %w", err) | ||
| } | ||
|
|
||
| return errors.Join(<-served, machine.Close()) | ||
| } | ||
| } | ||
| } |
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,16 @@ | ||
| //go:build !darwin | ||
|
|
||
| // shard-vz-shim holds one Virtualization.framework VM; off a Mac there is nothing to hold. | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/presmihaylov/shard/pkg/vz" | ||
| ) | ||
|
|
||
| func main() { | ||
| fmt.Fprintln(os.Stderr, "shard-vz-shim:", vz.ErrUnsupported) | ||
| os.Exit(1) | ||
| } |
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
Oops, something went wrong.
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.