11package image
22
33import (
4+ "encoding/json"
45 "errors"
56 "fmt"
67 "io"
@@ -26,6 +27,11 @@ type AgentPXEFiles struct {
2627 cpuArch string
2728 tmpPath string
2829 ipxeBaseURL string
30+ kernelArgs string
31+ }
32+
33+ type coreOSKargs struct {
34+ DefaultKernelArgs string `json:"default"`
2935}
3036
3137var _ asset.WritableAsset = (* AgentPXEFiles )(nil )
@@ -65,6 +71,11 @@ func (a *AgentPXEFiles) Generate(dependencies asset.Parents) error {
6571 a .cpuArch = agentArtifacts .CPUArch
6672 a .ipxeBaseURL = strings .Trim (agentconfig .Config .IPxeBaseURL , "/" )
6773
74+ kernelArgs , err := getKernelArgs (filepath .Join (a .tmpPath , "coreos" , "kargs.json" ))
75+ if err != nil {
76+ return err
77+ }
78+ a .kernelArgs = kernelArgs
6879 return nil
6980}
7081
@@ -93,27 +104,29 @@ func (a *AgentPXEFiles) PersistToFile(directory string) error {
93104 }
94105
95106 agentRootfsimgFile := filepath .Join (pxeAssetsFullPath , fmt .Sprintf ("agent.%s-rootfs.img" , a .cpuArch ))
96- fileReader , err := os .Open (filepath .Join (a .tmpPath , "images" , "pxeboot" , "rootfs.img" ))
107+ rootfsReader , err := os .Open (filepath .Join (a .tmpPath , "images" , "pxeboot" , "rootfs.img" ))
97108 if err != nil {
98109 return err
99110 }
100- err = a .copy (agentRootfsimgFile , fileReader )
111+ defer rootfsReader .Close ()
112+ err = a .copy (agentRootfsimgFile , rootfsReader )
101113 if err != nil {
102114 return err
103115 }
104116
105117 agentVmlinuzFile := filepath .Join (pxeAssetsFullPath , fmt .Sprintf ("agent.%s-vmlinuz" , a .cpuArch ))
106- fileReader , err = os .Open (filepath .Join (a .tmpPath , "images" , "pxeboot" , "vmlinuz" ))
118+ kernelReader , err : = os .Open (filepath .Join (a .tmpPath , "images" , "pxeboot" , "vmlinuz" ))
107119 if err != nil {
108120 return err
109121 }
110- err = a .copy (agentVmlinuzFile , fileReader )
122+ defer kernelReader .Close ()
123+ err = a .copy (agentVmlinuzFile , kernelReader )
111124 if err != nil {
112125 return err
113126 }
114127
115128 if a .ipxeBaseURL != "" {
116- err = a .createiPXEScript (a .cpuArch , a .ipxeBaseURL , pxeAssetsFullPath )
129+ err = a .createiPXEScript (a .cpuArch , a .ipxeBaseURL , pxeAssetsFullPath , a . kernelArgs )
117130 if err != nil {
118131 return err
119132 }
@@ -157,17 +170,17 @@ func (a *AgentPXEFiles) copy(filepath string, src io.Reader) error {
157170 return nil
158171}
159172
160- func (a * AgentPXEFiles ) createiPXEScript (cpuArch , ipxeBaseURL , pxeAssetsFullPath string ) error {
173+ func (a * AgentPXEFiles ) createiPXEScript (cpuArch , ipxeBaseURL , pxeAssetsFullPath , kernelArgs string ) error {
161174 iPXEScriptTemplate := `#!ipxe
162175initrd --name initrd %s/%s
163- kernel %s/%s initrd=initrd coreos.live.rootfs_url=%s/%s ignition.firstboot ignition.platform.id=metal
176+ kernel %s/%s initrd=initrd coreos.live.rootfs_url=%s/%s %s
164177boot
165178`
166179
167180 iPXEScript := fmt .Sprintf (iPXEScriptTemplate , ipxeBaseURL ,
168181 fmt .Sprintf ("agent.%s-initrd.img" , a .cpuArch ), ipxeBaseURL ,
169182 fmt .Sprintf ("agent.%s-vmlinuz" , a .cpuArch ), ipxeBaseURL ,
170- fmt .Sprintf ("agent.%s-rootfs.img" , a .cpuArch ))
183+ fmt .Sprintf ("agent.%s-rootfs.img" , a .cpuArch ), kernelArgs )
171184
172185 iPXEFile := fmt .Sprintf ("agent.%s.ipxe" , a .cpuArch )
173186
@@ -179,3 +192,26 @@ boot
179192
180193 return nil
181194}
195+
196+ func getKernelArgs (filepath string ) (string , error ) {
197+ kargs , err := os .Open (filepath )
198+ if err != nil {
199+ return "" , err
200+ }
201+ defer kargs .Close ()
202+
203+ data , err := io .ReadAll (kargs )
204+ if err != nil {
205+ return "" , err
206+ }
207+
208+ var args coreOSKargs
209+ err = json .Unmarshal (data , & args )
210+ if err != nil {
211+ return "" , err
212+ }
213+
214+ // Get the last 2 kernel params i.e. "ignition.firstboot" and "ignition.platform.id=metal"
215+ kernelArgs := strings .SplitN (args .DefaultKernelArgs , " " , 2 )[1 ]
216+ return kernelArgs , nil
217+ }
0 commit comments