Repository navigation
refactor external snapshot using qemu-img #150
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
Jbchan01
wants to merge
5
commits into
main
Choose a base branch
from
revert_qemu
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
277985d
refactor external snapshot using qemu-img
Jbchan01 b6f7b54
use safepath, Verification Separation in create_ext
Jbchan01 2212bf5
refactor(external_snap): separate concerns and move qemuimg to internal
Jbchan01 685e718
refactor(qemuimg): add RemoveOverlay to Runner interface and remove d…
Jbchan01 f9afd89
fix(qemuimg): validate path against baseDir in RemoveOverlay to preve…
Jbchan01 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
Binary file not shown.
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,102 @@ | ||
| package qemuimg | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Runner abstracts qemu-img command execution for testability. | ||
| type Runner interface { | ||
| Create(backingFile, backingFormat, overlayPath string) error | ||
| // ReplaceOverlay removes an existing file at overlayPath if present, then creates a fresh overlay. | ||
| ReplaceOverlay(backingFile, backingFormat, overlayPath string) error | ||
| // RemoveOverlay deletes the overlay file at path after verifying it resides under baseDir. | ||
| // Returns nil if the file does not exist. | ||
| RemoveOverlay(baseDir, path string) error | ||
| Info(diskPath string) (backingFile, backingFormat string, err error) | ||
| // Convert flattens the full backing chain of src into a new standalone dst file. | ||
| // Unlike Commit, it never writes to any backing file so shared base images stay untouched. | ||
| Convert(src, dst string) error | ||
| // Commit writes all changes recorded in overlay into base, collapsing every | ||
| // intermediate layer between them. Only base needs a write lock; files above | ||
| // base in the chain are read-only during the operation. | ||
| Commit(overlay, base string) error | ||
| } | ||
|
|
||
| type real struct{} | ||
|
|
||
| func New() Runner { | ||
| return &real{} | ||
| } | ||
|
|
||
| func (q *real) Create(backingFile, backingFormat, overlayPath string) error { | ||
| args := []string{"create", "-f", "qcow2", "-b", backingFile} | ||
| if backingFormat != "" { | ||
| args = append(args, "-F", backingFormat) | ||
| } | ||
| args = append(args, overlayPath) | ||
|
|
||
| out, err := exec.Command("qemu-img", args...).CombinedOutput() | ||
| if err != nil { | ||
| return fmt.Errorf("qemu-img create failed: %w: %s", err, strings.TrimSpace(string(out))) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (q *real) RemoveOverlay(baseDir, path string) error { | ||
| cleanBase := filepath.Clean(baseDir) | ||
| cleanPath := filepath.Clean(path) | ||
| if !strings.HasPrefix(cleanPath, cleanBase+string(filepath.Separator)) { | ||
| return fmt.Errorf("path %s is outside base directory %s", path, baseDir) | ||
| } | ||
| if err := os.Remove(cleanPath); err != nil && !os.IsNotExist(err) { | ||
| return fmt.Errorf("failed to remove overlay %s: %w", path, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (q *real) ReplaceOverlay(backingFile, backingFormat, overlayPath string) error { | ||
| if err := os.Remove(overlayPath); err != nil && !os.IsNotExist(err) { | ||
| return fmt.Errorf("failed to remove stale overlay %s: %w", overlayPath, err) | ||
| } | ||
| return q.Create(backingFile, backingFormat, overlayPath) | ||
| } | ||
|
|
||
| type infoResult struct { | ||
| BackingFilename string `json:"backing-filename"` | ||
| BackingFileFormat string `json:"backing-filename-format"` | ||
| } | ||
|
|
||
| func (q *real) Info(diskPath string) (backingFile, backingFormat string, err error) { | ||
| out, execErr := exec.Command("qemu-img", "info", "--output=json", diskPath).CombinedOutput() | ||
| if execErr != nil { | ||
| return "", "", fmt.Errorf("qemu-img info failed: %w: %s", execErr, strings.TrimSpace(string(out))) | ||
| } | ||
|
|
||
| var result infoResult | ||
| if jsonErr := json.Unmarshal(out, &result); jsonErr != nil { | ||
| return "", "", fmt.Errorf("failed to parse qemu-img info output: %w", jsonErr) | ||
| } | ||
|
|
||
| return result.BackingFilename, result.BackingFileFormat, nil | ||
| } | ||
|
|
||
| func (q *real) Convert(src, dst string) error { | ||
| out, err := exec.Command("qemu-img", "convert", "-f", "qcow2", "-O", "qcow2", src, dst).CombinedOutput() | ||
| if err != nil { | ||
| return fmt.Errorf("qemu-img convert failed: %w: %s", err, strings.TrimSpace(string(out))) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (q *real) Commit(overlay, base string) error { | ||
| out, err := exec.Command("qemu-img", "commit", "-b", base, overlay).CombinedOutput() | ||
| if err != nil { | ||
| return fmt.Errorf("qemu-img commit failed: %w: %s", err, strings.TrimSpace(string(out))) | ||
| } | ||
| return nil | ||
| } |
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.
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.
qemu-img CLI를 감싸는 패키지입니다.