Skip to content

Commit

Permalink
do a quick arch check to ensure both chosen arch && user program matc…
Browse files Browse the repository at this point in the history
…hes (#1676)

* do a quick arch check to ensure both chosen arch && user program matches

* .
  • Loading branch information
eyberg authored Dec 15, 2024
1 parent b88c603 commit 53310ee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
29 changes: 28 additions & 1 deletion fs/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,26 @@ func (m *Manifest) AddNetworkConfig(networkConfig *ManifestNetworkConfig) {
m.root["ip6addr"] = networkConfig.IPv6
}

// b7 = 183 = arm; 3e = 62 = x86
func archCheck(imgpath string) string {
f, err := os.Open(imgpath)
if err != nil {
fmt.Println(err)
}
defer f.Close()

h := make([]byte, 19)
_, err = f.Read(h)

if h[18] == 183 {
return "arm"
}

return "amd64"
}

// AddUserProgram adds user program
func (m *Manifest) AddUserProgram(imgpath string) (err error) {
func (m *Manifest) AddUserProgram(imgpath string, arm bool) (err error) {
parts := strings.Split(imgpath, "/")
if parts[0] == "." {
parts = parts[1:]
Expand All @@ -62,6 +80,15 @@ func (m *Manifest) AddUserProgram(imgpath string) (err error) {
if filepath.IsAbs(imgpath) {
imgpath = filepath.Join(m.targetRoot, imgpath)
}

fmt.Printf("adding user program of %s\n", imgpath)
a := archCheck(imgpath)
if arm && a != "arm" || !arm && a == "arm" {
fmt.Printf("you are trying to mix %s [%s] with the wrong kernel\n", imgpath, a)
fmt.Printf("try re-creating the image with --arch=")
os.Exit(1)
}

err = m.AddFile(program, imgpath)
if err != nil {
return
Expand Down
9 changes: 5 additions & 4 deletions lepton/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,14 @@ func BuildManifest(c *types.Config) (*fs.Manifest, error) {

m := fs.NewManifest(c.TargetRoot)

arm := false
if strings.Contains(c.Kernel, "arm") {
addCommonFilesToManifest(m, true)
} else {
addCommonFilesToManifest(m, false)
arm = true
}

err = m.AddUserProgram(c.Program)
addCommonFilesToManifest(m, arm)

err = m.AddUserProgram(c.Program, arm)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 53310ee

Please sign in to comment.