MCPcopy
hub / github.com/etcd-io/bbolt / createEmptyFSImage

Function createEmptyFSImage

tests/dmflakey/dmflakey.go:296–340  ·  view source on GitHub ↗

createEmptyFSImage creates empty filesystem on dataStorePath folder with default size - 10 GiB.

(imgPath string, fsType FSType, mkfsOpt string)

Source from the content-addressed store, hash-verified

294// createEmptyFSImage creates empty filesystem on dataStorePath folder with
295// default size - 10 GiB.
296func createEmptyFSImage(imgPath string, fsType FSType, mkfsOpt string) error {
297 if err := validateFSType(fsType); err != nil {
298 return err
299 }
300
301 mkfs, err := exec.LookPath(fmt.Sprintf("mkfs.%s", fsType))
302 if err != nil {
303 return fmt.Errorf("failed to ensure mkfs.%s: %w", fsType, err)
304 }
305
306 if _, err := os.Stat(imgPath); err == nil {
307 return fmt.Errorf("failed to create image because %s already exists", imgPath)
308 }
309
310 if err := os.MkdirAll(path.Dir(imgPath), 0600); err != nil {
311 return fmt.Errorf("failed to ensure parent directory %s: %w", path.Dir(imgPath), err)
312 }
313
314 f, err := os.Create(imgPath)
315 if err != nil {
316 return fmt.Errorf("failed to create image %s: %w", imgPath, err)
317 }
318
319 if err = func() error {
320 defer f.Close()
321
322 return f.Truncate(defaultImgSize)
323 }(); err != nil {
324 return fmt.Errorf("failed to truncate image %s with %v bytes: %w",
325 imgPath, defaultImgSize, err)
326 }
327
328 args := []string{imgPath}
329 if mkfsOpt != "" {
330 splitArgs := strings.Split(mkfsOpt, " ")
331 args = append(splitArgs, imgPath)
332 }
333
334 output, err := exec.Command(mkfs, args...).CombinedOutput()
335 if err != nil {
336 return fmt.Errorf("failed to mkfs on %s (%s %v) (out: %s): %w",
337 imgPath, mkfs, args, string(output), err)
338 }
339 return nil
340}
341
342// validateFSType validates the fs type input.
343func validateFSType(fsType FSType) error {

Callers 1

InitFlakeyFunction · 0.85

Calls 3

validateFSTypeFunction · 0.85
ErrorfMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected