Merging in latest changes from master and resolved conflicts.

This commit is contained in:
Anton Hvornum 2021-05-21 09:56:42 +02:00
commit f9ec8f2a27
68 changed files with 1937 additions and 1138 deletions

2
.flake8 Normal file
View File

@ -0,0 +1,2 @@
[flake8]
ignore = E501, W191, E741, E266

View File

@ -9,12 +9,12 @@ on:
- main # In case we adopt this convention in the future - main # In case we adopt this convention in the future
pull_request: pull_request:
paths-ignore: paths-ignore:
- 'docs/**' - 'docs/**'
- '**.editorconfig' - '**.editorconfig'
- '**.gitignore' - '**.gitignore'
- '**.md' - '**.md'
- 'LICENSE' - 'LICENSE'
- 'PKGBUILD' - 'PKGBUILD'
jobs: jobs:
build: build:
@ -23,22 +23,22 @@ jobs:
image: archlinux:latest image: archlinux:latest
options: --privileged options: --privileged
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- run: pwd - run: pwd
- run: find . - run: find .
- run: cat /etc/os-release - run: cat /etc/os-release
- run: mkdir -p /tmp/archlive/airootfs/root/archinstall-git; cp -r . /tmp/archlive/airootfs/root/archinstall-git - run: mkdir -p /tmp/archlive/airootfs/root/archinstall-git; cp -r . /tmp/archlive/airootfs/root/archinstall-git
- run: echo "pip uninstall archinstall -y; cd archinstall-git; python setup.py install" > /tmp/archlive/airootfs/root/.zprofile - run: echo "pip uninstall archinstall -y; cd archinstall-git; python setup.py install" > /tmp/archlive/airootfs/root/.zprofile
- run: echo "echo \"This is an unofficial ISO for development and testing of archinstall. No support will be provided.\"" >> /tmp/archlive/airootfs/root/.zprofile - run: echo "echo \"This is an unofficial ISO for development and testing of archinstall. No support will be provided.\"" >> /tmp/archlive/airootfs/root/.zprofile
- run: echo "echo \"This ISO was built from Git SHA $GITHUB_SHA\"" >> /tmp/archlive/airootfs/root/.zprofile - run: echo "echo \"This ISO was built from Git SHA $GITHUB_SHA\"" >> /tmp/archlive/airootfs/root/.zprofile
- run: echo "echo \"Type archinstall to launch the installer.\"" >> /tmp/archlive/airootfs/root/.zprofile - run: echo "echo \"Type archinstall to launch the installer.\"" >> /tmp/archlive/airootfs/root/.zprofile
- run: cat /tmp/archlive/airootfs/root/.zprofile - run: cat /tmp/archlive/airootfs/root/.zprofile
- run: pacman -Sy; pacman --noconfirm -S git archiso - run: pacman -Sy; pacman --noconfirm -S git archiso
- run: cp -r /usr/share/archiso/configs/releng/* /tmp/archlive - run: cp -r /usr/share/archiso/configs/releng/* /tmp/archlive
- run: echo -e "git\npython\npython-pip\npython-setuptools" >> /tmp/archlive/packages.x86_64 - run: echo -e "git\npython\npython-pip\npython-setuptools" >> /tmp/archlive/packages.x86_64
- run: find /tmp/archlive - run: find /tmp/archlive
- run: cd /tmp/archlive; mkarchiso -v -w work/ -o out/ ./ - run: cd /tmp/archlive; mkarchiso -v -w work/ -o out/ ./
- uses: actions/upload-artifact@v2 - uses: actions/upload-artifact@v2
with: with:
name: Arch Live ISO name: Arch Live ISO
path: /tmp/archlive/out/*.iso path: /tmp/archlive/out/*.iso

40
.github/workflows/lint-python.yaml vendored Normal file
View File

@ -0,0 +1,40 @@
on: [ push, pull_request ]
name: Lint Python and Find Syntax Errors
jobs:
mypy:
runs-on: ubuntu-latest
container:
image: archlinux:latest
steps:
- uses: actions/checkout@v2
- run: pacman --noconfirm -Syu python mypy
- name: run mypy
run: mypy . --ignore-missing-imports || exit 0
flake8:
runs-on: ubuntu-latest
container:
image: archlinux:latest
steps:
- uses: actions/checkout@v2
- run: pacman --noconfirm -Syu python python-pip
- run: python -m pip install --upgrade pip
- run: pip install flake8
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors
flake8 . --count --select=E9,F63,F7 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
pytest:
runs-on: ubuntu-latest
container:
image: archlinux:latest
steps:
- uses: actions/checkout@v2
- run: pacman --noconfirm -Syu python python-pip
- run: python -m pip install --upgrade pip
- run: pip install pytest
# TODO: Add tests and enable pytest checks.
# - name: Test with pytest
# run: |
# pytest

View File

@ -5,7 +5,7 @@ name: Upload archinstall to PyPi
on: on:
release: release:
types: [created, published] types: [ created, published ]
jobs: jobs:
deploy: deploy:
@ -13,18 +13,18 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Set up Python - name: Set up Python
uses: actions/setup-python@v2 uses: actions/setup-python@v2
with: with:
python-version: '3.x' python-version: '3.x'
- name: Install dependencies - name: Install dependencies
run: | run: |
python -m pip install --upgrade pip python -m pip install --upgrade pip
pip install setuptools wheel flit pip install setuptools wheel flit
- name: Build and publish - name: Build and publish
env: env:
FLIT_USERNAME: ${{ secrets.PYPI_USERNAME }} FLIT_USERNAME: ${{ secrets.PYPI_USERNAME }}
FLIT_PASSWORD: ${{ secrets.PYPI_PASSWORD }} FLIT_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: | run: |
flit publish flit publish

5
.gitignore vendored
View File

@ -20,7 +20,10 @@ SAFETY_LOCK
**/**.network **/**.network
**/**.target **/**.target
**/**.qcow2 **/**.qcow2
**/test.py /test*.py
**/archiso **/archiso
/guided.py /guided.py
/install.log /install.log
venv
.idea/**
**/install.log

View File

@ -1,14 +1,18 @@
# Contributing to archinstall # Contributing to archinstall
Any contributions through pull requests are welcome as this project aims to be a community based project to ease some Arch Linux installation steps. Bear in mind that in the future this repo might be transferred to the official [GitLab repo under Arch Linux](http://gitlab.archlinux.org/archlinux/) *(if GitLab becomes open to the general public)*. Any contributions through pull requests are welcome as this project aims to be a community based project to ease some Arch Linux installation steps.
Bear in mind that in the future this repo might be transferred to the official [GitLab repo under Arch Linux](http://gitlab.archlinux.org/archlinux/)
*(if GitLab becomes open to the general public)*.
Therefore guidelines and style changes to the code might come into affect as well as guidelines surrounding bug reporting and discussions. Therefore, guidelines and style changes to the code might come into effect as well as guidelines surrounding bug reporting and discussions.
## Branches ## Branches
`master` is currently the default branch, and that's where all future feature work is being done, this means that `master` is a living entity and will most likely never be in a fully stable state. For stable releases, please see the tagged commits. `master` is currently the default branch, and that's where all future feature work is being done, this means that `master` is a living entity and will most likely never be in a fully stable state.
For stable releases, please see the tagged commits.
Patch releases will be done against their own branches, branched from stable tagged releases and will be named according to the version it will become on release *(Patches to `v2.1.4` will be done on branch `v2.1.5` for instance)*. Patch releases will be done against their own branches, branched from stable tagged releases and will be named according to the version it will become on release.
*(Patches to `v2.1.4` will be done on branch `v2.1.5` for instance)*.
## Discussions ## Discussions
@ -17,41 +21,50 @@ For less formal discussions there are also a [archinstall Discord server](https:
## Coding convention ## Coding convention
Archinstall's goal is to follow [PEP8](https://www.python.org/dev/peps/pep-0008/) as best as it can with some minor exceptions.<br> ArchInstall's goal is to follow [PEP8](https://www.python.org/dev/peps/pep-0008/) as best as it can with some minor exceptions.<br>
The exceptions to PEP8 are: The exceptions to PEP8 are:
* Archinstall uses [tabs instead of spaces](https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces) simply to make it easier for non-IDE developers to navigate the code *(Tab display-width should be equal to 4 spaces)*. Exception to the rule are comments that need fine-tuned indentation for documentation purposes. * Archinstall uses [tabs instead of spaces](https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces) simply to make it
* [Line length](https://www.python.org/dev/peps/pep-0008/#maximum-line-length) should aim for no more than 100 characters, but not strictly enforced. easier for non-IDE developers to navigate the code *(Tab display-width should be equal to 4 spaces)*. Exception to the
* [Line breaks before/after binary operator](https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator) is not enforced, as long as the style of line breaks are consistent within the same code block. rule are comments that need fine-tuned indentation for documentation purposes.
* Archinstall should always be saved with **Unix-formatted line endings** and no other platform-specific formats. * [Line length](https://www.python.org/dev/peps/pep-0008/#maximum-line-length) should aim for no more than 100
* [Blank lines](https://www.python.org/dev/peps/pep-0008/#blank-lines) before/after imports and functions are not followed and discouraged. One space is commonly used in archinstall. characters, but not strictly enforced.
* Multiple [Imports](https://www.python.org/dev/peps/pep-0008/#imports) on the same line is allowed, but more than five imports should be avoided on any given line. This simply saves up some space at the top of the file *(for non-IDE developers)* and will not be enforced. * [Line breaks before/after binary operator](https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator)
* [String quotes](https://www.python.org/dev/peps/pep-0008/#string-quotes) follow PEP8, the exception being when creating formatted strings, double-quoted strings are *preferred* but not required on the outer edges *(Example: `f"Welcome {name}"` rather than `f'Welcome {name}'`)*. is not enforced, as long as the style of line breaks are consistent within the same code block.
* Archinstall should always be saved with **Unix-formatted line endings** and no other platform-specific formats.
* [String quotes](https://www.python.org/dev/peps/pep-0008/#string-quotes) follow PEP8, the exception being when
creating formatted strings, double-quoted strings are *preferred* but not required on the outer edges *(
Example: `f"Welcome {name}"` rather than `f'Welcome {name}'`)*.
Most of these style guidelines have been put into place after the fact *(in an attempt to clean up the code)*.<br> Most of these style guidelines have been put into place after the fact *(in an attempt to clean up the code)*.<br>
There might therefore be older code which does not follow the coding convention and the code is subject to change. There might therefore be older code which does not follow the coding convention and the code is subject to change.
## Submitting Changes ## Submitting Changes
Archinstall uses Github's pull-request workflow and all contributions in terms of code should be done through pull requests.<br> Archinstall uses GitHub's pull-request workflow and all contributions in terms of code should be done through pull requests.<br>
Anyone interested in archinstall may review your code. One of the core developers will merge your pull request when they think it is ready. Anyone interested in archinstall may review your code. One of the core developers will merge your pull request when they
For every pull request, we aim to promptly either merge it or say why it is not yet ready; if you go a few days without a reply, please feel free to ping the thread by adding a new comment. think it is ready. For every pull request, we aim to promptly either merge it or say why it is not yet ready; if you go
a few days without a reply, please feel free to ping the thread by adding a new comment.
To get your pull request merged sooner, you should explain why you are making the change. For example, you can point to a code sample that is outdated in terms of Arch Linux command lines. To get your pull request merged sooner, you should explain why you are making the change. For example, you can point to
It is also helpful to add links to online documentation or to the implementation of the code you are changing. a code sample that is outdated in terms of Arch Linux command lines. It is also helpful to add links to online
documentation or to the implementation of the code you are changing.
Also, do not squash your commits after you have submitted a pull request, as this erases context during review. We will squash commits when the pull request is merged. Also, do not squash your commits after you have submitted a pull request, as this erases context during review. We will
squash commits when the pull request is merged.
At present the current contributors are (alphabetically): At present the current contributors are (alphabetically):
* Anton Hvornum ([@Torxed](https://github.com/Torxed)) * Anton Hvornum ([@Torxed](https://github.com/Torxed))
* Borislav Kosharov ([@nikibobi](https://github.com/nikibobi)) * Borislav Kosharov ([@nikibobi](https://github.com/nikibobi))
* demostanis ([@demostanis](https://github.com/demostanis)) * demostanis ([@demostanis](https://github.com/demostanis))
* Giancarlo Razzolini (@[grazzolini](https://github.com/grazzolini)) * Dylan Taylor ([@dylanmtaylor](https://github.com/dylanmtaylor))
* j-james ([@j-james](https://github.com/j-james)) * Giancarlo Razzolini (@[grazzolini](https://github.com/grazzolini))
* Jerker Bengtsson ([@jaybent](https://github.com/jaybent)) * j-james ([@j-james](https://github.com/j-james))
* Ninchester ([@ninchester](https://github.com/ninchester)) * Jerker Bengtsson ([@jaybent](https://github.com/jaybent))
* Philipp Schaffrath ([@phisch](https://github.com/phisch)) * Ninchester ([@ninchester](https://github.com/ninchester))
* Varun Madiath ([@vamega](https://github.com/vamega)) * Philipp Schaffrath ([@phisch](https://github.com/phisch))
* nullrequest ([@advaithm](https://github.com/advaithm)) * Varun Madiath ([@vamega](https://github.com/vamega))
* nullrequest ([@advaithm](https://github.com/advaithm))

View File

@ -1,17 +1,17 @@
<!-- <div align="center"> --> <!-- <div align="center"> -->
<img src="https://github.com/archlinux/archinstall/raw/master/docs/logo.png" alt="drawing" width="200"/> <img src="https://github.com/archlinux/archinstall/raw/master/docs/logo.png" alt="drawing" width="200"/>
# Arch Installer
<!-- </div> --> <!-- </div> -->
# Arch Installer
[![Lint Python and Find Syntax Errors](https://github.com/archlinux/archinstall/actions/workflows/lint-python.yaml/badge.svg)](https://github.com/archlinux/archinstall/actions/workflows/lint-python.yaml)
Just another guided/automated [Arch Linux](https://wiki.archlinux.org/index.php/Arch_Linux) installer with a twist. Just another guided/automated [Arch Linux](https://wiki.archlinux.org/index.php/Arch_Linux) installer with a twist.
The installer also doubles as a python library to install Arch Linux and manage services, packages and other things inside the installed system *(Usually from a live medium)*. The installer also doubles as a python library to install Arch Linux and manage services, packages and other things inside the installed system *(Usually from a live medium)*.
* archinstall [discord](https://discord.gg/cqXU88y) server * archinstall [discord](https://discord.gg/cqXU88y) server
* archinstall [matrix.org](https://app.element.io/#/room/#archinstall:matrix.org) channel * archinstall [matrix.org](https://app.element.io/#/room/#archinstall:matrix.org) channel
* archinstall [#archinstall@freenode (IRC)](irc://#archinstall@FreeNode) * archinstall [#archinstall@freenode (IRC)](irc://#archinstall@FreeNode)
* archinstall [documentation](https://python-archinstall.readthedocs.io/en/latest/index.html) * archinstall [documentation](https://python-archinstall.readthedocs.io/en/latest/index.html)
# Installation & Usage # Installation & Usage
@ -22,24 +22,37 @@ Or use `pip install --upgrade archinstall` to use as a library.
## Running the [guided](examples/guided.py) installer ## Running the [guided](examples/guided.py) installer
Assuming you are on an Arch Linux live-ISO and booted into EFI mode.
# python -m archinstall --script guided
## Running from a declarative configuration file or URL
Prequisites:
1. Edit the [configuration file](examples/config-sample.json) according to your requirements.
Assuming you are on a Arch Linux live-ISO and booted into EFI mode. Assuming you are on a Arch Linux live-ISO and booted into EFI mode.
# python -m archinstall guided # python -m archinstall --config <path to config file or URL> --vars '<space_seperated KEY=VALUE pairs>'
# Help? # Help?
Submit an issue here on Github, or submit a post in the discord help channel.<br> Submit an issue here on GitHub, or submit a post in the discord help channel.<br>
When doing so, attach the `/var/log/archinstall/install.log` to the issue ticket. This helps us help you! When doing so, attach the `/var/log/archinstall/install.log` to the issue ticket. This helps us help you!
# Mission Statement # Mission Statement
Archinstall promises to ship a [guided installer](https://github.com/archlinux/archinstall/blob/master/examples/guided.py) that follows the [Arch Principles](https://wiki.archlinux.org/index.php/Arch_Linux#Principles) as well as a library to manage services, packages and other Arch Linux aspects. Archinstall promises to ship a [guided installer](https://github.com/archlinux/archinstall/blob/master/examples/guided.py) that follows
the [Arch Principles](https://wiki.archlinux.org/index.php/Arch_Linux#Principles) as well as a library to manage services, packages and other Arch Linux aspects.
The guided installer will provide user friendly options along the way, but the keyword here is options, they are optional and will never be forced upon anyone. The guided installer itself is also optional to use if so desired and not forced upon anyone. The guided installer will provide user-friendly options along the way, but the keyword here is options, they are optional and will never be forced upon anyone.
The guided installer itself is also optional to use if so desired and not forced upon anyone.
--- ---
Archinstall has one fundamental function which is to be a flexible library to manage services, packages and other aspects inside the installed system. This library is in turn used by the provided guided installer but is also for anyone who wants to script their own installations. Archinstall has one fundamental function which is to be a flexible library to manage services, packages and other aspects inside the installed system.
This library is in turn used by the provided guided installer but is also for anyone who wants to script their own installations.
Therefore, Archinstall will try its best to not introduce any breaking changes except for major releases which may break backwards compability after notifying about such changes. Therefore, Archinstall will try its best to not introduce any breaking changes except for major releases which may break backwards compability after notifying about such changes.
@ -47,8 +60,7 @@ Therefore, Archinstall will try its best to not introduce any breaking changes e
You could just copy [guided.py](examples/guided.py) as a starting point. You could just copy [guided.py](examples/guided.py) as a starting point.
But assuming you're building your own ISO and want to create an automated install process, or you want to install virtual machines on to local disk images.<br> However, assuming you're building your own ISO and want to create an automated installation process, or you want to install virtual machines on to local disk images, here is a [minimal example](examples/minimal.py) of how to install using archinstall as a Python library:<br>
This is probably what you'll need, a [minimal example](examples/minimal.py) of how to install using archinstall as a Python library.
```python ```python
import archinstall, getpass import archinstall, getpass
@ -99,34 +111,36 @@ with archinstall.Installer('/mnt') as installation:
This installer will perform the following: This installer will perform the following:
* Prompt the user to select a disk and disk-password * Prompt the user to select a disk and disk-password
* Proceed to wipe the selected disk with a `GPT` partition table on a UEFI system and MBR on a bios system. * 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. * 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 a basic instance of Arch Linux *(base base-devel linux linux-firmware btrfs-progs efibootmgr)*
* Installs and configures a bootloader to partition 0 on uefi. on bios it sets the root 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)* * Install additional packages *(nano, wget, git)*
* Installs a profile with a window manager called [awesome](https://github.com/archlinux/archinstall/blob/master/profiles/awesome.py) *(more on profile installations in the [documentation](https://python-archinstall.readthedocs.io/en/latest/archinstall/Profile.html))*.
> **Creating your own ISO with this script on it:** Follow [ArchISO](https://wiki.archlinux.org/index.php/archiso)'s guide on how to create your own ISO or use a pre-built [guided ISO](https://hvornum.se/archiso/) to skip the python installation step, or to create auto-installing ISO templates. Further down are examples and cheat sheets on how to create different live ISO's. > **Creating your own ISO with this script on it:** Follow [ArchISO](https://wiki.archlinux.org/index.php/archiso)'s guide on how to create your own ISO or use a pre-built [guided ISO](https://hvornum.se/archiso/) to skip the python installation step, or to create auto-installing ISO templates. Further down are examples and cheat sheets on how to create different live ISO's.
## Unattended installation based on MAC address ## Unattended installation based on MAC address
Archinstall comes with a [unattended](examples/unattended.py) example which will look for a matching profile for the machine it is being run on, based on any local MAC address. For instance, if the machine that [unattended](examples/unattended.py) is run on has the MAC address `52:54:00:12:34:56` it will look for a profile called [profiles/52-54-00-12-34-56.py](profiles/52-54-00-12-34-56.py). If it's found, the unattended installation will commence and source that profile as it's installation proceedure. Archinstall comes with a [unattended](examples/unattended.py) example which will look for a matching profile for the machine it is being run on, based on any local MAC address.
For instance, if the machine that [unattended](examples/unattended.py) is run on has the MAC address `52:54:00:12:34:56` it will look for a profile called [profiles/52-54-00-12-34-56.py](profiles/52-54-00-12-34-56.py).
If it's found, the unattended installation will commence and source that profile as it's installation procedure.
# Testing # Testing
## Using a Live ISO Image ## Using a Live ISO Image
If you want to test a commit, branch or bleeding edge release from the repository using the vanilla Arch Live ISO image, you can replace the version of archinstall with a new version and run that with the steps described below. If you want to test a commit, branch or bleeding edge release from the repository using the vanilla Arch Live ISO image,
you can replace the version of archinstall with a new version and run that with the steps described below:
1. You need a working network connection 1. You need a working network connection
2. Install the build requirements with `pacman -Sy; pacman -S git python-pip` 2. Install the build requirements with `pacman -Sy; pacman -S git python-pip`
*(note that this may or may not work depending on your RAM and current state of the squashfs maximum filesystem free space)* *(note that this may or may not work depending on your RAM and current state of the squashfs maximum filesystem free space)*
3. Uninstall the previous version of archinstall with `pip uninstall archinstall` 3. Uninstall the previous version of archinstall with `pip uninstall archinstall`
4. Now clone the latest repository with `git clone https://github.com/archlinux/archinstall` 4. Now clone the latest repository with `git clone https://github.com/archlinux/archinstall`
5. Enter the repository with `cd archinstall` 5. Enter the repository with `cd archinstall`
*At this stage, you can choose to check out a feature branch for instance with `git checkout torxed-v2.2.0`* *At this stage, you can choose to check out a feature branch for instance with `git checkout torxed-v2.2.0`*
6. Build the project and install it using `python setup.py install` 6. Build the project and install it using `python setup.py install`
After this, running archinstall with `python -m archinstall` will run against whatever branch you chose in step 5. After this, running archinstall with `python -m archinstall` will run against whatever branch you chose in step 5.
@ -139,12 +153,12 @@ This can be done by installing `pacman -S arch-install-scripts util-linux` local
# losetup -fP ./testimage.img # losetup -fP ./testimage.img
# losetup -a | grep "testimage.img" | awk -F ":" '{print $1}' # losetup -a | grep "testimage.img" | awk -F ":" '{print $1}'
# pip install --upgrade archinstall # pip install --upgrade archinstall
# python -m archinstall guided # python -m archinstall --script guided
# qemu-system-x86_64 -enable-kvm -machine q35,accel=kvm -device intel-iommu -cpu host -m 4096 -boot order=d -drive file=./testimage.img,format=raw -drive if=pflash,format=raw,readonly,file=/usr/share/ovmf/x64/OVMF_CODE.fd -drive if=pflash,format=raw,readonly,file=/usr/share/ovmf/x64/OVMF_VARS.fd # qemu-system-x86_64 -enable-kvm -machine q35,accel=kvm -device intel-iommu -cpu host -m 4096 -boot order=d -drive file=./testimage.img,format=raw -drive if=pflash,format=raw,readonly,file=/usr/share/ovmf/x64/OVMF_CODE.fd -drive if=pflash,format=raw,readonly,file=/usr/share/ovmf/x64/OVMF_VARS.fd
This will create a *5GB* `testimage.img` and create a loop device which we can use to format and install to.<br> This will create a *5 GB* `testimage.img` and create a loop device which we can use to format and install to.<br>
`archinstall` is installed and executed in [guided mode](#docs-todo). Once the installation is complete,<br> `archinstall` is installed and executed in [guided mode](#docs-todo). Once the installation is complete, ~~you can use qemu/kvm to boot the test media.~~<br>
~~you can use qemu/kvm to boot the test media.~~ *(You'd actually need to do some EFI magic in order to point the EFI vars to the partition 0 in the test medium so this won't work entirely out of the box, but gives you a general idea of what we're going for here)* *(You'd actually need to do some EFI magic in order to point the EFI vars to the partition 0 in the test medium, so this won't work entirely out of the box, but that gives you a general idea of what we're going for here)*
There's also a [Building and Testing](https://github.com/archlinux/archinstall/wiki/Building-and-Testing) guide.<br> There's also a [Building and Testing](https://github.com/archlinux/archinstall/wiki/Building-and-Testing) guide.<br>
It will go through everything from packaging, building and running *(with qemu)* the installer against a dev branch. It will go through everything from packaging, building and running *(with qemu)* the installer against a dev branch.

View File

@ -1,40 +1,78 @@
"""Arch Linux installer - guided, templates etc.""" """Arch Linux installer - guided, templates etc."""
from .lib.general import * import urllib.error
import urllib.parse
import urllib.request
from argparse import ArgumentParser
from .lib.disk import * from .lib.disk import *
from .lib.user_interaction import *
from .lib.exceptions import * from .lib.exceptions import *
from .lib.general import *
from .lib.hardware import *
from .lib.installer import __packages__, Installer from .lib.installer import __packages__, Installer
from .lib.profiles import * from .lib.locale_helpers import *
from .lib.luks import * from .lib.luks import *
from .lib.mirrors import * from .lib.mirrors import *
from .lib.networking import * from .lib.networking import *
from .lib.locale_helpers import *
from .lib.services import *
from .lib.packages import *
from .lib.output import * from .lib.output import *
from .lib.packages import *
from .lib.profiles import *
from .lib.services import *
from .lib.storage import * from .lib.storage import *
from .lib.hardware import * from .lib.systemd import *
from .lib.user_interaction import *
__version__ = "2.2.0" parser = ArgumentParser()
## Basic version of arg.parse() supporting: __version__ = "2.2.0.dev1"
## --key=value
## --boolean
arguments = {}
positionals = []
for arg in sys.argv[1:]:
if '--' == arg[:2]:
if '=' in arg:
key, val = [x.strip() for x in arg[2:].split('=', 1)]
else:
key, val = arg[2:], True
arguments[key] = val
else:
positionals.append(arg)
# TODO: Learn the dark arts of argparse... def initialize_arguments():
# (I summon thee dark spawn of cPython) config = {}
parser.add_argument("--config", nargs="?", help="JSON configuration file or URL")
parser.add_argument("--silent", action="store_true",
help="Warning!!! No prompts, ignored if config is not passed")
parser.add_argument("--script", default="guided", nargs="?", help="Script to run for installation", type=str)
parser.add_argument("--vars",
metavar="KEY=VALUE",
nargs='?',
help="Set a number of key-value pairs "
"(do not put spaces before or after the = sign). "
"If a value contains spaces, you should define "
"it with double quotes: "
'foo="this is a sentence". Note that '
"values are always treated as strings.")
args = parser.parse_args()
if args.config is not None:
try:
# First, let's check if this is a URL scheme instead of a filename
parsed_url = urllib.parse.urlparse(args.config)
if not parsed_url.scheme: # The Profile was not a direct match on a remote URL, it must be a local file.
with open(args.config) as file:
config = json.load(file)
else: # Attempt to load the configuration from the URL.
with urllib.request.urlopen(urllib.request.Request(args.config, headers={'User-Agent': 'ArchInstall'})) as response:
config = json.loads(response.read())
except Exception as e:
print(e)
# Installation can't be silent if config is not passed
config["silent"] = args.silent
if args.vars is not None:
try:
for var in args.vars.split(' '):
key, val = var.split("=")
config[key] = val
except Exception as e:
print(e)
config["script"] = args.script
return config
arguments = initialize_arguments()
# TODO: Learn the dark arts of argparse... (I summon thee dark spawn of cPython)
def run_as_a_module(): def run_as_a_module():
""" """
@ -45,12 +83,8 @@ def run_as_a_module():
# Add another path for finding profiles, so that list_profiles() in Script() can find guided.py, unattended.py etc. # Add another path for finding profiles, so that list_profiles() in Script() can find guided.py, unattended.py etc.
storage['PROFILE_PATH'].append(os.path.abspath(f'{os.path.dirname(__file__)}/examples')) storage['PROFILE_PATH'].append(os.path.abspath(f'{os.path.dirname(__file__)}/examples'))
if len(sys.argv) == 1:
sys.argv.append('guided')
try: try:
script = Script(sys.argv[1]) script = Script(arguments.get('script', None))
except ProfileNotFound as err: except ProfileNotFound as err:
print(f"Couldn't find file: {err}") print(f"Couldn't find file: {err}")
sys.exit(1) sys.exit(1)

View File

@ -1,6 +1,4 @@
import archinstall import archinstall
import sys
import os
if __name__ == '__main__': if __name__ == '__main__':
archinstall.run_as_a_module() archinstall.run_as_a_module()

View File

@ -1,22 +1,26 @@
import glob, re, os, json, time, hashlib import glob
import pathlib, traceback, logging import pathlib
import re
import time
from collections import OrderedDict from collections import OrderedDict
from .exceptions import DiskError from typing import Optional
from .general import * from .general import *
from .hardware import has_uefi
from .output import log from .output import log
from .storage import storage
from .hardware import hasUEFI
ROOT_DIR_PATTERN = re.compile('^.*?/devices') ROOT_DIR_PATTERN = re.compile('^.*?/devices')
GPT = 0b00000001 GPT = 0b00000001
MBR = 0b00000010 MBR = 0b00000010
#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)
class BlockDevice(): # 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)
class BlockDevice:
def __init__(self, path, info=None): def __init__(self, path, info=None):
if not info: if not info:
# If we don't give any information, we need to auto-fill it. # If we don't give any information, we need to auto-fill it.
@ -53,9 +57,9 @@ class BlockDevice():
to give less/partial information for user readability. to give less/partial information for user readability.
""" """
return { return {
'path' : self.path, 'path': self.path,
'size' : self.info['size'] if 'size' in self.info else '<unknown>', 'size': self.info['size'] if 'size' in self.info else '<unknown>',
'model' : self.info['model'] if 'model' in self.info else '<unknown>' 'model': self.info['model'] if 'model' in self.info else '<unknown>'
} }
def __dump__(self): def __dump__(self):
@ -87,8 +91,9 @@ class BlockDevice():
raise DiskError(f'Could not locate backplane info for "{self.path}"') raise DiskError(f'Could not locate backplane info for "{self.path}"')
if self.info['type'] == 'loop': if self.info['type'] == 'loop':
for drive in json.loads(b''.join(sys_command(['losetup', '--json'], hide_from_log=True)).decode('UTF_8'))['loopdevices']: for drive in json.loads(b''.join(SysCommand(['losetup', '--json'])).decode('UTF_8'))['loopdevices']:
if not drive['name'] == self.path: continue if not drive['name'] == self.path:
continue
return drive['back-file'] return drive['back-file']
elif self.info['type'] == 'disk': elif self.info['type'] == 'disk':
@ -103,21 +108,21 @@ class BlockDevice():
else: else:
log(f"Unknown blockdevice type for {self.path}: {self.info['type']}", level=logging.DEBUG) log(f"Unknown blockdevice type for {self.path}: {self.info['type']}", level=logging.DEBUG)
# if not stat.S_ISBLK(os.stat(full_path).st_mode): # if not stat.S_ISBLK(os.stat(full_path).st_mode):
# raise DiskError(f'Selected disk "{full_path}" is not a block device.') # raise DiskError(f'Selected disk "{full_path}" is not a block device.')
@property @property
def partitions(self): def partitions(self):
o = b''.join(sys_command(['partprobe', self.path])) o = b''.join(SysCommand(['partprobe', self.path]))
#o = b''.join(sys_command('/usr/bin/lsblk -o name -J -b {dev}'.format(dev=dev))) # o = b''.join(sys_command('/usr/bin/lsblk -o name -J -b {dev}'.format(dev=dev)))
o = b''.join(sys_command(['/usr/bin/lsblk', '-J', self.path])) o = b''.join(SysCommand(['/usr/bin/lsblk', '-J', self.path]))
if b'not a block device' in o: if b'not a block device' in o:
raise DiskError(f'Can not read partitions off something that isn\'t a block device: {self.path}') raise DiskError(f'Can not read partitions off something that isn\'t a block device: {self.path}')
if not o[:1] == b'{': if not o[:1] == b'{':
raise DiskError(f'Error getting JSON output from:', f'/usr/bin/lsblk -J {self.path}') raise DiskError('Error getting JSON output from:', f'/usr/bin/lsblk -J {self.path}')
r = json.loads(o.decode('UTF-8')) r = json.loads(o.decode('UTF-8'))
if len(r['blockdevices']) and 'children' in r['blockdevices'][0]: if len(r['blockdevices']) and 'children' in r['blockdevices'][0]:
@ -125,7 +130,7 @@ class BlockDevice():
for part in r['blockdevices'][0]['children']: for part in r['blockdevices'][0]['children']:
part_id = part['name'][len(os.path.basename(self.path)):] part_id = part['name'][len(os.path.basename(self.path)):]
if part_id not in self.part_cache: if part_id not in self.part_cache:
## TODO: Force over-write even if in 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']: 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, self, 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'])
@ -142,13 +147,13 @@ class BlockDevice():
@property @property
def uuid(self): def uuid(self):
log(f'BlockDevice().uuid is untested!', level=logging.WARNING, fg='yellow') log('BlockDevice().uuid is untested!', level=logging.WARNING, fg='yellow')
""" """
Returns the disk UUID as returned by lsblk. Returns the disk UUID as returned by lsblk.
This is more reliable than relying on /dev/disk/by-partuuid as This is more reliable than relying on /dev/disk/by-partuuid as
it doesn't seam to be able to detect md raid partitions. it doesn't seam to be able to detect md raid partitions.
""" """
lsblk = b''.join(sys_command(f'lsblk -J -o+UUID {self.path}')) lsblk = b''.join(SysCommand(f'lsblk -J -o+UUID {self.path}'))
for partition in json.loads(lsblk.decode('UTF-8'))['blockdevices']: for partition in json.loads(lsblk.decode('UTF-8'))['blockdevices']:
return partition.get('uuid', None) return partition.get('uuid', None)
@ -174,8 +179,9 @@ class BlockDevice():
def flush_cache(self): def flush_cache(self):
self.part_cache = OrderedDict() self.part_cache = OrderedDict()
class Partition():
def __init__(self, path :str, block_device :BlockDevice, part_id=None, size=-1, filesystem=None, mountpoint=None, encrypted=False, autodetect_filesystem=True): class Partition:
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: if not part_id:
part_id = os.path.basename(path) part_id = os.path.basename(path)
@ -185,10 +191,10 @@ class Partition():
self.mountpoint = mountpoint self.mountpoint = mountpoint
self.target_mountpoint = mountpoint self.target_mountpoint = mountpoint
self.filesystem = filesystem self.filesystem = filesystem
self.size = size # TODO: Refresh? self.size = size # TODO: Refresh?
self._encrypted = None self._encrypted = None
self.encrypted = encrypted self.encrypted = encrypted
self.allow_formatting = False # A fail-safe for unconfigured partitions, such as windows NTFS partitions. self.allow_formatting = False # A fail-safe for unconfigured partitions, such as windows NTFS partitions.
if mountpoint: if mountpoint:
self.mount(mountpoint) self.mount(mountpoint)
@ -198,11 +204,11 @@ class Partition():
if self.mountpoint != mount_information.get('target', None) and mountpoint: if self.mountpoint != mount_information.get('target', None) and mountpoint:
raise DiskError(f"{self} was given a mountpoint but the actual mountpoint differs: {mount_information.get('target', None)}") raise DiskError(f"{self} was given a mountpoint but the actual mountpoint differs: {mount_information.get('target', None)}")
if (target := mount_information.get('target', None)): if target := mount_information.get('target', None):
self.mountpoint = target self.mountpoint = target
if not self.filesystem and autodetect_filesystem: if not self.filesystem and autodetect_filesystem:
if (fstype := mount_information.get('fstype', get_filesystem_type(path))): if fstype := mount_information.get('fstype', get_filesystem_type(path)):
self.filesystem = fstype self.filesystem = fstype
if self.filesystem == 'crypto_LUKS': if self.filesystem == 'crypto_LUKS':
@ -213,7 +219,7 @@ class Partition():
left_comparitor = left_comparitor.path left_comparitor = left_comparitor.path
else: else:
left_comparitor = str(left_comparitor) left_comparitor = str(left_comparitor)
return self.path < left_comparitor # Not quite sure the order here is correct. But /dev/nvme0n1p1 comes before /dev/nvme0n1p5 so seems correct. return self.path < left_comparitor # Not quite sure the order here is correct. But /dev/nvme0n1p1 comes before /dev/nvme0n1p5 so seems correct.
def __repr__(self, *args, **kwargs): def __repr__(self, *args, **kwargs):
mount_repr = '' mount_repr = ''
@ -304,22 +310,23 @@ class Partition():
return device['pttype'] return device['pttype']
@property @property
def uuid(self) -> str: def uuid(self) -> Optional[str]:
""" """
Returns the PARTUUID as returned by lsblk. Returns the PARTUUID as returned by lsblk.
This is more reliable than relying on /dev/disk/by-partuuid as This is more reliable than relying on /dev/disk/by-partuuid as
it doesn't seam to be able to detect md raid partitions. it doesn't seam to be able to detect md raid partitions.
""" """
lsblk = b''.join(sys_command(f'lsblk -J -o+PARTUUID {self.path}')) lsblk = b''.join(SysCommand(f'lsblk -J -o+PARTUUID {self.path}'))
for partition in json.loads(lsblk.decode('UTF-8'))['blockdevices']: for partition in json.loads(lsblk.decode('UTF-8'))['blockdevices']:
return partition.get('partuuid', None) return partition.get('partuuid', None)
return None
@property @property
def encrypted(self): def encrypted(self):
return self._encrypted return self._encrypted
@encrypted.setter @encrypted.setter
def encrypted(self, value :bool): def encrypted(self, value: bool):
self._encrypted = value self._encrypted = value
@ -329,10 +336,10 @@ class Partition():
@property @property
def real_device(self): def real_device(self):
for blockdevice in json.loads(b''.join(sys_command('lsblk -J')).decode('UTF-8'))['blockdevices']: for blockdevice in json.loads(b''.join(SysCommand('lsblk -J')).decode('UTF-8'))['blockdevices']:
if (parent := self.find_parent_of(blockdevice, os.path.basename(self.path))): if parent := self.find_parent_of(blockdevice, os.path.basename(self.path)):
return f"/dev/{parent}" return f"/dev/{parent}"
# raise DiskError(f'Could not find appropriate parent for encrypted partition {self}') # raise DiskError(f'Could not find appropriate parent for encrypted partition {self}')
return self.path return self.path
def detect_inner_filesystem(self, password): def detect_inner_filesystem(self, password):
@ -349,15 +356,17 @@ class Partition():
if not get_filesystem_type(self.path): if not get_filesystem_type(self.path):
return False return False
temporary_mountpoint = '/tmp/'+hashlib.md5(bytes(f"{time.time()}", 'UTF-8')+os.urandom(12)).hexdigest() temporary_mountpoint = '/tmp/' + hashlib.md5(bytes(f"{time.time()}", 'UTF-8') + os.urandom(12)).hexdigest()
temporary_path = pathlib.Path(temporary_mountpoint) temporary_path = pathlib.Path(temporary_mountpoint)
temporary_path.mkdir(parents=True, exist_ok=True) temporary_path.mkdir(parents=True, exist_ok=True)
if (handle := sys_command(f'/usr/bin/mount {self.path} {temporary_mountpoint}')).exit_code != 0: if (handle := SysCommand(f'/usr/bin/mount {self.path} {temporary_mountpoint}')).exit_code != 0:
raise DiskError(f'Could not mount and check for content on {self.path} because: {b"".join(handle)}') raise DiskError(f'Could not mount and check for content on {self.path} because: {b"".join(handle)}')
files = len(glob.glob(f"{temporary_mountpoint}/*")) files = len(glob.glob(f"{temporary_mountpoint}/*"))
sys_command(f'/usr/bin/umount {temporary_mountpoint}') iterations = 0
while SysCommand(f"/usr/bin/umount -R {temporary_mountpoint}").exit_code != 0 and (iterations := iterations + 1) < 10:
time.sleep(1)
temporary_path.rmdir() temporary_path.rmdir()
@ -420,36 +429,36 @@ class Partition():
log(f'Formatting {path} -> {filesystem}', level=logging.INFO) log(f'Formatting {path} -> {filesystem}', level=logging.INFO)
if filesystem == 'btrfs': if filesystem == 'btrfs':
o = b''.join(sys_command(f'/usr/bin/mkfs.btrfs -f {path}')) o = b''.join(SysCommand(f'/usr/bin/mkfs.btrfs -f {path}'))
if b'UUID' not in o: if b'UUID' not in o:
raise DiskError(f'Could not format {path} with {filesystem} because: {o}') raise DiskError(f'Could not format {path} with {filesystem} because: {o}')
self.filesystem = 'btrfs' self.filesystem = 'btrfs'
elif filesystem == 'vfat': elif filesystem == 'vfat':
o = b''.join(sys_command(f'/usr/bin/mkfs.vfat -F32 {path}')) o = b''.join(SysCommand(f'/usr/bin/mkfs.vfat -F32 {path}'))
if (b'mkfs.fat' not in o and b'mkfs.vfat' not in o) or b'command not found' in o: if (b'mkfs.fat' not in o and b'mkfs.vfat' not in o) or b'command not found' in o:
raise DiskError(f'Could not format {path} with {filesystem} because: {o}') raise DiskError(f'Could not format {path} with {filesystem} because: {o}')
self.filesystem = 'vfat' self.filesystem = 'vfat'
elif filesystem == 'ext4': elif filesystem == 'ext4':
if (handle := sys_command(f'/usr/bin/mkfs.ext4 -F {path}')).exit_code != 0: if (handle := SysCommand(f'/usr/bin/mkfs.ext4 -F {path}')).exit_code != 0:
raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}') raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}')
self.filesystem = 'ext4' self.filesystem = 'ext4'
elif filesystem == 'xfs': elif filesystem == 'xfs':
if (handle := sys_command(f'/usr/bin/mkfs.xfs -f {path}')).exit_code != 0: if (handle := SysCommand(f'/usr/bin/mkfs.xfs -f {path}')).exit_code != 0:
raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}') raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}')
self.filesystem = 'xfs' self.filesystem = 'xfs'
elif filesystem == 'f2fs': elif filesystem == 'f2fs':
if (handle := sys_command(f'/usr/bin/mkfs.f2fs -f {path}')).exit_code != 0: if (handle := SysCommand(f'/usr/bin/mkfs.f2fs -f {path}')).exit_code != 0:
raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}') raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}')
self.filesystem = 'f2fs' self.filesystem = 'f2fs'
elif filesystem == 'crypto_LUKS': elif filesystem == 'crypto_LUKS':
# from .luks import luks2 # from .luks import luks2
# encrypted_partition = luks2(self, None, None) # encrypted_partition = luks2(self, None, None)
# encrypted_partition.format(path) # encrypted_partition.format(path)
self.filesystem = 'crypto_LUKS' self.filesystem = 'crypto_LUKS'
else: else:
@ -467,20 +476,24 @@ class Partition():
return parent return parent
elif 'children' in data: elif 'children' in data:
for child in data['children']: for child in data['children']:
if (parent := self.find_parent_of(child, name, parent=data['name'])): if parent := self.find_parent_of(child, name, parent=data['name']):
return parent return parent
def mount(self, target, fs=None, options=''): def mount(self, target, fs=None, options=''):
if not self.mountpoint: if not self.mountpoint:
log(f'Mounting {self} to {target}', level=logging.INFO) log(f'Mounting {self} to {target}', level=logging.INFO)
if not fs: if not fs:
if not self.filesystem: raise DiskError(f'Need to format (or define) the filesystem on {self} before mounting.') if not self.filesystem:
raise DiskError(f'Need to format (or define) the filesystem on {self} before mounting.')
fs = self.filesystem fs = self.filesystem
pathlib.Path(target).mkdir(parents=True, exist_ok=True) pathlib.Path(target).mkdir(parents=True, exist_ok=True)
try: try:
sys_command(f'/usr/bin/mount {self.path} {target}') if options:
SysCommand(f'/usr/bin/mount -o {options} {self.path} {target}')
else:
SysCommand(f'/usr/bin/mount {self.path} {target}')
except SysCallError as err: except SysCallError as err:
raise err raise err
@ -489,14 +502,14 @@ class Partition():
def unmount(self): def unmount(self):
try: try:
exit_code = sys_command(f'/usr/bin/umount {self.path}').exit_code exit_code = SysCommand(f'/usr/bin/umount {self.path}').exit_code
except SysCallError as err: except SysCallError as err:
exit_code = err.exit_code exit_code = err.exit_code
# Without to much research, it seams that low error codes are errors. # Without to much research, it seams that low error codes are errors.
# And above 8k is indicators such as "/dev/x not mounted.". # And above 8k is indicators such as "/dev/x not mounted.".
# So anything in between 0 and 8k are errors (?). # So anything in between 0 and 8k are errors (?).
if exit_code > 0 and exit_code < 8000: if 0 < exit_code < 8000:
raise err raise err
self.mountpoint = None self.mountpoint = None
@ -509,22 +522,23 @@ class Partition():
""" """
The support for a filesystem (this partition) is tested by calling The support for a filesystem (this partition) is tested by calling
partition.format() with a path set to '/dev/null' which returns two exceptions: partition.format() with a path set to '/dev/null' which returns two exceptions:
1. SysCallError saying that /dev/null is not formattable - but the filesystem is supported 1. SysCallError saying that /dev/null is not formattable - but the filesystem is supported
2. UnknownFilesystemFormat that indicates that we don't support the given filesystem type 2. UnknownFilesystemFormat that indicates that we don't support the given filesystem type
""" """
try: try:
self.format(self.filesystem, '/dev/null', log_formatting=False, allow_formatting=True) self.format(self.filesystem, '/dev/null', log_formatting=False, allow_formatting=True)
except SysCallError: except (SysCallError, DiskError):
pass # We supported it, but /dev/null is not formatable as expected so the mkfs call exited with an error code pass # We supported it, but /dev/null is not formatable as expected so the mkfs call exited with an error code
except UnknownFilesystemFormat as err: except UnknownFilesystemFormat as err:
raise err raise err
return True return True
class Filesystem():
class Filesystem:
# TODO: # TODO:
# When instance of a HDD is selected, check all usages and gracefully unmount them # When instance of a HDD is selected, check all usages and gracefully unmount them
# as well as close any crypto handles. # as well as close any crypto handles.
def __init__(self, blockdevice,mode): def __init__(self, blockdevice, mode):
self.blockdevice = blockdevice self.blockdevice = blockdevice
self.mode = mode self.mode = mode
@ -536,12 +550,12 @@ class Filesystem():
self.blockdevice.flush_cache() self.blockdevice.flush_cache()
return self return self
else: else:
raise DiskError(f'Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel gpt') raise DiskError('Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel gpt')
elif self.mode == MBR: elif self.mode == MBR:
if sys_command(f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos').exit_code == 0: if SysCommand(f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos').exit_code == 0:
return self return self
else: else:
raise DiskError(f'Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos') raise DiskError('Problem setting the partition format to GPT:', f'/usr/bin/parted -s {self.blockdevice.device} mklabel msdos')
else: else:
raise DiskError(f'Unknown mode selected to format in: {self.mode}') raise DiskError(f'Unknown mode selected to format in: {self.mode}')
@ -560,7 +574,7 @@ class Filesystem():
# TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager
if len(args) >= 2 and args[1]: if len(args) >= 2 and args[1]:
raise args[1] raise args[1]
b''.join(sys_command(f'sync')) b''.join(SysCommand('sync'))
return True return True
def find_partition(self, mountpoint): def find_partition(self, mountpoint):
@ -568,11 +582,11 @@ class Filesystem():
if partition.target_mountpoint == mountpoint or partition.mountpoint == mountpoint: if partition.target_mountpoint == mountpoint or partition.mountpoint == mountpoint:
return partition return partition
def raw_parted(self, string:str): def raw_parted(self, string: str):
x = sys_command(f'/usr/bin/parted -s {string}') x = SysCommand(f'/usr/bin/parted -s {string}')
return x return x
def parted(self, string:str): def parted(self, string: str):
""" """
Performs a parted execution of the given string Performs a parted execution of the given string
@ -583,8 +597,8 @@ class Filesystem():
def use_entire_disk(self, root_filesystem_type='ext4'): def use_entire_disk(self, root_filesystem_type='ext4'):
log(f"Using and formatting the entire {self.blockdevice}.", level=logging.DEBUG) log(f"Using and formatting the entire {self.blockdevice}.", level=logging.DEBUG)
if hasUEFI(): if has_uefi():
self.add_partition('primary', start='1MiB', end='513MiB', format='fat32') self.add_partition('primary', start='1MiB', end='513MiB', partition_format='fat32')
self.set_name(0, 'EFI') self.set_name(0, 'EFI')
self.set(0, 'boot on') self.set(0, 'boot on')
# TODO: Probably redundant because in GPT mode 'esp on' is an alias for "boot on"? # TODO: Probably redundant because in GPT mode 'esp on' is an alias for "boot on"?
@ -602,39 +616,40 @@ class Filesystem():
self.blockdevice.partition[0].allow_formatting = True self.blockdevice.partition[0].allow_formatting = True
self.blockdevice.partition[1].allow_formatting = True self.blockdevice.partition[1].allow_formatting = True
else: else:
#we don't need a seprate boot partition it would be a waste of space # we don't need a seprate boot partition it would be a waste of space
self.add_partition('primary', start='1MB', end='100%') self.add_partition('primary', start='1MB', end='100%')
self.blockdevice.partition[0].filesystem=root_filesystem_type 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=logging.DEBUG) log(f"Set the root partition {self.blockdevice.partition[0]} to use filesystem {root_filesystem_type}.", level=logging.DEBUG)
self.blockdevice.partition[0].target_mountpoint = '/' self.blockdevice.partition[0].target_mountpoint = '/'
self.blockdevice.partition[0].allow_formatting = True self.blockdevice.partition[0].allow_formatting = True
def add_partition(self, type, start, end, format=None): def add_partition(self, partition_type, start, end, partition_format=None):
log(f'Adding partition to {self.blockdevice}', level=logging.INFO) log(f'Adding partition to {self.blockdevice}', level=logging.INFO)
previous_partitions = self.blockdevice.partitions previous_partitions = self.blockdevice.partitions
if self.mode == MBR: if self.mode == MBR:
if len(self.blockdevice.partitions)>3: if len(self.blockdevice.partitions) > 3:
DiskError("Too many partitions on disk, MBR disks can only have 3 parimary partitions") DiskError("Too many partitions on disk, MBR disks can only have 3 parimary partitions")
if format: if partition_format:
partitioning = self.parted(f'{self.blockdevice.device} mkpart {type} {format} {start} {end}') == 0 partitioning = self.parted(f'{self.blockdevice.device} mkpart {partition_type} {partition_format} {start} {end}') == 0
else: else:
partitioning = self.parted(f'{self.blockdevice.device} mkpart {type} {start} {end}') == 0 partitioning = self.parted(f'{self.blockdevice.device} mkpart {partition_type} {start} {end}') == 0
if partitioning: if partitioning:
start_wait = time.time() start_wait = time.time()
while previous_partitions == self.blockdevice.partitions: while previous_partitions == self.blockdevice.partitions:
time.sleep(0.025) # Let the new partition come up in the kernel time.sleep(0.025) # Let the new partition come up in the kernel
if time.time() - start_wait > 10: if time.time() - start_wait > 10:
raise DiskError(f"New partition never showed up after adding new partition on {self} (timeout 10 seconds).") raise DiskError(f"New partition never showed up after adding new partition on {self} (timeout 10 seconds).")
return True return True
def set_name(self, partition:int, name:str): def set_name(self, partition: int, name: str):
return self.parted(f'{self.blockdevice.device} name {partition+1} "{name}"') == 0 return self.parted(f'{self.blockdevice.device} name {partition + 1} "{name}"') == 0
def set(self, partition: int, string: str):
return self.parted(f'{self.blockdevice.device} set {partition + 1} {string}') == 0
def set(self, partition:int, string:str):
return self.parted(f'{self.blockdevice.device} set {partition+1} {string}') == 0
def device_state(name, *args, **kwargs): def device_state(name, *args, **kwargs):
# Based out of: https://askubuntu.com/questions/528690/how-to-get-list-of-all-non-removable-disk-device-names-ssd-hdd-and-sata-ide-onl/528709#528709 # Based out of: https://askubuntu.com/questions/528690/how-to-get-list-of-all-non-removable-disk-device-names-ssd-hdd-and-sata-ide-onl/528709#528709
@ -653,28 +668,32 @@ def device_state(name, *args, **kwargs):
return return
return True return True
# lsblk --json -l -n -o path # lsblk --json -l -n -o path
def all_disks(*args, **kwargs): def all_disks(*args, **kwargs):
kwargs.setdefault("partitions", False) kwargs.setdefault("partitions", False)
drives = OrderedDict() drives = OrderedDict()
#for drive in json.loads(sys_command(f'losetup --json', *args, **lkwargs, hide_from_log=True)).decode('UTF_8')['loopdevices']: # for drive in json.loads(sys_command(f'losetup --json', *args, **lkwargs, hide_from_log=True)).decode('UTF_8')['loopdevices']:
for drive in json.loads(b''.join(sys_command(f'lsblk --json -l -n -o path,size,type,mountpoint,label,pkname,model', *args, **kwargs, hide_from_log=True)).decode('UTF_8'))['blockdevices']: for drive in json.loads(b''.join(SysCommand('lsblk --json -l -n -o path,size,type,mountpoint,label,pkname,model')).decode('UTF_8'))['blockdevices']:
if not kwargs['partitions'] and drive['type'] == 'part': continue if not kwargs['partitions'] and drive['type'] == 'part':
continue
drives[drive['path']] = BlockDevice(drive['path'], drive) drives[drive['path']] = BlockDevice(drive['path'], drive)
return drives return drives
def convert_to_gigabytes(string): def convert_to_gigabytes(string):
unit = string.strip()[-1] unit = string.strip()[-1]
size = float(string.strip()[:-1]) size = float(string.strip()[:-1])
if unit == 'M': if unit == 'M':
size = size/1024 size = size / 1024
elif unit == 'T': elif unit == 'T':
size = size*1024 size = size * 1024
return size return size
def harddrive(size=None, model=None, fuzzy=False): def harddrive(size=None, model=None, fuzzy=False):
collection = all_disks() collection = all_disks()
for drive in collection: for drive in collection:
@ -685,13 +704,18 @@ def harddrive(size=None, model=None, fuzzy=False):
return collection[drive] return collection[drive]
def get_mount_info(path):
def get_mount_info(path) -> dict:
try: try:
output = b''.join(sys_command(f'/usr/bin/findmnt --json {path}')) output = SysCommand(f'/usr/bin/findmnt --json {path}')
except SysCallError: except SysCallError:
return {} return {}
output = output.decode('UTF-8') output = output.decode('UTF-8')
if not output:
return {}
output = json.loads(output) output = json.loads(output)
if 'filesystems' in output: if 'filesystems' in output:
if len(output['filesystems']) > 1: if len(output['filesystems']) > 1:
@ -699,15 +723,20 @@ def get_mount_info(path):
return output['filesystems'][0] return output['filesystems'][0]
def get_partitions_in_use(mountpoint):
def get_partitions_in_use(mountpoint) -> list:
try: try:
output = b''.join(sys_command(f'/usr/bin/findmnt --json -R {mountpoint}')) output = SysCommand(f'/usr/bin/findmnt --json -R {mountpoint}')
except SysCallError: except SysCallError:
return {} return []
mounts = [] mounts = []
output = output.decode('UTF-8') output = output.decode('UTF-8')
if not output:
return []
output = json.loads(output) output = json.loads(output)
for target in output.get('filesystems', []): for target in output.get('filesystems', []):
mounts.append(Partition(target['source'], None, filesystem=target.get('fstype', None), mountpoint=target['target'])) mounts.append(Partition(target['source'], None, filesystem=target.get('fstype', None), mountpoint=target['target']))
@ -717,21 +746,24 @@ def get_partitions_in_use(mountpoint):
return mounts return mounts
def get_filesystem_type(path): def get_filesystem_type(path):
try: try:
handle = sys_command(f"blkid -o value -s TYPE {path}") handle = SysCommand(f"blkid -o value -s TYPE {path}")
return b''.join(handle).strip().decode('UTF-8') return b''.join(handle).strip().decode('UTF-8')
except SysCallError: except SysCallError:
return None return None
def disk_layouts(): def disk_layouts():
try: try:
handle = sys_command(f"lsblk -f -o+TYPE,SIZE -J") handle = SysCommand("lsblk -f -o+TYPE,SIZE -J")
return json.loads(b''.join(handle).decode('UTF-8')) return json.loads(b''.join(handle).decode('UTF-8'))
except SysCallError as err: except SysCallError as err:
log(f"Could not return disk layouts: {err}") log(f"Could not return disk layouts: {err}")
return None return None
def encrypted_partitions(blockdevices :dict) -> bool: def encrypted_partitions(blockdevices :dict) -> bool:
for partition in blockdevices.values(): for partition in blockdevices.values():
if partition.get('encrypted', False): if partition.get('encrypted', False):

View File

@ -1,23 +1,37 @@
class RequirementError(BaseException): class RequirementError(BaseException):
pass pass
class DiskError(BaseException): class DiskError(BaseException):
pass pass
class UnknownFilesystemFormat(BaseException): class UnknownFilesystemFormat(BaseException):
pass pass
class ProfileError(BaseException): class ProfileError(BaseException):
pass pass
class SysCallError(BaseException): class SysCallError(BaseException):
def __init__(self, message, exit_code): def __init__(self, message, exit_code):
super(SysCallError, self).__init__(message) super(SysCallError, self).__init__(message)
self.message = message self.message = message
self.exit_code = exit_code self.exit_code = exit_code
class ProfileNotFound(BaseException): class ProfileNotFound(BaseException):
pass pass
class HardwareIncompatibilityError(BaseException): class HardwareIncompatibilityError(BaseException):
pass pass
class PermissionError(BaseException):
pass
class UserError(BaseException): class UserError(BaseException):
pass pass
class ServiceException(BaseException): class ServiceException(BaseException):
pass pass

View File

@ -1,16 +1,26 @@
import os, json, hashlib, shlex, sys import hashlib
import time, pty, logging import json
import logging
import os
import pty
import shlex
import subprocess
import sys
import time
from datetime import datetime, date from datetime import datetime, date
from subprocess import Popen, STDOUT, PIPE, check_output
from select import epoll, EPOLLIN, EPOLLHUP from select import epoll, EPOLLIN, EPOLLHUP
from typing import Union
from .exceptions import * from .exceptions import *
from .output import log from .output import log
def gen_uid(entropy_length=256): def gen_uid(entropy_length=256):
return hashlib.sha512(os.urandom(entropy_length)).hexdigest() return hashlib.sha512(os.urandom(entropy_length)).hexdigest()
def multisplit(s, splitters): def multisplit(s, splitters):
s = [s,] s = [s, ]
for key in splitters: for key in splitters:
ns = [] ns = []
for obj in s: for obj in s:
@ -18,38 +28,41 @@ def multisplit(s, splitters):
for index, part in enumerate(x): for index, part in enumerate(x):
if len(part): if len(part):
ns.append(part) ns.append(part)
if index < len(x)-1: if index < len(x) - 1:
ns.append(key) ns.append(key)
s = ns s = ns
return s return s
def locate_binary(name): def locate_binary(name):
for PATH in os.environ['PATH'].split(':'): for PATH in os.environ['PATH'].split(':'):
for root, folders, files in os.walk(PATH): for root, folders, files in os.walk(PATH):
for file in files: for file in files:
if file == name: if file == name:
return os.path.join(root, file) return os.path.join(root, file)
break # Don't recurse break # Don't recurse
class JSON_Encoder: raise RequirementError(f"Binary {name} does not exist.")
class JsonEncoder:
def _encode(obj): def _encode(obj):
if isinstance(obj, dict): if isinstance(obj, dict):
## We'll need to iterate not just the value that default() usually gets passed # We'll need to iterate not just the value that default() usually gets passed
## But also iterate manually over each key: value pair in order to trap the keys. # But also iterate manually over each key: value pair in order to trap the keys.
copy = {} copy = {}
for key, val in list(obj.items()): for key, val in list(obj.items()):
if isinstance(val, dict): if isinstance(val, dict):
val = json.loads(json.dumps(val, cls=JSON)) # This, is a EXTREMELY ugly hack.. # This, is a EXTREMELY ugly hack.. but it's the only quick way I can think of to trigger a encoding of sub-dictionaries.
# But it's the only quick way I can think of to val = json.loads(json.dumps(val, cls=JSON))
# trigger a encoding of sub-dictionaries.
else: else:
val = JSON_Encoder._encode(val) val = JsonEncoder._encode(val)
if type(key) == str and key[0] == '!': if type(key) == str and key[0] == '!':
copy[JSON_Encoder._encode(key)] = '******' copy[JsonEncoder._encode(key)] = '******'
else: else:
copy[JSON_Encoder._encode(key)] = val copy[JsonEncoder._encode(key)] = val
return copy return copy
elif hasattr(obj, 'json'): elif hasattr(obj, 'json'):
return obj.json() return obj.json()
@ -65,113 +78,134 @@ class JSON_Encoder:
else: else:
return obj return obj
class JSON(json.JSONEncoder, json.JSONDecoder): class JSON(json.JSONEncoder, json.JSONDecoder):
def _encode(self, obj): def _encode(self, obj):
return JSON_Encoder._encode(obj) return JsonEncoder._encode(obj)
def encode(self, obj): def encode(self, obj):
return super(JSON, self).encode(self._encode(obj)) return super(JSON, self).encode(self._encode(obj))
class sys_command():#Thread):
"""
Stolen from archinstall_gui
"""
def __init__(self, cmd, callback=None, start_callback=None, peak_output=False, environment_vars={}, *args, **kwargs):
kwargs.setdefault("worker_id", gen_uid())
kwargs.setdefault("emulate", False)
kwargs.setdefault("suppress_errors", False)
self.log = kwargs.get('log', log) class SysCommandWorker:
def __init__(self, cmd, callbacks=None, peak_output=False, environment_vars=None, logfile=None, working_directory='./'):
if not callbacks:
callbacks = {}
if not environment_vars:
environment_vars = {}
if kwargs['emulate']: if type(cmd) is str:
self.log(f"Starting command '{cmd}' in emulation mode.", level=logging.DEBUG) cmd = shlex.split(cmd)
if type(cmd) is list: if cmd[0][0] != '/' and cmd[0][:2] != './':
# if we get a list of arguments # "which" doesn't work as it's a builtin to bash.
self.raw_cmd = shlex.join(cmd) # It used to work, but for whatever reason it doesn't anymore.
self.cmd = cmd # We there for fall back on manual lookup in os.PATH
else: cmd[0] = locate_binary(cmd[0])
# else consider it a single shell string
# this should only be used if really necessary
self.raw_cmd = cmd
try:
self.cmd = shlex.split(cmd)
except Exception as e:
raise ValueError(f'Incorrect string to split: {cmd}\n{e}')
self.args = args self.cmd = cmd
self.kwargs = kwargs self.callbacks = callbacks
self.peak_output = peak_output self.peak_output = peak_output
self.environment_vars = environment_vars self.environment_vars = environment_vars
self.logfile = logfile
self.working_directory = working_directory
self.kwargs.setdefault("worker", None)
self.callback = callback
self.pid = None
self.exit_code = None self.exit_code = None
self.started = time.time() self._trace_log = b''
self._trace_log_pos = 0
self.poll_object = epoll()
self.child_fd = None
self.started = None
self.ended = None self.ended = None
self.worker_id = kwargs['worker_id']
self.trace_log = b''
self.status = 'starting'
user_catalogue = os.path.expanduser('~') def __contains__(self, key: bytes):
"""
Contains will also move the current buffert position forward.
This is to avoid re-checking the same data when looking for output.
"""
assert type(key) == bytes
if (workdir := kwargs.get('workdir', None)): if (contains := key in self._trace_log[self._trace_log_pos:]):
self.cwd = workdir self._trace_log_pos += self._trace_log[self._trace_log_pos:].find(key) + len(key)
self.exec_dir = workdir
else:
self.cwd = f"{user_catalogue}/.cache/archinstall/workers/{kwargs['worker_id']}/"
self.exec_dir = f'{self.cwd}/{os.path.basename(self.cmd[0])}_workingdir'
if not self.cmd[0][0] == '/': return contains
# "which" doesn't work as it's a builtin to bash.
# It used to work, but for whatever reason it doesn't anymore. So back to square one..
#self.log('Worker command is not executed with absolute path, trying to find: {}'.format(self.cmd[0]), origin='spawn', level=5)
#self.log('This is the binary {} for {}'.format(o.decode('UTF-8'), self.cmd[0]), origin='spawn', level=5)
self.cmd[0] = locate_binary(self.cmd[0])
if not os.path.isdir(self.exec_dir):
os.makedirs(self.exec_dir)
if start_callback:
start_callback(self, *args, **kwargs)
self.run()
def __iter__(self, *args, **kwargs): def __iter__(self, *args, **kwargs):
for line in self.trace_log.split(b'\n'): for line in self._trace_log[self._trace_log_pos:self._trace_log.rfind(b'\n')].split(b'\n'):
yield line if line:
yield line + b'\n'
def __repr__(self, *args, **kwargs): self._trace_log_pos = self._trace_log.rfind(b'\n')
return f"{self.cmd, self.trace_log}"
def decode(self, fmt='UTF-8'): def __repr__(self):
return self.trace_log.decode(fmt) self.make_sure_we_are_executing()
return str(self._trace_log)
def dump(self): def __enter__(self):
return { return self
'status': self.status,
'worker_id': self.worker_id,
'worker_result': self.trace_log.decode('UTF-8'),
'started': self.started,
'ended': self.ended,
'started_pprint': '{}-{}-{} {}:{}:{}'.format(*time.localtime(self.started)),
'ended_pprint': '{}-{}-{} {}:{}:{}'.format(*time.localtime(self.ended)) if self.ended else None,
'exit_code': self.exit_code
}
def peak(self, output :str): def __exit__(self, *args):
if type(output) == bytes: # b''.join(sys_command('sync')) # No need to, since the underlying fs() object will call sync.
# TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager
if self.child_fd:
try: try:
output = output.decode('UTF-8') os.close(self.child_fd)
except UnicodeDecodeError: except:
return None pass
output = output.strip('\r\n ')
if len(output) <= 0:
return None
if self.peak_output: if self.peak_output:
# To make sure any peaked output didn't leave us hanging
# on the same line we were on.
sys.stdout.write("\n")
sys.stdout.flush()
if len(args) >= 2 and args[1]:
log(args[1], level=logging.ERROR, fg='red')
if self.exit_code != 0:
raise SysCallError(f"{self.cmd} exited with abnormal exit code: {self.exit_code}")
def is_alive(self):
self.poll()
if self.started and self.ended is None:
return True
return False
def write(self, data: bytes, line_ending=True):
assert type(data) == bytes # TODO: Maybe we can support str as well and encode it
self.make_sure_we_are_executing()
os.write(self.child_fd, data + (b'\n' if line_ending else b''))
def make_sure_we_are_executing(self):
if not self.started:
return self.execute()
def tell(self) -> int:
self.make_sure_we_are_executing()
return self._trace_log_pos
def seek(self, pos):
self.make_sure_we_are_executing()
# Safety check to ensure 0 < pos < len(tracelog)
self._trace_log_pos = min(max(0, pos), len(self._trace_log))
def peak(self, output: Union[str, bytes]) -> bool:
if self.peak_output:
if type(output) == bytes:
try:
output = output.decode('UTF-8')
except UnicodeDecodeError:
return False
output = output.strip('\r\n ')
if len(output) <= 0:
return False
from .user_interaction import get_terminal_width from .user_interaction import get_terminal_width
# Move back to the beginning of the terminal # Move back to the beginning of the terminal
@ -191,124 +225,129 @@ class sys_command():#Thread):
# And print the new output we're peaking on: # And print the new output we're peaking on:
sys.stdout.write(output) sys.stdout.write(output)
sys.stdout.flush() sys.stdout.flush()
return True
def run(self): def poll(self):
self.status = 'running' self.make_sure_we_are_executing()
old_dir = os.getcwd()
os.chdir(self.exec_dir)
self.pid, child_fd = pty.fork()
if not self.pid: # Child process
# Replace child process with our main process
if not self.kwargs['emulate']:
try:
os.execve(self.cmd[0], self.cmd, {**os.environ, **self.environment_vars})
except FileNotFoundError:
self.status = 'done'
self.log(f"{self.cmd[0]} does not exist.", level=logging.DEBUG)
self.exit_code = 1
return False
os.chdir(old_dir) got_output = False
for fileno, event in self.poll_object.poll(0.1):
try:
output = os.read(self.child_fd, 8192)
got_output = True
self.peak(output)
self._trace_log += output
except OSError as err:
self.ended = time.time()
break
poller = epoll() if self.ended or (got_output is False and pid_exists(self.pid) is False):
poller.register(child_fd, EPOLLIN | EPOLLHUP) self.ended = time.time()
if 'events' in self.kwargs and 'debug' in self.kwargs:
self.log(f'[D] Using triggers for command: {self.cmd}', level=logging.DEBUG)
self.log(json.dumps(self.kwargs['events']), level=logging.DEBUG)
alive = True
last_trigger_pos = 0
while alive and not self.kwargs['emulate']:
for fileno, event in poller.poll(0.1):
try:
output = os.read(child_fd, 8192)
self.peak(output)
self.trace_log += output
except OSError:
alive = False
break
if 'debug' in self.kwargs and self.kwargs['debug'] and len(output):
self.log(self.cmd, 'gave:', output.decode('UTF-8'), level=logging.DEBUG)
if 'on_output' in self.kwargs:
self.kwargs['on_output'](self.kwargs['worker'], output)
lower = output.lower()
broke = False
if 'events' in self.kwargs:
for trigger in list(self.kwargs['events']):
if type(trigger) != bytes:
original = trigger
trigger = bytes(original, 'UTF-8')
self.kwargs['events'][trigger] = self.kwargs['events'][original]
del(self.kwargs['events'][original])
if type(self.kwargs['events'][trigger]) != bytes:
self.kwargs['events'][trigger] = bytes(self.kwargs['events'][trigger], 'UTF-8')
if trigger.lower() in self.trace_log[last_trigger_pos:].lower():
trigger_pos = self.trace_log[last_trigger_pos:].lower().find(trigger.lower())
if 'debug' in self.kwargs and self.kwargs['debug']:
self.log(f"Writing to subprocess {self.cmd[0]}: {self.kwargs['events'][trigger].decode('UTF-8')}", level=logging.DEBUG)
self.log(f"Writing to subprocess {self.cmd[0]}: {self.kwargs['events'][trigger].decode('UTF-8')}", level=logging.DEBUG)
last_trigger_pos = trigger_pos
os.write(child_fd, self.kwargs['events'][trigger])
del(self.kwargs['events'][trigger])
broke = True
break
if broke:
continue
## Adding a exit trigger:
if len(self.kwargs['events']) == 0:
if 'debug' in self.kwargs and self.kwargs['debug']:
self.log(f"Waiting for last command {self.cmd[0]} to finish.", level=logging.DEBUG)
if bytes(f']$'.lower(), 'UTF-8') in self.trace_log[0-len(f']$')-5:].lower():
if 'debug' in self.kwargs and self.kwargs['debug']:
self.log(f"{self.cmd[0]} has finished.", level=logging.DEBUG)
alive = False
break
self.status = 'done'
if 'debug' in self.kwargs and self.kwargs['debug']:
self.log(f"{self.cmd[0]} waiting for exit code.", level=logging.DEBUG)
if not self.kwargs['emulate']:
try: try:
self.exit_code = os.waitpid(self.pid, 0)[1] self.exit_code = os.waitpid(self.pid, 0)[1]
except ChildProcessError: except ChildProcessError:
try: try:
self.exit_code = os.waitpid(child_fd, 0)[1] self.exit_code = os.waitpid(self.child_fd, 0)[1]
except ChildProcessError: except ChildProcessError:
self.exit_code = 1 self.exit_code = 1
else:
self.exit_code = 0
if 'debug' in self.kwargs and self.kwargs['debug']: def execute(self) -> bool:
self.log(f"{self.cmd[0]} got exit code: {self.exit_code}", level=logging.DEBUG) if (old_dir := os.getcwd()) != self.working_directory:
os.chdir(self.working_directory)
if 'ignore_errors' in self.kwargs: # Note: If for any reason, we get a Python exception between here
self.exit_code = 0 # and until os.close(), the traceback will get locked inside
# stdout of the child_fd object. `os.read(self.child_fd, 8192)` is the
# only way to get the traceback without loosing it.
self.pid, self.child_fd = pty.fork()
os.chdir(old_dir)
if self.exit_code != 0 and not self.kwargs['suppress_errors']: if not self.pid:
#self.log(self.trace_log.decode('UTF-8'), level=logging.DEBUG) try:
#self.log(f"'{self.raw_cmd}' did not exit gracefully, exit code {self.exit_code}.", level=logging.ERROR) os.execve(self.cmd[0], self.cmd, {**os.environ, **self.environment_vars})
raise SysCallError(message=f"{self.trace_log.decode('UTF-8')}\n'{self.raw_cmd}' did not exit gracefully (trace log above), exit code: {self.exit_code}", exit_code=self.exit_code) except FileNotFoundError:
log(f"{self.cmd[0]} does not exist.", level=logging.ERROR, fg="red")
self.exit_code = 1
return False
self.ended = time.time() self.started = time.time()
with open(f'{self.cwd}/trace.log', 'wb') as fh: self.poll_object.register(self.child_fd, EPOLLIN | EPOLLHUP)
fh.write(self.trace_log)
return True
def decode(self, encoding='UTF-8'):
return self._trace_log.decode(encoding)
class SysCommand:
def __init__(self, cmd, callback=None, start_callback=None, peak_output=False, environment_vars=None, working_directory='./'):
_callbacks = {}
if callback:
_callbacks['on_end'] = callback
if start_callback:
_callbacks['on_start'] = start_callback
self.cmd = cmd
self._callbacks = _callbacks
self.peak_output = peak_output
self.environment_vars = environment_vars
self.working_directory = working_directory
self.session = None
self.create_session()
def __enter__(self):
return self.session
def __exit__(self, *args, **kwargs):
# b''.join(sys_command('sync')) # No need to, since the underlying 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]:
log(args[1], level=logging.ERROR, fg='red')
def __iter__(self, *args, **kwargs):
for line in self.session:
yield line
def __repr__(self, *args, **kwargs):
return self.session._trace_log.decode('UTF-8')
def __json__(self):
return {
'cmd': self.cmd,
'callbacks': self._callbacks,
'peak': self.peak_output,
'environment_vars': self.environment_vars,
'session': True if self.session else False
}
def create_session(self):
if self.session:
return True
try: try:
os.close(child_fd) self.session = SysCommandWorker(self.cmd, callbacks=self._callbacks, peak_output=self.peak_output, environment_vars=self.environment_vars)
except:
pass while self.session.ended is None:
self.session.poll()
except SysCallError:
return False
return True
def decode(self, fmt='UTF-8'):
return self.session._trace_log.decode(fmt)
@property
def exit_code(self):
return self.session.exit_code
@property
def trace_log(self):
return self.session._trace_log
def prerequisite_check(): def prerequisite_check():
@ -317,5 +356,23 @@ def prerequisite_check():
return True return True
def reboot(): def reboot():
o = b''.join(sys_command("/usr/bin/reboot")) o = b''.join(SysCommand("/usr/bin/reboot"))
def pid_exists(pid: int):
try:
return any(subprocess.check_output(['/usr/bin/ps', '--no-headers', '-o', 'pid', '-p', str(pid)]).strip())
except subprocess.CalledProcessError:
return False
def run_custom_user_commands(commands, installation):
for index, command in enumerate(commands):
log(f'Executing custom command "{command}" ...', fg='yellow')
with open(f"{installation.target}/var/tmp/user-command.{index}.sh", "w") as temp_script:
temp_script.write(command)
execution_output = SysCommand(f"arch-chroot {installation.target} bash /var/tmp/user-command.{index}.sh")
log(execution_output)
os.unlink(f"{installation.target}/var/tmp/user-command.{index}.sh")

View File

@ -1,73 +1,120 @@
import os, subprocess, json import json
from .general import sys_command import os
from .networking import list_interfaces, enrichIfaceTypes import subprocess
from typing import Optional from typing import Optional
__packages__ = ['xf86-video-amdgpu', 'xf86-video-ati', 'xf86-video-intel', 'xf86-video-nouveau', 'xf86-video-fbdev', 'xf86-video-vesa', 'xf86-video-vmware', 'nvidia', 'mesa'] from .general import SysCommand
from .networking import list_interfaces, enrich_iface_types
__packages__ = [
"mesa",
"xf86-video-amdgpu",
"xf86-video-ati",
"xf86-video-nouveau",
"xf86-video-vmware",
"libva-mesa-driver",
"libva-intel-driver",
"intel-media-driver",
"vulkan-radeon",
"vulkan-intel",
"nvidia",
]
AVAILABLE_GFX_DRIVERS = { AVAILABLE_GFX_DRIVERS = {
# Sub-dicts are layer-2 options to be selected # Sub-dicts are layer-2 options to be selected
# and lists are a list of packages to be installed # and lists are a list of packages to be installed
'AMD / ATI' : { "All open-source (default)": [
'amd' : ['xf86-video-amdgpu'], "mesa",
'ati' : ['xf86-video-ati'] "xf86-video-amdgpu",
"xf86-video-ati",
"xf86-video-nouveau",
"xf86-video-vmware",
"libva-mesa-driver",
"libva-intel-driver",
"intel-media-driver",
"vulkan-radeon",
"vulkan-intel",
],
"AMD / ATI (open-source)": [
"mesa",
"xf86-video-amdgpu",
"xf86-video-ati",
"libva-mesa-driver",
"vulkan-radeon",
],
"Intel (open-source)": [
"mesa",
"libva-intel-driver",
"intel-media-driver",
"vulkan-intel",
],
"Nvidia": {
"open-source": ["mesa", "xf86-video-nouveau", "libva-mesa-driver"],
"proprietary": ["nvidia"],
}, },
'intel' : ['xf86-video-intel'], "VMware / VirtualBox (open-source)": ["mesa", "xf86-video-vmware"],
'nvidia' : {
'open-source' : ['xf86-video-nouveau'],
'proprietary' : ['nvidia']
},
'mesa' : ['mesa'],
'fbdev' : ['xf86-video-fbdev'],
'vesa' : ['xf86-video-vesa'],
'vmware / virtualbox' : ['xf86-video-vmware']
} }
def hasWifi()->bool:
return 'WIRELESS' in enrichIfaceTypes(list_interfaces().values()).values()
def hasAMDCPU()->bool: def has_wifi() -> bool:
return 'WIRELESS' in enrich_iface_types(list_interfaces().values()).values()
def has_amd_cpu() -> bool:
if subprocess.check_output("lscpu | grep AMD", shell=True).strip().decode(): if subprocess.check_output("lscpu | grep AMD", shell=True).strip().decode():
return True return True
return False return False
def hasIntelCPU()->bool:
def has_intel_cpu() -> bool:
if subprocess.check_output("lscpu | grep Intel", shell=True).strip().decode(): if subprocess.check_output("lscpu | grep Intel", shell=True).strip().decode():
return True return True
return False return False
def hasUEFI()->bool:
def has_uefi() -> bool:
return os.path.isdir('/sys/firmware/efi') return os.path.isdir('/sys/firmware/efi')
def graphicsDevices()->dict:
def graphics_devices() -> dict:
cards = {} cards = {}
for line in sys_command(f"lspci"): for line in SysCommand("lspci"):
if b' VGA ' in line: if b' VGA ' in line:
_, identifier = line.split(b': ',1) _, identifier = line.split(b': ', 1)
cards[identifier.strip().lower().decode('UTF-8')] = line cards[identifier.strip().lower().decode('UTF-8')] = line
return cards return cards
def hasNvidiaGraphics()->bool:
return any('nvidia' in x for x in graphicsDevices())
def hasAmdGraphics()->bool: def has_nvidia_graphics() -> bool:
return any('amd' in x for x in graphicsDevices()) return any('nvidia' in x for x in graphics_devices())
def hasIntelGraphics()->bool:
return any('intel' in x for x in graphicsDevices())
def cpuVendor()-> Optional[str]: def has_amd_graphics() -> bool:
cpu_info = json.loads(subprocess.check_output("lscpu -J", shell=True).decode('utf-8'))['lscpu'] return any('amd' in x for x in graphics_devices())
def has_intel_graphics() -> bool:
return any('intel' in x for x in graphics_devices())
def cpu_vendor() -> Optional[str]:
cpu_info_raw = SysCommand("lscpu -J")
cpu_info = json.loads(b"".join(cpu_info_raw).decode('UTF-8'))['lscpu']
for info in cpu_info: for info in cpu_info:
if info.get('field',None): if info.get('field', None) == "Vendor ID:":
if info.get('field',None) == "Vendor ID:": return info.get('data', None)
return info.get('data',None) return None
def isVM() -> bool:
def is_vm() -> bool:
try: try:
subprocess.check_call(["systemd-detect-virt"]) # systemd-detect-virt issues a non-zero exit code if it is not on a virtual machine # systemd-detect-virt issues a non-zero exit code if it is not on a virtual machine
return True if b"".join(SysCommand("systemd-detect-virt")).lower() != b"none":
return True
except: except:
return False pass
return False
# TODO: Add more identifiers # TODO: Add more identifiers

View File

@ -1,51 +1,51 @@
import os, stat, time, shutil, pathlib
import subprocess, logging
from .exceptions import *
from .disk import * from .disk import *
from .general import *
from .user_interaction import *
from .profiles import Profile
from .mirrors import *
from .systemd import Networkd
from .output import log
from .storage import storage
from .hardware import * from .hardware import *
from .locale_helpers import verify_x11_keyboard_layout
from .mirrors import *
from .storage import storage
from .user_interaction import *
# Any package that the Installer() is responsible for (optional and the default ones) # Any package that the Installer() is responsible for (optional and the default ones)
__packages__ = ["base", "base-devel", "linux-firmware", "linux", "linux-lts", "linux-zen", "linux-hardened"] __packages__ = ["base", "base-devel", "linux-firmware", "linux", "linux-lts", "linux-zen", "linux-hardened"]
class Installer():
class Installer:
""" """
`Installer()` is the wrapper for most basic installation steps. `Installer()` is the wrapper for most basic installation steps.
It also wraps :py:func:`~archinstall.Installer.pacstrap` among other things. It also wraps :py:func:`~archinstall.Installer.pacstrap` among other things.
:param partition: Requires a partition as the first argument, this is :param partition: Requires a partition as the first argument, this is
so that the installer can mount to `mountpoint` and strap packages there. so that the installer can mount to `mountpoint` and strap packages there.
:type partition: class:`archinstall.Partition` :type partition: class:`archinstall.Partition`
:param boot_partition: There's two reasons for needing a boot partition argument, :param boot_partition: There's two reasons for needing a boot partition argument,
The first being so that `mkinitcpio` can place the `vmlinuz` kernel at the right place The first being so that `mkinitcpio` can place the `vmlinuz` kernel at the right place
during the `pacstrap` or `linux` and the base packages for a minimal installation. during the `pacstrap` or `linux` and the base packages for a minimal installation.
The second being when :py:func:`~archinstall.Installer.add_bootloader` is called, The second being when :py:func:`~archinstall.Installer.add_bootloader` is called,
A `boot_partition` must be known to the installer before this is called. A `boot_partition` must be known to the installer before this is called.
:type boot_partition: class:`archinstall.Partition` :type boot_partition: class:`archinstall.Partition`
:param profile: A profile to install, this is optional and can be called later manually. :param profile: A profile to install, this is optional and can be called later manually.
This just simplifies the process by not having to call :py:func:`~archinstall.Installer.install_profile` later on. This just simplifies the process by not having to call :py:func:`~archinstall.Installer.install_profile` later on.
:type profile: str, optional :type profile: str, optional
:param hostname: The given /etc/hostname for the machine. :param hostname: The given /etc/hostname for the machine.
:type hostname: str, optional :type hostname: str, optional
""" """
def __init__(self, target, *, base_packages=__packages__[:3], kernels=['linux']):
def __init__(self, target, *, base_packages=None, kernels=None):
if base_packages is None:
base_packages = __packages__[:3]
if kernels is None:
kernels = ['linux']
self.target = target self.target = target
self.init_time = time.strftime('%Y-%m-%d_%H-%M-%S') self.init_time = time.strftime('%Y-%m-%d_%H-%M-%S')
self.milliseconds = int(str(time.time()).split('.')[1]) self.milliseconds = int(str(time.time()).split('.')[1])
self.helper_flags = { self.helper_flags = {
'base' : False, 'base': False,
'bootloader' : False 'bootloader': False
} }
self.base_packages = base_packages.split(' ') if type(base_packages) is str else base_packages self.base_packages = base_packages.split(' ') if type(base_packages) is str else base_packages
@ -57,6 +57,12 @@ class Installer():
storage['session'] = self storage['session'] = self
self.partitions = get_partitions_in_use(self.target) self.partitions = get_partitions_in_use(self.target)
self.MODULES = []
self.BINARIES = []
self.FILES = []
self.HOOKS = ["base", "udev", "autodetect", "keyboard", "keymap", "modconf", "block", "filesystems", "fsck"]
self.KERNEL_PARAMS = []
def log(self, *args, level=logging.DEBUG, **kwargs): def log(self, *args, level=logging.DEBUG, **kwargs):
""" """
installer.log() wraps output.log() mainly to set a default log-level for this install session. installer.log() wraps output.log() mainly to set a default log-level for this install session.
@ -68,11 +74,10 @@ class Installer():
return self return self
def __exit__(self, *args, **kwargs): def __exit__(self, *args, **kwargs):
# b''.join(sys_command(f'sync')) # No need to, since the underlying fs() object will call sync. # b''.join(sys_command('sync')) # No need to, since the underlying fs() object will call sync.
# TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager
if len(args) >= 2 and args[1]: if len(args) >= 2 and args[1]:
#self.log(self.trace_log.decode('UTF-8'), level=logging.DEBUG)
self.log(args[1], level=logging.ERROR, fg='red') self.log(args[1], level=logging.ERROR, fg='red')
self.sync_log_to_install_medium() self.sync_log_to_install_medium()
@ -80,7 +85,7 @@ class Installer():
# We avoid printing /mnt/<log path> because that might confuse people if they note it down # We avoid printing /mnt/<log path> because that might confuse people if they note it down
# and then reboot, and a identical log file will be found in the ISO medium anyway. # and then reboot, and a identical log file will be found in the ISO medium anyway.
print(f"[!] A log file has been created here: {os.path.join(storage['LOG_PATH'], storage['LOG_FILE'])}") print(f"[!] A log file has been created here: {os.path.join(storage['LOG_PATH'], storage['LOG_FILE'])}")
print(f" Please submit this issue (and file) to https://github.com/archlinux/archinstall/issues") print(" Please submit this issue (and file) to https://github.com/archlinux/archinstall/issues")
raise args[1] raise args[1]
self.genfstab() self.genfstab()
@ -96,7 +101,7 @@ class Installer():
self.log(f' - {step}', fg='red', level=logging.WARNING) self.log(f' - {step}', fg='red', level=logging.WARNING)
self.log(f"Detailed error logs can be found at: {storage['LOG_PATH']}", level=logging.WARNING) self.log(f"Detailed error logs can be found at: {storage['LOG_PATH']}", level=logging.WARNING)
self.log(f"Submit this zip file as an issue to https://github.com/archlinux/archinstall/issues", level=logging.WARNING) self.log("Submit this zip file as an issue to https://github.com/archlinux/archinstall/issues", level=logging.WARNING)
self.sync_log_to_install_medium() self.sync_log_to_install_medium()
return False return False
@ -105,7 +110,7 @@ class Installer():
# Copy over the install log (if there is one) to the install medium if # Copy over the install log (if there is one) to the install medium if
# at least the base has been strapped in, otherwise we won't have a filesystem/structure to copy to. # at least the base has been strapped in, otherwise we won't have a filesystem/structure to copy to.
if self.helper_flags.get('base-strapped', False) is True: if self.helper_flags.get('base-strapped', False) is True:
if (filename := storage.get('LOG_FILE', None)): if filename := storage.get('LOG_FILE', None):
absolute_logfile = os.path.join(storage.get('LOG_PATH', './'), filename) absolute_logfile = os.path.join(storage.get('LOG_PATH', './'), filename)
if not os.path.isdir(f"{self.target}/{os.path.dirname(absolute_logfile)}"): if not os.path.isdir(f"{self.target}/{os.path.dirname(absolute_logfile)}"):
@ -125,11 +130,12 @@ class Installer():
return [step for step, flag in self.helper_flags.items() if flag is False] return [step for step, flag in self.helper_flags.items() if flag is False]
def pacstrap(self, *packages, **kwargs): def pacstrap(self, *packages, **kwargs):
if type(packages[0]) in (list, tuple): packages = packages[0] if type(packages[0]) in (list, tuple):
packages = packages[0]
self.log(f'Installing packages: {packages}', level=logging.INFO) self.log(f'Installing packages: {packages}', level=logging.INFO)
if (sync_mirrors := sys_command('/usr/bin/pacman -Syy')).exit_code == 0: if (sync_mirrors := SysCommand('/usr/bin/pacman -Syy')).exit_code == 0:
if (pacstrap := sys_command(f'/usr/bin/pacstrap {self.target} {" ".join(packages)}', **kwargs)).exit_code == 0: if (pacstrap := SysCommand(f'/usr/bin/pacstrap {self.target} {" ".join(packages)}', peak_output=True)).exit_code == 0:
return True return True
else: else:
self.log(f'Could not strap in packages: {pacstrap.exit_code}', level=logging.INFO) self.log(f'Could not strap in packages: {pacstrap.exit_code}', level=logging.INFO)
@ -142,36 +148,38 @@ class Installer():
def genfstab(self, flags='-pU'): def genfstab(self, flags='-pU'):
self.log(f"Updating {self.target}/etc/fstab", level=logging.INFO) self.log(f"Updating {self.target}/etc/fstab", level=logging.INFO)
fstab = sys_command(f'/usr/bin/genfstab {flags} {self.target}').trace_log with open(f"{self.target}/etc/fstab", 'a') as fstab_fh:
with open(f"{self.target}/etc/fstab", 'ab') as fstab_fh: fstab_fh.write(SysCommand(f'/usr/bin/genfstab {flags} {self.target}').decode())
fstab_fh.write(fstab)
if not os.path.isfile(f'{self.target}/etc/fstab'): if not os.path.isfile(f'{self.target}/etc/fstab'):
raise RequirementError(f'Could not generate fstab, strapping in packages most likely failed (disk out of space?)\n{fstab}') raise RequirementError(f'Could not generate fstab, strapping in packages most likely failed (disk out of space?)\n{fstab}')
return True return True
def set_hostname(self, hostname :str, *args, **kwargs): def set_hostname(self, hostname: str, *args, **kwargs):
with open(f'{self.target}/etc/hostname', 'w') as fh: with open(f'{self.target}/etc/hostname', 'w') as fh:
fh.write(hostname + '\n') fh.write(hostname + '\n')
def set_locale(self, locale, encoding='UTF-8', *args, **kwargs): def set_locale(self, locale, encoding='UTF-8', *args, **kwargs):
if not len(locale): return True if not len(locale):
return True
with open(f'{self.target}/etc/locale.gen', 'a') as fh: with open(f'{self.target}/etc/locale.gen', 'a') as fh:
fh.write(f'{locale}.{encoding} {encoding}\n') fh.write(f'{locale}.{encoding} {encoding}\n')
with open(f'{self.target}/etc/locale.conf', 'w') as fh: with open(f'{self.target}/etc/locale.conf', 'w') as fh:
fh.write(f'LANG={locale}.{encoding}\n') fh.write(f'LANG={locale}.{encoding}\n')
return True if sys_command(f'/usr/bin/arch-chroot {self.target} locale-gen').exit_code == 0 else False return True if SysCommand(f'/usr/bin/arch-chroot {self.target} locale-gen').exit_code == 0 else False
def set_timezone(self, zone, *args, **kwargs): def set_timezone(self, zone, *args, **kwargs):
if not zone: return True if not zone:
if not len(zone): return True # Redundant return True
if not len(zone):
return True # Redundant
if (pathlib.Path("/usr")/"share"/"zoneinfo"/zone).exists(): if (pathlib.Path("/usr") / "share" / "zoneinfo" / zone).exists():
(pathlib.Path(self.target)/"etc"/"localtime").unlink(missing_ok=True) (pathlib.Path(self.target) / "etc" / "localtime").unlink(missing_ok=True)
sys_command(f'/usr/bin/arch-chroot {self.target} ln -s /usr/share/zoneinfo/{zone} /etc/localtime') SysCommand(f'/usr/bin/arch-chroot {self.target} ln -s /usr/share/zoneinfo/{zone} /etc/localtime')
return True return True
else: else:
self.log( self.log(
@ -181,7 +189,7 @@ class Installer():
) )
def activate_ntp(self): def activate_ntp(self):
self.log(f'Installing and activating NTP.', level=logging.INFO) self.log('Installing and activating NTP.', level=logging.INFO)
if self.pacstrap('ntp'): if self.pacstrap('ntp'):
if self.enable_service('ntpd'): if self.enable_service('ntpd'):
return True return True
@ -193,7 +201,7 @@ class Installer():
raise ServiceException(f"Unable to start service {service}: {output}") raise ServiceException(f"Unable to start service {service}: {output}")
def run_command(self, cmd, *args, **kwargs): def run_command(self, cmd, *args, **kwargs):
return sys_command(f'/usr/bin/arch-chroot {self.target} {cmd}') return SysCommand(f'/usr/bin/arch-chroot {self.target} {cmd}')
def arch_chroot(self, cmd, *args, **kwargs): def arch_chroot(self, cmd, *args, **kwargs):
if 'runas' in kwargs: if 'runas' in kwargs:
@ -205,6 +213,8 @@ class Installer():
subprocess.check_call(f"/usr/bin/arch-chroot {self.target}", shell=True) subprocess.check_call(f"/usr/bin/arch-chroot {self.target}", shell=True)
def configure_nic(self, nic, dhcp=True, ip=None, gateway=None, dns=None, *args, **kwargs): def configure_nic(self, nic, dhcp=True, ip=None, gateway=None, dns=None, *args, **kwargs):
from .systemd import Networkd
if dhcp: if dhcp:
conf = Networkd(Match={"Name": nic}, Network={"DHCP": "yes"}) conf = Networkd(Match={"Name": nic}, Network={"DHCP": "yes"})
else: else:
@ -222,10 +232,10 @@ class Installer():
with open(f"{self.target}/etc/systemd/network/10-{nic}.network", "a") as netconf: with open(f"{self.target}/etc/systemd/network/10-{nic}.network", "a") as netconf:
netconf.write(str(conf)) netconf.write(str(conf))
def copy_ISO_network_config(self, enable_services=False): def copy_iso_network_config(self, enable_services=False):
# Copy (if any) iwd password and config files # Copy (if any) iwd password and config files
if os.path.isdir('/var/lib/iwd/'): if os.path.isdir('/var/lib/iwd/'):
if (psk_files := glob.glob('/var/lib/iwd/*.psk')): if psk_files := glob.glob('/var/lib/iwd/*.psk'):
if not os.path.isdir(f"{self.target}/var/lib/iwd"): if not os.path.isdir(f"{self.target}/var/lib/iwd"):
os.makedirs(f"{self.target}/var/lib/iwd") os.makedirs(f"{self.target}/var/lib/iwd")
@ -233,6 +243,7 @@ class Installer():
# If we haven't installed the base yet (function called pre-maturely) # If we haven't installed the base yet (function called pre-maturely)
if self.helper_flags.get('base', False) is False: if self.helper_flags.get('base', False) is False:
self.base_packages.append('iwd') self.base_packages.append('iwd')
# This function will be called after minimal_installation() # This function will be called after minimal_installation()
# as a hook for post-installs. This hook is only needed if # as a hook for post-installs. This hook is only needed if
# base is not installed yet. # base is not installed yet.
@ -250,7 +261,7 @@ class Installer():
shutil.copy2(psk, f"{self.target}/var/lib/iwd/{os.path.basename(psk)}") shutil.copy2(psk, f"{self.target}/var/lib/iwd/{os.path.basename(psk)}")
# Copy (if any) systemd-networkd config files # Copy (if any) systemd-networkd config files
if (netconfigurations := glob.glob('/etc/systemd/network/*')): if netconfigurations := glob.glob('/etc/systemd/network/*'):
if not os.path.isdir(f"{self.target}/etc/systemd/network/"): if not os.path.isdir(f"{self.target}/etc/systemd/network/"):
os.makedirs(f"{self.target}/etc/systemd/network/") os.makedirs(f"{self.target}/etc/systemd/network/")
@ -260,38 +271,43 @@ class Installer():
if enable_services: if enable_services:
# If we haven't installed the base yet (function called pre-maturely) # If we haven't installed the base yet (function called pre-maturely)
if self.helper_flags.get('base', False) is False: if self.helper_flags.get('base', False) is False:
def post_install_enable_networkd_resolved(*args, **kwargs): def post_install_enable_networkd_resolved(*args, **kwargs):
self.enable_service('systemd-networkd', 'systemd-resolved') self.enable_service('systemd-networkd', 'systemd-resolved')
self.post_base_install.append(post_install_enable_networkd_resolved) self.post_base_install.append(post_install_enable_networkd_resolved)
# Otherwise, we can go ahead and enable the services # Otherwise, we can go ahead and enable the services
else: else:
self.enable_service('systemd-networkd', 'systemd-resolved') self.enable_service('systemd-networkd', 'systemd-resolved')
return True return True
def detect_encryption(self, partition): def detect_encryption(self, partition):
part = Partition(partition.parent, None, autodetect_filesystem=True)
if partition.encrypted: if partition.encrypted:
return partition return partition
elif partition.parent not in partition.path and Partition(partition.parent, None, autodetect_filesystem=True).filesystem == 'crypto_LUKS': elif partition.parent not in partition.path and part.filesystem == 'crypto_LUKS':
return Partition(partition.parent, None, autodetect_filesystem=True) return part
return False return False
def minimal_installation(self): def mkinitcpio(self, *flags):
## Add necessary packages if encrypting the drive with open(f'{self.target}/etc/mkinitcpio.conf', 'w') as mkinit:
## (encrypted partitions default to btrfs for now, so we need btrfs-progs) mkinit.write(f"MODULES=({' '.join(self.MODULES)})\n")
## TODO: Perhaps this should be living in the function which dictates mkinit.write(f"BINARIES=({' '.join(self.BINARIES)})\n")
## the partitioning. Leaving here for now. mkinit.write(f"FILES=({' '.join(self.FILES)})\n")
mkinit.write(f"HOOKS=({' '.join(self.HOOKS)})\n")
SysCommand(f'/usr/bin/arch-chroot {self.target} mkinitcpio {" ".join(flags)}')
MODULES = [] def minimal_installation(self):
BINARIES = [] # Add necessary packages if encrypting the drive
FILES = [] # (encrypted partitions default to btrfs for now, so we need btrfs-progs)
HOOKS = ["base", "udev", "autodetect", "keyboard", "keymap", "modconf", "block", "filesystems", "fsck"] # TODO: Perhaps this should be living in the function which dictates
# the partitioning. Leaving here for now.
for partition in self.partitions: for partition in self.partitions:
if partition.filesystem == 'btrfs': if partition.filesystem == 'btrfs':
#if partition.encrypted: # if partition.encrypted:
self.base_packages.append('btrfs-progs') self.base_packages.append('btrfs-progs')
if partition.filesystem == 'xfs': if partition.filesystem == 'xfs':
self.base_packages.append('xfsprogs') self.base_packages.append('xfsprogs')
@ -300,50 +316,48 @@ class Installer():
# Configure mkinitcpio to handle some specific use cases. # Configure mkinitcpio to handle some specific use cases.
if partition.filesystem == 'btrfs': if partition.filesystem == 'btrfs':
if 'btrfs' not in MODULES: if 'btrfs' not in self.MODULES:
MODULES.append('btrfs') self.MODULES.append('btrfs')
if '/usr/bin/btrfs-progs' not in BINARIES: if '/usr/bin/btrfs-progs' not in self.BINARIES:
BINARIES.append('/usr/bin/btrfs') self.BINARIES.append('/usr/bin/btrfs')
if self.detect_encryption(partition): if self.detect_encryption(partition):
if 'encrypt' not in HOOKS: if 'encrypt' not in self.HOOKS:
HOOKS.insert(HOOKS.index('filesystems'), 'encrypt') self.HOOKS.insert(self.HOOKS.index('filesystems'), 'encrypt')
if not(hasUEFI()): # TODO: Allow for grub even on EFI if not has_uefi():
self.base_packages.append('grub') self.base_packages.append('grub')
if not is_vm():
vendor = cpu_vendor()
if vendor == "AuthenticAMD":
self.base_packages.append("amd-ucode")
if (ucode := pathlib.Path(f"{self.target}/boot/amd-ucode.img")).exists():
ucode.unlink()
elif vendor == "GenuineIntel":
self.base_packages.append("intel-ucode")
if (ucode := pathlib.Path(f"{self.target}/boot/intel-ucode.img")).exists():
ucode.unlink()
else:
self.log(f"Unknown CPU vendor '{vendor}' detected. Archinstall won't install any ucode.", level=logging.DEBUG)
self.pacstrap(self.base_packages) self.pacstrap(self.base_packages)
self.helper_flags['base-strapped'] = True self.helper_flags['base-strapped'] = True
#self.genfstab()
if not isVM():
vendor = cpuVendor()
if vendor == "AuthenticAMD":
self.base_packages.append("amd-ucode")
elif vendor == "GenuineIntel":
self.base_packages.append("intel-ucode")
else:
self.log("Unknown cpu vendor not installing ucode")
with open(f"{self.target}/etc/fstab", "a") as fstab:
fstab.write(
"\ntmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0\n"
) # Redundant \n at the start? who knows?
## TODO: Support locale and timezone with open(f"{self.target}/etc/fstab", "a") as fstab:
#os.remove(f'{self.target}/etc/localtime') fstab.write("\ntmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0\n") # Redundant \n at the start? who knows?
#sys_command(f'/usr/bin/arch-chroot {self.target} ln -s /usr/share/zoneinfo/{localtime} /etc/localtime')
#sys_command('/usr/bin/arch-chroot /mnt hwclock --hctosys --localtime') # TODO: Support locale and timezone
# os.remove(f'{self.target}/etc/localtime')
# sys_command(f'/usr/bin/arch-chroot {self.target} ln -s /usr/share/zoneinfo/{localtime} /etc/localtime')
# sys_command('/usr/bin/arch-chroot /mnt hwclock --hctosys --localtime')
self.set_hostname('archinstall') self.set_hostname('archinstall')
self.set_locale('en_US') self.set_locale('en_US')
# TODO: Use python functions for this # TODO: Use python functions for this
sys_command(f'/usr/bin/arch-chroot {self.target} chmod 700 /root') SysCommand(f'/usr/bin/arch-chroot {self.target} chmod 700 /root')
with open(f'{self.target}/etc/mkinitcpio.conf', 'w') as mkinit: self.mkinitcpio('-P')
mkinit.write(f"MODULES=({' '.join(MODULES)})\n")
mkinit.write(f"BINARIES=({' '.join(BINARIES)})\n")
mkinit.write(f"FILES=({' '.join(FILES)})\n")
mkinit.write(f"HOOKS=({' '.join(HOOKS)})\n")
sys_command(f'/usr/bin/arch-chroot {self.target} mkinitcpio -P')
self.helper_flags['base'] = True self.helper_flags['base'] = True
@ -358,7 +372,7 @@ class Installer():
boot_partition = None boot_partition = None
root_partition = None root_partition = None
for partition in self.partitions: for partition in self.partitions:
if partition.mountpoint == self.target+'/boot': if partition.mountpoint == self.target + '/boot':
boot_partition = partition boot_partition = partition
elif partition.mountpoint == self.target: elif partition.mountpoint == self.target:
root_partition = partition root_partition = partition
@ -368,14 +382,16 @@ class Installer():
if bootloader == 'systemd-bootctl': if bootloader == 'systemd-bootctl':
self.pacstrap('efibootmgr') self.pacstrap('efibootmgr')
if not hasUEFI(): if not has_uefi():
raise HardwareIncompatibilityError raise HardwareIncompatibilityError
# TODO: Ideally we would want to check if another config # TODO: Ideally we would want to check if another config
# points towards the same disk and/or partition. # points towards the same disk and/or partition.
# And in which case we should do some clean up. # And in which case we should do some clean up.
# Install the boot loader # Install the boot loader
sys_command(f'/usr/bin/arch-chroot {self.target} bootctl --no-variables --path=/boot install') if SysCommand(f'/usr/bin/arch-chroot {self.target} bootctl --path=/boot install').exit_code != 0:
# Fallback, try creating the boot loader without touching the EFI variables
SysCommand(f'/usr/bin/arch-chroot {self.target} bootctl --no-variables --path=/boot install')
# Modify or create a loader.conf # Modify or create a loader.conf
if os.path.isfile(f'{self.target}/boot/loader/loader.conf'): if os.path.isfile(f'{self.target}/boot/loader/loader.conf'):
@ -384,65 +400,66 @@ class Installer():
else: else:
loader_data = [ loader_data = [
f"default {self.init_time}", f"default {self.init_time}",
f"timeout 5" "timeout 5"
] ]
with open(f'{self.target}/boot/loader/loader.conf', 'w') as loader: with open(f'{self.target}/boot/loader/loader.conf', 'w') as loader:
for line in loader_data: for line in loader_data:
if line[:8] == 'default ': if line[:8] == 'default ':
loader.write(f'default {self.init_time}\n') loader.write(f'default {self.init_time}\n')
elif line[:8] == '#timeout' and 'timeout 5' not in loader_data:
# We add in the default timeout to support dual-boot
loader.write(f"{line[1:]}\n")
else: else:
loader.write(f"{line}") loader.write(f"{line}\n")
## For some reason, blkid and /dev/disk/by-uuid are not getting along well. # For some reason, blkid and /dev/disk/by-uuid are not getting along well.
## And blkid is wrong in terms of LUKS. # 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() # UUID = sys_command('blkid -s PARTUUID -o value {drive}{partition_2}'.format(**args)).decode('UTF-8').strip()
# Setup the loader entry # Setup the loader entry
with open(f'{self.target}/boot/loader/entries/{self.init_time}.conf', 'w') as entry: with open(f'{self.target}/boot/loader/entries/{self.init_time}.conf', 'w') as entry:
entry.write(f'# Created by: archinstall\n') entry.write('# Created by: archinstall\n')
entry.write(f'# Created on: {self.init_time}\n') entry.write(f'# Created on: {self.init_time}\n')
entry.write(f'title Arch Linux\n') entry.write('title Arch Linux\n')
entry.write(f'linux /vmlinuz-linux\n') entry.write('linux /vmlinuz-linux\n')
if not isVM(): if not is_vm():
vendor = cpuVendor() vendor = cpu_vendor()
if vendor == "AuthenticAMD": if vendor == "AuthenticAMD":
entry.write("initrd /amd-ucode.img\n") entry.write("initrd /amd-ucode.img\n")
elif vendor == "GenuineIntel": elif vendor == "GenuineIntel":
entry.write("initrd /intel-ucode.img\n") entry.write("initrd /intel-ucode.img\n")
else: else:
self.log("unknow cpu vendor, not adding ucode to systemd-boot config") self.log("unknow cpu vendor, not adding ucode to systemd-boot config")
entry.write(f'initrd /initramfs-linux.img\n') entry.write('initrd /initramfs-linux.img\n')
## blkid doesn't trigger on loopback devices really well, # blkid doesn't trigger on loopback devices really well,
## so we'll use the old manual method until we get that sorted out. # so we'll use the old manual method until we get that sorted out.
if real_device := self.detect_encryption(root_partition):
if (real_device := self.detect_encryption(root_partition)):
# TODO: We need to detect if the encrypted device is a whole disk encryption, # TODO: We need to detect if the encrypted device is a whole disk encryption,
# or simply a partition encryption. Right now we assume it's a partition (and we always have) # or simply a partition encryption. Right now we assume it's a partition (and we always have)
log(f"Identifying root partition by PART-UUID on {real_device}: '{real_device.uuid}'.", level=logging.DEBUG) log(f"Identifying root partition by PART-UUID on {real_device}: '{real_device.uuid}'.", level=logging.DEBUG)
entry.write(f'options cryptdevice=PARTUUID={real_device.uuid}:luksdev root=/dev/mapper/luksdev rw intel_pstate=no_hwp\n') entry.write(f'options cryptdevice=PARTUUID={real_device.uuid}:luksdev root=/dev/mapper/luksdev rw intel_pstate=no_hwp {" ".join(self.KERNEL_PARAMS)}\n')
else: else:
log(f"Identifying root partition by PART-UUID on {root_partition}, looking for '{root_partition.uuid}'.", level=logging.DEBUG) log(f"Identifying root partition by PART-UUID on {root_partition}, looking for '{root_partition.uuid}'.", level=logging.DEBUG)
entry.write(f'options root=PARTUUID={root_partition.uuid} rw intel_pstate=no_hwp\n') entry.write(f'options root=PARTUUID={root_partition.uuid} rw intel_pstate=no_hwp {" ".join(self.KERNEL_PARAMS)}\n')
self.helper_flags['bootloader'] = bootloader self.helper_flags['bootloader'] = bootloader
return True return True
raise RequirementError(f"Could not identify the UUID of {self.partition}, there for {self.target}/boot/loader/entries/arch.conf will be broken until fixed.")
elif bootloader == "grub-install": elif bootloader == "grub-install":
self.pacstrap('grub') self.pacstrap('grub')
if hasUEFI(): if has_uefi():
self.pacstrap('efibootmgr') self.pacstrap('efibootmgr')
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.target} grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB')) o = b''.join(SysCommand(f'/usr/bin/arch-chroot {self.target} grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB'))
sys_command('/usr/bin/arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg') SysCommand('/usr/bin/arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg')
return True return True
else: else:
root_device = subprocess.check_output(f'basename "$(readlink -f /sys/class/block/{root_partition.path.replace("/dev/","")}/..)"', shell=True).decode().strip() root_device = subprocess.check_output(f'basename "$(readlink -f /sys/class/block/{root_partition.path.replace("/dev/", "")}/..)"', shell=True).decode().strip()
if root_device == "block": if root_device == "block":
root_device = f"{root_partition.path}" root_device = f"{root_partition.path}"
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.target} grub-install --target=i386-pc /dev/{root_device}')) o = b''.join(SysCommand(f'/usr/bin/arch-chroot {self.target} grub-install --target=i386-pc /dev/{root_device}'))
sys_command('/usr/bin/arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg') SysCommand('/usr/bin/arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg')
self.helper_flags['bootloader'] = bootloader self.helper_flags['bootloader'] = bootloader
return True return True
else: else:
@ -452,14 +469,7 @@ class Installer():
return self.pacstrap(*packages) return self.pacstrap(*packages)
def install_profile(self, profile): def install_profile(self, profile):
# TODO: Replace this with a import archinstall.session instead in the profiles. storage['installation_session'] = self
# The tricky thing with doing the import archinstall.session instead is that
# profiles might be run from a different chroot, and there's no way we can
# guarantee file-path safety when accessing the installer object that way.
# Doing the __builtins__ replacement, ensures that the global variable "installation"
# is always kept up to date. It's considered a nasty hack - but it's a safe way
# of ensuring 100% accuracy of archinstall session variables.
__builtins__['installation'] = self
if type(profile) == str: if type(profile) == str:
profile = Profile(self, profile) profile = Profile(self, profile)
@ -467,21 +477,23 @@ class Installer():
self.log(f'Installing network profile {profile}', level=logging.INFO) self.log(f'Installing network profile {profile}', level=logging.INFO)
return profile.install() return profile.install()
def enable_sudo(self, entity :str, group=False): def enable_sudo(self, entity: str, group=False):
self.log(f'Enabling sudo permissions for {entity}.', level=logging.INFO) self.log(f'Enabling sudo permissions for {entity}.', level=logging.INFO)
with open(f'{self.target}/etc/sudoers', 'a') as sudoers: with open(f'{self.target}/etc/sudoers', 'a') as sudoers:
sudoers.write(f'{"%" if group else ""}{entity} ALL=(ALL) ALL\n') sudoers.write(f'{"%" if group else ""}{entity} ALL=(ALL) ALL\n')
return True return True
def user_create(self, user :str, password=None, groups=[], sudo=False): def user_create(self, user: str, password=None, groups=None, sudo=False):
if groups is None:
groups = []
self.log(f'Creating user {user}', level=logging.INFO) self.log(f'Creating user {user}', level=logging.INFO)
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.target} useradd -m -G wheel {user}')) o = b''.join(SysCommand(f'/usr/bin/arch-chroot {self.target} useradd -m -G wheel {user}'))
if password: if password:
self.user_set_pw(user, password) self.user_set_pw(user, password)
if groups: if groups:
for group in groups: for group in groups:
o = b''.join(sys_command(f'/usr/bin/arch-chroot {self.target} gpasswd -a {user} {group}')) o = b''.join(SysCommand(f'/usr/bin/arch-chroot {self.target} gpasswd -a {user} {group}'))
if sudo and self.enable_sudo(user): if sudo and self.enable_sudo(user):
self.helper_flags['user'] = True self.helper_flags['user'] = True
@ -493,20 +505,53 @@ class Installer():
# This means the root account isn't locked/disabled with * in /etc/passwd # This means the root account isn't locked/disabled with * in /etc/passwd
self.helper_flags['user'] = True self.helper_flags['user'] = True
o = b''.join(sys_command(f"/usr/bin/arch-chroot {self.target} sh -c \"echo '{user}:{password}' | chpasswd\"")) o = b''.join(SysCommand(f"/usr/bin/arch-chroot {self.target} sh -c \"echo '{user}:{password}' | chpasswd\""))
pass pass
def user_set_shell(self, user, shell): def user_set_shell(self, user, shell):
self.log(f'Setting shell for {user} to {shell}', level=logging.INFO) self.log(f'Setting shell for {user} to {shell}', level=logging.INFO)
o = b''.join(sys_command(f"/usr/bin/arch-chroot {self.target} sh -c \"chsh -s {shell} {user}\"")) o = b''.join(SysCommand(f"/usr/bin/arch-chroot {self.target} sh -c \"chsh -s {shell} {user}\""))
pass pass
def set_keyboard_language(self, language): def set_keyboard_language(self, language: str) -> bool:
if len(language.strip()): if len(language.strip()):
with open(f'{self.target}/etc/vconsole.conf', 'w') as vconsole: if not verify_keyboard_layout(language):
vconsole.write(f'KEYMAP={language}\n') self.log(f"Invalid keyboard language specified: {language}", fg="red", level=logging.ERROR)
vconsole.write(f'FONT=lat9w-16\n') return False
# In accordance with https://github.com/archlinux/archinstall/issues/107#issuecomment-841701968
# Setting an empty keymap first, allows the subsequent call to set layout for both console and x11.
from .systemd import Boot
with Boot(self) as session:
session.SysCommand(["localectl", "set-keymap", '""'])
if (output := session.SysCommand(["localectl", "set-keymap", language])).exit_code != 0:
raise ServiceException(f"Unable to set locale '{language}' for console: {output}")
self.log(f"Keyboard language for this installation is now set to: {language}")
else: else:
self.log(f'Keyboard language was not changed from default (no language specified).', fg="yellow", level=logging.INFO) self.log('Keyboard language was not changed from default (no language specified).', fg="yellow", level=logging.INFO)
return True
def set_x11_keyboard_language(self, language: str) -> bool:
"""
A fallback function to set x11 layout specifically and separately from console layout.
This isn't strictly necessary since .set_keyboard_language() does this as well.
"""
if len(language.strip()):
if not verify_x11_keyboard_layout(language):
self.log(f"Invalid x11-keyboard language specified: {language}", fg="red", level=logging.ERROR)
return False
with Boot(self) as session:
session.SysCommand(["localectl", "set-x11-keymap", '""'])
if (output := session.SysCommand(["localectl", "set-x11-keymap", language])).exit_code != 0:
raise ServiceException(f"Unable to set locale '{language}' for X11: {output}")
else:
self.log(f'X11-Keyboard language was not changed from default (no language specified).', fg="yellow", level=logging.INFO)
return True return True

View File

@ -1,20 +1,19 @@
import subprocess import logging
import os
from .exceptions import ServiceException
from .general import SysCommand
from .output import log
from .exceptions import *
# from .general import sys_command
def list_keyboard_languages(): def list_keyboard_languages():
locale_dir = '/usr/share/kbd/keymaps/' for line in SysCommand("localectl --no-pager list-keymaps", environment_vars={'SYSTEMD_COLORS': '0'}):
yield line.decode('UTF-8').strip()
if not os.path.isdir(locale_dir):
raise RequirementError(f'Directory containing locales does not exist: {locale_dir}')
for root, folders, files in os.walk(locale_dir): def list_x11_keyboard_languages():
for line in SysCommand("localectl --no-pager list-x11-keymap-layouts", environment_vars={'SYSTEMD_COLORS': '0'}):
yield line.decode('UTF-8').strip()
for file in files:
if os.path.splitext(file)[1] == '.gz':
yield file.strip('.gz').strip('.map')
def verify_keyboard_layout(layout): def verify_keyboard_layout(layout):
for language in list_keyboard_languages(): for language in list_keyboard_languages():
@ -22,10 +21,29 @@ def verify_keyboard_layout(layout):
return True return True
return False return False
def search_keyboard_layout(filter):
def verify_x11_keyboard_layout(layout):
for language in list_x11_keyboard_languages():
if layout.lower() == language.lower():
return True
return False
def search_keyboard_layout(layout):
for language in list_keyboard_languages(): for language in list_keyboard_languages():
if filter.lower() in language.lower(): if layout.lower() in language.lower():
yield language yield language
def set_keyboard_language(locale): def set_keyboard_language(locale):
return subprocess.call(['loadkeys', locale]) == 0 if len(locale.strip()):
if not verify_keyboard_layout(locale):
log(f"Invalid keyboard locale specified: {locale}", fg="red", level=logging.ERROR)
return False
if (output := SysCommand(f'localectl set-keymap {locale}')).exit_code != 0:
raise ServiceException(f"Unable to set locale '{locale}' for console: {output}")
return True
return False

View File

@ -1,15 +1,11 @@
import os
import shlex
import time
import pathlib import pathlib
import logging
from .exceptions import *
from .general import *
from .disk import Partition
from .output import log
from .storage import storage
class luks2(): from .disk import Partition
from .general import *
from .output import log
class luks2:
def __init__(self, partition, mountpoint, password, key_file=None, auto_unmount=False, *args, **kwargs): def __init__(self, partition, mountpoint, password, key_file=None, auto_unmount=False, *args, **kwargs):
self.password = password self.password = password
self.partition = partition self.partition = partition
@ -22,9 +18,9 @@ class luks2():
self.mapdev = None self.mapdev = None
def __enter__(self): def __enter__(self):
#if self.partition.allow_formatting: # if self.partition.allow_formatting:
# self.key_file = self.encrypt(self.partition, *self.args, **self.kwargs) # self.key_file = self.encrypt(self.partition, *self.args, **self.kwargs)
#else: # else:
if not self.key_file: if not self.key_file:
self.key_file = f"/tmp/{os.path.basename(self.partition.path)}.disk_pw" # TODO: Make disk-pw-file randomly unique? self.key_file = f"/tmp/{os.path.basename(self.partition.path)}.disk_pw" # TODO: Make disk-pw-file randomly unique?
@ -47,7 +43,7 @@ class luks2():
def encrypt(self, partition, password=None, key_size=512, hash_type='sha512', iter_time=10000, key_file=None): def encrypt(self, partition, password=None, key_size=512, hash_type='sha512', iter_time=10000, key_file=None):
if not self.partition.allow_formatting: if not self.partition.allow_formatting:
raise DiskError(f'Could not encrypt volume {self.partition} due to it having a formatting lock.') raise DiskError(f'Could not encrypt volume {partition} due to it having a formatting lock.')
log(f'Encrypting {partition} (This might take a while)', level=logging.INFO) log(f'Encrypting {partition} (This might take a while)', level=logging.INFO)
@ -82,7 +78,7 @@ class luks2():
try: try:
# Try to setup the crypt-device # Try to setup the crypt-device
cmd_handle = sys_command(cryptsetup_args) cmd_handle = SysCommand(cryptsetup_args)
except SysCallError as err: except SysCallError as err:
if err.exit_code == 256: if err.exit_code == 256:
log(f'{partition} is being used, trying to unmount and crypt-close the device and running one more attempt at encrypting the device.', level=logging.DEBUG) log(f'{partition} is being used, trying to unmount and crypt-close the device and running one more attempt at encrypting the device.', level=logging.DEBUG)
@ -91,7 +87,7 @@ class luks2():
# Get crypt-information about the device by doing a reverse lookup starting with the partition path # Get crypt-information about the device by doing a reverse lookup starting with the partition path
# For instance: /dev/sda # For instance: /dev/sda
devinfo = json.loads(b''.join(sys_command(f"lsblk --fs -J {partition.path}")).decode('UTF-8'))['blockdevices'][0] devinfo = json.loads(b''.join(SysCommand(f"lsblk --fs -J {partition.path}")).decode('UTF-8'))['blockdevices'][0]
# For each child (sub-partition/sub-device) # For each child (sub-partition/sub-device)
if len(children := devinfo.get('children', [])): if len(children := devinfo.get('children', [])):
@ -99,19 +95,19 @@ class luks2():
# Unmount the child location # Unmount the child location
if child_mountpoint := child.get('mountpoint', None): if child_mountpoint := child.get('mountpoint', None):
log(f'Unmounting {child_mountpoint}', level=logging.DEBUG) log(f'Unmounting {child_mountpoint}', level=logging.DEBUG)
sys_command(f"umount -R {child_mountpoint}") SysCommand(f"umount -R {child_mountpoint}")
# And close it if possible. # And close it if possible.
log(f"Closing crypt device {child['name']}", level=logging.DEBUG) log(f"Closing crypt device {child['name']}", level=logging.DEBUG)
sys_command(f"cryptsetup close {child['name']}") SysCommand(f"cryptsetup close {child['name']}")
# Then try again to set up the crypt-device # Then try again to set up the crypt-device
cmd_handle = sys_command(cryptsetup_args) cmd_handle = SysCommand(cryptsetup_args)
else: else:
raise err raise err
if cmd_handle.exit_code != 0: if cmd_handle.exit_code != 0:
raise DiskError(f'Could not encrypt volume "{partition.path}": {cmd_output}') raise DiskError(f'Could not encrypt volume "{partition.path}": {b"".join(cmd_handle)}')
return key_file return key_file
@ -124,6 +120,7 @@ class luks2():
:type mountpoint: str :type mountpoint: str
""" """
from .disk import get_filesystem_type from .disk import get_filesystem_type
if '/' in mountpoint: if '/' in mountpoint:
os.path.basename(mountpoint) # TODO: Raise exception instead? os.path.basename(mountpoint) # TODO: Raise exception instead?
@ -131,7 +128,7 @@ class luks2():
while pathlib.Path(partition.path).exists() is False and time.time() - wait_timer < 10: while pathlib.Path(partition.path).exists() is False and time.time() - wait_timer < 10:
time.sleep(0.025) time.sleep(0.025)
sys_command(f'/usr/bin/cryptsetup open {partition.path} {mountpoint} --key-file {os.path.abspath(key_file)} --type luks2') SysCommand(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}'): if os.path.islink(f'/dev/mapper/{mountpoint}'):
self.mapdev = f'/dev/mapper/{mountpoint}' self.mapdev = f'/dev/mapper/{mountpoint}'
unlocked_partition = Partition(self.mapdev, None, 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)
@ -142,9 +139,9 @@ class luks2():
if not mountpoint: if not mountpoint:
mountpoint = self.mapdev mountpoint = self.mapdev
sys_command(f'/usr/bin/cryptsetup close {self.mapdev}') SysCommand(f'/usr/bin/cryptsetup close {self.mapdev}')
return os.path.islink(self.mapdev) is False return os.path.islink(self.mapdev) is False
def format(self, path): def format(self, path):
if (handle := sys_command(f"/usr/bin/cryptsetup -q -v luksErase {path}")).exit_code != 0: if (handle := SysCommand(f"/usr/bin/cryptsetup -q -v luksErase {path}")).exit_code != 0:
raise DiskError(f'Could not format {path} with {self.filesystem} because: {b"".join(handle)}') raise DiskError(f'Could not format {path} with {self.filesystem} because: {b"".join(handle)}')

View File

@ -1,28 +1,30 @@
import urllib.request, logging import urllib.error
import urllib.request
from .exceptions import *
from .general import * from .general import *
from .output import log from .output import log
from .storage import storage
def filter_mirrors_by_region(regions, destination='/etc/pacman.d/mirrorlist', tmp_dir='/root', *args, **kwargs):
def filter_mirrors_by_region(regions, destination='/etc/pacman.d/mirrorlist', *args, **kwargs):
""" """
This function will change the active mirrors on the live medium by This function will change the active mirrors on the live medium by
filtering which regions are active based on `regions`. filtering which regions are active based on `regions`.
:param region: A series of country codes separated by `,`. For instance `SE,US` for sweden and United States. :param regions: A series of country codes separated by `,`. For instance `SE,US` for sweden and United States.
:type region: str :type regions: str
""" """
region_list = [] region_list = []
for region in regions.split(','): for region in regions.split(','):
region_list.append(f'country={region}') region_list.append(f'country={region}')
o = b''.join(sys_command((f"/usr/bin/wget 'https://archlinux.org/mirrorlist/?{'&'.join(region_list)}&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on' -O {tmp_dir}/mirrorlist"))) response = urllib.request.urlopen(urllib.request.Request(f"https://archlinux.org/mirrorlist/?{'&'.join(region_list)}&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on'", headers={'User-Agent': 'ArchInstall'}))
o = b''.join(sys_command((f"/usr/bin/sed -i 's/#Server/Server/' {tmp_dir}/mirrorlist"))) new_list = response.read().replace(b"#Server", b"Server")
o = b''.join(sys_command((f"/usr/bin/mv {tmp_dir}/mirrorlist {destination}"))) with open(destination, "wb") as mirrorlist:
mirrorlist.write(new_list)
return True return True
def add_custom_mirrors(mirrors:list, *args, **kwargs):
def add_custom_mirrors(mirrors: list, *args, **kwargs):
""" """
This will append custom mirror definitions in pacman.conf This will append custom mirror definitions in pacman.conf
@ -37,6 +39,7 @@ def add_custom_mirrors(mirrors:list, *args, **kwargs):
return True return True
def insert_mirrors(mirrors, *args, **kwargs): def insert_mirrors(mirrors, *args, **kwargs):
""" """
This function will insert a given mirror-list at the top of `/etc/pacman.d/mirrorlist`. This function will insert a given mirror-list at the top of `/etc/pacman.d/mirrorlist`.
@ -58,7 +61,8 @@ def insert_mirrors(mirrors, *args, **kwargs):
return True return True
def use_mirrors(regions :dict, destination='/etc/pacman.d/mirrorlist'):
def use_mirrors(regions: dict, destination='/etc/pacman.d/mirrorlist'):
log(f'A new package mirror-list has been created: {destination}', level=logging.INFO) log(f'A new package mirror-list has been created: {destination}', level=logging.INFO)
for region, mirrors in regions.items(): for region, mirrors in regions.items():
with open(destination, 'w') as mirrorlist: with open(destination, 'w') as mirrorlist:
@ -67,13 +71,15 @@ def use_mirrors(regions :dict, destination='/etc/pacman.d/mirrorlist'):
mirrorlist.write(f'Server = {mirror}\n') mirrorlist.write(f'Server = {mirror}\n')
return True return True
def re_rank_mirrors(top=10, *positionals, **kwargs): def re_rank_mirrors(top=10, *positionals, **kwargs):
if sys_command((f'/usr/bin/rankmirrors -n {top} /etc/pacman.d/mirrorlist > /etc/pacman.d/mirrorlist')).exit_code == 0: if SysCommand(f'/usr/bin/rankmirrors -n {top} /etc/pacman.d/mirrorlist > /etc/pacman.d/mirrorlist').exit_code == 0:
return True return True
return False return False
def list_mirrors(): def list_mirrors():
url = f"https://archlinux.org/mirrorlist/?protocol=https&ip_version=4&ip_version=6&use_mirror_status=on" url = "https://archlinux.org/mirrorlist/?protocol=https&ip_version=4&ip_version=6&use_mirror_status=on"
regions = {} regions = {}
try: try:
@ -82,7 +88,6 @@ def list_mirrors():
log(f'Could not fetch an active mirror-list: {err}', level=logging.WARNING, fg="yellow") log(f'Could not fetch an active mirror-list: {err}', level=logging.WARNING, fg="yellow")
return regions return regions
region = 'Unknown region' region = 'Unknown region'
for line in response.readlines(): for line in response.readlines():
if len(line.strip()) == 0: if len(line.strip()) == 0:

View File

@ -1,28 +1,43 @@
import os
import fcntl import fcntl
import logging
import os
import socket import socket
import struct import struct
from collections import OrderedDict from collections import OrderedDict
from .exceptions import * from .exceptions import *
from .general import sys_command from .general import SysCommand
from .output import log
from .storage import storage from .storage import storage
def getHwAddr(ifname):
def get_hw_addr(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(ifname, 'utf-8')[:15])) info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(ifname, 'utf-8')[:15]))
return ':'.join('%02x' % b for b in info[18:24]) return ':'.join('%02x' % b for b in info[18:24])
def list_interfaces(skip_loopback=True): def list_interfaces(skip_loopback=True):
interfaces = OrderedDict() interfaces = OrderedDict()
for index, iface in socket.if_nameindex(): for index, iface in socket.if_nameindex():
if skip_loopback and iface == "lo": if skip_loopback and iface == "lo":
continue continue
mac = getHwAddr(iface).replace(':', '-').lower() mac = get_hw_addr(iface).replace(':', '-').lower()
interfaces[mac] = iface interfaces[mac] = iface
return interfaces return interfaces
def enrichIfaceTypes(interfaces :dict):
def check_mirror_reachable():
if (exit_code := SysCommand("pacman -Sy").exit_code) == 0:
return True
elif exit_code == 256:
log("check_mirror_reachable() uses 'pacman -Sy' which requires root.", level=logging.ERROR, fg="red")
return False
def enrich_iface_types(interfaces: dict):
result = {} result = {}
for iface in interfaces: for iface in interfaces:
if os.path.isdir(f"/sys/class/net/{iface}/bridge/"): if os.path.isdir(f"/sys/class/net/{iface}/bridge/"):
@ -39,30 +54,34 @@ def enrichIfaceTypes(interfaces :dict):
result[iface] = 'UNKNOWN' result[iface] = 'UNKNOWN'
return result return result
def get_interface_from_mac(mac): def get_interface_from_mac(mac):
return list_interfaces().get(mac.lower(), None) return list_interfaces().get(mac.lower(), None)
def wirelessScan(interface):
interfaces = enrichIfaceTypes(list_interfaces().values()) def wireless_scan(interface):
interfaces = enrich_iface_types(list_interfaces().values())
if interfaces[interface] != 'WIRELESS': if interfaces[interface] != 'WIRELESS':
raise HardwareIncompatibilityError(f"Interface {interface} is not a wireless interface: {interfaces}") raise HardwareIncompatibilityError(f"Interface {interface} is not a wireless interface: {interfaces}")
sys_command(f"iwctl station {interface} scan") SysCommand(f"iwctl station {interface} scan")
if not '_WIFI' in storage: if '_WIFI' not in storage:
storage['_WIFI'] = {} storage['_WIFI'] = {}
if not interface in storage['_WIFI']: if interface not in storage['_WIFI']:
storage['_WIFI'][interface] = {} storage['_WIFI'][interface] = {}
storage['_WIFI'][interface]['scanning'] = True storage['_WIFI'][interface]['scanning'] = True
# TODO: Full WiFi experience might get evolved in the future, pausing for now 2021-01-25 # TODO: Full WiFi experience might get evolved in the future, pausing for now 2021-01-25
def getWirelessNetworks(interface): def get_wireless_networks(interface):
# TODO: Make this oneliner pritter to check if the interface is scanning or not. # TODO: Make this oneliner pritter to check if the interface is scanning or not.
if not '_WIFI' in storage or interface not in storage['_WIFI'] or storage['_WIFI'][interface].get('scanning', False) is False: if '_WIFI' not in storage or interface not in storage['_WIFI'] or storage['_WIFI'][interface].get('scanning', False) is False:
import time import time
wirelessScan(interface)
wireless_scan(interface)
time.sleep(5) time.sleep(5)
for line in sys_command(f"iwctl station {interface} get-networks"): for line in SysCommand(f"iwctl station {interface} get-networks"):
print(line) print(line)

View File

@ -1,45 +1,48 @@
import abc import abc
import logging
import os import os
import sys import sys
import logging
from pathlib import Path from pathlib import Path
from .storage import storage from .storage import storage
# TODO: use logging's built in levels instead. # TODO: use logging's built in levels instead.
# Although logging is threaded and I wish to avoid that. # Although logging is threaded and I wish to avoid that.
# It's more Pythonistic or w/e you want to call it. # It's more Pythonistic or w/e you want to call it.
class LOG_LEVELS: class LogLevels:
Critical = 0b001 Critical = 0b001
Error = 0b010 Error = 0b010
Warning = 0b011 Warning = 0b011
Info = 0b101 Info = 0b101
Debug = 0b111 Debug = 0b111
class journald(dict):
class Journald(dict):
@abc.abstractmethod @abc.abstractmethod
def log(message, level=logging.DEBUG): def log(message, level=logging.DEBUG):
try: try:
import systemd.journal import systemd.journal # type: ignore
except ModuleNotFoundError: except ModuleNotFoundError:
return False return False
# For backwards compability, convert old style log-levels # For backwards compatibility, convert old style log-levels
# to logging levels (and warn about deprecated usage) # to logging levels (and warn about deprecated usage)
# There's some code re-usage here but that should be fine. # There's some code re-usage here but that should be fine.
# TODO: Remove these in a few versions: # TODO: Remove these in a few versions:
if level == LOG_LEVELS.Critical: if level == LogLevels.Critical:
log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True) log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True)
level = logging.CRITICAL level = logging.CRITICAL
elif level == LOG_LEVELS.Error: elif level == LogLevels.Error:
log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True) log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True)
level = logging.ERROR level = logging.ERROR
elif level == LOG_LEVELS.Warning: elif level == LogLevels.Warning:
log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True) log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True)
level = logging.WARNING level = logging.WARNING
elif level == LOG_LEVELS.Info: elif level == LogLevels.Info:
log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True) log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True)
level = logging.INFO level = logging.INFO
elif level == LOG_LEVELS.Debug: elif level == LogLevels.Debug:
log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True) log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True)
level = logging.DEBUG level = logging.DEBUG
@ -52,11 +55,13 @@ class journald(dict):
log_adapter.log(level, message) log_adapter.log(level, message)
# TODO: Replace log() for session based logging. # TODO: Replace log() for session based logging.
class SessionLogging(): class SessionLogging:
def __init__(self): def __init__(self):
pass pass
# Found first reference here: https://stackoverflow.com/questions/7445658/how-to-detect-if-the-console-does-support-ansi-escape-codes-in-python # Found first reference here: https://stackoverflow.com/questions/7445658/how-to-detect-if-the-console-does-support-ansi-escape-codes-in-python
# And re-used this: https://github.com/django/django/blob/master/django/core/management/color.py#L12 # And re-used this: https://github.com/django/django/blob/master/django/core/management/color.py#L12
def supports_color(): def supports_color():
@ -70,18 +75,19 @@ def supports_color():
is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
return supported_platform and is_a_tty return supported_platform and is_a_tty
# Heavily influenced by: https://github.com/django/django/blob/ae8338daf34fd746771e0678081999b656177bae/django/utils/termcolors.py#L13 # Heavily influenced by: https://github.com/django/django/blob/ae8338daf34fd746771e0678081999b656177bae/django/utils/termcolors.py#L13
# Color options here: https://askubuntu.com/questions/528928/how-to-do-underline-bold-italic-strikethrough-color-background-and-size-i # Color options here: https://askubuntu.com/questions/528928/how-to-do-underline-bold-italic-strikethrough-color-background-and-size-i
def stylize_output(text :str, *opts, **kwargs): def stylize_output(text: str, *opts, **kwargs):
opt_dict = {'bold': '1', 'italic' : '3', 'underscore': '4', 'blink': '5', 'reverse': '7', 'conceal': '8'} opt_dict = {'bold': '1', 'italic': '3', 'underscore': '4', 'blink': '5', 'reverse': '7', 'conceal': '8'}
color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white') color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
foreground = {color_names[x]: '3%s' % x for x in range(8)} foreground = {color_names[x]: '3%s' % x for x in range(8)}
background = {color_names[x]: '4%s' % x for x in range(8)} background = {color_names[x]: '4%s' % x for x in range(8)}
RESET = '0' reset = '0'
code_list = [] code_list = []
if text == '' and len(opts) == 1 and opts[0] == 'reset': if text == '' and len(opts) == 1 and opts[0] == 'reset':
return '\x1b[%sm' % RESET return '\x1b[%sm' % reset
for k, v in kwargs.items(): for k, v in kwargs.items():
if k == 'fg': if k == 'fg':
code_list.append(foreground[v]) code_list.append(foreground[v])
@ -91,9 +97,10 @@ def stylize_output(text :str, *opts, **kwargs):
if o in opt_dict: if o in opt_dict:
code_list.append(opt_dict[o]) code_list.append(opt_dict[o])
if 'noreset' not in opts: if 'noreset' not in opts:
text = '%s\x1b[%sm' % (text or '', RESET) text = '%s\x1b[%sm' % (text or '', reset)
return '%s%s' % (('\x1b[%sm' % ';'.join(code_list)), text or '') return '%s%s' % (('\x1b[%sm' % ';'.join(code_list)), text or '')
def log(*args, **kwargs): def log(*args, **kwargs):
string = orig_string = ' '.join([str(x) for x in args]) string = orig_string = ' '.join([str(x) for x in args])
@ -105,7 +112,7 @@ def log(*args, **kwargs):
# If a logfile is defined in storage, # If a logfile is defined in storage,
# we use that one to output everything # we use that one to output everything
if (filename := storage.get('LOG_FILE', None)): if filename := storage.get('LOG_FILE', None):
absolute_logfile = os.path.join(storage.get('LOG_PATH', './'), filename) absolute_logfile = os.path.join(storage.get('LOG_PATH', './'), filename)
try: try:
@ -114,8 +121,8 @@ def log(*args, **kwargs):
log_file.write("") log_file.write("")
except PermissionError: except PermissionError:
# Fallback to creating the log file in the current folder # Fallback to creating the log file in the current folder
err_string = f"Not enough permission to place log file at {absolute_logfile}, creating it in {Path('./').absolute()/filename} instead." err_string = f"Not enough permission to place log file at {absolute_logfile}, creating it in {Path('./').absolute() / filename} instead."
absolute_logfile = Path('./').absolute()/filename absolute_logfile = Path('./').absolute() / filename
absolute_logfile.parents[0].mkdir(exist_ok=True) absolute_logfile.parents[0].mkdir(exist_ok=True)
absolute_logfile = str(absolute_logfile) absolute_logfile = str(absolute_logfile)
storage['LOG_PATH'] = './' storage['LOG_PATH'] = './'
@ -128,35 +135,35 @@ def log(*args, **kwargs):
# Unless the level is higher than we've decided to output interactively. # Unless the level is higher than we've decided to output interactively.
# (Remember, log files still get *ALL* the output despite level restrictions) # (Remember, log files still get *ALL* the output despite level restrictions)
if 'level' in kwargs: if 'level' in kwargs:
# For backwards compability, convert old style log-levels # For backwards compatibility, convert old style log-levels
# to logging levels (and warn about deprecated usage) # to logging levels (and warn about deprecated usage)
# There's some code re-usage here but that should be fine. # There's some code re-usage here but that should be fine.
# TODO: Remove these in a few versions: # TODO: Remove these in a few versions:
if kwargs['level'] == LOG_LEVELS.Critical: if kwargs['level'] == LogLevels.Critical:
log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True) log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True)
kwargs['level'] = logging.CRITICAL kwargs['level'] = logging.CRITICAL
elif kwargs['level'] == LOG_LEVELS.Error: elif kwargs['level'] == LogLevels.Error:
log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True) log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True)
kwargs['level'] = logging.ERROR kwargs['level'] = logging.ERROR
elif kwargs['level'] == LOG_LEVELS.Warning: elif kwargs['level'] == LogLevels.Warning:
log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True) log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True)
kwargs['level'] = logging.WARNING kwargs['level'] = logging.WARNING
elif kwargs['level'] == LOG_LEVELS.Info: elif kwargs['level'] == LogLevels.Info:
log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True) log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True)
kwargs['level'] = logging.INFO kwargs['level'] = logging.INFO
elif kwargs['level'] == LOG_LEVELS.Debug: elif kwargs['level'] == LogLevels.Debug:
log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True) log("Deprecated level detected in log message, please use new logging.<level> instead for the following log message:", fg="red", level=logging.ERROR, force=True)
kwargs['level'] = logging.DEBUG kwargs['level'] = logging.DEBUG
if kwargs['level'] < storage.get('LOG_LEVEL', logging.INFO) and not 'force' in kwargs: if kwargs['level'] < storage.get('LOG_LEVEL', logging.INFO) and 'force' not in kwargs:
# Level on log message was Debug, but output level is set to Info. # Level on log message was Debug, but output level is set to Info.
# In that case, we'll drop it. # In that case, we'll drop it.
return None return None
try: try:
journald.log(string, level=kwargs.get('level', logging.INFO)) Journald.log(string, level=kwargs.get('level', logging.INFO))
except ModuleNotFoundError: except ModuleNotFoundError:
pass # Ignore writing to journald pass # Ignore writing to journald
# Finally, print the log unless we skipped it based on level. # Finally, print the log unless we skipped it based on level.
# We use sys.stdout.write()+flush() instead of print() to try and # We use sys.stdout.write()+flush() instead of print() to try and

View File

@ -1,10 +1,15 @@
import urllib.request, urllib.parse import json
import ssl, json import ssl
import urllib.error
import urllib.parse
import urllib.request
from .exceptions import * from .exceptions import *
BASE_URL = 'https://archlinux.org/packages/search/json/?name={package}' BASE_URL = 'https://archlinux.org/packages/search/json/?name={package}'
BASE_GROUP_URL = 'https://archlinux.org/groups/x86_64/{group}/' BASE_GROUP_URL = 'https://archlinux.org/groups/x86_64/{group}/'
def find_group(name): def find_group(name):
ssl_context = ssl.create_default_context() ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False ssl_context.check_hostname = False
@ -21,6 +26,7 @@ def find_group(name):
if response.code == 200: if response.code == 200:
return True return True
def find_package(name): def find_package(name):
""" """
Finds a specific package via the package database. Finds a specific package via the package database.
@ -33,6 +39,7 @@ def find_package(name):
data = response.read().decode('UTF-8') data = response.read().decode('UTF-8')
return json.loads(data) return json.loads(data)
def find_packages(*names): def find_packages(*names):
""" """
This function returns the search results for many packages. This function returns the search results for many packages.
@ -44,7 +51,8 @@ def find_packages(*names):
result[package] = find_package(package) result[package] = find_package(package)
return result return result
def validate_package_list(packages :list):
def validate_package_list(packages: list):
""" """
Validates a list of given packages. Validates a list of given packages.
Raises `RequirementError` if one or more packages are not found. Raises `RequirementError` if one or more packages are not found.

View File

@ -1,20 +1,28 @@
import os, urllib.request, urllib.parse, ssl, json, re import hashlib
import importlib.util, sys, glob, hashlib, logging import importlib.util
from collections import OrderedDict import json
from .general import multisplit, sys_command import re
from .exceptions import * import ssl
import sys
import urllib.error
import urllib.parse
import urllib.request
from typing import Optional
from .general import multisplit
from .networking import * from .networking import *
from .output import log
from .storage import storage from .storage import storage
def grab_url_data(path): def grab_url_data(path):
safe_path = path[:path.find(':')+1]+''.join([item if item in ('/', '?', '=', '&') else urllib.parse.quote(item) for item in multisplit(path[path.find(':')+1:], ('/', '?', '=', '&'))]) safe_path = path[: path.find(':') + 1] + ''.join([item if item in ('/', '?', '=', '&') else urllib.parse.quote(item) for item in multisplit(path[path.find(':') + 1:], ('/', '?', '=', '&'))])
ssl_context = ssl.create_default_context() ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False ssl_context.check_hostname = False
ssl_context.verify_mode=ssl.CERT_NONE ssl_context.verify_mode = ssl.CERT_NONE
response = urllib.request.urlopen(safe_path, context=ssl_context) response = urllib.request.urlopen(safe_path, context=ssl_context)
return response.read() return response.read()
def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_profiles=False): def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_profiles=False):
# TODO: Grab from github page as well, not just local static files # TODO: Grab from github page as well, not just local static files
if filter_irrelevant_macs: if filter_irrelevant_macs:
@ -23,8 +31,10 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof
cache = {} cache = {}
# Grab all local profiles found in PROFILE_PATH # Grab all local profiles found in PROFILE_PATH
for PATH_ITEM in storage['PROFILE_PATH']: for PATH_ITEM in storage['PROFILE_PATH']:
for root, folders, files in os.walk(os.path.abspath(os.path.expanduser(PATH_ITEM+subpath))): for root, folders, files in os.walk(os.path.abspath(os.path.expanduser(PATH_ITEM + subpath))):
for file in files: for file in files:
if file == '__init__.py':
continue
if os.path.splitext(file)[1] == '.py': if os.path.splitext(file)[1] == '.py':
tailored = False tailored = False
if len(mac := re.findall('(([a-zA-z0-9]{2}[-:]){5}([a-zA-z0-9]{2}))', file)): if len(mac := re.findall('(([a-zA-z0-9]{2}[-:]){5}([a-zA-z0-9]{2}))', file)):
@ -35,21 +45,21 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof
description = '' description = ''
with open(os.path.join(root, file), 'r') as fh: with open(os.path.join(root, file), 'r') as fh:
first_line = fh.readline() first_line = fh.readline()
if first_line[0] == '#': if len(first_line) and first_line[0] == '#':
description = first_line[1:].strip() description = first_line[1:].strip()
cache[file[:-3]] = {'path' : os.path.join(root, file), 'description' : description, 'tailored' : tailored} cache[file[:-3]] = {'path': os.path.join(root, file), 'description': description, 'tailored': tailored}
break break
# Grab profiles from upstream URL # Grab profiles from upstream URL
if storage['PROFILE_DB']: if storage['PROFILE_DB']:
profiles_url = os.path.join(storage["UPSTREAM_URL"]+subpath, storage['PROFILE_DB']) profiles_url = os.path.join(storage["UPSTREAM_URL"] + subpath, storage['PROFILE_DB'])
try: try:
profile_list = json.loads(grab_url_data(profiles_url)) profile_list = json.loads(grab_url_data(profiles_url))
except urllib.error.HTTPError as err: except urllib.error.HTTPError as err:
print(f'Error: Listing profiles on URL "{profiles_url}" resulted in:', err) print(f'Error: Listing profiles on URL "{profiles_url}" resulted in:', err)
return cache return cache
except: except json.decoder.JSONDecodeError as err:
print(f'Error: Could not decode "{profiles_url}" result as JSON:', err) print(f'Error: Could not decode "{profiles_url}" result as JSON:', err)
return cache return cache
@ -61,16 +71,17 @@ def list_profiles(filter_irrelevant_macs=True, subpath='', filter_top_level_prof
continue continue
tailored = True tailored = True
cache[profile[:-3]] = {'path' : os.path.join(storage["UPSTREAM_URL"]+subpath, profile), 'description' : profile_list[profile], 'tailored' : tailored} cache[profile[:-3]] = {'path': os.path.join(storage["UPSTREAM_URL"] + subpath, profile), 'description': profile_list[profile], 'tailored': tailored}
if filter_top_level_profiles: if filter_top_level_profiles:
for profile in list(cache.keys()): for profile in list(cache.keys()):
if Profile(None, profile).is_top_level_profile() is False: if Profile(None, profile).is_top_level_profile() is False:
del(cache[profile]) del cache[profile]
return cache return cache
class Script():
class Script:
def __init__(self, profile, installer=None): def __init__(self, profile, installer=None):
# profile: https://hvornum.se/something.py # profile: https://hvornum.se/something.py
# profile: desktop # profile: desktop
@ -144,19 +155,22 @@ class Script():
return self return self
def execute(self): def execute(self):
if not self.namespace in sys.modules or self.spec is None: if self.namespace not in sys.modules or self.spec is None:
self.load_instructions() self.load_instructions()
self.spec.loader.exec_module(sys.modules[self.namespace]) self.spec.loader.exec_module(sys.modules[self.namespace])
return sys.modules[self.namespace] return sys.modules[self.namespace]
class Profile(Script): class Profile(Script):
def __init__(self, installer, path, args={}): def __init__(self, installer, path, args=None):
super(Profile, self).__init__(path, installer) super(Profile, self).__init__(path, installer)
if args is None:
args = {}
def __dump__(self, *args, **kwargs): def __dump__(self, *args, **kwargs):
return {'path' : self.path} return {'path': self.path}
def __repr__(self, *args, **kwargs): def __repr__(self, *args, **kwargs):
return f'Profile({os.path.basename(self.profile)})' return f'Profile({os.path.basename(self.profile)})'
@ -215,7 +229,7 @@ class Profile(Script):
return True return True
@property @property
def packages(self) -> list: def packages(self) -> Optional[list]:
""" """
Returns a list of packages baked into the profile definition. Returns a list of packages baked into the profile definition.
If no package definition has been done, .packages() will return None. If no package definition has been done, .packages() will return None.
@ -235,6 +249,7 @@ class Profile(Script):
return imported.__packages__ return imported.__packages__
return None return None
class Application(Profile): class Application(Profile):
def __repr__(self, *args, **kwargs): def __repr__(self, *args, **kwargs):
return f'Application({os.path.basename(self.profile)})' return f'Application({os.path.basename(self.profile)})'

View File

@ -1,12 +1,10 @@
import os
from .exceptions import *
from .general import * from .general import *
def service_state(service_name: str): def service_state(service_name: str):
if os.path.splitext(service_name)[1] != '.service': if os.path.splitext(service_name)[1] != '.service':
service_name += '.service' # Just to be safe service_name += '.service' # Just to be safe
state = b''.join(sys_command(f'systemctl show --no-pager -p SubState --value {service_name}', environment_vars={'SYSTEMD_COLORS' : '0'})) state = b''.join(SysCommand(f'systemctl show --no-pager -p SubState --value {service_name}', environment_vars={'SYSTEMD_COLORS': '0'}))
return state.strip().decode('UTF-8') return state.strip().decode('UTF-8')

View File

@ -8,15 +8,15 @@ import os
# #
# And Keeping this in dict ensures that variables are shared across imports. # And Keeping this in dict ensures that variables are shared across imports.
storage = { storage = {
'PROFILE_PATH' : [ 'PROFILE_PATH': [
'./profiles', './profiles',
'~/.config/archinstall/profiles', '~/.config/archinstall/profiles',
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'profiles'), os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'profiles'),
#os.path.abspath(f'{os.path.dirname(__file__)}/../examples') # os.path.abspath(f'{os.path.dirname(__file__)}/../examples')
], ],
'UPSTREAM_URL' : 'https://raw.githubusercontent.com/archlinux/archinstall/master/profiles', 'UPSTREAM_URL': 'https://raw.githubusercontent.com/archlinux/archinstall/master/profiles',
'PROFILE_DB' : None, # Used in cases when listing profiles is desired, not mandatory for direct profile grabing. 'PROFILE_DB': None, # Used in cases when listing profiles is desired, not mandatory for direct profile grabing.
'LOG_PATH' : '/var/log/archinstall', 'LOG_PATH': '/var/log/archinstall',
'LOG_FILE' : 'install.log', 'LOG_FILE': 'install.log',
'MOUNT_POINT' : '/mnt' 'MOUNT_POINT': '/mnt',
} }

View File

@ -1,4 +1,12 @@
class Ini(): import logging
from .general import SysCommand, SysCommandWorker, locate_binary
from .installer import Installer
from .output import log
from .storage import storage
class Ini:
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
""" """
Limited INI handler for now. Limited INI handler for now.
@ -25,12 +33,89 @@ class Ini():
return result return result
class Systemd(Ini): class Systemd(Ini):
""" """
Placeholder class to do systemd specific setups. Placeholder class to do systemd specific setups.
""" """
class Networkd(Systemd): class Networkd(Systemd):
""" """
Placeholder class to do systemd-network specific setups. Placeholder class to do systemd-network specific setups.
""" """
class Boot:
def __init__(self, installation: Installer):
self.instance = installation
self.container_name = 'archinstall'
self.session = None
self.ready = False
def __enter__(self):
if (existing_session := storage.get('active_boot', None)) and existing_session.instance != self.instance:
raise KeyError("Archinstall only supports booting up one instance, and a active session is already active and it is not this one.")
if existing_session:
self.session = existing_session.session
self.ready = existing_session.ready
else:
self.session = SysCommandWorker([
'/usr/bin/systemd-nspawn',
'-D', self.instance.target,
'-b',
'--machine', self.container_name
])
if not self.ready:
while self.session.is_alive():
if b' login:' in self.session:
self.ready = True
break
storage['active_boot'] = self
return self
def __exit__(self, *args, **kwargs):
# b''.join(sys_command('sync')) # No need to, since the underlying 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]:
log(args[1], level=logging.ERROR, fg='red')
log(f"The error above occured in a temporary boot-up of the installation {self.instance}", level=logging.ERROR, fg="red")
SysCommand(f'machinectl shell {self.container_name} /bin/bash -c "shutdown now"')
def __iter__(self):
if self.session:
for value in self.session:
yield value
def __contains__(self, key: bytes):
if self.session is None:
return False
return key in self.session
def is_alive(self):
if self.session is None:
return False
return self.session.is_alive()
def SysCommand(self, cmd: list, *args, **kwargs):
if cmd[0][0] != '/' and cmd[0][:2] != './':
# This check is also done in SysCommand & SysCommandWorker.
# However, that check is done for `machinectl` and not for our chroot command.
# So this wrapper for SysCommand will do this additionally.
cmd[0] = locate_binary(cmd[0])
return SysCommand(["machinectl", "shell", self.container_name, *cmd], *args, **kwargs)
def SysCommandWorker(self, cmd: list, *args, **kwargs):
if cmd[0][0] != '/' and cmd[0][:2] != './':
cmd[0] = locate_binary(cmd[0])
return SysCommandWorker(["machinectl", "shell", self.container_name, *cmd], *args, **kwargs)

View File

@ -1,27 +1,41 @@
import getpass, pathlib, os, shutil, re, time import getpass
import sys, time, signal, ipaddress, logging import ipaddress
import termios, tty, select # Used for char by char polling of sys.stdin import logging
from .exceptions import * import pathlib
from .profiles import Profile import re
from .locale_helpers import list_keyboard_languages, verify_keyboard_layout, search_keyboard_layout import select # Used for char by char polling of sys.stdin
from .output import log import shutil
from .storage import storage import signal
from .networking import list_interfaces import sys
from .general import sys_command import termios
from .hardware import AVAILABLE_GFX_DRIVERS, hasUEFI import time
import tty
from .exceptions import *
from .general import SysCommand
from .hardware import AVAILABLE_GFX_DRIVERS, has_uefi
from .locale_helpers import list_keyboard_languages, verify_keyboard_layout, search_keyboard_layout
from .networking import list_interfaces
from .output import log
from .profiles import Profile
# TODO: Some inconsistencies between the selection processes.
# Some return the keys from the options, some the values?
## TODO: Some inconsistencies between the selection processes.
## Some return the keys from the options, some the values?
def get_terminal_height(): def get_terminal_height():
return shutil.get_terminal_size().lines return shutil.get_terminal_size().lines
def get_terminal_width(): def get_terminal_width():
return shutil.get_terminal_size().columns return shutil.get_terminal_size().columns
def get_longest_option(options): def get_longest_option(options):
return max([len(x) for x in options]) return max([len(x) for x in options])
def check_for_correct_username(username): def check_for_correct_username(username):
if re.match(r'^[a-z_][a-z0-9_-]*\$?$', username) and len(username) <= 32: if re.match(r'^[a-z_][a-z0-9_-]*\$?$', username) and len(username) <= 32:
return True return True
@ -32,8 +46,10 @@ def check_for_correct_username(username):
) )
return False return False
def do_countdown(): def do_countdown():
SIG_TRIGGER = False SIG_TRIGGER = False
def kill_handler(sig, frame): def kill_handler(sig, frame):
print() print()
exit(0) exit(0)
@ -67,8 +83,9 @@ def do_countdown():
signal.signal(signal.SIGINT, original_sigint_handler) signal.signal(signal.SIGINT, original_sigint_handler)
return True return True
def get_password(prompt="Enter a password: "): def get_password(prompt="Enter a password: "):
while (passwd := getpass.getpass(prompt)): while passwd := getpass.getpass(prompt):
passwd_verification = getpass.getpass(prompt='And one more time for verification: ') passwd_verification = getpass.getpass(prompt='And one more time for verification: ')
if passwd != passwd_verification: if passwd != passwd_verification:
log(' * Passwords did not match * ', fg='red') log(' * Passwords did not match * ', fg='red')
@ -80,26 +97,41 @@ def get_password(prompt="Enter a password: "):
return passwd return passwd
return None return None
def print_large_list(options, padding=5, margin_bottom=0, separator=': '): def print_large_list(options, padding=5, margin_bottom=0, separator=': '):
highest_index_number_length = len(str(len(options))) highest_index_number_length = len(str(len(options)))
longest_line = highest_index_number_length + len(separator) + get_longest_option(options) + padding longest_line = highest_index_number_length + len(separator) + get_longest_option(options) + padding
spaces_without_option = longest_line - (len(separator) + highest_index_number_length)
max_num_of_columns = get_terminal_width() // longest_line max_num_of_columns = get_terminal_width() // longest_line
max_options_in_cells = max_num_of_columns * (get_terminal_height()-margin_bottom) max_options_in_cells = max_num_of_columns * (get_terminal_height() - margin_bottom)
if (len(options) > max_options_in_cells): if len(options) > max_options_in_cells:
for index, option in enumerate(options): for index, option in enumerate(options):
print(f"{index}: {option}") print(f"{index}: {option}")
return 1, index
else: else:
for row in range(0, (get_terminal_height()-margin_bottom)): for row in range(0, (get_terminal_height() - margin_bottom)):
for column in range(row, len(options), (get_terminal_height()-margin_bottom)): for column in range(row, len(options), (get_terminal_height() - margin_bottom)):
spaces = " "*(longest_line - len(options[column])) spaces = " " * (spaces_without_option - len(options[column]))
print(f"{str(column): >{highest_index_number_length}}{separator}{options[column]}", end = spaces) print(f"{str(column): >{highest_index_number_length}}{separator}{options[column]}", end=spaces)
print() print()
return column, row return column, row
def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=True, default=None, allow_empty=False): def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=True, default=None, allow_empty=False):
# Checking if the options are different from `list` or `dict` or if they are empty
if type(options) not in [list, dict]:
log(f" * Generic multi-select doesn't support ({type(options)}) as type of options * ", fg='red')
log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow')
raise RequirementError("generic_multi_select() requires list or dictionary as options.")
if not options:
log(" * Generic multi-select didn't find any options to choose from * ", fg='red')
log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow')
raise RequirementError('generic_multi_select() requires at least one option to proceed.')
# After passing the checks, function continues to work
if type(options) == dict:
options = list(options.values())
if sort: if sort:
options = sorted(options) options = sorted(options)
@ -108,7 +140,7 @@ def generic_multi_select(options, text="Select one or more of the options above
selected_options = [] selected_options = []
while True: while True:
if len(selected_options) <= 0 and default and default in options: if not selected_options and default in options:
selected_options.append(default) selected_options.append(default)
printed_options = [] printed_options = []
@ -118,33 +150,43 @@ def generic_multi_select(options, text="Select one or more of the options above
else: else:
printed_options.append(f'{option}') printed_options.append(f'{option}')
section.clear(0, get_terminal_height()-section._cursor_y-1) section.clear(0, get_terminal_height() - section._cursor_y - 1)
x, y = print_large_list(printed_options, margin_bottom=2) print_large_list(printed_options, margin_bottom=2)
section._cursor_y = len(printed_options) section._cursor_y = len(printed_options)
section._cursor_x = 0 section._cursor_x = 0
section.write_line(text) section.write_line(text)
section.input_pos = section._cursor_x section.input_pos = section._cursor_x
selected_option = section.get_keyboard_input(end=None) selected_option = section.get_keyboard_input(end=None)
# This string check is necessary to correct work with it
if selected_option is None: # Without this, Python will raise AttributeError because of stripping `None`
if len(selected_options) <= 0 and default: # It also allows to remove empty spaces if the user accidentally entered them.
selected_options = [default] if isinstance(selected_option, str):
selected_option = selected_option.strip()
if len(selected_options) or allow_empty is True: try:
break if not selected_option:
if not selected_options and default:
selected_options = [default]
elif selected_options or allow_empty:
break
else:
raise RequirementError('Please select at least one option to continue')
elif selected_option.isnumeric():
if (selected_option := int(selected_option)) >= len(options):
raise RequirementError(f'Selected option "{selected_option}" is out of range')
selected_option = options[selected_option]
if selected_option in selected_options:
selected_options.remove(selected_option)
else:
selected_options.append(selected_option)
elif selected_option in options:
if selected_option in selected_options:
selected_options.remove(selected_option)
else:
selected_options.append(selected_option)
else: else:
log('* Need to select at least one option!', fg='red') raise RequirementError(f'Selected option "{selected_option}" does not exist in available options')
continue except RequirementError as e:
log(f" * {e} * ", fg='red')
elif selected_option.isdigit():
if (selected_option := int(selected_option)) >= len(options):
log('* Option is out of range, please select another one!', fg='red')
continue
selected_option = options[selected_option]
if selected_option in selected_options:
selected_options.remove(selected_option)
else:
selected_options.append(selected_option)
sys.stdout.write('\n') sys.stdout.write('\n')
sys.stdout.flush() sys.stdout.flush()
@ -165,7 +207,7 @@ def select_encrypted_partitions(blockdevices :dict) -> dict:
print(generic_multi_select(options, f"Choose which partitions to encrypt (leave blank when done): ")) print(generic_multi_select(options, f"Choose which partitions to encrypt (leave blank when done): "))
class MiniCurses(): class MiniCurses:
def __init__(self, width, height): def __init__(self, width, height):
self.width = width self.width = width
self.height = height self.height = height
@ -180,7 +222,7 @@ class MiniCurses():
sys.stdout.flush() sys.stdout.flush()
sys.stdout.write("\033[%dG" % 0) sys.stdout.write("\033[%dG" % 0)
sys.stdout.flush() sys.stdout.flush()
sys.stdout.write(" " * (get_terminal_width()-1)) sys.stdout.write(" " * (get_terminal_width() - 1))
sys.stdout.flush() sys.stdout.flush()
sys.stdout.write("\033[%dG" % 0) sys.stdout.write("\033[%dG" % 0)
sys.stdout.flush() sys.stdout.flush()
@ -189,36 +231,38 @@ class MiniCurses():
self._cursor_x += len(text) self._cursor_x += len(text)
def clear(self, x, y): def clear(self, x, y):
if x < 0: x = 0 if x < 0:
if y < 0: y = 0 x = 0
if y < 0:
y = 0
#import time # import time
#sys.stdout.write(f"Clearing from: {x, y}") # sys.stdout.write(f"Clearing from: {x, y}")
#sys.stdout.flush() # sys.stdout.flush()
#time.sleep(2) # time.sleep(2)
sys.stdout.flush() sys.stdout.flush()
sys.stdout.write('\033[%d;%df' % (y, x)) sys.stdout.write('\033[%d;%df' % (y, x))
for line in range(get_terminal_height()-y-1, y): for line in range(get_terminal_height() - y - 1, y):
sys.stdout.write(" " * (get_terminal_width()-1)) sys.stdout.write(" " * (get_terminal_width() - 1))
sys.stdout.flush() sys.stdout.flush()
sys.stdout.write('\033[%d;%df' % (y, x)) sys.stdout.write('\033[%d;%df' % (y, x))
sys.stdout.flush() sys.stdout.flush()
def deal_with_control_characters(self, char): def deal_with_control_characters(self, char):
mapper = { mapper = {
'\x7f' : 'BACKSPACE', '\x7f': 'BACKSPACE',
'\r' : 'CR', '\r': 'CR',
'\n' : 'NL' '\n': 'NL'
} }
if (mapped_char := mapper.get(char, None)) == 'BACKSPACE': if (mapped_char := mapper.get(char, None)) == 'BACKSPACE':
if self._cursor_x <= self.input_pos: if self._cursor_x <= self.input_pos:
# Don't backspace futher back than the cursor start position during input # Don't backspace further back than the cursor start position during input
return True return True
# Move back to the current known position (BACKSPACE doesn't updated x-pos) # Move back to the current known position (BACKSPACE doesn't updated x-pos)
sys.stdout.flush() sys.stdout.flush()
sys.stdout.write("\033[%dG" % (self._cursor_x)) sys.stdout.write("\033[%dG" % self._cursor_x)
sys.stdout.flush() sys.stdout.flush()
# Write a blank space # Write a blank space
@ -228,7 +272,7 @@ class MiniCurses():
# And move back again # And move back again
sys.stdout.flush() sys.stdout.flush()
sys.stdout.write("\033[%dG" % (self._cursor_x)) sys.stdout.write("\033[%dG" % self._cursor_x)
sys.stdout.flush() sys.stdout.flush()
self._cursor_x -= 1 self._cursor_x -= 1
@ -251,16 +295,16 @@ class MiniCurses():
poller.register(sys.stdin.fileno(), select.EPOLLIN) poller.register(sys.stdin.fileno(), select.EPOLLIN)
EOF = False eof = False
while EOF is False: while eof is False:
for fileno, event in poller.poll(0.025): for fileno, event in poller.poll(0.025):
char = sys.stdin.read(1) char = sys.stdin.read(1)
#sys.stdout.write(f"{[char]}") # sys.stdout.write(f"{[char]}")
#sys.stdout.flush() # sys.stdout.flush()
if (newline := (char in ('\n', '\r'))): if newline := (char in ('\n', '\r')):
EOF = True eof = True
if not newline or strip_rowbreaks is False: if not newline or strip_rowbreaks is False:
response += char response += char
@ -279,6 +323,7 @@ class MiniCurses():
if response: if response:
return response return response
def ask_for_superuser_account(prompt='Username for required superuser with sudo privileges: ', forced=False): def ask_for_superuser_account(prompt='Username for required superuser with sudo privileges: ', forced=False):
while 1: while 1:
new_user = input(prompt).strip(' ') new_user = input(prompt).strip(' ')
@ -294,7 +339,8 @@ def ask_for_superuser_account(prompt='Username for required superuser with sudo
continue continue
password = get_password(prompt=f'Password for user {new_user}: ') password = get_password(prompt=f'Password for user {new_user}: ')
return {new_user: {"!password" : password}} return {new_user: {"!password": password}}
def ask_for_additional_users(prompt='Any additional users to install (leave blank for no users): '): def ask_for_additional_users(prompt='Any additional users to install (leave blank for no users): '):
users = {} users = {}
@ -309,18 +355,19 @@ def ask_for_additional_users(prompt='Any additional users to install (leave blan
password = get_password(prompt=f'Password for user {new_user}: ') password = get_password(prompt=f'Password for user {new_user}: ')
if input("Should this user be a superuser (sudoer) [y/N]: ").strip(' ').lower() in ('y', 'yes'): if input("Should this user be a superuser (sudoer) [y/N]: ").strip(' ').lower() in ('y', 'yes'):
superusers[new_user] = {"!password" : password} superusers[new_user] = {"!password": password}
else: else:
users[new_user] = {"!password" : password} users[new_user] = {"!password": password}
return users, superusers return users, superusers
def ask_for_a_timezone(): def ask_for_a_timezone():
while True: while True:
timezone = input('Enter a valid timezone (examples: Europe/Stockholm, US/Eastern) or press enter to use UTC: ').strip().strip('*.') timezone = input('Enter a valid timezone (examples: Europe/Stockholm, US/Eastern) or press enter to use UTC: ').strip().strip('*.')
if timezone == '': if timezone == '':
timezone = 'UTC' timezone = 'UTC'
if (pathlib.Path("/usr")/"share"/"zoneinfo"/timezone).exists(): if (pathlib.Path("/usr") / "share" / "zoneinfo" / timezone).exists():
return timezone return timezone
else: else:
log( log(
@ -329,38 +376,41 @@ def ask_for_a_timezone():
fg='red' fg='red'
) )
def ask_for_bootloader() -> str: def ask_for_bootloader() -> str:
bootloader = "systemd-bootctl" bootloader = "systemd-bootctl"
if hasUEFI()==False: if not has_uefi():
bootloader="grub-install" bootloader = "grub-install"
else: else:
bootloader_choice = input("Would you like to use GRUB as a bootloader instead of systemd-boot? [y/N] ").lower() bootloader_choice = input("Would you like to use GRUB as a bootloader instead of systemd-boot? [y/N] ").lower()
if bootloader_choice == "y": if bootloader_choice == "y":
bootloader="grub-install" bootloader = "grub-install"
return bootloader return bootloader
def ask_for_audio_selection(): def ask_for_audio_selection():
audio = "pulseaudio" # Default for most desktop environments audio = "pulseaudio" # Default for most desktop environments
pipewire_choice = input("Would you like to install pipewire instead of pulseaudio as the default audio server? [Y/n] ").lower() pipewire_choice = input("Would you like to install pipewire instead of pulseaudio as the default audio server? [Y/n] ").lower()
if pipewire_choice in ("y", ""): if pipewire_choice in ("y", ""):
audio = "pipewire" audio = "pipewire"
return audio return audio
def ask_to_configure_network(): def ask_to_configure_network():
# Optionally configure one network interface. # Optionally configure one network interface.
#while 1: # while 1:
# {MAC: Ifname} # {MAC: Ifname}
interfaces = { interfaces = {
'ISO-CONFIG' : 'Copy ISO network configuration to installation', 'ISO-CONFIG': 'Copy ISO network configuration to installation',
'NetworkManager':'Use NetworkManager to control and manage your internet connection', 'NetworkManager': 'Use NetworkManager to control and manage your internet connection',
**list_interfaces() **list_interfaces()
} }
nic = generic_select(interfaces, "Select one network interface to configure (leave blank to skip): ") nic = generic_select(interfaces, "Select one network interface to configure (leave blank to skip): ")
if nic and nic != 'Copy ISO network configuration to installation': if nic and nic != 'Copy ISO network configuration to installation':
if nic == 'Use NetworkManager to control and manage your internet connection': if nic == 'Use NetworkManager to control and manage your internet connection':
return {'nic': nic,'NetworkManager':True} return {'nic': nic, 'NetworkManager': True}
# Current workaround: # Current workaround:
# For selecting modes without entering text within brackets, # For selecting modes without entering text within brackets,
@ -370,8 +420,7 @@ def ask_to_configure_network():
for index, mode in enumerate(modes): for index, mode in enumerate(modes):
print(f"{index}: {mode}") print(f"{index}: {mode}")
mode = generic_select(['DHCP', 'IP'], f"Select which mode to configure for {nic} or leave blank for DHCP: ", mode = generic_select(['DHCP', 'IP'], f"Select which mode to configure for {nic} or leave blank for DHCP: ", options_output=False)
options_output=False)
if mode == 'IP': if mode == 'IP':
while 1: while 1:
ip = input(f"Enter the IP and subnet for {nic} (example: 192.168.0.5/24): ").strip() ip = input(f"Enter the IP and subnet for {nic} (example: 192.168.0.5/24): ").strip()
@ -406,7 +455,7 @@ def ask_to_configure_network():
if len(dns_input := input('Enter your DNS servers (space separated, blank for none): ').strip()): if len(dns_input := input('Enter your DNS servers (space separated, blank for none): ').strip()):
dns = dns_input.split(' ') dns = dns_input.split(' ')
return {'nic': nic, 'dhcp': False, 'ip': ip, 'gateway' : gateway, 'dns' : dns} return {'nic': nic, 'dhcp': False, 'ip': ip, 'gateway': gateway, 'dns': dns}
else: else:
return {'nic': nic} return {'nic': nic}
elif nic: elif nic:
@ -414,29 +463,30 @@ def ask_to_configure_network():
return {} return {}
def ask_for_disk_layout(): def ask_for_disk_layout():
options = { options = {
'keep-existing' : 'Keep existing partition layout and select which ones to use where', 'keep-existing': 'Keep existing partition layout and select which ones to use where',
'format-all' : 'Format entire drive and setup a basic partition scheme', 'format-all': 'Format entire drive and setup a basic partition scheme',
'abort' : 'Abort the installation' 'abort': 'Abort the installation',
} }
value = generic_select(options, "Found partitions on the selected drive, (select by number) what you want to do: ", value = generic_select(options, "Found partitions on the selected drive, (select by number) what you want to do: ", allow_empty_input=False, sort=True)
allow_empty_input=False, sort=True)
return next((key for key, val in options.items() if val == value), None) return next((key for key, val in options.items() if val == value), None)
def ask_for_main_filesystem_format(): def ask_for_main_filesystem_format():
options = { options = {
'btrfs' : 'btrfs', 'btrfs': 'btrfs',
'ext4' : 'ext4', 'ext4': 'ext4',
'xfs' : 'xfs', 'xfs': 'xfs',
'f2fs' : 'f2fs' 'f2fs': 'f2fs'
} }
value = generic_select(options, "Select which filesystem your main partition should use (by number or name): ", value = generic_select(options, "Select which filesystem your main partition should use (by number or name): ", allow_empty_input=False)
allow_empty_input=False)
return next((key for key, val in options.items() if val == value), None) 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: ", allow_empty_input=True, options_output=True, sort=False): def generic_select(options, input_text="Select one of the above by index or absolute value: ", allow_empty_input=True, options_output=True, sort=False):
""" """
A generic select function that does not output anything A generic select function that does not output anything
@ -451,21 +501,23 @@ def generic_select(options, input_text="Select one of the above by index or abso
this function returns an item from list, a string, or None this function returns an item from list, a string, or None
""" """
# Checking if options are different from `list` or `dict` # Checking if the options are different from `list` or `dict` or if they are empty
if type(options) not in [list, dict]: if type(options) not in [list, dict]:
log(f" * Generic select doesn't support ({type(options)}) as type of options * ", fg='red') log(f" * Generic select doesn't support ({type(options)}) as type of options * ", fg='red')
log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow')
raise RequirementError("generic_select() requires list or dictionary as options.") raise RequirementError("generic_select() requires list or dictionary as options.")
# To allow only `list` and `dict`, converting values of options here. if not options:
# Therefore, now we can only provide the dictionary itself log(" * Generic select didn't find any options to choose from * ", fg='red')
if type(options) == dict: options = list(options.values())
if sort: options = sorted(options) # As we pass only list and dict (converted to list), we can skip converting to list
options = [x for x in options if x] # Clean it up from empty options
if len(options) == 0:
log(f" * Generic select didn't find any options to choose from * ", fg='red')
log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow')
raise RequirementError('generic_select() requires at least one option to proceed.') raise RequirementError('generic_select() requires at least one option to proceed.')
# After passing the checks, function continues to work
if type(options) == dict:
# To allow only `list` and `dict`, converting values of options here.
# Therefore, now we can only provide the dictionary itself
options = list(options.values())
if sort:
# As we pass only list and dict (converted to list), we can skip converting to list
options = sorted(options)
# Added ability to disable the output of options items, # Added ability to disable the output of options items,
# if another function displays something different from this # if another function displays something different from this
@ -477,8 +529,8 @@ def generic_select(options, input_text="Select one of the above by index or abso
# Now the try...except block handles validation for invalid input from the user # Now the try...except block handles validation for invalid input from the user
while True: while True:
try: try:
selected_option = input(input_text) selected_option = input(input_text).strip()
if len(selected_option.strip()) == 0: if not selected_option:
# `allow_empty_input` parameter handles return of None on empty input, if necessary # `allow_empty_input` parameter handles return of None on empty input, if necessary
# Otherwise raise `RequirementError` # Otherwise raise `RequirementError`
if allow_empty_input: if allow_empty_input:
@ -486,18 +538,16 @@ def generic_select(options, input_text="Select one of the above by index or abso
raise RequirementError('Please select an option to continue') raise RequirementError('Please select an option to continue')
# Replaced `isdigit` with` isnumeric` to discard all negative numbers # Replaced `isdigit` with` isnumeric` to discard all negative numbers
elif selected_option.isnumeric(): elif selected_option.isnumeric():
selected_option = int(selected_option) if (selected_option := int(selected_option)) >= len(options):
if selected_option >= len(options):
raise RequirementError(f'Selected option "{selected_option}" is out of range') raise RequirementError(f'Selected option "{selected_option}" is out of range')
selected_option = options[selected_option] selected_option = options[selected_option]
break break
elif selected_option in options: elif selected_option in options:
break # We gave a correct absolute value break # We gave a correct absolute value
else: else:
raise RequirementError(f'Selected option "{selected_option}" does not exist in available options') raise RequirementError(f'Selected option "{selected_option}" does not exist in available options')
except RequirementError as err: except RequirementError as err:
log(f" * {err} * ", fg='red') log(f" * {err} * ", fg='red')
continue
return selected_option return selected_option
@ -765,6 +815,7 @@ def select_disk_layout(block_devices :list):
else: else:
return select_individual_blockdevice_usage(block_devices) return select_individual_blockdevice_usage(block_devices)
def select_disk(dict_o_disks): def select_disk(dict_o_disks):
""" """
Asks the user to select a harddrive from the `dict_o_disks` selection. Asks the user to select a harddrive from the `dict_o_disks` selection.
@ -781,9 +832,8 @@ def select_disk(dict_o_disks):
for index, drive in enumerate(drives): for index, drive in enumerate(drives):
print(f"{index}: {drive} ({dict_o_disks[drive]['size'], dict_o_disks[drive].device, dict_o_disks[drive]['label']})") print(f"{index}: {drive} ({dict_o_disks[drive]['size'], dict_o_disks[drive].device, dict_o_disks[drive]['label']})")
log(f"You can skip selecting a drive and partitioning and use whatever drive-setup is mounted at /mnt (experimental)", fg="yellow") log("You can skip selecting a drive and partitioning and use whatever drive-setup is mounted at /mnt (experimental)", fg="yellow")
drive = generic_select(drives, 'Select one of the above disks (by name or number) or leave blank to use /mnt: ', drive = generic_select(drives, 'Select one of the above disks (by name or number) or leave blank to use /mnt: ', options_output=False)
options_output=False)
if not drive: if not drive:
return drive return drive
@ -792,6 +842,7 @@ def select_disk(dict_o_disks):
raise DiskError('select_disk() requires a non-empty dictionary of disks to select from.') raise DiskError('select_disk() requires a non-empty dictionary of disks to select from.')
def select_profile(options): def select_profile(options):
""" """
Asks the user to select a profile from the `options` dictionary parameter. Asks the user to select a profile from the `options` dictionary parameter.
@ -813,14 +864,14 @@ def select_profile(options):
print(' -- They might make it easier to install things like desktop environments. --') print(' -- They might make it easier to install things like desktop environments. --')
print(' -- (Leave blank and hit enter to skip this step and continue) --') print(' -- (Leave blank and hit enter to skip this step and continue) --')
selected_profile = generic_select(profiles, 'Enter a pre-programmed profile name if you want to install one: ', selected_profile = generic_select(profiles, 'Enter a pre-programmed profile name if you want to install one: ', options_output=False)
options_output=False)
if selected_profile: if selected_profile:
return Profile(None, selected_profile) return Profile(None, selected_profile)
else: else:
raise RequirementError("Selecting profiles require a least one profile to be given as an option.") raise RequirementError("Selecting profiles require a least one profile to be given as an option.")
def select_language(options, show_only_country_codes=True):
def select_language(options, show_only_country_codes=True, input_text='Select one of the above keyboard languages (by number or full name): '):
""" """
Asks the user to select a language from the `options` dictionary parameter. Asks the user to select a language from the `options` dictionary parameter.
Usually this is combined with :ref:`archinstall.list_keyboard_languages`. Usually this is combined with :ref:`archinstall.list_keyboard_languages`.
@ -834,7 +885,7 @@ def select_language(options, show_only_country_codes=True):
:return: The language/dictionary key of the selected language :return: The language/dictionary key of the selected language
:rtype: str :rtype: str
""" """
DEFAULT_KEYBOARD_LANGUAGE = 'us' default_keyboard_language = 'us'
if show_only_country_codes: if show_only_country_codes:
languages = sorted([language for language in list(options) if len(language) == 2]) languages = sorted([language for language in list(options) if len(language) == 2])
@ -842,16 +893,15 @@ def select_language(options, show_only_country_codes=True):
languages = sorted(list(options)) languages = sorted(list(options))
if len(languages) >= 1: if len(languages) >= 1:
for index, language in enumerate(languages): print_large_list(languages, margin_bottom=4)
print(f"{index}: {language}")
print(" -- You can choose a layout that isn't in this list, but whose name you know --") print(" -- You can choose a layout that isn't in this list, but whose name you know --")
print(" -- Also, you can enter '?' or 'help' to search for more languages, or skip to use US layout --") print(f" -- Also, you can enter '?' or 'help' to search for more languages, or skip to use {default_keyboard_language} layout --")
while True: while True:
selected_language = input('Select one of the above keyboard languages (by name or full name): ') selected_language = input(input_text)
if not selected_language: if not selected_language:
return DEFAULT_KEYBOARD_LANGUAGE return default_keyboard_language
elif selected_language.lower() in ('?', 'help'): elif selected_language.lower() in ('?', 'help'):
while True: while True:
filter_string = input("Search for layout containing (example: \"sv-\") or enter 'exit' to exit from search: ") filter_string = input("Search for layout containing (example: \"sv-\") or enter 'exit' to exit from search: ")
@ -879,6 +929,7 @@ def select_language(options, show_only_country_codes=True):
raise RequirementError("Selecting languages require a least one language to be given as an option.") raise RequirementError("Selecting languages require a least one language to be given as an option.")
def select_mirror_regions(mirrors, show_top_mirrors=True): def select_mirror_regions(mirrors, show_top_mirrors=True):
""" """
Asks the user to select a mirror or region from the `mirrors` dictionary parameter. Asks the user to select a mirror or region from the `mirrors` dictionary parameter.
@ -902,8 +953,7 @@ def select_mirror_regions(mirrors, show_top_mirrors=True):
print_large_list(regions, margin_bottom=4) print_large_list(regions, margin_bottom=4)
print(' -- You can skip this step by leaving the option blank --') print(' -- You can skip this step by leaving the option blank --')
selected_mirror = generic_select(regions, 'Select one of the above regions to download packages from (by number or full name): ', selected_mirror = generic_select(regions, 'Select one of the above regions to download packages from (by number or full name): ', options_output=False)
options_output=False)
if not selected_mirror: if not selected_mirror:
# Returning back empty options which can be both used to # Returning back empty options which can be both used to
# do "if x:" logic as well as do `x.get('mirror', {}).get('sub', None)` chaining # do "if x:" logic as well as do `x.get('mirror', {}).get('sub', None)` chaining
@ -920,9 +970,10 @@ def select_mirror_regions(mirrors, show_top_mirrors=True):
raise RequirementError("Selecting mirror region require a least one region to be given as an option.") raise RequirementError("Selecting mirror region require a least one region to be given as an option.")
def select_driver(options=AVAILABLE_GFX_DRIVERS): def select_driver(options=AVAILABLE_GFX_DRIVERS):
""" """
Some what convoluted function, which's job is simple. Some what convoluted function, whose job is simple.
Select a graphics driver from a pre-defined set of popular options. Select a graphics driver from a pre-defined set of popular options.
(The template xorg is for beginner users, not advanced, and should (The template xorg is for beginner users, not advanced, and should
@ -930,10 +981,10 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS):
""" """
drivers = sorted(list(options)) drivers = sorted(list(options))
default_option = options["All open-source (default)"]
if drivers: if drivers:
lspci = sys_command(f'/usr/bin/lspci') for line in SysCommand('/usr/bin/lspci'):
for line in lspci.trace_log.split(b'\r\n'):
if b' vga ' in line.lower(): if b' vga ' in line.lower():
if b'nvidia' in line.lower(): if b'nvidia' in line.lower():
print(' ** nvidia card detected, suggested driver: nvidia **') print(' ** nvidia card detected, suggested driver: nvidia **')
@ -941,13 +992,16 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS):
print(' ** AMD card detected, suggested driver: AMD / ATI **') print(' ** AMD card detected, suggested driver: AMD / ATI **')
initial_option = generic_select(drivers, input_text="Select your graphics card driver: ") initial_option = generic_select(drivers, input_text="Select your graphics card driver: ")
if not initial_option:
return default_option
selected_driver = options[initial_option] selected_driver = options[initial_option]
if type(selected_driver) == dict: if type(selected_driver) == dict:
driver_options = sorted(list(selected_driver)) driver_options = sorted(list(selected_driver))
driver_package_group = generic_select(driver_options, f'Which driver-type do you want for {initial_option}: ', driver_package_group = generic_select(driver_options, f'Which driver-type do you want for {initial_option}: ', allow_empty_input=False)
allow_empty_input=False)
driver_package_group = selected_driver[driver_package_group] driver_package_group = selected_driver[driver_package_group]
return driver_package_group return driver_package_group
@ -956,6 +1010,7 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS):
raise RequirementError("Selecting drivers require a least one profile to be given as an option.") raise RequirementError("Selecting drivers require a least one profile to be given as an option.")
def select_kernel(options): def select_kernel(options):
""" """
Asks the user to select a kernel for system. Asks the user to select a kernel for system.
@ -967,11 +1022,11 @@ def select_kernel(options):
:rtype: string :rtype: string
""" """
DEFAULT_KERNEL = "linux" default_kernel = "linux"
kernels = sorted(list(options)) kernels = sorted(list(options))
if kernels: if kernels:
return generic_multi_select(kernels, f"Choose which kernel to use (leave blank for default: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL) return generic_multi_select(kernels, f"Choose which kernels to use (leave blank for default: {default_kernel}): ", default=default_kernel, sort=False)
raise RequirementError("Selecting kernels require a least one kernel to be given as an option.") raise RequirementError("Selecting kernels require a least one kernel to be given as an option.")

View File

@ -1,7 +1,7 @@
.. _archinstall.helpers: .. _archinstall.helpers:
.. warning:: .. warning::
All these helper functions are mostly, if not all, related to outside-installation-instructions. Meaning the calls will affect your current running system - and not touch your installed system. All these helper functions are mostly, if not all, related to outside-installation-instructions. Meaning the calls will affect your current running system - and not touch your installed system.
Profile related helpers Profile related helpers
======================= =======================

View File

@ -1,19 +1,22 @@
import os import os
import re import re
import sys import sys
sys.path.insert(0, os.path.abspath('..')) sys.path.insert(0, os.path.abspath('..'))
def process_docstring(app, what, name, obj, options, lines): def process_docstring(app, what, name, obj, options, lines):
spaces_pat = re.compile(r"( {8})") spaces_pat = re.compile(r"( {8})")
ll = [] ll = []
for l in lines: for line in lines:
ll.append(spaces_pat.sub(" ", l)) ll.append(spaces_pat.sub(" ", line))
lines[:] = ll lines[:] = ll
def setup(app): def setup(app):
app.connect('autodoc-process-docstring', process_docstring) app.connect('autodoc-process-docstring', process_docstring)
# Configuration file for the Sphinx documentation builder. # Configuration file for the Sphinx documentation builder.
# #
# This file only contains a selection of the most common options. For a full # This file only contains a selection of the most common options. For a full
@ -40,7 +43,6 @@ author = 'Anton Hvornum'
# The full version, including alpha/beta/rc tags # The full version, including alpha/beta/rc tags
release = 'v2.1.0' release = 'v2.1.0'
# -- General configuration --------------------------------------------------- # -- General configuration ---------------------------------------------------
master_doc = 'index' master_doc = 'index'
@ -61,13 +63,12 @@ templates_path = ['_templates']
# This pattern also affects html_static_path and html_extra_path. # This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
# -- Options for HTML output ------------------------------------------------- # -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for # The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes. # a list of builtin themes.
# #
#html_theme = 'alabaster' # html_theme = 'alabaster'
html_theme = 'sphinx_rtd_theme' html_theme = 'sphinx_rtd_theme'
html_logo = "_static/logo.png" html_logo = "_static/logo.png"
@ -90,18 +91,18 @@ html_split_index = True
html_show_sourcelink = False html_show_sourcelink = False
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. # If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True # html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True # html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will # If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the # contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served. # base URL from which the finished HTML is served.
#html_use_opensearch = '' # html_use_opensearch = ''
# This is the file name suffix for HTML files (e.g. ".xhtml"). # This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None # html_file_suffix = None
# Output file base name for HTML help builder. # Output file base name for HTML help builder.
htmlhelp_basename = 'archinstalldoc' htmlhelp_basename = 'archinstalldoc'
@ -110,15 +111,10 @@ htmlhelp_basename = 'archinstalldoc'
# One entry per manual page. List of tuples # One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section). # (source start file, name, description, authors, manual section).
man_pages = [ man_pages = [("index", "archinstall", u"archinstall Documentation", [u"Anton Hvornum"], 1)]
(
"index", "archinstall", u"archinstall Documentation",
[u"Anton Hvornum"], 1
)
]
# If true, show URL addresses after external links. # If true, show URL addresses after external links.
#man_show_urls = False # man_show_urls = False
# -- Options for Texinfo output ------------------------------------------------ # -- Options for Texinfo output ------------------------------------------------
@ -127,8 +123,5 @@ man_pages = [
# (source start file, target name, title, author, # (source start file, target name, title, author,
# dir menu entry, description, category) # dir menu entry, description, category)
texinfo_documents = [ texinfo_documents = [
( ("index", "archinstall", u"archinstall Documentation", u"Anton Hvornum", "archinstall", "Simple and minimal HTTP server."),
"index", "archinstall", u"archinstall Documentation",
u"Anton Hvornum", "archinstall", "Simple and minimal HTTP server."
),
] ]

View File

@ -27,7 +27,7 @@ Lets create a `test_installer` - installer as an example. This is assuming that
We begin by creating `./archinstall/examples/test_installer.py`. The placement here is important later. We begin by creating `./archinstall/examples/test_installer.py`. The placement here is important later.
This script can now already be called using `python -m archinstall test_installer` after a successful installation of the library itself. This script can now already be called using `python -m archinstall test_installer` after a successful installation of the library itself.
But the script won't do much. So we'll do something simple like list all the harddrives as an example. But the script won't do much. So we'll do something simple like list all the hard drives as an example.
To do this, we'll begin by importing `archinstall` in our `./archinstall/examples/test_installer.py` and call some functions. To do this, we'll begin by importing `archinstall` in our `./archinstall/examples/test_installer.py` and call some functions.

View File

@ -3,7 +3,7 @@ Guided installation
This is the default scripted installation you'll encounter on the official Arch Linux Archinstall package as well as the unofficial ISO found on `https://archlinux.life <https://archlinux.life>`_. It will guide your through a very basic installation of Arch Linux. This is the default scripted installation you'll encounter on the official Arch Linux Archinstall package as well as the unofficial ISO found on `https://archlinux.life <https://archlinux.life>`_. It will guide your through a very basic installation of Arch Linux.
The installer has two pre-requisits: The installer has two pre-requisites:
* A Physical or Virtual machine to install on * A Physical or Virtual machine to install on
* An active internet connection prior to running archinstall * An active internet connection prior to running archinstall

View File

@ -3,14 +3,13 @@
# New features *(v2.2.0)* # New features *(v2.2.0)*
All future work towards *`v2.2.0`* is done against `master` now.<br> All future work towards *`v2.2.0`* is done against `master` now.<br>
Any patch work to existing verions will have to create a new branch against the tagged versions. Any patch work to existing versions will have to create a new branch against the tagged versions.
# Describe your PR # Describe your PR
If the changes has been discussed in an Issue, please tag it so we can backtrace from the Issue later on.<br> If the changes has been discussed in an Issue, please tag it so that we can backtrace from the issue later on.<br>
If the PR is larger than ~20 lines, please describe it here unless described in an issue. If the PR is larger than ~20 lines, please describe it here unless described in an issue.
# Testing # Testing
Any new feature or stability improvement should be tested if possible. Any new feature or stability improvement should be tested if possible. Please follow the test instructions at the bottom of the README or use the ISO built on each PR.
Please follow the test instructions at the bottom of the README or use the ISO built on each PR.

View File

@ -0,0 +1,35 @@
{
"!root-password": "<root_password>",
"audio": null,
"bootloader": "systemd-bootctl",
"filesystem": "btrfs",
"harddrive": {
"path": "/dev/sda"
},
"hostname": "box",
"kernels": [
"linux"
],
"keyboard-language": "us",
"mirror-region": {
"Worldwide": {
"https://mirror.rackspace.com/archlinux/$repo/os/$arch": true
}
},
"nic": {
"NetworkManager": true
},
"packages": [],
"profile": null,
"superusers": {
"<username>": {
"!password": "<password>"
}
},
"timezone": "UTC",
"users": {
"<username>": {
"!password": "<password>"
}
}
}

View File

@ -0,0 +1,36 @@
{
"audio": "pipewire",
"bootloader": "systemd-bootctl",
"custom-commands": [
"cd /home/devel; git clone https://aur.archlinux.org/paru.git",
"chown -R devel:devel /home/devel/paru",
"usermod -aG docker devel"
],
"!encryption-password": "supersecret",
"filesystem": "btrfs",
"harddrive": {
"path": "/dev/nvme0n1"
},
"hostname": "development-box",
"kernels": [
"linux"
],
"keyboard-language": "us",
"mirror-region": {
"Worldwide": {
"https://mirror.rackspace.com/archlinux/$repo/os/$arch": true
}
},
"nic": {
"NetworkManager": true
},
"packages": ["docker", "git", "wget", "zsh"],
"profile": "gnome",
"superusers": {
"devel": {
"!password": "devel"
}
},
"timezone": "US/Eastern",
"users": {}
}

View File

@ -1,6 +1,12 @@
import getpass, time, json, os, logging import json
import logging
import os
import time
import archinstall import archinstall
from archinstall.lib.hardware import hasUEFI from archinstall.lib.general import run_custom_user_commands
from archinstall.lib.hardware import has_uefi
from archinstall.lib.networking import check_mirror_reachable
from archinstall.lib.profiles import Profile from archinstall.lib.profiles import Profile
if archinstall.arguments.get('help'): if archinstall.arguments.get('help'):
@ -10,11 +16,12 @@ if archinstall.arguments.get('help'):
# For support reasons, we'll log the disk layout pre installation to match against post-installation layout # For support reasons, we'll log the disk layout pre installation to match against post-installation layout
archinstall.log(f"Disk states before installing: {archinstall.disk_layouts()}", level=logging.DEBUG) archinstall.log(f"Disk states before installing: {archinstall.disk_layouts()}", level=logging.DEBUG)
def ask_user_questions(): def ask_user_questions():
""" """
First, we'll ask the user for a bunch of user input. First, we'll ask the user for a bunch of user input.
Not until we're satisfied with what we want to install Not until we're satisfied with what we want to install
will we continue with the actual installation steps. will we continue with the actual installation steps.
""" """
if not archinstall.arguments.get('keyboard-layout', None): if not archinstall.arguments.get('keyboard-layout', None):
while True: while True:
@ -41,8 +48,7 @@ def ask_user_questions():
archinstall.log(e, fg="red") archinstall.log(e, fg="red")
else: else:
selected_region = archinstall.arguments['mirror-region'] selected_region = archinstall.arguments['mirror-region']
archinstall.arguments['mirror-region'] = {selected_region : archinstall.list_mirrors()[selected_region]} archinstall.arguments['mirror-region'] = {selected_region: archinstall.list_mirrors()[selected_region]}
# Ask which harddrives/block-devices we will install to # Ask which harddrives/block-devices we will install to
# and convert them into archinstall.BlockDevice() objects. # and convert them into archinstall.BlockDevice() objects.
@ -99,17 +105,14 @@ def ask_user_questions():
if not archinstall.arguments.get('profile', None): if not archinstall.arguments.get('profile', None):
archinstall.arguments['profile'] = archinstall.select_profile(archinstall.list_profiles(filter_top_level_profiles=True)) archinstall.arguments['profile'] = archinstall.select_profile(archinstall.list_profiles(filter_top_level_profiles=True))
else: else:
archinstall.arguments['profile'] = archinstall.list_profiles()[archinstall.arguments['profile']] archinstall.arguments['profile'] = Profile(installer=None, path=archinstall.arguments['profile'])
# Check the potentially selected profiles preparations to get early checks if some additional questions are needed. # Check the potentially selected profiles preparations to get early checks if some additional questions are needed.
if archinstall.arguments['profile'] and archinstall.arguments['profile'].has_prep_function(): if archinstall.arguments['profile'] and archinstall.arguments['profile'].has_prep_function():
with archinstall.arguments['profile'].load_instructions(namespace=f"{archinstall.arguments['profile'].namespace}.py") as imported: with archinstall.arguments['profile'].load_instructions(namespace=f"{archinstall.arguments['profile'].namespace}.py") as imported:
if not imported._prep_function(): if not imported._prep_function():
archinstall.log( archinstall.log(' * Profile\'s preparation requirements was not fulfilled.', fg='red')
' * Profile\'s preparation requirements was not fulfilled.',
fg='red'
)
exit(1) exit(1)
@ -140,12 +143,12 @@ def ask_user_questions():
if len(archinstall.arguments['packages']): if len(archinstall.arguments['packages']):
# Verify packages that were given # Verify packages that were given
try: try:
archinstall.log(f"Verifying that additional packages exist (this might take a few seconds)") archinstall.log("Verifying that additional packages exist (this might take a few seconds)")
archinstall.validate_package_list(archinstall.arguments['packages']) archinstall.validate_package_list(archinstall.arguments['packages'])
break break
except archinstall.RequirementError as e: except archinstall.RequirementError as e:
archinstall.log(e, fg='red') archinstall.log(e, fg='red')
archinstall.arguments['packages'] = None # Clear the packages to trigger a new input question archinstall.arguments['packages'] = None # Clear the packages to trigger a new input question
else: else:
# no additional packages were selected, which we'll allow # no additional packages were selected, which we'll allow
break break
@ -154,7 +157,7 @@ def ask_user_questions():
if not archinstall.arguments.get('nic', None): if not archinstall.arguments.get('nic', None):
archinstall.arguments['nic'] = archinstall.ask_to_configure_network() archinstall.arguments['nic'] = archinstall.ask_to_configure_network()
if not archinstall.arguments['nic']: if not archinstall.arguments['nic']:
archinstall.log(f"No network configuration was selected. Network is going to be unavailable until configured manually!", fg="yellow") archinstall.log("No network configuration was selected. Network is going to be unavailable until configured manually!", fg="yellow")
if not archinstall.arguments.get('timezone', None): if not archinstall.arguments.get('timezone', None):
archinstall.arguments['timezone'] = archinstall.ask_for_a_timezone() archinstall.arguments['timezone'] = archinstall.ask_for_a_timezone()
@ -167,7 +170,8 @@ def perform_filesystem_operations():
archinstall.log(json.dumps(archinstall.arguments, indent=4, sort_keys=True, cls=archinstall.JSON), level=logging.INFO) archinstall.log(json.dumps(archinstall.arguments, indent=4, sort_keys=True, cls=archinstall.JSON), level=logging.INFO)
print() print()
input('Press Enter to continue.') if not archinstall.arguments.get('silent'):
input('Press Enter to continue.')
""" """
Issue a final warning before we continue with something un-revertable. Issue a final warning before we continue with something un-revertable.
@ -183,9 +187,8 @@ def perform_filesystem_operations():
Once that's done, we'll hand over to perform_installation() Once that's done, we'll hand over to perform_installation()
""" """
mode = archinstall.GPT mode = archinstall.GPT
if hasUEFI() is False: if has_uefi() is False:
mode = archinstall.MBR mode = archinstall.MBR
with archinstall.Filesystem(archinstall.arguments['harddrive'], mode) as fs: with archinstall.Filesystem(archinstall.arguments['harddrive'], mode) as fs:
# Wipe the entire drive if the disk flag `keep_partitions`is False. # Wipe the entire drive if the disk flag `keep_partitions`is False.
if archinstall.arguments['harddrive'].keep_partitions is False: if archinstall.arguments['harddrive'].keep_partitions is False:
@ -209,8 +212,6 @@ def perform_filesystem_operations():
partition.format() partition.format()
else: else:
archinstall.log(f"Did not format {partition} because .safe_to_format() returned False or .allow_formatting was False.", level=logging.DEBUG) archinstall.log(f"Did not format {partition} because .safe_to_format() returned False or .allow_formatting was False.", level=logging.DEBUG)
if hasUEFI():
fs.find_partition('/boot').format('vfat')# we don't have a boot partition in bios mode
if archinstall.arguments.get('!encryption-password', None): if archinstall.arguments.get('!encryption-password', None):
# First encrypt and unlock, then format the desired partition inside the encrypted part. # First encrypt and unlock, then format the desired partition inside the encrypted part.
@ -218,12 +219,15 @@ def perform_filesystem_operations():
# unlocks the drive so that it can be used as a normal block-device within archinstall. # unlocks the drive so that it can be used as a normal block-device within archinstall.
with archinstall.luks2(fs.find_partition('/'), 'luksloop', archinstall.arguments.get('!encryption-password', None)) as unlocked_device: with archinstall.luks2(fs.find_partition('/'), 'luksloop', archinstall.arguments.get('!encryption-password', None)) as unlocked_device:
unlocked_device.format(fs.find_partition('/').filesystem) unlocked_device.format(fs.find_partition('/').filesystem)
unlocked_device.mount('/mnt') unlocked_device.mount(archinstall.storage.get('MOUNT_POINT', '/mnt'))
else: else:
fs.find_partition('/').format(fs.find_partition('/').filesystem) fs.find_partition('/').mount(archinstall.storage.get('MOUNT_POINT', '/mnt'))
fs.find_partition('/').mount('/mnt')
if hasUEFI(): if has_uefi():
fs.find_partition('/boot').mount('/mnt/boot') fs.find_partition('/boot').mount(archinstall.storage.get('MOUNT_POINT', '/mnt') + '/boot')
perform_installation(archinstall.storage.get('MOUNT_POINT', '/mnt'))
def perform_installation(mountpoint): def perform_installation(mountpoint):
""" """
@ -232,30 +236,29 @@ def perform_installation(mountpoint):
formatted and setup prior to entering this function. formatted and setup prior to entering this function.
""" """
with archinstall.Installer(mountpoint, kernels=archinstall.arguments.get('kernels', 'linux')) as installation: with archinstall.Installer(mountpoint, kernels=archinstall.arguments.get('kernels', 'linux')) as installation:
## if len(mirrors): # if len(mirrors):
# Certain services might be running that affects the system during installation. # Certain services might be running that affects the system during installation.
# Currently, only one such service is "reflector.service" which updates /etc/pacman.d/mirrorlist # Currently, only one such service is "reflector.service" which updates /etc/pacman.d/mirrorlist
# We need to wait for it before we continue since we opted in to use a custom mirror/region. # We need to wait for it before we continue since we opted in to use a custom mirror/region.
installation.log(f'Waiting for automatic mirror selection (reflector) to complete.', level=logging.INFO) installation.log('Waiting for automatic mirror selection (reflector) to complete.', level=logging.INFO)
while archinstall.service_state('reflector') not in ('dead', 'failed'): while archinstall.service_state('reflector') not in ('dead', 'failed'):
time.sleep(1) time.sleep(1)
# Set mirrors used by pacstrap (outside of installation) # Set mirrors used by pacstrap (outside of installation)
if archinstall.arguments.get('mirror-region', None): if archinstall.arguments.get('mirror-region', None):
archinstall.use_mirrors(archinstall.arguments['mirror-region']) # Set the mirrors for the live medium archinstall.use_mirrors(archinstall.arguments['mirror-region']) # Set the mirrors for the live medium
if installation.minimal_installation(): if installation.minimal_installation():
installation.set_hostname(archinstall.arguments['hostname']) installation.set_hostname(archinstall.arguments['hostname'])
if archinstall.arguments['mirror-region'].get("mirrors",{})!= None: if archinstall.arguments['mirror-region'].get("mirrors", None) is not None:
installation.set_mirrors(archinstall.arguments['mirror-region']) # Set the mirrors in the installation medium installation.set_mirrors(archinstall.arguments['mirror-region']) # Set the mirrors in the installation medium
if archinstall.arguments["bootloader"]=="grub-install" and hasUEFI()==True: if archinstall.arguments["bootloader"] == "grub-install" and has_uefi():
installation.add_additional_packages("grub") installation.add_additional_packages("grub")
installation.set_keyboard_language(archinstall.arguments['keyboard-language'])
installation.add_bootloader(archinstall.arguments["bootloader"]) installation.add_bootloader(archinstall.arguments["bootloader"])
# If user selected to copy the current ISO network configuration # If user selected to copy the current ISO network configuration
# Perform a copy of the config # Perform a copy of the config
if archinstall.arguments.get('nic', {}) == 'Copy ISO network configuration to installation': if archinstall.arguments.get('nic', {}) == 'Copy ISO network configuration to installation':
installation.copy_ISO_network_config(enable_services=True) # Sources the ISO network configuration to the install medium. installation.copy_iso_network_config(enable_services=True) # Sources the ISO network configuration to the install medium.
elif archinstall.arguments.get('nic', {}).get('NetworkManager',False): elif archinstall.arguments.get('nic', {}).get('NetworkManager', False):
installation.add_additional_packages("networkmanager") installation.add_additional_packages("networkmanager")
installation.enable_service('NetworkManager.service') installation.enable_service('NetworkManager.service')
# Otherwise, if a interface was selected, configure that interface # Otherwise, if a interface was selected, configure that interface
@ -264,7 +267,7 @@ def perform_installation(mountpoint):
installation.enable_service('systemd-networkd') installation.enable_service('systemd-networkd')
installation.enable_service('systemd-resolved') installation.enable_service('systemd-resolved')
if archinstall.arguments.get('audio', None) != None: if archinstall.arguments.get('audio', None) is not None:
installation.log(f"This audio server will be used: {archinstall.arguments.get('audio', None)}", level=logging.INFO) installation.log(f"This audio server will be used: {archinstall.arguments.get('audio', None)}", level=logging.INFO)
if archinstall.arguments.get('audio', None) == 'pipewire': if archinstall.arguments.get('audio', None) == 'pipewire':
print('Installing pipewire ...') print('Installing pipewire ...')
@ -288,31 +291,59 @@ def perform_installation(mountpoint):
for superuser, user_info in archinstall.arguments.get('superusers', {}).items(): for superuser, user_info in archinstall.arguments.get('superusers', {}).items():
installation.user_create(superuser, user_info["!password"], sudo=True) installation.user_create(superuser, user_info["!password"], sudo=True)
if (timezone := archinstall.arguments.get('timezone', None)): if timezone := archinstall.arguments.get('timezone', None):
installation.set_timezone(timezone) installation.set_timezone(timezone)
if (root_pw := archinstall.arguments.get('!root-password', None)) and len(root_pw): if (root_pw := archinstall.arguments.get('!root-password', None)) and len(root_pw):
installation.user_set_pw('root', root_pw) installation.user_set_pw('root', root_pw)
# This step must be after profile installs to allow profiles to install language pre-requisits.
# After which, this step will set the language both for console and x11 if x11 was installed for instance.
installation.set_keyboard_language(archinstall.arguments['keyboard-language'])
if archinstall.arguments['profile'] and archinstall.arguments['profile'].has_post_install(): if archinstall.arguments['profile'] and archinstall.arguments['profile'].has_post_install():
with archinstall.arguments['profile'].load_instructions(namespace=f"{archinstall.arguments['profile'].namespace}.py") as imported: with archinstall.arguments['profile'].load_instructions(namespace=f"{archinstall.arguments['profile'].namespace}.py") as imported:
if not imported._post_install(): if not imported._post_install():
archinstall.log( archinstall.log(' * Profile\'s post configuration requirements was not fulfilled.', fg='red')
' * Profile\'s post configuration requirements was not fulfilled.',
fg='red'
)
exit(1) exit(1)
# If the user provided custom commands to be run post-installation, execute them now.
if archinstall.arguments.get('custom-commands', None):
run_custom_user_commands(archinstall.arguments['custom-commands'], installation)
installation.log("For post-installation tips, see https://wiki.archlinux.org/index.php/Installation_guide#Post-installation", fg="yellow") installation.log("For post-installation tips, see https://wiki.archlinux.org/index.php/Installation_guide#Post-installation", fg="yellow")
choice = input("Would you like to chroot into the newly created installation and perform post-installation configuration? [Y/n] ") if not archinstall.arguments.get('silent'):
if choice.lower() in ("y", ""): choice = input("Would you like to chroot into the newly created installation and perform post-installation configuration? [Y/n] ")
try: if choice.lower() in ("y", ""):
installation.drop_to_shell() try:
except: installation.drop_to_shell()
pass except:
pass
# For support reasons, we'll log the disk layout post installation (crash or no crash) # For support reasons, we'll log the disk layout post installation (crash or no crash)
archinstall.log(f"Disk states after installing: {archinstall.disk_layouts()}", level=archinstall.LOG_LEVELS.Debug) archinstall.log(f"Disk states after installing: {archinstall.disk_layouts()}", level=logging.DEBUG)
if not check_mirror_reachable():
log_file = os.path.join(archinstall.storage.get('LOG_PATH', None), archinstall.storage.get('LOG_FILE', None))
archinstall.log(f"Arch Linux mirrors are not reachable. Please check your internet connection and the log file '{log_file}'.", level=logging.INFO, fg="red")
exit(1)
if archinstall.arguments.get('silent', None) is None:
ask_user_questions()
else:
# Workarounds if config is loaded from a file
# The harddrive section should be moved to perform_installation_steps, where it's actually being performed
# Blockdevice object should be created in perform_installation_steps
# This needs to be done until then
archinstall.arguments['harddrive'] = archinstall.BlockDevice(path=archinstall.arguments['harddrive']['path'])
# Temporarily disabling keep_partitions if config file is loaded
archinstall.arguments['harddrive'].keep_partitions = False
# Temporary workaround to make Desktop Environments work
if archinstall.arguments.get('profile', None) is not None:
archinstall.arguments['profile'] = archinstall.Profile(None, archinstall.arguments.get('profile', None))
else:
archinstall.arguments['profile'] = None
ask_user_questions() ask_user_questions()
perform_filesystem_operations() perform_filesystem_operations()

View File

@ -1,16 +1,17 @@
import archinstall import archinstall
# Select a harddrive and a disk password # Select a harddrive and a disk password
archinstall.log(f"Minimal only supports:") archinstall.log("Minimal only supports:")
archinstall.log(f" * Being installed to a single disk") archinstall.log(" * Being installed to a single disk")
if archinstall.arguments.get('help', None): if archinstall.arguments.get('help', None):
archinstall.log(f" - Optional disk encryption via --!encryption-password=<password>") archinstall.log(" - Optional disk encryption via --!encryption-password=<password>")
archinstall.log(f" - Optional filesystem type via --filesystem=<fs type>") archinstall.log(" - Optional filesystem type via --filesystem=<fs type>")
archinstall.log(f" - Optional systemd network via --network") archinstall.log(" - Optional systemd network via --network")
archinstall.arguments['harddrive'] = archinstall.select_disk(archinstall.all_disks()) archinstall.arguments['harddrive'] = archinstall.select_disk(archinstall.all_disks())
def install_on(mountpoint): def install_on(mountpoint):
# We kick off the installer by telling it where the # We kick off the installer by telling it where the
with archinstall.Installer(mountpoint) as installation: with archinstall.Installer(mountpoint) as installation:
@ -22,7 +23,7 @@ def install_on(mountpoint):
# Optionally enable networking: # Optionally enable networking:
if archinstall.arguments.get('network', None): if archinstall.arguments.get('network', None):
installation.copy_ISO_network_config(enable_services=True) installation.copy_iso_network_config(enable_services=True)
installation.add_additional_packages(['nano', 'wget', 'git']) installation.add_additional_packages(['nano', 'wget', 'git'])
installation.install_profile('minimal') installation.install_profile('minimal')
@ -32,9 +33,10 @@ def install_on(mountpoint):
# Once this is done, we output some useful information to the user # Once this is done, we output some useful information to the user
# And the installation is complete. # And the installation is complete.
archinstall.log(f"There are two new accounts in your installation after reboot:") archinstall.log("There are two new accounts in your installation after reboot:")
archinstall.log(f" * root (password: airoot)") archinstall.log(" * root (password: airoot)")
archinstall.log(f" * devel (password: devel)") archinstall.log(" * devel (password: devel)")
if archinstall.arguments['harddrive']: if archinstall.arguments['harddrive']:
archinstall.arguments['harddrive'].keep_partitions = False archinstall.arguments['harddrive'].keep_partitions = False

View File

@ -1,6 +1,7 @@
import archinstall
import time import time
import archinstall
archinstall.storage['UPSTREAM_URL'] = 'https://archlinux.life/profiles' archinstall.storage['UPSTREAM_URL'] = 'https://archlinux.life/profiles'
archinstall.storage['PROFILE_DB'] = 'index.json' archinstall.storage['PROFILE_DB'] = 'index.json'
@ -10,7 +11,7 @@ for name, info in archinstall.list_profiles().items():
# that fits the requirements for this machine specifically). # that fits the requirements for this machine specifically).
if info['tailored']: if info['tailored']:
print(f'Found a tailored profile for this machine called: "{name}".') print(f'Found a tailored profile for this machine called: "{name}".')
print(f'Starting install in:') print('Starting install in:')
for i in range(10, 0, -1): for i in range(10, 0, -1):
print(f'{i}...') print(f'{i}...')
time.sleep(1) time.sleep(1)

0
mypy.ini Normal file
View File

View File

@ -1,11 +1,12 @@
import archinstall import archinstall
import json
import urllib.request # import json
# import urllib.request
__packages__ = ['nano', 'wget', 'git'] __packages__ = ['nano', 'wget', 'git']
if __name__ == '52-54-00-12-34-56': if __name__ == '52-54-00-12-34-56':
awesome = archinstall.Application(installation, 'postgresql') awesome = archinstall.Application(archinstall.storage['installation_session'], 'postgresql')
awesome.install() awesome.install()
""" """
@ -22,7 +23,7 @@ with archinstall.Filesystem(harddrive) as fs:
fs.use_entire_disk('luks2') fs.use_entire_disk('luks2')
if harddrive.partition[1].size == '512M': if harddrive.partition[1].size == '512M':
raise OSError('Trying to encrypt the boot partition for petes sake..') raise OSError('Trying to encrypt the boot partition for Pete's sake..')
harddrive.partition[0].format('fat32') harddrive.partition[0].format('fat32')
with archinstall.luks2(harddrive.partition[1], 'luksloop', disk_password) as unlocked_device: with archinstall.luks2(harddrive.partition[1], 'luksloop', disk_password) as unlocked_device:

0
profiles/__init__.py Normal file
View File

View File

View File

@ -1,12 +1,22 @@
import archinstall import archinstall
__packages__ = ["awesome", "xorg-xrandr", "xterm", "feh", "slock", "terminus-font", "gnu-free-fonts", "ttf-liberation", "xsel"] __packages__ = [
"awesome",
"xorg-xrandr",
"xterm",
"feh",
"slock",
"terminus-font",
"gnu-free-fonts",
"ttf-liberation",
"xsel",
]
installation.install_profile('xorg') archinstall.storage['installation_session'].install_profile('xorg')
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
with open(f'{installation.target}/etc/X11/xinit/xinitrc', 'r') as xinitrc: with open(f"{archinstall.storage['installation_session'].target}/etc/X11/xinit/xinitrc", 'r') as xinitrc:
xinitrc_data = xinitrc.read() xinitrc_data = xinitrc.read()
for line in xinitrc_data.split('\n'): for line in xinitrc_data.split('\n'):
@ -20,5 +30,5 @@ for line in xinitrc_data.split('\n'):
xinitrc_data += '\n' xinitrc_data += '\n'
xinitrc_data += 'exec awesome\n' xinitrc_data += 'exec awesome\n'
with open(f'{installation.target}/etc/X11/xinit/xinitrc', 'w') as xinitrc: with open(f"{archinstall.storage['installation_session'].target}/etc/X11/xinit/xinitrc", 'w') as xinitrc:
xinitrc.write(xinitrc_data) xinitrc.write(xinitrc_data)

View File

@ -2,8 +2,12 @@ import archinstall
# Define the package list in order for lib to source # Define the package list in order for lib to source
# which packages will be installed by this profile # which packages will be installed by this profile
__packages__ = ["cockpit", "udisks2", "packagekit"] __packages__ = [
"cockpit",
"udisks2",
"packagekit",
]
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('cockpit.socket') archinstall.storage['installation_session'].enable_service('cockpit.socket')

View File

@ -4,6 +4,6 @@ import archinstall
# which packages will be installed by this profile # which packages will be installed by this profile
__packages__ = ["docker"] __packages__ = ["docker"]
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('docker') archinstall.storage['installation_session'].enable_service('docker')

View File

@ -4,6 +4,6 @@ import archinstall
# which packages will be installed by this profile # which packages will be installed by this profile
__packages__ = ["apache"] __packages__ = ["apache"]
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('httpd') archinstall.storage['installation_session'].enable_service('httpd')

View File

@ -4,6 +4,6 @@ import archinstall
# which packages will be installed by this profile # which packages will be installed by this profile
__packages__ = ["lighttpd"] __packages__ = ["lighttpd"]
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('lighttpd') archinstall.storage['installation_session'].enable_service('lighttpd')

View File

@ -4,8 +4,8 @@ import archinstall
# which packages will be installed by this profile # which packages will be installed by this profile
__packages__ = ["mariadb"] __packages__ = ["mariadb"]
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.arch_chroot("mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql") archinstall.storage['installation_session'].arch_chroot("mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql")
installation.enable_service('mariadb') archinstall.storage['installation_session'].enable_service('mariadb')

View File

@ -4,6 +4,6 @@ import archinstall
# which packages will be installed by this profile # which packages will be installed by this profile
__packages__ = ["nginx"] __packages__ = ["nginx"]
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('nginx') archinstall.storage['installation_session'].enable_service('nginx')

View File

@ -4,8 +4,8 @@ import archinstall
# which packages will be installed by this profile # which packages will be installed by this profile
__packages__ = ["postgresql"] __packages__ = ["postgresql"]
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.arch_chroot("initdb -D /var/lib/postgres/data", runas='postgres') archinstall.storage['installation_session'].arch_chroot("initdb -D /var/lib/postgres/data", runas='postgres')
installation.enable_service('postgresql') archinstall.storage['installation_session'].enable_service('postgresql')

View File

@ -4,6 +4,6 @@ import archinstall
# which packages will be installed by this profile # which packages will be installed by this profile
__packages__ = ["openssh"] __packages__ = ["openssh"]
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('sshd') archinstall.storage['installation_session'].enable_service('sshd')

View File

@ -7,6 +7,6 @@ import archinstall
# which packages will be installed by this profile # which packages will be installed by this profile
__packages__ = ["tomcat10"] __packages__ = ["tomcat10"]
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('tomcat10') archinstall.storage['installation_session'].enable_service('tomcat10')

View File

@ -6,7 +6,13 @@ is_top_level_profile = False
# New way of defining packages for a profile, which is iterable and can be used out side # New way of defining packages for a profile, which is iterable and can be used out side
# of the profile to get a list of "what packages will be installed". # of the profile to get a list of "what packages will be installed".
__packages__ = ['nemo', 'gpicview-gtk3', 'main', 'alacritty'] __packages__ = [
"nemo",
"gpicview",
"maim",
"alacritty",
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -30,23 +36,23 @@ def _prep_function(*args, **kwargs):
# or through conventional import awesome # or through conventional import awesome
if __name__ == 'awesome': if __name__ == 'awesome':
# Install the application awesome from the template under /applications/ # Install the application awesome from the template under /applications/
awesome = archinstall.Application(installation, 'awesome') awesome = archinstall.Application(archinstall.storage['installation_session'], 'awesome')
awesome.install() awesome.install()
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
# TODO: Copy a full configuration to ~/.config/awesome/rc.lua instead. # TODO: Copy a full configuration to ~/.config/awesome/rc.lua instead.
with open(f'{installation.target}/etc/xdg/awesome/rc.lua', 'r') as fh: with open(f"{archinstall.storage['installation_session'].target}/etc/xdg/awesome/rc.lua", 'r') as fh:
awesome_lua = fh.read() awesome_lua = fh.read()
## Replace xterm with alacritty for a smoother experience. # Replace xterm with alacritty for a smoother experience.
awesome_lua = awesome_lua.replace('"xterm"', '"alacritty"') awesome_lua = awesome_lua.replace('"xterm"', '"alacritty"')
with open(f'{installation.target}/etc/xdg/awesome/rc.lua', 'w') as fh: with open(f"{archinstall.storage['installation_session'].target}/etc/xdg/awesome/rc.lua", 'w') as fh:
fh.write(awesome_lua) fh.write(awesome_lua)
## TODO: Configure the right-click-menu to contain the above packages that were installed. (as a user config) # TODO: Configure the right-click-menu to contain the above packages that were installed. (as a user config)
## Remove some interfering nemo settings # Remove some interfering nemo settings
installation.arch_chroot("gsettings set org.nemo.desktop show-desktop-icons false") archinstall.storage['installation_session'].arch_chroot("gsettings set org.nemo.desktop show-desktop-icons false")
installation.arch_chroot("xdg-mime default nemo.desktop inode/directory application/x-gnome-saved-search") archinstall.storage['installation_session'].arch_chroot("xdg-mime default nemo.desktop inode/directory application/x-gnome-saved-search")

View File

@ -5,7 +5,13 @@ import archinstall
is_top_level_profile = False is_top_level_profile = False
# "It is recommended also to install the gnome group, which contains applications required for the standard GNOME experience." - Arch Wiki # "It is recommended also to install the gnome group, which contains applications required for the standard GNOME experience." - Arch Wiki
__packages__ = ["budgie-desktop", "lightdm", "lightdm-gtk-greeter", "gnome"] __packages__ = [
"budgie-desktop",
"gnome",
"lightdm",
"lightdm-gtk-greeter",
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -23,14 +29,15 @@ def _prep_function(*args, **kwargs):
else: else:
print('Deprecated (??): xorg profile has no _prep_function() anymore') print('Deprecated (??): xorg profile has no _prep_function() anymore')
# Ensures that this code only gets executed if executed # Ensures that this code only gets executed if executed
# through importlib.util.spec_from_file_location("budgie", "/somewhere/budgie.py") # through importlib.util.spec_from_file_location("budgie", "/somewhere/budgie.py")
# or through conventional import budgie # or through conventional import budgie
if __name__ == 'budgie': if __name__ == 'budgie':
# Install dependency profiles # Install dependency profiles
installation.install_profile('xorg') archinstall.storage['installation_session'].install_profile('xorg')
# Install the Budgie packages # Install the Budgie packages
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('lightdm') # Light Display Manager archinstall.storage['installation_session'].enable_service('lightdm') # Light Display Manager

View File

@ -4,7 +4,17 @@ import archinstall
is_top_level_profile = False is_top_level_profile = False
__packages__ = ["cinnamon", "system-config-printer", "gnome-keyring", "gnome-terminal", "blueberry", "metacity", "lightdm", "lightdm-gtk-greeter"] __packages__ = [
"cinnamon",
"system-config-printer",
"gnome-keyring",
"gnome-terminal",
"blueberry",
"metacity",
"lightdm",
"lightdm-gtk-greeter",
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -22,14 +32,15 @@ def _prep_function(*args, **kwargs):
else: else:
print('Deprecated (??): xorg profile has no _prep_function() anymore') print('Deprecated (??): xorg profile has no _prep_function() anymore')
# Ensures that this code only gets executed if executed # Ensures that this code only gets executed if executed
# through importlib.util.spec_from_file_location("cinnamon", "/somewhere/cinnamon.py") # through importlib.util.spec_from_file_location("cinnamon", "/somewhere/cinnamon.py")
# or through conventional import cinnamon # or through conventional import cinnamon
if __name__ == 'cinnamon': if __name__ == 'cinnamon':
# Install dependency profiles # Install dependency profiles
installation.install_profile('xorg') archinstall.storage['installation_session'].install_profile('xorg')
# Install the Cinnamon packages # Install the Cinnamon packages
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('lightdm') # Light Display Manager archinstall.storage['installation_session'].enable_service('lightdm') # Light Display Manager

View File

@ -1,10 +1,17 @@
# A desktop environment using "Deepin". # A desktop environment using "Deepin".
import archinstall, os import archinstall
is_top_level_profile = False is_top_level_profile = False
__packages__ = ["deepin", "deepin-terminal", "deepin-editor"] __packages__ = [
"deepin",
"deepin-terminal",
"deepin-editor",
"lightdm",
"lightdm-gtk-greeter",
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -28,10 +35,10 @@ def _prep_function(*args, **kwargs):
# or through conventional import deepin # or through conventional import deepin
if __name__ == 'deepin': if __name__ == 'deepin':
# Install dependency profiles # Install dependency profiles
installation.install_profile('xorg') archinstall.storage['installation_session'].install_profile('xorg')
# Install the Deepin packages # Install the Deepin packages
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
# Enable autostart of Deepin for all users # Enable autostart of Deepin for all users
installation.enable_service('lightdm') archinstall.storage['installation_session'].enable_service('lightdm')

View File

@ -1,12 +1,24 @@
# A desktop environment selector. # A desktop environment selector.
import archinstall, os import archinstall
is_top_level_profile = True is_top_level_profile = True
# New way of defining packages for a profile, which is iterable and can be used out side # New way of defining packages for a profile, which is iterable and can be used out side
# of the profile to get a list of "what packages will be installed". # of the profile to get a list of "what packages will be installed".
__packages__ = ['nano', 'vim', 'openssh', 'htop', 'wget', 'iwd', 'wireless_tools', 'wpa_supplicant', 'smartmontools', 'xdg-utils'] __packages__ = [
'nano',
'vim',
'openssh',
'htop',
'wget',
'iwd',
'wireless_tools',
'wpa_supplicant',
'smartmontools',
'xdg-utils',
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -16,14 +28,28 @@ def _prep_function(*args, **kwargs):
for more input before any other installer steps start. for more input before any other installer steps start.
""" """
supported_desktops = ['gnome', 'kde', 'awesome', 'sway', 'cinnamon', 'xfce4', 'lxqt', 'i3', 'budgie', 'mate'] supported_desktops = [
desktop = archinstall.generic_select(supported_desktops, 'Select your desired desktop environment: ', 'gnome',
allow_empty_input=False, sort=True) 'kde',
'awesome',
'sway',
'cinnamon',
'xfce4',
'lxqt',
'i3',
'budgie',
'mate',
'deepin',
'enlightenment',
]
desktop = archinstall.generic_select(supported_desktops, 'Select your desired desktop environment: ', allow_empty_input=False, sort=True)
# Temporarily store the selected desktop profile # Temporarily store the selected desktop profile
# in a session-safe location, since this module will get reloaded # in a session-safe location, since this module will get reloaded
# the next time it gets executed. # the next time it gets executed.
archinstall.storage['_desktop_profile'] = desktop if '_desktop_profile' not in archinstall.storage.keys():
archinstall.storage['_desktop_profile'] = desktop
profile = archinstall.Profile(None, desktop) profile = archinstall.Profile(None, desktop)
# Loading the instructions with a custom namespace, ensures that a __name__ comparison is never triggered. # Loading the instructions with a custom namespace, ensures that a __name__ comparison is never triggered.
@ -33,6 +59,7 @@ def _prep_function(*args, **kwargs):
else: else:
print(f"Deprecated (??): {desktop} profile has no _prep_function() anymore") print(f"Deprecated (??): {desktop} profile has no _prep_function() anymore")
if __name__ == 'desktop': if __name__ == 'desktop':
""" """
This "profile" is a meta-profile. This "profile" is a meta-profile.
@ -48,9 +75,6 @@ if __name__ == 'desktop':
""" """
# Install common packages for all desktop environments # Install common packages for all desktop environments
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
# TODO: Remove magic variable 'installation' and place it
# in archinstall.storage or archinstall.session/archinstall.installation
installation.install_profile(archinstall.storage['_desktop_profile'])
archinstall.storage['installation_session'].install_profile(archinstall.storage['_desktop_profile'])

43
profiles/enlightenment.py Normal file
View File

@ -0,0 +1,43 @@
# A desktop environment using "Enlightenment".
import archinstall
is_top_level_profile = False
__packages__ = [
"enlightenment",
"terminology",
"lightdm",
"lightdm-gtk-greeter",
]
def _prep_function(*args, **kwargs):
"""
Magic function called by the importing installer
before continuing any further. It also avoids executing any
other code in this stage. So it's a safe way to ask the user
for more input before any other installer steps start.
"""
# Enlightenment requires a functioning Xorg installation.
profile = archinstall.Profile(None, 'xorg')
with profile.load_instructions(namespace='xorg.py') as imported:
if hasattr(imported, '_prep_function'):
return imported._prep_function()
else:
print('Deprecated (??): xorg profile has no _prep_function() anymore')
# Ensures that this code only gets executed if executed
# through importlib.util.spec_from_file_location("enlightenment", "/somewhere/enlightenment.py")
# or through conventional import enlightenment
if __name__ == 'enlightenment':
# Install dependency profiles
archinstall.storage['installation_session'].install_profile('xorg')
# Install the enlightenment packages
archinstall.storage['installation_session'].add_additional_packages(__packages__)
# Enable autostart of enlightenment for all users
archinstall.storage['installation_session'].enable_service('lightdm')

View File

@ -5,7 +5,12 @@ import archinstall
is_top_level_profile = False is_top_level_profile = False
# Note: GDM should be part of the gnome group, but adding it here for clarity # Note: GDM should be part of the gnome group, but adding it here for clarity
__packages__ = ["gnome", "gnome-tweaks", "gdm"] __packages__ = [
"gnome",
"gnome-tweaks",
"gdm",
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -24,16 +29,17 @@ def _prep_function(*args, **kwargs):
else: else:
print('Deprecated (??): xorg profile has no _prep_function() anymore') print('Deprecated (??): xorg profile has no _prep_function() anymore')
# Ensures that this code only gets executed if executed # Ensures that this code only gets executed if executed
# through importlib.util.spec_from_file_location("gnome", "/somewhere/gnome.py") # through importlib.util.spec_from_file_location("gnome", "/somewhere/gnome.py")
# or through conventional import gnome # or through conventional import gnome
if __name__ == 'gnome': if __name__ == 'gnome':
# Install dependency profiles # Install dependency profiles
installation.install_profile('xorg') archinstall.storage['installation_session'].install_profile('xorg')
# Install the GNOME packages # Install the GNOME packages
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('gdm') # Gnome Display Manager archinstall.storage['installation_session'].enable_service('gdm') # Gnome Display Manager
# We could also start it via xinitrc since we do have Xorg, # We could also start it via xinitrc since we do have Xorg,
# but for gnome that's deprecated and wayland is preferred. # but for gnome that's deprecated and wayland is preferred.

View File

@ -1,12 +1,21 @@
# Common package for i3, lets user select which i3 configuration they want. # Common package for i3, lets user select which i3 configuration they want.
import archinstall, os import archinstall
is_top_level_profile = False is_top_level_profile = False
# New way of defining packages for a profile, which is iterable and can be used out side # New way of defining packages for a profile, which is iterable and can be used out side
# of the profile to get a list of "what packages will be installed". # of the profile to get a list of "what packages will be installed".
__packages__ = ['i3lock', 'i3status', 'i3blocks', 'xterm', 'lightdm-gtk-greeter', 'lightdm'] __packages__ = [
'i3lock',
'i3status',
'i3blocks',
'xterm',
'lightdm-gtk-greeter',
'lightdm',
'dmenu',
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -17,8 +26,7 @@ def _prep_function(*args, **kwargs):
""" """
supported_configurations = ['i3-wm', 'i3-gaps'] supported_configurations = ['i3-wm', 'i3-gaps']
desktop = archinstall.generic_select(supported_configurations, 'Select your desired configuration: ', desktop = archinstall.generic_select(supported_configurations, 'Select your desired configuration: ', allow_empty_input=False, sort=True)
allow_empty_input=False, sort=True)
# Temporarily store the selected desktop profile # Temporarily store the selected desktop profile
# in a session-safe location, since this module will get reloaded # in a session-safe location, since this module will get reloaded
@ -33,6 +41,7 @@ def _prep_function(*args, **kwargs):
else: else:
print('Deprecated (??): xorg profile has no _prep_function() anymore') print('Deprecated (??): xorg profile has no _prep_function() anymore')
if __name__ == 'i3': if __name__ == 'i3':
""" """
This "profile" is a meta-profile. This "profile" is a meta-profile.
@ -48,16 +57,16 @@ if __name__ == 'i3':
""" """
# Install common packages for all i3 configurations # Install common packages for all i3 configurations
installation.add_additional_packages(__packages__[:4]) archinstall.storage['installation_session'].add_additional_packages(__packages__[:4])
# Install dependency profiles # Install dependency profiles
installation.install_profile('xorg') archinstall.storage['installation_session'].install_profile('xorg')
# gaps is installed by deafult so we are overriding it here with lightdm # gaps is installed by default so we are overriding it here with lightdm
installation.add_additional_packages(__packages__[4:]) archinstall.storage['installation_session'].add_additional_packages(__packages__[4:])
# Auto start lightdm for all users # Auto start lightdm for all users
installation.enable_service('lightdm') archinstall.storage['installation_session'].enable_service('lightdm')
# install the i3 group now # install the i3 group now
installation.add_additional_packages(archinstall.storage['_i3_configuration']) archinstall.storage['installation_session'].add_additional_packages(archinstall.storage['_i3_configuration'])

View File

@ -1,13 +1,23 @@
# A desktop environment using "KDE". # A desktop environment using "KDE".
import archinstall, os import archinstall
is_top_level_profile = False is_top_level_profile = False
__packages__ = ["plasma-meta", "konsole", "kate", "dolphin", "sddm", "plasma-wayland-session", "egl-wayland"] __packages__ = [
"plasma-meta",
"konsole",
"kate",
"dolphin",
"sddm",
"plasma-wayland-session",
"egl-wayland",
]
# TODO: Remove hard dependency of bash (due to .bash_profile) # TODO: Remove hard dependency of bash (due to .bash_profile)
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
Magic function called by the importing installer Magic function called by the importing installer
@ -24,6 +34,7 @@ def _prep_function(*args, **kwargs):
else: else:
print('Deprecated (??): xorg profile has no _prep_function() anymore') print('Deprecated (??): xorg profile has no _prep_function() anymore')
""" """
def _post_install(*args, **kwargs): def _post_install(*args, **kwargs):
if "nvidia" in _gfx_driver_packages: if "nvidia" in _gfx_driver_packages:
@ -37,10 +48,10 @@ def _post_install(*args, **kwargs):
# or through conventional import kde # or through conventional import kde
if __name__ == 'kde': if __name__ == 'kde':
# Install dependency profiles # Install dependency profiles
installation.install_profile('xorg') archinstall.storage['installation_session'].install_profile('xorg')
# Install the KDE packages # Install the KDE packages
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
# Enable autostart of KDE for all users # Enable autostart of KDE for all users
installation.enable_service('sddm') archinstall.storage['installation_session'].enable_service('sddm')

View File

@ -1,11 +1,23 @@
# A desktop environment using "LXQt" # A desktop environment using "LXQt"
import archinstall import archinstall
is_top_level_profile = False is_top_level_profile = False
__packages__ = ["lxqt", "breeze-icons", "oxygen-icons", "xdg-utils", "ttf-freefont", "leafpad", "slock", "sddm"] # NOTE: SDDM is the only officially supported greeter for LXQt, so unlike other DEs, lightdm is not used here.
# LXQt works with lightdm, but since this is not supported, we will not default to this.
# https://github.com/lxqt/lxqt/issues/795
__packages__ = [
"lxqt",
"breeze-icons",
"oxygen-icons",
"xdg-utils",
"ttf-freefont",
"leafpad",
"slock",
"sddm",
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -23,14 +35,16 @@ def _prep_function(*args, **kwargs):
else: else:
print('Deprecated (??): xorg profile has no _prep_function() anymore') print('Deprecated (??): xorg profile has no _prep_function() anymore')
# Ensures that this code only gets executed if executed # Ensures that this code only gets executed if executed
# through importlib.util.spec_from_file_location("lxqt", "/somewhere/lxqt.py") # through importlib.util.spec_from_file_location("lxqt", "/somewhere/lxqt.py")
# or through conventional import lxqt # or through conventional import lxqt
if __name__ == 'lxqt': if __name__ == 'lxqt':
# Install dependency profiles # Install dependency profiles
installation.install_profile('xorg') archinstall.storage['installation_session'].install_profile('xorg')
# Install the LXQt packages # Install the LXQt packages
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('sddm') # SDDM Display Manager # Enable autostart of LXQt for all users
archinstall.storage['installation_session'].enable_service('sddm')

View File

@ -4,7 +4,13 @@ import archinstall
is_top_level_profile = False is_top_level_profile = False
__packages__ = ["mate", "mate-extra", "lightdm", "lightdm-gtk-greeter"] __packages__ = [
"mate",
"mate-extra",
"lightdm",
"lightdm-gtk-greeter",
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -22,14 +28,15 @@ def _prep_function(*args, **kwargs):
else: else:
print('Deprecated (??): xorg profile has no _prep_function() anymore') print('Deprecated (??): xorg profile has no _prep_function() anymore')
# Ensures that this code only gets executed if executed # Ensures that this code only gets executed if executed
# through importlib.util.spec_from_file_location("mate", "/somewhere/mate.py") # through importlib.util.spec_from_file_location("mate", "/somewhere/mate.py")
# or through conventional import mate # or through conventional import mate
if __name__ == 'mate': if __name__ == 'mate':
# Install dependency profiles # Install dependency profiles
installation.install_profile('xorg') archinstall.storage['installation_session'].install_profile('xorg')
# Install the MATE packages # Install the MATE packages
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('lightdm') # Light Display Manager archinstall.storage['installation_session'].enable_service('lightdm') # Light Display Manager

View File

@ -1,9 +1,8 @@
# Used to do a minimal install # Used to do a minimal install
import archinstall, os
is_top_level_profile = True is_top_level_profile = True
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
Magic function called by the importing installer Magic function called by the importing installer
@ -11,7 +10,8 @@ def _prep_function(*args, **kwargs):
we don't need to do anything special here, but it we don't need to do anything special here, but it
needs to exist and return True. needs to exist and return True.
""" """
return True # Do nothing and just return True return True # Do nothing and just return True
if __name__ == 'minimal': if __name__ == 'minimal':
""" """

View File

@ -1,30 +1,44 @@
# Used to select various server application profiles on top of a minimal installation. # Used to select various server application profiles on top of a minimal installation.
import archinstall, os, logging import logging
import archinstall
is_top_level_profile = True is_top_level_profile = True
available_servers = ["cockpit", "docker", "httpd", "lighttpd", "mariadb", "nginx", "postgresql", "sshd", "tomcat"] available_servers = [
"cockpit",
"docker",
"httpd",
"lighttpd",
"mariadb",
"nginx",
"postgresql",
"sshd",
"tomcat",
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
Magic function called by the importing installer Magic function called by the importing installer
before continuing any further. before continuing any further.
""" """
selected_servers = archinstall.generic_multi_select(available_servers, f"Choose which servers to install and enable (leave blank for a minimal installation): ") selected_servers = archinstall.generic_multi_select(available_servers, "Choose which servers to install and enable (leave blank for a minimal installation): ")
archinstall.storage['_selected_servers'] = selected_servers archinstall.storage['_selected_servers'] = selected_servers
return True return True
if __name__ == 'server': if __name__ == 'server':
""" """
This "profile" is a meta-profile. This "profile" is a meta-profile.
""" """
archinstall.log(f'Now installing the selected servers.', level=logging.INFO) archinstall.log('Now installing the selected servers.', level=logging.INFO)
archinstall.log(archinstall.storage['_selected_servers'], level=logging.DEBUG) archinstall.log(archinstall.storage['_selected_servers'], level=logging.DEBUG)
for server in archinstall.storage['_selected_servers']: for server in archinstall.storage['_selected_servers']:
archinstall.log(f'Installing {server} ...', level=logging.INFO) archinstall.log(f'Installing {server} ...', level=logging.INFO)
app = archinstall.Application(installation, server) app = archinstall.Application(archinstall.storage['installation_session'], server)
app.install() app.install()
archinstall.log('If your selections included multiple servers with the same port, you may have to reconfigure them.', fg="yellow", level=logging.INFO) archinstall.log('If your selections included multiple servers with the same port, you may have to reconfigure them.', fg="yellow", level=logging.INFO)

View File

@ -4,7 +4,19 @@ import archinstall
is_top_level_profile = False is_top_level_profile = False
__packages__ = ["sway", "swaylock", "swayidle", "waybar", "dmenu", "light", "grim", "slurp", "pavucontrol", "alacritty"] __packages__ = [
"sway",
"swaylock",
"swayidle",
"waybar",
"dmenu",
"light",
"grim",
"slurp",
"pavucontrol",
"alacritty",
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -13,18 +25,22 @@ def _prep_function(*args, **kwargs):
other code in this stage. So it's a safe way to ask the user other code in this stage. So it's a safe way to ask the user
for more input before any other installer steps start. for more input before any other installer steps start.
""" """
__builtins__["_gfx_driver_packages"] = archinstall.select_driver()
return True
# Ensures that this code only gets executed if executed
# through importlib.util.spec_from_file_location("sway", "/somewhere/sway.py")
# or through conventional import sway
if __name__ == "sway":
if "nvidia" in _gfx_driver_packages: if "nvidia" in _gfx_driver_packages:
choice = input("The proprietary Nvidia driver is not supported by Sway. It is likely that you will run into issues. Continue anyways? [y/N] ") choice = input("The proprietary Nvidia driver is not supported by Sway. It is likely that you will run into issues. Continue anyways? [y/N] ")
if choice.lower() in ("n", ""): if choice.lower() in ("n", ""):
raise archinstall.lib.exceptions.HardwareIncompatibilityError("Sway does not support the proprietary nvidia drivers.") raise archinstall.lib.exceptions.HardwareIncompatibilityError("Sway does not support the proprietary nvidia drivers.")
__builtins__['_gfx_driver_packages'] = archinstall.select_driver()
return True
# Ensures that this code only gets executed if executed
# through importlib.util.spec_from_file_location("sway", "/somewhere/sway.py")
# or through conventional import sway
if __name__ == 'sway':
# Install the Sway packages # Install the Sway packages
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
# Install the graphics driver packages
archinstall.storage['installation_session'].add_additional_packages(_gfx_driver_packages)

View File

@ -1,11 +1,17 @@
# A desktop environment using "Xfce4" # A desktop environment using "Xfce4"
import archinstall import archinstall
is_top_level_profile = False is_top_level_profile = False
__packages__ = ["xfce4", "xfce4-goodies", "lightdm", "lightdm-gtk-greeter"] __packages__ = [
"xfce4",
"xfce4-goodies",
"pavucontrol",
"lightdm",
"lightdm-gtk-greeter",
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -23,14 +29,15 @@ def _prep_function(*args, **kwargs):
else: else:
print('Deprecated (??): xorg profile has no _prep_function() anymore') print('Deprecated (??): xorg profile has no _prep_function() anymore')
# Ensures that this code only gets executed if executed # Ensures that this code only gets executed if executed
# through importlib.util.spec_from_file_location("xfce4", "/somewhere/xfce4.py") # through importlib.util.spec_from_file_location("xfce4", "/somewhere/xfce4.py")
# or through conventional import xfce4 # or through conventional import xfce4
if __name__ == 'xfce4': if __name__ == 'xfce4':
# Install dependency profiles # Install dependency profiles
installation.install_profile('xorg') archinstall.storage['installation_session'].install_profile('xorg')
# Install the XFCE4 packages # Install the XFCE4 packages
installation.add_additional_packages(__packages__) archinstall.storage['installation_session'].add_additional_packages(__packages__)
installation.enable_service('lightdm') # Light Display Manager archinstall.storage['installation_session'].enable_service('lightdm') # Light Display Manager

View File

@ -1,11 +1,18 @@
# A system with "xorg" installed # A system with "xorg" installed
import os
import archinstall import archinstall
is_top_level_profile = True is_top_level_profile = True
__packages__ = ['dkms', 'xorg-server', 'xorg-xinit', 'nvidia-dkms', 'xorg-server', *archinstall.lib.hardware.__packages__] __packages__ = [
'dkms',
'xorg-server',
'xorg-xinit',
'nvidia-dkms',
'xorg-server',
*archinstall.lib.hardware.__packages__,
]
def _prep_function(*args, **kwargs): def _prep_function(*args, **kwargs):
""" """
@ -22,28 +29,19 @@ def _prep_function(*args, **kwargs):
return True return True
# Ensures that this code only gets executed if executed # Ensures that this code only gets executed if executed
# through importlib.util.spec_from_file_location("xorg", "/somewhere/xorg.py") # through importlib.util.spec_from_file_location("xorg", "/somewhere/xorg.py")
# or through conventional import xorg # or through conventional import xorg
if __name__ == 'xorg': if __name__ == 'xorg':
try: try:
if "nvidia" in _gfx_driver_packages: if "nvidia" in _gfx_driver_packages:
if "linux-zen" in installation.base_packages or "linux-lts" in installation.base_packages: if "linux-zen" in archinstall.storage['installation_session'].base_packages or "linux-lts" in archinstall.storage['installation_session'].base_packages:
installation.add_additional_packages("dkms")#I've had kernel regen fail if it wasn't installed before nvidia-dkms archinstall.storage['installation_session'].add_additional_packages("dkms") # I've had kernel regen fail if it wasn't installed before nvidia-dkms
installation.add_additional_packages("xorg-server xorg-xinit nvidia-dkms") archinstall.storage['installation_session'].add_additional_packages("xorg-server xorg-xinit nvidia-dkms")
else: else:
installation.add_additional_packages(f"xorg-server xorg-xinit {' '.join(_gfx_driver_packages)}") archinstall.storage['installation_session'].add_additional_packages(f"xorg-server xorg-xinit {' '.join(_gfx_driver_packages)}")
else: else:
installation.add_additional_packages(f"xorg-server xorg-xinit {' '.join(_gfx_driver_packages)}") archinstall.storage['installation_session'].add_additional_packages(f"xorg-server xorg-xinit {' '.join(_gfx_driver_packages)}")
except: except:
installation.add_additional_packages(f"xorg-server xorg-xinit") # Prep didn't run, so there's no driver to install archinstall.storage['installation_session'].add_additional_packages("xorg-server xorg-xinit") # Prep didn't run, so there's no driver to install
# with open(f'{installation.mountpoint}/etc/X11/xinit/xinitrc', 'a') as X11:
# X11.write('setxkbmap se\n')
# with open(f'{installation.mountpoint}/etc/vconsole.conf', 'a') as vconsole:
# vconsole.write('KEYMAP={keyboard_layout}\n'.format(**arguments))
# vconsole.write('FONT=lat9w-16\n')
# awesome = archinstall.Application(installation, 'awesome')
# awesome.install()

View File

@ -1,2 +1,3 @@
import setuptools import setuptools # type: ignore
setuptools.setup() setuptools.setup()