Skip to content

Commit c7c3d3b

Browse files
committed
updated to be more like dracut in how it runs, made cpio required, warn about cpio chown errors and changed the cmdline args to allow specifying the output file
Signed-off-by: Zen <[email protected]>
1 parent 0e438ac commit c7c3d3b

File tree

10 files changed

+61
-44
lines changed

10 files changed

+61
-44
lines changed

config_example.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# This config will decrypt the LUKS volume with uuid "fdf442da-0574-4531-98c7-55227a041f1d", mapping it to "/dev/mapper/root"
22
# It will attempt to mount the btrfs volume with label "rootfs" to /mnt/root
33
# It will pull all current kernel modules from lspci -k results
4-
# The "ugrd.base.cpio" module will pack the initramfs into a cpio archive
54

65
modules = [
76
"ugrd.base.base",
87
"ugrd.kmod.kmod",
98
"ugrd.kmod.nosound",
109
"ugrd.kmod.novideo",
1110
"ugrd.crypto.cryptsetup",
12-
"ugrd.base.cpio"
1311
]
1412

1513
# The initramfs will be built in /tmp/initramfs if "build_dir" is not specified not specified

config_yubikey.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ modules = [
1111
"ugrd.kmod.usb",
1212
"ugrd.kmod.nvme",
1313
"ugrd.crypto.smartcard",
14-
"ugrd.base.cpio"
1514
]
1615

1716
sc_public_key = "/etc/ugrd/pubkey.gpg"

readme.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ To install `ugrd`, clone the repo and run `pip install .`, setting `--break-syst
2424

2525
Once installed, either set the config file in `/etc/ugrd/config.toml`, or pass it to the script:
2626

27-
`ugrd example_config.toml`
27+
`ugrd -c example_config.toml`
2828

2929
> Debug mode can be enabled with `-d` or verbose debugging with `-dd`
3030
31+
The last argument is the output file, which can be a path:
32+
33+
`ugrd /boot/ugrd.cpio`
34+
3135
## Runtime usage
3236

3337
`ugrd` runs the `init` script generated in the build dir. In cases where `agetty` is needed, most of the work is done in `init_main.sh`.
@@ -57,7 +61,7 @@ Modules write to a shared config dict that is accessible by other modules.
5761

5862
#### base.base
5963

60-
> The main module, mostly pulls basic binaries and pulls the `core` and `mounts` modules
64+
> The main module, mostly pulls basic binaries and pulls the `core`, `mounts`, and `cpio` module.
6165
6266
* `shebang` (#!/bin/bash) sets the shebang on the init script.
6367

@@ -70,6 +74,14 @@ Modules write to a shared config dict that is accessible by other modules.
7074
* `binaries` is a list used to define programs to be pulled into the initrams. `which` is used to find the path of added entries, and `lddtree` is used to resolve dependendies.
7175
* `paths` is a list of directores to create in the `build_dir`. They do not need a leading `/`.
7276

77+
#### base.cpio
78+
79+
This module handles CPIO creation.
80+
81+
* `out_file` (ugrd.cpio) Sets the name of the output file, under `out_dir` unless a path is defined.
82+
* `cpio_list_name` (cpio.list) Sets the name of the cpio list file for `gen_init_cpio`
83+
* `mknod_cpio` (true) Only create devicne nodes within the CPIO.
84+
7385
##### symlink creation
7486

7587
Symlinks are defined in the `symlinks` dict. Each entry must have a name, `source` and `target`:
@@ -80,7 +92,6 @@ source = "/usr/bin/pinentry-tty"
8092
target = "/usr/bin/pinentry"
8193
```
8294

83-
8495
##### Copying files to a different destination
8596

8697
Using the `dependencies` list will pull files into the initramfs using the same path on the host system.
@@ -145,19 +156,6 @@ Defines /dev/ttyS1 as a `vt100` terminal with a `115200` baud rate.
145156

146157
`primary_console` (tty0) is used to set which console will be initialized with agetty on boot.
147158

148-
#### base.cpio
149-
150-
This module assists in creating a CPIO out of the initramfs `build_dir`.
151-
152-
> This module is run during the `pack` phase.
153-
154-
The following parameters can be set to alter CPIO functionality:
155-
156-
* `mknod_cpio` (true) Only create the device nodes within the CPIO file.
157-
* `cpio_filename` (ugrd.cpio) can be set to change the final CPIO filename in the `out_dir`.
158-
* `cpio_list_name` (cpio.list) can be used to change the filename of the CPIO list for `gen_init_cpio`.
159-
* `_gen_init_cpio_path` The path to this tool can be specified. If not, it is included and will be built at runtime if needed.
160-
161159
#### base.debug
162160

163161
This module contains debug programs such as `cp`, `mv`, `rm`, `grep`, `dmesg`, `find`, and `nano`,
@@ -310,7 +308,7 @@ key_type = "gpg"
310308

311309
Cryptsetup global config:
312310

313-
* `cryptsetup_key_type` (keyfile) Used to determine how a key is unlocked, setting it globally changes the default for definitions
311+
* `cryptsetup_key_type` - Sets the default `key_type` for all cryptsetup entries.
314312
* `cryptsetup_retries` (5) The default number of times to try to unlock a device.
315313
* `cryptsetup_autoretry` (false) Whether or not to automatically retry mount attempts.
316314

@@ -448,7 +446,7 @@ The `cpio` module imports the `make_cpio_list` packing function with:
448446

449447
```
450448
[imports.pack]
451-
"ugrd.base.cpio" = [ "make_cpio_list" ]
449+
"ugrd.base.base" = [ "make_cpio_list" ]
452450
```
453451

454452
#### init hooks

ugrd/base/base.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
shebang = "#!/bin/bash"
2-
3-
modules = ["ugrd.base.core", "ugrd.fs.mounts"]
1+
modules = ["ugrd.base.core", "ugrd.fs.mounts", "ugrd.base.cpio"]
42

53
binaries = [ "bash", "ls", "cat", "switch_root" ]
64

75
paths = [ "root" ]
86

7+
shebang = "#!/bin/bash"
8+
99
[imports.init_final]
1010
"ugrd.base.base" = [ "switch_root" ]
1111

1212
[custom_parameters]
1313
shebang = "str" # Add the shebang property, used because this is a bash script
14-

ugrd/base/cpio.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
__author__ = "desultory"
2-
__version__ = "0.1.1"
1+
__author__ = 'desultory'
2+
__version__ = '1.1.0'
3+
34

45
from subprocess import run
56
from pathlib import Path
@@ -26,7 +27,7 @@ def pack_cpio(self):
2627
"""
2728
Packs the CPIO file using gen_init_cpio
2829
"""
29-
cpio_path = self.config_dict.get('gen_init_cpio_path', Path(__file__).parent.parent / 'include' / 'gen_init_cpio')
30+
cpio_path = Path(__file__).parent.parent / 'include' / 'gen_init_cpio'
3031

3132
if not cpio_path.exists():
3233
build_gen_init_cpio(self, cpio_path)
@@ -36,15 +37,19 @@ def pack_cpio(self):
3637
packing_list = str(self.out_dir / self.config_dict['cpio_list_name'])
3738
self.logger.info("Creating CPIO file from packing list: %s" % packing_list)
3839

39-
out_cpio = self.out_dir / self.config_dict['cpio_filename']
40+
out_cpio = self.out_dir / self.config_dict['out_file']
4041

4142
with open(out_cpio, 'wb') as cpio_file:
4243
cmd = run([str(cpio_path), packing_list], stdout=cpio_file)
4344
if cmd.returncode != 0:
4445
raise RuntimeError("gen_init_cpio failed with error: %s" % cmd.stderr.decode())
4546

4647
self.logger.info("CPIO file created at: %s" % out_cpio)
47-
self._chown(out_cpio)
48+
49+
try:
50+
self._chown(out_cpio)
51+
except PermissionError:
52+
self.logger.warning("Unable to change the owner of the CPIO file: %s" % out_cpio)
4853

4954

5055
def generate_cpio_mknods(self):
@@ -94,7 +99,7 @@ def make_cpio_list(self):
9499
file_dest = relative_dir / file
95100
file_source = current_dir / file
96101
if file_source.is_symlink():
97-
symlink_list.append(f"slink /{file_dest} /{file_source.resolve().relative_to(self.build_dir)} 777 0 0")
102+
symlink_list.append(f"slink /{file_dest} {file_source.resolve()} 777 0 0")
98103
elif file_source.is_char_device():
99104
node_major = major(file_source.stat().st_rdev)
100105
node_minor = minor(file_source.stat().st_rdev)
@@ -122,4 +127,3 @@ def make_cpio_list(self):
122127
packing_list = directory_list + file_list + symlink_list + node_list
123128

124129
self._write(self.out_dir / self.config_dict['cpio_list_name'], packing_list, in_build_dir=False)
125-

ugrd/base/cpio.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
cpio_filename = "ugrd.cpio"
1+
out_file = "ugrd.cpio"
22
cpio_list_name = "cpio.list"
33
mknod_cpio = true
44

55
[imports.pack]
66
"ugrd.base.cpio" = [ "make_cpio_list", "pack_cpio" ]
77

88
[custom_parameters]
9-
mknod_cpio = "bool" # When enabled, mknod is not used to create device nodes, they are just created in the cpio.
10-
cpio_filename = "str" # The name of the cpio file to create.
9+
out_file = "str" # The name of the cpio file to create.
1110
cpio_list_name = "str" # The name of the CPIO packing list to create
12-
_gen_init_cpio_path = "Path" # The path of the gen_init_cpio binary
11+
mknod_cpio = "bool" # When enabled, mknod is not used to create device nodes, they are just created in the cpio.

ugrd/crypto/cryptsetup.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ _kmod_depend = [ "dm_crypt", "crc32c" ]
66

77
cryptsetup_retries = 5
88
cryptsetup_autoretry = true
9-
cryptsetup_key_type = "keyfile"
109

1110
[imports.config_processing]
1211
"ugrd.crypto.cryptsetup" = [ "_process_cryptsetup_multi", "_process_cryptsetup_key_types_multi" ]

ugrd/initramfs_dict.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ def __init__(self, *args, **kwargs):
3232
else:
3333
super().__setitem__(parameter, default_type())
3434

35+
def import_args(self, args: dict):
36+
"""
37+
Imports data from an argument dict
38+
"""
39+
for arg, value in args.items():
40+
self.logger.warning("Importing argument '%s' with value: %s" % (arg, value))
41+
self[arg] = value
42+
3543
def __setitem__(self, key, value):
3644
# If the type is registered, use the appropriate update function
3745
if expected_type := self.builtin_parameters.get(key, self['custom_parameters'].get(key)):

ugrd/initramfs_generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
__author__ = "desultory"
3-
__version__ = "0.7.8"
3+
__version__ = "0.8.0"
44

55
from tomllib import load
66
from pathlib import Path
@@ -23,6 +23,7 @@ def __init__(self, config='/etc/ugrd/config.toml', *args, **kwargs):
2323
self.load_config()
2424
self.config_dict.verify_deps()
2525
self.config_dict.verify_mask()
26+
self.config_dict.import_args(kwargs)
2627

2728
def load_config(self):
2829
"""

ugrd/main.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ def main():
1111
argparser = ArgumentParser(prog='ugrd',
1212
description='MicrogRAM disk initramfs generator')
1313

14-
argparser.add_argument('config_file',
15-
action='store',
16-
nargs='?',
17-
help='Config file location')
18-
1914
argparser.add_argument('-d', '--debug', action='store_true', help='Debug mode')
2015
argparser.add_argument('-dd', '--verbose-debug', action='store_true', help='Verbose debug mode')
2116

17+
# Add arguments for dracut compatibility
18+
argparser.add_argument('-c', '--config', action='store', help='Config file location')
19+
argparser.add_argument('--kver', action='store', help='Kernel version')
20+
21+
# Add and ignore arguments for dracut compatibility
22+
argparser.add_argument('--force', action='store_true', help='Not used, for dracut compatibility')
23+
argparser.add_argument('--kernel-image', action='store', help='Kernel image')
24+
25+
# Add the argument for the output file
26+
argparser.add_argument('output_file', action='store', help='Output file location', nargs='?')
27+
2228
args = argparser.parse_args()
2329

2430
logger = logging.getLogger()
@@ -30,9 +36,15 @@ def main():
3036
logger.setLevel(20)
3137

3238
kwargs = {'logger': logger}
33-
if config := args.config_file:
39+
if config := args.config:
3440
kwargs['config'] = config
3541

42+
if config := args.kver:
43+
kwargs['kernel_version'] = config
44+
45+
if config := args.output_file:
46+
kwargs['out_file'] = config
47+
3648
generator = InitramfsGenerator(**kwargs)
3749
try:
3850
generator.build_structure()

0 commit comments

Comments
 (0)