%pre script used for automatically determining partition sizes for kickstart
–Validated for AlmaLinux 9.4, should also work for Almalinux 8
%pre --interpreter=/usr/libexec/platform-python
#
#
from pathlib import Path
import parted as Part
import os.path as Ospath
DEBUG=False
DEBUG=True
def SizeGB(dev):
s = dev.as_posix()
with open(s+'/size') as fd:
sz=int(fd.read())//2**21
return(sz)
if DEBUG:
print('\n\n*************************************************************\n')
paths = Path('/sys/block').glob('*')
lines = list(paths)
lines.sort() # this will prioritze NVME #sort in place
sizes = {}
for l in lines:
n=l.name
sizes[n] = SizeGB(l)
if DEBUG:
print(sizes)
print('\n\n*************************************************************\n')
devs = {}
#print(lines)
paths= Path('/dev/disk/by-label').glob('*')
lines = list(paths)
for l in lines:
s=l.as_posix()
if s.startswith('/dev/disk/by-label/ALMALINUX'):
Source = l
source = l.resolve().as_posix() # partition of source
d,s = Ospath.split(source)
sourceDevice = source.rstrip('0123456789') # will be something like sde
#for d,s in devs.items():
# print(d,s)
if DEBUG:
print(sourceDevice)
# only look at targets that are not usb
targets = {}
ignore = {}
ipaths = list(Path('/dev/disk/by-id').glob('*'))
#print(ipaths)
for i in range(len(ipaths)):
p = ipaths[i]
l=p.as_posix()
rp = p.resolve()
if not(l.endswith(('-part','-par'),-7,-2)):
print(l,' >> ',str(rp))
if 'usb-' in l:
ignore[rp.name] = rp
continue
if sizes[rp.name] < 200:
ignore[rp.name] = rp
continue
targets[rp.name] = rp
rem =[]
for name in targets.keys():
if name in ignore.keys():
print('eliminate %s from targets'%name)
rem.append(name) # use list to keep track, will remove next block
for name in rem:
n = targets.pop(name)
if DEBUG:
print('\n-------------------------------------------------------------\n')
print(targets)
print('\n###########################################################\n')
print(ignore)
#
# now have a small bios level set of devices to install to.
# I have deliberatly removed usb devices, as they
# cannot be depended on to remain with the system.
# They are at least 200GiB in size.
#
# choose the smallest. or the first in line if al equal, for root/boot
size = 1e12
mxsz =0
homedev=None
dest = None
count =0
for d in targets.keys():
if sizes[d] < size:
size = sizes[d]
dest = targets[d]
dindex = d
if sizes[d] >= mxsz:
mxsz = sizes[d]
homedev = targets[d]
count +=1
# determine swap size
with open('/proc/meminfo') as fd:
l=fd.readline()
ll = l.split()
sz=int(ll[1])//1e6 # GB ram,
# allocate min 4GB for swap, to a max of 24GB mem+swap
# with swap no larger than 2x memory
# this limits swap to 16Gb at 8Gb ram
# minimal swap in low disk size limit
# if disk space is not an issue, limit at mem/2
swap = min(max(4,(24-sz)),2*sz) # in Gb
maxopt=''
if(homedev == dest):
maxopt = '--maxsize=131072 '
if (mxsz >400) or (homedev != dest):
swap = max(swap,sz//2)
if DEBUG:
print('destination disk',dest,' size:',sizes[dindex],'GiB')
print('home: ',homedev,' swap: ',swap)
diskmb = sizes[dindex]*2**10 # disk size in MiB
# sizes in MB
'''
biosboot=2
#swap calc'd above, max 32GiB
boot=2 000 # 2GB
bootefi=400
root=48 000 153 600
usr=48 000 200 000
var=32 000 150 000
log=48 000
vartmp=20 000
audit=20 000
tmp=20 000
opt=32 000 150 000
home=10 000 remander
'''
#
ign = list(ignore.keys())
sd =[]
for n in ign:
if n[1] == 'd':
sd.append(n)
sd_str= ','.join(sd)
##
fd = sys.stdout
print('ignoredisk --drives=%s'%(sd_str),file=fd)
print('zerombr',file=fd)
print('bootloader --location=mbr --drive=%s'%(dest),file=fd)
if homedev != dest:
print('clearpart --all --initlabel --disklabel=gpt --drives=%s,%s'%(dest,homedev),file=fd)
else:
print('clearpart --all --initlabel --disklabel=gpt --drives=%s'%(dest),file=fd)
# Disk partitioning information
print('part biosboot --fstype="biosboot" --size=2 --ondisk=%s'%(dest),file=fd)
print('part /boot/efi --fstype="efi" --size=512 --ondisk=%s'%(dest),file=fd)
print('part swap --fstype="swap" --hibernation --maxsize=%d --ondisk=%s'%(swap,dest),file=fd)
print('part /boot --fstype="xfs" --label=BOOT --size=2048 --ondisk=%s'%(dest),file=fd)
print('part / --fstype="xfs" --label=ROOT --size=24568 --maxsize=131072 --grow --ondisk=%s'%(dest),file=fd)
print('part /usr --fstype="xfs" --label=USR --size=49152 --maxsize=226815 --grow --ondisk=%s'%(dest),file=fd)
print('part /var --fstype="xfs" --label=VAR --size=20480 --maxsize=163840 --grow --ondisk=%s'%(dest),file=fd)
print('part /var/log --fstype="xfs" --label=VAR --size=20480 --maxsize=40960 --grow --ondisk=%s'%(dest),file=fd)
print('part /tmp --fstype="xfs" --label=TMP --size=10240 --maxsize=32768 --grow --ondisk=%s'%(dest),file=fd)
print('part /var/tmp --fstype="xfs" --label=VARTMP --size=10240 --maxsize=32768 --grow --ondisk=%s'%(dest),file=fd)
print('part /var/log/audit --fstype="xfs" --label=AUDIT --size=10240 --maxsize=20480 --grow --ondisk=%s'%(dest),file=fd)
print('part /opt --fstype="xfs" --label=OPT --size=10240 %s--grow --ondisk=%s'%(maxopt,dest),file=fd) # dump rest of disk in /opt if home on separte device
print('part /home --fstype="xfs" --label=HOME --size=10240 --grow --ondisk=%s'%(homedev),file=fd)
#
ign = list(ignore.keys())
sd =[]
for n in ign:
if n[1] == 'd':
sd.append(n)
sd_str= ','.join(sd)
##
fd = open('/tmp/parts.cfg',mode='w')
print('ignoredisk --drives=%s'%(sd_str),file=fd)
print('zerombr',file=fd)
print('bootloader --location=mbr --drive=%s'%(dest),file=fd)
if homedev != dest:
print('clearpart --all --initlabel --disklabel=gpt --drives=%s,%s'%(dest,homedev),file=fd)
else:
print('clearpart --all --initlabel --disklabel=gpt --drives=%s'%(dest),file=fd)
# Disk partitioning information
print('part biosboot --fstype="biosboot" --size=2 --ondisk=%s'%(dest),file=fd)
print('part /boot/efi --fstype="efi" --size=512 --ondisk=%s'%(dest),file=fd)
print('part swap --fstype="swap" --hibernation --maxsize=%d --ondisk=%s'%(swap,dest),file=fd)
print('part /boot --fstype="xfs" --label=BOOT --size=2048 --ondisk=%s'%(dest),file=fd)
print('part / --fstype="xfs" --label=ROOT --size=24568 --maxsize=131072 --grow --ondisk=%s'%(dest),file=fd)
print('part /usr --fstype="xfs" --label=USR --size=49152 --maxsize=226815 --grow --ondisk=%s'%(dest),file=fd)
print('part /var --fstype="xfs" --label=VAR --size=20480 --maxsize=163840 --grow --ondisk=%s'%(dest),file=fd)
print('part /var/log --fstype="xfs" --label=VAR --size=20480 --maxsize=40960 --grow --ondisk=%s'%(dest),file=fd)
print('part /tmp --fstype="xfs" --label=TMP --size=10240 --maxsize=32768 --grow --ondisk=%s'%(dest),file=fd)
print('part /var/tmp --fstype="xfs" --label=VARTMP --size=10240 --maxsize=32768 --grow --ondisk=%s'%(dest),file=fd)
print('part /var/log/audit --fstype="xfs" --label=AUDIT --size=10240 --maxsize=20480 --grow --ondisk=%s'%(dest),file=fd)
print('part /opt --fstype="xfs" --label=OPT --size=10240 %s--grow --ondisk=%s'%(maxopt,dest),file=fd) # dump rest of disk in /opt if home on separte device
print('part /home --fstype="xfs" --label=HOME --size=10240 --grow --ondisk=%s'%(homedev),file=fd)
fd.close()
%end