-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from nirs/block-status-preps
Preparing for adding block status interface
- Loading branch information
Showing
7 changed files
with
164 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -11,23 +11,23 @@ jobs: | |
steps: | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: 1.20.x | ||
go-version: 1.22.x | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 1 | ||
- name: Run golangci-lint | ||
uses: golangci/[email protected] | ||
with: | ||
version: v1.52.2 | ||
version: v1.60.1 | ||
args: --verbose | ||
- name: Unit tests | ||
run: go test -v ./... | ||
- name: Install go-qcow2reader-example | ||
run: cd ./cmd/go-qcow2reader-example && go install | ||
- name: Install qemu-img as a test dependency | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install -y qemu-utils | ||
- name: Unit tests | ||
run: go test -v ./... | ||
- name: Install go-qcow2reader-example | ||
run: cd ./cmd/go-qcow2reader-example && go install | ||
- name: Cache test-images-ro | ||
id: cache-test-images-ro | ||
uses: actions/cache@v4 | ||
|
This file contains 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 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 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/lima-vm/go-qcow2reader | ||
|
||
go 1.20 | ||
go 1.22 |
This file contains 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 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 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,52 @@ | ||
package qemuio | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os/exec" | ||
|
||
"github.com/lima-vm/go-qcow2reader/test/qemuimg" | ||
) | ||
|
||
// Write writes a number of bytes at a specified offset, allocating all clusters | ||
// in specified range. | ||
func Write(path string, format qemuimg.Format, off, len int64, pattern byte) error { | ||
// qemu-io -f qcow2 -c 'write -P pattern off len' disk.qcow2 | ||
command := fmt.Sprintf("write -P %d %d %d", pattern, off, len) | ||
_, err := qemuIo([]string{"-f", string(format), "-c", command, path}) | ||
return err | ||
} | ||
|
||
// Zero writes zeros at a specified offset. The behavior depens on qcow2 | ||
// version; In qcow2 v3, allocate zero clusters, marming entire cluster as zero. | ||
// In qcow2 v2, if the cluster are unallocated and there is no backing file, do | ||
// nothing. Otherwise allocate clusters and write actual zeros. | ||
func Zero(path string, format qemuimg.Format, off, len int64) error { | ||
// qemu-io -f qcow2 -c 'write -z off len' disk.qcow2 | ||
command := fmt.Sprintf("write -z %d %d", off, len) | ||
_, err := qemuIo([]string{"-f", string(format), "-c", command, path}) | ||
return err | ||
} | ||
|
||
// Discard unmap number of bytes at specified offset. Allocated cluster are | ||
// deaallocated and replaced with zero clusters. | ||
func Discard(path string, format qemuimg.Format, off, len int64, unmap bool) error { | ||
// qemu-io -f qcow2 -c 'write -zu off len' disk.qcow2 | ||
command := fmt.Sprintf("write -zu %d %d", off, len) | ||
_, err := qemuIo([]string{"-f", string(format), "-c", command, path}) | ||
return err | ||
} | ||
|
||
func qemuIo(args []string) ([]byte, error) { | ||
cmd := exec.Command("qemu-io", args...) | ||
var stderr bytes.Buffer | ||
cmd.Stderr = &stderr | ||
out, err := cmd.Output() | ||
if err != nil { | ||
if _, ok := err.(*exec.ExitError); ok { | ||
return out, fmt.Errorf("%w: stderr=%q", err, stderr.String()) | ||
} | ||
return out, err | ||
} | ||
return out, nil | ||
} |