Merge PR #113 into torxed-v2.2.0 feature branch.

This merge should add BIOS through MBR support and GRUB.
This commit is contained in:
Anton Hvornum 2021-04-02 10:42:26 +02:00
commit 65ec50dae5
13 changed files with 148 additions and 56 deletions

View File

@ -2,7 +2,7 @@
# Contributor: Anton Hvornum anton@hvornum.se
pkgname="archinstall-bin"
pkgver="2.1.2"
pkgver="2.1.3"
pkgdesc="Installs a pre-built binary of ${pkgname}"
pkgrel=1
url="https://github.com/Torxed/archinstall"

View File

@ -2,7 +2,7 @@
# Contributor: demostanis worlds <demostanis@protonmail.com>
pkgname="archinstall"
pkgver="2.1.2"
pkgver="2.1.3"
pkgdesc="Installs launcher scripts for archinstall"
pkgrel=1
url="https://github.com/Torxed/archinstall"

View File

@ -2,7 +2,7 @@
# Contributor: demostanis worlds <demostanis@protonmail.com>
pkgname="python-archinstall"
pkgver="2.1.2"
pkgver="2.1.3"
pkgdesc="Installs ${pkgname} as a python library."
pkgrel=1
url="https://github.com/Torxed/archinstall"

View File

@ -57,10 +57,10 @@ with archinstall.Filesystem(harddrive, archinstall.GPT) as fs:
This installer will perform the following:
* Prompt the user to select a disk and disk-password
* Proceed to wipe the selected disk with a `GPT` partition table.
* Proceed to wipe the selected disk with a `GPT` partition table on a UEFI system and MBR on a bios system.
* Sets up a default 100% used disk with encryption.
* Installs a basic instance of Arch Linux *(base base-devel linux linux-firmware btrfs-progs efibootmgr)*
* Installs and configures a bootloader to partition 0.
* Installs and configures a bootloader to partition 0 on uefi. on bios it sets the root to partition 0.
* Install additional packages *(nano, wget, git)*
* Installs a network-profile called [workstation](https://github.com/Torxed/archinstall/blob/master/profiles/workstation.json) *(more on network profiles in the docs)*

View File

@ -1 +1 @@
2.1.2
2.1.3

View File

@ -1,10 +1,11 @@
import glob, re, os, json, time, hashlib
import pathlib
import pathlib, traceback
from collections import OrderedDict
from .exceptions import DiskError
from .general import *
from .output import log, LOG_LEVELS
from .storage import storage
from .hardware import hasUEFI
ROOT_DIR_PATTERN = re.compile('^.*?/devices')
GPT = 0b00000001
@ -107,7 +108,7 @@ class BlockDevice():
if part_id not in self.part_cache:
## TODO: Force over-write even if in cache?
if part_id not in self.part_cache or self.part_cache[part_id].size != part['size']:
self.part_cache[part_id] = Partition(root_path + part_id, part_id=part_id, size=part['size'])
self.part_cache[part_id] = Partition(root_path + part_id, self, part_id=part_id, size=part['size'])
return {k: self.part_cache[k] for k in sorted(self.part_cache)}
@ -133,15 +134,18 @@ class BlockDevice():
self.part_cache = OrderedDict()
class Partition():
def __init__(self, path, part_id=None, size=-1, filesystem=None, mountpoint=None, encrypted=False, autodetect_filesystem=True):
def __init__(self, path :str, block_device :BlockDevice, part_id=None, size=-1, filesystem=None, mountpoint=None, encrypted=False, autodetect_filesystem=True):
if not part_id:
part_id = os.path.basename(path)
self.block_device = block_device
self.path = path
self.part_id = part_id
self.mountpoint = mountpoint
self.target_mountpoint = mountpoint
self.filesystem = filesystem
self.size = size # TODO: Refresh?
self._encrypted = None
self.encrypted = encrypted
self.allow_formatting = False # A fail-safe for unconfigured partitions, such as windows NTFS partitions.
@ -177,14 +181,26 @@ class Partition():
elif self.target_mountpoint:
mount_repr = f", rel_mountpoint={self.target_mountpoint}"
if self.encrypted:
if self._encrypted:
return f'Partition(path={self.path}, real_device={self.real_device}, fs={self.filesystem}{mount_repr})'
else:
return f'Partition(path={self.path}, fs={self.filesystem}{mount_repr})'
@property
def encrypted(self):
return self._encrypted
@encrypted.setter
def encrypted(self, value :bool):
if value:
log(f'Marking {self} as encrypted: {value}', level=LOG_LEVELS.Debug)
log(f"Callstrack when marking the partition: {''.join(traceback.format_stack())}", level=LOG_LEVELS.Debug)
self._encrypted = value
@property
def real_device(self):
if not self.encrypted:
if not self._encrypted:
return self.path
else:
for blockdevice in json.loads(b''.join(sys_command('lsblk -J')).decode('UTF-8'))['blockdevices']:
@ -237,7 +253,7 @@ class Partition():
"""
from .luks import luks2
if not self.encrypted:
if not self._encrypted:
raise DiskError(f"Attempting to encrypt a partition that was not marked for encryption: {self}")
if not self.safe_to_format():
@ -260,6 +276,11 @@ class Partition():
if allow_formatting is None:
allow_formatting = self.allow_formatting
# To avoid "unable to open /dev/x: No such file or directory"
start_wait = time.time()
while pathlib.Path(path).exists() is False and time.time() - start_wait < 10:
time.sleep(0.025)
if not allow_formatting:
raise PermissionError(f"{self} is not formatable either because instance is locked ({self.allow_formatting}) or a blocking flag was given ({allow_formatting})")
@ -301,6 +322,12 @@ class Partition():
else:
raise UnknownFilesystemFormat(f"Fileformat '{filesystem}' is not yet implemented.")
if get_filesystem_type(path) == 'crypto_LUKS' or get_filesystem_type(self.real_device) == 'crypto_LUKS':
self.encrypted = True
else:
self.encrypted = False
return True
def find_parent_of(self, data, name, parent=None):
@ -363,7 +390,7 @@ class Filesystem():
# TODO:
# When instance of a HDD is selected, check all usages and gracefully unmount them
# as well as close any crypto handles.
def __init__(self, blockdevice, mode=GPT):
def __init__(self, blockdevice,mode):
self.blockdevice = blockdevice
self.mode = mode
@ -376,6 +403,11 @@ class Filesystem():
return self
else:
raise DiskError(f'Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel gpt')
elif self.mode == MBR:
if sys_command(f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos').exit_code == 0:
return self
else:
raise DiskError(f'Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos')
else:
raise DiskError(f'Unknown mode selected to format in: {self.mode}')
@ -416,34 +448,41 @@ class Filesystem():
"""
return self.raw_parted(string).exit_code
def use_entire_disk(self, root_filesystem_type='ext4', encrypt_root_partition=True):
def use_entire_disk(self, root_filesystem_type='ext4'):
log(f"Using and formatting the entire {self.blockdevice}.", level=LOG_LEVELS.Debug)
self.add_partition('primary', start='1MiB', end='513MiB', format='fat32')
self.set_name(0, 'EFI')
self.set(0, 'boot on')
# TODO: Probably redundant because in GPT mode 'esp on' is an alias for "boot on"?
# https://www.gnu.org/software/parted/manual/html_node/set.html
self.set(0, 'esp on')
self.add_partition('primary', start='513MiB', end='100%')
if hasUEFI():
self.add_partition('primary', start='1MiB', end='513MiB', format='fat32')
self.set_name(0, 'EFI')
self.set(0, 'boot on')
# TODO: Probably redundant because in GPT mode 'esp on' is an alias for "boot on"?
# https://www.gnu.org/software/parted/manual/html_node/set.html
self.set(0, 'esp on')
self.add_partition('primary', start='513MiB', end='100%')
self.blockdevice.partition[0].filesystem = 'vfat'
self.blockdevice.partition[1].filesystem = root_filesystem_type
log(f"Set the root partition {self.blockdevice.partition[1]} to use filesystem {root_filesystem_type}.", level=LOG_LEVELS.Debug)
self.blockdevice.partition[0].filesystem = 'vfat'
self.blockdevice.partition[1].filesystem = root_filesystem_type
log(f"Set the root partition {self.blockdevice.partition[1]} to use filesystem {root_filesystem_type}.", level=LOG_LEVELS.Debug)
self.blockdevice.partition[0].target_mountpoint = '/boot'
self.blockdevice.partition[1].target_mountpoint = '/'
self.blockdevice.partition[0].target_mountpoint = '/boot'
self.blockdevice.partition[1].target_mountpoint = '/'
self.blockdevice.partition[0].allow_formatting = True
self.blockdevice.partition[1].allow_formatting = True
if encrypt_root_partition:
log(f"Marking partition {self.blockdevice.partition[1]} as encrypted.", level=LOG_LEVELS.Debug)
self.blockdevice.partition[1].encrypted = True
self.blockdevice.partition[0].allow_formatting = True
self.blockdevice.partition[1].allow_formatting = True
else:
#we don't need a seprate boot partition it would be a waste of space
self.add_partition('primary', start='1MB', end='100%')
self.blockdevice.partition[0].filesystem=root_filesystem_type
log(f"Set the root partition {self.blockdevice.partition[0]} to use filesystem {root_filesystem_type}.", level=LOG_LEVELS.Debug)
self.blockdevice.partition[0].target_mountpoint = '/'
self.blockdevice.partition[0].allow_formatting = True
def add_partition(self, type, start, end, format=None):
log(f'Adding partition to {self.blockdevice}', level=LOG_LEVELS.Info)
previous_partitions = self.blockdevice.partitions
if self.mode == MBR:
if len(self.blockdevice.partitions)>3:
DiskError("Too many partitions on disk, MBR disks can only have 3 parimary partitions")
if format:
partitioning = self.parted(f'{self.blockdevice.device} mkpart {type} {format} {start} {end}') == 0
else:

View File

@ -1,4 +1,4 @@
import os
import os, subprocess
from .general import sys_command
from .networking import list_interfaces, enrichIfaceTypes
@ -7,6 +7,11 @@ def hasWifi():
return True
return False
def hasAMDCPU():
if subprocess.check_output("lscpu | grep AMD", shell=True).strip().decode():
return True
return False
def hasUEFI():
return os.path.isdir('/sys/firmware/efi')

View File

@ -1,4 +1,4 @@
import os, stat, time, shutil, pathlib
import os, stat, time, shutil, pathlib, subprocess
from .exceptions import *
from .disk import *
@ -71,9 +71,11 @@ class Installer():
log(*args, level=level, **kwargs)
def __enter__(self, *args, **kwargs):
self.partition.mount(self.mountpoint)
os.makedirs(f'{self.mountpoint}/boot', exist_ok=True)
self.boot_partition.mount(f'{self.mountpoint}/boot')
if hasUEFI():
# on bios we don't have a boot partition
self.partition.mount(self.mountpoint)
os.makedirs(f'{self.mountpoint}/boot', exist_ok=True)
self.boot_partition.mount(f'{self.mountpoint}/boot')
return self
def __exit__(self, *args, **kwargs):
@ -173,11 +175,19 @@ class Installer():
return True if sys_command(f'/usr/bin/arch-chroot {self.mountpoint} locale-gen').exit_code == 0 else False
def set_timezone(self, zone, *args, **kwargs):
if not len(zone): return True
if not zone: return True
if not len(zone): return True # Redundant
(pathlib.Path(self.mountpoint)/"etc"/"localtime").unlink(missing_ok=True)
sys_command(f'/usr/bin/arch-chroot {self.mountpoint} ln -s /usr/share/zoneinfo/{zone} /etc/localtime')
return True
if (pathlib.Path("/usr")/"share"/"zoneinfo"/zone).exists():
(pathlib.Path(self.mountpoint)/"etc"/"localtime").unlink(missing_ok=True)
sys_command(f'/usr/bin/arch-chroot {self.mountpoint} ln -s /usr/share/zoneinfo/{zone} /etc/localtime')
return True
else:
self.log(
f"Time zone {zone} does not exist, continuing with system default.",
level=LOG_LEVELS.Warning,
fg='red'
)
def activate_ntp(self):
self.log(f'Installing and activating NTP.', level=LOG_LEVELS.Info)
@ -325,6 +335,8 @@ class Installer():
self.log(f'Adding bootloader {bootloader} to {self.boot_partition}', level=LOG_LEVELS.Info)
if bootloader == 'systemd-bootctl':
if not hasUEFI():
raise HardwareIncompatibilityError
# TODO: Ideally we would want to check if another config
# points towards the same disk and/or partition.
# And in which case we should do some clean up.
@ -365,10 +377,12 @@ class Installer():
if self.partition.encrypted:
log(f"Identifying root partition {self.partition} to boot based on disk UUID, looking for '{os.path.basename(self.partition.real_device)}'.", level=LOG_LEVELS.Debug)
log(f"Identifying root partition by DISK-UUID on {self.partition}, looking for '{os.path.basename(self.partition.real_device)}'.", level=LOG_LEVELS.Debug)
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))
log(f"Checking root partition match {os.path.basename(real_path)} against {os.path.basename(self.partition.real_device)}: {os.path.basename(real_path) == os.path.basename(self.partition.real_device)}", level=LOG_LEVELS.Debug)
if not os.path.basename(real_path) == os.path.basename(self.partition.real_device): continue
entry.write(f'options cryptdevice=UUID={uid}:luksdev root=/dev/mapper/luksdev rw intel_pstate=no_hwp\n')
@ -377,10 +391,12 @@ class Installer():
return True
break
else:
log(f"Identifying root partition {self.partition} to boot based on partition UUID, looking for '{os.path.basename(self.partition.path)}'.", level=LOG_LEVELS.Debug)
log(f"Identifying root partition by PART-UUID on {self.partition}, looking for '{os.path.basename(self.partition.path)}'.", level=LOG_LEVELS.Debug)
for root, folders, uids in os.walk('/dev/disk/by-partuuid'):
for uid in uids:
real_path = os.path.realpath(os.path.join(root, uid))
log(f"Checking root partition match {os.path.basename(real_path)} against {os.path.basename(self.partition.path)}: {os.path.basename(real_path) == os.path.basename(self.partition.path)}", level=LOG_LEVELS.Debug)
if not os.path.basename(real_path) == os.path.basename(self.partition.path): continue
entry.write(f'options root=PARTUUID={uid} rw intel_pstate=no_hwp\n')
@ -390,6 +406,16 @@ class Installer():
break
raise RequirementError(f"Could not identify the UUID of {self.partition}, there for {self.mountpoint}/boot/loader/entries/arch.conf will be broken until fixed.")
elif bootloader == "grub-install":
if hasUEFI():
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.mountpoint} grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB'))
sys_command('/usr/bin/arch-chroot grub-mkconfig -o /boot/grub/grub.cfg')
else:
root_device = subprocess.check_output(f'basename "$(readlink -f "/sys/class/block/{self.partition.path.strip("/dev/")}/..")',shell=True).decode().strip()
if root_device == "block":
root_device = f"{self.partition.path}"
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.mountpoint} grub-install --target=--target=i386-pc /dev/{root_device}'))
sys_command('/usr/bin/arch-chroot grub-mkconfig -o /boot/grub/grub.cfg')
else:
raise RequirementError(f"Unknown (or not yet implemented) bootloader added to add_bootloader(): {bootloader}")

View File

@ -113,7 +113,7 @@ class luks2():
sys_command(f'/usr/bin/cryptsetup open {partition.path} {mountpoint} --key-file {os.path.abspath(key_file)} --type luks2')
if os.path.islink(f'/dev/mapper/{mountpoint}'):
self.mapdev = f'/dev/mapper/{mountpoint}'
unlocked_partition = Partition(self.mapdev, encrypted=True, filesystem=get_filesystem_type(self.mapdev), autodetect_filesystem=False)
unlocked_partition = Partition(self.mapdev, None, encrypted=True, filesystem=get_filesystem_type(self.mapdev), autodetect_filesystem=False)
unlocked_partition.allow_formatting = self.partition.allow_formatting
return unlocked_partition

View File

@ -24,6 +24,10 @@ def get_password(prompt="Enter a password: "):
if passwd != passwd_verification:
log(' * Passwords did not match * ', bg='black', fg='red')
continue
if len(passwd.strip()) <= 0:
break
return passwd
return None
@ -77,8 +81,14 @@ def ask_for_additional_users(prompt='Any additional users to install (leave blan
def ask_for_a_timezone():
timezone = input('Enter a valid timezone (Example: Europe/Stockholm): ').strip()
if pathlib.Path(timezone).exists():
if (pathlib.Path("/usr")/"share"/"zoneinfo"/timezone).exists():
return timezone
else:
log(
f"Time zone {timezone} does not exist, continuing with system default.",
level=LOG_LEVELS.Warning,
fg='red'
)
def ask_to_configure_network():
# Optionally configure one network interface.
@ -135,7 +145,7 @@ def ask_for_main_filesystem_format():
'f2fs' : 'f2fs'
}
value = generic_select(options.values(), "Select which filesystem your main partition should use (by number of name): ")
value = generic_select(options.values(), "Select which filesystem your main partition should use (by number or name): ")
return next((key for key, val in options.items() if val == value), None)
def generic_select(options, input_text="Select one of the above by index or absolute value: ", sort=True):
@ -160,7 +170,10 @@ def generic_select(options, input_text="Select one of the above by index or abso
if len(selected_option.strip()) <= 0:
return None
elif selected_option.isdigit():
selected_option = options[int(selected_option)]
selected_option = int(selected_option)
if selected_option >= len(options):
raise RequirementError(f'Selected option "{selected_option}" is out of range')
selected_option = options[selected_option]
elif selected_option in options:
pass # We gave a correct absolute value
else:
@ -185,7 +198,10 @@ def select_disk(dict_o_disks):
print(f"{index}: {drive} ({dict_o_disks[drive]['size'], dict_o_disks[drive].device, dict_o_disks[drive]['label']})")
drive = input('Select one of the above disks (by number or full path): ')
if drive.isdigit():
drive = dict_o_disks[drives[int(drive)]]
drive = int(drive)
if drive >= len(drives):
raise DiskError(f'Selected option "{drive}" is out of range')
drive = dict_o_disks[drives[drive]]
elif drive in dict_o_disks:
drive = dict_o_disks[drive]
else:

View File

@ -1,5 +1,6 @@
import getpass, time, json, sys, signal, os
import archinstall
from archinstall.lib.hardware import hasUEFI
"""
This signal-handler chain (and global variable)
@ -139,7 +140,7 @@ def ask_user_questions():
# Get disk encryption password (or skip if blank)
if not archinstall.arguments.get('!encryption-password', None):
if passwd := archinstall.get_password(prompt='Enter disk encryption password (leave blank for no encryption): '):
if (passwd := archinstall.get_password(prompt='Enter disk encryption password (leave blank for no encryption): ')):
archinstall.arguments['!encryption-password'] = passwd
archinstall.arguments['harddrive'].encryption_password = archinstall.arguments['!encryption-password']
@ -244,13 +245,18 @@ def perform_installation_steps():
Setup the blockdevice, filesystem (and optionally encryption).
Once that's done, we'll hand over to perform_installation()
"""
with archinstall.Filesystem(archinstall.arguments['harddrive'], archinstall.GPT) as fs:
# maybe we can ask the user what they would prefer on uefi systems?
if hasUEFI():
mode = archinstall.GPT
else:
mode = archinstall.MBR
with archinstall.Filesystem(archinstall.arguments['harddrive'],mode) as fs:
# Wipe the entire drive if the disk flag `keep_partitions`is False.
if archinstall.arguments['harddrive'].keep_partitions is False:
fs.use_entire_disk(root_filesystem_type=archinstall.arguments.get('filesystem', 'btrfs'),
encrypt_root_partition=archinstall.arguments.get('!encryption-password', False))
# Otherwise, check if encryption is desired and mark the root partition as encrypted.
elif archinstall.arguments.get('!encryption-password', None):
fs.use_entire_disk(root_filesystem_type=archinstall.arguments.get('filesystem', 'btrfs'))
# Check if encryption is desired and mark the root partition as encrypted.
if archinstall.arguments.get('!encryption-password', None):
root_partition = fs.find_partition('/')
root_partition.encrypted = True

View File

@ -8,7 +8,7 @@ archinstall.sys_command(f'cryptsetup close /dev/mapper/luksloop', suppress_error
harddrive = archinstall.select_disk(archinstall.all_disks())
disk_password = getpass.getpass(prompt='Disk password (won\'t echo): ')
with archinstall.Filesystem(harddrive, archinstall.GPT) as fs:
with archinstall.Filesystem(harddrive) as fs:
# Use the entire disk instead of setting up partitions on your own
fs.use_entire_disk('luks2')

View File

@ -11,7 +11,7 @@ archinstall.sys_command(f'cryptsetup close /dev/mapper/luksloop', suppress_error
harddrive = archinstall.all_disks()['/dev/sda']
disk_password = '1234'
with archinstall.Filesystem(harddrive, archinstall.GPT) as fs:
with archinstall.Filesystem(harddrive) as fs:
# Use the entire disk instead of setting up partitions on your own
fs.use_entire_disk('luks2')