Skip to content

Commit 010b4da

Browse files
committed
devmapper: implement dmsetup status
Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent 1e893b1 commit 010b4da

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

snapshots/devmapper/dmsetup/dmsetup.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,53 @@ func Version() (string, error) {
263263
return dmsetup("version")
264264
}
265265

266+
// DeviceStatus represents devmapper device status information
267+
type DeviceStatus struct {
268+
Offset int64
269+
Length int64
270+
Target string
271+
Params []string
272+
}
273+
274+
// Status provides status information for devmapper device
275+
func Status(deviceName string) (*DeviceStatus, error) {
276+
var (
277+
err error
278+
status DeviceStatus
279+
)
280+
281+
output, err := dmsetup("status", deviceName)
282+
if err != nil {
283+
return nil, err
284+
}
285+
286+
// Status output format:
287+
// Offset (int64)
288+
// Length (int64)
289+
// Target type (string)
290+
// Params (Array of strings)
291+
const MinParseCount = 4
292+
parts := strings.Split(output, " ")
293+
if len(parts) < MinParseCount {
294+
return nil, errors.Errorf("failed to parse output: %q", output)
295+
}
296+
297+
status.Offset, err = strconv.ParseInt(parts[0], 10, 64)
298+
if err != nil {
299+
return nil, errors.Wrapf(err, "failed to parse offset: %q", parts[0])
300+
}
301+
302+
status.Length, err = strconv.ParseInt(parts[1], 10, 64)
303+
if err != nil {
304+
return nil, errors.Wrapf(err, "failed to parse length: %q", parts[1])
305+
}
306+
307+
status.Target = parts[2]
308+
status.Params = parts[3:]
309+
310+
return &status, nil
311+
}
312+
266313
// GetFullDevicePath returns full path for the given device name (like "/dev/mapper/name")
267314
func GetFullDevicePath(deviceName string) string {
268315
if strings.HasPrefix(deviceName, DevMapperDir) {

snapshots/devmapper/dmsetup/dmsetup_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ import (
2424
"strings"
2525
"testing"
2626

27-
"github.com/containerd/containerd/pkg/testutil"
28-
"github.com/containerd/containerd/snapshots/devmapper/losetup"
2927
"github.com/docker/go-units"
3028
"golang.org/x/sys/unix"
3129
"gotest.tools/assert"
3230
is "gotest.tools/assert/cmp"
31+
32+
"github.com/containerd/containerd/pkg/testutil"
33+
"github.com/containerd/containerd/snapshots/devmapper/losetup"
3334
)
3435

3536
const (
@@ -83,6 +84,7 @@ func TestDMSetup(t *testing.T) {
8384
t.Run("DeleteSnapshot", testDeleteSnapshot)
8485

8586
t.Run("ActivateDevice", testActivateDevice)
87+
t.Run("DeviceStatus", testDeviceStatus)
8688
t.Run("SuspendResumeDevice", testSuspendResumeDevice)
8789
t.Run("RemoveDevice", testRemoveDevice)
8890

@@ -139,6 +141,16 @@ func testActivateDevice(t *testing.T) {
139141
assert.Assert(t, info.TableLive)
140142
}
141143

144+
func testDeviceStatus(t *testing.T) {
145+
status, err := Status(testDeviceName)
146+
assert.NilError(t, err)
147+
148+
assert.Equal(t, int64(0), status.Offset)
149+
assert.Equal(t, int64(2), status.Length)
150+
assert.Equal(t, "thin", status.Target)
151+
assert.DeepEqual(t, status.Params, []string{"0", "-"})
152+
}
153+
142154
func testSuspendResumeDevice(t *testing.T) {
143155
err := SuspendDevice(testDeviceName)
144156
assert.NilError(t, err)

0 commit comments

Comments
 (0)