live: fix EBUSY error in newer kernels with mkfs

python-libfdisk does not close file descriptor to /dev/sda after completing
partitioning. This results EBUSY errors when formatting partitions with mkfs
in newer kernels. Encapsulate code to partition in method so python garbage
collection knows ctx objects can be release then close file descritor to
/dev/sda.

ogRest is not accessible from _partition(), remove check to ogRest.terminated,
actually no need to terminate inmediately when formatting is ongoing, better
leave things in consistent state when stop command is received.
master
OpenGnSys Support Team 2024-10-01 16:58:02 +02:00
parent f0953c969a
commit cf9e1c96fd
1 changed files with 33 additions and 31 deletions

View File

@ -454,6 +454,38 @@ class OgLiveOperations:
logging.info('Successful hardware inventory command execution')
return result
def _partition(self, diskname, table_type, partlist):
cxt = fdisk.Context(f'/dev/{diskname}',
details=True)
if table_type == 'MSDOS':
cxt.create_disklabel('dos')
elif table_type == 'GPT':
cxt.create_disklabel('gpt')
else:
raise OgError(f'Unsupported partition scheme {table_type}, only MSDOS and GPT are supported')
logging.info(f'Setting up partition layout to {table_type}')
for part in partlist:
logging.info(f'Creating partition {part["partition"]} with {part["code"]} of {int(part["size"])//1024} MiB')
if part["code"] == 'EMPTY':
continue
pa = fdisk.Partition(start_follow_default=True,
end_follow_default=False,
partno_follow_default=False)
parttype = get_parttype(cxt, part["code"])
size = int(part["size"])
pa.size = (size * (1 << 10)) // cxt.sector_size
pa.partno = int(part["partition"]) - 1
pa.type = parttype
cxt.add_partition(pa)
cxt.write_disklabel()
os.sync()
def setup(self, request, ogRest):
table_type = request.getType()
disk = int(request.getDisk())
@ -471,37 +503,7 @@ class OgLiveOperations:
raise OgError(f'Invalid disk number {disk}, {len(get_disks())} disks available.')
diskname = get_disks()[disk-1]
cxt = fdisk.Context(f'/dev/{diskname}',
details=True)
if table_type == 'MSDOS':
cxt.create_disklabel('dos')
elif table_type == 'GPT':
cxt.create_disklabel('gpt')
else:
raise OgError(f'Unsupported partition scheme {table_type}, only MSDOS and GPT are supported')
logging.info(f'Setting up partition layout to {table_type}')
for part in partlist:
logging.info(f'Creating partition {part["partition"]} with {part["code"]} of {int(part["size"])//1024} MiB')
if part["code"] == 'EMPTY':
continue
if ogRest.terminated:
break
pa = fdisk.Partition(start_follow_default=True,
end_follow_default=False,
partno_follow_default=False)
parttype = get_parttype(cxt, part["code"])
size = int(part["size"])
pa.size = (size * (1 << 10)) // cxt.sector_size
pa.partno = int(part["partition"]) - 1
pa.type = parttype
cxt.add_partition(pa)
cxt.write_disklabel()
os.sync()
self._partition(diskname, table_type, partlist)
ret = subprocess.run(['partprobe', f'/dev/{diskname}'])
logging.info(f'first partprobe /dev/{diskname} reports {ret.returncode}')