-
Notifications
You must be signed in to change notification settings - Fork 3
[codex] feat: support resizable macOS system disks #33
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import ( | |
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/docker/go-units" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/cocoonstack/cocoon-macos/home" | ||
|
|
@@ -55,6 +56,48 @@ func bakeOverlay(ctx context.Context, base, dst string) error { | |
| return nil | ||
| } | ||
|
|
||
| func storageFromFlag(cmd *cobra.Command) (int64, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reuse + consistency:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. measured to be precise — |
||
| raw, _ := cmd.Flags().GetString("storage") | ||
| if strings.TrimSpace(raw) == "" { | ||
| return 0, nil | ||
| } | ||
| parsed := strings.TrimSpace(raw) | ||
| if strings.HasSuffix(strings.ToLower(parsed), "i") { | ||
| parsed = parsed[:len(parsed)-1] | ||
| } | ||
| n, err := units.RAMInBytes(parsed) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("invalid --storage %q: %w", raw, err) | ||
| } | ||
| if n <= 0 { | ||
| return 0, fmt.Errorf("invalid --storage %q: size must be positive", raw) | ||
| } | ||
| return n, nil | ||
| } | ||
|
|
||
| // resizeSystemDisk expands a newly-created overlay to target bytes. A zero | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment budget: 3 lines → 1. keep the WHY only: |
||
| // target preserves the image size; shrinking is rejected because qemu-img | ||
| // cannot prove that the guest partition/filesystem would remain intact. | ||
| func resizeSystemDisk(ctx context.Context, path string, target int64) (int64, error) { | ||
| hdr, ok, err := utils.ReadQcow2Header(path) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("read system disk %s: %w", path, err) | ||
| } | ||
| if !ok { | ||
| return 0, fmt.Errorf("system disk %s is not qcow2", path) | ||
| } | ||
| if target == 0 || target == hdr.VirtualSize { | ||
| return hdr.VirtualSize, nil | ||
| } | ||
| if target < hdr.VirtualSize { | ||
| return 0, fmt.Errorf("--storage %d bytes is smaller than image virtual size %d bytes; shrinking is not supported", target, hdr.VirtualSize) | ||
| } | ||
| if err := utils.RunQemuImg(ctx, "resize", path, fmt.Sprintf("%d", target)); err != nil { | ||
| return 0, fmt.Errorf("resize system disk %s to %d bytes: %w", path, target, err) | ||
| } | ||
| return target, nil | ||
| } | ||
|
|
||
| // scaffoldVM lays down a new VM dir, disk overlay, and OVMF_VARS copy; it refuses an existing record — a second create/clone under the same name would truncate the live overlay. | ||
| func scaffoldVM(cmd *cobra.Command, name, image, varsSrc, varsName string) (dir, overlay, ovmfVars, digest string, err error) { | ||
| dir = home.VMDir(cmd, name) | ||
|
|
||
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.
json:"storage"with no omitempty writes"storage":0into every record that keeps the image size. add,omitempty— zero already means 'image virtual size'.