this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(agent): kernel params parser

Khue Doan a2ea40db 652ef96f

+115 -1
+44
cmd/nixie-agent/kernel.go
··· 1 + package main 2 + 3 + import ( 4 + "errors" 5 + "os" 6 + "strings" 7 + ) 8 + 9 + type NixieParams struct { 10 + MAC string 11 + API string 12 + } 13 + 14 + func GetNixieParams() (*NixieParams, error) { 15 + data, err := os.ReadFile("/proc/cmdline") 16 + if err != nil { 17 + return nil, err 18 + } 19 + 20 + params := parseKernelParams(string(data)) 21 + nixieParams := &NixieParams{ 22 + MAC: params["nixie_mac_address"], 23 + API: params["nixie_api"], 24 + } 25 + if nixieParams.MAC == "" || nixieParams.API == "" { 26 + return nil, errors.New("missing required kernel parameters: nixie_mac_address or nixie_api") 27 + } 28 + 29 + return nixieParams, nil 30 + } 31 + 32 + func parseKernelParams(cmdline string) map[string]string { 33 + params := make(map[string]string) 34 + 35 + for field := range strings.FieldsSeq(cmdline) { 36 + parts := strings.SplitN(field, "=", 2) 37 + if len(parts) != 2 { 38 + continue 39 + } 40 + params[parts[0]] = parts[1] 41 + } 42 + 43 + return params 44 + }
+68
cmd/nixie-agent/kernel_test.go
··· 1 + package main 2 + 3 + import ( 4 + "reflect" 5 + "testing" 6 + ) 7 + 8 + func TestParseKernelParams(t *testing.T) { 9 + tests := []struct { 10 + name string 11 + in string 12 + want map[string]string 13 + }{ 14 + { 15 + "Valid", 16 + "kernel initrd=initrd0 init=/nix/store/nixos-installer-kexec/init loglevel=4 nixie_mac_address=bc:24:11:0d:2f:20 nixie_api=192.168.1.15:5000", 17 + map[string]string{ 18 + "initrd": "initrd0", 19 + "init": "/nix/store/nixos-installer-kexec/init", 20 + "loglevel": "4", 21 + "nixie_mac_address": "bc:24:11:0d:2f:20", 22 + "nixie_api": "192.168.1.15:5000", 23 + }, 24 + }, 25 + { 26 + "OnlyMAC", 27 + "kernel nixie_mac_address=aa:bb:cc:dd:ee:ff", 28 + map[string]string{ 29 + "nixie_mac_address": "aa:bb:cc:dd:ee:ff", 30 + }, 31 + }, 32 + { 33 + "OnlyAPI", 34 + "kernel nixie_api=192.168.1.100:5000", 35 + map[string]string{ 36 + "nixie_api": "192.168.1.100:5000", 37 + }, 38 + }, 39 + { 40 + "Irrelevant", 41 + "kernel root=/dev/sda1 ro quiet", 42 + map[string]string{ 43 + "root": "/dev/sda1", 44 + }, 45 + }, 46 + { 47 + "Malformed", 48 + "kernel nixie_api nixie_mac_address=aa:bb:cc:dd:ee:ff", 49 + map[string]string{ 50 + "nixie_mac_address": "aa:bb:cc:dd:ee:ff", 51 + }, 52 + }, 53 + { 54 + "Empty", 55 + "", 56 + map[string]string{}, 57 + }, 58 + } 59 + 60 + for _, tt := range tests { 61 + t.Run(tt.name, func(t *testing.T) { 62 + got := parseKernelParams(tt.in) 63 + if !reflect.DeepEqual(got, tt.want) { 64 + t.Errorf("parseKernelParams(%v) = %+v, want %+v", tt.in, got, tt.want) 65 + } 66 + }) 67 + } 68 + }
+3 -1
internal/serve/pxe.go
··· 14 14 ) 15 15 16 16 type PXEBooter struct { 17 + Address string 17 18 Kernel string 18 19 Initrd string 19 20 Init string ··· 26 27 return &pixiecore.Spec{ 27 28 Kernel: pixiecore.ID("kernel"), 28 29 Initrd: []pixiecore.ID{"initrd"}, 29 - Cmdline: fmt.Sprintf("init=%s loglevel=4 nixie_mac_address=%s", b.Init, m.MAC), 30 + Cmdline: fmt.Sprintf("init=%s loglevel=4 nixie_mac_address=%s nixie_api=%s:5000", b.Init, m.MAC, b.Address), 30 31 }, nil 31 32 } 32 33 } ··· 86 87 } 87 88 88 89 booter := &PXEBooter{ 90 + Address: address, 89 91 Kernel: kernel, 90 92 Initrd: initrd, 91 93 Init: init,