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.
274 lines
12 KiB
274 lines
12 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.
|
|
|
|
__version__ = "2.2.0.0"
|
|
__app__ = "calculate-builder"
|
|
|
|
import os
|
|
import re
|
|
import sys
|
|
import traceback
|
|
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_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"""
|
|
|
|
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
|
|
|
|
def initVars(self):
|
|
"""Primary initialization of variables"""
|
|
self.clVars = DataVarsBuilder()
|
|
self.clVars.importBuilder()
|
|
self.clVars.flIniFile()
|
|
|
|
def makeKernel(self,quiet=True,showMenuConfig=False,noClean=False,
|
|
lvmOpt=False,dmraidOpt=False,mdadmOpt=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 []
|
|
kernelName = ["--kernname=%s"%clVars.Get('os_linux_system')]
|
|
|
|
params = ["genkernel"]+standardParams+kernelDir+modulePrefix+\
|
|
logLevel+makeOpts+menuConfig+noClean+kernelConfig+\
|
|
bootDir+lvmOpt+dmraidOpt+mdadmOpt+["all"]
|
|
|
|
try:
|
|
genkernelProcess = process(*params,stdout=None,stderr=STDOUT,
|
|
stdin=None)
|
|
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}
|
|
fullVerWithoutCalculate = kernelFullVer.replace("-calculate","")
|
|
newSuffixName = "%s-%s-%s-installed"%(fullVerWithoutCalculate,
|
|
clVars.Get('os_arch_machine'),
|
|
clVars.Get('os_linux_shortname'))
|
|
baseInitrdName = path.join(bootDir,"initramfs-%s"%suffixName)
|
|
baseKernelName = path.join(bootDir,"kernel-%s"%suffixName)
|
|
baseSystemMap = path.join(bootDir,"System.map-%s"%suffixName)
|
|
|
|
newInitrdName = path.join(bootDir,"initramfs-%s"%newSuffixName)
|
|
newKernelName = path.join(bootDir,"vmlinuz-%s"%newSuffixName)
|
|
newSystemMap = path.join(bootDir,"System.map-%s"%newSuffixName)
|
|
newConfigName = path.join(bootDir,"config-%s"%newSuffixName)
|
|
|
|
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 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 link iniramfs-UUID,vmlinuz-UUID,System.map-UUID.
|
|
Create initramfs install (copy of initramfs)
|
|
"""
|
|
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'))
|
|
newSuffixName = "%s-%s-%s"%(fullVerWithoutCalculate,
|
|
clVars.Get('os_arch_machine'),
|
|
clVars.Get('os_linux_shortname'))
|
|
initrdName = path.join(bootDir,"initramfs-%s"%suffixName)
|
|
kernelName = path.join(bootDir,"vmlinuz-%s"%suffixName)
|
|
systemMap = path.join(bootDir,"System.map-%s"%suffixName)
|
|
configName = path.join(bootDir,"config-%s"%suffixName)
|
|
newInitrdName = path.join(bootDir,"initramfs-%s"%newSuffixName)
|
|
newInitrdNameInstall = path.join(bootDir,
|
|
"initramfs-%s-install"%newSuffixName)
|
|
newKernelName = path.join(bootDir,"vmlinuz-%s"%newSuffixName)
|
|
newSystemMap = path.join(bootDir,"System.map-%s"%newSuffixName)
|
|
newConfigName = path.join(bootDir,"config-%s"%newSuffixName)
|
|
|
|
kernelUid = clVars.Get('cl_kernel_uid')
|
|
symlinkInitrdName = path.join(bootDir,"initramfs-%s"%kernelUid)
|
|
symlinkInitrdNameInstall = path.join(bootDir,
|
|
"initramfs-%s-install"%kernelUid)
|
|
symlinkKernelName = path.join(bootDir,"vmlinuz-%s"%kernelUid)
|
|
symlinkSystemMap = path.join(bootDir,"System.map-%s"%kernelUid)
|
|
try:
|
|
self._installFile(initrdName,newInitrdName,removeSource=False)
|
|
self._installFile(initrdName,newInitrdNameInstall)
|
|
self._installFile(kernelName,newKernelName)
|
|
self._installFile(systemMap,newSystemMap)
|
|
self._installFile(configName,newConfigName)
|
|
|
|
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 (OSError,IOError),e:
|
|
self.printERROR(_("Can not install kernel files: %s")%e.strerror)
|
|
return False
|
|
return True
|
|
|
|
def cleanInitrd(self):
|
|
clVars = self.clVars
|
|
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
|