39 Commits
2.2.1 ... main

Author SHA1 Message Date
8f1ff5c374 Update cmd/mkinitfs/main.go 2025-10-04 20:50:34 +00:00
Clayton Craft
15c95f6b13 Add support for hooks-cleanup
The pmOS initramfs will execute scripts in this dir, so let's add a way
to install them in the archive!
2025-09-12 15:40:01 -07:00
Clayton Craft
bab4be1a89 misc/getfiles: fix zst fallback
The previous symlink recursion fix broke the fallback for zst by
returning early.

Fixes 7a07a16ecb

Part-of: https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/merge_requests/67
[ci:skip-build]: already built successfully in CI
2025-08-04 17:37:25 -07:00
Pablo Correa Gómez
1428f27b4a gitlab-ci: don't run CI-tron on tags
Fixes #49

Part-of: https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/merge_requests/66
[ci:skip-build]: already built successfully in CI
2025-08-04 17:36:01 -07:00
Clayton Craft
f6e4773507 misc/getfiles: add tests for getFile
This adds some tests for getFile, one of which would have caught
the recent recursion issue and other will hopefully catch future
regressions.

Part-of: https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/merge_requests/65

[ci:skip-build]: already built successfully in CI
2025-08-04 09:23:05 -07:00
Clayton Craft
7a07a16ecb misc/getfiles: fix infinite recursion loop when given a symlink
This fixes an infinite recursion loop in getFile caused by:

1) `os.Stat(file)` resolves a symlink so that `fileInfo.isDir()` returns True
2) `filepath.Walk()` starts iterating on the root directory (in this case the symlink)
3) `filepath.Walk()` uses `os.Lstat` internally, which does NOT dereference the symlink
4) in the walk func, `f.isDir()` returns False, and the walk func calls `getFile()` on it
5) goto 1

fixes #47

Part-of: https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/merge_requests/65
2025-08-04 09:23:00 -07:00
Clayton Craft
4f6af31a7a archive: don't create a symlink for /usr/sbin
Fixes this warning when running on a merged /usr system:

   addSymlink: failed to get symlink target for: /usr/sbin

Part-of: https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/merge_requests/64
[ci:skip-build]: already built successfully in CI
2025-08-03 22:44:35 -07:00
Pablo Correa Gómez
39ee6752fd ci: build aarch64 mkinitfs to use in ci-tron testing
Else mkinitfs fails:
https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/jobs/1395713
to run x86_64 binary on aarch64

Part-of: https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/merge_requests/63
[ci:skip-build]: already built successfully in CI
2025-07-25 12:40:23 +02:00
Martin Roukala (né Peres)
0edee0afbd ci: test the generated artifacts in qemu runners
Part-of: https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/merge_requests/63
2025-07-25 12:09:01 +02:00
Pablo Correa Gómez
95edf678f4 ci: upload test report to CI
To make it possible, tweak a bit the Makefile

Part-of: https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/merge_requests/63
2025-06-16 18:31:13 +02:00
Pablo Correa Gómez
be6a6da417 ci: migrate from deprecated "only" keyword to "rules"
In the process, removed the unused "lint" stage and a massive comment
that is no longer useful. The rules should be self-explanatory

Part-of: https://gitlab.postmarketos.org/postmarketOS/postmarketos-mkinitfs/-/merge_requests/63
2025-06-06 16:40:15 +02:00
Clayton Craft
4e771ab96f osutil: mergeusr: don't rewrite /usr/sbin to /usr/bin (MR 58)
Alpine TSC decided to keep /usr/sbin for now, so let's not break paths
to stuff under /usr/sbin in the meantime.
2024-11-13 09:21:59 -08:00
Clayton Craft
4d7dd79bcf deviceinfo: test that inline-comments work (MR 57)
see: https://gitlab.postmarketos.org/postmarketOS/pmaports/-/merge_requests/5766
2024-11-13 00:49:34 -08:00
Clayton Craft
d63e600614 add compile-time flag to disable Go GC (MR 56)
I hate this, but it's the only good way I could find to allow working around this ugly QEMU bug:
https://gitlab.com/qemu-project/qemu/-/issues/2560
2024-09-28 08:32:53 -07:00
Clayton Craft
741c0553d5 Allow including initramfs-extra files in the initramfs (MR 48)
This uses a "deviceinfo_create_initfs_extra" to allow including
initramfs-extra files in the initramfs and skip creating a separate
initramfs-extra archive when it's set to "false".
If this variable is unset, mkinitfs uses a default value of "false".
2024-09-27 12:13:24 -07:00
Clayton Craft
cd97df108a filelist: trim whitespace from lines read from files (MR 55)
Fixes issues with leading/trailing whitespaces really messing with mkinitfs
2024-07-11 14:18:55 -07:00
Arnav Singh
1fed057a82 doc: fix spelling typo 2024-06-25 11:26:10 -07:00
Caleb Connolly
5efdb9f170 archive: add /usr/sbin symlinks for UsrMerge (MR 53)
[ci:skip-build]: already built successfully in CI
2024-06-18 23:59:34 +02:00
Clayton Craft
81de8b438d archive,hookfiles: convert paths to usr-merge when necessary (MR 39)
Co-authored-by: Caleb Connolly <caleb@postmarketos.org>
2024-05-29 15:29:41 -07:00
Caleb Connolly
af9a0f0ca5 archive: create symlinks for /bin, /sbin, /lib (MR 39) 2024-05-29 15:29:35 -07:00
Clayton Craft
014563fdbc osutil: add function to detect merged /usr systems (MR 39)
This is very crude, but in lieu of an actual spec for usr merge or some
standard way to detect whether it's done, this will have to do for now.
We can and should improve it later!
2024-05-29 15:29:35 -07:00
Caleb Connolly
83282187c2 archive: split out and rework symlink handling (MR 39)
There were some subtle bugs when handling symlinks to directories, and
other usrmerge bits. Rework the symlink handling to deal with this
properly and produce a sane ramdisk on usrmerge systems.

Signed-off-by: Caleb Connolly <caleb@connolly.tech>
Co-authored-by: Clayton Craft <clayton@craftyguy.net>
2024-05-29 15:29:35 -07:00
Caleb Connolly
eda4f3ba22 filelist/modules: try /usr/lib/modules (MR 39)
This is the path used on usrmerge distros, try it first as /lib/modules
will implicitly follow the /lib symlink.

Signed-off-by: Caleb Connolly <caleb@connolly.tech>
2024-05-29 15:29:35 -07:00
Caleb Connolly
866d37b85d archive: improve error messages (MR 39)
Improve errors when writing out the cpio archive fails.

Signed-off-by: Caleb Connolly <caleb@connolly.tech>
2024-05-29 15:29:35 -07:00
Clayton Craft
1334fdfa26 deviceinfo: replace implementation with mvdan/sh (MR 52)
This library has a convenient "source file" method designed for sourcing
shell envs and returning values set in them. deviceinfo's syntax every
where else seems to be "whatever sh can 'source'", so using this library
seems a lot nicer than trying to implement a parser/interpreter here
(and almost certainly missing corner cases, functionality, etc)

[ci:skip-build]: already built successfully in CI
2024-05-15 16:11:17 -07:00
Clayton Craft
56db822b88 deviceinfo: implement stringer (MR 52) 2024-05-15 16:11:17 -07:00
Clayton Craft
631d6078c2 misc/getfiles: add systemd lib dir to search paths (MR 51)
fixes #41
2024-05-15 16:07:43 -07:00
Clayton Craft
e5f14d70a6 doc: add archive compression formats/levels (MR 50)
fixes #28
2024-03-20 10:08:18 -07:00
Stefan Hansson
dd5cdeace5 misc: Also check for .zst-compressed variants of files (MR 49)
Initially I thought of breaking off the Stat + error check call into its
own function as to reduce repetition, but given that it's only useful in
this situation where it only happens twice anyway, I'm not sure it
actually would reduce complexity.

Additionally, this means that .zst-compressed variants of files will be
searched for in all contexts where this function is used. I'm not sure
this is desirable.

Tested and works with arrow-db820c. I didn't test it on actual hardware,
but I verified that the firmware ended up in the initramfs via
$ pmbootstrap initfs ls. I choose this device because it uses firmware
from linux-firmware and also needs said firmware present in the
initramfs.

Closes https://gitlab.com/postmarketOS/postmarketos-mkinitfs/-/issues/39
2024-03-14 12:01:48 -07:00
Clayton Craft
1a99953aa2 bootdeploy: fallback to vmlinuz* kernels when zboot is set (MR 47)
Some kernel packages (e.g. linux-lts in Alpine) don't ship linux.efi, so
this needs to fallback to "vmlinuz" or else it won't be able to find a
kernel for boot-deploy.
2024-01-29 15:32:25 -08:00
Clayton Craft
e2f4e6254f modules: fix issue with some module extensions being ignored in dirs (MR 46)
There are several valid extensions that kernel modules can have, and the
list I had here was not complete... this meant that mkinitfs would fail
to include modules with extensions like ".ko.gz" when searching
directories.

This makes the check for a "valid" module file name a lot simpler,
allowing any file with ".ko" in the file name. While it's possible for a
non-module file to have ".ko" somewhere in the file name, it seems
unlikely if it's in the kernel modules directory... and this is an OK
compromise for now.
2024-01-29 10:16:28 -08:00
Clayton Craft
2efeb4510d doc: add new deviceinfo var for sd-boot (MR 44) 2024-01-26 11:12:12 -08:00
Caleb Connolly
f0b3c1d992 bootdeploy: support zboot kernel image (MR 44)
As we move towards UEFI on more devices, we want to use systemd-boot and
kernel images built with CONFIG_ZBOOT. However, these images aren't
compatible with existing Android bootloaders. As a result, we must
install both the old vmlinuz image for old bootloaders, and the fancy
new self-extracting EFI image.

When using systemd_boot, use linux.efi as the kernel file name instead
of globbing on vmlinuz*.

Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
2024-01-26 11:12:11 -08:00
Caleb Connolly
98bdb23f01 deviceinfo: parse GenerateSystemdBoot option (MR 44)
This will be used to adjust behaviour when using systemd boot

Signed-off-by: Caleb Connolly <caleb.connolly@linaro.org>
2024-01-26 11:06:37 -08:00
Clayton Craft
6618e564ad doc: drop unused deviceinfo_mesa_driver (MR 45)
Yay, one less deviceinfo var!
2024-01-26 11:05:23 -08:00
Clayton Craft
6df75d5682 deviceinfo: clean up unused MesaDriver (MR 45)
This deviceinfo var is no longer used in mkinitfs (see previous
commits).
2024-01-26 11:05:17 -08:00
Clayton Craft
9475572811 filelist/osksdl: drop module / support for osk-sdl (MR 42)
This removes code specific to installing osk-sdl in the initramfs
archive. osk-sdl has been deprecated / removed from postmarketOS for FDE
unlocking (in favor of using the app "unl0kr"), and other distros should
consider doing the same.
2023-11-24 18:13:26 -08:00
Pablo Correa Gómez
2b467eb77f Do not support loading modules from "deviceinfo_modules_initfs" (MR 38)
This variable will dissappear from the deviceinfo at some point:
https://gitlab.com/postmarketOS/pmaports/-/merge_requests/4169
so after it does, it would not make much sense to keep its use around
2023-11-24 18:01:24 -08:00
Clayton Craft
d77e1cd11d filelist/osk-sdl: add deprecation warning
Co-authored-by: Oliver Smith <ollieparanoid@postmarketos.org>
2023-10-12 23:26:16 -07:00
22 changed files with 727 additions and 412 deletions

View File

@@ -6,43 +6,126 @@ image: alpine:edge
variables:
GOFLAGS: "-buildvcs=false"
PACKAGE_REGISTRY_URL: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/mkinitfs-vendor-${CI_COMMIT_TAG}/${CI_COMMIT_TAG}"
CI_TRON_TEMPLATE_PROJECT: &ci-tron-template-project postmarketOS/ci-common
CI_TRON_JOB_TEMPLATE_PROJECT_URL: $CI_SERVER_URL/$CI_TRON_TEMPLATE_PROJECT
CI_TRON_JOB_TEMPLATE_COMMIT: &ci-tron-template-commit 7c95b5f2d53533e8722abf57c73e558168e811f3
include:
- project: *ci-tron-template-project
ref: *ci-tron-template-commit
file: '/ci-tron/common.yml'
stages:
- lint
- build
- hardware tests
- vendor
- release
# defaults for "only"
# We need to run the CI jobs in a "merge request specific context", if CI is
# running in a merge request. Otherwise the environment variable that holds the
# merge request ID is not available. This means, we must set the "only"
# variable accordingly - and if we only do it for one job, all other jobs will
# not get executed. So have the defaults here, and use them in all jobs that
# should run on both the master branch, and in merge requests.
# https://docs.gitlab.com/ee/ci/merge_request_pipelines/index.html#excluding-certain-jobs
.only-default: &only-default
only:
- master
- merge_requests
- tags
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == 'master'
- if: '$CI_COMMIT_TAG != null'
build:
stage: build
<<: *only-default
variables:
GOTEST: "gotestsum --junitfile report.xml --format testname -- ./..."
parallel:
matrix:
- TAG: shared
- TAG: arm64
tags:
- $TAG
before_script:
- apk -q add go staticcheck make scdoc
- apk -q add go gotestsum staticcheck make scdoc
script:
- make test
- make
after_script:
- mkdir -p rootfs/usr/sbin
- cp mkinitfs rootfs/usr/sbin
artifacts:
expire_in: 1 week
reports:
junit: report.xml
paths:
- rootfs
.qemu-common:
variables:
DEVICE_NAME: qemu-$CPU_ARCH
KERNEL_VARIANT: lts
rules:
- if: '$CI_COMMIT_TAG != null'
when: never
.build-ci-tron-qemu:
stage: hardware tests
extends:
- .pmos-ci-tron-build-boot-artifacts
- .qemu-common
variables:
INSTALL_PACKAGES: device-${DEVICE_NAME} device-${DEVICE_NAME}-kernel-${KERNEL_VARIANT} postmarketos-mkinitfs-hook-ci
build-ci-tron-qemu-amd64:
extends:
- .build-ci-tron-qemu
needs:
- job: "build"
parallel:
matrix:
- TAG: shared
variables:
CPU_ARCH: amd64
build-ci-tron-qemu-aarch64:
extends:
- .build-ci-tron-qemu
needs:
- job: "build"
parallel:
matrix:
- TAG: arm64
variables:
CPU_ARCH: aarch64
.test-ci-tron-qemu:
stage: hardware tests
extends:
- .pmos-ci-tron-initramfs-test
- .qemu-common
dependencies: []
variables:
CI_TRON_KERNEL__URL: "glartifact://build-ci-tron-qemu-$CPU_ARCH/${CI_TRON__PMB_EXPORT_PATH}/vmlinuz-${KERNEL_VARIANT}"
CI_TRON_INITRAMFS__INITRAMFS__URL: "glartifact://build-ci-tron-qemu-$CPU_ARCH/${CI_TRON__PMB_EXPORT_PATH}/initramfs"
CI_TRON_KERNEL_CMDLINE__DEVICEINFO: 'console=tty1 console=ttyS0,115200 PMOS_FORCE_PARTITION_RESIZE'
test-ci-tron-qemu-amd64:
extends:
- .test-ci-tron-qemu
- .pmos-ci-tron-runner-qemu-amd64
needs:
- job: 'build-ci-tron-qemu-amd64'
artifacts: false
variables:
CPU_ARCH: amd64
test-ci-tron-qemu-aarch64:
extends:
- .test-ci-tron-qemu
- .pmos-ci-tron-runner-qemu-aarch64
needs:
- job: 'build-ci-tron-qemu-aarch64'
artifacts: false
variables:
CPU_ARCH: aarch64
vendor:
stage: vendor
image: alpine:latest
only:
- tags
rules:
- if: '$CI_COMMIT_TAG != null'
before_script:
- apk -q add curl go make
script:
@@ -54,8 +137,8 @@ vendor:
release:
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
only:
- tags
rules:
- if: '$CI_COMMIT_TAG != null'
script:
- |
release-cli create --name "Release $CI_COMMIT_TAG" --tag-name $CI_COMMIT_TAG \

View File

@@ -12,7 +12,13 @@ GO?=go
GOFLAGS?=
LDFLAGS+=-s -w -X main.Version=$(VERSION)
RM?=rm -f
GOTEST=go test -count=1 -race
GOTESTOPTS?=-count=1 -race
GOTEST?=go test ./...
DISABLE_GOGC?=
ifeq ($(DISABLE_GOGC),1)
LDFLAGS+=-X main.DisableGC=true
endif
GOSRC!=find * -name '*.go'
GOSRC+=go.mod go.sum
@@ -42,10 +48,10 @@ test:
fi
@staticcheck ./...
@$(GOTEST) ./...
$(GOTEST) $(GOTESTOPTS)
clean:
$(RM) mkinitfs $(DOCS)
$(RM) mkinitfs $(DOCS)
$(RM) $(VENDORED)*
install: $(DOCS) mkinitfs

View File

@@ -9,6 +9,7 @@ import (
"log"
"os"
"path/filepath"
"runtime/debug"
"strings"
"time"
@@ -20,7 +21,6 @@ import (
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist/hookscripts"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist/initramfs"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist/modules"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist/osksdl"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/osutil"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/pkgs/deviceinfo"
@@ -28,8 +28,14 @@ import (
// set at build time
var Version string
var DisableGC string
func main() {
// To allow working around silly GC-related issues, like https://gitlab.com/qemu-project/qemu/-/issues/2560
if strings.ToLower(DisableGC) == "true" {
debug.SetGCPercent(-1)
}
retCode := 0
defer func() { os.Exit(retCode) }()
@@ -105,15 +111,38 @@ func main() {
hookfiles.New("/etc/mkinitfs/files"),
hookscripts.New("/usr/share/mkinitfs/hooks", "/hooks"),
hookscripts.New("/etc/mkinitfs/hooks", "/hooks"),
modules.New(strings.Fields(devinfo.ModulesInitfs), "/usr/share/mkinitfs/modules"),
modules.New([]string{}, "/etc/mkinitfs/modules"),
hookscripts.New("/usr/share/mkinitfs/hooks-cleanup", "/hooks-cleanup"),
hookscripts.New("/etc/mkinitfs/hooks-cleanup", "/hooks-cleanup"),
//modules.New("/usr/share/mkinitfs/modules"),
modules.New("/etc/mkinitfs/modules"),
})
initfsExtra := initramfs.New([]filelist.FileLister{
hookfiles.New("/usr/share/mkinitfs/files-extra"),
hookfiles.New("/etc/mkinitfs/files-extra"),
hookscripts.New("/usr/share/mkinitfs/hooks-extra", "/hooks-extra"),
hookscripts.New("/etc/mkinitfs/hooks-extra", "/hooks-extra"),
modules.New("/usr/share/mkinitfs/modules-extra"),
modules.New("/etc/mkinitfs/modules-extra"),
})
if err := initramfsAr.AddItems(initfs); err != nil {
log.Println(err)
log.Println("failed to generate: ", "initramfs")
retCode = 1
return
}
// Include initramfs-extra files in the initramfs if not making a separate
// archive
if !devinfo.CreateInitfsExtra {
if err := initramfsAr.AddItems(initfsExtra); err != nil {
log.Println(err)
log.Println("failed to generate: ", "initramfs")
retCode = 1
return
}
}
if err := initramfsAr.Write(filepath.Join(workDir, "initramfs"), os.FileMode(0644)); err != nil {
log.Println(err)
log.Println("failed to generate: ", "initramfs")
@@ -122,42 +151,35 @@ func main() {
}
misc.TimeFunc(start, "initramfs")
//
// initramfs-extra
//
// deviceinfo.InitfsExtraCompression needs a little more post-processing
compressionFormat, compressionLevel = archive.ExtractFormatLevel(devinfo.InitfsExtraCompression)
log.Printf("== Generating %s ==\n", "initramfs-extra")
log.Printf("- Using compression format %s with level %q\n", compressionFormat, compressionLevel)
if devinfo.CreateInitfsExtra {
//
// initramfs-extra
//
// deviceinfo.InitfsExtraCompression needs a little more post-processing
compressionFormat, compressionLevel = archive.ExtractFormatLevel(devinfo.InitfsExtraCompression)
log.Printf("== Generating %s ==\n", "initramfs-extra")
log.Printf("- Using compression format %s with level %q\n", compressionFormat, compressionLevel)
start = time.Now()
initramfsExtraAr := archive.New(compressionFormat, compressionLevel)
initfsExtra := initramfs.New([]filelist.FileLister{
hookfiles.New("/usr/share/mkinitfs/files-extra"),
hookfiles.New("/etc/mkinitfs/files-extra"),
hookscripts.New("/usr/share/mkinitfs/hooks-extra", "/hooks-extra"),
hookscripts.New("/etc/mkinitfs/hooks-extra", "/hooks-extra"),
modules.New([]string{}, "/usr/share/mkinitfs/modules-extra"),
modules.New([]string{}, "/etc/mkinitfs/modules-extra"),
osksdl.New(devinfo.MesaDriver),
})
if err := initramfsExtraAr.AddItemsExclude(initfsExtra, initfs); err != nil {
log.Println(err)
log.Println("failed to generate: ", "initramfs-extra")
retCode = 1
return
start = time.Now()
initramfsExtraAr := archive.New(compressionFormat, compressionLevel)
if err := initramfsExtraAr.AddItemsExclude(initfsExtra, initfs); err != nil {
log.Println(err)
log.Println("failed to generate: ", "initramfs-extra")
retCode = 1
return
}
if err := initramfsExtraAr.Write(filepath.Join(workDir, "initramfs-extra"), os.FileMode(0644)); err != nil {
log.Println(err)
log.Println("failed to generate: ", "initramfs-extra")
retCode = 1
return
}
misc.TimeFunc(start, "initramfs-extra")
}
if err := initramfsExtraAr.Write(filepath.Join(workDir, "initramfs-extra"), os.FileMode(0644)); err != nil {
log.Println(err)
log.Println("failed to generate: ", "initramfs-extra")
retCode = 1
return
}
misc.TimeFunc(start, "initramfs-extra")
// Final processing of initramfs / kernel is done by boot-deploy
if !disableBootDeploy {
if err := bootDeploy(workDir, *outDir, devinfo.UbootBoardname); err != nil {
if err := bootDeploy(workDir, *outDir, devinfo); err != nil {
log.Println(err)
log.Println("boot-deploy failed")
retCode = 1
@@ -166,10 +188,10 @@ func main() {
}
}
func bootDeploy(workDir, outDir, ubootBoardname string) error {
func bootDeploy(workDir string, outDir string, devinfo deviceinfo.DeviceInfo) error {
log.Print("== Using boot-deploy to finalize/install files ==")
defer misc.TimeFunc(time.Now(), "boot-deploy")
bd := bootdeploy.New(workDir, outDir, ubootBoardname)
bd := bootdeploy.New(workDir, outDir, devinfo)
return bd.Run()
}

View File

@@ -42,10 +42,10 @@ mkinitfs reads deviceinfo values from */usr/share/deviceinfo/deviceinfo* and
*/etc/deviceinfo*, in that order. The following variables
are *required* by mkinitfs:
- deviceinfo_create_initfs_extra
- deviceinfo_generate_systemd_boot
- deviceinfo_initfs_compression
- deviceinfo_initfs_extra_compression
- deviceinfo_mesa_driver
- deviceinfo_modules_initfs
- deviceinfo_uboot_boardname
It is a design goal to keep the number of required variables from deviceinfo to
@@ -55,6 +55,36 @@ a bare minimum, and to require only variables that don't hold lists of things.
necessary tools to extract the configured archive format are in the initramfs
archive.
# ARCHIVE COMPRESSION
Archive compression parameters are specified in the
*deviceinfo_initfs_compression* and *deviceinfo_initfs_extra_compression*
deviceinfo variables. Their values do not have to match, but special
consideration should be taken since some formats may require additional kernel
options or tools in the initramfs to support it.
Supported compression *formats* for mkinitfs are:
- gzip
- lz4
- lzma
- none
- zstd
Supported compression *levels* for mkinitfs:
- best
- default
- fast
The value of these variables follows this syntax: *<format>:<level>*. For
example, *zstd* with the *fast* compression level would be:
*deviceinfo_initfs_compression="zstd:fast"*
Defaults to *gzip* and *default* for both archives if format and/or level is
unsupported or omitted.
# DIRECTORIES
The following directories are used by mkinitfs to generate the initramfs and
@@ -104,7 +134,8 @@ create/manage. mkinitfs reads configuration from */usr/share/mkinitfs* first, an
skipped.
## /usr/share/mkinitfs/hooks, /etc/mkinitfs/hooks
## /usr/share/mkinitfs/hooks-extra*, /etc/mkinitfs/hooks-extra
## /usr/share/mkinitfs/hooks-cleanup, /etc/mkinitfs/hooks-cleanup
## /usr/share/mkinitfs/hooks-extra, /etc/mkinitfs/hooks-extra
Any files listed under these directories are copied as-is into the
relevant archives. Hooks are generally script files, but how they are
@@ -117,7 +148,7 @@ create/manage. mkinitfs reads configuration from */usr/share/mkinitfs* first, an
## /usr/share/mkinitfs/modules, /etc/mkinitfs/modules
## /usr/share/mkinitfs/modules-extra, /etc/mkinitfs/modules-extra
Files with the *.modules* extention in these directories are lists of
Files with the *.modules* extension in these directories are lists of
kernel modules to include in the initramfs. Individual modules and
directories can be listed in the files here. Globbing is also supported.

10
go.mod
View File

@@ -7,5 +7,13 @@ require (
github.com/klauspost/compress v1.15.12
github.com/pierrec/lz4/v4 v4.1.17
github.com/ulikunitz/xz v0.5.10
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
golang.org/x/sys v0.18.0
)
require (
github.com/mvdan/sh v2.6.4+incompatible // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/term v0.18.0 // indirect
mvdan.cc/sh v2.6.4+incompatible // indirect
)

12
go.sum
View File

@@ -2,9 +2,21 @@ github.com/cavaliercoder/go-cpio v0.0.0-20180626203310-925f9528c45e h1:hHg27A0RS
github.com/cavaliercoder/go-cpio v0.0.0-20180626203310-925f9528c45e/go.mod h1:oDpT4efm8tSYHXV5tHSdRvBet/b/QzxZ+XyyPehvm3A=
github.com/klauspost/compress v1.15.12 h1:YClS/PImqYbn+UILDnqxQCZ3RehC9N318SU3kElDUEM=
github.com/klauspost/compress v1.15.12/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/mvdan/sh v2.6.4+incompatible h1:D4oEWW0J8cL7zeQkrXw76IAYXF0mJfDaBwjgzmKb6zs=
github.com/mvdan/sh v2.6.4+incompatible/go.mod h1:kipHzrJQZEDCMTNRVRAlMMFjqHEYrthfIlFkJSrmDZE=
github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc=
github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
mvdan.cc/sh v2.6.4+incompatible h1:eD6tDeh0pw+/TOTI1BBEryZ02rD2nMcFsgcvde7jffM=
mvdan.cc/sh v2.6.4+incompatible/go.mod h1:IeeQbZq+x2SUGBensq/jge5lLQbS3XT2ktyp3wrt4x8=

View File

@@ -237,7 +237,10 @@ func (archive *Archive) AddItemsExclude(flister filelist.FileLister, exclude fil
// Adds the given file or directory at "source" to the archive at "dest"
func (archive *Archive) AddItem(source string, dest string) error {
if osutil.HasMergedUsr() {
source = osutil.MergeUsr(source)
dest = osutil.MergeUsr(dest)
}
sourceStat, err := os.Lstat(source)
if err != nil {
e, ok := err.(*os.PathError)
@@ -248,6 +251,12 @@ func (archive *Archive) AddItem(source string, dest string) error {
return fmt.Errorf("AddItem: failed to get stat for %q: %w", source, err)
}
// A symlink to a directory doesn't have the os.ModeDir bit set, so we need
// to check if it's a symlink first
if sourceStat.Mode()&os.ModeSymlink != 0 {
return archive.addSymlink(source, dest)
}
if sourceStat.Mode()&os.ModeDir != 0 {
return archive.addDir(dest)
}
@@ -255,6 +264,45 @@ func (archive *Archive) AddItem(source string, dest string) error {
return archive.addFile(source, dest)
}
func (archive *Archive) addSymlink(source string, dest string) error {
target, err := os.Readlink(source)
if err != nil {
log.Print("addSymlink: failed to get symlink target for: ", source)
return err
}
// Make sure we pick up the symlink target too
targetAbs := target
if filepath.Dir(target) == "." {
// relative symlink, make it absolute so we can add the target to the archive
targetAbs = filepath.Join(filepath.Dir(source), target)
}
if !filepath.IsAbs(targetAbs) {
targetAbs, err = osutil.RelativeSymlinkTargetToDir(targetAbs, filepath.Dir(source))
if err != nil {
return err
}
}
archive.AddItem(targetAbs, targetAbs)
// Now add the symlink itself
destFilename := strings.TrimPrefix(dest, "/")
archive.items.add(archiveItem{
sourcePath: source,
header: &cpio.Header{
Name: destFilename,
Linkname: target,
Mode: 0644 | cpio.ModeSymlink,
Size: int64(len(target)),
},
})
return nil
}
func (archive *Archive) addFile(source string, dest string) error {
if err := archive.addDir(filepath.Dir(dest)); err != nil {
return err
@@ -266,42 +314,6 @@ func (archive *Archive) addFile(source string, dest string) error {
return err
}
// Symlink: write symlink to archive then set 'file' to link target
if sourceStat.Mode()&os.ModeSymlink != 0 {
// log.Printf("File %q is a symlink", file)
target, err := os.Readlink(source)
if err != nil {
log.Print("addFile: failed to get symlink target: ", source)
return err
}
destFilename := strings.TrimPrefix(dest, "/")
archive.items.add(archiveItem{
sourcePath: source,
header: &cpio.Header{
Name: destFilename,
Linkname: target,
Mode: 0644 | cpio.ModeSymlink,
Size: int64(len(target)),
// Checksum: 1,
},
})
if filepath.Dir(target) == "." {
target = filepath.Join(filepath.Dir(source), target)
}
// make sure target is an absolute path
if !filepath.IsAbs(target) {
target, err = osutil.RelativeSymlinkTargetToDir(target, filepath.Dir(source))
if err != nil {
return err
}
}
err = archive.addFile(target, target)
return err
}
destFilename := strings.TrimPrefix(dest, "/")
archive.items.add(archiveItem{
@@ -404,6 +416,12 @@ func (archive *Archive) writeCompressed(path string, mode os.FileMode) (err erro
}
func (archive *Archive) writeCpio() error {
// Just in case
if osutil.HasMergedUsr() {
archive.addSymlink("/bin", "/bin")
archive.addSymlink("/sbin", "/sbin")
archive.addSymlink("/lib", "/lib")
}
// having a transient function for actually adding files to the archive
// allows the deferred fd.close to run after every copy and prevent having
// tons of open file handles until the copying is all done
@@ -418,19 +436,19 @@ func (archive *Archive) writeCpio() error {
if header.Mode.IsRegular() {
fd, err := os.Open(source)
if err != nil {
return fmt.Errorf("archive.writeCpio: uname to open file %q, %w", source, err)
return fmt.Errorf("archive.writeCpio: Unable to open file %q, %w", source, err)
}
defer fd.Close()
if _, err := io.Copy(archive.cpioWriter, fd); err != nil {
return fmt.Errorf("archive.writeCpio: unable to write out archive: %w", err)
return fmt.Errorf("archive.writeCpio: Couldn't process %q: %w", source, err)
}
} else if header.Linkname != "" {
// the contents of a symlink is just need the link name
if _, err := archive.cpioWriter.Write([]byte(header.Linkname)); err != nil {
return fmt.Errorf("archive.writeCpio: unable to write out symlink: %w", err)
return fmt.Errorf("archive.writeCpio: unable to write out symlink: %q -> %q: %w", source, header.Linkname, err)
}
} else {
return fmt.Errorf("archive.writeCpio: unknown type for file: %s", source)
return fmt.Errorf("archive.writeCpio: unknown type for file: %q: %d", source, header.Mode)
}
}

View File

@@ -10,32 +10,32 @@ import (
"path"
"path/filepath"
"strings"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/pkgs/deviceinfo"
)
type BootDeploy struct {
inDir string
outDir string
ubootBoardname string
inDir string
outDir string
devinfo deviceinfo.DeviceInfo
}
// New returns a new BootDeploy, which then runs:
//
// boot-deploy -d indir -o outDir
//
// ubootBoardname is used for copying in some u-boot files prior to running
// boot-deploy. This is optional, passing an empty string is ok if this is not
// needed.
func New(inDir, outDir, ubootBoardname string) *BootDeploy {
// devinfo is used to access some deviceinfo values, such as UbootBoardname
// and GenerateSystemdBoot
func New(inDir string, outDir string, devinfo deviceinfo.DeviceInfo) *BootDeploy {
return &BootDeploy{
inDir: inDir,
outDir: outDir,
ubootBoardname: ubootBoardname,
inDir: inDir,
outDir: outDir,
devinfo: devinfo,
}
}
func (b *BootDeploy) Run() error {
if err := copyUbootFiles(b.inDir, b.ubootBoardname); errors.Is(err, os.ErrNotExist) {
if err := copyUbootFiles(b.inDir, b.devinfo.UbootBoardname); errors.Is(err, os.ErrNotExist) {
log.Println("u-boot files copying skipped: ", err)
} else {
if err != nil {
@@ -43,15 +43,9 @@ func (b *BootDeploy) Run() error {
}
}
return bootDeploy(b.inDir, b.outDir)
}
func bootDeploy(workDir string, outDir string) error {
// boot-deploy expects the kernel to be in the same dir as initramfs.
// Assume that the kernel is in the output dir...
kernels, _ := filepath.Glob(filepath.Join(outDir, "vmlinuz*"))
if len(kernels) == 0 {
return errors.New("Unable to find any kernels at " + filepath.Join(outDir, "vmlinuz*"))
kernels, err := getKernelPath(b.outDir, b.devinfo.GenerateSystemdBoot == "true")
if err != nil {
return err
}
// Pick a kernel that does not have suffixes added by boot-deploy
@@ -71,7 +65,7 @@ func bootDeploy(workDir string, outDir string) error {
defer kernFd.Close()
kernFilename := path.Base(kernFile)
kernFileCopy, err := os.Create(filepath.Join(workDir, kernFilename))
kernFileCopy, err := os.Create(filepath.Join(b.inDir, kernFilename))
if err != nil {
return err
}
@@ -84,12 +78,17 @@ func bootDeploy(workDir string, outDir string) error {
}
// boot-deploy -i initramfs -k vmlinuz-postmarketos-rockchip -d /tmp/cpio -o /tmp/foo initramfs-extra
cmd := exec.Command("boot-deploy",
args := []string{
"-i", "initramfs",
"-k", kernFilename,
"-d", workDir,
"-o", outDir,
"initramfs-extra")
"-d", b.inDir,
"-o", b.outDir,
}
if b.devinfo.CreateInitfsExtra {
args = append(args, "initramfs-extra")
}
cmd := exec.Command("boot-deploy", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -100,6 +99,25 @@ func bootDeploy(workDir string, outDir string) error {
return nil
}
func getKernelPath(outDir string, zboot bool) ([]string, error) {
var kernels []string
if zboot {
kernels, _ = filepath.Glob(filepath.Join(outDir, "linux.efi"))
if len(kernels) > 0 {
return kernels, nil
}
// else fallback to vmlinuz* below
}
kernFile := "vmlinuz*"
kernels, _ = filepath.Glob(filepath.Join(outDir, kernFile))
if len(kernels) == 0 {
return nil, errors.New("Unable to find any kernels at " + filepath.Join(outDir, kernFile))
}
return kernels, nil
}
// Copy copies the file at srcFile path to a new file at dstFile path
func copy(srcFile, dstFile string) error {
out, err := os.Create(dstFile)

View File

@@ -44,7 +44,7 @@ func (h *HookDirs) List() (*filelist.FileList, error) {
s := bufio.NewScanner(f)
for s.Scan() {
dir := s.Text()
dir := strings.TrimSpace(s.Text())
if len(dir) == 0 || strings.HasPrefix(dir, "#") {
continue
}

View File

@@ -11,6 +11,7 @@ import (
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/osutil"
)
type HookFiles struct {
@@ -58,12 +59,15 @@ func slurpFiles(fd io.Reader) (*filelist.FileList, error) {
s := bufio.NewScanner(fd)
for s.Scan() {
line := s.Text()
line := strings.TrimSpace(s.Text())
if len(line) == 0 || strings.HasPrefix(line, "#") {
continue
}
src, dest, has_dest := strings.Cut(line, ":")
if osutil.HasMergedUsr() {
src = osutil.MergeUsr(src)
}
fFiles, err := misc.GetFiles([]string{src}, true)
if err != nil {

View File

@@ -17,14 +17,11 @@ import (
type Modules struct {
modulesListPath string
modulesList []string
}
// New returns a new Modules that will use the given moduleto provide a list
// of script files.
func New(modulesList []string, modulesListPath string) *Modules {
// New returns a new Modules that will read in lists of kernel modules in the given path.
func New(modulesListPath string) *Modules {
return &Modules{
modulesList: modulesList,
modulesListPath: modulesListPath,
}
}
@@ -36,8 +33,14 @@ func (m *Modules) List() (*filelist.FileList, error) {
}
files := filelist.NewFileList()
libDir := "/usr/lib/modules"
if exists, err := misc.Exists(libDir); !exists {
libDir = "/lib/modules"
} else if err != nil {
return nil, fmt.Errorf("received unexpected error when getting status for %q: %w", libDir, err)
}
modDir := filepath.Join("/lib/modules", kernVer)
modDir := filepath.Join(libDir, kernVer)
if exists, err := misc.Exists(modDir); !exists {
// dir /lib/modules/<kernel> if kernel built without module support, so just print a message
log.Printf("-- kernel module directory not found: %q, not including modules", modDir)
@@ -52,20 +55,6 @@ func (m *Modules) List() (*filelist.FileList, error) {
files.Add(file, file)
}
// slurp up given list of modules
if len(m.modulesList) > 0 {
log.Printf("-- Including kernel modules from deviceinfo")
for _, module := range m.modulesList {
if modFilelist, err := getModule(module, modDir); err != nil {
return nil, fmt.Errorf("unable to get modules from deviceinfo: %w", err)
} else {
for _, file := range modFilelist {
files.Add(file, file)
}
}
}
}
// slurp up modules from lists in modulesListPath
log.Printf("- Searching for kernel modules from %s", m.modulesListPath)
fileInfo, err := os.ReadDir(m.modulesListPath)
@@ -94,7 +83,7 @@ func slurpModules(fd io.Reader, modDir string) (*filelist.FileList, error) {
files := filelist.NewFileList()
s := bufio.NewScanner(fd)
for s.Scan() {
line := s.Text()
line := strings.TrimSpace(s.Text())
if len(line) == 0 || strings.HasPrefix(line, "#") {
continue
}
@@ -114,8 +103,8 @@ func slurpModules(fd io.Reader, modDir string) (*filelist.FileList, error) {
}
} else if dir == "" {
// item is a module name
if modFilelist, err := getModule(s.Text(), modDir); err != nil {
return nil, fmt.Errorf("unable to get module file %q: %w", s.Text(), err)
if modFilelist, err := getModule(line, modDir); err != nil {
return nil, fmt.Errorf("unable to get module file %q: %w", line, err)
} else {
for _, file := range modFilelist {
files.Add(file, file)
@@ -135,7 +124,9 @@ func getModulesInDir(modPath string) (files []string, err error) {
// Unable to walk path
return err
}
if filepath.Ext(path) != ".ko" && filepath.Ext(path) != ".xz" {
// this assumes module names are in the format <name>.ko[.format],
// where ".format" (e.g. ".gz") is optional.
if !strings.Contains(".ko", path) {
return nil
}
files = append(files, path)
@@ -197,7 +188,7 @@ func getModuleDeps(modName string, modulesDep io.Reader) ([]string, error) {
s := bufio.NewScanner(modulesDep)
for s.Scan() {
line := s.Text()
line := strings.TrimSpace(s.Text())
if len(line) == 0 || strings.HasPrefix(line, "#") {
continue
}

View File

@@ -18,6 +18,7 @@ func TestStripExts(t *testing.T) {
{"another_file", "another_file"},
{"a.b.c.d.e.f.g.h.i", "a"},
{"virtio_blk.ko", "virtio_blk"},
{"virtio_blk.ko ", "virtio_blk"},
}
for _, table := range tables {
out := stripExts(table.in)

View File

@@ -1,158 +0,0 @@
package osksdl
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc"
)
type OskSdl struct {
mesaDriver string
}
// New returns a new HookScripts that will use the given path to provide a list
// of script files.
func New(mesaDriverName string) *OskSdl {
return &OskSdl{
mesaDriver: mesaDriverName,
}
}
// Get a list of files and their dependencies related to supporting rootfs full
// disk (d)encryption
func (s *OskSdl) List() (*filelist.FileList, error) {
files := filelist.NewFileList()
if exists, err := misc.Exists("/usr/bin/osk-sdl"); !exists {
return files, nil
} else if err != nil {
return files, fmt.Errorf("received unexpected error when getting status for %q: %w", "/usr/bin/osk-sdl", err)
}
log.Println("- Including osk-sdl support")
confFiles := []string{
"/etc/osk.conf",
"/etc/ts.conf",
"/etc/pointercal",
"/etc/fb.modes",
"/etc/directfbrc",
}
confFileList, err := misc.GetFiles(confFiles, false)
if err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add files: %w", err)
}
for _, file := range confFileList {
files.Add(file, file)
}
// osk-sdl
oskFiles := []string{
"/usr/bin/osk-sdl",
"/sbin/cryptsetup",
"/usr/lib/libGL.so.1",
}
if oskFileList, err := misc.GetFiles(oskFiles, true); err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add files: %w", err)
} else {
for _, file := range oskFileList {
files.Add(file, file)
}
}
fontFile, err := getOskConfFontPath("/etc/osk.conf")
if err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add file %q: %w", fontFile, err)
}
files.Add(fontFile, fontFile)
// Directfb
dfbFiles := []string{}
err = filepath.Walk("/usr/lib/directfb-1.7-7", func(path string, f os.FileInfo, err error) error {
if filepath.Ext(path) == ".so" {
dfbFiles = append(dfbFiles, path)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add file %w", err)
}
if dfbFileList, err := misc.GetFiles(dfbFiles, true); err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add files: %w", err)
} else {
for _, file := range dfbFileList {
files.Add(file, file)
}
}
// tslib
tslibFiles := []string{}
err = filepath.Walk("/usr/lib/ts", func(path string, f os.FileInfo, err error) error {
if filepath.Ext(path) == ".so" {
tslibFiles = append(tslibFiles, path)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add file: %w", err)
}
libts, _ := filepath.Glob("/usr/lib/libts*")
tslibFiles = append(tslibFiles, libts...)
if tslibFileList, err := misc.GetFiles(tslibFiles, true); err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add files: %w", err)
} else {
for _, file := range tslibFileList {
files.Add(file, file)
}
}
// mesa hw accel
if s.mesaDriver != "" {
mesaFiles := []string{
"/usr/lib/libEGL.so.1",
"/usr/lib/libGLESv2.so.2",
"/usr/lib/libgbm.so.1",
"/usr/lib/libudev.so.1",
"/usr/lib/xorg/modules/dri/" + s.mesaDriver + "_dri.so",
}
if mesaFileList, err := misc.GetFiles(mesaFiles, true); err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add files: %w", err)
} else {
for _, file := range mesaFileList {
files.Add(file, file)
}
}
}
return files, nil
}
func getOskConfFontPath(oskConfPath string) (string, error) {
var path string
f, err := os.Open(oskConfPath)
if err != nil {
return path, err
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
fields := strings.Fields(s.Text())
// "key = val" is 3 fields
if len(fields) > 2 && fields[0] == "keyboard-font" {
path = fields[2]
}
}
if exists, err := misc.Exists(path); !exists {
return path, fmt.Errorf("unable to find font: %s", path)
} else if err != nil {
return path, fmt.Errorf("received unexpected error when getting status for %q: %w", path, err)
}
return path, nil
}

View File

@@ -3,6 +3,7 @@ package misc
import (
"debug/elf"
"fmt"
"io/fs"
"os"
"path/filepath"
@@ -39,12 +40,42 @@ func getFile(file string, required bool) (files []string, err error) {
return RemoveDuplicates(files), nil
}
// If the file is a symlink we need to do this to prevent an infinite recursion
// loop:
// Symlinks need special handling to prevent infinite recursion:
// 1) add the symlink to the list of files
// 2) set file to dereferenced target
// 4) continue this function to either walk it if the target is a dir or add the
// target to the list of files
if s, err := os.Lstat(file); err == nil {
if s.Mode()&fs.ModeSymlink != 0 {
files = append(files, file)
if target, err := filepath.EvalSymlinks(file); err != nil {
return files, err
} else {
file = target
}
}
}
fileInfo, err := os.Stat(file)
if err != nil {
if required {
return files, fmt.Errorf("getFile: failed to stat file %q: %w", file, err)
// Check if there is a Zstd-compressed version of the file
fileZstd := file + ".zst" // .zst is the extension used by linux-firmware
fileInfoZstd, errZstd := os.Stat(fileZstd)
if errZstd == nil {
file = fileZstd
fileInfo = fileInfoZstd
// Unset nil so we don't retain the error from the os.Stat call for the uncompressed version.
err = nil
} else {
if required {
return files, fmt.Errorf("getFile: failed to stat file %q: %w (also tried %q: %w)", file, err, fileZstd, errZstd)
}
return files, nil
}
return files, nil
}
if fileInfo.IsDir() {
@@ -108,6 +139,7 @@ func getDeps(file string, parents map[string]struct{}) (files []string, err erro
"/usr/lib",
"/lib",
"/usr/lib/expect*",
"/usr/lib/systemd",
}
for _, lib := range libs {

View File

@@ -0,0 +1,167 @@
// Copyright 2025 Clayton Craft <clayton@craftyguy.net>
// SPDX-License-Identifier: GPL-3.0-or-later
package misc
import (
"os"
"path/filepath"
"reflect"
"sort"
"testing"
"time"
)
func TestGetFile(t *testing.T) {
subtests := []struct {
name string
setup func(tmpDir string) (inputPath string, expectedFiles []string, err error)
required bool
}{
{
name: "symlink to directory - no infinite recursion",
setup: func(tmpDir string) (string, []string, error) {
// Create target directory with files
targetDir := filepath.Join(tmpDir, "target")
if err := os.MkdirAll(targetDir, 0755); err != nil {
return "", nil, err
}
testFile1 := filepath.Join(targetDir, "file1.txt")
testFile2 := filepath.Join(targetDir, "file2.txt")
if err := os.WriteFile(testFile1, []byte("content1"), 0644); err != nil {
return "", nil, err
}
if err := os.WriteFile(testFile2, []byte("content2"), 0644); err != nil {
return "", nil, err
}
// Create symlink pointing to target directory
symlinkPath := filepath.Join(tmpDir, "symlink")
if err := os.Symlink(targetDir, symlinkPath); err != nil {
return "", nil, err
}
expected := []string{symlinkPath, testFile1, testFile2}
return symlinkPath, expected, nil
},
required: true,
},
{
name: "symlink to file - returns both symlink and target",
setup: func(tmpDir string) (string, []string, error) {
// Create target file
targetFile := filepath.Join(tmpDir, "target.txt")
if err := os.WriteFile(targetFile, []byte("content"), 0644); err != nil {
return "", nil, err
}
// Create symlink pointing to target file
symlinkPath := filepath.Join(tmpDir, "symlink.txt")
if err := os.Symlink(targetFile, symlinkPath); err != nil {
return "", nil, err
}
expected := []string{symlinkPath, targetFile}
return symlinkPath, expected, nil
},
required: true,
},
{
name: "regular file",
setup: func(tmpDir string) (string, []string, error) {
regularFile := filepath.Join(tmpDir, "regular.txt")
if err := os.WriteFile(regularFile, []byte("content"), 0644); err != nil {
return "", nil, err
}
expected := []string{regularFile}
return regularFile, expected, nil
},
required: true,
},
{
name: "regular directory",
setup: func(tmpDir string) (string, []string, error) {
// Create directory with files
dirPath := filepath.Join(tmpDir, "testdir")
if err := os.MkdirAll(dirPath, 0755); err != nil {
return "", nil, err
}
file1 := filepath.Join(dirPath, "file1.txt")
file2 := filepath.Join(dirPath, "subdir", "file2.txt")
if err := os.WriteFile(file1, []byte("content1"), 0644); err != nil {
return "", nil, err
}
if err := os.MkdirAll(filepath.Dir(file2), 0755); err != nil {
return "", nil, err
}
if err := os.WriteFile(file2, []byte("content2"), 0644); err != nil {
return "", nil, err
}
expected := []string{file1, file2}
return dirPath, expected, nil
},
required: true,
},
{
name: "zst compressed file fallback",
setup: func(tmpDir string) (string, []string, error) {
// Create a .zst file but NOT the original file
zstFile := filepath.Join(tmpDir, "firmware.bin.zst")
if err := os.WriteFile(zstFile, []byte("compressed content"), 0644); err != nil {
return "", nil, err
}
// Request the original file (without .zst extension)
originalFile := filepath.Join(tmpDir, "firmware.bin")
// Expected: should find and return the .zst version
expected := []string{zstFile}
return originalFile, expected, nil
},
required: true,
},
}
for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
tmpDir := t.TempDir()
inputPath, expectedFiles, err := st.setup(tmpDir)
if err != nil {
t.Fatalf("setup failed: %v", err)
}
// Add timeout protection for infinite recursion test
done := make(chan struct{})
var files []string
var getFileErr error
go func() {
defer close(done)
files, getFileErr = getFile(inputPath, st.required)
}()
select {
case <-done:
if getFileErr != nil {
t.Fatalf("getFile failed: %v", getFileErr)
}
case <-time.After(5 * time.Second):
t.Fatal("getFile appears to be in infinite recursion (timeout)")
}
// Sort for comparison
sort.Strings(expectedFiles)
sort.Strings(files)
if !reflect.DeepEqual(expectedFiles, files) {
t.Fatalf("expected: %q, got: %q", expectedFiles, files)
}
})
}
}

View File

@@ -10,6 +10,39 @@ import (
"golang.org/x/sys/unix"
)
// Try to guess whether the system has merged dirs under /usr
func HasMergedUsr() bool {
for _, dir := range []string{"/bin", "/lib"} {
stat, err := os.Lstat(dir)
if err != nil {
// TODO: probably because the dir doesn't exist... so
// should we assume that it's because the system has some weird
// implementation of "merge /usr"?
return true
} else if stat.Mode()&os.ModeSymlink == 0 {
// Not a symlink, so must not be merged /usr
return false
}
}
return true
}
// Converts given path to one supported by a merged /usr config.
// E.g., /bin/foo becomes /usr/bin/foo, /lib/bar becomes /usr/lib/bar
// See: https://www.freedesktop.org/wiki/Software/systemd/TheCaseForTheUsrMerge
func MergeUsr(file string) string {
// Prepend /usr to supported paths
for _, prefix := range []string{"/bin", "/sbin", "/lib", "/lib64"} {
if strings.HasPrefix(file, prefix) {
file = filepath.Join("/usr", file)
break
}
}
return file
}
// Converts a relative symlink target path (e.g. ../../lib/foo.so), that is
// absolute path
func RelativeSymlinkTargetToDir(symPath string, dir string) (string, error) {

View File

@@ -0,0 +1,49 @@
// Copyright 2024 Clayton Craft <clayton@craftyguy.net>
// SPDX-License-Identifier: GPL-3.0-or-later
package osutil
import (
"testing"
)
func TestMergeUsr(t *testing.T) {
subtests := []struct {
in string
expected string
}{
{
in: "/bin/foo",
expected: "/usr/bin/foo",
},
{
in: "/sbin/foo",
expected: "/usr/sbin/foo",
},
{
in: "/usr/sbin/foo",
expected: "/usr/sbin/foo",
},
{
in: "/usr/bin/foo",
expected: "/usr/bin/foo",
},
{
in: "/lib/foo.so",
expected: "/usr/lib/foo.so",
},
{
in: "/lib64/foo.so",
expected: "/usr/lib64/foo.so",
},
}
for _, st := range subtests {
t.Run(st.in, func(t *testing.T) {
out := MergeUsr(st.in)
if out != st.expected {
t.Fatalf("expected: %q, got: %q\n", st.expected, out)
}
})
}
}

View File

@@ -4,23 +4,24 @@
package deviceinfo
import (
"bufio"
"context"
"fmt"
"io"
"log"
"os"
"reflect"
"strconv"
"strings"
"time"
"github.com/mvdan/sh/shell"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc"
)
type DeviceInfo struct {
InitfsCompression string
InitfsExtraCompression string
MesaDriver string
ModulesInitfs string
UbootBoardname string
GenerateSystemdBoot string
FormatVersion string
CreateInitfsExtra bool
}
// Reads the relevant entries from "file" into DeviceInfo struct
@@ -33,13 +34,7 @@ func (d *DeviceInfo) ReadDeviceinfo(file string) error {
return fmt.Errorf("unexpected error getting status for %q: %s", file, err)
}
fd, err := os.Open(file)
if err != nil {
return err
}
defer fd.Close()
if err := d.unmarshal(fd); err != nil {
if err := d.unmarshal(file); err != nil {
return err
}
@@ -47,53 +42,44 @@ func (d *DeviceInfo) ReadDeviceinfo(file string) error {
}
// Unmarshals a deviceinfo into a DeviceInfo struct
func (d *DeviceInfo) unmarshal(r io.Reader) error {
s := bufio.NewScanner(r)
for s.Scan() {
line := s.Text()
if strings.HasPrefix(line, "#") {
continue
}
// line isn't setting anything, so just ignore it
if !strings.Contains(line, "=") {
continue
}
// sometimes line has a comment at the end after setting an option
line = strings.SplitN(line, "#", 2)[0]
line = strings.TrimSpace(line)
// must support having '=' in the value (e.g. kernel cmdline)
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("error parsing deviceinfo line, invalid format: %s", line)
}
name, val := parts[0], parts[1]
val = strings.ReplaceAll(val, "\"", "")
if name == "deviceinfo_format_version" && val != "0" {
return fmt.Errorf("deviceinfo format version %q is not supported", val)
}
fieldName := nameToField(name)
if fieldName == "" {
return fmt.Errorf("error parsing deviceinfo line, invalid format: %s", line)
}
func (d *DeviceInfo) unmarshal(file string) error {
ctx, cancelCtx := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
defer cancelCtx()
vars, err := shell.SourceFile(ctx, file)
if err != nil {
return fmt.Errorf("parsing deviceinfo %q failed: %w", file, err)
}
for k, v := range vars {
fieldName := nameToField(k)
field := reflect.ValueOf(d).Elem().FieldByName(fieldName)
if !field.IsValid() {
// an option that meets the deviceinfo "specification", but isn't
// one we care about in this module
continue
}
field.SetString(val)
switch field.Interface().(type) {
case string:
field.SetString(v.String())
case bool:
if v, err := strconv.ParseBool(v.String()); err != nil {
return fmt.Errorf("deviceinfo %q has unsupported type for field %q, expected 'bool'", file, k)
} else {
field.SetBool(v)
}
case int:
if v, err := strconv.ParseInt(v.String(), 10, 32); err != nil {
return fmt.Errorf("deviceinfo %q has unsupported type for field %q, expected 'int'", file, k)
} else {
field.SetInt(v)
}
default:
return fmt.Errorf("deviceinfo %q has unsupported type for field %q", file, k)
}
}
if err := s.Err(); err != nil {
log.Print("unable to parse deviceinfo: ", err)
return err
if d.FormatVersion != "0" {
return fmt.Errorf("deviceinfo %q has an unsupported format version %q", file, d.FormatVersion)
}
return nil
@@ -117,3 +103,25 @@ func nameToField(name string) string {
return field
}
func (d DeviceInfo) String() string {
return fmt.Sprintf(`{
%s: %v
%s: %v
%s: %v
%s: %v
%s: %v
%s: %v
%s: %v
%s: %v
}`,
"deviceinfo_format_version", d.FormatVersion,
"deviceinfo_", d.FormatVersion,
"deviceinfo_initfs_compression", d.InitfsCompression,
"deviceinfo_initfs_extra_compression", d.InitfsCompression,
"deviceinfo_ubootBoardname", d.UbootBoardname,
"deviceinfo_generateSystemdBoot", d.GenerateSystemdBoot,
"deviceinfo_formatVersion", d.FormatVersion,
"deviceinfo_createInitfsExtra", d.CreateInitfsExtra,
)
}

View File

@@ -4,16 +4,13 @@
package deviceinfo
import (
"fmt"
"reflect"
"strings"
"testing"
)
// Test ReadDeviceinfo and the logic of reading from multiple files
func TestReadDeviceinfo(t *testing.T) {
modules_expected := "panfrost foo bar bazz"
mesa_expected := "msm"
compression_expected := "gz -9"
var devinfo DeviceInfo
err := devinfo.ReadDeviceinfo("./test_resources/deviceinfo-missing")
@@ -28,11 +25,8 @@ func TestReadDeviceinfo(t *testing.T) {
if err != nil {
t.Errorf("received an unexpected err: %s", err)
}
if devinfo.ModulesInitfs != modules_expected {
t.Errorf("expected %q, got: %q", modules_expected, devinfo.ModulesInitfs)
}
if devinfo.MesaDriver != mesa_expected {
t.Errorf("expected %q, got: %q", mesa_expected, devinfo.MesaDriver)
if devinfo.InitfsCompression != compression_expected {
t.Errorf("expected %q, got: %q", compression_expected, devinfo.InitfsCompression)
}
}
@@ -44,10 +38,11 @@ func TestNameToField(t *testing.T) {
}{
{"deviceinfo_dtb", "Dtb"},
{"dtb", "Dtb"},
{"deviceinfo_modules_initfs", "ModulesInitfs"},
{"deviceinfo_initfs_compression", "InitfsCompression"},
{"modules_initfs", "ModulesInitfs"},
{"deviceinfo_modules_initfs___", "ModulesInitfs"},
{"deviceinfo_initfs_compression___", "InitfsCompression"},
{"deviceinfo_initfs_extra_compression", "InitfsExtraCompression"},
{"deviceinfo_create_initfs_extra", "CreateInitfsExtra"},
}
for _, table := range tables {
@@ -63,39 +58,25 @@ func TestUnmarshal(t *testing.T) {
tables := []struct {
// field is just used for reflection within the test, so it must be a
// valid DeviceInfo field
field string
in string
expected string
file string
expected DeviceInfo
}{
{"ModulesInitfs", "deviceinfo_modules_initfs=\"panfrost foo bar bazz\"\n", "panfrost foo bar bazz"},
{"ModulesInitfs", "deviceinfo_modules_initfs=\"panfrost foo bar bazz\"", "panfrost foo bar bazz"},
// line with multiple '='
{"InitfsCompression", "deviceinfo_initfs_compression=zstd:--foo=1 -T0 --bar=bazz", "zstd:--foo=1 -T0 --bar=bazz"},
// empty option
{"ModulesInitfs", "deviceinfo_modules_initfs=\"\"\n", ""},
// line with comment at the end
{"MesaDriver", "deviceinfo_mesa_driver=\"panfrost\" # this is a nice driver", "panfrost"},
{"", "# this is a comment!\n", ""},
// empty lines are fine
{"", "", ""},
// line with whitepace characters only
{"", " \t \n\r", ""},
{"./test_resources/deviceinfo-unmarshal-1", DeviceInfo{
FormatVersion: "0",
UbootBoardname: "foobar-bazz",
InitfsCompression: "zstd:--foo=1 -T0 --bar=bazz",
InitfsExtraCompression: "",
CreateInitfsExtra: true,
},
},
}
var d DeviceInfo
for _, table := range tables {
testName := fmt.Sprintf("unmarshal::'%s':", strings.ReplaceAll(table.in, "\n", "\\n"))
if err := d.unmarshal(strings.NewReader(table.in)); err != nil {
t.Errorf("%s received an unexpected err: ", err)
if err := d.unmarshal(table.file); err != nil {
t.Error(err)
}
// Check against expected value
field := reflect.ValueOf(&d).Elem().FieldByName(table.field)
out := ""
if table.field != "" {
out = field.String()
}
if out != table.expected {
t.Errorf("%s expected: %q, got: %q", testName, table.expected, out)
if d != table.expected {
t.Errorf("expected: %s, got: %s", table.expected, d)
}
}

View File

@@ -1,2 +1,3 @@
deviceinfo_modules_initfs="panfrost foo bar bazz"
deviceinfo_format_version="0"
deviceinfo_initfs_compression="gz -9"
deviceinfo_mesa_driver="panfrost"

View File

@@ -1 +1,2 @@
deviceinfo_mesa_driver="msm"
deviceinfo_format_version="0"
deviceinfo_mesa_driver="msm"

View File

@@ -0,0 +1,7 @@
deviceinfo_format_version="0"
deviceinfo_uboot_boardname="foobar-bazz"
# line with multiple =
deviceinfo_initfs_compression="zstd:--foo=1 -T0 --bar=bazz"
# empty option
deviceinfo_initfs_extra_compression=""
deviceinfo_create_initfs_extra="true" # in-line comment that should be ignored