Potential solution for #67

It's a 0.025 second sleep waiting for the partition to pop up in partprobe.

Also added a grace period of 10 seconds for that to occur. Otherwise we'll throw an exception since something most likely broke down. (Note here: Older drives, say 6200 RPM spin disks, might take a few seconds to come online. Have no such hardware to test with, but worth testing)
This commit is contained in:
Anton Hvornum 2020-12-07 15:50:47 +01:00
parent 1aef9807f9
commit e637852df0
1 changed files with 13 additions and 7 deletions

View File

@ -1,4 +1,4 @@
import glob, re, os, json import glob, re, os, json, time # Time is only used to gracefully wait for new paritions to come online
from collections import OrderedDict from collections import OrderedDict
from .exceptions import DiskError from .exceptions import DiskError
from .general import * from .general import *
@ -77,7 +77,7 @@ class BlockDevice():
#o = b''.join(sys_command('/usr/bin/lsblk -o name -J -b {dev}'.format(dev=dev))) #o = b''.join(sys_command('/usr/bin/lsblk -o name -J -b {dev}'.format(dev=dev)))
o = b''.join(sys_command(f'/usr/bin/lsblk -J {self.path}')) o = b''.join(sys_command(f'/usr/bin/lsblk -J {self.path}'))
#print(self, 'partitions:', o)
if b'not a block device' in o: if b'not a block device' in o:
raise DiskError(f'Can not read partitions off something that isn\'t a block device: {self.path}') raise DiskError(f'Can not read partitions off something that isn\'t a block device: {self.path}')
@ -188,6 +188,9 @@ class Filesystem():
else: else:
raise DiskError(f'Unknown mode selected to format in: {self.mode}') raise DiskError(f'Unknown mode selected to format in: {self.mode}')
def __repr__(self):
return f"Filesystem(blockdevice={self.blockdevice}, mode={self.mode})"
def __exit__(self, *args, **kwargs): def __exit__(self, *args, **kwargs):
# TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager
if len(args) >= 2 and args[1]: if len(args) >= 2 and args[1]:
@ -221,17 +224,20 @@ class Filesystem():
def add_partition(self, type, start, end, format=None): def add_partition(self, type, start, end, format=None):
log(f'Adding partition to {self.blockdevice}', level=LOG_LEVELS.Info, file=storage.get('logfile', None)) log(f'Adding partition to {self.blockdevice}', level=LOG_LEVELS.Info, file=storage.get('logfile', None))
print('Before:', self.blockdevice.partitions)
previous_partitions = self.blockdevice.partitions
if format: if format:
partitioning = self.parted(f'{self.blockdevice.device} mkpart {type} {format} {start} {end}') == 0 partitioning = self.parted(f'{self.blockdevice.device} mkpart {type} {format} {start} {end}') == 0
else: else:
partitioning = self.parted(f'{self.blockdevice.device} mkpart {type} {start} {end}') == 0 partitioning = self.parted(f'{self.blockdevice.device} mkpart {type} {start} {end}') == 0
import time
time.sleep(5)
print('After:', print(self.blockdevice.partitions))
if partitioning: if partitioning:
start_wait = time.time()
while previous_partitions == self.blockdevice.partitions:
time.sleep(0.025) # Let the new partition come up in the kernel
if time.time() - start_wait > 10:
raise DiskError(f"New partition never showed up after adding new partition on {self} (timeout 10 seconds).")
return True return True
def set_name(self, partition:int, name:str): def set_name(self, partition:int, name:str):