summaryrefslogtreecommitdiff
path: root/cloudinit/config/cc_disk_setup.py
blob: 4aae55304d101974e1be8e2b5f251b92881f1afb (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
# Copyright (C) 2009-2010 Canonical Ltd.
# Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
#
# Author: Ben Howard <ben.howard@canonical.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

"""Disk Setup: Configure partitions and filesystems."""

import logging
import os
import shlex
from logging import Logger
from textwrap import dedent

from cloudinit import subp, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema, get_meta_doc
from cloudinit.distros import ALL_DISTROS
from cloudinit.settings import PER_INSTANCE

# Define the commands to use
SFDISK_CMD = subp.which("sfdisk")
SGDISK_CMD = subp.which("sgdisk")
LSBLK_CMD = subp.which("lsblk")
BLKID_CMD = subp.which("blkid")
BLKDEV_CMD = subp.which("blockdev")
PARTPROBE_CMD = subp.which("partprobe")
WIPEFS_CMD = subp.which("wipefs")

LANG_C_ENV = {"LANG": "C"}
LOG = logging.getLogger(__name__)

MODULE_DESCRIPTION = """\
This module is able to configure simple partition tables and filesystems.

.. note::
    for more detail about configuration options for disk setup, see the disk
    setup example

.. note::
    if a swap partition is being created via ``disk_setup`` then a ``fs_entry``
    entry is also needed in order for mkswap to be run, otherwise when swap
    activation is later attempted it will fail.

For convenience, aliases can be specified for disks using the
``device_aliases`` config key, which takes a dictionary of alias: path
mappings. There are automatic aliases for ``swap`` and ``ephemeral<X>``, where
``swap`` will always refer to the active swap partition and ``ephemeral<X>``
will refer to the block device of the ephemeral image.

Disk partitioning is done using the ``disk_setup`` directive. This config
directive accepts a dictionary where each key is either a path to a block
device or an alias specified in ``device_aliases``, and each value is the
configuration options for the device. File system configuration is done using
the ``fs_setup`` directive. This config directive accepts a list of
filesystem configs.
"""

meta: MetaSchema = {
    "id": "cc_disk_setup",
    "name": "Disk Setup",
    "title": "Configure partitions and filesystems",
    "description": MODULE_DESCRIPTION,
    "distros": [ALL_DISTROS],
    "frequency": PER_INSTANCE,
    "examples": [
        dedent(
            """\
            device_aliases:
              my_alias: /dev/sdb
              swap_disk: /dev/sdc
            disk_setup:
              my_alias:
                table_type: gpt
                layout: [50, 50]
                overwrite: true
              swap_disk:
                table_type: gpt
                layout: [[100, 82]]
                overwrite: true
              /dev/sdd:
                table_type: mbr
                layout: true
                overwrite: true
            fs_setup:
            - label: fs1
              filesystem: ext4
              device: my_alias.1
              cmd: mkfs -t %(filesystem)s -L %(label)s %(device)s
            - label: fs2
              device: my_alias.2
              filesystem: ext4
            - label: swap
              device: swap_disk.1
              filesystem: swap
            - label: fs3
              device: /dev/sdd1
              filesystem: ext4
            mounts:
            - ["my_alias.1", "/mnt1"]
            - ["my_alias.2", "/mnt2"]
            - ["swap_disk.1", "none", "swap", "sw", "0", "0"]
            - ["/dev/sdd1", "/mnt3"]
            """
        )
    ],
    "activate_by_schema_keys": ["disk_setup", "fs_setup"],
}

__doc__ = get_meta_doc(meta)


def handle(
    name: str, cfg: Config, cloud: Cloud, log: Logger, args: list
) -> None:
    """
    See doc/examples/cloud-config-disk-setup.txt for documentation on the
    format.
    """
    device_aliases = cfg.get("device_aliases", {})

    def alias_to_device(cand):
        name = device_aliases.get(cand)
        return cloud.device_name_to_device(name or cand) or name

    disk_setup = cfg.get("disk_setup")
    if isinstance(disk_setup, dict):
        update_disk_setup_devices(disk_setup, alias_to_device)
        log.debug("Partitioning disks: %s", str(disk_setup))
        for disk, definition in disk_setup.items():
            if not isinstance(definition, dict):
                log.warning("Invalid disk definition for %s" % disk)
                continue

            try:
                log.debug("Creating new partition table/disk")
                util.log_time(
                    logfunc=LOG.debug,
                    msg="Creating partition on %s" % disk,
                    func=mkpart,
                    args=(disk, definition),
                )
            except Exception as e:
                util.logexc(LOG, "Failed partitioning operation\n%s" % e)

    fs_setup = cfg.get("fs_setup")
    if isinstance(fs_setup, list):
        log.debug("setting up filesystems: %s", str(fs_setup))
        update_fs_setup_devices(fs_setup, alias_to_device)
        for definition in fs_setup:
            if not isinstance(definition, dict):
                log.warning("Invalid file system definition: %s" % definition)
                continue

            try:
                log.debug("Creating new filesystem.")
                device = definition.get("device")
                util.log_time(
                    logfunc=LOG.debug,
                    msg="Creating fs for %s" % device,
                    func=mkfs,
                    args=(definition,),
                )
            except Exception as e:
                util.logexc(LOG, "Failed during filesystem operation\n%s" % e)


def update_disk_setup_devices(disk_setup, tformer):
    # update 'disk_setup' dictionary anywhere were a device may occur
    # update it with the response from 'tformer'
    for origname in list(disk_setup):
        transformed = tformer(origname)
        if transformed is None or transformed == origname:
            continue
        if transformed in disk_setup:
            LOG.info(
                "Replacing %s in disk_setup for translation of %s",
                origname,
                transformed,
            )
            del disk_setup[transformed]

        disk_setup[transformed] = disk_setup[origname]
        if isinstance(disk_setup[transformed], dict):
            disk_setup[transformed]["_origname"] = origname
        del disk_setup[origname]
        LOG.debug(
            "updated disk_setup device entry '%s' to '%s'",
            origname,
            transformed,
        )


def update_fs_setup_devices(disk_setup, tformer):
    # update 'fs_setup' dictionary anywhere were a device may occur
    # update it with the response from 'tformer'
    for definition in disk_setup:
        if not isinstance(definition, dict):
            LOG.warning("entry in disk_setup not a dict: %s", definition)
            continue

        origname = definition.get("device")

        if origname is None:
            continue

        (dev, part) = util.expand_dotted_devname(origname)

        tformed = tformer(dev)
        if tformed is not None:
            dev = tformed
            LOG.debug(
                "%s is mapped to disk=%s part=%s", origname, tformed, part
            )
            definition["_origname"] = origname
            definition["device"] = tformed

        if part:
            # In origname with <dev>.N, N overrides 'partition' key.
            if "partition" in definition:
                LOG.warning(
                    "Partition '%s' from dotted device name '%s' "
                    "overrides 'partition' key in %s",
                    part,
                    origname,
                    definition,
                )
                definition["_partition"] = definition["partition"]
            definition["partition"] = part


def value_splitter(values, start=None):
    """
    Returns the key/value pairs of output sent as string
    like:  FOO='BAR' HOME='127.0.0.1'
    """
    _values = shlex.split(values)
    if start:
        _values = _values[start:]

    for key, value in [x.split("=") for x in _values]:
        yield key, value


def enumerate_disk(device, nodeps=False):
    """
    Enumerate the elements of a child device.

    Parameters:
        device: the kernel device name
        nodeps <BOOL>: don't enumerate children devices

    Return a dict describing the disk:
        type: the entry type, i.e disk or part
        fstype: the filesystem type, if it exists
        label: file system label, if it exists
        name: the device name, i.e. sda
    """

    lsblk_cmd = [
        LSBLK_CMD,
        "--pairs",
        "--output",
        "NAME,TYPE,FSTYPE,LABEL",
        device,
    ]

    if nodeps:
        lsblk_cmd.append("--nodeps")

    info = None
    try:
        info, _err = subp.subp(lsblk_cmd)
    except Exception as e:
        raise Exception(
            "Failed during disk check for %s\n%s" % (device, e)
        ) from e

    parts = [x for x in (info.strip()).splitlines() if len(x.split()) > 0]

    for part in parts:
        d = {
            "name": None,
            "type": None,
            "fstype": None,
            "label": None,
        }

        for key, value in value_splitter(part):
            d[key.lower()] = value

        yield d


def device_type(device):
    """
    Return the device type of the device by calling lsblk.
    """

    for d in enumerate_disk(device, nodeps=True):
        if "type" in d:
            return d["type"].lower()
    return None


def is_device_valid(name, partition=False):
    """
    Check if the device is a valid device.
    """
    d_type = ""
    try:
        d_type = device_type(name)
    except Exception:
        LOG.warning("Query against device %s failed", name)
        return False

    if partition and d_type == "part":
        return True
    elif not partition and d_type == "disk":
        return True
    return False


def check_fs(device):
    """
    Check if the device has a filesystem on it

    Output of blkid is generally something like:
    /dev/sda: LABEL="Backup500G" UUID="..." TYPE="ext4"

    Return values are device, label, type, uuid
    """
    out, label, fs_type, uuid = None, None, None, None

    blkid_cmd = [BLKID_CMD, "-c", "/dev/null", device]
    try:
        out, _err = subp.subp(blkid_cmd, rcs=[0, 2])
    except Exception as e:
        raise Exception(
            "Failed during disk check for %s\n%s" % (device, e)
        ) from e

    if out:
        if len(out.splitlines()) == 1:
            for key, value in value_splitter(out, start=1):
                if key.lower() == "label":
                    label = value
                elif key.lower() == "type":
                    fs_type = value
                elif key.lower() == "uuid":
                    uuid = value

    return label, fs_type, uuid


def is_filesystem(device):
    """
    Returns true if the device has a file system.
    """
    _, fs_type, _ = check_fs(device)
    return fs_type


def find_device_node(
    device,
    fs_type=None,
    label=None,
    valid_targets=None,
    label_match=True,
    replace_fs=None,
):
    """
    Find a device that is either matches the spec, or the first

    The return is value is (<device>, <bool>) where the device is the
    device to use and the bool is whether the device matches the
    fs_type and label.

    Note: This works with GPT partition tables!
    """
    # label of None is same as no label
    if label is None:
        label = ""

    if not valid_targets:
        valid_targets = ["disk", "part"]

    raw_device_used = False
    for d in enumerate_disk(device):

        if d["fstype"] == replace_fs and label_match is False:
            # We found a device where we want to replace the FS
            return ("/dev/%s" % d["name"], False)

        if d["fstype"] == fs_type and (
            (label_match and d["label"] == label) or not label_match
        ):
            # If we find a matching device, we return that
            return ("/dev/%s" % d["name"], True)

        if d["type"] in valid_targets:

            if d["type"] != "disk" or d["fstype"]:
                raw_device_used = True

            if d["type"] == "disk":
                # Skip the raw disk, its the default
                pass

            elif not d["fstype"]:
                return ("/dev/%s" % d["name"], False)

    if not raw_device_used:
        return (device, False)

    LOG.warning("Failed to find device during available device search.")
    return (None, False)


def is_disk_used(device):
    """
    Check if the device is currently used. Returns true if the device
    has either a file system or a partition entry
    is no filesystem found on the disk.
    """

    # If the child count is higher 1, then there are child nodes
    # such as partition or device mapper nodes
    if len(list(enumerate_disk(device))) > 1:
        return True

    # If we see a file system, then its used
    _, check_fstype, _ = check_fs(device)
    if check_fstype:
        return True

    return False


def get_hdd_size(device):
    try:
        size_in_bytes, _ = subp.subp([BLKDEV_CMD, "--getsize64", device])
        sector_size, _ = subp.subp([BLKDEV_CMD, "--getss", device])
    except Exception as e:
        raise Exception("Failed to get %s size\n%s" % (device, e)) from e

    return int(size_in_bytes) / int(sector_size)


def check_partition_mbr_layout(device, layout):
    """
    Returns true if the partition layout matches the one on the disk

    Layout should be a list of values. At this time, this only
    verifies that the number of partitions and their labels is correct.
    """

    read_parttbl(device)
    prt_cmd = [SFDISK_CMD, "-l", device]
    try:
        out, _err = subp.subp(prt_cmd, data="%s\n" % layout)
    except Exception as e:
        raise Exception(
            "Error running partition command on %s\n%s" % (device, e)
        ) from e

    found_layout = []
    for line in out.splitlines():
        _line = line.split()
        if len(_line) == 0:
            continue

        if device in _line[0]:
            # We don't understand extended partitions yet
            if _line[-1].lower() in ["extended", "empty"]:
                continue

            # Find the partition types
            type_label = None
            for x in sorted(range(1, len(_line)), reverse=True):
                if _line[x].isdigit() and _line[x] != "/":
                    type_label = _line[x]
                    break

            found_layout.append(type_label)
    return found_layout


def check_partition_gpt_layout(device, layout):
    prt_cmd = [SGDISK_CMD, "-p", device]
    try:
        out, _err = subp.subp(prt_cmd, update_env=LANG_C_ENV)
    except Exception as e:
        raise Exception(
            "Error running partition command on %s\n%s" % (device, e)
        ) from e

    out_lines = iter(out.splitlines())
    # Skip header.  Output looks like:
    # ***************************************************************
    # Found invalid GPT and valid MBR; converting MBR to GPT format
    # in memory.
    # ***************************************************************
    #
    # Disk /dev/vdb: 83886080 sectors, 40.0 GiB
    # Logical sector size: 512 bytes
    # Disk identifier (GUID): 8A7F11AD-3953-491B-8051-077E01C8E9A7
    # Partition table holds up to 128 entries
    # First usable sector is 34, last usable sector is 83886046
    # Partitions will be aligned on 2048-sector boundaries
    # Total free space is 83476413 sectors (39.8 GiB)
    #
    # Number Start (sector) End (sector) Size       Code  Name
    # 1      2048           206847       100.0 MiB  0700  Microsoft basic data
    for line in out_lines:
        if line.strip().startswith("Number"):
            break

    codes = [line.strip().split()[5] for line in out_lines]
    cleaned = []

    # user would expect a code '83' to be Linux, but sgdisk outputs 8300.
    for code in codes:
        if len(code) == 4 and code.endswith("00"):
            code = code[0:2]
        cleaned.append(code)
    return cleaned


def check_partition_layout(table_type, device, layout):
    """
    See if the partition lay out matches.

    This is future a future proofing function. In order
    to add support for other disk layout schemes, add a
    function called check_partition_%s_layout
    """
    if "gpt" == table_type:
        found_layout = check_partition_gpt_layout(device, layout)
    elif "mbr" == table_type:
        found_layout = check_partition_mbr_layout(device, layout)
    else:
        raise Exception("Unable to determine table type")

    LOG.debug(
        "called check_partition_%s_layout(%s, %s), returned: %s",
        table_type,
        device,
        layout,
        found_layout,
    )
    if isinstance(layout, bool):
        # if we are using auto partitioning, or "True" be happy
        # if a single partition exists.
        if layout and len(found_layout) >= 1:
            return True
        return False

    elif len(found_layout) == len(layout):
        # This just makes sure that the number of requested
        # partitions and the type labels are right
        layout_types = [
            str(x[1]) if isinstance(x, (tuple, list)) else None for x in layout
        ]
        LOG.debug(
            "Layout types=%s. Found types=%s", layout_types, found_layout
        )
        for itype, ftype in zip(layout_types, found_layout):
            if itype is not None and str(ftype) != str(itype):
                return False
        return True

    return False


def get_partition_mbr_layout(size, layout):
    """
    Calculate the layout of the partition table. Partition sizes
    are defined as percentage values or a tuple of percentage and
    partition type.

    For example:
        [ 33, [66: 82] ]

    Defines the first partition to be a size of 1/3 the disk,
    while the remaining 2/3's will be of type Linux Swap.
    """

    if not isinstance(layout, list) and isinstance(layout, bool):
        # Create a single partition, default to Linux
        return ",,83"

    if (len(layout) == 0 and isinstance(layout, list)) or not isinstance(
        layout, list
    ):
        raise Exception("Partition layout is invalid")

    last_part_num = len(layout)
    if last_part_num > 4:
        raise Exception("Only simply partitioning is allowed.")

    part_definition = []
    part_num = 0
    for part in layout:
        part_type = 83  # Default to Linux
        percent = part
        part_num += 1

        if isinstance(part, list):
            if len(part) != 2:
                raise Exception("Partition was incorrectly defined: %s" % part)
            percent, part_type = part

        part_size = int(float(size) * (float(percent) / 100))

        if part_num == last_part_num:
            part_definition.append(",,%s" % part_type)
        else:
            part_definition.append(",%s,%s" % (part_size, part_type))

    sfdisk_definition = "\n".join(part_definition)
    if len(part_definition) > 4:
        raise Exception(
            "Calculated partition definition is too big\n%s"
            % sfdisk_definition
        )

    return sfdisk_definition


def get_partition_gpt_layout(size, layout):
    if isinstance(layout, bool):
        return [(None, [0, 0])]

    partition_specs = []
    for partition in layout:
        if isinstance(partition, list):
            if len(partition) != 2:
                raise Exception(
                    "Partition was incorrectly defined: %s" % partition
                )
            percent, partition_type = partition
        else:
            percent = partition
            partition_type = None

        part_size = int(float(size) * (float(percent) / 100))
        partition_specs.append((partition_type, [0, "+{}".format(part_size)]))

    # The last partition should use up all remaining space
    partition_specs[-1][-1][-1] = 0
    return partition_specs


def purge_disk_ptable(device):
    # wipe the first and last megabyte of a disk (or file)
    # gpt stores partition table both at front and at end.
    null = b"\0"
    start_len = 1024 * 1024
    end_len = 1024 * 1024
    with open(device, "rb+") as fp:
        fp.write(null * (start_len))
        fp.seek(-end_len, os.SEEK_END)
        fp.write(null * end_len)
        fp.flush()

    read_parttbl(device)


def purge_disk(device):
    """
    Remove parition table entries
    """

    # wipe any file systems first
    for d in enumerate_disk(device):
        if d["type"] not in ["disk", "crypt"]:
            wipefs_cmd = [WIPEFS_CMD, "--all", "/dev/%s" % d["name"]]
            try:
                LOG.info("Purging filesystem on /dev/%s", d["name"])
                subp.subp(wipefs_cmd)
            except Exception as e:
                raise Exception(
                    "Failed FS purge of /dev/%s" % d["name"]
                ) from e

    purge_disk_ptable(device)


def get_partition_layout(table_type, size, layout):
    """
    Call the appropriate function for creating the table
    definition. Returns the table definition

    This is a future proofing function. To add support for
    other layouts, simply add a "get_partition_%s_layout"
    function.
    """
    if "mbr" == table_type:
        return get_partition_mbr_layout(size, layout)
    elif "gpt" == table_type:
        return get_partition_gpt_layout(size, layout)
    raise Exception("Unable to determine table type")


def read_parttbl(device):
    """
    `Partprobe` is preferred over `blkdev` since it is more reliably
    able to probe the partition table.
    """
    if PARTPROBE_CMD is not None:
        probe_cmd = [PARTPROBE_CMD, device]
    else:
        probe_cmd = [BLKDEV_CMD, "--rereadpt", device]
    util.udevadm_settle()
    try:
        subp.subp(probe_cmd)
    except Exception as e:
        util.logexc(LOG, "Failed reading the partition table %s" % e)

    util.udevadm_settle()


def exec_mkpart_mbr(device, layout):
    """
    Break out of mbr partition to allow for future partition
    types, i.e. gpt
    """
    # Create the partitions
    prt_cmd = [SFDISK_CMD, "--force", device]
    try:
        subp.subp(prt_cmd, data="%s\n" % layout)
    except Exception as e:
        raise Exception(
            "Failed to partition device %s\n%s" % (device, e)
        ) from e

    read_parttbl(device)


def exec_mkpart_gpt(device, layout):
    try:
        subp.subp([SGDISK_CMD, "-Z", device])
        for index, (partition_type, (start, end)) in enumerate(layout):
            index += 1
            subp.subp(
                [
                    SGDISK_CMD,
                    "-n",
                    "{}:{}:{}".format(index, start, end),
                    device,
                ]
            )
            if partition_type is not None:
                # convert to a 4 char (or more) string right padded with 0
                # 82 -> 8200.  'Linux' -> 'Linux'
                pinput = str(partition_type).ljust(4, "0")
                subp.subp(
                    [SGDISK_CMD, "-t", "{}:{}".format(index, pinput), device]
                )
    except Exception:
        LOG.warning("Failed to partition device %s", device)
        raise

    read_parttbl(device)


def assert_and_settle_device(device):
    """Assert that device exists and settle so it is fully recognized."""
    if not os.path.exists(device):
        util.udevadm_settle()
        if not os.path.exists(device):
            raise RuntimeError(
                "Device %s did not exist and was not created "
                "with a udevadm settle." % device
            )

    # Whether or not the device existed above, it is possible that udev
    # events that would populate udev database (for reading by lsdname) have
    # not yet finished. So settle again.
    util.udevadm_settle()


def mkpart(device, definition):
    """
    Creates the partition table.

    Parameters:
        definition: dictionary describing how to create the partition.

            The following are supported values in the dict:
                overwrite: Should the partition table be created regardless
                            of any pre-exisiting data?
                layout: the layout of the partition table
                table_type: Which partition table to use, defaults to MBR
                device: the device to work on.
    """
    # ensure that we get a real device rather than a symbolic link
    assert_and_settle_device(device)
    device = os.path.realpath(device)

    LOG.debug("Checking values for %s definition", device)
    overwrite = definition.get("overwrite", False)
    layout = definition.get("layout", False)
    table_type = definition.get("table_type", "mbr")

    # Check if the default device is a partition or not
    LOG.debug("Checking against default devices")

    if (isinstance(layout, bool) and not layout) or not layout:
        LOG.debug("Device is not to be partitioned, skipping")
        return  # Device is not to be partitioned

    # This prevents you from overwriting the device
    LOG.debug("Checking if device %s is a valid device", device)
    if not is_device_valid(device):
        raise Exception(
            "Device {device} is not a disk device!".format(device=device)
        )

    # Remove the partition table entries
    if isinstance(layout, str) and layout.lower() == "remove":
        LOG.debug("Instructed to remove partition table entries")
        purge_disk(device)
        return

    LOG.debug("Checking if device layout matches")
    if check_partition_layout(table_type, device, layout):
        LOG.debug("Device partitioning layout matches")
        return True

    LOG.debug("Checking if device is safe to partition")
    if not overwrite and (is_disk_used(device) or is_filesystem(device)):
        LOG.debug("Skipping partitioning on configured device %s", device)
        return

    LOG.debug("Checking for device size of %s", device)
    device_size = get_hdd_size(device)

    LOG.debug("Calculating partition layout")
    part_definition = get_partition_layout(table_type, device_size, layout)
    LOG.debug("   Layout is: %s", part_definition)

    LOG.debug("Creating partition table on %s", device)
    if "mbr" == table_type:
        exec_mkpart_mbr(device, part_definition)
    elif "gpt" == table_type:
        exec_mkpart_gpt(device, part_definition)
    else:
        raise Exception("Unable to determine table type")

    LOG.debug("Partition table created for %s", device)


def lookup_force_flag(fs):
    """
    A force flag might be -F or -F, this look it up
    """
    flags = {
        "ext": "-F",
        "btrfs": "-f",
        "xfs": "-f",
        "reiserfs": "-f",
        "swap": "-f",
    }

    if "ext" in fs.lower():
        fs = "ext"

    if fs.lower() in flags:
        return flags[fs]

    LOG.warning("Force flag for %s is unknown.", fs)
    return ""


def mkfs(fs_cfg):
    """
    Create a file system on the device.

        label: defines the label to use on the device
        fs_cfg: defines how the filesystem is to look
            The following values are required generally:
                device: which device or cloud defined default_device
                filesystem: which file system type
                overwrite: indiscriminately create the file system
                partition: when device does not define a partition,
                            setting this to a number will mean
                            device + partition. When set to 'auto', the
                            first free device or the first device which
                            matches both label and type will be used.

                            'any' means the first filesystem that matches
                            on the device.

            When 'cmd' is provided then no other parameter is required.
    """
    label = fs_cfg.get("label")
    device = fs_cfg.get("device")
    partition = str(fs_cfg.get("partition", "any"))
    fs_type = fs_cfg.get("filesystem")
    fs_cmd = fs_cfg.get("cmd", [])
    fs_opts = fs_cfg.get("extra_opts", [])
    fs_replace = fs_cfg.get("replace_fs", False)
    overwrite = fs_cfg.get("overwrite", False)

    # ensure that we get a real device rather than a symbolic link
    assert_and_settle_device(device)
    device = os.path.realpath(device)

    # This allows you to define the default ephemeral or swap
    LOG.debug("Checking %s against default devices", device)

    if not partition or partition.isdigit():
        # Handle manual definition of partition
        if partition.isdigit():
            device = "%s%s" % (device, partition)
            LOG.debug(
                "Manual request of partition %s for %s", partition, device
            )

        # Check to see if the fs already exists
        LOG.debug("Checking device %s", device)
        check_label, check_fstype, _ = check_fs(device)
        LOG.debug(
            "Device '%s' has check_label='%s' check_fstype=%s",
            device,
            check_label,
            check_fstype,
        )

        if check_label == label and check_fstype == fs_type:
            LOG.debug("Existing file system found at %s", device)

            if not overwrite:
                LOG.debug("Device %s has required file system", device)
                return
            else:
                LOG.warning("Destroying filesystem on %s", device)

        else:
            LOG.debug("Device %s is cleared for formating", device)

    elif partition and str(partition).lower() in ("auto", "any"):
        # For auto devices, we match if the filesystem does exist
        odevice = device
        LOG.debug("Identifying device to create %s filesytem on", label)

        # 'any' means pick the first match on the device with matching fs_type
        label_match = True
        if partition.lower() == "any":
            label_match = False

        device, reuse = find_device_node(
            device,
            fs_type=fs_type,
            label=label,
            label_match=label_match,
            replace_fs=fs_replace,
        )
        LOG.debug("Automatic device for %s identified as %s", odevice, device)

        if reuse:
            LOG.debug("Found filesystem match, skipping formating.")
            return

        if not reuse and fs_replace and device:
            LOG.debug("Replacing file system on %s as instructed.", device)

        if not device:
            LOG.debug(
                "No device available that matches request. "
                "Skipping fs creation for %s",
                fs_cfg,
            )
            return
    elif not partition or str(partition).lower() == "none":
        LOG.debug("Using the raw device to place filesystem %s on", label)

    else:
        LOG.debug("Error in device identification handling.")
        return

    LOG.debug(
        "File system type '%s' with label '%s' will be created on %s",
        fs_type,
        label,
        device,
    )

    # Make sure the device is defined
    if not device:
        LOG.warning("Device is not known: %s", device)
        return

    # Check that we can create the FS
    if not (fs_type or fs_cmd):
        raise Exception(
            "No way to create filesystem '{label}'. fs_type or fs_cmd "
            "must be set.".format(label=label)
        )

    # Create the commands
    shell = False
    if fs_cmd:
        fs_cmd = fs_cfg["cmd"] % {
            "label": label,
            "filesystem": fs_type,
            "device": device,
        }
        shell = True

        if overwrite:
            LOG.warning(
                "fs_setup:overwrite ignored because cmd was specified: %s",
                fs_cmd,
            )
        if fs_opts:
            LOG.warning(
                "fs_setup:extra_opts ignored because cmd was specified: %s",
                fs_cmd,
            )
    else:
        # Find the mkfs command
        mkfs_cmd = subp.which("mkfs.%s" % fs_type)
        if not mkfs_cmd:
            # for "mkswap"
            mkfs_cmd = subp.which("mk%s" % fs_type)

        if not mkfs_cmd:
            LOG.warning(
                "Cannot create fstype '%s'.  No mkfs.%s command",
                fs_type,
                fs_type,
            )
            return

        fs_cmd = [mkfs_cmd]

        if label:
            fs_cmd.extend(["-L", label])

        # File systems that support the -F flag
        if overwrite or device_type(device) == "disk":
            force_flag = lookup_force_flag(fs_type)
            if force_flag:
                fs_cmd.append(force_flag)

        # Add the extends FS options
        if fs_opts:
            fs_cmd.extend(fs_opts)

        fs_cmd.append(device)

    LOG.debug("Creating file system %s on %s", label, device)
    LOG.debug("     Using cmd: %s", str(fs_cmd))
    try:
        subp.subp(fs_cmd, shell=shell)
    except Exception as e:
        raise Exception("Failed to exec of '%s':\n%s" % (fs_cmd, e)) from e


# vi: ts=4 expandtab