source: ogClient-Git/src/live/parttypes.py @ eac9426

Last change on this file since eac9426 was 29c53e5, checked in by Jose M. Guisado <jguisado@…>, 2 years ago

live: add parttypes.py

Adds parttypes.py module with utility functions to get partition types
(parttypes) from python-libfdisk.

Supports standard partition types, either DOS or GPT.
DOS labels use a hex code to define partition types, python-libfdisk
exposes get_parttype_from_code to look up for DOS partition types from a
given hexcode.
GPT label uses a string (UUID) for each supported partition type,
python-libfdisk exposes get_parttype_from_string to look up for GPT
partition types from a given string.

  • Property mode set to 100644
File size: 1.6 KB
Line 
1#
2# Copyright (C) 2023 Soleta Networks <info@soleta.eu>
3#
4# This program is free software: you can redistribute it and/or modify it under
5# the terms of the GNU Affero General Public License as published by the
6# Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8
9import fdisk
10
11GPT_PARTTYPES = {
12    'LINUX':    '0FC63DAF-8483-4772-8E79-3D69D8477DE4',
13    'NTFS':     'EBD0A0A2-B9E5-4433-87C0-68B6B72699C7',
14    'EFI':      'C12A7328-F81F-11D2-BA4B-00A0C93EC93B',
15    'HFS':      '48465300-0000-11AA-AA11-00306543ECAC',
16}
17
18DOS_PARTTYPES = {
19    'EXTENDED': 0x0f,
20    'EMPTY':    0x00,
21    'LINUX':    0x83,
22    'CACHE':    0x83,
23    'NTFS':     0x07,
24    'HFS':      0xaf,
25}
26
27
28def get_dos_parttype(cxt, ptype_str):
29    l = cxt.label
30    code = DOS_PARTTYPES.get(ptype_str, 0x0)
31    parttype = l.get_parttype_from_code(code)
32    return parttype
33
34
35def get_gpt_parttype(cxt, ptype_str):
36    l = cxt.label
37    uuid = GPT_PARTTYPES.get(ptype_str, GPT_PARTTYPES['LINUX'])
38    parttype = l.get_parttype_from_string(uuid)
39    return parttype
40
41
42def get_parttype(cxt, ptype_str):
43    if not cxt:
44        raise RuntimeError('No libfdisk context')
45    if not cxt.label or cxt.label.name not in ['dos', 'gpt']:
46        raise RuntimeError('Unknown libfdisk label')
47    if type(ptype_str) != str:
48        raise RuntimeError('Invalid partition type')
49
50    if cxt.label.name == 'dos':
51        return get_dos_parttype(cxt, ptype_str)
52    elif cxt.label.name == 'gpt':
53        return get_gpt_parttype(cxt, ptype_str)
54    else:
55        raise RuntimeError('BUG: Invalid partition label')
Note: See TracBrowser for help on using the repository browser.