build_library/disk_util: work around losetup bug

Retry losetup if it fails, up to 5 times with 5 seconds between retries.
This commit is contained in:
Andrew Jeddeloh 2019-12-13 00:15:49 +00:00 committed by Kai Lüke
parent 5b8c706c70
commit 00d77d199a
No known key found for this signature in database
GPG Key ID: E5601DA3A1D902A8

View File

@ -11,6 +11,7 @@ import re
import subprocess
import sys
import tempfile
import time
import uuid
# First sector we can use.
@ -433,11 +434,22 @@ def FormatFat(part, device):
def PartitionLoop(options, partition):
"""Allocate (and automatically free) loop devices for a partition."""
loop_dev = subprocess.check_output(['sudo', 'losetup',
'--offset', str(partition['first_byte']),
'--sizelimit', str(partition['bytes']),
'--find', '--show', options.disk_image])
loop_dev = loop_dev.strip()
for i in range(0,5):
try:
loop_dev = subprocess.check_output(['sudo', 'losetup',
'--offset', str(partition['first_byte']),
'--sizelimit', str(partition['bytes']),
'--find', '--show', options.disk_image])
loop_dev = loop_dev.strip()
err = None
break
except subprocess.CalledProcessError as error:
print("Failed to set up loopback, attempt %d" % i)
err = error
time.sleep(5)
if err is not None:
raise err
try:
yield loop_dev