@@ -9,10 +9,74 @@ import (
99 "strings"
1010)
1111
12+ type InfoChild struct {
13+ Name string `json:"name,omitempty"` // since QEMU 8.0
14+ Info Info `json:"info,omitempty"` // since QEMU 8.0
15+ }
16+
17+ type InfoFormatSpecific struct {
18+ Type string `json:"type,omitempty"` // since QEMU 1.7
19+ Data json.RawMessage `json:"data,omitempty"` // since QEMU 1.7
20+ }
21+
22+ func (sp * InfoFormatSpecific ) Qcow2 () * InfoFormatSpecificDataQcow2 {
23+ if sp .Type != "qcow2" {
24+ return nil
25+ }
26+ var x InfoFormatSpecificDataQcow2
27+ if err := json .Unmarshal (sp .Data , & x ); err != nil {
28+ panic (err )
29+ }
30+ return & x
31+ }
32+
33+ func (sp * InfoFormatSpecific ) Vmdk () * InfoFormatSpecificDataVmdk {
34+ if sp .Type != "vmdk" {
35+ return nil
36+ }
37+ var x InfoFormatSpecificDataVmdk
38+ if err := json .Unmarshal (sp .Data , & x ); err != nil {
39+ panic (err )
40+ }
41+ return & x
42+ }
43+
44+ type InfoFormatSpecificDataQcow2 struct {
45+ Compat string `json:"compat,omitempty"` // since QEMU 1.7
46+ LazyRefcounts bool `json:"lazy-refcounts,omitempty"` // since QEMU 1.7
47+ Corrupt bool `json:"corrupt,omitempty"` // since QEMU 2.2
48+ RefcountBits int `json:"refcount-bits,omitempty"` // since QEMU 2.3
49+ CompressionType string `json:"compression-type,omitempty"` // since QEMU 5.1
50+ ExtendedL2 bool `json:"extended-l2,omitempty"` // since QEMU 5.2
51+ }
52+
53+ type InfoFormatSpecificDataVmdk struct {
54+ CreateType string `json:"create-type,omitempty"` // since QEMU 1.7
55+ CID int `json:"cid,omitempty"` // since QEMU 1.7
56+ ParentCID int `json:"parent-cid,omitempty"` // since QEMU 1.7
57+ Extents []InfoFormatSpecificDataVmdkExtent `json:"extents,omitempty"` // since QEMU 1.7
58+ }
59+
60+ type InfoFormatSpecificDataVmdkExtent struct {
61+ Filename string `json:"filename,omitempty"` // since QEMU 1.7
62+ Format string `json:"format,omitempty"` // since QEMU 1.7
63+ VSize int64 `json:"virtual-size,omitempty"` // since QEMU 1.7
64+ ClusterSize int `json:"cluster-size,omitempty"` // since QEMU 1.7
65+ }
66+
1267// Info corresponds to the output of `qemu-img info --output=json FILE`
1368type Info struct {
14- Format string `json:"format,omitempty"` // since QEMU 1.3
15- VSize int64 `json:"virtual-size,omitempty"`
69+ Filename string `json:"filename,omitempty"` // since QEMU 1.3
70+ Format string `json:"format,omitempty"` // since QEMU 1.3
71+ VSize int64 `json:"virtual-size,omitempty"` // since QEMU 1.3
72+ ActualSize int64 `json:"actual-size,omitempty"` // since QEMU 1.3
73+ DirtyFlag bool `json:"dirty-flag,omitempty"` // since QEMU 5.2
74+ ClusterSize int `json:"cluster-size,omitempty"` // since QEMU 1.3
75+ BackingFilename string `json:"backing-filename,omitempty"` // since QEMU 1.3
76+ FullBackingFilename string `json:"full-backing-filename,omitempty"` // since QEMU 1.3
77+ BackingFilenameFormat string `json:"backing-filename-format,omitempty"` // since QEMU 1.3
78+ FormatSpecific * InfoFormatSpecific `json:"format-specific,omitempty"` // since QEMU 1.7
79+ Children []InfoChild `json:"children,omitempty"` // since QEMU 8.0
1680}
1781
1882func ConvertToRaw (source string , dest string ) error {
@@ -27,6 +91,14 @@ func ConvertToRaw(source string, dest string) error {
2791 return nil
2892}
2993
94+ func ParseInfo (b []byte ) (* Info , error ) {
95+ var imgInfo Info
96+ if err := json .Unmarshal (b , & imgInfo ); err != nil {
97+ return nil , err
98+ }
99+ return & imgInfo , nil
100+ }
101+
30102func GetInfo (f string ) (* Info , error ) {
31103 var stdout , stderr bytes.Buffer
32104 cmd := exec .Command ("qemu-img" , "info" , "--output=json" , "--force-share" , f )
@@ -36,11 +108,7 @@ func GetInfo(f string) (*Info, error) {
36108 return nil , fmt .Errorf ("failed to run %v: stdout=%q, stderr=%q: %w" ,
37109 cmd .Args , stdout .String (), stderr .String (), err )
38110 }
39- var imgInfo Info
40- if err := json .Unmarshal (stdout .Bytes (), & imgInfo ); err != nil {
41- return nil , err
42- }
43- return & imgInfo , nil
111+ return ParseInfo (stdout .Bytes ())
44112}
45113
46114func DetectFormat (f string ) (string , error ) {
0 commit comments