mirror of
https://github.com/flatcar/scripts.git
synced 2025-09-22 22:21:10 +02:00
cgpt: validate layout files when loading
Add more sanity checks to the input .json file to catch when people make typos or other random mistakes. BUG=None TEST=loaded all .json files we have Change-Id: Ibc2439684628225da43639c2fac25958b5fa794e Reviewed-on: https://gerrit.chromium.org/gerrit/34708 Reviewed-by: Liam McLoughlin <lmcloughlin@chromium.org> Tested-by: Liam McLoughlin <lmcloughlin@chromium.org> Commit-Ready: Mike Frysinger <vapier@chromium.org> Tested-by: Mike Frysinger <vapier@chromium.org>
This commit is contained in:
parent
c17cf6aff0
commit
1ef08a9fcf
@ -28,25 +28,51 @@ def LoadPartitionConfig(filename):
|
|||||||
Object containing disk layout configuration
|
Object containing disk layout configuration
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
valid_keys = set(('_comment', 'metadata', 'layouts'))
|
||||||
|
valid_layout_keys = set((
|
||||||
|
'_comment', 'type', 'num', 'label', 'blocks', 'block_size', 'fs_blocks',
|
||||||
|
'fs_block_size', 'features'))
|
||||||
|
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
raise ConfigNotFound('Partition config %s was not found!' % filename)
|
raise ConfigNotFound('Partition config %s was not found!' % filename)
|
||||||
with open(filename) as f:
|
with open(filename) as f:
|
||||||
config = json.load(f)
|
config = json.load(f)
|
||||||
|
|
||||||
metadata = config['metadata']
|
try:
|
||||||
metadata['block_size'] = int(metadata['block_size'])
|
metadata = config['metadata']
|
||||||
|
for key in ('block_size', 'fs_block_size'):
|
||||||
|
metadata[key] = int(metadata[key])
|
||||||
|
|
||||||
for layout_name, layout in config['layouts'].items():
|
unknown_keys = set(config.keys()) - valid_keys
|
||||||
for part in layout:
|
if unknown_keys:
|
||||||
part['blocks'] = int(part['blocks'])
|
raise InvalidLayout('Unknown items: %r' % unknown_keys)
|
||||||
part['bytes'] = part['blocks'] * metadata['block_size']
|
|
||||||
|
|
||||||
if 'fs_blocks' in part:
|
if len(config['layouts']) <= 0:
|
||||||
part['fs_blocks'] = int(part['fs_blocks'])
|
raise InvalidLayout('Missing "layouts" entries')
|
||||||
part['fs_bytes'] = part['fs_blocks'] * metadata['fs_block_size']
|
|
||||||
|
|
||||||
if part['fs_bytes'] > part['bytes']:
|
for layout_name, layout in config['layouts'].items():
|
||||||
raise InvalidLayout('Filesystem may not be larger than partition')
|
for part in layout:
|
||||||
|
unknown_keys = set(part.keys()) - valid_layout_keys
|
||||||
|
if unknown_keys:
|
||||||
|
raise InvalidLayout('Unknown items in layout %s: %r' %
|
||||||
|
(layout_name, unknown_keys))
|
||||||
|
|
||||||
|
if part['type'] != 'blank':
|
||||||
|
for s in ('num', 'label'):
|
||||||
|
if not s in part:
|
||||||
|
raise InvalidLayout('Layout "%s" missing "%s"' % (layout_name, s))
|
||||||
|
|
||||||
|
part['blocks'] = int(part['blocks'])
|
||||||
|
part['bytes'] = part['blocks'] * metadata['block_size']
|
||||||
|
|
||||||
|
if 'fs_blocks' in part:
|
||||||
|
part['fs_blocks'] = int(part['fs_blocks'])
|
||||||
|
part['fs_bytes'] = part['fs_blocks'] * metadata['fs_block_size']
|
||||||
|
|
||||||
|
if part['fs_bytes'] > part['bytes']:
|
||||||
|
raise InvalidLayout('Filesystem may not be larger than partition')
|
||||||
|
except KeyError as e:
|
||||||
|
raise InvalidLayout('Layout is missing required entries: %s' % e)
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
|
"_comment": "See http://www.chromium.org/chromium-os/building-chromium-os/disk-layout-format",
|
||||||
"metadata":{
|
"metadata":{
|
||||||
"block_size": 512,
|
"block_size": 512,
|
||||||
"fs_block_size": 4096
|
"fs_block_size": 4096
|
||||||
|
Loading…
x
Reference in New Issue
Block a user