summaryrefslogtreecommitdiffstats
path: root/src/live/parttypes.py
blob: aaeb74be1f1f4a553d0678c7442a2dc3cd3a5cf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
# 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
from src.log import OgError

GPT_PARTTYPES = {
    'LINUX-SWAP':   '0657FD6D-A4AB-43C4-84E5-0933C84B4F4F',
    'LINUX':        '0FC63DAF-8483-4772-8E79-3D69D8477DE4',
    'NTFS':         'EBD0A0A2-B9E5-4433-87C0-68B6B72699C7',
    'EFI':          'C12A7328-F81F-11D2-BA4B-00A0C93EC93B',
    'HFS':          '48465300-0000-11AA-AA11-00306543ECAC',
    'FAT32':        'EBD0A0A2-B9E5-4433-87C0-68B6B72699C7',
    'WIN-RECOV':    'DE94BBA4-06D1-4D40-A16A-BFD50179D6AC',
}

DOS_PARTTYPES = {
    'LINUX-SWAP':   0x82,
    'EXTENDED':     0x0f,
    'EMPTY':        0x00,
    'LINUX':        0x83,
    'CACHE':        0x83,
    'NTFS':         0x07,
    'HFS':          0xaf,
    'FAT32':        0x0b,
    'EFI':          0xef,
}


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 OgError('No libfdisk context')
    if not cxt.label or cxt.label.name not in ['dos', 'gpt']:
        raise OgError('Unknown libfdisk label')
    if type(ptype_str) != str:
        raise OgError('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 OgError(f'Invalid partition label \'{cxt.label.name}\'')