Grub2 install.

netsetup
Mike Hiretsky 13 years ago
parent e02e2948c2
commit b627531eb5

Binary file not shown.

@ -26,6 +26,7 @@ import operator
from cl_utils import runOsCommand,isMount,removeDir,typeFile,pathJoin, \
process,getRunCommands,getTupleVersion,cmpVersion, \
detectDeviceForPartition, getProgPath,listDirectory
from cl_vars_share import varsShare
from shutil import copyfile,copytree
from cl_template import _terms
from subprocess import Popen,PIPE,STDOUT
@ -412,8 +413,13 @@ class Distributive(object, SignalInterrupt):
return res,mes
def getDirectory(self):
"""Get directory which contains distro"""
return self.convertToDirectory().directory
def getBootDirectory(self):
"""Get directory which contains boot"""
return self.getDirectory()
def _makeDirectory(self,pathname):
"""Make directory and parent.
@ -466,9 +472,9 @@ class Distributive(object, SignalInterrupt):
raise DistributiveError(_("Cann't copy") + " '%s' to '%s':\n%s"\
%(indir,outdir,str(e)))
def copy(self,fromdir,todir):
def rsync(self,fromdir,todir,hideSpin=False):
"""Copy files from 'fromdir' directory to 'todir' directory"""
if self.flagSpinner:
if self.flagSpinner and not hideSpin:
def checkRsync():
try:
return len(filter(lambda x:"rsync\x00-a\x00-x" in x,
@ -478,8 +484,11 @@ class Distributive(object, SignalInterrupt):
spin = Spinner()
spin.init(checkFunction=checkRsync,interval=0.6)
spin.start()
rsyncCmd = varsShare().getProgPath('/usr/bin/rsync')
if not rsyncCmd:
raise DistributiveError(_("Can not find '%s' command")%"rsync")
try:
rsyncProcess = process("/usr/bin/rsync", "-a", "-x",
rsyncProcess = process(rsyncCmd, "-a", "-x",
"%s/"%fromdir,"%s/"%todir,stderr=STDOUT)
self.addInterruptProcess(rsyncProcess)
res = rsyncProcess.success()
@ -492,7 +501,7 @@ class Distributive(object, SignalInterrupt):
if self.flagSpinner:
spin.stop()
raise KeyboardInterrupt()
if self.flagSpinner:
if self.flagSpinner and not hideSpin:
spin.stop()
if res is True:
return True
@ -621,7 +630,7 @@ class DirectoryDistributive(Distributive):
# get source distributive as directory distributive
dFrom = source.convertToDirectory()
# copy distributive from source to this
self.copy(dFrom.directory,self.directory)
self.rsync(dFrom.directory,self.directory)
class DataPartition:
"""Data partition"""
@ -1277,7 +1286,7 @@ class FlashDistributive(PartitionDistributive):
distrTo = self.convertToDirectory()
# getting squash from source
if isinstance(source,IsoDistributive):
self.copy(source.getIsoContentDirectory(),distrTo.directory)
self.rsync(source.getIsoContentDirectory(),distrTo.directory)
else:
raise DistributiveError(
_("Installation to flash not support %s"%
@ -1354,7 +1363,7 @@ class ScratchPartitionDistributive(PartitionDistributive):
child.directory = None
def convertToDirectory(self):
"""Convert scratch partition to directory by scratch directory"""
"""Convert scratch partition to directory by scratch directory"""
return self.convertToScratch().convertToDirectory()
def convertToScratch(self):
@ -1368,14 +1377,23 @@ class ScratchPartitionDistributive(PartitionDistributive):
return ScratchDistributive(mdirectory,parent=self)
def prepareScratch(self,directory):
for scrDirectory in ("calculate","delta","workspace"):
"""Create need scratch directories"""
for scrDirectory in ("calculate","delta","workspace","boot"):
self._makeDirectory(path.join(directory,scrDirectory))
os.symlink('workspace/etc',path.join(directory,'etc'))
def postinstallMountBind(self):
def getBootDirectory(self):
"""Get directory which contains boot"""
return path.join(self.convertToScratch().directory,"boot")
def syncBoot(self):
"""Copy boot from aufs to scratch partition"""
dirDistr = self.convertToDirectory().directory
scrDir = self.convertToScratch().directory
self._copytree(path.join(dirDistr,"boot"),
path.join(scrDir,"boot"))
realBootDir = self.getBootDirectory()
self.rsync(path.join(dirDistr,"boot"),realBootDir,hideSpin=True)
def postinstallMountBind(self):
self.syncBoot()
def installFrom(self, source):
"""Install distributive to partition from source distributive"""
@ -1398,6 +1416,7 @@ class ScratchPartitionDistributive(PartitionDistributive):
# prepare scratch
self.prepareScratch(scratchDirectory)
self.syncBoot()
except DistributiveError,e:
raise e
except KeyboardInterrupt,e:

@ -23,7 +23,7 @@ from cl_datavars import glob_attr
from os import path
from os import readlink,access,R_OK
from cl_utils import isMount,typeFile,getTupleVersion,pathJoin,isFstabMount,\
listDirectory, getAvailableX11Drivers, getUUIDDict
listDirectory, getAvailableX11Drivers, getUUIDDict,process
from cl_distr import DistributiveRepository,PartitionDistributive
from cl_fill import clLocale
from operator import itemgetter
@ -297,7 +297,6 @@ class fillVars(object, glob_attr):
if key in blkid_hash:
key = blkid_hash[key]
disk_hash[dev][key] = value[1:-1]
# discard comment and empty string
mapUuidDev = getUUIDDict()
mapDevUuid = dict(zip(mapUuidDev.values(),mapUuidDev.keys()))
mountOptionsList = \
@ -1294,3 +1293,19 @@ class fillVars(object, glob_attr):
resolution = "%s-32"%textLines[0]
return resolution or "1024x768-32@60"
def get_os_grub2_path(self):
"""Get Grub2 Install cmd"""
# find grub2-install
grubInstall = self.getProgPath('/sbin/grub2-install')
if grubInstall:
return grubInstall
# find grub-install and check, that this is grub2-install (ver 1.99)
grubInstall = self.getProgPath('/sbin/grub-install')
if grubInstall and filter(lambda x:"1.99" in x,
process(grubInstall,'-v')):
return grubInstall
return ""
def get_cl_chroot_grub(self):
"""Chroot for grub-mkconfig"""
return self.Get('cl_chroot_path')

@ -27,6 +27,7 @@ from cl_utils import runOsCommand,appendProgramToEnvFile, \
scanDirectory,process,getTupleVersion, \
detectDeviceForPartition,listDirectory, \
cmpVersion
from cl_vars_share import varsShare
from cl_kernel_utils import KernelConfig,InitRamFs
@ -790,18 +791,6 @@ class cl_install(color_print, SignalInterrupt):
self.clVars.importInstall()
self.clVars.flIniFile()
def applyTemplatesLive(self):
"""Apply templates for root of system."""
#self.clVars.Set("cl_root_path","/", True)
self.clVars.Set("cl_chroot_path","/", True)
self.clTempl = template(self.clVars,cltFilter=False)
dirsFiles = self.clTempl.applyTemplates()
if self.clTempl.getError():
self.printERROR(self.clTempl.getError())
return False
else:
return dirsFiles
def cmpInstallVersion(self):
"""Compare current and install version(build)
@ -940,12 +929,23 @@ class cl_install(color_print, SignalInterrupt):
else:
self.printWARNING(_("No update available."))
def canInstallGrub2(self,target):
"""Check that system has grub2 in current and installed system"""
if self.clVars.Get('os_grub2_path'):
return bool(
filter(lambda x:x.startswith('grub-1.99'),
listDirectory('/var/db/pkg/sys-boot')))
return False
def prepareBoot(self,targetDistr):
"""Prepare system for boot"""
if self.clVars.Get('os_install_root_type') == "flash":
self.installSyslinuxBootloader(targetDistr)
else:
self.installGrubBootloader(targetDistr)
if self.canInstallGrub2(targetDistr):
self.installGrub2Bootloader(targetDistr)
else:
self.installLegacyGrubBootloader(targetDistr)
def getTargetDistributive(self,disk,fileSystem="reiserfs",isFormat=False,
systemId=None,buildermode=False):
@ -1041,10 +1041,24 @@ class cl_install(color_print, SignalInterrupt):
self.clTempl.closeFiles()
self.clTempl = None
def applyTemplatesLive(self):
"""Apply templates for root of system."""
#self.clVars.Set("cl_root_path","/", True)
self.clVars.Set("cl_chroot_path","/", True)
self.clVars.Set("cl_chroot_grub","/", True)
self.clTempl = template(self.clVars,cltFilter=False)
dirsFiles = self.clTempl.applyTemplates()
if self.clTempl.getError():
self.printERROR(self.clTempl.getError())
return False
else:
return dirsFiles
def applyTemplatesFlash(self,directory):
"""Apply templates for root of system."""
#self.clVars.Set("cl_root_path",directory, True)
self.clVars.Set("cl_chroot_path","/", True)
self.clVars.Set("cl_chroot_grub","/", True)
self.clVars.Set("cl_root_path",directory, True)
self.clTempl = template(self.clVars,cltObj=False)
dirsFiles = self.clTempl.applyTemplates()
@ -1055,7 +1069,6 @@ class cl_install(color_print, SignalInterrupt):
def applyTemplates(self,directory):
"""Apply templates for root of system."""
#self.clVars.Set("cl_root_path",directory, True)
self.clVars.Set("cl_chroot_path",directory, True)
clTemplateCltPath = \
filter(lambda x:path.exists(x),
@ -1474,30 +1487,96 @@ class cl_install(color_print, SignalInterrupt):
# is partition active
return self.setActivePartition(self.clVars.Get('os_install_root_dev'))
def installGrubBootloader(self,target):
def varSelect(self,selField,where="os_disk_dev",eq=""):
"""Select value from os_disk/device matrix"""
res = filter(lambda x: x[1] == eq,
zip(self.clVars.Get(selField),
self.clVars.Get(where))) or\
[("","")]
return res[0][0]
def getPartitionForParted(self,partition):
"""Get partition info for parted exectution"""
# get disk num and partitin num
bootMap = \
self.varSelect("os_disk_grub",where="os_disk_dev",eq=partition)
# check valid numbers
deviceNumber,op,partitionNumber = bootMap.partition(',')
if not deviceNumber.isdigit() or \
not partitionNumber.isdigit():
return (False,False)
# get partition number
partitionNumber = int(partitionNumber)+1
# get device name
deviceName = self.varSelect("os_device_dev",where="os_device_map",
eq=int(deviceNumber))
if deviceName:
return ("/dev/%s"%deviceName,partitionNumber)
return (False,False)
def setBiosGrubForBootPartition(self):
"""Set bios_grub flag for boot partition in gpt"""
for bootPath in ("/boot","/"):
# get grub disk by mount point
bootPart = self.varSelect(
"os_disk_part",where="os_install_disk_mount",eq=bootPath)
if bootPart:
if bootPart == "gpt":
bootPart = self.varSelect(
"os_disk_dev",where="os_install_disk_mount",eq=bootPath)
deviceName,partitionNumber = \
self.getPartitionForParted(bootPart)
cmdParted = varsShare().getProgPath('/usr/sbin/parted')
if not cmdParted:
return False
partedProcess = process(cmdParted,deviceName,
"set",str(partitionNumber),"bios_grub","on")
return partedProcess.success()
return True
return False
def installGrub2Bootloader(self,target):
"""Install grub2 boot loader"""
cmdGrubInstall = self.clVars.Get('os_grub2_path')
if not self.setBiosGrubForBootPartition():
raise DistributiveError(
_("Cann't set bios_grub flag for boot partition"))
if not cmdGrubInstall:
raise DistributiveError(_("Cann't install bootloader"))
mbrDisk = self.clVars.Get('os_install_mbr')
grubProcess = process(cmdGrubInstall,
"--boot-directory=%s"%target.getBootDirectory(),
"/dev/%s"%mbrDisk)
if grubProcess.failed():
raise DistributiveError(_("Cann't install bootloader"))
def installLegacyGrubBootloader(self,target):
"""Install boot loader
Perform grub installation to disk, which has root partition
"""
if not path.exists('/sbin/grub'):
cmdGrub = varsShare().getProgPath('/sbin/grub')
if not cmdGrub:
raise DistributiveError(_("Cann't install bootloader"))
grubProcess = process("/sbin/grub",
grubProcess = process(cmdGrub,
"--device-map=%s/boot/grub/device.map"%target.getDirectory(),
"--batch",stderr=STDOUT)
bootDisk = self.getFieldByField("grub","mount","/boot",
secondPrefix="os_install_disk")
if not bootDisk:
bootDisk = self.getFieldByField("grub","mount","/",
secondPrefix="os_install_disk")
for bootPath in ("/boot","/"):
# get grub disk by mount point
bootDisk = self.varSelect(
"os_disk_grub",where="os_install_disk_mount",eq=bootPath)
if bootDisk:
break
mbrDisk = self.clVars.Get('os_install_mbr')
mbrDiskNum = filter(lambda x:x[0]==mbrDisk,
zip(self.clVars.Get('os_device_dev'),
self.clVars.Get('os_device_map')))
mbrDiskNum = self.varSelect(
"os_device_map",where="os_device_dev",eq=mbrDisk)
if not mbrDiskNum:
raise DistributiveError(_("Cann't determine mbr disk"))
grubProcess.write("root (hd%s)\n" %bootDisk)
grubProcess.write("setup (hd%d)\n"%mbrDiskNum[0][1])
grubProcess.write("quit\n")
for line in ("root (hd%s)"%bootDisk,
"setup (hd%d)"%mbrDiskNum,
"quit"):
grubProcess.write("%s\n"%line)
if grubProcess.failed():
raise DistributiveError(_("Cann't install bootloader"))
@ -1843,37 +1922,19 @@ class cl_install(color_print, SignalInterrupt):
"""Clean initrd from needless modules"""
# get path to initrd and initrd-install in new system
# (/boot/initramfs-...-install,/boot/initramfs-...)
initrdPath = path.join(self.clVars.Get('cl_chroot_path'),
'boot',self.clVars.Get('os_install_initrd'))
initrdInstallPath = path.join(self.clVars.Get('cl_chroot_path'),
'boot',self.clVars.Get('os_install_initrd_install'))
# section which must contains equealent modules and kernel buildins
importantSections = ['File systems','Pseudo filesystems',
'CDROM.*Filesystems', 'DOS.*Filesystems',
'SCSI Transport']
curConfig = dict(KernelConfig().getSectionParams(*importantSections))
if not self.clVars.Get('os_install_kernel_config'):
copy2(initrdInstallPath,initrdPath);
return True
newConfig = KernelConfig( configFile=
path.join(self.clVars.Get('cl_chroot_path'),
'boot',self.clVars.Get('os_install_kernel_config')))
newConfig = newConfig.getSectionParams(*importantSections)
conflictOptions = filter(lambda x: x[1]=="M" and
x[0] in curConfig and
curConfig[x[0]]=="Y",
newConfig)
if conflictOptions:
copy2(initrdInstallPath,initrdPath);
return True
hr_video = {'nvidia':'nouveau',
'ati':'radeon',
'intel':'intel'}.get(self.clVars.Get('hr_video'),"")
x11_video_drv = self.clVars.Get('os_install_x11_video_drv')
if x11_video_drv == hr_video:
x11_video_drv = ""
return InitRamFs(initrdInstallPath).cleanInitRamFs(initrdPath,
x11_video_drv)
chrootPath = path.join(self.clVars.Get('cl_chroot_path'),'boot')
chrootGrub = path.join(self.clVars.Get('cl_chroot_grub'),'boot')
initrdPath = path.join(chrootPath,self.clVars.Get('os_install_initrd'))
initrdInstallPath = path.join(chrootPath,
self.clVars.Get('os_install_initrd_install'))
copy2(initrdInstallPath,initrdPath);
# if chrootGrub and chrootPath different then it is scratch mode
if chrootGrub != chrootPath:
# copy initrd to scratch device
initrdPathScratch = path.join(chrootGrub,
self.clVars.Get('os_install_initrd'))
copy2(initrdInstallPath,initrdPathScratch);
return True
def afterCopyHDDinstall(self,targetDistr, addUsers, changePwdUsers,
migrateUsers):
@ -1894,6 +1955,13 @@ class cl_install(color_print, SignalInterrupt):
fileCpy.performCopy('/etc')
self.printByResult(True)
# optimize initrd
self.clVars.Set("cl_chroot_path",targetDistr.getDirectory(), True)
self.clVars.Set("cl_chroot_grub",
targetDistr.getBootDirectory()[:-4], True)
self.printMessageForTest(_("Creating new initrd file"))
self.printByResult(self.cleanInitrd())
# join templates
self.printMessageForTest(_("Updating config"))
self.applyTemplates(targetDistr.getDirectory())
@ -1901,11 +1969,6 @@ class cl_install(color_print, SignalInterrupt):
self.printByResult(True)
# optimize initrd
if self.clVars.Get('os_install_root_type') != "usb-hdd":
self.printMessageForTest(_("Creating new initrd file"))
self.printByResult(self.cleanInitrd())
self.printMessageForTest(_("Post-install configuration"))
targetDistr.postinstallMountBind()
self.printByResult(True)

@ -304,6 +304,12 @@ class Data:
# current grub
os_grub_conf = {}
# grub2 install path
os_grub2_path = {'mode':'w'}
# grub chroot need for grub-mkconfig
cl_chroot_grub = {}
# migrate users
cl_migrate_user = {}

Loading…
Cancel
Save