summaryrefslogtreecommitdiffstats
path: root/src/live/parttypes.py
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2023-02-07 09:21:58 +0100
committerJose M. Guisado <jguisado@soleta.eu>2023-02-08 17:11:44 +0100
commit29c53e54e9459cd652c908e2e13b616778e6e4f1 (patch)
treed0f77ad282abc1a1fd540235bd503e1cfe17d4c8 /src/live/parttypes.py
parentc010c42008cbc42346f5b7e664cc5811e953e5a5 (diff)
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.
Diffstat (limited to 'src/live/parttypes.py')
-rw-r--r--src/live/parttypes.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/live/parttypes.py b/src/live/parttypes.py
new file mode 100644
index 0000000..c840f39
--- /dev/null
+++ b/src/live/parttypes.py
@@ -0,0 +1,55 @@
+#
+# Copyright (C) 2023 Soleta Networks <info@soleta.eu>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the
+# Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+import fdisk
+
+GPT_PARTTYPES = {
+ 'LINUX': '0FC63DAF-8483-4772-8E79-3D69D8477DE4',
+ 'NTFS': 'EBD0A0A2-B9E5-4433-87C0-68B6B72699C7',
+ 'EFI': 'C12A7328-F81F-11D2-BA4B-00A0C93EC93B',
+ 'HFS': '48465300-0000-11AA-AA11-00306543ECAC',
+}
+
+DOS_PARTTYPES = {
+ 'EXTENDED': 0x0f,
+ 'EMPTY': 0x00,
+ 'LINUX': 0x83,
+ 'CACHE': 0x83,
+ 'NTFS': 0x07,
+ 'HFS': 0xaf,
+}
+
+
+def get_dos_parttype(cxt, ptype_str):
+ l = cxt.label
+ code = DOS_PARTTYPES.get(ptype_str, 0x0)
+ parttype = l.get_parttype_from_code(code)
+ return parttype
+
+
+def get_gpt_parttype(cxt, ptype_str):
+ l = cxt.label
+ uuid = GPT_PARTTYPES.get(ptype_str, GPT_PARTTYPES['LINUX'])
+ parttype = l.get_parttype_from_string(uuid)
+ return parttype
+
+
+def get_parttype(cxt, ptype_str):
+ if not cxt:
+ raise RuntimeError('No libfdisk context')
+ if not cxt.label or cxt.label.name not in ['dos', 'gpt']:
+ raise RuntimeError('Unknown libfdisk label')
+ if type(ptype_str) != str:
+ raise RuntimeError('Invalid partition type')
+
+ if cxt.label.name == 'dos':
+ return get_dos_parttype(cxt, ptype_str)
+ elif cxt.label.name == 'gpt':
+ return get_gpt_parttype(cxt, ptype_str)
+ else:
+ raise RuntimeError('BUG: Invalid partition label')