Skip to content

Commit

Permalink
test for compressed kernel modules
Browse files Browse the repository at this point in the history
improved tempdir handling

Co-authored-by: Jörg Vehlow <github@jv-coder.de>
  • Loading branch information
2 people authored and thir820 committed Feb 12, 2025
1 parent 14fab3a commit b2fe039
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 19 deletions.
37 changes: 20 additions & 17 deletions ebcl/tools/initrd/initrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,19 @@ def find(self, name: str) -> Module | None:
mod_name
)
name = mod_name
return self._modules.get(name, None)
module = self._modules.get(name, None)

if module is None:
mod_names = list(self._modules.keys())
mod_names.sort()
logging.debug('Available modules: %s', mod_names)
logging.debug('Missing module: %s', name)

return module

def __get_or_create(self, mod: str,) -> Module:
modpath = Path(mod)
mod_name = Module.get_module_name(modpath)
logging.debug('Adding module %s (%s)', mod_name, mod)
module = self._modules.get(mod_name, None)
if not module:
module = Module(modpath)
Expand Down Expand Up @@ -167,13 +174,6 @@ def install_busybox(self) -> bool:
self.target_dir, package)
return False

# Linker is expected as /lib/ld-... but installed as /usr/lib/...
self.config.fake.run_sudo(f'mkdir -p {self.target_dir}/lib')
self.config.fake.run_sudo(f'ln -sf /usr/lib/ld-linux-aarch64.so.1 {self.target_dir}/lib/ld-linux-aarch64.so.1')

# Installation of dynamically linked busybox fails if folder doesn't exist.
self.config.fake.run_sudo(f'mkdir -p {self.target_dir}/bin')

self.config.fake.run_chroot(
f'{"/" / busybox_path} --install -s /bin', self.target_dir)

Expand Down Expand Up @@ -315,6 +315,17 @@ def create_initrd(self) -> Optional[str]:
""" Create the initrd image. """
image_path = os.path.join(self.config.output_path, self.name)

# Create necessary directories
for dir_name in ['proc', 'sys', 'dev', 'sysroot', 'var', 'usr/bin',
'tmp', 'run', 'root', 'usr', 'usr/sbin', 'usr/lib', 'etc']:
self.config.fake.run_sudo(
f'mkdir -p {os.path.join(self.target_dir, dir_name)}')

# Create lib and bin folder symlinks
self.config.fake.run_sudo(f'ln -sf usr/lib {self.target_dir}/lib')
self.config.fake.run_sudo(f'ln -sf usr/bin {self.target_dir}/bin')
self.config.fake.run_sudo(f'ln -sf usr/sbin {self.target_dir}/sbin')

logging.info('Installing busybox...')

success = self.install_busybox()
Expand All @@ -323,14 +334,6 @@ def create_initrd(self) -> Optional[str]:

self.download_deb_packages()

# Create necessary directories
for dir_name in ['proc', 'sys', 'dev', 'sysroot', 'var', 'bin',
'tmp', 'run', 'root', 'usr', 'sbin', 'lib', 'etc']:
self.config.fake.run_sudo(
f'mkdir -p {os.path.join(self.target_dir, dir_name)}')
self.config.fake.run_sudo(
f'chown 0:0 {os.path.join(self.target_dir, dir_name)}')

if self.config.base_tarball:
base_tarball = self.config.base_tarball
logging.info('Extracting base tarball %s...', base_tarball)
Expand Down
5 changes: 3 additions & 2 deletions robot_tests/initrd.robot
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ Rootfs should be set up
Directory Should Exist /run
Directory Should Exist /root
Directory Should Exist /usr
Directory Should Exist /sbin
Directory Should Exist /lib
Directory Should Exist /usr/sbin
Directory Should Exist /usr/lib
Directory Should Exist /usr/bin
Directory Should Exist /etc

File dummy.txt should be OK
Expand Down
11 changes: 11 additions & 0 deletions tests/data/initrd_noble.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apt_repos:
- apt_repo: http://ports.ubuntu.com/ubuntu-ports
distro: noble
components:
- main
- universe
modules:
- veth
kernel: linux-image-6.8.0-31-generic
arch: 'arm64'
root_device: /dev/mmcblk0p2
41 changes: 41 additions & 0 deletions tests/test_initrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,47 @@ def test_extract_modules_from_deb(self):
assert os.path.isfile(os.path.join(
self.generator.config.target_dir, 'lib', 'modules', kversion, module))

@pytest.mark.requires_download
def test_extract_compressed_modules_from_deb(self, tmp_path: Path):
""" Test compressed modules. """
test_dir = os.path.dirname(os.path.abspath(__file__))
yaml = os.path.join(test_dir, 'data', 'initrd_noble.yaml')
# Prepare generator
generator = InitrdGenerator(yaml, str(tmp_path))

vd = VersionDepends(
name='linux-modules-6.8.0-31-generic',
package_relation=None,
version_relation=None,
version=None,
arch=generator.config.arch
)
package = generator.config.proxy.find_package(vd)
assert package

pkg = generator.config.proxy.download_package(
generator.config.arch, package)
assert pkg
assert pkg.local_file
assert os.path.isfile(pkg.local_file)

mods_temp = tempfile.mkdtemp()

pkg.extract(mods_temp)

module = 'veth'
generator.config.modules = [module]

kversion = generator.find_kernel_version(mods_temp)
assert kversion

generator.copy_modules(mods_temp)

self.fake.run_sudo(f'rm -rf {mods_temp}', check=False)

assert os.path.isfile(os.path.join(
generator.config.target_dir, 'lib', 'modules', kversion, 'kernel/drivers/net/veth.ko.zst'))

@pytest.mark.requires_download
def test_add_devices(self):
""" Test device node creation. """
Expand Down

0 comments on commit b2fe039

Please sign in to comment.