@@ -106,200 +106,6 @@ def remove(self, label):
106106 del self .menu [label ]
107107 self .menu_order .remove (label )
108108
109- @classmethod
110- def readExtLinux (cls , src_file ):
111- # type:(str) -> Bootloader
112- menu = {} # type: dict[str, MenuEntry | dict[str, str]]
113- menu_order = []
114- default = None
115- timeout = None
116- location = None
117- serial = None
118- label = None
119- title = None
120- kernel = None
121-
122- fh = open_textfile (src_file , "r" )
123- try :
124- for line in fh :
125- l = line .strip ()
126- els = l .split (None , 2 )
127- if len (els ) == 0 :
128- continue
129- keywrd = els [0 ].lower ()
130-
131- # header
132- if l .startswith ('# location ' ) and len (els ) == 3 and els [2 ] in ['mbr' , 'partition' ]:
133- location = els [2 ]
134- elif keywrd == 'serial' and len (els ) > 1 :
135- baud = '9600'
136- flow = None
137- if len (els ) > 2 :
138- if ' ' in els [2 ]:
139- baud , flow = els [2 ].split (None , 1 )
140- else :
141- baud = els [2 ]
142- serial = {'port' : int (els [1 ]), 'baud' : int (baud ), 'flow' : flow }
143- elif keywrd == 'default' and len (els ) == 2 :
144- default = els [1 ]
145- elif keywrd == 'timeout' and len (els ) == 2 :
146- timeout = int (els [1 ])
147-
148- # menu
149- elif keywrd == 'label' and len (els ) == 2 :
150- label = els [1 ]
151- menu [label ] = {}
152- menu_order .append (label )
153- title = None
154- elif label :
155- if keywrd == '#' :
156- title = l [1 :].lstrip ()
157- elif keywrd == 'kernel' and len (els ) > 1 :
158- kernel = els [1 ]
159- elif keywrd == 'append' and len (els ) > 1 and kernel == 'mboot.c32' :
160- if 'tboot' in els [1 ]:
161- # els[2] contains tboot args, hypervisor,
162- # hypervisor args, kernel,
163- # kernel args & initrd
164- args = [x .strip () for x in els [2 ].split ('---' )]
165- if len (args ) == 4 :
166- hypervisor = args [1 ].split (None , 1 )
167- kernel = args [2 ].split (None , 1 ) # type:ignore[assignment] # mypy
168- if len (hypervisor ) == 2 and len (kernel ) == 2 :
169- menu [label ] = MenuEntry (tboot = els [1 ],
170- tboot_args = args [0 ],
171- hypervisor = hypervisor [0 ],
172- hypervisor_args = hypervisor [1 ],
173- kernel = kernel [0 ],
174- kernel_args = kernel [1 ],
175- initrd = args [3 ],
176- title = title )
177- elif 'xen' in els [1 ]:
178- # els[2] contains hypervisor args, kernel,
179- # kernel args & initrd
180- args = [x .strip () for x in els [2 ].split ('---' )]
181- if len (args ) == 3 :
182- kernel = args [1 ].split (None , 1 ) # type:ignore[assignment] # mypy
183- if len (kernel ) == 2 :
184- menu [label ] = MenuEntry (hypervisor = els [1 ],
185- hypervisor_args = args [0 ],
186- kernel = kernel [0 ],
187- kernel_args = kernel [1 ],
188- initrd = args [2 ],
189- title = title )
190- finally :
191- fh .close ()
192-
193- return cls ('extlinux' , src_file , menu , menu_order , default , timeout ,
194- serial , location )
195-
196- @classmethod
197- def readGrub (cls , src_file ):
198- # type: (str) -> Bootloader
199- menu = {}
200- menu_order = []
201- default = 0
202- timeout = None
203- location = None
204- serial = None
205- label = None
206- title = None
207- hypervisor = None
208- hypervisor_args = None
209- kernel = None
210- kernel_args = None
211-
212- def create_label (title ):
213- global COUNTER
214-
215- if title == branding .PRODUCT_BRAND :
216- return 'xe'
217-
218- if title .endswith ('(Serial)' ):
219- return 'xe-serial'
220- if title .endswith ('Safe Mode' ):
221- return 'safe'
222- if ' / ' in title :
223- if '(Serial,' in title :
224- return 'fallback-serial'
225- else :
226- return 'fallback'
227- COUNTER += 1
228- return "label%d" % COUNTER
229-
230- fh = open_textfile (src_file , "r" )
231- try :
232- for line in fh :
233- l = line .strip ()
234- els = l .split (None , 2 )
235- if len (els ) == 0 :
236- continue
237-
238- # header
239- if l .startswith ('# location ' ) and len (els ) == 3 and els [2 ] in ['mbr' , 'partition' ]:
240- location = els [2 ]
241- elif els [0 ] == 'serial' and len (els ) > 1 :
242- port = 0
243- baud = 9600
244- for arg in l .split (None , 1 )[1 ].split ():
245- if '=' in arg :
246- opt , val = arg .split ('=' )
247- if opt == '--unit' :
248- port = int (val )
249- elif opt == '--speed' :
250- baud = int (val )
251- serial = {'port' : port , 'baud' : baud }
252- elif els [0 ] == 'default' and len (els ) == 2 :
253- # default is index into menu list, fixup later
254- default = int (els [1 ])
255- elif els [0 ] == 'timeout' and len (els ) == 2 :
256- timeout = int (els [1 ]) * 10
257-
258- # menu
259- elif els [0 ] == 'title' and len (els ) > 1 :
260- title = l .split (None , 1 )[1 ]
261- elif title :
262- if els [0 ] == 'kernel' and len (els ) > 2 :
263- hypervisor , hypervisor_args = (l .split (None , 1 )
264- [1 ].split (None , 1 ))
265- elif els [0 ] == 'module' and len (els ) > 1 :
266- if kernel and hypervisor :
267- # second module == initrd
268- label = create_label (title )
269- menu_order .append (label )
270- menu [label ] = MenuEntry (hypervisor = hypervisor ,
271- hypervisor_args = hypervisor_args ,
272- kernel = kernel ,
273- kernel_args = kernel_args ,
274- initrd = els [1 ], title = title )
275- hypervisor = None
276- kernel = None
277- else :
278- kernel , kernel_args = (l .split (None , 1 )
279- [1 ].split (None , 1 ))
280- elif els [0 ] == 'initrd' and len (els ) > 1 :
281- # not multiboot
282- kernel = hypervisor
283- kernel_args = hypervisor_args
284- label = create_label (title )
285- menu_order .append (label )
286- menu [label ] = MenuEntry (None ,
287- None ,
288- kernel = kernel ,
289- kernel_args = kernel_args ,
290- initrd = els [1 ], title = title )
291- hypervisor = None
292- hypervisor_args = None
293-
294- # fixup default
295- if len (menu_order ) > default :
296- default = menu_order [default ]
297- finally :
298- fh .close ()
299-
300- return cls ('grub' , src_file , menu , menu_order , default ,
301- timeout , serial , location )
302-
303109 @classmethod
304110 def readGrub2 (cls , src_file ):
305111 # type:(str) -> Bootloader
@@ -472,89 +278,9 @@ def loadExisting(cls, root = '/'):
472278 return cls .readGrub2 (os .path .join (root , "boot/grub/grub.cfg" ))
473279 elif os .path .exists (os .path .join (root , "boot/grub2/grub.cfg" )):
474280 return cls .readGrub2 (os .path .join (root , "boot/grub2/grub.cfg" ))
475- elif os .path .exists (os .path .join (root , "boot/extlinux.conf" )):
476- return cls .readExtLinux (os .path .join (root , "boot/extlinux.conf" ))
477- elif os .path .exists (os .path .join (root , "boot/grub/menu.lst" )):
478- return cls .readGrub (os .path .join (root , "boot/grub/menu.lst" ))
479281 else :
480282 raise RuntimeError ("No existing bootloader configuration found" )
481283
482- def writeExtLinux (self , dst_file = None ):
483- if dst_file and hasattr (dst_file , 'name' ):
484- fh = dst_file
485- else :
486- fh = open_textfile (cast (str , dst_file ), "w" )
487- print ("# location " + self .location , file = fh )
488-
489- if self .serial :
490- if self .serial .get ("flow" , None ) is None :
491- print ("serial %s %s" % (self .serial ['port' ],
492- self .serial ['baud' ]), file = fh )
493- else :
494- print ("serial %s %s %s" % (self .serial ['port' ],
495- self .serial ['baud' ],
496- self .serial ['flow' ]), file = fh )
497- if self .default :
498- print ("default " + self .default , file = fh )
499- print ("prompt 1" , file = fh )
500- if self .timeout :
501- print ("timeout %d" % self .timeout , file = fh )
502-
503- for label in self .menu_order :
504- print ("\n label " + label , file = fh )
505- m = self .menu [label ]
506- if m .title :
507- print (" # " + m .title , file = fh )
508- if m .tboot :
509- print (" kernel mboot.c32" , file = fh )
510- print (" append %s %s --- %s %s --- %s %s --- %s" %
511- (m .tboot , m .tboot_args , m .hypervisor , m .hypervisor_args ,
512- m .kernel , m .kernel_args , m .initrd ), file = fh )
513- elif m .hypervisor :
514- print (" kernel mboot.c32" , file = fh )
515- print (" append %s %s --- %s %s --- %s" %
516- (m .hypervisor , m .hypervisor_args , m .kernel , m .kernel_args , m .initrd ), file = fh )
517- else :
518- print (" kernel " + m .kernel , file = fh )
519- print (" append " + m .kernel_args , file = fh )
520- print (" initrd " + m .initrd , file = fh )
521- if not hasattr (dst_file , 'name' ):
522- fh .close ()
523-
524- def writeGrub (self , dst_file = None ):
525- if dst_file and hasattr (dst_file , 'name' ):
526- fh = dst_file
527- else :
528- fh = open_textfile (cast (str , dst_file ), "w" )
529- print ("# location " + self .location , file = fh )
530-
531- if self .serial :
532- print ("serial --unit=%s --speed=%s" %
533- (self .serial ['port' ], self .serial ['baud' ]), file = fh )
534- print ("terminal --timeout=10 console serial" , file = fh )
535- else :
536- print ("terminal console" , file = fh )
537- if self .default :
538- for i in range (len (self .menu_order )):
539- if self .menu_order [i ] == self .default :
540- print ("default %d" % i , file = fh )
541- break
542- if self .timeout :
543- print ("timeout %d" % (self .timeout // 10 ), file = fh )
544-
545- for label in self .menu_order :
546- m = self .menu [label ]
547- print ("\n title " + m .title , file = fh )
548- if m .hypervisor :
549- print (" kernel " + m .hypervisor + " " + m .hypervisor_args , file = fh )
550- print (" module " + m .kernel + " " + m .kernel_args , file = fh )
551- print (" module " + m .initrd , file = fh )
552- else :
553- print (" kernel " + m .kernel + " " + m .kernel_args , file = fh )
554- print (" initrd " + m .initrd , file = fh )
555- if not hasattr (dst_file , 'name' ):
556- fh .close ()
557-
558284 def writeGrub2 (self , dst_file = None ):
559285 if dst_file and hasattr (dst_file , 'name' ):
560286 fh = dst_file
@@ -626,12 +352,8 @@ def commit(self, dst_file = None):
626352 # write to temp file in final destination directory
627353 fd , tmp_file = tempfile .mkstemp (dir = os .path .dirname (dst_file ))
628354
629- if self .src_fmt == 'extlinux' :
630- self .writeExtLinux (tmp_file )
631- elif self .src_fmt == 'grub' :
632- self .writeGrub (tmp_file )
633- elif self .src_fmt == 'grub2' :
634- self .writeGrub2 (tmp_file )
355+ assert self .src_fmt == 'grub2'
356+ self .writeGrub2 (tmp_file )
635357
636358 # atomically replace destination file
637359 os .close (fd )
0 commit comments