|
5 | 5 | package simulator
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "errors" |
8 | 9 | "fmt"
|
9 | 10 | "os"
|
10 |
| - "strconv" |
| 11 | + "path/filepath" |
11 | 12 | "strings"
|
12 | 13 |
|
13 | 14 | "github.com/vmware/govmomi/internal"
|
14 | 15 | "github.com/vmware/govmomi/vim25/methods"
|
15 | 16 | "github.com/vmware/govmomi/vim25/mo"
|
16 | 17 | "github.com/vmware/govmomi/vim25/soap"
|
17 | 18 | "github.com/vmware/govmomi/vim25/types"
|
| 19 | + "github.com/vmware/govmomi/vmdk" |
18 | 20 | )
|
19 | 21 |
|
20 | 22 | type VirtualDiskManager struct {
|
@@ -46,79 +48,72 @@ func vdmCreateVirtualDisk(ctx *Context, op types.VirtualDeviceConfigSpecFileOper
|
46 | 48 |
|
47 | 49 | shouldReplace := op == types.VirtualDeviceConfigSpecFileOperationReplace
|
48 | 50 | shouldExist := op == ""
|
49 |
| - for _, name := range vdmNames(file) { |
50 |
| - _, err := os.Stat(name) |
51 |
| - if err == nil { |
52 |
| - if shouldExist { |
53 |
| - return nil |
54 |
| - } |
55 |
| - if shouldReplace { |
56 |
| - if err = os.Truncate(file, 0); err != nil { |
57 |
| - return fm.fault(name, err, new(types.CannotCreateFile)) |
58 |
| - } |
59 |
| - return nil |
60 |
| - } |
61 |
| - return fm.fault(name, nil, new(types.FileAlreadyExists)) |
62 |
| - } else if shouldExist { |
63 |
| - return fm.fault(name, nil, new(types.FileNotFound)) |
64 |
| - } |
65 | 51 |
|
66 |
| - f, err := os.Create(name) |
67 |
| - if err != nil { |
68 |
| - return fm.fault(name, err, new(types.CannotCreateFile)) |
| 52 | + _, err := os.Stat(file) |
| 53 | + if err == nil { |
| 54 | + if shouldExist { |
| 55 | + return nil |
| 56 | + } |
| 57 | + if !shouldReplace { |
| 58 | + return fm.fault(file, nil, new(types.FileAlreadyExists)) |
69 | 59 | }
|
| 60 | + } else if shouldExist { |
| 61 | + return fm.fault(file, nil, new(types.FileNotFound)) |
| 62 | + } |
70 | 63 |
|
71 |
| - if req.Spec != nil { |
72 |
| - var spec any = req.Spec |
73 |
| - fileBackedSpec, ok := spec.(*types.FileBackedVirtualDiskSpec) |
| 64 | + backing := VirtualDiskBackingFileName(file) |
74 | 65 |
|
75 |
| - if !ok { |
76 |
| - return fm.fault(name, nil, new(types.FileFault)) |
77 |
| - } |
| 66 | + extent := vmdk.Extent{ |
| 67 | + Info: filepath.Base(backing), |
| 68 | + } |
78 | 69 |
|
79 |
| - if _, err = f.WriteString(strconv.FormatInt(fileBackedSpec.CapacityKb, 10)); err != nil { |
80 |
| - return fm.fault(name, err, new(types.FileFault)) |
81 |
| - } |
| 70 | + f, err := os.Create(file) |
| 71 | + if err != nil { |
| 72 | + return fm.fault(file, err, new(types.CannotCreateFile)) |
| 73 | + } |
| 74 | + |
| 75 | + defer f.Close() |
| 76 | + |
| 77 | + if req.Spec != nil { |
| 78 | + spec, ok := req.Spec.(types.BaseFileBackedVirtualDiskSpec) |
| 79 | + if !ok { |
| 80 | + return fm.fault(file, nil, new(types.FileFault)) |
82 | 81 | }
|
83 | 82 |
|
84 |
| - _ = f.Close() |
| 83 | + fileSpec := spec.GetFileBackedVirtualDiskSpec() |
| 84 | + extent.Size = fileSpec.CapacityKb * 1024 / vmdk.SectorSize |
| 85 | + } |
| 86 | + |
| 87 | + desc := vmdk.NewDescriptor(extent) |
| 88 | + if err := desc.Write(f); err != nil { |
| 89 | + return fm.fault(file, err, new(types.FileFault)) |
85 | 90 | }
|
86 | 91 |
|
| 92 | + b, err := os.Create(backing) |
| 93 | + if err != nil { |
| 94 | + return fm.fault(backing, err, new(types.CannotCreateFile)) |
| 95 | + } |
| 96 | + _ = b.Close() |
| 97 | + |
87 | 98 | return nil
|
88 | 99 | }
|
89 | 100 |
|
90 | 101 | func vdmExtendVirtualDisk(ctx *Context, req *types.ExtendVirtualDisk_Task) types.BaseMethodFault {
|
91 | 102 | fm := ctx.Map.FileManager()
|
92 | 103 |
|
93 |
| - file, fault := fm.resolve(ctx, req.Datacenter, req.Name) |
| 104 | + desc, file, fault := fm.DiskDescriptor(ctx, req.Datacenter, req.Name) |
94 | 105 | if fault != nil {
|
95 | 106 | return fault
|
96 | 107 | }
|
97 | 108 |
|
98 |
| - for _, name := range vdmNames(file) { |
99 |
| - _, err := os.Stat(name) |
100 |
| - if err == nil { |
101 |
| - content, err := os.ReadFile(name) |
102 |
| - if err != nil { |
103 |
| - return fm.fault(name, err, new(types.FileFault)) |
104 |
| - } |
105 |
| - |
106 |
| - capacity, err := strconv.Atoi(string(content)) |
107 |
| - if err != nil { |
108 |
| - return fm.fault(name, err, new(types.FileFault)) |
109 |
| - } |
110 |
| - |
111 |
| - if int64(capacity) > req.NewCapacityKb { |
112 |
| - // cannot shrink disk |
113 |
| - return fm.fault(name, nil, new(types.FileFault)) |
114 |
| - } |
115 |
| - return nil |
116 |
| - } else { |
117 |
| - return fm.fault(name, nil, new(types.FileNotFound)) |
118 |
| - } |
| 109 | + newCapacity := req.NewCapacityKb * 1024 |
| 110 | + if desc.Capacity() > newCapacity { |
| 111 | + return fm.fault(req.Name, errors.New("cannot shrink disk"), new(types.FileFault)) |
119 | 112 | }
|
120 | 113 |
|
121 |
| - return nil |
| 114 | + desc.Extent[0].Size = newCapacity / vmdk.SectorSize |
| 115 | + |
| 116 | + return fm.SaveDiskDescriptor(ctx, desc, file) |
122 | 117 | }
|
123 | 118 |
|
124 | 119 | func (m *VirtualDiskManager) CreateVirtualDiskTask(ctx *Context, req *types.CreateVirtualDisk_Task) soap.HasFault {
|
|
0 commit comments