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-3-install/pym/install/variables/autopartition.py

1044 lines
35 KiB

9 years ago
# -*- coding: utf-8 -*-
9 years ago
# Copyright 2008-2015 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.
import sys
import re
from os import path
from time import sleep
9 years ago
from calculate.lib.datavars import (Variable, VariableError,
VariableInterface,
ReadonlyVariable, ReadonlyTableVariable)
from calculate.lib.utils.device import (humanreadableSize, refreshUdev)
from calculate.lib.utils.files import (readLinesFile, process, getProgPath)
from calculate.install.fs_manager import FileSystemManager
from calculate.lib.utils.tools import Sizes
from itertools import *
import operator
9 years ago
from calculate.lib.cl_lang import setLocalTranslate, _
9 years ago
setLocalTranslate('cl_install3', sys.modules[__name__])
9 years ago
class SizeHelper(VariableInterface):
default_size = Sizes.M
9 years ago
def set(self, value):
# convert table from value to MB
9 years ago
sizeMap = {'kB': Sizes.kB,
'K': Sizes.K,
'M': Sizes.M,
'Mb': Sizes.Mb,
'G': Sizes.G,
'Gb': Sizes.Gb,
'T': Sizes.T,
'Tb': Sizes.Tb}
value = value.strip()
9 years ago
reSizeValue = re.compile('^(\d+)\s*(%s)?' % "|".join(sizeMap.keys()))
res = reSizeValue.search(value)
if not res:
9 years ago
return "0"
intValue = int(res.group(1))
if res.group(2):
intValue = intValue * sizeMap[res.group(2)]
else:
intValue = intValue * self.default_size
return str(intValue)
9 years ago
MINROOTSIZE = 7 * Sizes.G
class AutopartitionError(Exception):
"""
Autopartition error
"""
pass
9 years ago
class AutoPartition(object):
"""
Autopartition maker
"""
9 years ago
def recreateSpace(self, table, device, data, lvm, vgname, bios_grub,
bios_grub_size):
"""
Recreate disk space by table device data lvm flag and vgname
table (gpt,dos)
device - list devices ['/dev/sda']
9 years ago
data - partitions data [['/dev/calculate/boot','/boot','ext4',
534234234'gpt]...]
lvm True or False
vgname lvm Volume Group Name
"""
if lvm:
9 years ago
self.recreateLvm(table, device, data, vgname, bios_grub,
bios_grub_size)
else:
9 years ago
self.recreatePartitionTable(table, device, data, bios_grub,
bios_grub_size)
11 years ago
refreshUdev()
9 years ago
def recreatePartitionTable(self, table, device, data, bios_grub,
bios_grub_size):
"""
"""
9 years ago
mapDispatch = {'dos': self.recreatePartitionTableDos,
'gpt': self.recreatePartitionTableGpt}
if table in mapDispatch:
9 years ago
mapDispatch[table](device, data, bios_grub, bios_grub_size)
else:
9 years ago
raise AutopartitionError(
_('Autopartitioning for %s is not supported') % table)
9 years ago
def recreatePartitionTableDos(self, device, data, bios_grub,
bios_grub_size):
"""
Create DOS partition table by /sbin/fdisk
"""
NEW_PARTITION_TABLE = "o\n"
NEW_PRIMARY_PARTITION = "n\np\n\n\n"
NEW_EXTENDED_PARTITION = "n\ne\n\n"
NEW_LOGICAL_PARTITION = "n\n\n"
MAX_SIZE = "\n"
WRITE_AND_QUIT = "w\nq\n"
fdiskProg = getProgPath('/sbin/fdisk')
9 years ago
fdisk = process(fdiskProg, device[0])
fdisk.write(NEW_PARTITION_TABLE)
num = 1
9 years ago
for size in map(lambda x: str(Sizes().to_K(int(x))) \
if x.isdigit() else x,
map(operator.itemgetter(3), data)):
if num == 4:
9 years ago
fdisk.write(NEW_EXTENDED_PARTITION + MAX_SIZE)
num += 1
9 years ago
size = MAX_SIZE if size == "allfree" else "+%sK\n" % size
if num < 4:
9 years ago
fdisk.write(NEW_PRIMARY_PARTITION + size)
else:
9 years ago
fdisk.write(NEW_LOGICAL_PARTITION + size)
num += 1
fdisk.write(WRITE_AND_QUIT)
fdisk.success()
9 years ago
self._waitDevice(device[0] + str(num - 1))
9 years ago
def recreatePartitionTableGpt(self, device, data, bios_grub=True,
bios_grub_size=0):
"""
Create GPT partition table by /sbin/gdisk
"""
NEW_PARTITION_TABLE = "3\no\ny\n"
NEW_PARTITION = "n\n\n\n"
NEW_BIOSBOOT_PARTITION = "n\n\n\n%s\nef02\n"
MAX_SIZE = "\n\n"
WRITE_AND_QUIT = "w\ny\n"
BIOS_BOOT_PART_NUM = 4
9 years ago
BIOS_BOOT_PART_SIZE = "%dM" % (int(bios_grub_size) / Sizes.M)
fdiskProg = getProgPath('/usr/sbin/gdisk')
9 years ago
fdisk = process(fdiskProg, device[0])
fdisk.write(NEW_PARTITION_TABLE)
num = 1
biosBootCreated = not bios_grub
9 years ago
for size in map(lambda x: str(Sizes().to_K(int(x))) \
if x.isdigit() else x,
map(operator.itemgetter(3), data)):
11 years ago
if num == BIOS_BOOT_PART_NUM and not biosBootCreated:
9 years ago
fdisk.write(NEW_BIOSBOOT_PARTITION %
("+%s" % BIOS_BOOT_PART_SIZE))
biosBootCreated = True
num += 1
if size == "allfree":
if biosBootCreated:
size = MAX_SIZE
else:
9 years ago
size = "-%s\n\n" % BIOS_BOOT_PART_SIZE
else:
9 years ago
size = "+%sK\n\n" % size
fdisk.write(NEW_PARTITION + size)
num += 1
if not biosBootCreated:
9 years ago
fdisk.write(NEW_BIOSBOOT_PARTITION % "")
fdisk.write(WRITE_AND_QUIT)
fdisk.success()
9 years ago
self._waitDevice(device[0] + str(num - 1))
9 years ago
def _waitDevice(self, device):
for waittime in (0.1, 0.2, 0.5, 1, 2, 4):
if path.exists(device):
return True
else:
sleep(waittime)
raise AutopartitionError(
_("Failed to found partition %s after creating the partition table")
9 years ago
% device)
9 years ago
def _createPhisicalVolumes(self, devices):
pvCreateProg = getProgPath('/sbin/pvcreate')
9 years ago
return process(pvCreateProg, "-ff", *devices).success()
9 years ago
def _createVolumesGroup(self, vgname, disks):
vgCreateProg = getProgPath('/sbin/vgcreate')
9 years ago
return process(vgCreateProg, vgname, *disks).success()
9 years ago
def _createLogicalVolume(self, vgname, lvname, size):
if size.isdigit():
size = str(Sizes().to_K(int(size)))
if size == "allfree":
sizeparam = "-l100%FREE"
else:
9 years ago
sizeparam = "-L%sK" % size
lvCreateProg = getProgPath('/sbin/lvcreate')
9 years ago
return process(lvCreateProg, sizeparam, vgname, "-n", lvname).success()
9 years ago
def _removeVolumeGroup(self, vgname):
vgRemoveProg = getProgPath('/sbin/vgremove')
# double remove volume group
9 years ago
return process(vgRemoveProg, vgname, "-f").success() or \
process(vgRemoveProg, vgname, "-f").success()
9 years ago
def clearLvm(self, devices, dv):
"""
Remove lvm physical volumes from devices
"""
vgRemoveProg = getProgPath('/sbin/vgremove')
pvRemoveProg = getProgPath('/sbin/pvremove')
lvRemoveProg = getProgPath('/sbin/lvremove')
9 years ago
disks = dv.Select('os_disk_dev', where='os_disk_parent', _in=devices)
12 years ago
failed = False
9 years ago
for group, pair in groupby(dv.Select(['os_lvm_vgname', 'os_lvm_lvname'],
where='os_lvm_pvname', _in=disks,
12 years ago
sort="ASC/1"),
operator.itemgetter(0)):
9 years ago
for vgname, lvname in pair:
failed |= self.doubleExec(lvRemoveProg,
9 years ago
"%s/%s" % (vgname, lvname), "-f")
failed |= self.doubleExec(vgRemoveProg, group, "-f")
for pvname in list(set(disks) & set(dv.Get('os_lvm_pvname'))):
failed |= self.doubleExec(pvRemoveProg, pvname, "-ffy")
12 years ago
return not failed
9 years ago
def doubleExec(self, *args):
"""
Running double exec command with 2 seconds interval
"""
if process(*args).failed():
sleep(2)
return process(*args).failed()
return False
9 years ago
def clearRaid(self, devices, dv):
"""
12 years ago
Remove raid from devices
"""
12 years ago
mdAdmProg = getProgPath('/sbin/mdadm')
failed = False
9 years ago
for disktype, grouped in groupby(
dv.Select(['os_disk_type', 'os_disk_dev'],
where='os_disk_parent',
_in=['/dev/sda', '/dev/sdb'],
sort="ASC/1"),
operator.itemgetter(0)):
12 years ago
if disktype.endswith('-raid'):
for disk_type, disk_dev in grouped:
9 years ago
failed |= self.doubleExec(mdAdmProg, '-S', disk_dev)
12 years ago
if "raidmember" in disktype:
for disk_type, disk_dev in grouped:
failed |= self.doubleExec(mdAdmProg,
9 years ago
'--zero-superblock', disk_dev)
12 years ago
return not failed
9 years ago
def recreateLvm(self, table, devices, data, vgname, bios_grub,
bios_grub_size):
"""
Create GPT partition table by /sbin/gdisk
"""
9 years ago
notInLvm = []
for i, device in enumerate(devices):
if i == 0:
9 years ago
DEV, MP, FS, SIZE, TABLE = 0, 1, 2, 3, 4
notInLvm = filter(lambda x: "boot" in x[MP], data)
self.recreatePartitionTable(table,
9 years ago
[device], notInLvm +
[['', '', '', 'allfree']],
bios_grub,
bios_grub_size)
else:
self.recreatePartitionTable(table,
9 years ago
[device], [['', '', '', 'allfree']],
bios_grub,
bios_grub_size)
lvmPartOffset = 1 + len(notInLvm)
9 years ago
iPart = [lvmPartOffset] + [1] * (len(devices) - 1)
disks = map(lambda x: "%s%d" % x, zip(devices, iPart))
if not self._createPhisicalVolumes(disks):
raise AutopartitionError(
_("Failed to create physical volumes from %s")
9 years ago
% ",".join(devices))
9 years ago
if not self._createVolumesGroup(vgname, disks):
raise AutopartitionError(
12 years ago
_("Failed to create volume group {groupname} from {disks}")
9 years ago
.format(groupname=vgname,
disks=",".join(devices)))
9 years ago
getProgPath('/sbin/lvcreate')
for disk, size in map(operator.itemgetter(0, 3),
filter(lambda x: not "boot" in x[MP], data)):
lvname = disk.rpartition('/')[2]
9 years ago
if not self._createLogicalVolume(vgname, lvname, size):
raise AutopartitionError(
_("Failed to create logical volume {name}")
9 years ago
.format(name=lvname))
self._waitDevice('/dev/%s/root' % vgname)
9 years ago
class AutopartitionHelper(VariableInterface):
"""
Helper for autopartiton device and mount point creating
"""
9 years ago
def deviceOpts(self, listvalue):
l = [x for x in listvalue if x not in ("home", "lvm", "raid", "grub")]
for i in ("uefi", "swap", "boot"):
if i in l:
l.remove(i)
yield i
break
yield "root2"
if "root" in l:
l.remove("root")
yield "root"
for i in l:
yield i
9 years ago
def bindOpts(self, listvalue):
return filter(lambda x: x in ("home",),
listvalue)
def mpByOpts(self, value):
mapMp = {'swap': 'swap',
'boot': '/boot',
'root2': '/',
'root': '',
'uefi': '/boot/efi',
'data': '/var/calculate'}
return mapMp.get(value, '')
def sourceMpByOpts(self, value):
mapMp = {'home': '/var/calculate/home'}
return mapMp.get(value, '')
def targetMpByOpts(self, value):
mapMp = {'home': '/home'}
return mapMp.get(value, '')
def getAutopartitionScheme(self):
9 years ago
optOrder = {'uefi': 0,
'root': 1,
'home': 2,
'swap': 3,
'boot': 4,
'data': 5,
}
return sorted(self.Get('cl_autopartition_scheme'), key=optOrder.get)
def uncompatible(self):
if self.Get('cl_autopartition_set') == "off":
return \
12 years ago
_("Autopartition options are not available with manual "
"partitioning")
return ""
9 years ago
class VariableHrMemorySize(ReadonlyVariable):
"""
Memory size in bytes
"""
type = "int"
def get(self):
reMemTotal = re.compile(r'^MemTotal:\s*(\d+)\s*kB$')
9 years ago
totalMemList = filter(lambda x: x,
map(reMemTotal.search,
readLinesFile('/proc/meminfo')))
if totalMemList:
9 years ago
size = int(totalMemList[0].group(1)) * Sizes.K
return str(size)
return "0"
def humanReadable(self):
return humanreadableSize(self.Get())
9 years ago
class VariableClAutopartitionSwapSize(SizeHelper, AutopartitionHelper,
Variable):
"""
Swap size
"""
type = "size-m"
opt = ["--swap-size"]
metavalue = "SIZE"
untrusted = True
def init(self):
9 years ago
self.label = _("Swap partition size") + " (MB)"
self.help = _("set the swap partition size for autopartition")
def get(self):
size = int(self.Get('hr_memory_size'))
if size < Sizes.G:
size = Sizes.G
return str(size)
def humanReadable(self):
return humanreadableSize(int(self.Get()))
9 years ago
class VariableClAutopartitionDevice(AutopartitionHelper, Variable):
"""
Device for autopartition
"""
type = "choice-list"
element = "selecttable"
opt = ["-D"]
metavalue = "DEVICE"
untrusted = True
def init(self):
12 years ago
self.help = _("set the device for autopartition")
self.label = _("Devices for install")
def get(self):
9 years ago
choiceVal = map(lambda x: x[0], self.choice())
if len(choiceVal) == 1:
return [choiceVal[0]]
return []
def choice(self):
9 years ago
deviceParentMap = dict(self.ZipVars('os_device_dev', 'os_device_name'))
return map(lambda x: (x, "%s (%s)" % (x,
deviceParentMap.get(x, _(
"Unknown")))),
self.Get('os_device_dev'))
9 years ago
def checkNeeded(self, valuelist, usedDevices, agregationType):
12 years ago
needDevices = list(set(usedDevices) - set(valuelist))
if needDevices:
raise VariableError(_("Disks {selecteddisk} are part of "
9 years ago
"{agrtype}\nYou need to use {needdisk} as well or "
"clear {agrtype} manually")
.format(selecteddisk=",".join(
list(set(usedDevices) & set(valuelist))),
needdisk=",".join(needDevices),
agrtype=agregationType))
def checkOnLvm(self, valuelist):
12 years ago
disks = self.Select('os_disk_dev',
9 years ago
where='os_disk_parent', _in=valuelist)
12 years ago
vgroups = self.Select('os_lvm_vgname',
9 years ago
where='os_lvm_pvname', _in=disks)
12 years ago
lvmDisks = self.Select('os_lvm_pvname',
9 years ago
where='os_lvm_vgname', _in=vgroups)
lvmDevices = self.Select('os_disk_parent', where='os_disk_dev',
12 years ago
_in=lvmDisks)
9 years ago
self.checkNeeded(valuelist, lvmDevices, "LVM")
12 years ago
9 years ago
def checkOnRaid(self, valuelist):
12 years ago
disks = self.Select('os_disk_dev',
9 years ago
where='os_disk_parent', _in=valuelist)
raids = filter(None, self.Select('os_disk_raid',
where='os_disk_dev', _in=disks))
raidDisks = self.Select('os_disk_dev', where='os_disk_raid', _in=raids)
12 years ago
raidDevices = self.Select('os_disk_parent',
where='os_disk_dev',
_in=raidDisks)
9 years ago
self.checkNeeded(valuelist, raidDevices, "RAID")
12 years ago
9 years ago
def check(self, valuelist):
if self.Get('cl_autopartition_set') == "on":
if not valuelist:
9 years ago
raise VariableError(
_("For autopartition, please select the device"))
useDisks = self.Select('os_disk_parent',
9 years ago
where='os_disk_mount', ne='')
for value in valuelist:
if value in useDisks:
raise VariableError(
12 years ago
_("Device %s is already in use by the current "
9 years ago
"system") % value)
12 years ago
self.checkOnLvm(valuelist)
self.checkOnRaid(valuelist)
if len(valuelist) > 1 and \
9 years ago
self.Get('cl_autopartition_lvm_set') == 'off':
raise VariableError(
12 years ago
_("You should use LVM to install on more that one device"))
freeSize = int(self.Get('cl_autopartition_free_size'))
9 years ago
if freeSize < 0 and (abs(freeSize)) / (Sizes.M * 100) > 0:
availSize = int(self.Get('cl_autopartition_device_size'))
raise VariableError(
9 years ago
_("There is not enough space on this device") + "\n" +
_("{avail} available, {need} needed").format(
9 years ago
avail=humanreadableSize(availSize),
need=humanreadableSize(availSize - freeSize)))
class VariableClAutopartitionSet(Variable):
"""
Using autopartition
"""
type = "bool"
value = "off"
element = "radio"
opt = ["--autopartition", "-p"]
def init(self):
self.label = _("Partitions")
self.help = _("autopartition")
def choice(self):
9 years ago
return [("off", _("Use the current partitions")),
("on", _("Autopartition"))]
class VariableClAutopartitionBriefSet(VariableClAutopartitionSet):
def get(self):
return self.Get('cl_autopartition_set')
def uncompatible(self):
if self.Get('os_install_root_type') == 'flash':
return \
_("This option not used for Flash install")
9 years ago
class VariableClAutopartitionScheme(AutopartitionHelper, AutoPartition,
Variable):
"""
Autopartition scheme
"""
type = "choice-list"
element = "selecttable"
opt = ["--auto-scheme", "-S"]
metavalue = "AUTOPARTOPTS"
11 years ago
check_after = ["cl_autopartition_table"]
def init(self):
self.help = _("autopartition options")
12 years ago
self.label = _("Autopartition options")
def get(self):
if self.Get('os_uefi_set') == 'on':
9 years ago
return ["uefi", "swap", "data", "home"]
elif self.Get('cl_autopartition_table') == 'gpt':
9 years ago
return ["swap", "data", "home", "grub"]
else:
9 years ago
return ["swap", "data", "home"]
def choice(self):
return [
9 years ago
("swap", _("Swap partition")),
("root", _("Additional root partition")),
("data", _("/var/calculate partition")),
("home", _("Mount /var/calculate/home to /home")),
("boot", _("Separate boot partition")),
("uefi", _("Use the UEFI bootloader")),
("lvm", _("Use LVM")),
("grub", _("Create the bios_grub partition")),
]
def check(self, value):
if "uefi" in value:
if self.Get('os_uefi_set') == 'off':
raise VariableError(
9 years ago
_("Your system must be loaded in UEFI for using this "
"bootloader"))
if self.Get('os_install_arch_machine') != 'x86_64':
raise VariableError(
_("Architecture of the target system must be x86_64 "
"for using the UEFI bootloader"))
if self.Get('cl_autopartition_table') != 'gpt':
raise VariableError(
_("The partition table must be GPT for using "
"UEFI bootloader"))
11 years ago
else:
9 years ago
if self.Get(
'cl_autopartition_table') == 'gpt' and not "grub" in value:
11 years ago
raise VariableError(
_("A 'bios_grub' partition is needed to install grub"))
if "grub" in value:
if self.Get('cl_autopartition_table') != 'gpt':
raise VariableError(
9 years ago
_(
"The bios_grub partition need the partition table to be GPT"))
class VariableClAutopartitionRootSizeDefault(Variable):
"""
Размер root раздела при авторазметке
"""
9 years ago
value = str(Sizes.G * 15)
class VariableClAutopartitionRootSizeMin(Variable):
"""
Минимальнй размер root раздела
"""
9 years ago
value = str(Sizes.G * 7)
9 years ago
class VariableClAutopartitionRootSize(SizeHelper, AutopartitionHelper,
Variable):
"""
Root partition size for autopartition
"""
type = "size-m"
opt = ["--root-size"]
metavalue = "SIZE"
untrusted = True
def init(self):
9 years ago
self.label = _("Root partition size") + " (MB)"
self.help = _("set the root partition size for autopartition")
def get(self):
size = int(self.Get('cl_autopartition_root_size_default'))
deviceSize = int(self.Get('cl_autopartition_device_size'))
minRootSize = int(self.Get('cl_autopartition_root_size_min'))
if size >= deviceSize:
size = max(deviceSize, minRootSize)
return str(size)
9 years ago
def check(self, value):
minRootSize = int(self.Get('cl_autopartition_root_size_min'))
9 years ago
if (self.Get('cl_autopartition_device') and
self.Get('cl_autopartition_set') == "on"):
if int(value) < minRootSize:
raise VariableError(
_("The root partition should be at least {size}").format(
9 years ago
size="%s Gb" % (Sizes().to_G(minRootSize))))
def humanReadable(self):
return humanreadableSize(int(self.Get()))
9 years ago
class VariableClAutopartitionTable(AutopartitionHelper, Variable):
"""
Partition table for autopartition
"""
type = "choice"
value = "gpt"
opt = ["--partition-table", "-T"]
metavalue = "TABLE"
def init(self):
self.label = _("Partition table")
self.help = _("set the partition table for autopartition")
def choice(self):
9 years ago
return [("dos", "DOS-type Partition Table"),
("gpt", "GUID Partition Table (GPT)")]
class VariableClAutopartitionDiskData(ReadonlyTableVariable):
"""
New partition data for autopart device
"""
source = ['cl_autopartition_disk_dev',
'cl_autopartition_disk_mount',
'cl_autopartition_disk_format',
'cl_autopartition_disk_size',
'cl_autopartition_disk_part',
'cl_autopartition_disk_scheme']
9 years ago
class VariableClAutopartitionBiosGrubSet(ReadonlyVariable):
"""
Create bios_grub partition
"""
type = "bool"
def get(self):
return "on" if "grub" in self.Get('cl_autopartition_scheme') else "off"
class VariableClAutopartitionLvmSet(ReadonlyVariable):
"""
Using LVM for autopartition
"""
type = "bool"
def get(self):
return "on" if "lvm" in self.Get('cl_autopartition_scheme') else "off"
9 years ago
class VariableClAutopartitionUefiSet(ReadonlyVariable):
"""
Using UEFI bootloader
"""
type = "bool"
def get(self):
return "on" if "uefi" in self.Get('cl_autopartition_scheme') else "off"
class VariableClAutopartitionLvmVgname(Variable):
"""
Volume group name for LVM autopartition
"""
def get(self):
def generateName(startName):
yield startName
for i in count(2):
9 years ago
yield "%s%d" % (startName, i)
for name in generateName("calculate"):
9 years ago
disks = self.Select('os_lvm_pvname', where='os_lvm_vgname', eq=name)
devices = self.Select('os_disk_parent',
9 years ago
where='os_disk_dev', _in=disks)
if set(devices) <= set(self.Get('cl_autopartition_device')):
return name
9 years ago
class VariableClAutopartitionDiskDev(AutopartitionHelper, ReadonlyVariable):
"""
Autopartition virtual disk on device
"""
type = "list"
9 years ago
def generateDisks(self, dos=False, devices=(), scheme=(), number=0):
"""
Generate disks for automount scheme
"""
number = 1
for line in self.deviceOpts(scheme):
# for dos 4 - extension
# for gpt 4 - for bios_boot
9 years ago
if (number == 4
and (self.Get(
'cl_autopartition_bios_grub_set') == 'on' or dos)):
number += 1
9 years ago
yield "%s%d" % (devices[0], number)
number += 1
9 years ago
def generateLvm(self, scheme=(), devices=(), **kwargs):
vgname = self.Get('cl_autopartition_lvm_vgname')
number = 1
9 years ago
for line in map(lambda x: {'root': 'root2',
'root2': 'root'}.get(x, x),
self.deviceOpts(scheme)):
if line in ("boot", "uefi"):
if (number == 4 and
self.Get('cl_autopartition_bios_grub_set') == 'on'):
number += 1
9 years ago
yield "%s%d" % (devices[0], number)
number += 1
else:
9 years ago
yield "/dev/%s/%s" % (vgname, line)
def diskGenerator(self):
if self.Get('cl_autopartition_lvm_set') == 'on':
return self.generateLvm
else:
return self.generateDisks
def get(self):
if self.Get('cl_autopartition_set') == "on":
scheme = self.getAutopartitionScheme()
devices = self.Get('cl_autopartition_device')
if devices:
res = list(self.diskGenerator()(
9 years ago
devices=devices,
scheme=scheme,
dos=self.Get('cl_autopartition_table') == 'dos'))
return res
return []
9 years ago
class VariableClAutopartitionDiskScheme(AutopartitionHelper, ReadonlyVariable):
"""
Назначение раздела относительно схемы авторазметки
"""
type = "list"
9 years ago
def generateScheme(self, scheme):
"""
Generate mount points for automount scheme
"""
for line in self.deviceOpts(scheme):
yield line
def get(self):
if self.Get('cl_autopartition_set') == "on":
scheme = self.getAutopartitionScheme()
device = self.Get('cl_autopartition_device')
if device:
return list(self.generateScheme(scheme))
return []
9 years ago
class VariableClAutopartitionDiskMount(AutopartitionHelper, ReadonlyVariable):
"""
Autopartition mount points
"""
type = "list"
9 years ago
def generateMounts(self, scheme):
"""
Generate mount points for automount scheme
"""
for line in self.deviceOpts(scheme):
yield self.mpByOpts(line)
def get(self):
if self.Get('cl_autopartition_set') == "on":
scheme = self.getAutopartitionScheme()
device = self.Get('cl_autopartition_device')
if device:
return list(self.generateMounts(scheme))
return []
9 years ago
class VariableClAutopartitionDiskFormat(AutopartitionHelper, ReadonlyVariable):
"""
Autopartition disk filesystem
"""
type = "list"
9 years ago
def generateFormat(self, scheme):
"""
Generate filesystems for automount scheme
"""
for line in self.deviceOpts(scheme):
9 years ago
if line == "swap":
yield "swap"
elif line == "uefi":
yield "vfat"
else:
yield FileSystemManager.defaultFS['hdd']
def get(self):
if self.Get('cl_autopartition_set') == "on":
scheme = self.getAutopartitionScheme()
device = self.Get('cl_autopartition_device')
if device:
return list(self.generateFormat(scheme))
return []
9 years ago
class VariableClAutopartitionDiskPart(AutopartitionHelper, ReadonlyVariable):
"""
Autopartition partition type (primary,extended,logical,gpt)
"""
type = "list"
9 years ago
def generatePart(self, scheme, dos):
"""
Generate part type for automount scheme
"""
number = 1
for line in self.deviceOpts(scheme):
if dos:
9 years ago
if number < 4:
yield "primary"
else:
yield "logical"
number += 1
else:
yield "gpt"
def get(self):
if self.Get('cl_autopartition_set') == "on":
scheme = self.getAutopartitionScheme()
table = self.Get('cl_autopartition_table')
if scheme:
return list(self.generatePart(scheme,
9 years ago
self.Get(
'cl_autopartition_table') == 'dos'))
return []
9 years ago
class VariableClAutopartitionDiskType(AutopartitionHelper, ReadonlyVariable):
"""
Autopartition partition scheme (simple - disk-partition)
"""
type = "list"
9 years ago
def get(self):
if self.Get('cl_autopartition_set') == "on":
9 years ago
if self.Get('cl_autopartition_lvm_set') == "on":
diskType = "LVM"
else:
diskType = "disk-partition"
9 years ago
return [diskType] * len(self.Get('cl_autopartition_disk_dev'))
return []
9 years ago
class VariableClAutopartitionDiskSize(AutopartitionHelper, ReadonlyVariable):
"""
Autopartition disk size
"""
type = "list"
9 years ago
def generateSize(self, scheme, memory, bootsize, uefisize, rootsize,
availsize, minrootsize):
args = {'swap': memory,
'root': rootsize,
'root2': rootsize,
'boot': bootsize,
'uefi': uefisize,
9 years ago
}
minrootsize = int(minrootsize)
deviceOpts = list(self.deviceOpts(scheme))
for line in deviceOpts[:-1]:
9 years ago
availsize -= int(args.get(line, 0))
yield str(args.get(line, 0))
if "root" in deviceOpts[-1] and availsize < minrootsize:
9 years ago
yield str(minrootsize)
elif availsize < 1 * Sizes.G:
yield str(1 * Sizes.G)
else:
yield "allfree"
def get(self):
if self.Get('cl_autopartition_set') == "on":
scheme = self.getAutopartitionScheme()
device = self.Get('cl_autopartition_device')
availSize = self.Get('cl_autopartition_device_size')
if device:
return list(self.generateSize(scheme,
9 years ago
self.Get(
'cl_autopartition_swap_size'),
self.Get(
'cl_autopartition_boot_size'),
self.Get(
'cl_autopartition_uefi_size'),
self.Get(
'cl_autopartition_root_size'),
int(availSize),
self.Get(
'cl_autopartition_root_size_min')
))
return []
def humanReadable(self):
allSize = self.Get('cl_autopartition_free_size')
return map(humanreadableSize,
9 years ago
map(lambda x: allSize if x == "allfree" else x,
self.Get()))
class VariableClAutopartitionUefiSize(Variable):
"""
Size of EF00 partition
"""
9 years ago
value = str(200 * Sizes.M)
class VariableClAutopartitionBootSize(Variable):
"""
Size of boot partition
"""
9 years ago
value = str(512 * Sizes.M)
class VariableClAutopartitionBiosGrubSize(Variable):
"""
Размер раздела bios_grub для авторазметки
"""
9 years ago
value = str(10 * Sizes.M)
class VariableClAutopartitionDeviceSize(ReadonlyVariable):
"""
Available devices size
"""
9 years ago
def get(self):
devices = self.Get('cl_autopartition_device')
if not devices:
return '0'
9 years ago
sizeDevice = map(int, self.Select('os_device_size',
where='os_device_dev',
_in=devices))
# TODO: remove set 10G
9 years ago
# return str(1024*1024*1024*10)
return str(reduce(operator.add, sizeDevice, 0))
class VariableClAutopartitionFreeSize(ReadonlyVariable):
"""
Freesize of device with current root_size and memory
"""
type = "int"
def get(self):
sizeDevice = int(self.Get('cl_autopartition_device_size'))
sizes = self.Get('cl_autopartition_disk_size')
9 years ago
return str(reduce(lambda x, y: (x - int(y)) if y.isdigit() else x,
sizes, int(sizeDevice)))
class VariableClAutopartitionBindData(ReadonlyTableVariable):
"""
Autopartition bind data
"""
source = ['cl_autopartition_bind_path',
'cl_autopartition_bind_mountpoint']
9 years ago
class VariableClAutopartitionBindPath(AutopartitionHelper, ReadonlyVariable):
"""
Autopartition bind points
"""
type = "list"
9 years ago
def generatePath(self, scheme):
for line in self.bindOpts(scheme):
yield self.sourceMpByOpts(line)
def get(self):
if self.Get('cl_autopartition_set') == "on":
scheme = self.getAutopartitionScheme()
device = self.Get('cl_autopartition_device')
return list(self.generatePath(scheme))
return []
9 years ago
class VariableClAutopartitionBindMountpoint(AutopartitionHelper,
ReadonlyVariable):
"""
Autopartition bind points
"""
type = "list"
9 years ago
def generateMountPoint(self, scheme):
for line in self.bindOpts(scheme):
yield self.targetMpByOpts(line)
def get(self):
if self.Get('cl_autopartition_set') == "on":
scheme = self.getAutopartitionScheme()
device = self.Get('cl_autopartition_device')
return list(self.generateMountPoint(scheme))
return []