Skip to content

Commit

Permalink
LargeBlock: refactor private function
Browse files Browse the repository at this point in the history
Signed-off-by: Damien Thenot <[email protected]>
  • Loading branch information
Nambrok committed Mar 15, 2024
1 parent ea0ae24 commit 4a42836
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions drivers/LargeBlockSR.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,40 @@ def load(self, sr_uuid):

def attach(self, sr_uuid):
if not self.is_deleting:
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
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):
super(LargeBlockSR, self).detach(sr_uuid)
if not self.is_deleting:
self.destroy_emulated_device()
self._destroy_emulated_device()

@deviceCheck
def create(self, sr_uuid, size):
#TODO: Need to check blocksize of the device before accepting to create a SR.
old_devices = self.dconf["device"].split(",")
try:
self.create_emulated_device()
self._create_emulated_device()
super(LargeBlockSR, self).create(sr_uuid, size)
finally:
self.destroy_emulated_device(old_devices)
self._destroy_emulated_device(old_devices)

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

self.is_deleting = True
try:
self.create_emulated_device()
self._create_emulated_device()
super(LargeBlockSR, self).delete(sr_uuid=sr_uuid)
except SR.SROSError as e:
util.SMlog(e)
finally:
self.destroy_emulated_device(old_devices)
self._destroy_emulated_device(old_devices)
self.is_deleting = False

def create_loopdev(self, dev, new_path):
def _create_loopdev(self, dev, new_path):
cmd = ["losetup", "-f", "-v", "--show", "--sector-size", str(self.LOOP_SECTOR_SIZE), dev]
self.loopdev = util.pread2(cmd).rstrip()

Expand All @@ -105,25 +105,25 @@ def create_loopdev(self, dev, new_path):
#util.pread2(["losetup", "-d", self.loopdev])
raise xs_errors.XenError("LargeBlockSymlinkExist", opterr="Symlink {} couldn't be created".format(new_path))

def delete_loopdev(self, dev, new_path):
def _delete_loopdev(self, dev, new_path):
if os.path.exists(new_path) and os.path.islink(new_path):
os.unlink(new_path)

# The backing file isn't a symlink if given by ID in device-config but the real device
dev = os.path.realpath(dev)
loopdev = self.get_loopdev_from_device(dev)
loopdevs = self._get_loopdev_from_device(dev)

if loopdev != None:
if loopdevs != None:
try:
for lp in loopdev:
for lp in loopdevs:
cmd = ["losetup", "-d", lp] # Remove the loop device
util.pread2(cmd)
except SR.SROSError:
util.SMlog("Couldn't removed losetup device {}".format(loopdev))
util.SMlog("Couldn't removed losetup devices: {}".format(loopdevs))
else:
xs_errors.XenError("LargeBlockNoLosetup", opterr="Couldn't find loop device for {}".format(dev))

def get_loopdev_from_device(self, device):
def _get_loopdev_from_device(self, device):
lpdevs = []
output = util.pread2(["losetup", "--list"]).rstrip()
if output:
Expand All @@ -135,7 +135,7 @@ def get_loopdev_from_device(self, device):
lpdevs.append(loopdev)
return lpdevs

def is_vg_connection_correct(self):
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]
Expand All @@ -144,7 +144,7 @@ def is_vg_connection_correct(self):
else:
return False

def redo_vg_connection(self):
def _redo_vg_connection(self):
"""
In case of using a LargeBlockSR, the LVM scan at boot will find the LogicalVolume on the real block device.
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.
Expand All @@ -163,27 +163,27 @@ def redo_vg_connection(self):
util.SMlog("Failed to reconnect the VolumeGroup {}, error: {}".format(self.vgname, e))


def get_emulated_device_path(self, dev):
def _get_emulated_device_path(self, dev):
return "{dev}.{bs}".format(dev=dev, bs=self.LOOP_SECTOR_SIZE)

def create_emulated_device(self):
def _create_emulated_device(self):
devices = self.dconf["device"].split(",")
emulated_devices = []
for dev in devices:
new_path = self.get_emulated_device_path(dev)
self.create_loopdev(dev, new_path)
new_path = self._get_emulated_device_path(dev)
self._create_loopdev(dev, new_path)
emulated_devices.append(new_path)

emulated_devices = ",".join(emulated_devices)
self.dconf["device"] = emulated_devices

def destroy_emulated_device(self, devices=None):
def _destroy_emulated_device(self, devices=None):
if devices is None:
devices = self.dconf["device"].split(",")

for dev in devices:
new_path = self.get_emulated_device_path(dev)
self.delete_loopdev(dev, new_path)
new_path = self._get_emulated_device_path(dev)
self._delete_loopdev(dev, new_path)

if __name__ == '__main__':
SRCommand.run(LargeBlockSR, DRIVER_INFO)
Expand Down

0 comments on commit 4a42836

Please sign in to comment.