MCPcopy
hub / github.com/firecracker-microvm/firecracker-containerd / createDiskImage

Method createDiskImage

volume/set.go:114–149  ·  view source on GitHub ↗
(ctx context.Context, size int64)

Source from the content-addressed store, hash-verified

112}
113
114func (vs *Set) createDiskImage(ctx context.Context, size int64) (path string, retErr error) {
115 f, err := os.CreateTemp(vs.tempDir, "createDiskImage")
116 if err != nil {
117 retErr = err
118 return
119 }
120 defer func() {
121 // The file must be closed even in the success case.
122 err := f.Close()
123 if err != nil {
124 retErr = multierror.Append(retErr, err)
125 }
126 // But the file must not be deleted in the success case.
127 if retErr != nil {
128 err = os.Remove(path)
129 if err != nil {
130 retErr = multierror.Append(retErr, err)
131 }
132 }
133 }()
134
135 err = f.Truncate(size)
136 if err != nil {
137 retErr = err
138 return
139 }
140
141 out, err := exec.CommandContext(ctx, "mkfs."+fsType, "-F", f.Name()).CombinedOutput()
142 if err != nil {
143 retErr = fmt.Errorf("failed to execute mkfs.%s: %s: %w", fsType, out, err)
144 return
145 }
146
147 path = f.Name()
148 return
149}
150
151func mountDiskImage(source, target string) error {
152 return mount.All([]mount.Mount{{Type: fsType, Source: source, Options: []string{"loop"}}}, target)

Callers 2

TestCreateDiskImageFunction · 0.95
PrepareDriveMountMethod · 0.95

Calls 3

CloseMethod · 0.65
NameMethod · 0.65
RemoveMethod · 0.45

Tested by 1

TestCreateDiskImageFunction · 0.76