9 Commits
1.5 ... 1.6.1

Author SHA1 Message Date
Clayton Craft
731a805a9e getBinaryDeps: properly handle circular lib dependencies
This moves the recursive bit outside of getBinaryDeps and properly
handles circular dependencies.
2023-02-09 12:42:06 -08:00
Clayton Craft
b90624d7dd getBinaryDeps: move recursive bit to new function 2023-02-09 12:41:34 -08:00
Oliver Smith
2a75cf9b4e getInitfsExtraFiles: add scripts from hooks-extra (MR 27)
Put scripts from /etc/postmarketos-mkinitfs/hooks-extra into the extra
initramfs instead of the regular one, similar to how it is possible with
files listed in /etc/postmarketos-mkinitfs/files-extra.

This way we will be able to launch hooks not only very early in the
initramfs as it's currently the case. But also later on after the
initramfs-extra was extracted, and more files are available. ondev2 will
make use of this feature.
2023-01-17 08:12:03 +01:00
Oliver Smith
d52cc16c88 getBinaryDeps: search in /usr/lib/expect* (MR 28)
Instead of only searching for shared libraries in /usr/lib and /lib,
also search in /usr/lib/expect* (currently the expect binary links
against /usr/lib/expect5.45.4/libexpect5.45.4.so).

I've also considered searching /usr/lib recursively, but that would be a
major performance hit.

Expect gets added to the initramfs-extra in a script that runs the
ondev2 testsuite inside qemu.
2023-01-16 22:55:36 -08:00
Oliver Smith
112b572dc2 getFile: print exact error from os.Stat (MR 28)
Pass the exact error message down and make formatting of the message
consistent.
2023-01-16 22:55:30 -08:00
Oliver Smith
0c0a85f3bb getHookFiles: print exact error from getFiles (MR 28)
Instead of assuming that the error is "unable to find file", print the
actual error from getFiles. I just had a situation where the file
exists, but a dependency couldn't be found.

Before:
  generateInitfsExtra: getHookFiles: unable to find file "/usr/bin/expect" required by "/etc/postmarketos-mkinitfs/files-extra/ondev2-test.files"

After:
  generateInitfsExtra: getHookFiles: unable to add file "/usr/bin/expect" required by "/etc/postmarketos-mkinitfs/files-extra/ondev2-test.files": getBinaryDeps: unable to locate dependency for "/usr/bin/expect": libexpect5.45.4.so
2023-01-16 22:55:18 -08:00
Clayton Craft
2761535e12 getInitfsFiles: fix path to kpartx
This changed in a multipath update.

Fixes #16
2022-11-03 12:00:18 -07:00
Clayton Craft
1a72589f6f getHookFiles: print path of each hook file as it is processed
This may be useful during debug later on
2022-09-17 21:31:37 -07:00
Clayton Craft
df0b5d66d7 getHookFiles: wrap errors returned by this function 2022-09-17 21:29:16 -07:00

149
main.go
View File

@@ -151,32 +151,88 @@ func exists(file string) bool {
func getHookFiles(filesdir string) (files []string, err error) {
fileInfo, err := os.ReadDir(filesdir)
if err != nil {
return nil, err
return nil, fmt.Errorf("getHookFiles: unable to read hook file dir: %w", err)
}
for _, file := range fileInfo {
path := filepath.Join(filesdir, file.Name())
f, err := os.Open(path)
if err != nil {
return nil, err
return nil, fmt.Errorf("getHookFiles: unable to open hook file: %w", err)
}
defer f.Close()
log.Printf("-- Including files from: %s\n", path)
s := bufio.NewScanner(f)
for s.Scan() {
if filelist, err := getFiles([]string{s.Text()}, true); err != nil {
return nil, fmt.Errorf("unable to find file %q required by %q", s.Text(), path)
return nil, fmt.Errorf("getHookFiles: unable to add file %q required by %q: %w", s.Text(), path, err)
} else {
files = append(files, filelist...)
}
}
if err := s.Err(); err != nil {
return nil, err
return nil, fmt.Errorf("getHookFiles: uname to process hook file %q: %w", path, err)
}
}
return files, nil
}
func getDeps(file string, parents map[string]struct{}) (files []string, err error) {
if _, found := parents[file]; found {
return
}
// get dependencies for binaries
fd, err := elf.Open(file)
if err != nil {
return nil, fmt.Errorf("getDeps: unable to open elf binary %q: %w", file, err)
}
libs, _ := fd.ImportedLibraries()
fd.Close()
files = append(files, file)
parents[file] = struct{}{}
if len(libs) == 0 {
return
}
// we don't recursively search these paths for performance reasons
libdirGlobs := []string{
"/usr/lib",
"/lib",
"/usr/lib/expect*",
}
for _, lib := range libs {
found := false
findDepLoop:
for _, libdirGlob := range libdirGlobs {
libdirs, _ := filepath.Glob(libdirGlob)
for _, libdir := range libdirs {
path := filepath.Join(libdir, lib)
if _, err := os.Stat(path); err == nil {
binaryDepFiles, err := getDeps(path, parents)
if err != nil {
return nil, err
}
files = append(files, binaryDepFiles...)
files = append(files, path)
found = true
break findDepLoop
}
}
}
if !found {
return nil, fmt.Errorf("getDeps: unable to locate dependency for %q: %s", file, lib)
}
}
return
}
// Recursively list all dependencies for a given ELF binary
func getBinaryDeps(file string) (files []string, err error) {
func getBinaryDeps(file string) ([]string, error) {
// if file is a symlink, resolve dependencies for target
fileStat, err := os.Lstat(file)
if err != nil {
@@ -192,52 +248,14 @@ func getBinaryDeps(file string) (files []string, err error) {
if !filepath.IsAbs(target) {
target, err = misc.RelativeSymlinkTargetToDir(target, filepath.Dir(file))
if err != nil {
return files, err
return nil, err
}
}
binaryDepFiles, err := getBinaryDeps(target)
if err != nil {
return files, err
}
files = append(files, binaryDepFiles...)
return files, err
file = target
}
// get dependencies for binaries
fd, err := elf.Open(file)
if err != nil {
return nil, fmt.Errorf("getBinaryDeps: unable to open elf binary %q: %w", file, err)
}
libs, _ := fd.ImportedLibraries()
fd.Close()
files = append(files, file)
return getDeps(file, make(map[string]struct{}))
if len(libs) == 0 {
return files, err
}
libdirs := []string{"/usr/lib", "/lib"}
for _, lib := range libs {
found := false
for _, libdir := range libdirs {
path := filepath.Join(libdir, lib)
if _, err := os.Stat(path); err == nil {
binaryDepFiles, err := getBinaryDeps(path)
if err != nil {
return files, err
}
files = append(files, binaryDepFiles...)
files = append(files, path)
found = true
break
}
}
if !found {
return nil, fmt.Errorf("getBinaryDeps: unable to locate dependency for %q: %s", file, lib)
}
}
return
}
func getFiles(list []string, required bool) (files []string, err error) {
@@ -273,7 +291,7 @@ func getFile(file string, required bool) (files []string, err error) {
fileInfo, err := os.Stat(file)
if err != nil {
if required {
return files, errors.New("getFile: File does not exist :" + file)
return files, fmt.Errorf("getFile: failed to stat file %q: %w", file, err)
}
return files, nil
}
@@ -424,11 +442,16 @@ func getFdeFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) {
return
}
func getHookScripts() (files []string) {
scripts, _ := filepath.Glob("/etc/postmarketos-mkinitfs/hooks/*.sh")
files = append(files, scripts...)
return
func getHookScripts(scriptsdir string) (files []string, err error) {
fileInfo, err := os.ReadDir(scriptsdir)
if err != nil {
return nil, fmt.Errorf("getHookScripts: unable to read hook script dir: %w", err)
}
for _, file := range fileInfo {
path := filepath.Join(scriptsdir, file.Name())
files = append(files, path)
}
return files, nil
}
func getInitfsExtraFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) {
@@ -464,6 +487,15 @@ func getInitfsExtraFiles(devinfo deviceinfo.DeviceInfo) (files []string, err err
}
}
if exists("/etc/postmarketos-mkinitfs/hooks-extra") {
log.Println("- Including extra hook scripts")
if hookScripts, err := getHookScripts("/etc/postmarketos-mkinitfs/hooks-extra"); err != nil {
return nil, err
} else {
files = append(files, hookScripts...)
}
}
if exists("/usr/bin/osk-sdl") {
log.Println("- Including FDE support")
if fdeFiles, err := getFdeFiles(devinfo); err != nil {
@@ -485,7 +517,7 @@ func getInitfsFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) {
"/bin/sh",
"/bin/busybox-extras",
"/usr/sbin/telnetd",
"/sbin/kpartx",
"/usr/sbin/kpartx",
"/etc/deviceinfo",
"/usr/bin/unudhcpd",
}
@@ -504,9 +536,14 @@ func getInitfsFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) {
}
}
log.Println("- Including hook scripts")
hookScripts := getHookScripts()
files = append(files, hookScripts...)
if exists("/etc/postmarketos-mkinitfs/hooks") {
log.Println("- Including hook scripts")
if hookScripts, err := getHookScripts("/etc/postmarketos-mkinitfs/hooks"); err != nil {
return nil, err
} else {
files = append(files, hookScripts...)
}
}
log.Println("- Including required binaries")
if filelist, err := getFiles(requiredFiles, true); err != nil {