From 1ab62bfc6894a9e2c843dbe701cd6f7765973afa Mon Sep 17 00:00:00 2001 From: Misha Sakhnov Date: Thu, 17 Oct 2024 13:14:54 +0200 Subject: [PATCH] vm-builder: agetty init per architecture --- vm-builder/files/Dockerfile.img | 2 +- vm-builder/files/agetty-init-amd64 | 1 + vm-builder/files/agetty-init-arm64 | 1 + vm-builder/files/inittab | 2 +- vm-builder/main.go | 42 +++++++++++++++++++++++++++--- 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 vm-builder/files/agetty-init-amd64 create mode 100644 vm-builder/files/agetty-init-arm64 diff --git a/vm-builder/files/Dockerfile.img b/vm-builder/files/Dockerfile.img index 990ae8617..0abed8262 100644 --- a/vm-builder/files/Dockerfile.img +++ b/vm-builder/files/Dockerfile.img @@ -21,7 +21,7 @@ RUN set -e \ chmod +x /neonvm/bin/busybox \ && /neonvm/bin/busybox --install -s /neonvm/bin - COPY helper.move-bins.sh /helper.move-bins.sh +COPY helper.move-bins.sh /helper.move-bins.sh # add udevd and agetty (with shared libs) RUN set -e \ diff --git a/vm-builder/files/agetty-init-amd64 b/vm-builder/files/agetty-init-amd64 new file mode 100644 index 000000000..894a24ae4 --- /dev/null +++ b/vm-builder/files/agetty-init-amd64 @@ -0,0 +1 @@ +ttyS0::respawn:/neonvm/bin/agetty --8bits --local-line --noissue --noclear --noreset --host console --login-program /neonvm/bin/login --login-pause --autologin root 115200 ttyS0 linux diff --git a/vm-builder/files/agetty-init-arm64 b/vm-builder/files/agetty-init-arm64 new file mode 100644 index 000000000..713a4f669 --- /dev/null +++ b/vm-builder/files/agetty-init-arm64 @@ -0,0 +1 @@ +ttyAMA0::respawn:/neonvm/bin/agetty --8bits --local-line --noissue --noclear --noreset --host console --login-program /neonvm/bin/login --login-pause --autologin root 115200 ttyAMA0 linux diff --git a/vm-builder/files/inittab b/vm-builder/files/inittab index 9f7c284ec..e9ef19f1c 100644 --- a/vm-builder/files/inittab +++ b/vm-builder/files/inittab @@ -12,5 +12,5 @@ {{ range .InittabCommands }} ::{{.SysvInitAction}}:su -p {{.CommandUser}} -c {{.ShellEscapedCommand}} {{ end }} -ttyAMA0::respawn:/neonvm/bin/agetty --8bits --local-line --noissue --noclear --noreset --host console --login-program /neonvm/bin/login --login-pause --autologin root 115200 ttyAMA0 linux +{{ .AgettyInitLine }} ::shutdown:/neonvm/bin/vmshutdown diff --git a/vm-builder/main.go b/vm-builder/main.go index 6fc354643..ef3b1f14a 100644 --- a/vm-builder/main.go +++ b/vm-builder/main.go @@ -38,6 +38,10 @@ var ( scriptVmStart string //go:embed files/inittab scriptInitTab string + //go:embed files/agetty-init-amd64 + scriptAgettyInitAmd64 string + //go:embed files/agetty-init-arm64 + scriptAgettyInitArm64 string //go:embed files/vmacpi scriptVmAcpi string //go:embed files/vmshutdown @@ -58,6 +62,11 @@ var ( configSshd string ) +const ( + targetArchLinuxAmd64 = "linux/amd64" + targetArchLinuxArm64 = "linux/arm64" +) + var ( Version string NeonvmDaemonImage string @@ -72,7 +81,7 @@ var ( version = flag.Bool("version", false, `Print vm-builder version`) daemonImageFlag = flag.String("daemon-image", "", `Specify the neonvm-daemon image: --daemon-image=neonvm-daemon:dev`) - targetArch = flag.String("target-arch", "linux/amd64", `Target architecture: --arch linux/amd64`) + targetArch = flag.String("target-arch", "", fmt.Sprintf("Target architecture: --arch %s | %s", targetArchLinuxAmd64, targetArchLinuxArm64)) ) func AddTemplatedFileToTar(tw *tar.Writer, tmplArgs any, filename string, tmplString string) error { @@ -117,6 +126,7 @@ type TemplatesContext struct { SpecBuild string SpecMerge string InittabCommands []inittabCommand + AgettyInitLine string ShutdownHook string } @@ -133,13 +143,24 @@ func main() { fmt.Println(Version) os.Exit(0) } - if len(*daemonImageFlag) == 0 && len(NeonvmDaemonImage) == 0 { log.Println("neonvm-daemon image not set, needs to be explicitly passed in, or compiled with -ldflags '-X main.NeonvmDaemonImage=...'") flag.PrintDefaults() os.Exit(1) } + if targetArch == nil || *targetArch == "" { + log.Println("Target architecture not set, see usage info:") + flag.PrintDefaults() + os.Exit(1) + } + + if *targetArch != targetArchLinuxAmd64 && *targetArch != targetArchLinuxArm64 { + log.Fatalf("Unsupported target architecture: %q", *targetArch) + flag.PrintDefaults() + return + } + neonvmDaemonImage := NeonvmDaemonImage if len(*daemonImageFlag) != 0 { neonvmDaemonImage = *daemonImageFlag @@ -290,7 +311,8 @@ func main() { SpecBuild: "", // overridden below if spec != nil SpecMerge: "", // overridden below if spec != nil InittabCommands: nil, // overridden below if spec != nil - ShutdownHook: "", // overridden below if spec != nil + AgettyInitLine: getAgettyInitLine(*targetArch), + ShutdownHook: "", // overridden below if spec != nil } if len(imageSpec.Config.User) != 0 { @@ -347,6 +369,8 @@ func main() { {"vmstart", scriptVmStart}, {"vmshutdown", scriptVmShutdown}, {"inittab", scriptInitTab}, + {"agetty-init-amd64", scriptAgettyInitAmd64}, + {"agetty-init-arm64", scriptAgettyInitArm64}, {"vmacpi", scriptVmAcpi}, {"vminit", scriptVmInit}, {"vector.yaml", configVector}, @@ -543,3 +567,15 @@ func (f file) validate() []error { return errs } + +func getAgettyInitLine(targetArch string) string { + switch targetArch { + case targetArchLinuxAmd64: + return scriptAgettyInitAmd64 + case targetArchLinuxArm64: + return scriptAgettyInitArm64 + default: + log.Fatalf("Unsupported target architecture: %q", targetArch) + return "" + } +}