Skip to content

Commit 05e5ce0

Browse files
NambrokWescoeur
authored andcommitted
feat(LargeBlock): backport of largeblocksr (#51) (#55)
A SR inheriting from a EXTSR allowing to use a 4KiB blocksize device as SR. Create a 512 bytes loop device on top of a 4KiB device then give it to EXTSR code. It uses the same device-config as a normal local SR, i.e. `device-config:device=/dev/nvme0n1` After creation, the driver find the device under the VG to identify the correct disk. It means that creating the SR with a non-stable disk identifier is doable and it will work as EXTSR would by ignoring the device-config after creation. Identifying the correct disk by using LVM infos. The VG is created using a different prefix name from EXTSR. It is `XSLocalLargeBlock-<SR UUID>`. The SR artificially limits the creation to disk not being 512b. It will throw an error if a disk whose blocksize is 512 is given. We currently don't support multi devices, it fails at the EXTSR creation. We added an error to explicitly say that multi devices SR is not supported on the driver. Before that, it would make another error: ``` Error code: SR_BACKEND_FAILURE_77 Error parameters: , Logical Volume group creation failed, ``` Sometimes the pvremove from EXTSR using the loop device fails. In this case, we need to remove the real device from PV list ourself in the error handling. Signed-off-by: Damien Thenot <[email protected]>
1 parent eea1352 commit 05e5ce0

File tree

6 files changed

+281
-4
lines changed

6 files changed

+281
-4
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ SM_DRIVERS += XFS
2424
SM_DRIVERS += ZFS
2525
SM_DRIVERS += EXT4
2626
SM_DRIVERS += MooseFS
27+
SM_DRIVERS += LargeBlock
2728

2829
SM_LIBS := SR
2930
SM_LIBS += SRCommand

drivers/LargeBlockSR.py

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright (C) 2024 Vates SAS - [email protected]
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
import SR
18+
from SR import deviceCheck
19+
import SRCommand
20+
import EXTSR
21+
import util
22+
import xs_errors
23+
import os
24+
import re
25+
import lvutil
26+
27+
CAPABILITIES = ["SR_PROBE", "SR_UPDATE", "SR_SUPPORTS_LOCAL_CACHING",
28+
"VDI_CREATE", "VDI_DELETE", "VDI_ATTACH", "VDI_DETACH",
29+
"VDI_UPDATE", "VDI_CLONE", "VDI_SNAPSHOT", "VDI_RESIZE", "VDI_MIRROR",
30+
"VDI_GENERATE_CONFIG",
31+
"VDI_RESET_ON_BOOT/2", "ATOMIC_PAUSE", "VDI_CONFIG_CBT",
32+
"VDI_ACTIVATE", "VDI_DEACTIVATE", "THIN_PROVISIONING", "VDI_READ_CACHING"]
33+
34+
CONFIGURATION = [['device', 'local device path (required) (e.g. /dev/sda3)']]
35+
36+
DRIVER_INFO = {
37+
'name': 'Large Block SR',
38+
'description': 'SR plugin which emulates a 512 bytes disk on top of a 4KiB device then create a EXT SR',
39+
'vendor': 'Vates',
40+
'copyright': '(C) 2024 Vates',
41+
'driver_version': '1.0',
42+
'required_api_version': '1.0',
43+
'capabilities': CAPABILITIES,
44+
'configuration': CONFIGURATION
45+
}
46+
47+
LARGEBLOCK_PREFIX = "XSLocalLargeBlock-"
48+
49+
class LargeBlockSR(EXTSR.EXTSR):
50+
"""Emulating 512b drives for EXT storage repository"""
51+
52+
DRIVER_TYPE = "largeblock"
53+
LOOP_SECTOR_SIZE = 512
54+
55+
@staticmethod
56+
def handles(srtype):
57+
return srtype == LargeBlockSR.DRIVER_TYPE
58+
59+
def load(self, sr_uuid):
60+
super(LargeBlockSR, self).load(sr_uuid)
61+
self.is_deleting = False
62+
self.vgname = LARGEBLOCK_PREFIX + sr_uuid
63+
self.remotepath = os.path.join("/dev", self.vgname, sr_uuid)
64+
65+
def attach(self, sr_uuid):
66+
if not self.is_deleting:
67+
vg_device = self._get_device()
68+
self.dconf["device"] = ",".join(vg_device)
69+
self._create_emulated_device()
70+
if not self._is_vg_connection_correct(): # Check if we need to redo the connection by parsing `vgs -o vg_name,devices self.vgname`
71+
self._redo_vg_connection() # Call redo VG connection to connect it correctly to the loop device instead of the real 4KiB block device
72+
super(LargeBlockSR, self).attach(sr_uuid)
73+
74+
def detach(self, sr_uuid):
75+
if not self.is_deleting:
76+
vg_device = self._get_device()
77+
self.dconf["device"] = ",".join(vg_device)
78+
super(LargeBlockSR, self).detach(sr_uuid)
79+
if not self.is_deleting:
80+
self._destroy_emulated_device()
81+
82+
@deviceCheck
83+
def create(self, sr_uuid, size):
84+
base_devices = self.dconf["device"].split(",")
85+
if len(base_devices) > 1:
86+
raise xs_errors.XenError("ConfigDeviceInvalid", opterr="Multiple devices configuration is not supported")
87+
88+
for dev in base_devices:
89+
logical_blocksize = util.pread2(["blockdev", "--getss", dev]).strip()
90+
if logical_blocksize == "512":
91+
raise xs_errors.XenError("LargeBlockIncorrectBlocksize", opterr="The logical blocksize of the device {} is compatible with normal SR types".format(dev))
92+
93+
try:
94+
self._create_emulated_device()
95+
super(LargeBlockSR, self).create(sr_uuid, size)
96+
finally:
97+
self._destroy_emulated_device(base_devices)
98+
99+
def delete(self, sr_uuid):
100+
base_devices = self._get_device()
101+
self.dconf["device"] = ",".join(self._get_loopdev_from_device(base_devices))
102+
103+
self.is_deleting = True
104+
try:
105+
super(LargeBlockSR, self).delete(sr_uuid)
106+
except SR.SROSError:
107+
# In case, the lvremove doesn't like the loop device, it will throw an error.
108+
# We need to remove the device ourselves using the real device in this case.
109+
for dev in base_devices:
110+
util.pread2(["pvremove", dev])
111+
finally:
112+
self._destroy_emulated_device(base_devices)
113+
self.is_deleting = False
114+
115+
@deviceCheck
116+
def probe(self):
117+
# We override EXTSR.probe because it uses EXT_PREFIX in this call
118+
return lvutil.srlist_toxml(
119+
lvutil.scan_srlist(LARGEBLOCK_PREFIX, self.dconf['device']),
120+
LARGEBLOCK_PREFIX
121+
)
122+
123+
def _create_loopdev(self, dev, emulated_path):
124+
cmd = ["losetup", "-f", "-v", "--show", "--sector-size", str(self.LOOP_SECTOR_SIZE), dev]
125+
loopdev = util.pread2(cmd).rstrip()
126+
127+
if os.path.exists(emulated_path) and os.path.islink(emulated_path):
128+
os.unlink(emulated_path)
129+
130+
try:
131+
os.symlink(loopdev, emulated_path)
132+
except OSError:
133+
raise xs_errors.XenError("LargeBlockSymlinkExist", opterr="Symlink {} couldn't be created".format(emulated_path))
134+
135+
def _delete_loopdev(self, dev, emulated_path):
136+
if os.path.exists(emulated_path) and os.path.islink(emulated_path):
137+
os.unlink(emulated_path)
138+
139+
# The backing file isn't a symlink if given by ID in device-config but the real device
140+
dev = os.path.realpath(dev)
141+
loopdevs = self._get_loopdev_from_device(dev)
142+
143+
if loopdevs != None:
144+
try:
145+
for lp in loopdevs:
146+
cmd = ["losetup", "-d", lp] # Remove the loop device
147+
util.pread2(cmd)
148+
except SR.SROSError:
149+
util.SMlog("Couldn't removed losetup devices: {}".format(loopdevs))
150+
else:
151+
xs_errors.XenError("LargeBlockNoLosetup", opterr="Couldn't find loop device for {}".format(dev))
152+
153+
@staticmethod
154+
def _get_loopdev_from_device(device):
155+
lpdevs = []
156+
output = util.pread2(["losetup", "--list"]).rstrip()
157+
if output:
158+
for line in output.split("\n"):
159+
line = line.split()
160+
loopdev = line[0]
161+
dev = line[5].strip()
162+
if dev in device:
163+
lpdevs.append(loopdev)
164+
return lpdevs
165+
166+
@staticmethod
167+
def _get_device_from_loopdev(loopdevs):
168+
devices = []
169+
output = util.pread2(["losetup", "--list"]).rstrip()
170+
if output:
171+
for line in output.split("\n"):
172+
line = line.split()
173+
lpdev = line[0]
174+
dev = line[5]
175+
if lpdev in loopdevs:
176+
devices.append(dev)
177+
return devices
178+
179+
def _get_device_from_vg(self):
180+
devices = []
181+
output = util.pread2(["vgs", "--noheadings", "-o", "vg_name,devices", self.vgname]).splitlines()
182+
for line in output:
183+
line = line.split()
184+
dev = line[1].split("(")[0]
185+
if os.path.islink(dev):
186+
dev = os.path.realpath(dev)
187+
devices.append(dev)
188+
return devices
189+
190+
def _get_device(self):
191+
vg_device = self._get_device_from_vg()
192+
for dev in vg_device:
193+
if re.match(r"(.*\.512)|(/dev/loop[0-9]+)", dev):
194+
lpdev = os.path.realpath(dev)
195+
realdev = self._get_device_from_loopdev(lpdev)[0]
196+
vg_device.remove(dev)
197+
vg_device.append(realdev)
198+
199+
return vg_device
200+
201+
def _is_vg_connection_correct(self):
202+
output = util.pread2(["vgs", "--noheadings", "-o", "vg_name,devices", self.vgname]).split()
203+
output[1] = output[1].split("(")[0]
204+
return bool(re.match(r"(.*\.512)|(/dev/loop[0-9]+)", output[1]))
205+
206+
def _redo_vg_connection(self):
207+
"""
208+
In case of using a LargeBlockSR, the LVM scan at boot will find the LogicalVolume on the real block device.
209+
And when the PBD is connecting, it will mount from the original device instead of the loop device since LVM prefers real devices it has seen first.
210+
The PBD plug will succeed but then the SR will be accessed through the 4KiB device, returning to the erroneous behaviour on 4KiB device.
211+
VM won't be able to run because vhd-util will fail to scan the VDI.
212+
This function force the LogicalVolume to be mounted on top of our emulation layer by disabling the VolumeGroup and re-enabling while applying a filter.
213+
"""
214+
215+
util.SMlog("Reconnecting VG {} to use emulated device".format(self.vgname))
216+
try:
217+
lvutil.setActiveVG(self.vgname, False)
218+
lvutil.setActiveVG(self.vgname, True, config="devices{ global_filter = [ \"r|^/dev/nvme.*|\", \"a|/dev/loop.*|\" ] }")
219+
except util.CommandException as e:
220+
xs_errors.XenError("LargeBlockVGReconnectFailed", opterr="Failed to reconnect the VolumeGroup {}, error: {}".format(self.vgname, e))
221+
222+
223+
@classmethod
224+
def _get_emulated_device_path(cls, dev):
225+
return "{dev}.{bs}".format(dev=dev, bs=cls.LOOP_SECTOR_SIZE)
226+
227+
def _create_emulated_device(self):
228+
base_devices = self.dconf["device"].split(",")
229+
emulated_devices = []
230+
for dev in base_devices:
231+
emulated_path = self._get_emulated_device_path(dev)
232+
self._create_loopdev(dev, emulated_path)
233+
emulated_devices.append(emulated_path)
234+
235+
emulated_devices = ",".join(emulated_devices)
236+
self.dconf["device"] = emulated_devices
237+
238+
def _destroy_emulated_device(self, devices=None):
239+
if devices is None:
240+
devices = self.dconf["device"].split(",")
241+
242+
for dev in devices:
243+
emulated_path = self._get_emulated_device_path(dev)
244+
self._delete_loopdev(dev, emulated_path)
245+
246+
if __name__ == '__main__':
247+
SRCommand.run(LargeBlockSR, DRIVER_INFO)
248+
else:
249+
SR.registerSR(LargeBlockSR)

drivers/XE_SR_ERRORCODES.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,4 +947,26 @@
947947
<description>LINSTOR SR delete error</description>
948948
<value>5007</value>
949949
</code>
950+
951+
<code>
952+
<name>LargeBlockSymlinkExist</name>
953+
<description>Symlink already exists</description>
954+
<value>5008</value>
955+
</code>
956+
957+
<code>
958+
<name>LargeBlockNoLosetup</name>
959+
<description>Couldn't find loop device</description>
960+
<value>5009</value>
961+
</code>
962+
<code>
963+
<name>LargeBlockIncorrectBlocksize</name>
964+
<description>Blocksize isn't compatible with the driver</description>
965+
<value>5010</value>
966+
</code>
967+
<code>
968+
<name>LargeBlockVGReconnectFailed</name>
969+
<description>Failed to reconnect the VolumeGroup</description>
970+
<value>5011</value>
971+
</code>
950972
</SM-errorcodes>

drivers/cleanup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3123,7 +3123,7 @@ def normalizeType(type):
31233123
type = SR.TYPE_LVHD
31243124
if type in [
31253125
"ext", "nfs", "ocfsoiscsi", "ocfsohba", "smb", "cephfs", "glusterfs",
3126-
"moosefs", "xfs", "zfs", "ext4"
3126+
"moosefs", "xfs", "zfs", "ext4", "largeblock"
31273127
]:
31283128
type = SR.TYPE_FILE
31293129
if type in ["linstor"]:

drivers/lvutil.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,17 @@ def resizePV(dev):
566566
cmd_lvm([CMD_PVRESIZE, dev])
567567
except util.CommandException, inst:
568568
util.SMlog("Failed to grow the PV, non-fatal")
569-
570-
def setActiveVG(path, active):
569+
570+
def setActiveVG(path, active, config=None):
571571
"activate or deactivate VG 'path'"
572572
val = "n"
573573
if active:
574574
val = "y"
575-
text = cmd_lvm([CMD_VGCHANGE, "-a" + val, path])
575+
cmd = [CMD_VGCHANGE, "-a" + val, path]
576+
if config:
577+
cmd.append("--config")
578+
cmd.append(config)
579+
cmd_lvm(cmd)
576580

577581
def create(name, size, vgname, tag=None, size_in_percentage=None):
578582
if size_in_percentage:

drivers/on_slave.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def _is_open(session, args):
7676
import SR
7777
import CephFSSR
7878
import EXTSR
79+
import LargeBlockSR
7980
import GlusterFSSR
8081
import LVHDSR
8182
import MooseFSSR

0 commit comments

Comments
 (0)