Skip to content

Commit

Permalink
in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Wescoeur committed Dec 13, 2024
1 parent ddfc8d8 commit 43b0ce7
Show file tree
Hide file tree
Showing 15 changed files with 790 additions and 566 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SM_LIBS += util
SM_LIBS += verifyVHDsOnSR
SM_LIBS += scsiutil
SM_LIBS += scsi_host_rescan
SM_LIBS += cowutil
SM_LIBS += vhdutil
SM_LIBS += linstorjournaler
SM_LIBS += linstorvhdutil
Expand Down
1 change: 0 additions & 1 deletion drivers/CephFSSR.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import cleanup
import lock
import util
import vhdutil
import xs_errors

CAPABILITIES = ["SR_PROBE", "SR_UPDATE",
Expand Down
53 changes: 25 additions & 28 deletions drivers/FileSR.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import SRCommand
import util
import scsiutil
import vhdutil
import lock
import os
import errno
Expand Down Expand Up @@ -109,7 +108,7 @@ def load(self, sr_uuid) -> None:

@override
def create(self, sr_uuid, size) -> None:
""" Create the SR. The path must not already exist, or if it does,
""" Create the SR. The path must not already exist, or if it does,
it must be empty. (This accounts for the case where the user has
mounted a device onto a directory manually and want to use this as the
root of a file-based SR.) """
Expand Down Expand Up @@ -279,7 +278,7 @@ def _loadvdis(self):

pattern = os.path.join(self.path, "*%s" % VdiTypeExtension.VHD)
try:
self.vhds = vhdutil.getAllVHDs(pattern, FileVDI.extractUuid)
self.vhds = CowUtil.getAllInfoFromVG(pattern, FileVDI.extractUuid)
except util.CommandException as inst:
raise xs_errors.XenError('SRScan', opterr="error VHD-scanning " \
"path %s (%s)" % (self.path, inst))
Expand All @@ -296,7 +295,7 @@ def _loadvdis(self):
self.vdis[uuid] = self.vdi(uuid)
# Get the key hash of any encrypted VDIs:
vhd_path = os.path.join(self.path, self.vhds[uuid].path)
key_hash = vhdutil.getKeyHash(vhd_path)
key_hash = cowutil.getKeyHash(vhd_path)
self.vdis[uuid].sm_config_override['key_hash'] = key_hash

# raw VDIs and CBT log files
Expand Down Expand Up @@ -418,18 +417,18 @@ def _check_hardlinks(self) -> bool:
return True

class FileVDI(VDI.VDI):
PARAM_VHD = "vhd"
PARAM_RAW = "raw"
PARAM_VHD = "vhd"
VDI_TYPE = {
PARAM_VHD: VdiType.VHD,
PARAM_RAW: VdiType.RAW
PARAM_RAW: VdiType.RAW,
PARAM_VHD: VdiType.VHD
}

def _find_path_with_retries(self, vdi_uuid, maxretry=5, period=2.0):
vhd_path = os.path.join(self.sr.path, "%s.%s" % \
(vdi_uuid, self.PARAM_VHD))
raw_path = os.path.join(self.sr.path, "%s.%s" % \
(vdi_uuid, self.PARAM_RAW))
vhd_path = os.path.join(self.sr.path, "%s.%s" % \
(vdi_uuid, self.PARAM_VHD))
cbt_path = os.path.join(self.sr.path, "%s.%s" %
(vdi_uuid, CBTLOG_TAG))
found = False
Expand Down Expand Up @@ -581,7 +580,7 @@ def create(self, sr_uuid, vdi_uuid, size) -> str:

if VdiType.isCowImage(self.vdi_type):
try:
size = vhdutil.validate_and_round_vhd_size(int(size))
size = self._cowutil.validateAndRoundImageSize(int(size))
mb = 1024 * 1024
size_mb = size // mb
util.ioretry(lambda: self._create(str(size_mb), self.path))
Expand Down Expand Up @@ -679,19 +678,19 @@ def resize(self, sr_uuid, vdi_uuid, size) -> str:
if size == self.size:
return VDI.VDI.get_params(self)

# We already checked it is a VHD
size = vhdutil.validate_and_round_vhd_size(int(size))
# We already checked it is a cow image.
size = self._cowutil.validateAndRoundImageSize(int(size))

jFile = JOURNAL_FILE_PREFIX + self.uuid
try:
vhdutil.setSizeVirt(self.path, size, jFile)
self._cowutil.setSizeVirt(self.path, size, jFile)
except:
# Revert the operation
vhdutil.revert(self.path, jFile)
self._cowutil.revert(self.path, jFile)
raise xs_errors.XenError('VDISize', opterr='resize operation failed')

old_size = self.size
self.size = vhdutil.getSizeVirt(self.path)
self.size = self._cowutil.getSizeVirt(self.path)
st = util.ioretry(lambda: os.stat(self.path))
self.utilisation = int(st.st_size)

Expand All @@ -711,11 +710,9 @@ def compose(self, sr_uuid, vdi1, vdi2) -> None:
parent_fn = vdi1 + VDI_TYPE_TO_EXTENSION[VdiType.VHD]
parent_path = os.path.join(self.sr.path, parent_fn)
assert(util.pathexists(parent_path))
vhdutil.setParent(self.path, parent_path, False)
vhdutil.setHidden(parent_path)
self._cowutil.setParent(self.path, parent_path, False)
self._cowutil.setHidden(parent_path)
self.sr.session.xenapi.VDI.set_managed(self.sr.srcmd.params['args'][0], False)
util.pread2([vhdutil.VHD_UTIL, "modify", "-p", parent_path,
"-n", self.path])
# Tell tapdisk the chain has changed
if not blktap2.VDI.tap_refresh(self.session, sr_uuid, vdi2):
raise util.SMException("failed to refresh VDI %s" % self.uuid)
Expand All @@ -726,11 +723,11 @@ def reset_leaf(self, sr_uuid, vdi_uuid):
raise xs_errors.XenError('Unimplemented')

# safety check
if not vhdutil.hasParent(self.path):
if not self._cowutil.hasParent(self.path):
raise util.SMException("ERROR: VDI %s has no parent, " + \
"will not reset contents" % self.uuid)

vhdutil.killData(self.path)
self._cowutil.killData(self.path)

@override
def _do_snapshot(self, sr_uuid, vdi_uuid, snapType,
Expand Down Expand Up @@ -796,11 +793,11 @@ def _snapshot(self, snap_type, cbtlog=None, cbt_consistency=None):
if self.hidden:
raise xs_errors.XenError('VDIClone', opterr='hidden VDI')

depth = vhdutil.getDepth(self.path)
depth = self._cowutil.getDepth(self.path)
if depth == -1:
raise xs_errors.XenError('VDIUnavailable', \
opterr='failed to get VHD depth')
elif depth >= vhdutil.MAX_CHAIN_SIZE:
opterr='failed to get image depth')
elif depth >= self._cowutil.getMaxChainLength():
raise xs_errors.XenError('SnapshotChainTooLong')

newuuid = util.gen_uuid()
Expand Down Expand Up @@ -1004,14 +1001,14 @@ def _create(self, size, path):
cmd = [SR.TAPDISK_UTIL, "create", VdiType.VHD, size, path]
text = util.pread(cmd)
if self.key_hash:
vhdutil.setKey(path, self.key_hash)
self._cowutil.setKey(path, self.key_hash)

def _mark_hidden(self, path):
vhdutil.setHidden(path, True)
self._cowutil.setHidden(path, True)
self.hidden = 1

def _is_hidden(self, path):
return vhdutil.getHidden(path) == 1
return self._cowutil.getHidden(path) == 1

def extractUuid(path):
fileName = os.path.basename(path)
Expand Down
66 changes: 33 additions & 33 deletions drivers/LVHDSR.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,17 +732,16 @@ def scan(self, uuid) -> None:
util.roundup(lvutil.LVM_SIZE_INCREMENT,
int(size))
else:
parent = \
vhdutil._getVHDParentNoCheck(lvPath)
parent = cowutil.getParentNoCheck(lvPath)

if parent is not None:
sm_config['vhd-parent'] = parent[len( \
lvhdutil.LV_PREFIX[VdiType.VHD]):]
size = vhdutil.getSizeVirt(lvPath)
size = cowutil.getSizeVirt(lvPath)
if self.provision == "thin":
utilisation = \
util.roundup(lvutil.LVM_SIZE_INCREMENT,
vhdutil.calcOverheadEmpty(lvhdutil.MSIZE))
cowutil.calcOverheadEmpty(lvhdutil.MSIZE))
else:
utilisation = lvhdutil.calcSizeVHDLV(int(size))

Expand Down Expand Up @@ -962,7 +961,7 @@ def _handleInterruptedCloneOp(self, origUuid, jval, forceUndo=False):
parent = vdis[orig.parentUuid]
self.lvActivator.activate(parent.uuid, parent.lvName, False)
origPath = os.path.join(self.path, orig.lvName)
if not vhdutil.check(origPath):
if cowutil.check(origPath) != cowutil.CheckResult.Success:
util.SMlog("Orig VHD invalid => revert")
self._undoCloneOp(lvs, origUuid, baseUuid, clonUuid)
return
Expand All @@ -971,7 +970,7 @@ def _handleInterruptedCloneOp(self, origUuid, jval, forceUndo=False):
clon = vdis[clonUuid]
clonPath = os.path.join(self.path, clon.lvName)
self.lvActivator.activate(clonUuid, clon.lvName, False)
if not vhdutil.check(clonPath):
if cowutil.check(clonPath) != cowutil.CheckResult.Success:
util.SMlog("Clon VHD invalid => revert")
self._undoCloneOp(lvs, origUuid, baseUuid, clonUuid)
return
Expand All @@ -995,10 +994,10 @@ def _undoCloneOp(self, lvs, origUuid, baseUuid, clonUuid):
if VdiType.isCowImage(base.vdiType):
self.lvActivator.activate(baseUuid, base.name, False)
origRefcountNormal = 1
vhdInfo = vhdutil.getVHDInfo(basePath, lvhdutil.extractUuid, False)
if vhdInfo.hidden:
vhdutil.setHidden(basePath, False)
elif base.vdiType == VdiType.RAW and base.hidden:
cow_info = cowutil.getInfo(basePath, lvhdutil.extractUuid, False)
if cow_info.hidden:
cowutil.setHidden(basePath, False)
elif base.hidden:
self.lvmCache.setHidden(base.name, False)

# remove the child nodes
Expand Down Expand Up @@ -1054,7 +1053,7 @@ def _completeCloneOp(self, vdis, origUuid, baseUuid, clonUuid):
self.lvmCache.setHidden(base.lvName)
else:
basePath = os.path.join(self.path, base.lvName)
vhdutil.setHidden(basePath)
cowutil.setHidden(basePath)
if not base.lvReadonly:
self.lvmCache.setReadonly(base.lvName, True)

Expand Down Expand Up @@ -1206,13 +1205,13 @@ def _undoAllVHDJournals(self):
lvhdutil.inflate(self.journaler, self.uuid, vdi.uuid, fullSize)
try:
jFile = os.path.join(self.path, jlvName)
vhdutil.revert(vdi.path, jFile)
cowutil.revert(vdi.path, jFile)
except util.CommandException:
util.logException("VHD journal revert")
vhdutil.check(vdi.path)
util.SMlog("VHD revert failed but VHD ok: removing journal")
# Attempt to reclaim unused space
vhdInfo = vhdutil.getVHDInfo(vdi.path, lvhdutil.extractUuid, False)
vhdInfo = cowutil.getInfo(vdi.path, lvhdutil.extractUuid, False)
NewSize = lvhdutil.calcSizeVHDLV(vhdInfo.sizeVirt)
if NewSize < fullSize:
lvhdutil.deflate(self.lvmCache, vdi.lvname, int(NewSize))
Expand Down Expand Up @@ -1374,7 +1373,8 @@ def create(self, sr_uuid, vdi_uuid, size) -> str:
if self.exists:
raise xs_errors.XenError('VDIExists')

size = vhdutil.validate_and_round_vhd_size(int(size))

size = self._cowutil.validateAndRoundImageSize(int(size))

util.SMlog("LVHDVDI.create: type = %s, %s (size=%s)" % \
(self.vdi_type, self.path, size))
Expand All @@ -1396,8 +1396,8 @@ def create(self, sr_uuid, vdi_uuid, size) -> str:
if self.vdi_type == VdiType.RAW:
self.size = self.sr.lvmCache.getSize(self.lvname)
else:
vhdutil.create(self.path, int(size), False, lvhdutil.MSIZE_MB)
self.size = vhdutil.getSizeVirt(self.path)
self._cowutil.create(self.path, int(size), False, lvhdutil.MSIZE_MB)
self.size = self._cowutil.getSizeVirt(self.path)
self.sr.lvmCache.deactivateNoRefcount(self.lvname)
except util.CommandException as e:
util.SMlog("Unable to create VDI")
Expand Down Expand Up @@ -1555,7 +1555,7 @@ def resize(self, sr_uuid, vdi_uuid, size) -> str:
'(current size: %d, new size: %d)' % (self.size, size))
raise xs_errors.XenError('VDISize', opterr='shrinking not allowed')

size = vhdutil.validate_and_round_vhd_size(int(size))
size = self._cowutil.validateAndRoundImageSize(int(size))

if size == self.size:
return VDI.VDI.get_params(self)
Expand All @@ -1582,8 +1582,8 @@ def resize(self, sr_uuid, vdi_uuid, size) -> str:
if lvSizeNew != lvSizeOld:
lvhdutil.inflate(self.sr.journaler, self.sr.uuid, self.uuid,
lvSizeNew)
vhdutil.setSizeVirtFast(self.path, size)
self.size = vhdutil.getSizeVirt(self.path)
self._cowutil.setSizeVirtFast(self.path, size)
self.size = self._cowutil.getSizeVirt(self.path)
self.utilisation = self.sr.lvmCache.getSize(self.lvname)

vdi_ref = self.sr.srcmd.params['vdi_ref']
Expand Down Expand Up @@ -1613,8 +1613,8 @@ def compose(self, sr_uuid, vdi1, vdi2) -> None:
self.sr.lvActivator.activate(self.uuid, self.lvname, False)
self.sr.lvActivator.activate(parent_uuid, parent_lvname, False)

vhdutil.setParent(self.path, parent_path, False)
vhdutil.setHidden(parent_path)
self._cowutil.setParent(self.path, parent_path, False)
self._cowutil.setHidden(parent_path)
self.sr.session.xenapi.VDI.set_managed(self.sr.srcmd.params['args'][0], False)

if not blktap2.VDI.tap_refresh(self.session, self.sr.uuid, self.uuid,
Expand All @@ -1631,11 +1631,11 @@ def reset_leaf(self, sr_uuid, vdi_uuid):
self.sr.lvActivator.activate(self.uuid, self.lvname, False)

# safety check
if not vhdutil.hasParent(self.path):
if not self._cowutil.hasParent(self.path):
raise util.SMException("ERROR: VDI %s has no parent, " + \
"will not reset contents" % self.uuid)

vhdutil.killData(self.path)
self._cowutil.killData(self.path)

def _attach(self):
self._chainSetActive(True, True, True)
Expand Down Expand Up @@ -1723,11 +1723,11 @@ def _snapshot(self, snapType, cloneOp=False, cbtlog=None, cbt_consistency=None):
opterr='VDI unavailable: %s' % (self.path))

if VdiType.isCowImage(self.vdi_type):
depth = vhdutil.getDepth(self.path)
depth = self._cowutil.getDepth(self.path)
if depth == -1:
raise xs_errors.XenError('VDIUnavailable', \
opterr='failed to get VHD depth')
elif depth >= vhdutil.MAX_CHAIN_SIZE:
elif depth >= self._cowutil.getMaxChainLength():
raise xs_errors.XenError('SnapshotChainTooLong')

self.issnap = self.session.xenapi.VDI.get_is_a_snapshot( \
Expand Down Expand Up @@ -1816,7 +1816,7 @@ def _snapshot(self, snapType, cloneOp=False, cbtlog=None, cbt_consistency=None):
if self.vdi_type == VdiType.RAW:
self.sr.lvmCache.setHidden(self.lvname)
else:
vhdutil.setHidden(self.path)
self._cowutil.setHidden(self.path)
util.fistpoint.activate("LVHDRT_clone_vdi_after_parent_hidden", self.sr.uuid)

# set the base copy to ReadOnly
Expand Down Expand Up @@ -1860,8 +1860,8 @@ def _createSnap(self, snapUuid, snapSizeLV, isNew):
RefCounter.set(snapUuid, 1, 0, lvhdutil.NS_PREFIX_LVM + self.sr.uuid)
self.sr.lvActivator.add(snapUuid, snapLV, False)
parentRaw = (self.vdi_type == VdiType.RAW)
vhdutil.snapshot(snapPath, self.path, parentRaw, lvhdutil.MSIZE_MB)
snapParent = vhdutil.getParent(snapPath, lvhdutil.extractUuid)
self._cowutil.snapshot(snapPath, self.path, parentRaw, lvhdutil.MSIZE_MB)
snapParent = self._cowutil.getParent(snapPath, lvhdutil.extractUuid)

snapVDI = LVHDVDI(self.sr, snapUuid)
snapVDI.read_only = False
Expand Down Expand Up @@ -2068,15 +2068,15 @@ def _determineType(self):
# LVM commands can be costly, so check the file directly first in case
# the LV is active
found = False
for t in lvhdutil.VDI_TYPES:
lvname = "%s%s" % (lvhdutil.LV_PREFIX[t], self.uuid)
for vdiType, prefix in lvhdutil.LV_PREFIX:
lvname = "%s%s" % (prefix, self.uuid)
path = os.path.join(self.sr.path, lvname)
if util.pathexists(path):
if found:
raise xs_errors.XenError('VDILoad',
opterr="multiple VDI's: uuid %s" % self.uuid)
found = True
self.vdi_type = t
self.vdi_type = vdiType
self.lvname = lvname
self.path = path
if found:
Expand Down Expand Up @@ -2110,7 +2110,7 @@ def _loadThis(self):
self._initFromLVInfo(lvs[self.uuid])
if VdiType.isCowImage(self.vdi_type):
self.sr.lvActivator.activate(self.uuid, self.lvname, False)
vhdInfo = vhdutil.getVHDInfo(self.path, lvhdutil.extractUuid, False)
vhdInfo = self._cowutil.getInfo(self.path, lvhdutil.extractUuid, False)
if not vhdInfo:
raise xs_errors.XenError('VDIUnavailable', \
opterr='getVHDInfo failed')
Expand Down Expand Up @@ -2154,7 +2154,7 @@ def _markHidden(self):
if self.vdi_type == VdiType.RAW:
self.sr.lvmCache.setHidden(self.lvname)
else:
vhdutil.setHidden(self.path)
self._cowutil.setHidden(self.path)
self.hidden = 1

def _prepareThin(self, attach):
Expand Down
Loading

0 comments on commit 43b0ce7

Please sign in to comment.