Converted the lib to a pip supported structure to make packaging easier. Also tweaked some minor issues and added the AUR function

This commit is contained in:
Anton Hvornum 2020-07-06 18:44:42 +02:00
parent ff9475ffe4
commit b4a6f03b96
14 changed files with 147 additions and 81 deletions

View File

@ -1,57 +0,0 @@
import os, stat
from exceptions import *
from helpers.disk import *
from helpers.general import *
from helpers.user_interaction import *
class Installer():
def __init__(self, partition, *, profile=None, mountpoint='/mnt', hostname='ArchInstalled'):
self.profile = profile
self.hostname = hostname
self.mountpoint = mountpoint
self.partition = partition
def __enter__(self, *args, **kwargs):
self.partition.mount(self.mountpoint)
return self
def __exit__(self, *args, **kwargs):
# b''.join(sys_command(f'sync')) # No need to, since the underlaying fs() object will call sync.
# TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager
if len(args) >= 2 and args[1]:
raise args[1]
return True
def minimal_installation(self):
if (sync_mirrors := sys_command('/usr/bin/pacman -Syy')).exit_code == 0:
if (pacstrap := sys_command(f'/usr/bin/pacstrap {self.mountpoint} base base-devel linux linux-firmware btrfs-progs efibootmgr nano wpa_supplicant dialog')).exit_code == 0:
return True
else:
log(f'Could not strap in base: {pacstrap.exit_code}')
else:
log(f'Could not sync mirrors: {sync_mirrors.exit_code}')
def add_bootloader(self, partition):
os.makedirs(f'{self.mountpoint}/boot', exist_ok=True)
partition.mount(f'{self.mountpoint}/boot')
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.mountpoint} bootctl --no-variables --path=/boot install'))
with open('/mnt/boot/loader/loader.conf', 'w') as loader:
loader.write('default arch\n')
loader.write('timeout 5\n')
## For some reason, blkid and /dev/disk/by-uuid are not getting along well.
## And blkid is wrong in terms of LUKS.
#UUID = sys_command('blkid -s PARTUUID -o value {drive}{partition_2}'.format(**args)).decode('UTF-8').strip()
with open('/mnt/boot/loader/entries/arch.conf', 'w') as entry:
entry.write('title Arch Linux\n')
entry.write('linux /vmlinuz-linux\n')
entry.write('initrd /initramfs-linux.img\n')
## blkid doesn't trigger on loopback devices really well,
## so we'll use the old manual method until we get that sorted out.
# UUID = simple_command(f"blkid -s PARTUUID -o value /dev/{os.path.basename(args['drive'])}{args['partitions']['2']}").decode('UTF-8').strip()
# entry.write('options root=PARTUUID={UUID} rw intel_pstate=no_hwp\n'.format(UUID=UUID))
UUID = b''.join(sys_command(f"ls -l /dev/disk/by-uuid/ | grep {os.path.basename(partition.path)} | awk '{{print $9}}'")).decode('UTF-8').strip()
entry.write(f'options cryptdevice=UUID={UUID}:luksdev root=/dev/mapper/luksdev rw intel_pstate=no_hwp\n')

5
archinstall/__init__.py Normal file
View File

@ -0,0 +1,5 @@
from .lib.general import *
from .lib.disk import *
from .lib.user_interaction import *
from .lib.exceptions import *
from .lib.installer import *

View File

@ -1,12 +1,13 @@
import glob, re, os, json
from collections import OrderedDict
#import ctypes
#import ctypes.util
from exceptions import *
from helpers.general import sys_command
from .exceptions import *
from .general import sys_command
ROOT_DIR_PATTERN = re.compile('^.*?/devices')
GPT = 0b00000001
#import ctypes
#import ctypes.util
#libc = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True)
#libc.mount.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_ulong, ctypes.c_char_p)
@ -89,6 +90,7 @@ class Partition():
return f'Partition({self.path}, fs={self.filesystem}, mounted={self.mountpoint})'
def format(self, filesystem):
print(f'Formatting {self} -> {filesystem}')
if filesystem == 'btrfs':
o = b''.join(sys_command(f'/usr/bin/mkfs.btrfs -f {self.path}'))
if not b'UUID' in o:
@ -104,6 +106,7 @@ class Partition():
return True
def mount(self, target, fs=None, options=''):
print(f'Mounting {self} to {target}')
if not fs:
if not self.filesystem: raise DiskError(f'Need to format (or define) the filesystem on {self} before mounting.')
fs = self.filesystem
@ -127,9 +130,11 @@ class luks2():
# TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager
if len(args) >= 2 and args[1]:
raise args[1]
print(args)
return True
def encrypt(self, partition, password, key_size=512, hash_type='sha512', iter_time=10000, key_file=None):
print(f'Encrypting {partition}')
if not key_file: key_file = f'/tmp/{os.path.basename(self.filesystem.blockdevice.device)}.disk_pw' #TODO: Make disk-pw-file randomly unique?
if type(password) != bytes: password = bytes(password, 'UTF-8')
@ -177,7 +182,12 @@ class Filesystem():
raise DiskError(f'Unknown mode selected to format in: {self.mode}')
def __exit__(self, *args, **kwargs):
# TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager
if len(args) >= 2 and args[1]:
raise args[1]
print(args)
b''.join(sys_command(f'sync'))
return True
def raw_parted(self, string:str):
x = sys_command(f'/usr/bin/parted -s {string}')
@ -204,6 +214,7 @@ class Filesystem():
self.add_partition('primary', start='1MiB', end='513MiB', format='ext4')
def add_partition(self, type, start, end, format=None):
print(f'Adding partition to {self.blockdevice}')
if format:
return self.parted(f'{self.blockdevice.device} mkpart {type} {format} {start} {end}') == 0
else:

View File

@ -0,0 +1,104 @@
import os, stat
from .exceptions import *
from .disk import *
from .general import *
from .user_interaction import *
class Installer():
def __init__(self, partition, *, profile=None, mountpoint='/mnt', hostname='ArchInstalled'):
self.profile = profile
self.hostname = hostname
self.mountpoint = mountpoint
self.partition = partition
def __enter__(self, *args, **kwargs):
self.partition.mount(self.mountpoint)
return self
def __exit__(self, *args, **kwargs):
# b''.join(sys_command(f'sync')) # No need to, since the underlaying fs() object will call sync.
# TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager
if len(args) >= 2 and args[1]:
raise args[1]
print(args)
return True
def pacstrap(self, *packages):
if type(packages[0]) in (list, tuple): packages = packages[0]
print(f'Installing packages: {packages}')
if (sync_mirrors := sys_command('/usr/bin/pacman -Syy')).exit_code == 0:
if (pacstrap := sys_command(f'/usr/bin/pacstrap {self.mountpoint} {" ".join(packages)}')).exit_code == 0:
return True
else:
print(f'Could not strap in packages: {pacstrap.exit_code}')
else:
print(f'Could not sync mirrors: {sync_mirrors.exit_code}')
def minimal_installation(self):
return self.pacstrap('base base-devel linux linux-firmware btrfs-progs efibootmgr nano wpa_supplicant dialog'.split(' '))
def add_bootloader(self, partition):
print(f'Adding bootloader to {partition}')
os.makedirs(f'{self.mountpoint}/boot', exist_ok=True)
partition.mount(f'{self.mountpoint}/boot')
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.mountpoint} bootctl --no-variables --path=/boot install'))
with open(f'{self.mountpoint}/boot/loader/loader.conf', 'w') as loader:
loader.write('default arch\n')
loader.write('timeout 5\n')
## For some reason, blkid and /dev/disk/by-uuid are not getting along well.
## And blkid is wrong in terms of LUKS.
#UUID = sys_command('blkid -s PARTUUID -o value {drive}{partition_2}'.format(**args)).decode('UTF-8').strip()
with open(f'{self.mountpoint}/boot/loader/entries/arch.conf', 'w') as entry:
entry.write('title Arch Linux\n')
entry.write('linux /vmlinuz-linux\n')
entry.write('initrd /initramfs-linux.img\n')
## blkid doesn't trigger on loopback devices really well,
## so we'll use the old manual method until we get that sorted out.
# UUID = simple_command(f"blkid -s PARTUUID -o value /dev/{os.path.basename(args['drive'])}{args['partitions']['2']}").decode('UTF-8').strip()
# entry.write('options root=PARTUUID={UUID} rw intel_pstate=no_hwp\n'.format(UUID=UUID))
for root, folders, uids in os.walk('/dev/disk/by-uuid'):
for uid in uids:
real_path = os.path.realpath(os.path.join(root, uid))
if not os.path.basename(real_path) == os.path.basename(partition.path): continue
entry.write(f'options cryptdevice=UUID={uid}:luksdev root=/dev/mapper/luksdev rw intel_pstate=no_hwp\n')
return True
break
raise RequirementError(f'Could not identify the UUID of {partition}, there for {self.mountpoint}/boot/loader/entries/arch.conf will be broken until fixed.')
def add_additional_packages(self, *packages):
self.pacstrap(*packages)
def install_profile(self, profile):
print(f'[STUB] Installing network profile {profile}')
pass
def user_create(self, user :str, password=None, groups=[]):
print(f'Creating user {user}')
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.mountpoint} useradd -m -G wheel {user}'))
if password:
self.user_set_pw(user, password)
if groups:
for group in groups:
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.mountpoint} gpasswd -a {user} {group}'))
def user_set_pw(self, user, password):
print(f'Setting password for {user}')
o = b''.join(sys_command(f"/usr/bin/arch-chroot {self.mountpoint} sh -c \"echo '{user}:{password}' | chpasswd\""))
pass
def add_AUR_support(self):
print(f'Building and installing yay support into {self.mountpoint}')
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.mountpoint} sh -c "useradd -m -G wheel aibuilder"'))
o = b''.join(sys_command(f"/usr/bin/sed -i 's/# %wheel ALL=(ALL) NO/%wheel ALL=(ALL) NO/' {self.mountpoint}/etc/sudoers"))
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.mountpoint} sh -c "su - aibuilder -c \\"(cd /home/aibuilder; git clone https://aur.archlinux.org/yay.git)\\""'))
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.mountpoint} sh -c "chown -R aibuilder.aibuilder /home/aibuilder/yay"'))
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.mountpoint} sh -c "su - aibuilder -c \\"(cd /home/aibuilder/yay; makepkg -si --noconfirm)\\" >/dev/null"'))
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.mountpoint} sh -c "userdel aibuilder; rm -rf /hoem/aibuilder"'))

View File

@ -1,4 +1,4 @@
from exceptions import *
from .exceptions import *
def select_disk(dict_o_disks):
drives = sorted(list(dict_o_disks.keys()))

View File

Before

Width:  |  Height:  |  Size: 94 KiB

After

Width:  |  Height:  |  Size: 94 KiB

View File

Before

Width:  |  Height:  |  Size: 44 KiB

After

Width:  |  Height:  |  Size: 44 KiB

View File

@ -10,25 +10,20 @@ harddrive = archinstall.all_disks()['/dev/loop0']
disk_password = '1234' # getpass.getpass(prompt='Disk password (won\'t echo): ')
with archinstall.Filesystem(harddrive, archinstall.GPT) as fs:
print(f'Formatting {harddrive}')
fs.use_entire_disk('luks2')
with archinstall.luks2(fs) as crypt:
if harddrive.partition[1].size == '512M':
raise OSError('Trying to encrypt the boot partition for petes sake..')
print(f'Encrypting {harddrive.partition[1]}')
key_file = crypt.encrypt(harddrive.partition[1], password=disk_password, key_size=512, hash_type='sha512', iter_time=10000, key_file='./pwfile')
unlocked_device = crypt.unlock(harddrive.partition[1], 'luksloop', key_file)
print('Formatting partitions.')
harddrive.partition[0].format('fat32')
unlocked_device.format('btrfs')
with archinstall.Installer(unlocked_device, hostname='testmachine') as installation:
print('Installing minimal installation to disk.')
if installation.minimal_installation():
print('Adding bootloader.')
installation.add_bootloader(harddrive.partition[0])
installation.add_additional_packages(['nano', 'wget', 'git'])

22
setup.py Normal file
View File

@ -0,0 +1,22 @@
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="archinstall-Torxed", # Replace with your own username
version="2.0.0",
author="Anton Hvornum",
author_email="anton@hvornum.se",
description="Arch Linux installer - guided, templates etc.",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/Torxed/archinstall",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GPL 3.0 License",
"Operating System :: Arch Linux",
],
python_requires='>=3.8',
)

View File

@ -1,14 +0,0 @@
import json
import archinstall
archinstall.update_drive_list(emulate=False)
archinstall.setup_args_defaults(archinstall.args, interactive=False)
#for drive in archinstall.harddrives:
# print(drive, archinstall.human_disk_info(drive))
instructions = archinstall.load_automatic_instructions(emulate=False)
profile_instructions = archinstall.get_instructions('workstation', emulate=False)
profile_instructions = archinstall.merge_in_includes(profile_instructions, emulate=False)
archinstall.args['password'] = 'test'
print(json.dumps(archinstall.args, indent=4))