From 017efafe9baaa5c542082136d584bd391487e0f8 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Fri, 13 May 2016 16:10:32 +0200 Subject: [PATCH] New luksopenswaphookcfg module to set up openswap configuration. --- .../luksopenswaphookcfg.conf | 2 + src/modules/luksopenswaphookcfg/main.py | 73 +++++++++++++++++++ src/modules/luksopenswaphookcfg/module.desc | 5 ++ 3 files changed, 80 insertions(+) create mode 100644 src/modules/luksopenswaphookcfg/luksopenswaphookcfg.conf create mode 100644 src/modules/luksopenswaphookcfg/main.py create mode 100644 src/modules/luksopenswaphookcfg/module.desc diff --git a/src/modules/luksopenswaphookcfg/luksopenswaphookcfg.conf b/src/modules/luksopenswaphookcfg/luksopenswaphookcfg.conf new file mode 100644 index 000000000..886867f8d --- /dev/null +++ b/src/modules/luksopenswaphookcfg/luksopenswaphookcfg.conf @@ -0,0 +1,2 @@ +--- +configFilePath: /etc/openswap.conf diff --git a/src/modules/luksopenswaphookcfg/main.py b/src/modules/luksopenswaphookcfg/main.py new file mode 100644 index 000000000..77686f65e --- /dev/null +++ b/src/modules/luksopenswaphookcfg/main.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# === This file is part of Calamares - === +# +# Copyright 2016, Teo Mrnjavac +# +# Calamares 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. +# +# Calamares 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 Calamares. If not, see . + +import libcalamares + + +def write_openswap_conf(partitions, openswap_conf_path): + swap_outer_uuid = "" + swap_mapper_name = "" + mountable_keyfile_device = "" + + for partition in partitions: + if partition["fs"] == "linuxswap" and "luksMapperName" in partition: + swap_outer_uuid = partition["luksUuid"] + swap_mapper_name = partition["luksMapperName"] + + elif partition["mountPoint"] == "/" and "luksMapperName" in partition: + mountable_keyfile_device = "/dev/mapper/{!s}".format(partition["luksMapperName"]) + + if not mountable_keyfile_device or not swap_outer_uuid: + return None + + swap_device_path = "/dev/disk/by-uuid/{!s}".format(swap_outer_uuid) + + lines = [] + with open(openswap_conf_path, 'r') as openswap_file: + lines = [x.strip() for x in openswap_file.readlines()] + + for i in range(len(lines)): + if lines[i].startswith("swap_device"): + lines[i] = "swap_device={!s}".format(swap_device_path) + + elif lines[i].startswith("crypt_swap_name"): + lines[i] = "crypt_swap_name={!s}".format(swap_mapper_name) + + elif lines[i].startswith("keyfile_device"): + lines[i] = "keyfile_device={!s}".format(mountable_keyfile_device) + + elif lines[i].startswith("keyfile_filename"): + lines[i] = "keyfile_filename=crypto_keyfile.bin" + + with open(openswap_conf_path, 'w') as openswap_file: + openswap_file.write("\n".join(lines) + "\n") + + return None + + +def run(): + """ + This module sets up the openswap hook for a resumable encrypted swap. + :return: + """ + + openswap_conf_path = libcalamares.job.configuration["configFilePath"] + partitions = libcalamares.globalstorage.value["partitions"] + return write_openswap_conf(partitions, openswap_conf_path) diff --git a/src/modules/luksopenswaphookcfg/module.desc b/src/modules/luksopenswaphookcfg/module.desc new file mode 100644 index 000000000..53f8b7c39 --- /dev/null +++ b/src/modules/luksopenswaphookcfg/module.desc @@ -0,0 +1,5 @@ +--- +type: "job" +name: "luksopenswaphookcfg" +interface: "python" +script: "main.py"