Skip to content

Commit

Permalink
fix start/end_sector keys in partition methods
Browse files Browse the repository at this point in the history
(cherry picked from commit 6a48fc9)
  • Loading branch information
yocalebo authored and bugclerk committed Jun 28, 2024
1 parent 2e2c9c4 commit 61d02bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
14 changes: 9 additions & 5 deletions src/middlewared/middlewared/plugins/device_/device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import libsgio
from middlewared.plugins.disk_.enums import DISKS_TO_IGNORE
from middlewared.plugins.disk_.disk_info import get_partition_size_info
from middlewared.schema import Dict, returns
from middlewared.service import Service, accepts, private
from middlewared.utils.functools import cache
Expand Down Expand Up @@ -100,6 +101,9 @@ def get_disk_partitions(self, dev):
for i in filter(lambda x: all(x.get(k) for k in keys), dev.children):
part_num = int(i['ID_PART_ENTRY_NUMBER'])
part_name = self.middleware.call_sync('disk.get_partition_for_disk', parent, part_num)
lbs, start_sector, end_sector, sect_size = get_partition_size_info(
parent, int(i['ID_PART_ENTRY_OFFSET']), int(i['ID_PART_ENTRY_SIZE'])
)
part = {
'name': part_name,
'id': part_name,
Expand All @@ -109,13 +113,13 @@ def get_disk_partitions(self, dev):
'partition_type': i['ID_PART_ENTRY_TYPE'],
'partition_number': part_num,
'partition_uuid': i['ID_PART_ENTRY_UUID'],
'start_sector': int(i['ID_PART_ENTRY_OFFSET']),
'end_sector': int(i['ID_PART_ENTRY_OFFSET']) + int(i['ID_PART_ENTRY_SIZE']) - 1,
'start_sector': start_sector,
'end_sector': end_sector,
'encrypted_provider': None,
}
part['start'] = part['start_sector'] * 512
part['end'] = part['end_sector'] * 512
part['size'] = int(i['ID_PART_ENTRY_SIZE']) * 512
part['start'] = start_sector * lbs # bytes
part['end'] = end_sector * lbs # bytes
part['size'] = sect_size * lbs # bytes

for attr in filter(lambda x: x.startswith('holders/md'), i.attributes.available_attributes):
# looks like `holders/md123`
Expand Down
39 changes: 33 additions & 6 deletions src/middlewared/middlewared/plugins/disk_/disk_info.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import glob
import os
import pathlib
Expand All @@ -7,6 +8,31 @@
from middlewared.service import CallError, private, Service


def get_partition_size_info(disk_name, s_offset, s_size):
"""Kernel sysfs reports most disk files related to "size" in 512 bytes.
To properly calculate the starting SECTOR of partitions, you must
look at logical_block_size (again, reported in 512 bytes) and
do some calculations. It is _very_ important to do this properly
since almost all userspace tools that format disks expect partition
positions to be in sectors."""
lbs = start_sect = end_sect = sect_size = 0
with contextlib.suppress(FileNotFoundError, ValueError):
with open(f'/sys/block/{disk_name}/queue/logical_block_size') as f:
lbs = int(f.read().strip())

if not lbs:
# this should never happen
return lbs, start_sect, end_sect, sect_size

# important when dealing with 4kn drives
divisor = lbs // 512
start_sect = s_offset // divisor
sect_size = s_size // divisor
end_sect = sect_size + start_sect - 1

return lbs, start_sect, end_sect, sect_size


class DiskService(Service):

@private
Expand All @@ -32,22 +58,23 @@ def list_partitions(self, disk):
if not bd.children:
return parts

req_keys = ('ID_PART_' + i for i in ('TYPE', 'UUID', 'NUMBER', 'SIZE'))
req_keys = ('ID_PART_ENTRY_' + i for i in ('TYPE', 'UUID', 'NUMBER', 'SIZE'))
for p in filter(lambda p: all(p.get(k) for k in req_keys), bd.children):
part_name = self.get_partition_for_disk(disk, p['ID_PART_ENTRY_NUMBER'])
start_sector = int(p['ID_PART_ENTRY_OFFSET'])
end_sector = int(p['ID_PART_ENTRY_OFFSET']) + int(p['ID_PART_ENTRY_SIZE']) - 1
lbs, start_sector, end_sector, sect_size = get_partition_size_info(
disk, int(p['ID_PART_ENTRY_OFFSET']), int(p['ID_PART_ENTRY_SIZE'])
)
part = {
'name': part_name,
'partition_type': p['ID_PART_ENTRY_TYPE'],
'partition_number': int(p['ID_PART_ENTRY_NUMBER']),
'partition_uuid': p['ID_PART_ENTRY_UUID'],
'disk': disk,
'start_sector': start_sector,
'start': start_sector * 512,
'start': start_sector * lbs, # bytes
'end_sector': end_sector,
'end': end_sector * 512,
'size': int(p['ID_PART_ENTRY_SIZE']) * 512,
'end': end_sector * lbs, # bytes
'size': sect_size * lbs, # bytes
'id': part_name,
'path': os.path.join('/dev', part_name),
'encrypted_provider': None,
Expand Down

0 comments on commit 61d02bd

Please sign in to comment.