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

Function CreateBlockDevice

internal/fsutil.go:76–106  ·  view source on GitHub ↗

CreateBlockDevice creates a block device, or block special file for testing

(ctx context.Context, t testing.TB)

Source from the content-addressed store, hash-verified

74
75// CreateBlockDevice creates a block device, or block special file for testing
76func CreateBlockDevice(ctx context.Context, t testing.TB) (string, func()) {
77 t.Helper()
78
79 f, err := os.CreateTemp("", "")
80 require.NoError(t, err)
81
82 err = f.Truncate(32 * mib)
83 require.NoError(t, err)
84
85 out, err := exec.CommandContext(ctx, "/usr/sbin/mkfs.ext4", "-v", f.Name()).CombinedOutput()
86 require.NoErrorf(t, err, "failed to create ext img, command out:%s \n", string(out))
87
88 err = f.Close()
89 require.NoError(t, err)
90
91 out, err = exec.CommandContext(ctx, "losetup", "--show", "--find", f.Name()).CombinedOutput()
92 require.NoError(t, err)
93
94 device := strings.TrimRight(string(out), "\n")
95
96 err = os.Chmod(device, 0600)
97 require.NoError(t, err, "failed to change file mode for the new created block device")
98
99 return device, func() {
100 out, err := exec.CommandContext(ctx, "losetup", "--detach", device).CombinedOutput()
101 if len(out) > 0 {
102 t.Logf("losetup --detach: %s", out)
103 }
104 require.NoError(t, err)
105 }
106}
107
108// MountInfo holds data parsed from a line of /proc/mounts
109type MountInfo struct {

Callers 1

testAttachBlockDeviceFunction · 0.92

Calls 2

NameMethod · 0.65
CloseMethod · 0.65

Tested by 1

testAttachBlockDeviceFunction · 0.74