Skip to content

Commit 085293e

Browse files
committed
Revert "refactor: use VHD header to fetch block size"
This reverts commit baa7044. Signed-off-by: Mark Syms <mark.syms@citrix.com>
1 parent 6afc9fa commit 085293e

9 files changed

Lines changed: 35 additions & 113 deletions

File tree

libs/sm/VDI.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ def __init__(self, sr, uuid):
101101
self.description = ''
102102
self.vbds = []
103103
self.size = 0
104-
self._block_size = -1
105104
self.utilisation = 0
106105
self.vdi_type = ''
107106
self.has_child = 0
@@ -121,12 +120,6 @@ def __init__(self, sr, uuid):
121120

122121
self.load(uuid)
123122

124-
@property
125-
def block_size(self):
126-
if self._block_size < 0:
127-
self._block_size = vhdutil.getBlockSize(self.path)
128-
return self._block_size
129-
130123
@staticmethod
131124
def from_uuid(session, vdi_uuid):
132125

libs/sm/cleanup.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -535,19 +535,12 @@ def __init__(self, sr, uuid, raw):
535535
self.sizeVirt = -1
536536
self._sizeVHD = -1
537537
self._sizeAllocated = -1
538-
self._block_size = -1
539538
self._hidden = False
540539
self.parent = None
541540
self.children = []
542541
self._vdiRef = None
543542
self._clearRef()
544543

545-
@property
546-
def block_size(self):
547-
if self._block_size < 0:
548-
self._block_size = vhdutil.getBlockSize(self.path)
549-
return self._block_size
550-
551544
@staticmethod
552545
def extractUuid(path):
553546
raise NotImplementedError("Implement in sub class")
@@ -1085,16 +1078,14 @@ def _getCoalescedSizeData(self):
10851078
blocksParent = self.parent.getVHDBlocks()
10861079
numBlocks = Util.countBits(blocksChild, blocksParent)
10871080
Util.log("Num combined blocks = %d" % numBlocks)
1088-
sizeData = numBlocks * self.block_size
1081+
sizeData = numBlocks * vhdutil.VHD_BLOCK_SIZE
10891082
assert(sizeData <= self.sizeVirt)
10901083
return sizeData
10911084

10921085
def _calcExtraSpaceForCoalescing(self):
10931086
sizeData = self._getCoalescedSizeData()
1094-
sizeCoalesced = sizeData + vhdutil.calcOverheadBitmap(
1095-
sizeData,
1096-
self.block_size
1097-
) + vhdutil.calcOverheadEmpty(self.sizeVirt)
1087+
sizeCoalesced = sizeData + vhdutil.calcOverheadBitmap(sizeData) + \
1088+
vhdutil.calcOverheadEmpty(self.sizeVirt)
10981089
Util.log("Coalesced size = %s" % Util.num2str(sizeCoalesced))
10991090
return sizeCoalesced - self.parent.getSizeVHD()
11001091

@@ -1250,7 +1241,7 @@ def deflate(self):
12501241
self._sizeAllocated = -1
12511242

12521243
def inflateFully(self):
1253-
self.inflate(lvhdutil.calcSizeVHDLV(self.sizeVirt, self.block_size))
1244+
self.inflate(lvhdutil.calcSizeVHDLV(self.sizeVirt))
12541245

12551246
def inflateParentForCoalesce(self):
12561247
"""Inflate the parent only as much as needed for the purposes of
@@ -1476,10 +1467,7 @@ def _queryVHDBlocks(self):
14761467
def _calcExtraSpaceForCoalescing(self):
14771468
if self.parent.raw:
14781469
return 0 # raw parents are never deflated in the first place
1479-
sizeCoalesced = lvhdutil.calcSizeVHDLV(
1480-
self._getCoalescedSizeData(),
1481-
self.block_size
1482-
)
1470+
sizeCoalesced = lvhdutil.calcSizeVHDLV(self._getCoalescedSizeData())
14831471
Util.log("Coalesced size = %s" % Util.num2str(sizeCoalesced))
14841472
return sizeCoalesced - self.parent.sizeLV
14851473

@@ -2821,7 +2809,7 @@ def _finishCoalesceLeaf(self, parent):
28212809
parent.deflate()
28222810

28232811
def _calcExtraSpaceNeeded(self, child, parent):
2824-
return lvhdutil.calcSizeVHDLV(parent.sizeVirt, parent.block_size) - parent.sizeLV
2812+
return lvhdutil.calcSizeVHDLV(parent.sizeVirt) - parent.sizeLV
28252813

28262814
def _handleInterruptedCoalesceLeaf(self):
28272815
entries = self.journaler.getAll(VDI.JRN_LEAF)

libs/sm/drivers/FileSR.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ def resize(self, sr_uuid, vdi_uuid, size):
656656
return VDI.VDI.get_params(self)
657657

658658
# We already checked it is a VDI_TYPE_VHD
659-
size = vhdutil.validate_and_round_vhd_size(int(size), self.block_size)
659+
size = vhdutil.validate_and_round_vhd_size(int(size))
660660

661661
jFile = JOURNAL_FILE_PREFIX + self.uuid
662662
try:

libs/sm/drivers/LVHDSR.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,7 @@ def scan(self, uuid):
721721
util.roundup(lvutil.LVM_SIZE_INCREMENT,
722722
vhdutil.calcOverheadEmpty(lvhdutil.MSIZE))
723723
else:
724-
utilisation = lvhdutil.calcSizeVHDLV(
725-
int(size),
726-
vhdutil.getBlockSize(lvPath)
727-
)
724+
utilisation = lvhdutil.calcSizeVHDLV(int(size))
728725

729726
vdi_ref = self.session.xenapi.VDI.db_introduce(
730727
vdi_uuid,
@@ -992,10 +989,7 @@ def _undoCloneOp(self, lvs, origUuid, baseUuid, clonUuid):
992989

993990
# inflate the parent to fully-allocated size
994991
if base.vdiType == vhdutil.VDI_TYPE_VHD:
995-
fullSize = lvhdutil.calcSizeVHDLV(
996-
vhdInfo.sizeVirt,
997-
vhdutil.getBlockSize(basePath)
998-
)
992+
fullSize = lvhdutil.calcSizeVHDLV(vhdInfo.sizeVirt)
999993
lvhdutil.inflate(self.journaler, self.uuid, baseUuid, fullSize)
1000994

1001995
# rename back
@@ -1184,7 +1178,7 @@ def _undoAllVHDJournals(self):
11841178
util.SMlog("Found VHD journal %s, reverting %s" % (uuid, vdi.path))
11851179
self.lvActivator.activate(uuid, vdi.lvname, False)
11861180
self.lvmCache.activateNoRefcount(jlvName)
1187-
fullSize = lvhdutil.calcSizeVHDLV(vdi.size, vdi.block_size)
1181+
fullSize = lvhdutil.calcSizeVHDLV(vdi.size)
11881182
lvhdutil.inflate(self.journaler, self.uuid, vdi.uuid, fullSize)
11891183
try:
11901184
jFile = os.path.join(self.path, jlvName)
@@ -1195,7 +1189,7 @@ def _undoAllVHDJournals(self):
11951189
util.SMlog("VHD revert failed but VHD ok: removing journal")
11961190
# Attempt to reclaim unused space
11971191
vhdInfo = vhdutil.getVHDInfo(vdi.path, lvhdutil.extractUuid, False)
1198-
NewSize = lvhdutil.calcSizeVHDLV(vhdInfo.sizeVirt, vdi.block_size)
1192+
NewSize = lvhdutil.calcSizeVHDLV(vhdInfo.sizeVirt)
11991193
if NewSize < fullSize:
12001194
lvhdutil.deflate(self.lvmCache, vdi.lvname, int(NewSize))
12011195
lvhdutil.lvRefreshOnAllSlaves(self.session, self.uuid,
@@ -1450,10 +1444,7 @@ def attach(self, sr_uuid, vdi_uuid):
14501444
needInflate = False
14511445
else:
14521446
self._loadThis()
1453-
if (
1454-
self.utilisation >=
1455-
lvhdutil.calcSizeVHDLV(self.size, self.block_size)
1456-
):
1447+
if self.utilisation >= lvhdutil.calcSizeVHDLV(self.size):
14571448
needInflate = False
14581449

14591450
if needInflate:
@@ -1473,7 +1464,7 @@ def detach(self, sr_uuid, vdi_uuid):
14731464
util.SMlog("LVHDVDI.detach for %s" % self.uuid)
14741465
self._loadThis()
14751466
already_deflated = (self.utilisation < \
1476-
lvhdutil.calcSizeVHDLV(self.size, self.block_size))
1467+
lvhdutil.calcSizeVHDLV(self.size))
14771468
needDeflate = True
14781469
if self.vdi_type == vhdutil.VDI_TYPE_RAW or already_deflated:
14791470
needDeflate = False
@@ -1514,7 +1505,7 @@ def resize(self, sr_uuid, vdi_uuid, size):
15141505
'(current size: %d, new size: %d)' % (self.size, size))
15151506
raise xs_errors.XenError('VDISize', opterr='shrinking not allowed')
15161507

1517-
size = vhdutil.validate_and_round_vhd_size(int(size), self.block_size)
1508+
size = vhdutil.validate_and_round_vhd_size(int(size))
15181509

15191510
if size == self.size:
15201511
return VDI.VDI.get_params(self)
@@ -1524,7 +1515,7 @@ def resize(self, sr_uuid, vdi_uuid, size):
15241515
lvSizeNew = util.roundup(lvutil.LVM_SIZE_INCREMENT, size)
15251516
else:
15261517
lvSizeOld = self.utilisation
1527-
lvSizeNew = lvhdutil.calcSizeVHDLV(size, self.block_size)
1518+
lvSizeNew = lvhdutil.calcSizeVHDLV(size)
15281519
if self.sr.provision == "thin":
15291520
# VDI is currently deflated, so keep it deflated
15301521
lvSizeNew = lvSizeOld
@@ -1690,7 +1681,7 @@ def _snapshot(self, snapType, cloneOp=False, cbtlog=None, cbt_consistency=None):
16901681
self.issnap = self.session.xenapi.VDI.get_is_a_snapshot( \
16911682
self.sr.srcmd.params['vdi_ref'])
16921683

1693-
fullpr = lvhdutil.calcSizeVHDLV(self.size, self.block_size)
1684+
fullpr = lvhdutil.calcSizeVHDLV(self.size)
16941685
thinpr = util.roundup(lvutil.LVM_SIZE_INCREMENT, \
16951686
vhdutil.calcOverheadEmpty(lvhdutil.MSIZE))
16961687
lvSizeOrig = thinpr

libs/sm/lvhdutil.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ def calcSizeLV(sizeVHD):
9595
return util.roundup(LVM_SIZE_INCREMENT, sizeVHD)
9696

9797

98-
def calcSizeVHDLV(sizeVirt, block_size=vhdutil.DEFAULT_VHD_BLOCK_SIZE):
98+
def calcSizeVHDLV(sizeVirt):
9999
# all LVHD VDIs have the metadata area preallocated for the maximum
100100
# possible virtual size (for fast online VDI.resize)
101101
metaOverhead = vhdutil.calcOverheadEmpty(MSIZE)
102-
bitmapOverhead = vhdutil.calcOverheadBitmap(sizeVirt, block_size)
102+
bitmapOverhead = vhdutil.calcOverheadBitmap(sizeVirt)
103103
return calcSizeLV(sizeVirt + metaOverhead + bitmapOverhead)
104104

105105

@@ -208,12 +208,7 @@ def setSizeVirt(journaler, srUuid, vdiUuid, size, jFile):
208208
lvName = LV_PREFIX[vhdutil.VDI_TYPE_VHD] + vdiUuid
209209
vgName = VG_PREFIX + srUuid
210210
path = os.path.join(VG_LOCATION, vgName, lvName)
211-
inflate(
212-
journaler,
213-
srUuid,
214-
vdiUuid,
215-
calcSizeVHDLV(size, vhdutil.getBlockSize(path))
216-
)
211+
inflate(journaler, srUuid, vdiUuid, calcSizeVHDLV(size))
217212
vhdutil.setSizeVirt(path, size, jFile)
218213

219214

@@ -238,8 +233,7 @@ def attachThin(journaler, srUuid, vdiUuid):
238233
_tryAcquire(lock)
239234
lvmCache.refresh()
240235
vhdInfo = vhdutil.getVHDInfoLVM(lvName, extractUuid, vgName)
241-
path = os.path.join(VG_LOCATION, vgName, lvName)
242-
newSize = calcSizeVHDLV(vhdInfo.sizeVirt, vhdutil.getBlockSize(path))
236+
newSize = calcSizeVHDLV(vhdInfo.sizeVirt)
243237
currSizeLV = lvmCache.getSize(lvName)
244238
if newSize <= currSizeLV:
245239
return

libs/sm/vhdutil.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
MAX_CHAIN_SIZE = 30 # max VHD parent chain size
3131
VHD_UTIL = "/usr/bin/vhd-util"
3232
OPT_LOG_ERR = "--debug"
33-
DEFAULT_VHD_BLOCK_SIZE = 2 * 1024 * 1024
33+
VHD_BLOCK_SIZE = 2 * 1024 * 1024
3434
VHD_FOOTER_SIZE = 512
3535

3636
# lock to lock the entire SR for short ops
@@ -82,9 +82,9 @@ def calcOverheadEmpty(virtual_size):
8282
return overhead
8383

8484

85-
def calcOverheadBitmap(virtual_size, block_size=DEFAULT_VHD_BLOCK_SIZE):
86-
num_blocks = virtual_size // block_size
87-
if virtual_size % block_size:
85+
def calcOverheadBitmap(virtual_size):
86+
num_blocks = virtual_size // VHD_BLOCK_SIZE
87+
if virtual_size % VHD_BLOCK_SIZE:
8888
num_blocks += 1
8989
return num_blocks * 4096
9090

@@ -93,18 +93,10 @@ def ioretry(cmd, text=True):
9393
return util.ioretry(lambda: util.pread2(cmd, text=text),
9494
errlist=[errno.EIO, errno.EAGAIN])
9595

96-
def getBlockSize(path):
97-
cmd = [VHD_UTIL, "read", "-pn", path]
98-
ret = ioretry(cmd)
99-
for field in ret.split('\n'):
100-
field = field.lstrip()
101-
if not field.startswith("Block size"): continue
102-
return int(field.split(':')[1].lstrip().split()[0])
103-
raise util.SMException("Unable to find block size in VHD with path: {}".format(path))
104-
10596

106-
def convertAllocatedSizeToBytes(size, block_size=DEFAULT_VHD_BLOCK_SIZE):
107-
return size * block_size
97+
def convertAllocatedSizeToBytes(size):
98+
# Assume we have standard 2MB allocation blocks
99+
return size * 2 * 1024 * 1024
108100

109101

110102
def getVHDInfo(path, extractUuidFunction, includeParent=True):
@@ -128,10 +120,7 @@ def getVHDInfo(path, extractUuidFunction, includeParent=True):
128120
vhdInfo.parentUuid = extractUuidFunction(fields[nextIndex])
129121
nextIndex += 1
130122
vhdInfo.hidden = int(fields[nextIndex].replace("hidden: ", ""))
131-
vhdInfo.sizeAllocated = convertAllocatedSizeToBytes(
132-
int(fields[nextIndex+1]),
133-
getBlockSize(path)
134-
)
123+
vhdInfo.sizeAllocated = convertAllocatedSizeToBytes(int(fields[nextIndex+1]))
135124
vhdInfo.path = path
136125
return vhdInfo
137126

@@ -290,7 +279,7 @@ def setSizePhys(path, size, debug=True):
290279
def getAllocatedSize(path):
291280
cmd = [VHD_UTIL, "query", OPT_LOG_ERR, '-a', '-n', path]
292281
ret = ioretry(cmd)
293-
return convertAllocatedSizeToBytes(int(ret), getBlockSize(path))
282+
return convertAllocatedSizeToBytes(int(ret))
294283

295284
def killData(path):
296285
"zero out the disk (kill all data inside the VHD file)"
@@ -417,7 +406,7 @@ def repair(path):
417406
ioretry([VHD_UTIL, 'repair', '-n', path])
418407

419408

420-
def validate_and_round_vhd_size(size, block_size=DEFAULT_VHD_BLOCK_SIZE):
409+
def validate_and_round_vhd_size(size):
421410
""" Take the supplied vhd size, in bytes, and check it is positive and less
422411
that the maximum supported size, rounding up to the next block boundary
423412
"""
@@ -430,7 +419,7 @@ def validate_and_round_vhd_size(size, block_size=DEFAULT_VHD_BLOCK_SIZE):
430419
if size < MIN_VHD_SIZE:
431420
size = MIN_VHD_SIZE
432421

433-
size = util.roundup(block_size, size)
422+
size = util.roundup(VHD_BLOCK_SIZE, size)
434423

435424
return size
436425

tests/test_FileSR.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ def test_create_vdi_vhd(self, mock_vhdutil):
327327
vdi = FakeFileVDI(sr, vdi_uuid)
328328
vdi.vdi_type = vhdutil.VDI_TYPE_VHD
329329
mock_vhdutil.validate_and_round_vhd_size.side_effect = vhdutil.validate_and_round_vhd_size
330-
mock_vhdutil.DEFAULT_VHD_BLOCK_SIZE = vhdutil.DEFAULT_VHD_BLOCK_SIZE
331330

332331
# Act
333332
vdi.create(sr_uuid, vdi_uuid, 20 * 1024 * 1024)

tests/test_LVHDSR.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,9 @@ def setUp(self):
566566
self.mock_lvhdutil.LV_PREFIX = lvhdutil.LV_PREFIX
567567
vhdutil_patcher = mock.patch('sm.drivers.LVHDSR.vhdutil', autospec=True)
568568
self.mock_vhdutil = vhdutil_patcher.start()
569-
self.mock_vhdutil.getBlockSize.return_value = vhdutil.DEFAULT_VHD_BLOCK_SIZE
570569
self.mock_vhdutil.VDI_TYPE_VHD = vhdutil.VDI_TYPE_VHD
571570
self.mock_vhdutil.VDI_TYPE_RAW = vhdutil.VDI_TYPE_RAW
572571
self.mock_vhdutil.MAX_CHAIN_SIZE = vhdutil.MAX_CHAIN_SIZE
573-
vdi_vhdutil_patcher = mock.patch('sm.VDI.vhdutil', autospec=True)
574-
self.mock_vdi_vhdutil = vdi_vhdutil_patcher.start()
575-
self.mock_vdi_vhdutil.getBlockSize.return_value = vhdutil.DEFAULT_VHD_BLOCK_SIZE
576572
lvutil_patcher = mock.patch('sm.drivers.LVHDSR.lvutil', autospec=True)
577573
self.mock_lvutil = lvutil_patcher.start()
578574
vdi_util_patcher = mock.patch('sm.VDI.util', autospec=True)

tests/test_vhdutil.py

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,10 @@ def test_function(args, inp):
376376

377377
context.add_executable(VHD_UTIL, test_function)
378378
from sm.drivers import FileSR
379-
with unittest.mock.patch(
380-
"sm.vhdutil.getBlockSize",
381-
return_value=vhdutil.DEFAULT_VHD_BLOCK_SIZE
382-
):
383-
vhdinfo = vhdutil.getVHDInfo(TEST_VHD_PATH, FileSR.FileVDI.extractUuid)
379+
vhdinfo = vhdutil.getVHDInfo(TEST_VHD_PATH, FileSR.FileVDI.extractUuid)
384380

385-
# Act/Assert
386-
self.assertEqual(18856*2*1024*1024 , vhdinfo.sizeAllocated)
381+
# Act/Assert
382+
self.assertEqual(18856*2*1024*1024 , vhdinfo.sizeAllocated)
387383

388384
@testlib.with_context
389385
def test_get_allocated_size(self, context):
@@ -400,35 +396,11 @@ def test_function(args, inp):
400396
context.add_executable(VHD_UTIL, test_function)
401397

402398
# Act
403-
result = 0
404-
with unittest.mock.patch(
405-
"sm.vhdutil.getBlockSize",
406-
return_value=vhdutil.DEFAULT_VHD_BLOCK_SIZE
407-
):
408-
result = vhdutil.getAllocatedSize(TEST_VHD_NAME)
399+
result = vhdutil.getAllocatedSize(TEST_VHD_NAME)
409400

410401
# Assert
411402
self.assertEqual(18856*2*1024*1024, result)
412403
self.assertEqual(
413404
[VHD_UTIL, "query", "--debug", "-a",
414405
"-n", TEST_VHD_NAME],
415406
call_args)
416-
417-
@testlib.with_context
418-
def test_get_block_size(self, context):
419-
"""
420-
Test that vhdutil.getBlockSize returns the block size in bytes
421-
"""
422-
423-
# Arrange
424-
call_args = None
425-
426-
def test_function(args, inp):
427-
nonlocal call_args
428-
call_args = args
429-
return 0, "Header version: 0x00010000\nBlock size: {} (2 MB)".format(vhdutil.DEFAULT_VHD_BLOCK_SIZE), ""
430-
431-
context.add_executable(VHD_UTIL, test_function)
432-
433-
# Act/Assert
434-
self.assertEqual(vhdutil.DEFAULT_VHD_BLOCK_SIZE, vhdutil.getBlockSize(TEST_VHD_NAME))

0 commit comments

Comments
 (0)