Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ The following root filesystems have been tested:
* BTRFS
* EXT4
* XFS
* FAT32
* NILFS2

Additionally, the following filesystems have been tested for non-root mounts:

* FAT32

If the required kernel module is not built into the kernel, and the filesystem is not listed above, the kernel module may need to be included in `kmod_init`.

> The example config has `kmod_autodetect_lsmod` enabled which should automatically pull in the required modules, unless the active kernel differs from the build kernel.
Expand Down
47 changes: 29 additions & 18 deletions src/ugrd/fs/test_image.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
__version__ = "0.7.1"
__version__ = "0.8.0"

from zenlib.util import contains


@contains('test_flag', 'A test flag must be set to create a test image', raise_exception=True)
@contains("test_flag", "A test flag must be set to create a test image", raise_exception=True)
def init_banner(self):
""" Initialize the test image banner, set a random flag if not set. """
self['banner'] = f"echo {self['test_flag']}"
"""Initialize the test image banner, set a random flag if not set."""
self["banner"] = f"echo {self['test_flag']}"


def _allocate_image(self, image_path):
""" Allocate the test image size """
"""Allocate the test image size"""
if image_path.exists():
if self.clean:
self.logger.warning("Removing existing filesystem image file: %s" % image_path)
Expand All @@ -20,27 +20,38 @@ def _allocate_image(self, image_path):

with open(image_path, "wb") as f:
self.logger.info("Allocating test image file: %s" % f.name)
f.write(b"\0" * self.test_image_size * 2 ** 20)
f.write(b"\0" * self.test_image_size * 2**20)


def make_test_image(self):
""" Creates a test image from the build dir """
build_dir = self._get_build_path('/').resolve()
"""Creates a test image from the build dir"""
build_dir = self._get_build_path("/").resolve()
self.logger.info("Creating test image from: %s" % build_dir)

rootfs_uuid = self['mounts']['root']['uuid']
rootfs_type = self['mounts']['root']['type']
rootfs_uuid = self["mounts"]["root"]["uuid"]
rootfs_type = self["mounts"]["root"]["type"]

image_path = self._get_out_path(self['out_file'])
if rootfs_type == 'ext4':
image_path = self._get_out_path(self["out_file"])
if rootfs_type == "ext4":
# Create the test image file, flll with 0s
_allocate_image(self, image_path)
self._run(['mkfs', '-t', rootfs_type, '-d', build_dir, '-U', rootfs_uuid, '-F', image_path])
elif rootfs_type == 'btrfs':
if self['clean'] and image_path.exists():
self._run(["mkfs", "-t", rootfs_type, "-d", build_dir, "-U", rootfs_uuid, "-F", image_path])
elif rootfs_type == "btrfs":
if self["clean"] and image_path.exists():
self.logger.warning("Removing existing test image file: %s" % image_path)
image_path.unlink()
self._run(['mkfs', '-t', rootfs_type, '-f', '--rootdir', build_dir, '-U', rootfs_uuid, image_path])
self._run(["mkfs", "-t", rootfs_type, "-f", "--rootdir", build_dir, "-U", rootfs_uuid, image_path])
elif rootfs_type == "xfs":
_allocate_image(self, image_path)
self._run(["mkfs", "-t", rootfs_type, "-m", "uuid=%s" % rootfs_uuid, image_path])
try: # XFS doesn't support importing a directory as a filesystem, it must be mounted
from tempfile import TemporaryDirectory

with TemporaryDirectory() as tmp_dir:
self._run(["mount", image_path, tmp_dir])
self._run(["cp", "-a", f"{build_dir}/.", tmp_dir])
self._run(["umount", tmp_dir])
except RuntimeError as e:
raise RuntimeError("Could not mount the XFS test image: %s", e)
else:
raise Exception("Unsupported test rootfs type: %s" % rootfs_type)

raise NotImplementedError("Unsupported test rootfs type: %s" % rootfs_type)
11 changes: 11 additions & 0 deletions tests/fs/btrfs.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# BTRFS test
modules = [ "ugrd.base.test" ]

out_dir = "initramfs_test"

cpio_compression = false
hostonly = false

[mounts.root]
uuid = "aaaabbbb-cccc-dddd-eeee-ffff00000000"
type = "btrfs"
5 changes: 0 additions & 5 deletions tests/ext4.toml → tests/fs/ext4.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
# EXT4 test
modules = [ "ugrd.base.test" ]

# The initramfs will be built in /tmp/initramfs if "build_dir" is not specified not specified
out_dir = "initramfs_test"

cpio_compression = false

autodetect_root = false

hostonly = false

[mounts.root]
Expand Down
13 changes: 13 additions & 0 deletions tests/fs/xfs.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# XFS test
modules = [ "ugrd.base.test" ]

out_dir = "initramfs_test"

cpio_compression = false
hostonly = false

test_image_size = 300 # XFS requires at least 300MB

[mounts.root]
uuid = "aaaabbbb-cccc-dddd-eeee-ffff00000000"
type = "xfs"
23 changes: 23 additions & 0 deletions tests/test_filesystems.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from unittest import TestCase, main

from ugrd.initramfs_generator import InitramfsGenerator
from zenlib.logging import loggify


@loggify
class TestCpio(TestCase):
def test_ext4(self):
generator = InitramfsGenerator(logger=self.logger, config="tests/fs/ext4.toml")
generator.build()

def test_btrfs(self):
generator = InitramfsGenerator(logger=self.logger, config="tests/fs/btrfs.toml")
generator.build()

def test_xfs(self):
generator = InitramfsGenerator(logger=self.logger, config="tests/fs/xfs.toml")
generator.build()


if __name__ == "__main__":
main()
4 changes: 0 additions & 4 deletions tests/test_ugrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ def test_fullauto(self):
generator = InitramfsGenerator(logger=self.logger, config='tests/fullauto.toml')
generator.build()

def test_ext4(self):
generator = InitramfsGenerator(logger=self.logger, config='tests/ext4.toml')
generator.build()

def test_xz(self):
generator = InitramfsGenerator(logger=self.logger, config='tests/fullauto.toml', cpio_compression='xz')
generator.build()
Expand Down