-
Notifications
You must be signed in to change notification settings - Fork 64
/
utils.go
48 lines (41 loc) · 1.28 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package unikraft
import (
"debug/elf"
"fmt"
"os"
"kraftkit.sh/internal/set"
)
// IsFileUnikraftUnikernel is a utility method that determines whether the
// provided input file is a Unikraft unikernel. The file is checked with a
// number of known facts about the kernel image built with Unikraft.
func IsFileUnikraftUnikernel(path string) (bool, error) {
fs, err := os.Stat(path)
if err != nil {
return false, err
} else if fs.IsDir() {
return false, fmt.Errorf("first positional argument is a directory: %v", path)
}
// Sanity check whether the provided file is an ELF kernel with
// Unikraft-centric properties. This check might not always work, especially
// if the version changes and the sections change name.
//
// TODO(nderjung): This check should be replaced with a more stable mechanism
// that detects whether a bootflag is set. See[0].
// [0]: https://github.com/unikraft/unikraft/pull/
fe, err := elf.Open(path)
if err != nil {
return false, err
}
defer fe.Close()
knownUnikraftSections := set.NewStringSet(
".uk_inittab",
".uk_ctortab",
".uk_thread_inittab",
)
for _, symbol := range fe.Sections {
if knownUnikraftSections.ContainsExactly(symbol.Name) {
return true, nil
}
}
return false, fmt.Errorf("provided file is not a Unikraft unikernel")
}