You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
199 lines
8.4 KiB
199 lines
8.4 KiB
#-*- coding: utf-8 -*-
|
|
|
|
# Copyright 2010 Calculate Ltd. http://www.calculate-linux.org
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from cl_kernel import cl_kernel, __app__, __version__
|
|
from cl_opt import opt
|
|
from cl_share_cmd import share_cmd
|
|
import os
|
|
from os import path
|
|
from os import access,R_OK
|
|
import re
|
|
import sys
|
|
|
|
from cl_lang import lang
|
|
lang().setLanguage(sys.modules[__name__])
|
|
|
|
DESCRIPTION = _("The Calculate Linux kernel builder")
|
|
CMD_OPTIONS = [{'shortOption':"c",
|
|
'longOption':"kernel-config",
|
|
'optVal':"FILE",
|
|
'help':_("kernel configuration file to use for compilation")
|
|
},
|
|
{'longOption':"dmraid",
|
|
'help':_("include DMRAID support")
|
|
},
|
|
{'shortOption':"e",
|
|
'longOption':"extraversion",
|
|
'optVal':"VER",
|
|
'help':_("specify extraversion for kernel")
|
|
},
|
|
{'shortOption':"k",
|
|
'longOption':"kerneldir",
|
|
'optVal':"DIR",
|
|
'help':_("location of the kernel sources")
|
|
},
|
|
{'longOption':"lvm",
|
|
'help':_("include LVM support")
|
|
},
|
|
{ 'longOption':"mdadm",
|
|
'help':_("copy /etc/mdadm.conf to initramfs")
|
|
},
|
|
{'shortOption':"m",
|
|
'longOption':"menuconfig",
|
|
'help':_("run menuconfig after oldconfig")
|
|
},
|
|
{'longOption':"mrproper",
|
|
'help':_("run make mrproper before compilation")
|
|
},
|
|
{'longOption':"no-clean",
|
|
'help':_("do not run make clean before compilation")
|
|
},
|
|
{'shortOption':"o",
|
|
'longOption':"use-own-config",
|
|
'help':_("use config from kernel directory")
|
|
},
|
|
{'shortOption':"q",
|
|
'help':_("do not display kernel compilation process")
|
|
},
|
|
{'longOption':"initrd",
|
|
'help':_("perform current initramfs optimization")},
|
|
{'longOption':"ebuild",
|
|
'help':_("build kernel by ebuild phase (need ebuild enviroment)")}
|
|
]
|
|
|
|
|
|
class kernel_cmd(share_cmd):
|
|
"""Class for work with cl_install by console"""
|
|
def __init__(self):
|
|
self.optobj = opt(package=__app__,
|
|
version=__version__,
|
|
description=DESCRIPTION,
|
|
option_list= CMD_OPTIONS + opt.variable_control +
|
|
opt.color_control,
|
|
check_values=self.checkOpts)
|
|
self.logicObj = cl_kernel()
|
|
self.optionsInitrdIncompatible = ["o","no_clean","m","mdadm","lvm",
|
|
"k", "e","dmraid","c" ]
|
|
|
|
def _getNamesAllSetOptions(self):
|
|
"""Get list set options"""
|
|
setOptDict = self.optobj.values.__dict__.items()
|
|
defaultOptDict = self.optobj.get_default_values().__dict__.items()
|
|
return reduce(lambda x,y: x+[y[0][0]],
|
|
filter(lambda x:x[0][1] != x[1][1],
|
|
zip(setOptDict,defaultOptDict)), [])
|
|
|
|
def getStringIncompatibleOptions(self,listOpt):
|
|
"""Форматированная строка несовместимых опций разделенных ','"""
|
|
return ", ".join(map(lambda x: len(x) == 1 and "'-%s'"%x or "'--%s'"%x,
|
|
listOpt))
|
|
|
|
def checkIncompatibleInitrd(self):
|
|
"""Check incompatible options for option --live"""
|
|
incompatible = list(set(self._getNamesAllSetOptions()) &
|
|
set(self.optionsInitrdIncompatible))
|
|
if incompatible:
|
|
self.optobj.error(_("incompatible options")+":"+" %s"\
|
|
%self.getStringIncompatibleOptions(incompatible+["initrd"]))
|
|
|
|
def checkEnviromentForEbuild(self):
|
|
"""Check current enviroment for ebuild enviroment variables"""
|
|
missedEnvVar = filter(lambda x:not x in os.environ,
|
|
['S','T','D','ROOT','WORKDIR',
|
|
'EBUILD_PHASE'])
|
|
if missedEnvVar:
|
|
self.optobj.error(_("Can not read some ebuild variable: %s")%
|
|
", ".join(missedEnvVar))
|
|
|
|
def checkOpts(self, values, args):
|
|
"""Check values all specified options."""
|
|
if len(args) > 0:
|
|
self.optobj.error(_("unrecognized option") + ": %s"% "".join(args))
|
|
if values.ebuild:
|
|
self.checkEnviromentForEbuild()
|
|
if values.initrd:
|
|
self.checkIncompatibleInitrd()
|
|
if values.k:
|
|
if not self.logicObj._testKernelDirectory(values.k):
|
|
self.optobj.error("%s:'%s'"%
|
|
(_("wrong kernel source directory"),values.k))
|
|
else:
|
|
self.logicObj.clVars.Set('cl_kernel_src_path',values.k,True)
|
|
if values.c and values.o:
|
|
self.optobj.error("%s: %s"%(_("incompatible options"),
|
|
self.getStringIncompatibleOptions(["c","o"])))
|
|
if values.c:
|
|
if not path.exists(values.c):
|
|
self.optobj.error(_("kernel config '%s' not found")%values.c)
|
|
else:
|
|
if values.c == path.join(
|
|
self.logicObj.clVars.Get('cl_kernel_src_path'),".config"):
|
|
configFile = "%s.bak"%values.c
|
|
else:
|
|
configFile = values.c
|
|
self.logicObj.clVars.Set('cl_kernel_config',configFile,True)
|
|
elif values.o:
|
|
if path.exists(
|
|
path.join(self.logicObj.clVars.Get('cl_kernel_src_path'),
|
|
".config")):
|
|
self.logicObj.clVars.Set('cl_kernel_config',
|
|
path.join(self.logicObj.clVars.Get('cl_kernel_src_path'),
|
|
".config.bak"),True)
|
|
|
|
self.optobj.checkVarSyntax(values)
|
|
return (values, args)
|
|
|
|
def cleanInitrd(self,options):
|
|
if not self.logicObj.cleanInitrd():
|
|
self.printERROR(_("Failed to optimize initramfs"))
|
|
return False
|
|
else:
|
|
self.printSUCCESS(_("Initramfs successfully optimized"))
|
|
return True
|
|
|
|
def checkEbuildParam(self,options,phase):
|
|
return not options.ebuild or os.environ["EBUILD_PHASE"] == phase
|
|
|
|
def makeKernel(self,options):
|
|
"""Run kernel compilation, installation, optimization"""
|
|
# if set ebuild param check cur EBUILD_PHASE, run kernel compilation
|
|
if self.checkEbuildParam(options,"compile"):
|
|
if not self.logicObj.makeKernel(quiet=options.q,
|
|
showMenuConfig=options.m,
|
|
noClean=options.no_clean,
|
|
lvmOpt=options.lvm,
|
|
dmraidOpt=options.dmraid,
|
|
mdadmOpt=options.mdadm,
|
|
mrproper=options.mrproper):
|
|
self.printERROR(_("Failed kernel compilation"))
|
|
return False
|
|
if not self.logicObj.prepareBoot():
|
|
self.printERROR(_("Failed prepare boot directory"))
|
|
return False
|
|
if self.checkEbuildParam(options,"postinst"):
|
|
if not self.logicObj.installBootFiles():
|
|
self.printERROR(_("Failed kernel install"))
|
|
return False
|
|
if not self.logicObj.versionMigrate():
|
|
self.printERROR(_("Failed kernel nomenclature update"))
|
|
return False
|
|
if not self.logicObj.cleanInitrd():
|
|
self.printWARNING(_("Failed initramfs optimization"))
|
|
if not self.logicObj.setKernelForCurrent():
|
|
self.printWARNING(_("Failed change '%s'")%"/usr/scr/linux")
|
|
return True
|
|
|