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 | |
---|
9 | import fdisk |
---|
10 | |
---|
11 | GPT_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 | |
---|
18 | DOS_PARTTYPES = { |
---|
19 | 'EXTENDED': 0x0f, |
---|
20 | 'EMPTY': 0x00, |
---|
21 | 'LINUX': 0x83, |
---|
22 | 'CACHE': 0x83, |
---|
23 | 'NTFS': 0x07, |
---|
24 | 'HFS': 0xaf, |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
28 | def 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 | |
---|
35 | def 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 | |
---|
42 | def 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') |
---|