source: calamares/trunk/fuentes/src/modules/luksopenswaphookcfg/main.py @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 3.0 KB
Line 
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# === This file is part of Calamares - <https://github.com/calamares> ===
5#
6#   Copyright 2016, Teo Mrnjavac <teo@kde.org>
7#   Copyright 2017, Alf Gaida <agaida@siduction.org>
8#
9#   Calamares is free software: you can redistribute it and/or modify
10#   it under the terms of the GNU General Public License as published by
11#   the Free Software Foundation, either version 3 of the License, or
12#   (at your option) any later version.
13#
14#   Calamares is distributed in the hope that it will be useful,
15#   but WITHOUT ANY WARRANTY; without even the implied warranty of
16#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17#   GNU General Public License for more details.
18#
19#   You should have received a copy of the GNU General Public License
20#   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
21
22import libcalamares
23import os.path
24
25
26def write_openswap_conf(partitions, root_mount_point, openswap_conf_path):
27    swap_outer_uuid = ""
28    swap_mapper_name = ""
29    mountable_keyfile_device = ""
30
31    for partition in partitions:
32        if partition["fs"] == "linuxswap" and "luksMapperName" in partition:
33            swap_outer_uuid = partition["luksUuid"]
34            swap_mapper_name = partition["luksMapperName"]
35
36        elif partition["mountPoint"] == "/" and "luksMapperName" in partition:
37            mountable_keyfile_device = (
38                "/dev/mapper/{!s}".format(partition["luksMapperName"])
39                )
40
41    if not mountable_keyfile_device or not swap_outer_uuid:
42        return None
43
44    swap_device_path = "/dev/disk/by-uuid/{!s}".format(swap_outer_uuid)
45
46    lines = []
47    with open(os.path.join(root_mount_point,
48                           openswap_conf_path), 'r') as openswap_file:
49        lines = [x.strip() for x in openswap_file.readlines()]
50
51    for i in range(len(lines)):
52        if lines[i].startswith("swap_device"):
53            lines[i] = "swap_device={!s}".format(swap_device_path)
54
55        elif lines[i].startswith("crypt_swap_name"):
56            lines[i] = "crypt_swap_name={!s}".format(swap_mapper_name)
57
58        elif lines[i].startswith("keyfile_device"):
59            lines[i] = "keyfile_device={!s}".format(mountable_keyfile_device)
60
61        elif lines[i].startswith("keyfile_filename"):
62            lines[i] = "keyfile_filename=crypto_keyfile.bin"
63
64    with open(os.path.join(root_mount_point,
65                           openswap_conf_path), 'w') as openswap_file:
66        openswap_file.write("\n".join(lines) + "\n")
67
68    return None
69
70
71def run():
72    """
73    This module sets up the openswap hook for a resumable encrypted swap.
74    :return:
75    """
76
77    root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
78    openswap_conf_path = libcalamares.job.configuration["configFilePath"]
79    partitions = libcalamares.globalstorage.value("partitions")
80
81    openswap_conf_path = openswap_conf_path.lstrip('/')
82
83    return write_openswap_conf(
84        partitions, root_mount_point, openswap_conf_path
85        )
Note: See TracBrowser for help on using the repository browser.