Skip to content

Commit

Permalink
feat(LargeBlock): read the VG device to attach, detach and delete
Browse files Browse the repository at this point in the history
Signed-off-by: Damien Thenot <[email protected]>
  • Loading branch information
Nambrok committed Apr 3, 2024
1 parent 8ef44bc commit ee5e639
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions drivers/LargeBlockSR.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ def load(self, sr_uuid):

def attach(self, sr_uuid):
if not self.is_deleting:
vg_device = self._get_device()
self.dconf["device"] = ",".join(vg_device)
self._create_emulated_device()
if not self._is_vg_connection_correct(): # Check if we need to redo the connection by parsing `vgs -o vg_name,devices self.vgname`
self._redo_vg_connection() # Call redo VG connection to connect it correctly to the loop device instead of the real 4KiB block device
super(LargeBlockSR, self).attach(sr_uuid)

def detach(self, sr_uuid):
vg_device = self._get_device()

self.dconf["device"] = ",".join(vg_device)
super(LargeBlockSR, self).detach(sr_uuid)
if not self.is_deleting:
self._destroy_emulated_device()
Expand All @@ -89,6 +94,7 @@ def create(self, sr_uuid, size):
self._destroy_emulated_device(base_devices)

def delete(self, sr_uuid):
self.dconf["device"] = ",".join(self._get_device())
base_devices = self.dconf["device"].split(",")

self.is_deleting = True
Expand Down Expand Up @@ -152,18 +158,44 @@ def _get_loopdev_from_device(device):
lpdevs.append(loopdev)
return lpdevs

@staticmethod
def _get_device_from_loopdev(loopdevs):
devices = []
output = util.pread2(["losetup", "--list"]).rstrip()
if output:
for line in output.split("\n"):
line = line.split()
lpdev = line[0]
dev = line[5]
if lpdev in loopdevs:
devices.append(dev)
return devices

def _get_device_from_vg(self):
devices = []
output = util.pread2(["vgs", "--noheadings", "-o", "vg_name,devices", self.vgname]).splitlines()
for line in output:
line = line.split()
devices.append(line[1].split("(")[0])
dev = line[1].split("(")[0]
if os.path.islink(dev):
dev = os.path.realpath(dev)
devices.append(dev)
return devices

def _get_device(self):
vg_device = self._get_device_from_vg()
for dev in vg_device:
if re.match(r"(.*\.512)|(/dev/loop[0-9]+)", dev):
lpdev = os.path.realpath(dev)
realdev = self._get_device_from_loopdev(lpdev)[0]
vg_device.remove(dev)
vg_device.append(realdev)

return vg_device

def _is_vg_connection_correct(self):
output = util.pread2(["vgs", "--noheadings", "-o", "vg_name,devices", self.vgname]).split()
if self.vgname == output[0]:
output[1] = output[1].split("(")[0]
output[1] = output[1].split("(")[0]
return bool(re.match(r"(.*\.512)|(/dev/loop[0-9]+)", output[1]))

def _redo_vg_connection(self):
Expand Down

0 comments on commit ee5e639

Please sign in to comment.