2525import traceback
2626import re
2727import yaml
28+ import base_cmd
2829
2930
30- class install_kernel :
31+ class InstallKernel ( base_cmd . BaseCmd ) :
3132 mount_path = "./mnt"
3233 host_tmp = "/tmp"
3334 mount_tmp = os .path .join (mount_path , "tmp" )
@@ -49,6 +50,7 @@ class install_kernel:
4950 build_path_rel = "build"
5051
5152 def __init__ (self ):
53+ super (InstallKernel , self ).__init__ ()
5254 self ._image_mounted = False
5355
5456 self .device = None
@@ -67,6 +69,8 @@ def __init__(self):
6769 self ._image_dir_path = os .path .join (self .build_path , "VM-" + "ubuntu.aarch64" )
6870 self ._default_image_path = os .path .join (self ._image_dir_path , self .default_image_name )
6971 self .parse_args ()
72+ self .set_debug (self ._args .debug )
73+ self .set_dry_run (self ._args .dry_run )
7074 self ._image_path = os .path .abspath (getattr (self ._args , 'image' ))
7175 self ._image_dir_path = os .path .dirname (self ._image_path )
7276 self .vm_config_path = os .path .join (self ._image_dir_path , "conf.yml" )
@@ -75,54 +79,17 @@ def __init__(self):
7579 self .kernel_ver = self ._args .kernel_ver
7680 self ._kernel_pkg_name = os .path .basename (self ._args .kernel_pkg )
7781 self ._kernel_pkg_path = os .path .abspath (self ._args .kernel_pkg )
78- self .get_kernel_version ( )
82+ self .get_kernel_pkg_version ( self . _kernel_pkg_path )
7983 self .kernel_config_path = os .path .join (self ._image_dir_path ,
80- "conf-{}.yml" .format (self .kernel_ver_minor ))
81- self ._output_image_path = self ._image_path + '.kernel-' + self .kernel_ver
84+ "conf-kernel- {}.yml" .format (self .kernel_ver_minor ))
85+ self ._output_image_path = self ._image_path + '.kernel-' + self .kernel_ver_minor
8286 self ._config_path = os .path .abspath (self ._args .config )
8387 self ._install_pkg_vm_path = os .path .join (self .install_pkg_vm_path , self ._kernel_pkg_name )
8488 self .chroot_cmd = "chroot {} {}" .format (self ._mount_path ,
8589 os .path .join (self .qemu_static_path ,
8690 self .qemu_static_name ))
8791 self .print ("image_path: " + self ._image_path )
8892 self .print ("kernel_pkg_name: " + self ._kernel_pkg_name )
89-
90- def print (self , trace ):
91- print ("{}: {}" .format (sys .argv [0 ], trace ))
92-
93- def terminate (self , err ):
94- if not self .continue_on_error :
95- exit (err )
96-
97- def issue_cmd (self , cmd , fail_on_err = True , err_msg = None , enable_stdout = True , no_capture = False ):
98- rc , output = self .run_command (cmd , enable_stdout = enable_stdout , no_capture = no_capture )
99- if fail_on_err and rc != 0 :
100- self .print ("cmd failed with status: {} cmd: {}" .format (rc , cmd ))
101- if (err_msg ):
102- self .print (err_msg )
103- self .terminate (1 )
104- return rc , output
105-
106- def run_command (self , command , enable_stdout = True , no_capture = False ):
107- output_lines = []
108- if self ._args .debug :
109- print ("{}: {}" .format (sys .argv [0 ], command ))
110- if self ._args .dry_run :
111- print ("" )
112- return 0 , output_lines
113- if no_capture :
114- rc = subprocess .call (command , shell = True )
115- return rc , output_lines
116- process = subprocess .Popen (command .split (), stdout = subprocess .PIPE ) #shlex.split(command)
117- while True :
118- output = process .stdout .readline ()
119- if (not output or output == '' ) and process .poll () is not None :
120- break
121- if output and enable_stdout :
122- self .print (str (output , 'utf-8' ).strip ())
123- output_lines .append (str (output , 'utf-8' ))
124- rc = process .poll ()
125- return rc , output_lines
12693
12794 def parse_args (self ):
12895 parser = argparse .ArgumentParser (formatter_class = RawTextHelpFormatter ,
@@ -234,28 +201,11 @@ def move_old_kernels(self, root_path="/"):
234201 for pattern in copy_patterns :
235202 for file in glob .glob (os .path .join (src_path , pattern )):
236203 if (self .kernel_ver not in file ):
237- self .print ("move {} to {}" .format (file , dest_path ))
204+ self .print ("move {} to {}" .format (file , dest_path ),
205+ debug = True )
238206 cmd = "mv {} {}" .format (file , dest_path )
239207 self .issue_cmd (cmd )
240208
241- def get_kernel_version (self ):
242- if self .kernel_ver :
243- return
244- #Description: Linux kernel, version 5.4.0+
245- cmd = "dpkg --info {}" .format (self ._kernel_pkg_path )
246- rc , output = self .issue_cmd (cmd )
247- self .kernel_ver = None
248- self .kernel_ver_minor = None
249- for line in output :
250- if "Version: " in line :
251- entries = line .split (" " )
252- if len (entries ) > 2 :
253- self .kernel_ver_minor = entries [2 ].rstrip ()
254- self .kernel_ver = entries [2 ].split ("-" )[0 ].rstrip ()
255- if self .kernel_ver == None :
256- raise Exception ("Unable to determine kernel version, please use --kernel_ver argument." )
257- self .print ("Kernel version is: {} ({})" .format (self .kernel_ver , self .kernel_ver_minor ))
258-
259209 def install_pkg (self ):
260210 cmd = self .kernel_pkg_cpy_cmd .format (self ._kernel_pkg_path )
261211 self .issue_cmd (cmd )
@@ -296,7 +246,7 @@ def read_config(self):
296246 print ("default config file {} does not exist. Continuing." )
297247 return None
298248 with open (self .vm_config_path ) as f :
299- print ("parse {}" .format (self .vm_config_path ))
249+ self . print ("parse {}" .format (self .vm_config_path ))
300250 yaml_dict = yaml .safe_load (f )
301251 if 'qemu-conf' in yaml_dict :
302252 return yaml_dict
@@ -310,7 +260,6 @@ def get_qemu_args_for_kernel(self, existing_args):
310260 "initrd.img-{}" .format (self .kernel_ver_minor ))
311261 args = "-kernel {} --initrd {}" .format (vmlinuz_path , initrd_path )
312262 args += ' -append "root=/dev/vda1 nokaslr console=ttyAMA0"'
313- print (args )
314263 return existing_args + " " + args
315264
316265 def create_config_file (self ):
@@ -324,7 +273,7 @@ def create_config_file(self):
324273 yaml_dict ['qemu-conf' ]['qemu_args' ] = new_args
325274 with open (self .kernel_config_path , 'w' ) as f :
326275 yaml_dict = yaml .dump (yaml_dict , f )
327- print ("config file {} written" .format (self .kernel_config_path ))
276+ self . print ("config file {} written" .format (self .kernel_config_path ))
328277
329278 def run_cmd_in_vm (self ):
330279 env_vars = "QEMU=./aarch64-softmmu/qemu-system-aarch64 "
@@ -392,5 +341,5 @@ def run(self):
392341 self .cleanup ()
393342
394343if __name__ == "__main__" :
395- inst_obj = install_kernel ()
344+ inst_obj = InstallKernel ()
396345 inst_obj .run ()
0 commit comments