mirror of
https://github.com/flatcar/scripts.git
synced 2025-11-24 03:51:33 +01:00
fix(disk_util): Move existing image reading code to new function.
This will allow the code to be shared, making it possible to safely resize existing images with disk_util.
This commit is contained in:
parent
8e0d630699
commit
5afd720956
@ -178,6 +178,35 @@ def GetPartitionTableFromConfig(options):
|
||||
return partitions
|
||||
|
||||
|
||||
def GetPartitionTableFromImage(options):
|
||||
"""Loads very basic partition table info from an existing image.
|
||||
|
||||
Currently only includes blocks and first_block values.
|
||||
|
||||
Args:
|
||||
options: Flags passed to the script
|
||||
Returns:
|
||||
A list defining all existing partitions.
|
||||
"""
|
||||
partitions = {}
|
||||
cgpt_show = subprocess.check_output(
|
||||
['cgpt', 'show', '-q', options.disk_image])
|
||||
for line in cgpt_show.split('\n'):
|
||||
if not line.strip():
|
||||
continue
|
||||
fields = line.split(None, 3)
|
||||
if len(fields) != 4 or not all(f.isdigit() for f in fields[:3]):
|
||||
raise Exception('Invalid output from cgpt show -q: %r' % line)
|
||||
|
||||
part_num = fields[2]
|
||||
partitions[part_num] = {
|
||||
'num': int(part_num),
|
||||
'first_block': int(fields[0]),
|
||||
'blocks': int(fields[1])}
|
||||
|
||||
return partitions
|
||||
|
||||
|
||||
def WritePartitionTable(options, config=None, partitions=None):
|
||||
"""Writes the given partition table to a disk image or device.
|
||||
|
||||
@ -347,20 +376,12 @@ def Mount(options):
|
||||
"""
|
||||
|
||||
config, partitions = LoadPartitionConfig(options)
|
||||
image_parts = GetPartitionTableFromImage(options)
|
||||
mounts = {}
|
||||
|
||||
cgpt_show = subprocess.check_output(
|
||||
['cgpt', 'show', '-q', options.disk_image])
|
||||
for line in cgpt_show.split('\n'):
|
||||
if not line.strip():
|
||||
continue
|
||||
fields = line.split(None, 3)
|
||||
if len(fields) != 4 or not all(f.isdigit() for f in fields[:3]):
|
||||
raise Exception('Invalid output from cgpt show -q: %r' % line)
|
||||
|
||||
first_block = int(fields[0])
|
||||
blocks = int(fields[1])
|
||||
part_num = fields[2]
|
||||
for part_num, image_part in image_parts.iteritems():
|
||||
first_block = image_part['first_block']
|
||||
blocks = image_part['blocks']
|
||||
part = partitions.get(part_num, {})
|
||||
path = part.get('mount', None)
|
||||
if not path or not path.startswith('/'):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user