fix: support qemu backing file with json format

This commit is contained in:
Qiu Jian
2022-04-01 21:08:39 +08:00
parent 8da40d66ee
commit cb89eae95e
2 changed files with 37 additions and 2 deletions
+26 -2
View File
@@ -157,7 +157,10 @@ func (img *SQemuImage) parse() error {
img.ClusterSize = info.ClusterSize
img.Compat = info.FormatSpecific.Data.Compat
img.Encrypted = info.Encrypted
img.BackFilePath = info.FullBackingFilename
img.BackFilePath, err = parseBackingFilepath(info.FullBackingFilename)
if err != nil {
return errors.Wrap(err, "parseBackingFilepath")
}
img.Subformat = info.CreateType
if img.Encrypted {
@@ -525,7 +528,11 @@ func (img *SQemuImage) CreateQcow2(sizeMB int, compact bool, backPath string, pa
if backQemu.Encrypted {
info.EncrptKeySecret = "sec0"
}
extraArgs = append(extraArgs, "-b", fmt.Sprintf("json:%s", jsonutils.Marshal(info)))
if backQemu.Encrypted {
extraArgs = append(extraArgs, "-b", fmt.Sprintf("json:%s", jsonutils.Marshal(info)))
} else {
extraArgs = append(extraArgs, "-b", backPath)
}
if !compact {
options = append(options, "cluster_size=2M")
}
@@ -683,3 +690,20 @@ func (img *SQemuImage) Check() error {
}
return nil
}
// "json:{\"driver\":\"qcow2\",\"file\":{\"driver\":\"file\",\"filename\":\"/opt/cloud/workspace/disks/snapshots/72a2383d-e980-486f-816c-6c562e1757f3_snap/f39f225a-921f-492e-8fb6-0a4167d6ed91\"}}"
func parseBackingFilepath(pathInfo string) (string, error) {
if strings.HasPrefix(pathInfo, "json:{") {
pathJson, err := jsonutils.ParseString(pathInfo[len("json:"):])
if err != nil {
return "", errors.Wrap(err, "jsonutils.ParseString")
}
path, err := pathJson.GetString("file", "filename")
if err != nil {
return "", errors.Wrap(err, "GetString file.filename")
}
return path, nil
} else {
return pathInfo, nil
}
}
+11
View File
@@ -173,3 +173,14 @@ func TestVmdk(t *testing.T) {
t.Logf("%s %v", img, img.IsSparse())
img.Delete()
}*/
func TestParseBackingFile(t *testing.T) {
in := `json:{"driver":"qcow2","file":{"driver":"file","filename":"/opt/cloud/workspace/disks/snapshots/72a2383d-e980-486f-816c-6c562e1757f3_snap/f39f225a-921f-492e-8fb6-0a4167d6ed91"}}`
want := "/opt/cloud/workspace/disks/snapshots/72a2383d-e980-486f-816c-6c562e1757f3_snap/f39f225a-921f-492e-8fb6-0a4167d6ed91"
path, err := parseBackingFilepath(in)
if err != nil {
t.Errorf("parseBackingFilepath: %s", err)
} else if path != want {
t.Errorf("want: %s got: %s", want, path)
}
}