summaryrefslogtreecommitdiff
path: root/vmdb/plugins/fstab_plugin.py
blob: fdc358c646ef2cd4edba1b3e3f27e4630572480e (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
# Copyright 2019 Antonio Terceiro
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=

import os

import vmdb


class FstabPlugin(vmdb.Plugin):
    def enable(self):
        self.app.step_runners.add(FstabStepRunner())


class FstabStepRunner(vmdb.StepRunnerInterface):
    def get_key_spec(self):
        return {"fstab": str}

    def run(self, values, setting, state):
        tag = values["fstab"]
        chroot = state.tags.get_builder_mount_point(tag)

        filesystems = []
        crypts = []

        for tag in state.tags.get_tags():
            device = state.tags.get_dev(tag)
            mount_point = state.tags.get_target_mount_point(tag)

            fstype = state.tags.get_fstype(tag)
            fsuuid = state.tags.get_fsuuid(tag)
            luksuuid = state.tags.get_luksuuid(tag)
            dm = state.tags.get_dm(tag)

            if mount_point is not None:
                if fsuuid is None:
                    raise Exception(
                        "Unknown UUID for device {} (to be mounted on {})".format(
                            device, mount_point
                        )
                    )

                filesystems.append(
                    {
                        "uuid": fsuuid,
                        "mount_point": mount_point,
                        "fstype": fstype,
                    }
                )
            elif luksuuid is not None and dm is not None:
                crypts.append(
                    {
                        "dm": dm,
                        "luksuuid": luksuuid,
                    }
                )

        fstab_path = os.path.join(chroot, "etc/fstab")
        line = "UUID={uuid} {mount_point} {fstype} errors=remount-ro 0 1\n"
        with open(fstab_path, "w") as fstab:
            for entry in filesystems:
                fstab.write(line.format(**entry))

        vmdb.progress(f"crypts: {crypts}")
        if crypts:
            crypttab_path = os.path.join(chroot, "etc/crypttab")
            line = "{dm} UUID={luksuuid} none luks,discard\n"
            with open(crypttab_path, "w") as crypttab:
                for entry in crypts:
                    crypttab.write(line.format(**entry))