-
Notifications
You must be signed in to change notification settings - Fork 768
limactl: add --cpus, --memory, --mount-type, --vm-type, ...
#1468
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,252 @@ | ||
| package editflags | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/bits" | ||
| "runtime" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/pbnjay/memory" | ||
| "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| flag "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| // RegisterEdit registers flags related to in-place YAML modification, for `limactl edit`. | ||
| func RegisterEdit(cmd *cobra.Command) { | ||
| registerEdit(cmd, "") | ||
| } | ||
|
|
||
| func registerEdit(cmd *cobra.Command, commentPrefix string) { | ||
| flags := cmd.Flags() | ||
|
|
||
| flags.Int("cpus", 0, commentPrefix+"number of CPUs") // Similar to colima's --cpu, but the flag name is slightly different (cpu vs cpus) | ||
| _ = cmd.RegisterFlagCompletionFunc("cpus", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| var res []string | ||
| for _, f := range completeCPUs(runtime.NumCPU()) { | ||
| res = append(res, strconv.Itoa(f)) | ||
| } | ||
| return res, cobra.ShellCompDirectiveNoFileComp | ||
| }) | ||
|
|
||
| flags.IPSlice("dns", nil, commentPrefix+"specify custom DNS (disable host resolver)") // colima-compatible | ||
|
|
||
| flags.Float32("memory", 0, commentPrefix+"memory in GiB") // colima-compatible | ||
| _ = cmd.RegisterFlagCompletionFunc("memory", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| var res []string | ||
| for _, f := range completeMemoryGiB(memory.TotalMemory()) { | ||
| res = append(res, fmt.Sprintf("%.1f", f)) | ||
| } | ||
| return res, cobra.ShellCompDirectiveNoFileComp | ||
| }) | ||
|
|
||
| flags.StringSlice("mount", nil, commentPrefix+"directories to mount, suffix ':w' for writable (Do not specify directories that overlap with the existing mounts)") // colima-compatible | ||
|
|
||
| flags.String("mount-type", "", commentPrefix+"mount type (reverse-sshfs, 9p, virtiofs)") // Similar to colima's --mount-type=(sshfs|9p|virtiofs), but "reverse-sshfs" is Lima is called "sshfs" in colima | ||
| _ = cmd.RegisterFlagCompletionFunc("mount-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return []string{"reverse-sshfs", "9p", "virtiofs"}, cobra.ShellCompDirectiveNoFileComp | ||
| }) | ||
|
|
||
| flags.Bool("mount-writable", false, commentPrefix+"make all mounts writable") | ||
|
|
||
| flags.StringSlice("network", nil, commentPrefix+"additional networks, e.g., \"vzNAT\" or \"lima:shared\" to assign vmnet IP") | ||
| _ = cmd.RegisterFlagCompletionFunc("network", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| // TODO: retrieve the lima:* network list from networks.yaml | ||
| return []string{"lima:shared", "lima:bridged", "lima:host", "lima:user-v2", "vzNAT"}, cobra.ShellCompDirectiveNoFileComp | ||
| }) | ||
|
|
||
| flags.Bool("rosetta", false, commentPrefix+"enable Rosetta (for vz instances)") | ||
|
|
||
| flags.String("set", "", commentPrefix+"modify the template inplace, using yq syntax") | ||
| } | ||
|
|
||
| // RegisterCreate registers flags related to in-place YAML modification, for `limactl create`. | ||
| func RegisterCreate(cmd *cobra.Command, commentPrefix string) { | ||
| registerEdit(cmd, commentPrefix) | ||
| flags := cmd.Flags() | ||
|
|
||
| flags.String("arch", "", commentPrefix+"machine architecture (x86_64, aarch64, riscv64)") // colima-compatible | ||
| _ = cmd.RegisterFlagCompletionFunc("arch", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return []string{"x86_64", "aarch64", "riscv64"}, cobra.ShellCompDirectiveNoFileComp | ||
| }) | ||
|
|
||
| flags.String("containerd", "", commentPrefix+"containerd mode (user, system, user+system, none)") | ||
| _ = cmd.RegisterFlagCompletionFunc("vm-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return []string{"user", "system", "user+system", "none"}, cobra.ShellCompDirectiveNoFileComp | ||
| }) | ||
|
|
||
| flags.Float32("disk", 0, commentPrefix+"disk size in GiB") // colima-compatible | ||
| _ = cmd.RegisterFlagCompletionFunc("memory", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return []string{"10", "30", "50", "100", "200"}, cobra.ShellCompDirectiveNoFileComp | ||
| }) | ||
|
|
||
| flags.String("vm-type", "", commentPrefix+"virtual machine type (qemu, vz)") // colima-compatible | ||
| _ = cmd.RegisterFlagCompletionFunc("vm-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return []string{"qemu", "vz"}, cobra.ShellCompDirectiveNoFileComp | ||
| }) | ||
| } | ||
|
|
||
| func defaultExprFunc(expr string) func(v *flag.Flag) (string, error) { | ||
| return func(v *flag.Flag) (string, error) { | ||
| return fmt.Sprintf(expr, v.Value), nil | ||
| } | ||
| } | ||
|
|
||
| // YQExpressions returns YQ expressions. | ||
| func YQExpressions(flags *flag.FlagSet) ([]string, error) { | ||
| type def struct { | ||
| flagName string | ||
| exprFunc func(*flag.Flag) (string, error) | ||
| experimental bool | ||
| } | ||
| d := defaultExprFunc | ||
| defs := []def{ | ||
| {"cpus", d(".cpus = %s"), false}, | ||
| {"dns", | ||
| func(_ *flag.Flag) (string, error) { | ||
| ipSlice, err := flags.GetIPSlice("dns") | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| expr := `.dns += [` | ||
| for i, ip := range ipSlice { | ||
| expr += fmt.Sprintf("%q", ip) | ||
| if i < len(ipSlice)-1 { | ||
| expr += "," | ||
| } | ||
| } | ||
| expr += `] | .dns |= unique | .hostResolver.enabled=false` | ||
| logrus.Warnf("Disabling HostResolver, as custom DNS addresses (%v) are specified", ipSlice) | ||
| return expr, nil | ||
| }, | ||
| false}, | ||
| {"memory", d(".memory = \"%sGiB\""), false}, | ||
| {"mount", | ||
| func(_ *flag.Flag) (string, error) { | ||
| ss, err := flags.GetStringSlice("mount") | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| expr := `.mounts += [` | ||
| for i, s := range ss { | ||
| writable := strings.HasSuffix(s, ":w") | ||
| loc := strings.TrimSuffix(s, ":w") | ||
| expr += fmt.Sprintf(`{"location": %q, "writable": %v}`, loc, writable) | ||
| if i < len(ss)-1 { | ||
| expr += "," | ||
| } | ||
| } | ||
| expr += `] | .mounts |= unique_by(.location)` | ||
| return expr, nil | ||
| }, | ||
| false}, | ||
| {"mount-type", d(".mountType = %q"), false}, | ||
| {"mount-writable", d(".mounts[].writable = %s"), false}, | ||
| {"network", | ||
| func(_ *flag.Flag) (string, error) { | ||
| ss, err := flags.GetStringSlice("network") | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| expr := `.networks += [` | ||
| for i, s := range ss { | ||
| // CLI syntax is still experimental (YAML syntax is out of experimental) | ||
| switch { | ||
| case s == "vzNAT": | ||
| expr += `{"vzNAT": true}` | ||
| case strings.HasPrefix(s, "lima:"): | ||
| network := strings.TrimPrefix(s, "lima:") | ||
| expr += fmt.Sprintf(`{"lima": %q}`, network) | ||
| default: | ||
| return "", fmt.Errorf("network name must be \"vzNAT\" or \"lima:*\", got %q", s) | ||
| } | ||
| if i < len(ss)-1 { | ||
| expr += "," | ||
| } | ||
| } | ||
| expr += `] | .networks |= unique_by(.lima)` | ||
| return expr, nil | ||
| }, | ||
| true}, | ||
| {"rosetta", | ||
| func(_ *flag.Flag) (string, error) { | ||
| b, err := flags.GetBool("rosetta") | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return fmt.Sprintf(".rosetta.enabled = %v | .rosetta.binfmt = %v", b, b), nil | ||
| }, | ||
| true}, | ||
| {"set", d("%s"), true}, | ||
| {"arch", d(".arch = %q"), false}, | ||
| {"containerd", | ||
| func(_ *flag.Flag) (string, error) { | ||
| s, err := flags.GetString("containerd") | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| switch s { | ||
| case "user": | ||
| return `.containerd.user = true | .containerd.system = false`, nil | ||
| case "system": | ||
| return `.containerd.user = false | .containerd.system = true`, nil | ||
| case "user+system", "system+user": | ||
| return `.containerd.user = true | .containerd.system = true`, nil | ||
| case "none": | ||
| return `.containerd.user = false | .containerd.system = false`, nil | ||
| default: | ||
| return "", fmt.Errorf(`expected one of ["user", "system", "user+system", "none"], got %q`, s) | ||
| } | ||
| }, | ||
| false}, | ||
|
|
||
| {"disk", d(".disk= \"%sGiB\""), false}, | ||
| {"vm-type", d(".vmType = %q"), false}, | ||
| } | ||
| var exprs []string | ||
| for _, def := range defs { | ||
| v := flags.Lookup(def.flagName) | ||
| if v != nil && v.Changed { | ||
| if def.experimental { | ||
| logrus.Warnf("`--%s` is experimental", def.flagName) | ||
| } | ||
| expr, err := def.exprFunc(v) | ||
| if err != nil { | ||
| return exprs, fmt.Errorf("error while processing flag %q: %w", def.flagName, err) | ||
| } | ||
| exprs = append(exprs, expr) | ||
| } | ||
| } | ||
| return exprs, nil | ||
| } | ||
|
|
||
| func isPowerOfTwo(x int) bool { | ||
| return bits.OnesCount(uint(x)) == 1 | ||
| } | ||
|
|
||
| func completeCPUs(hostCPUs int) []int { | ||
| var res []int | ||
| for i := 1; i <= hostCPUs; i *= 2 { | ||
| res = append(res, i) | ||
| } | ||
| if !isPowerOfTwo(hostCPUs) { | ||
| res = append(res, hostCPUs) | ||
| } | ||
| return res | ||
| } | ||
|
|
||
| func completeMemoryGiB(hostMemory uint64) []float32 { | ||
| hostMemoryHalfGiB := int(hostMemory / 2 / 1024 / 1024 / 1024) | ||
| var res []float32 | ||
| if hostMemoryHalfGiB < 1 { | ||
| res = append(res, 0.5) | ||
| } | ||
| for i := 1; i <= hostMemoryHalfGiB; i *= 2 { | ||
| res = append(res, float32(i)) | ||
| } | ||
| if hostMemoryHalfGiB > 1 && !isPowerOfTwo(hostMemoryHalfGiB) { | ||
| res = append(res, float32(hostMemoryHalfGiB)) | ||
| } | ||
| return res | ||
| } | ||
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,22 @@ | ||
| package editflags | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func TestCompleteCPUs(t *testing.T) { | ||
| assert.DeepEqual(t, []int{1}, completeCPUs(1)) | ||
| assert.DeepEqual(t, []int{1, 2}, completeCPUs(2)) | ||
| assert.DeepEqual(t, []int{1, 2, 4, 8}, completeCPUs(8)) | ||
| assert.DeepEqual(t, []int{1, 2, 4, 8, 16, 20}, completeCPUs(20)) | ||
| } | ||
|
|
||
| func TestCompleteMemoryGiB(t *testing.T) { | ||
| assert.DeepEqual(t, []float32{0.5}, completeMemoryGiB(1<<30)) | ||
| assert.DeepEqual(t, []float32{1}, completeMemoryGiB(2<<30)) | ||
| assert.DeepEqual(t, []float32{1, 2}, completeMemoryGiB(4<<30)) | ||
| assert.DeepEqual(t, []float32{1, 2, 4}, completeMemoryGiB(8<<30)) | ||
| assert.DeepEqual(t, []float32{1, 2, 4, 8, 10}, completeMemoryGiB(20<<30)) | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should also add "disk" here, ("disk in GiB")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some concern here, because disk size can only ever be increased. The only way to shrink disk size is by destroying and re-creating the instance, losing all your data.
So this is not something that you should casually do. Or it should require a confirmation prompt for the user to acknowledge (disabled by
--tty=false).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For new instances it can be casually done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, my concern is purely about
limectl edit default --disk 500GiB, and then being unable to ever shrink it again.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could disable (or prompt) the feature to edit the disk size, until grow and shrink has been implemented ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
--diskshould not be implemented until Lima can grow the disk automatically.FWIW, I don't expect us to ever be able to shrink disks; is this even possible?