Added cl-builder, cl-image script

master
Mike Hiretsky 14 years ago
parent de190ad068
commit f533d90139

@ -46,25 +46,13 @@ class DataVarsBuilder(DataVars):
# import builder variables
self.importData(envSection, ('cl_vars_builder','cl_fill_builder'))
class cl_kernel(color_print):
"""Primary class for kernel manipulation"""
kernelCurPath = '/usr/src/linux'
class cl_builder(color_print):
"""Primary class for image manipulation"""
def __init__(self):
self.clVars = None
self.startMessage = ""
def _testKernelDirectory(self,dirpath):
"""Test directory for kernel sources"""
makefilepath = path.join(dirpath,'Makefile')
kbuildpath = path.join(dirpath,'Kbuild')
if not path.exists(makefilepath) \
or not path.exists(kbuildpath) \
or not "Kbuild for top-level directory of the kernel" in \
open(kbuildpath,'r').read():
return False
return True
def setNoColor(self):
self.color = False
@ -73,319 +61,3 @@ class cl_kernel(color_print):
self.clVars = DataVarsBuilder()
self.clVars.importBuilder()
self.clVars.flIniFile()
def makeKernel(self,quiet=True,showMenuConfig=False,noClean=False,
lvmOpt=False,dmraidOpt=False,mdadmOpt=False,
mrproper=False):
"""Run kernel compilation"""
clVars = self.clVars
standardParams = ["--splash=tty1", "--unionfs",
"--all-ramdisk-modules","--disklabel",
"--slowusb", "--no-save-config"]
kernelDir = ["--kerneldir=%s"%clVars.Get('cl_kernel_src_path')]
kernelDestination = clVars.Get('cl_kernel_install_path')
modulePrefix = ["--module-prefix=%s"%kernelDestination]
if not path.exists(kernelDestination):
self.printERROR("Not found destination directory '%s'"%
kernelDestination)
return False
logLevel = ["--loglevel=%d"%(1 if quiet else 2)]
makeOpts = clVars.Get('os_builder_makeopts')
if makeOpts:
makeOpts = ["--makeopts=%s"%makeOpts]
else:
makeOpts = []
menuConfig = ["--menuconfig"] if showMenuConfig else []
noClean = ["--no-clean"] if noClean else []
kernelConfig = ["--kernel-config=%s"%clVars.Get('cl_kernel_config')]
bootDir = clVars.Get('cl_kernel_boot_path')
if not path.exists(bootDir):
os.makedirs(bootDir,mode=0755)
bootDir = ["--bootdir=%s"%bootDir]
lvmOpt = ["--lvm"] if lvmOpt else []
dmraidOpt = ["--dmraid"] if dmraidOpt else []
mdadmOpt = ["--mdadm"] if mdadmOpt else []
mrproperOpt = ["--mrproper"] if mrproper else []
kernelName = ["--kernname=%s"%clVars.Get('os_linux_system')]
cachedir = ["--cachedir=%s"%clVars.Get('cl_kernel_cache_path')]
tempdir = ["--tempdir=%s"%clVars.Get('cl_kernel_temp_path')]
params = ["genkernel"]+cachedir+tempdir+\
standardParams+kernelDir+modulePrefix+\
logLevel+makeOpts+menuConfig+noClean+kernelConfig+\
bootDir+lvmOpt+dmraidOpt+mdadmOpt+mrproperOpt+["all"]
try:
genkernelProcess = process(*params,stdout=None,stderr=STDOUT,
stdin=None,envdict=os.environ)
return genkernelProcess.success()
except KeyboardInterrupt:
self.printERROR("Keyboard interrupt")
return False
def prepareBoot(self):
"""Rename received by genkernel files"""
clVars = self.clVars
bootDir = clVars.Get('cl_kernel_boot_path')
if not os.access(bootDir,os.W_OK):
self.printERROR(_("No permissions to write to '%s'")% bootDir)
return False
march = clVars.Get('os_arch_machine')
if re.match("^i.86$",march):
march = "x86"
baseConfigName = path.join(clVars.Get('cl_kernel_src_path'),
".config")
if path.exists(baseConfigName):
clVars.Set('cl_kernel_config',baseConfigName,True)
kernelFullVer = clVars.Get('cl_kernel_full_ver')
suffixName = "genkernel-%(march)s-%(fullver)s"%\
{"march":march,
"fullver":kernelFullVer}
baseInitrdName = path.join(bootDir,"initramfs-%s"%suffixName)
baseKernelName = path.join(bootDir,"kernel-%s"%suffixName)
baseSystemMap = path.join(bootDir,"System.map-%s"%suffixName)
newInitrdName = self._getName("initramfs")
newKernelName = self._getName("vmlinuz")
newSystemMap = self._getName("System.map")
newConfigName = self._getName("config")
try:
os.rename(baseInitrdName,newInitrdName)
os.rename(baseKernelName,newKernelName)
os.rename(baseSystemMap,newSystemMap)
copy_with_perm(baseConfigName,newConfigName)
except OSError,e:
self.printERROR(_("Can not rename kernel files: %s")%e.strerror)
return False
return True
def _installFile(self,source,target,removeSource=True,symlink=False):
"""Copy, move or create symlink source file to target. Save old."""
def getLinkForTarget(target):
"""Get symlinks from target dirname which point to target"""
baseDir = path.dirname(path.normpath(target))
linkFiles = filter(path.islink,map(lambda x:path.join(baseDir,x),
os.listdir(baseDir)))
return filter(lambda x:path.join(baseDir,
os.readlink(x))==target, linkFiles)
# raise IOError if source is not exists
open(source,'r').close()
targetLinkFiles = getLinkForTarget(target)
oldtarget = ""
if path.lexists(target):
oldtarget = "%s.old" % target
if path.lexists(oldtarget):
oldTargetLinkFiles = getLinkForTarget(oldtarget)
map(os.unlink,oldTargetLinkFiles)
os.unlink(oldtarget)
os.rename(target,oldtarget)
if symlink:
if path.dirname(source)==path.dirname(target):
os.symlink(path.basename(source),target)
else:
os.symlink(source,target)
elif removeSource:
os.rename(source,target)
else:
copy_with_perm(source,target)
if oldtarget:
map(os.unlink,targetLinkFiles)
map(lambda x:os.symlink(path.basename(oldtarget),x),targetLinkFiles)
def _getName(self,obj):
"""Get names for (initramfs,initramfs-install,vmlinuz,System.map,
config) after kernel compilation (installed)"""
clVars = self.clVars
bootDir = clVars.Get('cl_kernel_boot_path')
kernelFullVer = clVars.Get('cl_kernel_full_ver')
fullVerWithoutCalculate = kernelFullVer.replace("-calculate","")
suffixName = "%s-%s-%s-installed"%(fullVerWithoutCalculate,
clVars.Get('os_arch_machine'),
clVars.Get('os_linux_shortname'))
return path.join(bootDir,{"initramfs":"initramfs-%s",
"initramfs-install":"initramfs-%s",
"vmlinuz":"vmlinuz-%s",
"System.map":"System.map-%s",
"config":"config-%s"}[obj]%suffixName)
def _getNewName(self,obj):
"""Get new names for (initramfs,initramfs-install,vmlinuz,System.map,
config) which they have after renaming"""
clVars = self.clVars
bootDir = clVars.Get('cl_kernel_boot_path')
kernelFullVer = clVars.Get('cl_kernel_full_ver')
fullVerWithoutCalculate = kernelFullVer.replace("-calculate","")
newSuffixName = "%s-%s-%s"%(fullVerWithoutCalculate,
clVars.Get('os_arch_machine'),
clVars.Get('os_linux_shortname'))
return path.join(bootDir,{"initramfs":"initramfs-%s",
"initramfs-install":"initramfs-%s-install",
"vmlinuz":"vmlinuz-%s",
"System.map":"System.map-%s",
"config":"config-%s"}[obj]%newSuffixName)
def _getSymlink(self,obj):
"""Get uid symlinks (initramfs,initramfs-install,vmlinuz,System.map)
they pointet to object by _getNewName"""
clVars = self.clVars
bootDir = clVars.Get('cl_kernel_boot_path')
kernelUid = clVars.Get('cl_kernel_uid')
return path.join(bootDir,{"initramfs":"initrd-%s",
"initramfs-install":"initrd-%s-install",
"vmlinuz":"vmlinuz-%s",
"System.map":"System.map-%s"}[obj]%kernelUid)
def createUidSymlinks(self):
"""Create link iniramfs-UUID,vmlinuz-UUID,System.map-UUID."""
if self.skipByChrootAndShortname():
return True
newInitrdName = self._getNewName("initramfs")
newInitrdNameInstall = self._getNewName("initramfs-install")
newKernelName = self._getNewName("vmlinuz")
newSystemMap = self._getNewName("System.map")
symlinkInitrdName = self._getSymlink("initramfs")
symlinkInitrdNameInstall = self._getSymlink("initramfs-install")
symlinkKernelName = self._getSymlink("vmlinuz")
symlinkSystemMap = self._getSymlink("System.map")
try:
self._installFile(newInitrdName,symlinkInitrdName, symlink=True)
self._installFile(newInitrdNameInstall,symlinkInitrdNameInstall,
symlink=True)
self._installFile(newKernelName,symlinkKernelName, symlink=True)
self._installFile(newSystemMap, symlinkSystemMap, symlink=True)
except IOError,e:
self.printERROR(
_("Can not create symlink to current kernel: %s '%s'")%
(e.strerror,e.filename))
self.printERROR(_("May be kernel was not compiled"))
return False
except OSError,e:
self.printERROR(_("Can not create symlink to current kernel: %s")%
e.strerror)
return False
return True
def installBootFiles(self):
"""Copy -install files to without suffix name, and save old copy.
initramfs, vmlinuz, System.map, config with suffix installed copy
withou suffix. Save old files by append suffix .old.
Search link files boot directory link to oldfiles and fix symlink.
Create initramfs install (copy of initramfs)
"""
initrdName = self._getName("initramfs")
kernelName = self._getName("vmlinuz")
systemMap = self._getName("System.map")
configName = self._getName("config")
newInitrdName = self._getNewName("initramfs")
newInitrdNameInstall = self._getNewName("initramfs-install")
newKernelName = self._getNewName("vmlinuz")
newSystemMap = self._getNewName("System.map")
newConfigName = self._getNewName("config")
try:
self._installFile(initrdName,newInitrdName,removeSource=False)
self._installFile(initrdName,newInitrdNameInstall)
self._installFile(kernelName,newKernelName)
self._installFile(systemMap,newSystemMap)
self._installFile(configName,newConfigName)
except (OSError,IOError),e:
self.printERROR(_("Can not install kernel files: %s")%e.strerror)
return False
return True
def skipByChrootAndShortname(self):
"""Return true if run from chroot or system not Calculate"""
clVars = self.clVars
return clVars.Get('cl_chroot_status') == 'on' or \
clVars.Get('os_linux_shortname') in ('Gentoo','Linux')
def versionMigrate(self):
clVars = self.clVars
if self.skipByChrootAndShortname():
return True
calculate2env = clVars.Get('cl_env_path')[0]
if not path.exists(calculate2env) or \
not filter(lambda x:x.startswith('cl_kernel_uid'),
open(calculate2env,'r')):
return self.performVersionMigrate()
return True
def performVersionMigrate(self):
"""Generate cl_kernel_uid, write to calculate2.env, fix grub.conf"""
clVars = self.clVars
clKernelUid = clVars.Get('cl_kernel_uid')
clVars.Write('cl_kernel_uid',clKernelUid,force=True)
reChangeKernel = \
re.compile("(/boot/(?:vmlinuz))(?:-\S+?)?((?:-install)?) ")
reChangeInitrd = \
re.compile("(/boot/)(?:initrd|initramfs)(?:-\S+?)?((?:-install)?)$")
grubconf = '/boot/grub/grub.conf'
rootdev = clVars.Get('os_root_dev')
def grubreduce(x,y):
if y.startswith('title'):
x[0] = False
elif y.startswith('kernel') and "root=%s"%rootdev in y:
x[0] = True
if x[0]:
y = reChangeKernel.sub("\\1-%s\\2 "%clKernelUid,y)
y = reChangeInitrd.sub("\\1initrd-%s\\2"%clKernelUid,y)
return [x[0],x[1] + [y]]
if not os.access(grubconf,os.W_OK):
self.printERROR(_("No permissions to write to '%s'")%grubconf)
return False
newGrub = reduce(grubreduce,open(grubconf,'r'),[False,[]])[1]
open(grubconf,'w').writelines(newGrub)
return True
def setKernelForCurrent(self):
"""Set compiled kernel to current"""
clVars = self.clVars
if self.skipByChrootAndShortname():
return True
# compiling kernel not current.
clKernelPath = clVars.Get('cl_kernel_src_path')
if not clKernelPath in (self.kernelCurPath,
path.realpath(self.kernelCurPath)) and \
path.islink(self.kernelCurPath):
os.unlink(self.kernelCurPath)
kernelName = path.basename(path.normpath(clKernelPath))
try:
os.symlink(kernelName,self.kernelCurPath)
except (IOError,OSError),e:
self.printERROR(str(e))
return False
return True
def cleanInitrd(self):
"""Discard from initrd non-critical modules"""
clVars = self.clVars
if self.skipByChrootAndShortname():
return True
bootDir = clVars.Get('cl_kernel_boot_path')
clKernelUid = clVars.Get('cl_kernel_uid')
initrdName = path.join(bootDir,"initramfs-%s-install"%clKernelUid)
optInitrdName = path.join(bootDir,"initramfs-%s"%clKernelUid)
# old mode (for compatibility)
if not path.lexists(initrdName) or not path.lexists(optInitrdName):
kernelFullVer = clVars.Get('cl_kernel_full_ver')
fullVerWithoutCalculate = kernelFullVer.replace("-calculate","")
suffixName = "%s-%s-%s"%(fullVerWithoutCalculate,
clVars.Get('os_arch_machine'),
clVars.Get('os_linux_shortname'))
initrdName = path.join(bootDir,"initramfs-%s-install"%suffixName)
optInitrdName = path.join(bootDir,"initramfs-%s"%suffixName)
if path.exists(path.realpath(optInitrdName)):
os.unlink(path.realpath(optInitrdName))
try:
initRamFs = InitRamFs(initrdName)
return initRamFs.cleanInitRamFs(path.realpath(optInitrdName))
except (OSError,IOError),e:
self.printERROR(str(e))
return False

@ -25,27 +25,13 @@ from os import path
from cl_utils import process
from subprocess import STDOUT,PIPE
from cl_print import color_print
from cl_datavars import DataVars
from shutil import copy2 as copy_with_perm
from cl_kernel_utils import KernelConfig,InitRamFs
from cl_builder import DataVarsBuilder, printNoColor
from cl_lang import lang
class printNoColor:
def colorPrint(self,attr,fg,bg,string):
sys.stdout.write(string)
class DataVarsBuilder(DataVars):
"""Variable class for installation"""
def importBuilder(self, **args):
'''Get variables for builder'''
# section name in calculate.env
envSection = "builder"
# import builder variables
self.importData(envSection, ('cl_vars_builder','cl_fill_builder'))
class cl_kernel(color_print):
"""Primary class for kernel manipulation"""
kernelCurPath = '/usr/src/linux'

@ -95,28 +95,6 @@ class kernel_cmd(share_cmd):
self.optionsSymlinkIncompatible = ["o","no_clean","m","mdadm","lvm",
"e","dmraid","c","ebuild","initrd" ]
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 checkIncompatibleParam(self,param):
"""Check incompatible options for option specified by param"""
incompatible = list(set(self._getNamesAllSetOptions()) &
set(getattr(self,"options%sIncompatible"%
param.capitalize())))
if incompatible:
self.optobj.error(_("incompatible options")+":"+" %s"\
%self.getStringIncompatibleOptions(incompatible+[param]))
def checkEnviromentForEbuild(self):
"""Check current enviroment for ebuild enviroment variables"""
missedEnvVar = filter(lambda x:not x in os.environ,

@ -85,3 +85,24 @@ class share_cmd(color_print, _error):
color_print.colorPrint = lambda *arg : sys.stdout.write(arg[-1]) or\
sys.stdout.flush()
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 checkIncompatibleParam(self,param):
"""Check incompatible options for option specified by param"""
incompatible = list(set(self._getNamesAllSetOptions()) &
set(getattr(self,"options%sIncompatible"%
param.capitalize())))
if incompatible:
self.optobj.error(_("incompatible options")+":"+" %s"\
%self.getStringIncompatibleOptions(incompatible+[param]))

@ -97,7 +97,6 @@ class cl_install_data(install_data):
if flagFound:
os.chmod(path, mode)
setup(
name = __app__,
version = __version__,
@ -109,6 +108,7 @@ setup(
package_dir = {'calculate-builder': "."},
packages = ['calculate-builder.pym'],
data_files = data_files,
scripts=["./scripts/cl-kernel","./scripts/cl-builder"],
scripts=["./scripts/cl-kernel","./scripts/cl-builder",
"./scripts/cl-image"],
cmdclass={'install_data': cl_install_data},
)

Loading…
Cancel
Save