14 Commits
2.6.0 ... 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
10 changed files with 314 additions and 34 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,8 @@ import (
"log"
"os"
"path/filepath"
"runtime/debug"
"strings"
"time"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/archive"
@@ -26,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) }()
@@ -103,7 +111,9 @@ func main() {
hookfiles.New("/etc/mkinitfs/files"),
hookscripts.New("/usr/share/mkinitfs/hooks", "/hooks"),
hookscripts.New("/etc/mkinitfs/hooks", "/hooks"),
modules.New("/usr/share/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{

View File

@@ -134,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

View File

@@ -421,7 +421,6 @@ func (archive *Archive) writeCpio() error {
archive.addSymlink("/bin", "/bin")
archive.addSymlink("/sbin", "/sbin")
archive.addSymlink("/lib", "/lib")
archive.addSymlink("/usr/sbin", "/usr/sbin")
}
// having a transient function for actually adding files to the archive
// allows the deferred fd.close to run after every copy and prevent having

View File

@@ -3,6 +3,7 @@ package misc
import (
"debug/elf"
"fmt"
"io/fs"
"os"
"path/filepath"
@@ -39,6 +40,24 @@ 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 {
// Check if there is a Zstd-compressed version of the file

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

@@ -40,11 +40,6 @@ func MergeUsr(file string) string {
}
}
// Convert /usr/sbin --> /usr/bin
if part, found := strings.CutPrefix(file, "/usr/sbin"); found {
file = filepath.Join("/usr/bin/", part)
}
return file
}

View File

@@ -18,11 +18,11 @@ func TestMergeUsr(t *testing.T) {
},
{
in: "/sbin/foo",
expected: "/usr/bin/foo",
expected: "/usr/sbin/foo",
},
{
in: "/usr/sbin/foo",
expected: "/usr/bin/foo",
expected: "/usr/sbin/foo",
},
{
in: "/usr/bin/foo",

View File

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