resin-xbuild.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //nolint
  2. package main
  3. import (
  4. "log"
  5. "os"
  6. "os/exec"
  7. "syscall"
  8. )
  9. func crossBuildStart() {
  10. err := os.Remove("/bin/sh")
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. err = os.Link("/usr/bin/resin-xbuild", "/bin/sh")
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. }
  19. func crossBuildEnd() {
  20. err := os.Remove("/bin/sh")
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. err = os.Link("/bin/sh.real", "/bin/sh")
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. }
  29. func runShell() error {
  30. cmd := exec.Command("/usr/bin/qemu-aarch64-static", append([]string{"-0", "/bin/sh", "/bin/sh"}, os.Args[1:]...)...)
  31. cmd.Stdin = os.Stdin
  32. cmd.Stdout = os.Stdout
  33. cmd.Stderr = os.Stderr
  34. return cmd.Run()
  35. }
  36. func main() {
  37. switch os.Args[0] {
  38. case "cross-build-start":
  39. crossBuildStart()
  40. case "cross-build-end":
  41. crossBuildEnd()
  42. case "/bin/sh":
  43. code := 0
  44. crossBuildEnd()
  45. if err := runShell(); err != nil {
  46. code = 1
  47. if exiterr, ok := err.(*exec.ExitError); ok {
  48. if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
  49. code = status.ExitStatus()
  50. }
  51. }
  52. }
  53. crossBuildStart()
  54. // Hack to bypass apk issues with triggering
  55. code = 0
  56. os.Exit(code)
  57. }
  58. }