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.
calculate-utils-2.2-install/pym/cl_install.py

422 lines
17 KiB

14 years ago
#-*- coding: utf-8 -*-
# Copyright 2010 Mir Calculate Ltd. http://www.calculate-linux.org
14 years ago
#
# 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-install"
14 years ago
import os
import re
import sys
import cl_overriding
install_errors = ""
def installExit(*args,**kwars):
raise InstallError(globals()["install_errors"])
def overprintERROR(s):
globals()["install_errors"] += s +"\n"
cl_overriding.exit = installExit
cl_overriding.printERROR = overprintERROR
from cl_lang import lang
from cl_fill import clLocale
from cl_template import template
from cl_datavars import DataVars
from cl_print import color_print
from cl_distr import PartitionDistributive,DistributiveRepository,\
DistributiveError, ScratchDistributive
from time import sleep
from subprocess import Popen,PIPE
14 years ago
tr = lang()
tr.setGlobalDomain('cl_install')
14 years ago
tr.setLanguage(sys.modules[__name__])
class InstallError(Exception):
"""Installation Error"""
pass
14 years ago
class printNoColor:
def colorPrint(self,attr,fg,bg,string):
sys.stdout.write(string)
class DataVarsInstall(DataVars):
"""Variable class for installation"""
def importInstall(self, **args):
'''Заполнить конфигурацию переменных, для десктопа'''
# Имя секции в calculate.env
envSection = "calculate-install"
# заполнить переменные окружения алгоритмом по умолнанию
self.importData(envSection, ('cl_vars_install','cl_fill_install'))
class cl_install(color_print):
"""Primary class for templates appling and system installation"""
def __init__(self):
self.clVars = None
self.clTempl = None
self.color = True
def colorPrint(self,*argv,**kwarg):
if self.color:
color_print.colorPrint(self,*argv,**kwarg)
else:
sys.stdout.write(argv[-1])
sys.stdout.flush()
def setNoColor(self):
self.color = False
def initVars(self):
"""Primary initialization of variables"""
self.clVars = DataVarsInstall()
self.clVars.importInstall()
self.clVars.flIniFile()
def applyTemplatesForSystem(self):
"""Apply templates for root of system."""
self.clVars.Set("cl_root_path","/", True)
self.clVars.Set("cl_pass_action", "system", True)
self.clTempl = template(self.clVars)
dirsFiles = self.clTempl.applyTemplates()
if self.clTempl.getError():
self.printERROR(self.clTempl.getError())
return False
else:
return dirsFiles
def printInfo(self,sourceDistr,targetDistr):
self.printSUCCESS(_("Installation %s %s")%
(self.clVars.Get('os_install_linux_name'),
self.clVars.Get('os_install_linux_subname')))
self.defaultPrint(_("System information\n"))
self.printSUCCESS(_("Computer name")+": %s"%
self.clVars.Get('os_net_hostname'))
self.printSUCCESS(_("Domain name")+": %s"%
self.clVars.Get('os_net_domain'))
self.printSUCCESS(_("Network devices")+": %s"%
self.clVars.Get("os_net_interfaces_info"))
self.printSUCCESS(_("Installed system")+": %s %s"%
(self.clVars.Get('os_linux_name'),
self.clVars.Get('os_linux_ver')))
self.defaultPrint(_("Hardware\n"))
self.printSUCCESS(_("Machine hardware name")+": %s"%
self.clVars.Get('os_arch_machine'))
self.printSUCCESS(_("Quantity processors")+": %s"%
self.clVars.Get('hr_cpu_num'))
self.printSUCCESS(_("Videocard")+": %s"%
'<needed>')
self.printSUCCESS(_("Screen resolution")+": %s"%
'<needed>')
self.defaultPrint(_("Localization\n"))
self.printSUCCESS(_("Language")+": %s"%
self.clVars.Get('os_locale_lang'))
self.printSUCCESS(_("Keymap")+": %s"%
self.clVars.Get('os_locale_xkbname'))
self.printSUCCESS(_("Timezone")+": %s"%
self.clVars.Get('os_clock_timezone'))
self.defaultPrint(_("Location\n"))
self.printSUCCESS(_("Working volume")+": %s"%
14 years ago
self.getDeviceByField("load",'/') or "unknown" )
self.printSUCCESS(_("Installation volume")+": %s"%
self.clVars.Get('os_root_dev'))
self.printSUCCESS(_("File system")+": %s"%
self.getFieldByField("format","install","/"))
self.printSUCCESS(_("Swap disk")+": %s"%
self.getFieldByField("dev","install","swap"))
self.printSUCCESS(_("Mounted disks")+": %s"%
", ".join(map(lambda x: x[0],
filter(lambda x: not x[1] in ["","/","swap"],
zip(self.clVars.Get('os_disk_dev'),
self.clVars.Get('os_disk_mount'))))))
self.defaultPrint(_("Network services\n"))
self.printSUCCESS(_("PROXY")+": %s"%
"<needed>")
self.printSUCCESS(_("NTP")+": %s"%
"<needed>")
self.printSUCCESS(_("Authentification")+": %s"%
"<needed>")
14 years ago
self.printSUCCESS("Found update: %s %s %s"%
(self.clVars.Get('os_install_linux_name'),
self.clVars.Get('os_install_linux_subname'),
self.clVars.Get('os_install_linux_ver')))
def wait(self,sec=10):
self.printSUCCESS(_("Press %s to cancel")%"Ctrl+C"+"... %d"%sec, printBR=False)
for i in xrange(sec,0,-1):
self.defaultPrint("\a\b\b %d" % i)
sleep(1)
self.defaultPrint("\b\b \n")
def prepareBoot(self,targetDistr):
14 years ago
self.installBootloader(targetDistr)
def getTargetDistributive(self,disk,buildermode):
if buildermode:
return ScratchDistributive(disk,mdirectory="/mnt/install",
check=True)
else:
return PartitionDistributive(disk,mdirectory="/mnt/install",
check=True)
def applyTemplates(self,directory):
"""Apply templates for root of system."""
self.clVars.Set("cl_root_path",directory, True)
self.clVars.Set("cl_pass_action", "system", True)
self.clTempl = template(self.clVars)
dirsFiles = self.clTempl.applyTemplates()
if self.clTempl.getError():
raise InstallError(self.clTempl.getError())
else:
return dirsFiles
def setInstallDisk(self,disks):
"""Set installation partitions"""
osdiskdev = self.clVars.Get('os_disk_dev')
# all disks must starts with /
wrongDisks = map(lambda x:x[0],
filter(lambda x: not x[0].startswith('/'),
disks))
if wrongDisks:
raise InstallError(_("Wrong disk names %s"%
", ".join(wrongDisks)))
# all mount points must starts with /
wrongMP = map(lambda x:x[1],
filter(lambda x: x[1] and
not x[1].startswith('/'),
disks))
if wrongMP:
raise InstallError(_("Wrong mount point %s"%
", ".join(wrongMP)))
# separate disks to binds and devices
binds, devs = [],[]
for i in disks:
if i[2] == "bind" or not i[0].startswith('/dev'):
binds.append(i)
else:
devs.append(i)
# default mount point for disks is root
devs = map(lambda x:[x[0], x[1] or '/', x[2], x[3]], devs)
# find wrong disks
wrongDisks = map(lambda x:x[0],
filter(lambda x: not x[0] in osdiskdev,
devs))
if wrongDisks:
raise InstallError(_("Disk %s is not exists"%
", ".join(wrongDisks)))
# root partition needed
rootDev = filter(lambda x: x[1] == '/',devs)
if not rootDev:
raise InstallError(_("Need specify root partition"))
rootDev = rootDev[0][0]
# detect duplicate mountPoints
mountPoints = filter(lambda x:x, map(lambda x:x[1], devs + binds))
dupPoint = filter(lambda x: mountPoints.count(x)>1,set(mountPoints))
if dupPoint:
raise InstallError(_("Duplicate mount points %s")%", ".join(
dupPoint))
## detect duplicate partition
devices = map(lambda x:x[0], devs)
dupDevices = filter(lambda x: devices.count(x)>1,set(devices))
if dupDevices:
raise InstallError(_("Duplicate devices %s")%", ".join(
dupDevices))
# detect using extended drives
usedExtendedDrives = map(lambda x: x[0],
filter(lambda x: x[1] == "extended" and
x[0] in devices,
zip(self.clVars.Get('os_disk_dev'),
self.clVars.Get('os_disk_part'))))
if usedExtendedDrives:
raise InstallError(_("Specified disk '%s' is extended")%", ".join(
usedExtendedDrives))
# find device on loaded system and check mounted
loadMount = filter(lambda x:x[0] == rootDev and x[1] != '',
zip(self.clVars.Get('os_disk_dev'),
self.clVars.Get('os_disk_mount')))
if loadMount:
raise InstallError(
_("Specified root disk '%s' mounted to '%s' in current system")%
(rootDev,loadMount[0][1]))
self.clVars.Set('os_install_root_dev',rootDev,True)
# change os_install_disk_mount
newDisks = dict(map(lambda x: [x[0],x[1]],devs))
self.clVars.Set('os_install_disk_mount',
map(lambda x: newDisks[x[0]] if x[0] in newDisks else x[1],
zip(self.clVars.Get('os_disk_dev'),
self.clVars.Get('os_install_disk_mount'))),True)
bdirs = map(lambda x: x[0],binds)
bmountpoints = map(lambda x: x[1],binds)
# remove from old the mountpoint definde by -d
loadbinds = filter(lambda x:not x[0] in bdirs and
not x[1] in bmountpoints,
zip(self.clVars.Get('os_install_bind_dir'),
self.clVars.Get('os_install_bind_mountpoint')))
# add new to old, remove empty mountpoint
loadbinds = loadbinds + filter(lambda x:x[1],
zip(bdirs,bmountpoints))
# reset new bind dirs
self.clVars.Set('os_install_bind_dir',map(lambda x:x[0],loadbinds),True)
self.clVars.Set('os_install_bind_mountpoint',map(lambda x:x[1],loadbinds),True)
# newFormat = dict(filter(lambda x: x[2],
# map(lambda x: [x[0],[x[2],[x[3]]],devs)))
#
#def pr(device,format):
# if device in newFormat
#self.clVars.Set('os_disk_format',
# map(lambda x: newFormat[x[0]] if x[0] in newFormat and newFormat[x[0]] else x[1],
# zip(self.clVars.Get('os_disk_dev'),
# self.clVars.Get('os_disk_format'))),True)
14 years ago
def getDeviceByField(self,field,value):
"""Get device by fields (install load format uuid grub part name)"""
return self.getFieldByField('dev',field,value)
def getFieldByField(self,resfield,field,value):
res = filter(lambda x: x[1] == value,
zip(self.clVars.Get('os_disk_%s'%resfield),
self.clVars.Get('os_disk_%s'%field))) or [("","")]
return res[0][0]
def getFieldByDevice(self,field,device):
"""Get value of field by device"""
device_hash = self.clVars.Get('os_disk_hash')
if device in device_hash and field in device_hash[device]:
return device_hash[device][field]
else:
return ""
def installBootloader(self,target):
"""Install boot loader
Perform grub installation to disk, which has root partition
"""
pipe = Popen(["/sbin/grub",
"--device-map=%s/boot/grub/device.map"%target.getDirectory(),
"--batch"], stdin=PIPE, stdout=PIPE,stderr=PIPE)
14 years ago
pipe.stdin.write("root (hd%s)\n"%
self.getFieldByField("grub","install","/"))
# TODO: change hd0 for bootloader install to other disks
# may be another parameters
pipe.stdin.write("setup (hd0)\n")
pipe.stdin.write("quit\n")
pipe.stdin.close()
# TODO: break if command is hang (KeyboardInterrupt is supported)
if pipe.wait() != 0:
raise DistributiveError("Cann't install bootloader")
def installSystem(self,buildermode=False):
"""install System by current variable enviroment"""
sourceDistr = None
targetDistr = None
error = None
try:
targetDistr = self.getTargetDistributive(
self.clVars.Get('os_install_root_dev'),buildermode)
distRep = DistributiveRepository()
distName = self.clVars.Get('cl_image')
if distName:
# print info
sourceDistr = distRep.getDistributiveByFile(distName)
self.printInfo(sourceDistr,targetDistr)
# wait 10 sec
waittime = 3
self.printSUCCESS(_("Installation will start pass %d seconds.")
%waittime)
self.wait(waittime)
# format partition if needed
self.printSUCCESS(_("Formating partition"))
14 years ago
targetDistr.formatPartition(
self.getFieldByField("format","install","/") or "reiserfs")
# install distributive
self.printSUCCESS(_("Unpacking system image into target"))
targetDistr.installFrom(sourceDistr)
self.printOK("Unpacking complete")
# join templates
self.printSUCCESS(_("Update config"))
self.applyTemplates(targetDistr.getDirectory())
# change boot config
self.printSUCCESS(_("Prepare system for reboot"))
self.prepareBoot(targetDistr)
else:
self.printWARNING("No update available.")
return False
except (InstallError,DistributiveError),e:
error = e
except KeyboardInterrupt,e:
self.defaultPrint("\n")
self.printWARNING("Interrupting the installation")
error = _("Installation manually interrupt")
try:
if self.clTempl:
self.clTempl.closeFiles()
if sourceDistr:
self.printSUCCESS(_("Releasing source distributive"))
sourceDistr.close()
if targetDistr:
self.printSUCCESS(_("Unmount target system volume"))
targetDistr.close()
except (InstallError,DistributiveError),e:
error = "%s\n%s" % (str(error),_("Unmouting error"))
except KeyboardInterrupt,e:
pass
if error:
for line in filter(lambda x: x,str(error).split('\n')):
self.printERROR(line)
return False
self.printSUCCESS(_("System successfully installed"))
return False
def setLinuxName(self,shortname):
self.clVars.Set('os_install_linux_shortname',shortname,True)
def setAllLocaleByLang(self,lang):
"""Set all locale variable by specified lang"""
locale = clLocale()
if not locale.isLangExists(lang):
return False
self.clVars.Set('os_locale_lang',lang, True)
self.clVars.Set('os_locale_locale',
locale.getFieldByLang('locale',lang), True)
self.clVars.Set('os_locale_language',
locale.getFieldByLang('language',lang), True)
self.clVars.Set('os_locale_keymap',
locale.getFieldByLang('keymap',lang), True)
self.clVars.Set('os_locale_dumpkeys',
locale.getFieldByLang('dumpkeys_charset',lang), True)
self.clVars.Set('os_locale_xkb',
locale.getFieldByLang('xkblayout',lang), True)
return True