mirror of
https://github.com/gryf/mkinitramfs.git
synced 2025-12-18 12:00:21 +01:00
Moved configuration to toml
This commit is contained in:
77
README.rst
77
README.rst
@@ -7,46 +7,40 @@ Usage
|
|||||||
-----
|
-----
|
||||||
|
|
||||||
- Create encrypted disk or partition using `cryptsetup`_
|
- Create encrypted disk or partition using `cryptsetup`_
|
||||||
- Create ``~/.config/mkinitramfs/disks.json`` file with similar content to:
|
- Create ``~/.config/mkinitramfs.toml`` file with similar content to:
|
||||||
|
|
||||||
.. code:: json
|
.. code:: toml
|
||||||
|
|
||||||
{
|
[name]
|
||||||
"name": {
|
uuid = "disk-uuid"
|
||||||
"uuid": "disk-uuid",
|
key = "key-filename"
|
||||||
"key": "key-filename"
|
|
||||||
},
|
|
||||||
...
|
|
||||||
}
|
|
||||||
|
|
||||||
where every entry have disk name (**name** in this case), which have two
|
...
|
||||||
attributes - disk/partition UUID and key filename.
|
|
||||||
|
where every entry have disk name (**name** in this case), which have at least
|
||||||
|
two attributes - disk/partition UUID and key filename.
|
||||||
- Provide a key file for the disk/partition. Assumption is, that it is an
|
- Provide a key file for the disk/partition. Assumption is, that it is an
|
||||||
encrypted file using `ccrypt`_ instead of plain file or password protected
|
encrypted file using `ccrypt`_ instead of plain file or password protected
|
||||||
luks. Keys will be looked using provided path, i.e.
|
luks. Keys will be looked using provided path, i.e.
|
||||||
|
|
||||||
.. code:: json
|
.. code:: toml
|
||||||
|
|
||||||
{
|
[laptop]
|
||||||
"laptop": {
|
uuid = "88b99002-028f-4744-94e7-45e4580e2ddd"
|
||||||
"uuid": "88b99002-028f-4744-94e7-45e4580e2ddd",
|
key = "/full/path/to/the/laptop.key"
|
||||||
"key": "/full/path/to/the/laptop.key"
|
|
||||||
},
|
[desktop]
|
||||||
"desktop": {
|
uuid = "23e31327-1411-491c-ab00-c36f74c441f1"
|
||||||
"uuid": "23e31327-1411-491c-ab00-c36f74c441f1",
|
key = "desktop.key"
|
||||||
"key": "desktop.key"
|
|
||||||
},
|
[pendrive]
|
||||||
"pendrive": {
|
uuid = "1453a45e-ca3f-4d39-8fd7-a6a96873c25c"
|
||||||
"uuid": "1453a45e-ca3f-4d39-8fd7-a6a96873c25c",
|
key = "../pendrive.key"
|
||||||
"key": "../pendrive.key"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
so yes - it is possible to use key file in absolute or relative paths. If no
|
so yes - it is possible to use key file in absolute or relative paths. If no
|
||||||
key will be found, it's been looking for in path specified by
|
key will be found, it's been looking for in path specified by
|
||||||
``--key-path | -k`` parameter, which by default is in
|
``--key-path | -k`` parameter, which by default is in
|
||||||
``$XDG_CONFIG_HOME/mkinitramfs/keys`` (usually in
|
``$XDG_DATA_HOME/keys`` (usually it will be ``~/.local/share/keys``).
|
||||||
``~/.config/mkinitramfs/keys``.
|
|
||||||
- Move ``mkinitramfs.py`` script to some location in your ``$PATH`` (like
|
- Move ``mkinitramfs.py`` script to some location in your ``$PATH`` (like
|
||||||
``~/bin``)
|
``~/bin``)
|
||||||
- Invoke ``mkinitramfs.py`` script:
|
- Invoke ``mkinitramfs.py`` script:
|
||||||
@@ -62,6 +56,33 @@ Usage
|
|||||||
on ``/boot`` with appropriate links. Note, that old images (they have
|
on ``/boot`` with appropriate links. Note, that old images (they have
|
||||||
``.old`` suffix in the filename) will be removed in that case.
|
``.old`` suffix in the filename) will be removed in that case.
|
||||||
|
|
||||||
|
Configuration
|
||||||
|
-------------
|
||||||
|
|
||||||
|
Other than key path and device UUID, configuration can hold additional options
|
||||||
|
similar to those passed via commandline. Consider following example:
|
||||||
|
|
||||||
|
.. code:: toml
|
||||||
|
|
||||||
|
[laptop]
|
||||||
|
uuid = "88b99002-028f-4744-94e7-45e4580e2ddd"
|
||||||
|
key_path = "/full/path/to/the/keys/dir"
|
||||||
|
key = "laptop.key"
|
||||||
|
yubikey = true
|
||||||
|
|
||||||
|
This will inform mkinitramfs script, that dropbear and yubikey features are
|
||||||
|
enabled. Also for network related configuration, there are last three options.
|
||||||
|
|
||||||
|
The complete list of supported options is listed below:
|
||||||
|
|
||||||
|
- ``copy_modules``
|
||||||
|
- ``no_key``
|
||||||
|
- ``key_path``
|
||||||
|
- ``key``
|
||||||
|
- ``disk_label``
|
||||||
|
- ``sdcard``
|
||||||
|
- ``yubikey``
|
||||||
|
|
||||||
Using key devices
|
Using key devices
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
|||||||
113
mkinitramfs.py
113
mkinitramfs.py
@@ -1,20 +1,20 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
"""
|
"""
|
||||||
Python2/3 compatible initrd generating script
|
Python initrd generating script
|
||||||
"""
|
"""
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import tomllib
|
||||||
|
|
||||||
|
|
||||||
XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
|
XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
|
||||||
XDG_DATA_HOME = os.getenv('XDG_DATA_HOME',
|
XDG_DATA_HOME = os.getenv('XDG_DATA_HOME',
|
||||||
os.path.expanduser('~/.local/share'))
|
os.path.expanduser('~/.local/share'))
|
||||||
CONF_PATH = os.path.join(XDG_CONFIG_HOME, 'mkinitramfs.json')
|
CONF_PATH = os.path.join(XDG_CONFIG_HOME, 'mkinitramfs.toml')
|
||||||
KEYS_PATH = os.path.join(XDG_DATA_HOME, 'keys')
|
KEYS_PATH = os.path.join(XDG_DATA_HOME, 'keys')
|
||||||
SHEBANG = "#!/bin/bash\n"
|
SHEBANG = "#!/bin/bash\n"
|
||||||
SHEBANG_ASH = "#!/bin/sh\n"
|
SHEBANG_ASH = "#!/bin/sh\n"
|
||||||
@@ -227,22 +227,61 @@ exec switch_root /new-root /sbin/init
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
defaults = {'copy_modules': False,
|
||||||
|
'disk_label': None,
|
||||||
|
'install': False,
|
||||||
|
'key_path': None,
|
||||||
|
'lvm': False,
|
||||||
|
'no_key': False,
|
||||||
|
'sdcard': None,
|
||||||
|
'yubikey': False}
|
||||||
|
|
||||||
|
def __init__(self, args, toml_conf):
|
||||||
|
self.drive = args.get('drive')
|
||||||
|
toml_ = toml_conf[self.drive]
|
||||||
|
|
||||||
|
for k, v in self.defaults.items():
|
||||||
|
setattr(self, k, toml_.get(k, v))
|
||||||
|
if getattr(self, k) is not args.get(k) and args.get(k) is not None:
|
||||||
|
setattr(self, k, args[k])
|
||||||
|
|
||||||
|
key = None
|
||||||
|
if not self.key_path and toml_.get('key'):
|
||||||
|
key = toml_.get('key')
|
||||||
|
if not os.path.exists(key):
|
||||||
|
key = os.path.join(KEYS_PATH, key)
|
||||||
|
if not os.path.exists(key):
|
||||||
|
sys.stderr.write(f'Cannot find key file for '
|
||||||
|
'{toml_.get("key")}.\n')
|
||||||
|
sys.exit(2)
|
||||||
|
self.key_path = key
|
||||||
|
|
||||||
|
if not (self.key_path or self.no_key):
|
||||||
|
sys.stderr.write(f'key file for {self.drive} is not provided, '
|
||||||
|
'while no-key option is not set.\n')
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
|
# UUID is only available via config file
|
||||||
|
self.uuid = toml_.get('uuid')
|
||||||
|
|
||||||
|
|
||||||
class Initramfs(object):
|
class Initramfs(object):
|
||||||
def __init__(self, args, disks):
|
def __init__(self, conf):
|
||||||
self.lvm = args.lvm
|
self.lvm = conf.lvm
|
||||||
self.yk = args.yubikey
|
self.yk = conf.yubikey
|
||||||
self.name = args.disk
|
self.name = args.disk
|
||||||
self.modules = args.copy_modules
|
self.modules = conf.copy_modules
|
||||||
self.key_path = args.key_path
|
self.key_path = conf.key_path
|
||||||
self.disk_label = args.disk_label
|
self.disk_label = conf.disk_label
|
||||||
self.sdcard = args.sdcard
|
self.sdcard = conf.sdcard
|
||||||
self.install = args.install
|
self.install = conf.install
|
||||||
self.no_key = args.no_key
|
self.no_key = conf.no_key
|
||||||
|
|
||||||
self.dirname = None
|
self.dirname = None
|
||||||
self.kernel_ver = os.readlink('/usr/src/linux').replace('linux-', '')
|
self.kernel_ver = os.readlink('/usr/src/linux').replace('linux-', '')
|
||||||
self._make_tmp()
|
self._make_tmp()
|
||||||
self._disks = disks
|
self._disks = conf.drive
|
||||||
|
|
||||||
def _make_tmp(self):
|
def _make_tmp(self):
|
||||||
self.dirname = tempfile.mkdtemp(prefix='init_')
|
self.dirname = tempfile.mkdtemp(prefix='init_')
|
||||||
@@ -318,14 +357,13 @@ class Initramfs(object):
|
|||||||
os.symlink('busybox', command)
|
os.symlink('busybox', command)
|
||||||
|
|
||||||
def _copy_key(self, suffix=''):
|
def _copy_key(self, suffix=''):
|
||||||
key_path = self._disks[self.name]['key'] + suffix
|
key_path = self.key_path
|
||||||
if not os.path.exists(key_path):
|
if not os.path.exists(key_path):
|
||||||
key_path = os.path.join(self.key_path,
|
key_path = os.path.join(self.key_path + suffix)
|
||||||
self._disks[self.name]['key'] + suffix)
|
|
||||||
|
|
||||||
if not os.path.exists(key_path):
|
if not os.path.exists(key_path):
|
||||||
self._cleanup()
|
self._cleanup()
|
||||||
sys.stderr.write(f'Cannot find key(s) file for {self.name}.\n')
|
sys.stderr.write(f'Cannot find key(s) file for {self._drive}.\n')
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
||||||
key_path = os.path.abspath(key_path)
|
key_path = os.path.abspath(key_path)
|
||||||
@@ -337,8 +375,9 @@ class Initramfs(object):
|
|||||||
os.chdir(self.dirname)
|
os.chdir(self.dirname)
|
||||||
with open('init', 'w') as fobj:
|
with open('init', 'w') as fobj:
|
||||||
fobj.write(SHEBANG_ASH)
|
fobj.write(SHEBANG_ASH)
|
||||||
fobj.write(f"UUID='{self._disks[self.name]['uuid']}'\n")
|
fobj.write(f"UUID='{self.uuid}'\n")
|
||||||
fobj.write(f"KEY='/keys/{self._disks[self.name]['key']}'\n")
|
if self.key:
|
||||||
|
fobj.write(f"KEY='/keys/{self.key}'\n")
|
||||||
fobj.write(INIT)
|
fobj.write(INIT)
|
||||||
fobj.write(INIT_CMD)
|
fobj.write(INIT_CMD)
|
||||||
if self.disk_label:
|
if self.disk_label:
|
||||||
@@ -349,7 +388,7 @@ class Initramfs(object):
|
|||||||
if self.disk_label or self.sdcard:
|
if self.disk_label or self.sdcard:
|
||||||
fobj.write(DECRYPT_KEYDEV)
|
fobj.write(DECRYPT_KEYDEV)
|
||||||
if self.yk:
|
if self.yk:
|
||||||
fobj.write(DECRYPT_YUBICP % {'disk': self.name})
|
fobj.write(DECRYPT_YUBICP % {'disk': self._drive})
|
||||||
fobj.write(DECRYPT_PASSWORD)
|
fobj.write(DECRYPT_PASSWORD)
|
||||||
fobj.write(SWROOT)
|
fobj.write(SWROOT)
|
||||||
|
|
||||||
@@ -421,22 +460,22 @@ class Initramfs(object):
|
|||||||
self._cleanup()
|
self._cleanup()
|
||||||
|
|
||||||
|
|
||||||
def _disks_msg():
|
def _disks_msg(msg=None):
|
||||||
sys.stdout.write('You need to create %s json file with the '
|
if not msg:
|
||||||
'contents:\n\n'
|
sys.stdout.write('You need to create %s toml file with the '
|
||||||
'{\n'
|
'contents:\n\n'
|
||||||
' "name": {\n'
|
'[name]\n'
|
||||||
' "uuid": "disk-uuid",\n'
|
'uuid = "disk-uuid"\n'
|
||||||
' "key": "key-filename"\n'
|
'key = "key-filename"\n'
|
||||||
' },\n'
|
'...\n' % CONF_PATH)
|
||||||
' ...\n'
|
else:
|
||||||
'}\n' % CONF_PATH)
|
sys.stdout.write(msg + '\n')
|
||||||
|
|
||||||
|
|
||||||
def _load_disks():
|
def _load_disks():
|
||||||
try:
|
try:
|
||||||
with open(CONF_PATH) as fobj:
|
with open(CONF_PATH, 'rb') as fobj:
|
||||||
return json.load(fobj)
|
return tomllib.load(fobj)
|
||||||
except IOError:
|
except IOError:
|
||||||
_disks_msg()
|
_disks_msg()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@@ -465,7 +504,7 @@ def main():
|
|||||||
'assuming SD card/usb stick is the only way to open '
|
'assuming SD card/usb stick is the only way to open '
|
||||||
'encrypted root.')
|
'encrypted root.')
|
||||||
parser.add_argument('-k', '--key-path', help='path to the location where '
|
parser.add_argument('-k', '--key-path', help='path to the location where '
|
||||||
'keys are stored', default=KEYS_PATH)
|
'keys are stored')
|
||||||
parser.add_argument('-d', '--disk-label', help='Provide disk label '
|
parser.add_argument('-d', '--disk-label', help='Provide disk label '
|
||||||
'to be read decription key from.')
|
'to be read decription key from.')
|
||||||
parser.add_argument('-s', '--sdcard', help='Use built in sdcard reader to '
|
parser.add_argument('-s', '--sdcard', help='Use built in sdcard reader to '
|
||||||
@@ -474,10 +513,14 @@ def main():
|
|||||||
help='Enable LVM in init.')
|
help='Enable LVM in init.')
|
||||||
parser.add_argument('-y', '--yubikey', action='store_true',
|
parser.add_argument('-y', '--yubikey', action='store_true',
|
||||||
help='Enable Yubikey challenge-response in init.')
|
help='Enable Yubikey challenge-response in init.')
|
||||||
parser.add_argument('disk', choices=disks.keys(), help='Disk name')
|
parser.add_argument('drive', choices=disks.keys(), help='Drive name')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
init = Initramfs(args, disks)
|
if args.drive not in disks:
|
||||||
|
_disks_msg(f'Drive {args.drive} not found in configuration')
|
||||||
|
sys.exit(4)
|
||||||
|
conf = Config(args.__dict__, disks)
|
||||||
|
init = Initramfs(conf)
|
||||||
init.build()
|
init.build()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user