Skip to content

Commit 70f53b0

Browse files
authored
🐛 Use single mountpoint from lsblk. (#5866)
Signed-off-by: Vasil Sirakov <sirakov97@gmail.com>
1 parent 2e8ce88 commit 70f53b0

File tree

4 files changed

+71
-88
lines changed

4 files changed

+71
-88
lines changed

providers/os/connection/snapshot/blockdevices.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ type BlockDevices struct {
2222
}
2323

2424
type BlockDevice struct {
25-
Name string `json:"name,omitempty"`
26-
FsType string `json:"fstype,omitempty"`
27-
Label string `json:"label,omitempty"`
28-
Uuid string `json:"uuid,omitempty"`
29-
PartUuid string `json:"partuuid,omitempty"`
30-
MountPoints []string `json:"mountpoints,omitempty"`
31-
Children []*BlockDevice `json:"children,omitempty"`
32-
Size Size `json:"size,omitempty"`
25+
Name string `json:"name,omitempty"`
26+
FsType string `json:"fstype,omitempty"`
27+
Label string `json:"label,omitempty"`
28+
Uuid string `json:"uuid,omitempty"`
29+
PartUuid string `json:"partuuid,omitempty"`
30+
MountPoint string `json:"mountpoint,omitempty"`
31+
Children []*BlockDevice `json:"children,omitempty"`
32+
Size Size `json:"size,omitempty"`
3333

3434
// This is how the device was requested/searched as. It might differ from the actual name
3535
// e.g. we treat /dev/sdm the same as /dev/xvdm, so RequestedName can be '/dev/sdm' while Name is '/dev/xvdm'
@@ -68,7 +68,7 @@ func (s *Size) UnmarshalJSON(data []byte) error {
6868
}
6969

7070
func (cmdRunner *LocalCommandRunner) GetBlockDevices() (*BlockDevices, error) {
71-
cmd, err := cmdRunner.RunCommand("lsblk -bo NAME,SIZE,FSTYPE,MOUNTPOINTS,LABEL,UUID,PARTUUID --json")
71+
cmd, err := cmdRunner.RunCommand("lsblk -bo NAME,SIZE,FSTYPE,MOUNTPOINT,LABEL,UUID,PARTUUID --json")
7272
if err != nil {
7373
return nil, err
7474
}
@@ -209,7 +209,7 @@ func (device *BlockDevice) GetPartitions(includeBoot bool, includeMounted bool)
209209

210210
// skip mounted partitions unless includeMounted is true
211211
if partition.isMounted() && !includeMounted {
212-
log.Debug().Str("name", partition.Name).Strs("mountpoints", partition.MountPoints).Msg("skipping mounted partition")
212+
log.Debug().Str("name", partition.Name).Str("mountpoint", partition.MountPoint).Msg("skipping mounted partition")
213213
return false
214214
}
215215

@@ -235,7 +235,7 @@ func (device *BlockDevice) GetPartitions(includeBoot bool, includeMounted bool)
235235
log.Debug().
236236
Str("name", partition.Name).
237237
Str("fs_type", partition.FsType).
238-
Strs("mountpoints", partition.MountPoints).
238+
Str("mountpoint", partition.MountPoint).
239239
Msg("skipping partition, because the filter did not match")
240240
}
241241
}

providers/os/connection/snapshot/blockdevices_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestFindDevice(t *testing.T) {
6262
t.Run("match by exact name", func(t *testing.T) {
6363
blockEntries := BlockDevices{
6464
BlockDevices: []*BlockDevice{
65-
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoints: []string{"/"}}}},
65+
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoint: "/"}}},
6666
{Name: "nvme0n1", Children: []*BlockDevice{{Uuid: "12345", FsType: "xfs", Label: "ROOT", Name: "nvmd1n1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
6767
{Name: "sdx", Children: []*BlockDevice{{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sdh1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
6868
},
@@ -77,7 +77,7 @@ func TestFindDevice(t *testing.T) {
7777
t.Run("match by alias name", func(t *testing.T) {
7878
blockEntries := BlockDevices{
7979
BlockDevices: []*BlockDevice{
80-
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoints: []string{"/"}}}},
80+
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoint: "/"}}},
8181
{Name: "nvme0n1", Children: []*BlockDevice{{Uuid: "12345", FsType: "xfs", Label: "ROOT", Name: "nvmd1n1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
8282
{Name: "sdx", Aliases: []string{"xvdx"}, Children: []*BlockDevice{{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sdh1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
8383
},
@@ -92,7 +92,7 @@ func TestFindDevice(t *testing.T) {
9292
t.Run("match by interchangeable name", func(t *testing.T) {
9393
blockEntries := BlockDevices{
9494
BlockDevices: []*BlockDevice{
95-
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoints: []string{"/"}}}},
95+
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoint: "/"}}},
9696
{Name: "nvme0n1", Children: []*BlockDevice{{Uuid: "12345", FsType: "xfs", Label: "ROOT", Name: "nvmd1n1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
9797
{Name: "xvdc", Children: []*BlockDevice{{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sdh1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
9898
},
@@ -107,7 +107,7 @@ func TestFindDevice(t *testing.T) {
107107
t.Run("no match", func(t *testing.T) {
108108
blockEntries := BlockDevices{
109109
BlockDevices: []*BlockDevice{
110-
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoints: []string{"/"}}}},
110+
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoint: "/"}}},
111111
{Name: "nvme0n1", Children: []*BlockDevice{{Uuid: "12345", FsType: "xfs", Label: "ROOT", Name: "nvmd1n1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
112112
{Name: "xvdc", Children: []*BlockDevice{{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sdh1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
113113
},
@@ -121,7 +121,7 @@ func TestFindDevice(t *testing.T) {
121121
t.Run("multiple matches by trailing letter", func(t *testing.T) {
122122
blockEntries := BlockDevices{
123123
BlockDevices: []*BlockDevice{
124-
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoints: []string{"/"}}}},
124+
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoint: "/"}}},
125125
{Name: "nvme0n1", Children: []*BlockDevice{{Uuid: "12345", FsType: "xfs", Label: "ROOT", Name: "nvmd1n1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
126126
{Name: "stc", Children: []*BlockDevice{{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sdh1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
127127
{Name: "xvdc", Children: []*BlockDevice{{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sdh1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
@@ -137,7 +137,7 @@ func TestFindDevice(t *testing.T) {
137137
t.Run("perfect match and trailing letter matches", func(t *testing.T) {
138138
blockEntries := BlockDevices{
139139
BlockDevices: []*BlockDevice{
140-
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoints: []string{"/"}}}},
140+
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoint: "/"}}},
141141
{Name: "nvme0n1", Children: []*BlockDevice{{Uuid: "12345", FsType: "xfs", Label: "ROOT", Name: "nvmd1n1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
142142
{Name: "sta", Children: []*BlockDevice{{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sdh1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
143143
{Name: "xvda", Children: []*BlockDevice{{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sdh1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
@@ -156,7 +156,7 @@ func TestFindDevice(t *testing.T) {
156156
{Name: "xvda", Children: []*BlockDevice{{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sdh1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
157157
{Name: "sta", Children: []*BlockDevice{{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sdh1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
158158
{Name: "nvme0n1", Children: []*BlockDevice{{Uuid: "12345", FsType: "xfs", Label: "ROOT", Name: "nvmd1n1"}, {Uuid: "12345", FsType: "", Label: "EFI"}}},
159-
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoints: []string{"/"}}}},
159+
{Name: "sda", Children: []*BlockDevice{{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoint: "/"}}},
160160
},
161161
}
162162

@@ -172,7 +172,7 @@ func TestGetMountablePartition(t *testing.T) {
172172
block := BlockDevice{
173173
Name: "sda",
174174
Children: []*BlockDevice{
175-
{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoints: []string{"/"}},
175+
{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoint: "/"},
176176
},
177177
}
178178
_, err := block.GetMountablePartition()
@@ -184,7 +184,7 @@ func TestGetMountablePartition(t *testing.T) {
184184
block := BlockDevice{
185185
Name: "sda",
186186
Children: []*BlockDevice{
187-
{Uuid: "1234", FsType: "", Label: "ROOT", Name: "sda1", MountPoints: []string{}},
187+
{Uuid: "1234", FsType: "", Label: "ROOT", Name: "sda1", MountPoint: ""},
188188
},
189189
}
190190
_, err := block.GetMountablePartition()
@@ -196,7 +196,7 @@ func TestGetMountablePartition(t *testing.T) {
196196
block := BlockDevice{
197197
Name: "sda",
198198
Children: []*BlockDevice{
199-
{Uuid: "1234", FsType: "xfs", Label: "EFI", Name: "sda1", MountPoints: []string{}},
199+
{Uuid: "1234", FsType: "xfs", Label: "EFI", Name: "sda1", MountPoint: ""},
200200
},
201201
}
202202
_, err := block.GetMountablePartition()
@@ -208,7 +208,7 @@ func TestGetMountablePartition(t *testing.T) {
208208
block := BlockDevice{
209209
Name: "sda",
210210
Children: []*BlockDevice{
211-
{Uuid: "1234", FsType: "vfat", Label: "", Name: "sda1", MountPoints: []string{}},
211+
{Uuid: "1234", FsType: "vfat", Label: "", Name: "sda1", MountPoint: ""},
212212
},
213213
}
214214
_, err := block.GetMountablePartition()
@@ -220,7 +220,7 @@ func TestGetMountablePartition(t *testing.T) {
220220
block := BlockDevice{
221221
Name: "sda",
222222
Children: []*BlockDevice{
223-
{Uuid: "1234", FsType: "xfs", Label: "boot", Name: "sda1", MountPoints: []string{}},
223+
{Uuid: "1234", FsType: "xfs", Label: "boot", Name: "sda1", MountPoint: ""},
224224
},
225225
}
226226
_, err := block.GetMountablePartition()
@@ -270,11 +270,11 @@ func TestGetPartitions(t *testing.T) {
270270
Name: "sda",
271271
Children: []*BlockDevice{
272272
// already mounted
273-
{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoints: []string{"/"}},
274-
{Uuid: "12345", FsType: "xfs", Label: "ROOT", Name: "sda2", MountPoints: []string{}},
275-
{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sda3", MountPoints: []string{}},
273+
{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoint: "/"},
274+
{Uuid: "12345", FsType: "xfs", Label: "ROOT", Name: "sda2", MountPoint: ""},
275+
{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sda3", MountPoint: ""},
276276
// no fs type
277-
{Uuid: "12347", FsType: "", Label: "ROOT", Name: "sda4", MountPoints: []string{}},
277+
{Uuid: "12347", FsType: "", Label: "ROOT", Name: "sda4", MountPoint: ""},
278278
},
279279
}
280280
parts, err := block.GetPartitions(true, false)
@@ -306,11 +306,11 @@ func TestGetPartitions(t *testing.T) {
306306
Name: "sda",
307307
Children: []*BlockDevice{
308308
// already mounted
309-
{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoints: []string{"/"}},
310-
{Uuid: "12345", FsType: "xfs", Label: "ROOT", Name: "sda2", MountPoints: []string{}},
311-
{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sda3", MountPoints: []string{}},
309+
{Uuid: "1234", FsType: "xfs", Label: "ROOT", Name: "sda1", MountPoint: "/"},
310+
{Uuid: "12345", FsType: "xfs", Label: "ROOT", Name: "sda2", MountPoint: ""},
311+
{Uuid: "12346", FsType: "xfs", Label: "ROOT", Name: "sda3", MountPoint: ""},
312312
// no fs type
313-
{Uuid: "12347", FsType: "", Label: "ROOT", Name: "sda4", MountPoints: []string{}},
313+
{Uuid: "12347", FsType: "", Label: "ROOT", Name: "sda4", MountPoint: ""},
314314
},
315315
}
316316
parts, err := block.GetPartitions(true, true)
@@ -327,11 +327,11 @@ func TestGetPartitions(t *testing.T) {
327327
block := BlockDevice{
328328
Name: "sda",
329329
Children: []*BlockDevice{
330-
{Uuid: "1234", FsType: "fat32", Label: "EFI", Name: "sda1", MountPoints: []string{}},
330+
{Uuid: "1234", FsType: "fat32", Label: "EFI", Name: "sda1", MountPoint: ""},
331331
{
332-
Uuid: "12345", FsType: "lvm2_member", Label: "LVM", Name: "sda2", MountPoints: []string{}, Children: []*BlockDevice{
333-
{Uuid: "lv12346", FsType: "lvm", Label: "ROOT", Name: "rootvg-rootlv", MountPoints: []string{}},
334-
{Uuid: "lv12347", FsType: "lvm", Label: "HOME", Name: "rootvg-homelv", MountPoints: []string{}},
332+
Uuid: "12345", FsType: "lvm2_member", Label: "LVM", Name: "sda2", MountPoint: "", Children: []*BlockDevice{
333+
{Uuid: "lv12346", FsType: "lvm", Label: "ROOT", Name: "rootvg-rootlv", MountPoint: ""},
334+
{Uuid: "lv12347", FsType: "lvm", Label: "HOME", Name: "rootvg-homelv", MountPoint: ""},
335335
},
336336
},
337337
},

providers/os/connection/snapshot/partition.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,5 @@ func (entry BlockDevice) isNoBootVolume() bool {
7474
}
7575

7676
func (entry BlockDevice) isMounted() bool {
77-
if len(entry.MountPoints) == 1 && entry.MountPoints[0] == "" {
78-
// This is a special case where the partition is not mounted
79-
return false
80-
}
81-
return len(entry.MountPoints) > 0
77+
return entry.MountPoint != ""
8278
}

providers/os/connection/snapshot/partition_test.go

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,55 @@ import (
1212
func TestIsNoBootVolume(t *testing.T) {
1313
t.Run("is not boot", func(t *testing.T) {
1414
block := BlockDevice{
15-
Uuid: "12345",
16-
FsType: "xfs",
17-
Label: "label",
18-
Name: "sda2",
19-
MountPoints: []string{},
15+
Uuid: "12345",
16+
FsType: "xfs",
17+
Label: "label",
18+
Name: "sda2",
19+
MountPoint: "",
2020
}
2121
require.True(t, block.isNoBootVolume())
2222
})
2323

2424
t.Run("is boot (boot label)", func(t *testing.T) {
2525
block := BlockDevice{
26-
Uuid: "12345",
27-
FsType: "vfat",
28-
Label: "BOOT",
29-
Name: "sda1",
30-
MountPoints: []string{},
26+
Uuid: "12345",
27+
FsType: "vfat",
28+
Label: "BOOT",
29+
Name: "sda1",
30+
MountPoint: "",
3131
}
3232
require.False(t, block.isNoBootVolume())
3333
})
3434

3535
t.Run("is boot (vfat label)", func(t *testing.T) {
3636
block := BlockDevice{
37-
Uuid: "12345",
38-
FsType: "vfat",
39-
Label: "vfat",
40-
Name: "sda1",
41-
MountPoints: []string{"/boot"},
37+
Uuid: "12345",
38+
FsType: "vfat",
39+
Label: "vfat",
40+
Name: "sda1",
41+
MountPoint: "/boot",
4242
}
4343
require.False(t, block.isNoBootVolume())
4444
})
4545

4646
t.Run("is boot (efi label)", func(t *testing.T) {
4747
block := BlockDevice{
48-
Uuid: "12345",
49-
FsType: "vfat",
50-
Label: "efi",
51-
Name: "sda1",
52-
MountPoints: []string{"/boot"},
48+
Uuid: "12345",
49+
FsType: "vfat",
50+
Label: "efi",
51+
Name: "sda1",
52+
MountPoint: "/boot",
5353
}
5454
require.False(t, block.isNoBootVolume())
5555
})
5656

5757
t.Run("is boot (empty uuid)", func(t *testing.T) {
5858
block := BlockDevice{
59-
Uuid: "",
60-
FsType: "vfat",
61-
Label: "test",
62-
Name: "sda1",
63-
MountPoints: []string{"/boot"},
59+
Uuid: "",
60+
FsType: "vfat",
61+
Label: "test",
62+
Name: "sda1",
63+
MountPoint: "/boot",
6464
}
6565
require.False(t, block.isNoBootVolume())
6666
})
@@ -69,35 +69,22 @@ func TestIsNoBootVolume(t *testing.T) {
6969
func TestIsMounted(t *testing.T) {
7070
t.Run("is mounted", func(t *testing.T) {
7171
block := BlockDevice{
72-
Uuid: "12345",
73-
FsType: "xfs",
74-
Label: "label",
75-
Name: "sda2",
76-
MountPoints: []string{"/mnt"},
72+
Uuid: "12345",
73+
FsType: "xfs",
74+
Label: "label",
75+
Name: "sda2",
76+
MountPoint: "/mnt",
7777
}
7878
require.True(t, block.isMounted())
7979
})
8080

8181
t.Run("is not mounted", func(t *testing.T) {
8282
block := BlockDevice{
83-
Uuid: "12345",
84-
FsType: "xfs",
85-
Label: "label",
86-
Name: "sda2",
87-
MountPoints: []string{},
88-
}
89-
require.False(t, block.isMounted())
90-
})
91-
92-
t.Run("is not mounted (special case)", func(t *testing.T) {
93-
block := BlockDevice{
94-
Uuid: "12345",
95-
FsType: "xfs",
96-
Label: "label",
97-
Name: "sda2",
98-
// lsblk returns an empty string for unmounted partitions
99-
// and not an empty array
100-
MountPoints: []string{""},
83+
Uuid: "12345",
84+
FsType: "xfs",
85+
Label: "label",
86+
Name: "sda2",
87+
MountPoint: "",
10188
}
10289
require.False(t, block.isMounted())
10390
})

0 commit comments

Comments
 (0)