Skip to content

Commit

Permalink
vm-builder: agetty init per architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail-sakhnov committed Oct 21, 2024
1 parent 68661df commit 1ab62bf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion vm-builder/files/Dockerfile.img
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions vm-builder/files/agetty-init-amd64
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions vm-builder/files/agetty-init-arm64
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion vm-builder/files/inittab
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 39 additions & 3 deletions vm-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -58,6 +62,11 @@ var (
configSshd string
)

const (
targetArchLinuxAmd64 = "linux/amd64"
targetArchLinuxArm64 = "linux/arm64"
)

var (
Version string
NeonvmDaemonImage string
Expand All @@ -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 {
Expand Down Expand Up @@ -117,6 +126,7 @@ type TemplatesContext struct {
SpecBuild string
SpecMerge string
InittabCommands []inittabCommand
AgettyInitLine string
ShutdownHook string
}

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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 ""
}
}

0 comments on commit 1ab62bf

Please sign in to comment.